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
20 changes: 19 additions & 1 deletion Core/Model/Base/TransformerDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ abstract class TransformerDocument extends BusinessDocument
*/
public $idestado;

/**
* Document previous status, from the EstadoDocumento model.
*
* @var int
*/
public $idestadoanterior;

/**
* Campos que se pueden modificar aunque el documento no sea editable.
*
* @var array
*/
private static $unlocked_fields = ['femail', 'idestado', 'numdocs', 'pagada'];
private static $unlocked_fields = ['femail', 'idestado', 'idestadoanterior', 'numdocs', 'pagada'];

/**
* Adds a field to the list of unlocked fields (editable even when document is not editable).
Expand Down Expand Up @@ -176,6 +183,14 @@ public function delete(): bool

// change parent doc status
foreach ($parents as $parent) {
// si tiene idestadoanterior lo asignamos
if(false === empty($parent->idestadoanterior)){
$parent->idestado = $parent->idestadoanterior;
$parent->save();
continue;
}

// si no tiene idestadoanterior asignamos el default como se hacia antes
foreach ($parent->getAvailableStatus() as $status) {
if ($status->predeterminado) {
$parent->idestado = $status->idestado;
Expand Down Expand Up @@ -339,6 +354,9 @@ protected function onChange(string $field): bool
return parent::onChange($field);
}

// guardamos el estado anterior para poder revertirlo
$this->idestadoanterior = $this->getOriginal('idestado');

$status = $this->getStatus();
if (empty($status->generadoc) || false === self::$document_generation) {
// update lines to update stock
Expand Down
8 changes: 8 additions & 0 deletions Core/Table/albaranescli.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@
<name>idestado</name>
<type>integer</type>
</column>
<column>
<name>idestadoanterior</name>
<type>integer</type>
</column>
<column>
<name>irpf</name>
<type>double precision</type>
Expand Down Expand Up @@ -250,6 +254,10 @@
<name>ca_albaranescli_idestados</name>
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_albaranescli_idestadoanterior</name>
<type>FOREIGN KEY (idestadoanterior) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_albaranescli_nick</name>
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
Expand Down
8 changes: 8 additions & 0 deletions Core/Table/albaranesprov.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
<name>idestado</name>
<type>integer</type>
</column>
<column>
<name>idestadoanterior</name>
<type>integer</type>
</column>
<column>
<name>irpf</name>
<type>double precision</type>
Expand Down Expand Up @@ -191,6 +195,10 @@
<name>ca_albaranesprov_idestado</name>
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_albaranesprov_idestadoanterior</name>
<type>FOREIGN KEY (idestadoanterior) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_albaranesprov_nick</name>
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
Expand Down
8 changes: 8 additions & 0 deletions Core/Table/facturascli.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
<name>idestado</name>
<type>integer</type>
</column>
<column>
<name>idestadoanterior</name>
<type>integer</type>
</column>
<column>
<name>idfactura</name>
<type>serial</type>
Expand Down Expand Up @@ -281,6 +285,10 @@
<name>ca_facturascli_idestado</name>
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_facturascli_idestadoanterior</name>
<type>FOREIGN KEY (idestadoanterior) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_facturascli_idfacturarect</name>
<type>FOREIGN KEY (idfacturarect) REFERENCES facturascli (idfactura) ON DELETE RESTRICT ON UPDATE CASCADE</type>
Expand Down
8 changes: 8 additions & 0 deletions Core/Table/facturasprov.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
<name>idestado</name>
<type>integer</type>
</column>
<column>
<name>idestadoanterior</name>
<type>integer</type>
</column>
<column>
<name>idfactura</name>
<type>serial</type>
Expand Down Expand Up @@ -221,6 +225,10 @@
<name>ca_facturasprov_idestado</name>
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_facturasprov_idestadoanterior</name>
<type>FOREIGN KEY (idestadoanterior) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_facturasprov_idfacturarect</name>
<type>FOREIGN KEY (idfacturarect) REFERENCES facturasprov (idfactura) ON DELETE RESTRICT ON UPDATE CASCADE</type>
Expand Down
8 changes: 8 additions & 0 deletions Core/Table/pedidoscli.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@
<name>idestado</name>
<type>integer</type>
</column>
<column>
<name>idestadoanterior</name>
<type>integer</type>
</column>
<column>
<name>idpedido</name>
<type>serial</type>
Expand Down Expand Up @@ -254,6 +258,10 @@
<name>ca_pedidoscli_idestado</name>
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_pedidoscli_idestadoanterior</name>
<type>FOREIGN KEY (idestadoanterior) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_pedidoscli_nick</name>
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
Expand Down
8 changes: 8 additions & 0 deletions Core/Table/pedidosprov.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
<name>idestado</name>
<type>integer</type>
</column>
<column>
<name>idestadoanterior</name>
<type>integer</type>
</column>
<column>
<name>idpedido</name>
<type>serial</type>
Expand Down Expand Up @@ -190,6 +194,10 @@
<name>ca_pedidosprov_idestado</name>
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_pedidosprov_idestadoanterior</name>
<type>FOREIGN KEY (idestadoanterior) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_pedidosprov_nick</name>
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
Expand Down
8 changes: 8 additions & 0 deletions Core/Table/presupuestoscli.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
<name>idestado</name>
<type>integer</type>
</column>
<column>
<name>idestadoanterior</name>
<type>integer</type>
</column>
<column>
<name>idpresupuesto</name>
<type>serial</type>
Expand Down Expand Up @@ -253,6 +257,10 @@
<name>ca_presupuestoscli_idestado</name>
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_presupuestoscli_idestadoanterior</name>
<type>FOREIGN KEY (idestadoanterior) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_presupuestoscli_nick</name>
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
Expand Down
8 changes: 8 additions & 0 deletions Core/Table/presupuestosprov.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
<name>idestado</name>
<type>integer</type>
</column>
<column>
<name>idestadoanterior</name>
<type>integer</type>
</column>
<column>
<name>idpresupuesto</name>
<type>serial</type>
Expand Down Expand Up @@ -178,6 +182,10 @@
<name>ca_presupuestosprov_idestado</name>
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_presupuestosprov_idestadoanterior</name>
<type>FOREIGN KEY (idestadoanterior) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
</constraint>
<constraint>
<name>ca_presupuestosprov_codproveedor</name>
<type>FOREIGN KEY (codproveedor) REFERENCES proveedores (codproveedor) ON DELETE RESTRICT ON UPDATE CASCADE</type>
Expand Down
102 changes: 102 additions & 0 deletions Test/Core/Model/Base/TransformerDocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Base;

use FacturaScripts\Core\Model\EstadoDocumento;
use FacturaScripts\Core\Model\PedidoCliente;
use FacturaScripts\Dinamic\Lib\Calculator;
use FacturaScripts\Test\Traits\DefaultSettingsTrait;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use FacturaScripts\Test\Traits\RandomDataTrait;
use PHPUnit\Framework\TestCase;

class TransformerDocumentTest extends TestCase
{
use LogErrorsTrait;
use RandomDataTrait;
use DefaultSettingsTrait;

protected function setUp(): void
{
self::setDefaultSettings();
}

/**
* Restaurar estado anterior de documentos al desagrupar
*/
public function testRestoreDocumentState(): void
{
// creamos un presupuesto con estado NO PREDETERMINADO
$doc = $this->getRandomCustomerOrder();

// nos guardamos el estado original del pedido
$idEstadoOriginal = $doc->idestado;

// transformamos en albaran
$estadoGenerarAlbaran = $this->getEstadoGenerarAlbaran();
$doc->idestado = $estadoGenerarAlbaran->idestado;
$this->assertTrue($doc->save());

// eliminamos albaran
foreach ($doc->childrenDocuments() as $children_document) {
$this->assertTrue($children_document->delete());
}

// recargamos el pedido
$doc->reload();

// comprobamos que el estado del pedido es el de origen y no el predeterminado
$this->assertEquals($idEstadoOriginal, $doc->idestado);
}

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

private function getRandomCustomerOrder(): PedidoCliente
{
// creamos un estado NO PREDETERMINADO
$estadoAceptado = $this->getEstadoAceptado();

$subject = $this->getRandomCustomer();
$this->assertTrue($subject->save());

$doc = new PedidoCliente();
$doc->setSubject($subject);
$doc->idestado = $estadoAceptado->idestado;
$this->assertTrue($doc->save());

$line = $doc->getNewLine();
$line->cantidad = 1;
$line->pvpunitario = 100;
$this->assertTrue($line->save());

$lines = $doc->getLines();
Calculator::calculate($doc, $lines, true);

return $doc;
}

private function getEstadoAceptado(): EstadoDocumento
{
$estadoAceptado = new EstadoDocumento();
$estadoAceptado->nombre = 'Aceptado';
$estadoAceptado->tipodoc = 'PedidoCliente';
$estadoAceptado->generadoc = '';
$this->assertTrue($estadoAceptado->save());

return $estadoAceptado;
}

private function getEstadoGenerarAlbaran(): EstadoDocumento
{
foreach (EstadoDocumento::all() as $estado_documento) {
if ($estado_documento->tipodoc === 'PedidoCliente' && $estado_documento->generadoc === 'AlbaranCliente') {
return $estado_documento;
}
}

return new EstadoDocumento();
}
}
Loading