Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esto debería ser un OR no un AND, ya que puede cambiar el teléfono 1 pero no el 2, si alguno de los dos cambia actualizamos los 2, o haces dos comprobaciones diferentes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He separado las comprobaciones de telefono 1 y 2 para asi cubrir todos los casos:
Modificas solo teléfono 1 → al cambiar de cliente, teléfono 1 se mantiene (tu valor manual), teléfono 2 se actualiza al del nuevo cliente.
Modificas teléfono 1 y teléfono 2 → ninguno cambia, se preservan ambos valores manuales.
Modificas solo teléfono 2 → teléfono 2 se mantiene, teléfono 1 se actualiza al del nuevo cliente.
Si no modificas ninguno → ambos se actualizan al nuevo cliente.

$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