Skip to content

Commit a627128

Browse files
committed
Track previous document status
1 parent f7cff40 commit a627128

15 files changed

+143
-40
lines changed

Core/Model/Base/TransformerDocument.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2013-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2013-2026 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -19,8 +19,8 @@
1919

2020
namespace FacturaScripts\Core\Model\Base;
2121

22-
use FacturaScripts\Core\Where;
2322
use FacturaScripts\Core\Tools;
23+
use FacturaScripts\Core\Where;
2424
use FacturaScripts\Dinamic\Lib\BusinessDocumentGenerator;
2525
use FacturaScripts\Dinamic\Model\DocTransformation;
2626
use FacturaScripts\Dinamic\Model\EstadoDocumento;
@@ -58,12 +58,19 @@ abstract class TransformerDocument extends BusinessDocument
5858
*/
5959
public $idestado;
6060

61+
/**
62+
* Estado anterior del documento, del modelo EstadoDocumento.
63+
*
64+
* @var int|null
65+
*/
66+
public $idestado_ant;
67+
6168
/**
6269
* Campos que se pueden modificar aunque el documento no sea editable.
6370
*
6471
* @var array
6572
*/
66-
private static $unlocked_fields = ['femail', 'idestado', 'numdocs', 'pagada'];
73+
private static $unlocked_fields = ['femail', 'idestado', 'idestado_ant', 'numdocs', 'pagada'];
6774

6875
/**
6976
* Añade un campo a la lista de campos desbloqueados (editables aunque el documento no sea editable).
@@ -238,6 +245,21 @@ public function getStatus(): EstadoDocumento
238245
return $status;
239246
}
240247

248+
/**
249+
* Devuelve el modelo EstadoDocumento anterior de este documento.
250+
*
251+
* @return EstadoDocumento|null
252+
*/
253+
public function getPreviousStatus(): ?EstadoDocumento
254+
{
255+
if (empty($this->idestado_ant)) {
256+
return null;
257+
}
258+
259+
$status = new EstadoDocumento();
260+
return $status->load($this->idestado_ant) ? $status : null;
261+
}
262+
241263
/**
242264
* Devuelve la lista de campos desbloqueados (editables aunque el documento no sea editable).
243265
*
@@ -342,6 +364,9 @@ protected function onChange(string $field): bool
342364
return parent::onChange($field);
343365
}
344366

367+
// guardamos el estado anterior real antes del cambio
368+
$this->idestado_ant = $this->getOriginal('idestado');
369+
345370
$status = $this->getStatus();
346371
if (empty($status->generadoc) || false === self::$document_generation) {
347372
// actualizamos las líneas para actualizar el stock

Core/Table/albaranescli.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@
122122
<name>idestado</name>
123123
<type>integer</type>
124124
</column>
125+
<column>
126+
<name>idestado_ant</name>
127+
<type>integer</type>
128+
</column>
125129
<column>
126130
<name>irpf</name>
127131
<type>double precision</type>
@@ -250,6 +254,10 @@
250254
<name>ca_albaranescli_idestados</name>
251255
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
252256
</constraint>
257+
<constraint>
258+
<name>ca_albaranescli_idestado_ant</name>
259+
<type>FOREIGN KEY (idestado_ant) REFERENCES estados_documentos (idestado) ON DELETE SET NULL ON UPDATE CASCADE</type>
260+
</constraint>
253261
<constraint>
254262
<name>ca_albaranescli_nick</name>
255263
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>

Core/Table/albaranesprov.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
<name>idestado</name>
8383
<type>integer</type>
8484
</column>
85+
<column>
86+
<name>idestado_ant</name>
87+
<type>integer</type>
88+
</column>
8589
<column>
8690
<name>irpf</name>
8791
<type>double precision</type>
@@ -191,6 +195,10 @@
191195
<name>ca_albaranesprov_idestado</name>
192196
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
193197
</constraint>
198+
<constraint>
199+
<name>ca_albaranesprov_idestado_ant</name>
200+
<type>FOREIGN KEY (idestado_ant) REFERENCES estados_documentos (idestado) ON DELETE SET NULL ON UPDATE CASCADE</type>
201+
</constraint>
194202
<constraint>
195203
<name>ca_albaranesprov_nick</name>
196204
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
@@ -199,4 +207,4 @@
199207
<name>uniq_codigo_albaranesprov</name>
200208
<type>UNIQUE (codigo,idempresa)</type>
201209
</constraint>
202-
</table>
210+
</table>

Core/Table/facturascli.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@
129129
<name>idestado</name>
130130
<type>integer</type>
131131
</column>
132+
<column>
133+
<name>idestado_ant</name>
134+
<type>integer</type>
135+
</column>
132136
<column>
133137
<name>idfactura</name>
134138
<type>serial</type>
@@ -281,6 +285,10 @@
281285
<name>ca_facturascli_idestado</name>
282286
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
283287
</constraint>
288+
<constraint>
289+
<name>ca_facturascli_idestado_ant</name>
290+
<type>FOREIGN KEY (idestado_ant) REFERENCES estados_documentos (idestado) ON DELETE SET NULL ON UPDATE CASCADE</type>
291+
</constraint>
284292
<constraint>
285293
<name>ca_facturascli_idfacturarect</name>
286294
<type>FOREIGN KEY (idfacturarect) REFERENCES facturascli (idfactura) ON DELETE RESTRICT ON UPDATE CASCADE</type>
@@ -297,4 +305,4 @@
297305
<name>uniq_number_facturascli</name>
298306
<type>UNIQUE (codejercicio,codserie,idempresa,numero)</type>
299307
</constraint>
300-
</table>
308+
</table>

Core/Table/facturasprov.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@
8989
<name>idestado</name>
9090
<type>integer</type>
9191
</column>
92+
<column>
93+
<name>idestado_ant</name>
94+
<type>integer</type>
95+
</column>
9296
<column>
9397
<name>idfactura</name>
9498
<type>serial</type>
@@ -221,6 +225,10 @@
221225
<name>ca_facturasprov_idestado</name>
222226
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
223227
</constraint>
228+
<constraint>
229+
<name>ca_facturasprov_idestado_ant</name>
230+
<type>FOREIGN KEY (idestado_ant) REFERENCES estados_documentos (idestado) ON DELETE SET NULL ON UPDATE CASCADE</type>
231+
</constraint>
224232
<constraint>
225233
<name>ca_facturasprov_idfacturarect</name>
226234
<type>FOREIGN KEY (idfacturarect) REFERENCES facturasprov (idfactura) ON DELETE RESTRICT ON UPDATE CASCADE</type>
@@ -237,4 +245,4 @@
237245
<name>uniq_numero_facturasprov</name>
238246
<type>UNIQUE (codejercicio,codserie,idempresa,numero)</type>
239247
</constraint>
240-
</table>
248+
</table>

Core/Table/pedidoscli.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@
121121
<name>idestado</name>
122122
<type>integer</type>
123123
</column>
124+
<column>
125+
<name>idestado_ant</name>
126+
<type>integer</type>
127+
</column>
124128
<column>
125129
<name>idpedido</name>
126130
<type>serial</type>
@@ -254,6 +258,10 @@
254258
<name>ca_pedidoscli_idestado</name>
255259
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
256260
</constraint>
261+
<constraint>
262+
<name>ca_pedidoscli_idestado_ant</name>
263+
<type>FOREIGN KEY (idestado_ant) REFERENCES estados_documentos (idestado) ON DELETE SET NULL ON UPDATE CASCADE</type>
264+
</constraint>
257265
<constraint>
258266
<name>ca_pedidoscli_nick</name>
259267
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
@@ -262,4 +270,4 @@
262270
<name>uniq_codigo_pedidoscli</name>
263271
<type>UNIQUE (codigo,idempresa)</type>
264272
</constraint>
265-
</table>
273+
</table>

Core/Table/pedidosprov.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@
7777
<name>idestado</name>
7878
<type>integer</type>
7979
</column>
80+
<column>
81+
<name>idestado_ant</name>
82+
<type>integer</type>
83+
</column>
8084
<column>
8185
<name>idpedido</name>
8286
<type>serial</type>
@@ -190,6 +194,10 @@
190194
<name>ca_pedidosprov_idestado</name>
191195
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
192196
</constraint>
197+
<constraint>
198+
<name>ca_pedidosprov_idestado_ant</name>
199+
<type>FOREIGN KEY (idestado_ant) REFERENCES estados_documentos (idestado) ON DELETE SET NULL ON UPDATE CASCADE</type>
200+
</constraint>
193201
<constraint>
194202
<name>ca_pedidosprov_nick</name>
195203
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
@@ -198,4 +206,4 @@
198206
<name>uniq_codigo_pedidosprov</name>
199207
<type>UNIQUE (codigo,idempresa)</type>
200208
</constraint>
201-
</table>
209+
</table>

Core/Table/presupuestoscli.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@
120120
<name>idestado</name>
121121
<type>integer</type>
122122
</column>
123+
<column>
124+
<name>idestado_ant</name>
125+
<type>integer</type>
126+
</column>
123127
<column>
124128
<name>idpresupuesto</name>
125129
<type>serial</type>
@@ -253,6 +257,10 @@
253257
<name>ca_presupuestoscli_idestado</name>
254258
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
255259
</constraint>
260+
<constraint>
261+
<name>ca_presupuestoscli_idestado_ant</name>
262+
<type>FOREIGN KEY (idestado_ant) REFERENCES estados_documentos (idestado) ON DELETE SET NULL ON UPDATE CASCADE</type>
263+
</constraint>
256264
<constraint>
257265
<name>ca_presupuestoscli_nick</name>
258266
<type>FOREIGN KEY (nick) REFERENCES users (nick) ON DELETE SET NULL ON UPDATE CASCADE</type>
@@ -261,4 +269,4 @@
261269
<name>uniq_codigo_presupuestoscli</name>
262270
<type>UNIQUE (codigo,idempresa)</type>
263271
</constraint>
264-
</table>
272+
</table>

Core/Table/presupuestosprov.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@
7777
<name>idestado</name>
7878
<type>integer</type>
7979
</column>
80+
<column>
81+
<name>idestado_ant</name>
82+
<type>integer</type>
83+
</column>
8084
<column>
8185
<name>idpresupuesto</name>
8286
<type>serial</type>
@@ -178,6 +182,10 @@
178182
<name>ca_presupuestosprov_idestado</name>
179183
<type>FOREIGN KEY (idestado) REFERENCES estados_documentos (idestado) ON DELETE RESTRICT ON UPDATE CASCADE</type>
180184
</constraint>
185+
<constraint>
186+
<name>ca_presupuestosprov_idestado_ant</name>
187+
<type>FOREIGN KEY (idestado_ant) REFERENCES estados_documentos (idestado) ON DELETE SET NULL ON UPDATE CASCADE</type>
188+
</constraint>
181189
<constraint>
182190
<name>ca_presupuestosprov_codproveedor</name>
183191
<type>FOREIGN KEY (codproveedor) REFERENCES proveedores (codproveedor) ON DELETE RESTRICT ON UPDATE CASCADE</type>
@@ -194,4 +202,4 @@
194202
<name>uniq_codigo_presupuestosprov</name>
195203
<type>UNIQUE (codigo,idempresa)</type>
196204
</constraint>
197-
</table>
205+
</table>

Test/Core/Model/AlbaranClienteTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2017-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2017-2026 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -19,7 +19,6 @@
1919

2020
namespace FacturaScripts\Test\Core\Model;
2121

22-
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
2322
use FacturaScripts\Core\DataSrc\Impuestos;
2423
use FacturaScripts\Core\Lib\Calculator;
2524
use FacturaScripts\Core\Model\AlbaranCliente;
@@ -345,8 +344,7 @@ public function testSecondCompany(): void
345344

346345
// obtenemos el almacén de la empresa 2
347346
$warehouse = new Almacen();
348-
$where = [new DataBaseWhere('idempresa', $company2->idempresa)];
349-
$warehouse->loadWhere($where);
347+
$warehouse->loadWhereEq('idempresa', $company2->idempresa);
350348

351349
// creamos un cliente
352350
$subject = $this->getRandomCustomer();
@@ -371,8 +369,11 @@ public function testSecondCompany(): void
371369
}
372370

373371
// al cambiar el estado genera una nueva factura
372+
$previous = $doc->idestado;
374373
$doc->idestado = $status->idestado;
375374
$this->assertTrue($doc->save(), 'albaran-cant-save');
375+
$this->assertEquals($previous, $doc->idestado_ant, 'albaran-bad-previous-status');
376+
$this->assertEquals($previous, $doc->getPreviousStatus()->idestado, 'albaran-bad-previous-status-model');
376377

377378
// comprobamos que la factura se ha creado
378379
$children = $doc->childrenDocuments();

0 commit comments

Comments
 (0)