Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Model/ServicioAT.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,17 @@ public function test(): bool
$customer = $this->getSubject();
$this->telefono1 = $customer->telefono1;
$this->telefono2 = $customer->telefono2;
} elseif ($this->exists() && $this->codcliente != $this->getOriginal('codcliente')) {
// si cambiamos el cliente y los teléfonos no se han modificado manualmente,
// los actualizamos con los del nuevo cliente
$oldCustomer = $this->getCustomer($this->getOriginal('codcliente') ?? '');
$customer = $this->getSubject();
if ($this->telefono1 == $oldCustomer->telefono1) {
$this->telefono1 = $customer->telefono1;
}
if ($this->telefono2 == $oldCustomer->telefono2) {
$this->telefono2 = $customer->telefono2;
}
}

$fields = ['codigo', 'descripcion', 'material', 'observaciones', 'solucion', 'telefono1', 'telefono2'];
Expand Down
118 changes: 118 additions & 0 deletions Test/main/ServicioAtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,124 @@ public function testDefaultType(): void
$this->assertTrue($customer->delete());
}

public function testUpdatePhoneOnCustomerChange(): void
{
// creamos dos clientes con teléfonos distintos
$customer1 = $this->getRandomCustomer();
$customer1->telefono1 = '111111111';
$customer1->telefono2 = '222222222';
$this->assertTrue($customer1->save());

$customer2 = $this->getRandomCustomer();
$customer2->telefono1 = '333333333';
$customer2->telefono2 = '444444444';
$this->assertTrue($customer2->save());

// creamos un servicio con el primer cliente
$service = new ServicioAT();
$service->codalmacen = Tools::settings('default', 'codalmacen');
$service->codcliente = $customer1->codcliente;
$service->descripcion = 'Test service';
$service->idempresa = Tools::settings('default', 'idempresa');
$this->assertTrue($service->save());

// comprobamos que se ha rellenado con el teléfono del primer cliente
$this->assertEquals($customer1->telefono1, $service->telefono1);
$this->assertEquals($customer1->telefono2, $service->telefono2);

// cambiamos el cliente asignado
$service->codcliente = $customer2->codcliente;
$this->assertTrue($service->save());

// comprobamos que el teléfono se ha actualizado al del nuevo cliente
$this->assertEquals($customer2->telefono1, $service->telefono1);
$this->assertEquals($customer2->telefono2, $service->telefono2);

// eliminamos
$this->assertTrue($service->delete());
$this->assertTrue($customer1->delete());
$this->assertTrue($customer2->delete());
}

public function testKeepManualPhoneOnCustomerChange(): void
{
// creamos dos clientes con teléfonos distintos
$customer1 = $this->getRandomCustomer();
$customer1->telefono1 = '111111111';
$customer1->telefono2 = '222222222';
$this->assertTrue($customer1->save());

$customer2 = $this->getRandomCustomer();
$customer2->telefono1 = '333333333';
$customer2->telefono2 = '444444444';
$this->assertTrue($customer2->save());

// creamos un servicio con el primer cliente
$service = new ServicioAT();
$service->codalmacen = Tools::settings('default', 'codalmacen');
$service->codcliente = $customer1->codcliente;
$service->descripcion = 'Test service';
$service->idempresa = Tools::settings('default', 'idempresa');
$this->assertTrue($service->save());

// modificamos el teléfono manualmente
$service->telefono1 = '999999999';
$service->telefono2 = '888888888';
$this->assertTrue($service->save());

// cambiamos el cliente asignado
$service->codcliente = $customer2->codcliente;
$this->assertTrue($service->save());

// comprobamos que el teléfono manual no se ha sobrescrito
$this->assertEquals('999999999', $service->telefono1);
$this->assertEquals('888888888', $service->telefono2);

// eliminamos
$this->assertTrue($service->delete());
$this->assertTrue($customer1->delete());
$this->assertTrue($customer2->delete());
}

public function testUpdateOnlyUnmodifiedPhoneOnCustomerChange(): void
{
// creamos dos clientes con teléfonos distintos
$customer1 = $this->getRandomCustomer();
$customer1->telefono1 = '111111111';
$customer1->telefono2 = '222222222';
$this->assertTrue($customer1->save());

$customer2 = $this->getRandomCustomer();
$customer2->telefono1 = '333333333';
$customer2->telefono2 = '444444444';
$this->assertTrue($customer2->save());

// creamos un servicio con el primer cliente
$service = new ServicioAT();
$service->codalmacen = Tools::settings('default', 'codalmacen');
$service->codcliente = $customer1->codcliente;
$service->descripcion = 'Test service';
$service->idempresa = Tools::settings('default', 'idempresa');
$this->assertTrue($service->save());

// modificamos manualmente solo el teléfono 1
$service->telefono1 = '999999999';
$this->assertTrue($service->save());

// cambiamos el cliente asignado
$service->codcliente = $customer2->codcliente;
$this->assertTrue($service->save());

// el teléfono 1 modificado manualmente no cambia, el teléfono 2 se actualiza
$this->assertEquals('999999999', $service->telefono1);
$this->assertEquals($customer2->telefono2, $service->telefono2);

// eliminamos
$this->assertTrue($service->delete());
$this->assertTrue($customer1->delete());
$this->assertTrue($customer2->delete());
}

protected function tearDown(): void
{
$this->logErrors();
Expand Down