Skip to content
Open
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
9 changes: 9 additions & 0 deletions Model/ServicioAT.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ 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') ?? '');
if ($this->telefono1 == $oldCustomer->telefono1 && $this->telefono2 == $oldCustomer->telefono2) {
$customer = $this->getSubject();
$this->telefono1 = $customer->telefono1;
$this->telefono2 = $customer->telefono2;
}
}

$fields = ['codigo', 'descripcion', 'material', 'observaciones', 'solucion', 'telefono1', 'telefono2'];
Expand Down
79 changes: 79 additions & 0 deletions Test/main/ServicioAtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,85 @@ 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());
}

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