Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tarea #710 - Al borrar contacto desvincularlo de clientes y proveedores #1633

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions Core/Model/Contacto.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,19 @@ public function url(string $type = 'auto', string $list = 'ListCliente?activetab
{
return parent::url($type, $list);
}

public function delete(): bool
{
$deleted = parent::delete();

if ($deleted){
$sql = 'UPDATE ' . Cliente::tableName() . ' SET idcontactoenv = NULL WHERE idcontactoenv = ' . $this->idcontacto . ';';
$sql .= 'UPDATE ' . Cliente::tableName() . ' SET idcontactofact = NULL WHERE idcontactofact = ' . $this->idcontacto . ';';
$sql .= 'UPDATE ' . Proveedor::tableName() . ' SET idcontacto = NULL WHERE idcontacto = ' . $this->idcontacto;

return self::$dataBase->exec($sql);
}

return $deleted;
}
}
69 changes: 69 additions & 0 deletions Test/Core/Model/ClienteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

use FacturaScripts\Core\Lib\Vies;
use FacturaScripts\Core\Model\Cliente;
use FacturaScripts\Core\Model\Contacto;
use FacturaScripts\Core\Model\Proveedor;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -210,6 +212,73 @@ public function testVies(): void
$this->assertTrue($cliente->delete());
}

/**
* Comprobamos que al eliminar un contacto,
* se desvincula de todos los clientes y proveedores.
*/
public function testUnlinkCustomerContact()
{
// creamos un contacto
$contacto = new Contacto();
$contacto->nombre = 'Test Contacto';
$this->assertTrue($contacto->save(), 'contact-cant-save');

// creamos un cliente y vinculamos al contacto
$cliente = new Cliente();
$cliente->nombre = 'Test';
$cliente->cifnif = '12345677A';
$cliente->idcontactoenv = $contacto->idcontacto;
$cliente->idcontactofact = $contacto->idcontacto;
$this->assertTrue($cliente->save(), 'cliente-cant-save');

// creamos otro cliente y vinculamos al contacto
$cliente2 = new Cliente();
$cliente2->nombre = 'Test 2';
$cliente2->cifnif = '12345678A';
$cliente2->idcontactoenv = $contacto->idcontacto;
$cliente2->idcontactofact = $contacto->idcontacto;
$this->assertTrue($cliente2->save(), 'cliente-cant-save');

// creamos un proveedor y vinculamos al contacto
$proveedor = new Proveedor();
$proveedor->nombre = 'Test 3';
$proveedor->cifnif = '12345679A';
$proveedor->idcontacto = $contacto->idcontacto;
$this->assertTrue($proveedor->save(), 'proveedor-cant-save');


// comprobamos que el contacto creado se encuentra asociado
$this->assertEquals($cliente->idcontactoenv, $contacto->idcontacto);
$this->assertEquals($cliente->idcontactofact, $contacto->idcontacto);

$this->assertEquals($cliente2->idcontactoenv, $contacto->idcontacto);
$this->assertEquals($cliente2->idcontactofact, $contacto->idcontacto);

$this->assertEquals($proveedor->idcontacto, $contacto->idcontacto);

// eliminamos contacto
$this->assertTrue($contacto->delete(), 'contact-cant-delete');

// refrescamos los datos desde la base de datos.
$cliente->loadFromCode($cliente->codcliente);
$cliente2->loadFromCode($cliente2->codcliente);
$proveedor->loadFromCode($proveedor->codproveedor);

// comprobamos que el contacto creado, NO se encuentra asociado
$this->assertNotEquals($cliente->idcontactoenv, $contacto->idcontacto);
$this->assertNotEquals($cliente->idcontactofact, $contacto->idcontacto);

$this->assertNotEquals($cliente2->idcontactoenv, $contacto->idcontacto);
$this->assertNotEquals($cliente2->idcontactofact, $contacto->idcontacto);

$this->assertNotEquals($proveedor->idcontacto, $contacto->idcontacto);

// eliminamos
$this->assertTrue($cliente->delete());
$this->assertTrue($cliente2->delete());
$this->assertTrue($proveedor->delete());
}

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