Skip to content

Commit f7cff40

Browse files
committed
Add supplier delivery note stock adjustment test
1 parent 5244ad0 commit f7cff40

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

Test/Core/Model/AlbaranProveedorTest.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
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\AlbaranProveedor;
2625
use FacturaScripts\Core\Model\Almacen;
2726
use FacturaScripts\Core\Model\Empresa;
27+
use FacturaScripts\Core\Model\Stock;
2828
use FacturaScripts\Core\Tools;
29+
use FacturaScripts\Core\Where;
2930
use FacturaScripts\Test\Traits\DefaultSettingsTrait;
3031
use FacturaScripts\Test\Traits\LogErrorsTrait;
3132
use FacturaScripts\Test\Traits\RandomDataTrait;
@@ -247,6 +248,84 @@ public function testCreateProductLine(): void
247248
$this->assertTrue($product->delete(), 'can-not-delete-product-3');
248249
}
249250

251+
public function testApproveAfterManualStockChangeKeepsFinalStock(): void
252+
{
253+
// creamos un proveedor
254+
$subject = $this->getRandomSupplier();
255+
$this->assertTrue($subject->save(), 'can-not-save-supplier-4');
256+
257+
// creamos un producto
258+
$product = $this->getRandomProduct();
259+
$this->assertTrue($product->save(), 'can-not-save-product-4');
260+
261+
// creamos un albarán
262+
$doc = new AlbaranProveedor();
263+
$doc->setSubject($subject);
264+
$this->assertTrue($doc->save(), 'can-not-create-albaran-proveedor-4');
265+
266+
// añadimos el producto con 10 unidades
267+
$line = $doc->getNewProductLine($product->referencia);
268+
$line->cantidad = 10;
269+
$line->pvpunitario = 10;
270+
$this->assertTrue($line->save(), 'can-not-save-line-4');
271+
272+
// comprobamos que el albarán ha sumado stock
273+
$product->reload();
274+
$this->assertEquals(10, $product->stockfis, 'albaran-proveedor-product-do-not-update-stock-4');
275+
276+
$stock = new Stock();
277+
$where = [
278+
Where::eq('codalmacen', $doc->codalmacen),
279+
Where::eq('referencia', $product->referencia),
280+
];
281+
$this->assertTrue($stock->loadWhere($where), 'can-not-load-stock-4');
282+
$this->assertEquals(10, $stock->cantidad, 'stock-not-created-from-delivery-note-4');
283+
284+
// simulamos una corrección manual del stock a 4 unidades
285+
$stock->cantidad = 4;
286+
$this->assertTrue($stock->save(), 'can-not-update-stock-4');
287+
288+
$stock->reload();
289+
$product->reload();
290+
$this->assertEquals(4, $stock->cantidad, 'manual-stock-change-not-persist-4');
291+
$this->assertEquals(4, $product->stockfis, 'manual-product-stock-change-not-persist-4');
292+
293+
// aprobamos el albarán para generar la factura
294+
$statusFound = false;
295+
foreach ($doc->getAvailableStatus() as $status) {
296+
if (empty($status->generadoc)) {
297+
continue;
298+
}
299+
300+
$statusFound = true;
301+
$doc->idestado = $status->idestado;
302+
$this->assertTrue($doc->save(), 'can-not-approve-albaran-proveedor-4');
303+
break;
304+
}
305+
306+
$this->assertTrue($statusFound, 'no-status-with-document-generation-4');
307+
308+
// el stock final debe seguir siendo 4: el albarán deja de sumar y la factura lo vuelve a añadir
309+
$stock->reload();
310+
$product->reload();
311+
$this->assertEquals(4, $stock->cantidad, 'bad-final-stock-after-approve-4');
312+
$this->assertEquals(4, $product->stockfis, 'bad-final-product-stock-after-approve-4');
313+
314+
// comprobamos que la factura se ha generado
315+
$children = $doc->childrenDocuments();
316+
$this->assertNotEmpty($children, 'factura-no-generada-4');
317+
318+
// eliminamos
319+
foreach ($children as $child) {
320+
$this->assertTrue($child->delete(), 'factura-cant-delete-4');
321+
}
322+
$this->assertTrue($doc->delete(), 'can-not-delete-albaran-proveedor-4');
323+
$this->assertFalse($line->exists(), 'linea-albaran-proveedor-still-exists-4');
324+
$this->assertTrue($subject->getDefaultAddress()->delete(), 'contacto-cant-delete');
325+
$this->assertTrue($subject->delete(), 'can-not-delete-proveedor-4');
326+
$this->assertTrue($product->delete(), 'can-not-delete-product-4');
327+
}
328+
250329
public function testPropertiesLength(): void
251330
{
252331
// creamos un proveedor
@@ -294,7 +373,7 @@ public function testSecondCompany(): void
294373

295374
// obtenemos el almacén de la empresa 2
296375
$warehouse = new Almacen();
297-
$where = [new DataBaseWhere('idempresa', $company2->idempresa)];
376+
$where = [Where::eq('idempresa', $company2->idempresa)];
298377
$warehouse->loadWhere($where);
299378

300379
// creamos un proveedor

0 commit comments

Comments
 (0)