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
3 changes: 3 additions & 0 deletions Core/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public static function init(): void
Calculator::addMod(new CalculatorModSpain());

// workers
WorkQueue::addWorker('CostPriceWorker', 'Model.ProductoProveedor.Delete');
WorkQueue::addWorker('CostPriceWorker', 'Model.ProductoProveedor.Insert');
WorkQueue::addWorker('CostPriceWorker', 'Model.ProductoProveedor.Update');
WorkQueue::addWorker('CuentaWorker', 'Model.Cuenta.Delete');
WorkQueue::addWorker('CuentaWorker', 'Model.Cuenta.Update');
WorkQueue::addWorker('CuentaWorker', 'Model.Subcuenta.Delete');
Expand Down
32 changes: 0 additions & 32 deletions Core/Model/ProductoProveedor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use FacturaScripts\Core\Template\ModelClass;
use FacturaScripts\Core\Template\ModelTrait;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Lib\CostPriceTools;
use FacturaScripts\Dinamic\Model\Divisa as DinDivisa;
use FacturaScripts\Dinamic\Model\Producto as DinProducto;
use FacturaScripts\Dinamic\Model\Proveedor as DinProveedor;
Expand Down Expand Up @@ -192,35 +191,4 @@ public function url(string $type = 'auto', string $list = 'List'): string
return $this->getVariant()->url($type);
}

/**
* This method is called after a record is deleted on the database (delete).
*/
protected function onDelete(): void
{
CostPriceTools::update($this->getVariant());

parent::onDelete();
}

/**
* This method is called after a new record is saved on the database (saveInsert).
*/
protected function onInsert(): void
{
CostPriceTools::update($this->getVariant());

parent::onInsert();
}

/**
* This method is called after a record is updated on the database (saveUpdate).
*/
protected function onUpdate(): void
{
if ($this->isDirty('neto')) {
CostPriceTools::update($this->getVariant());
}

parent::onUpdate();
}
}
50 changes: 50 additions & 0 deletions Core/Worker/CostPriceWorker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2025 Pablo Aceituno <civernet@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace FacturaScripts\Core\Worker;

use FacturaScripts\Core\Model\WorkEvent;
use FacturaScripts\Core\Template\WorkerClass;
use FacturaScripts\Core\Where;
use FacturaScripts\Dinamic\Lib\CostPriceTools;
use FacturaScripts\Dinamic\Model\Variante;

class CostPriceWorker extends WorkerClass
{
public function run(WorkEvent $event): bool
{
// obtenemos la referencia del producto
$referencia = $event->param('referencia');
if (empty($referencia)) {
return $this->done();
}

// cargamos la variante
$variante = new Variante();
$where = [Where::eq('referencia', $referencia)];
if (false === $variante->loadWhere($where)) {
return $this->done();
}

// actualizamos el precio de coste
CostPriceTools::update($variante);

return $this->done();
}
}
22 changes: 22 additions & 0 deletions Test/Core/Model/ProductoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use FacturaScripts\Core\Model\Stock;
use FacturaScripts\Core\Model\Variante;
use FacturaScripts\Core\Tools;
use FacturaScripts\Core\WorkQueue;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -226,6 +227,9 @@ public function testCostPriceNoPolicy(): void
$supplierProduct->precio = 200;
$this->assertTrue($supplierProduct->save(), 'supplier-product-cant-save');

// procesamos la cola de trabajos
$this->processWorkQueue();

// recargamos la variante para comprobar que NO se ha actualizado el coste, ya que no hay política asignada
$variant->reload();
$this->assertEquals(66, $variant->coste, 'variant-cost-should-not-change');
Expand Down Expand Up @@ -274,6 +278,9 @@ public function testCostPricePolicyLastPrice(): void
$supplierProduct2->precio = 200;
$this->assertTrue($supplierProduct2->save(), 'supplier-product-2-cant-save');

// procesamos la cola de trabajos
$this->processWorkQueue();

// recargamos la variante para comprobar que SI se ha actualizado el coste
$variant->reload();
$this->assertEquals(200, $variant->coste, 'variant-cost-not-last');
Expand Down Expand Up @@ -325,6 +332,9 @@ public function testCostPricePolicyHighPrice(): void
$supplierProduct2->precio = 100;
$this->assertTrue($supplierProduct2->save(), 'supplier-product-2-cant-save');

// procesamos la cola de trabajos
$this->processWorkQueue();

// recargamos la variante para comprobar que SI se ha actualizado el coste
$variant->reload();
$this->assertEquals(200, $variant->coste, 'variant-cost-not-last');
Expand Down Expand Up @@ -375,6 +385,9 @@ public function testCostPricePolicyAveragePrice(): void
$supplierProduct2->precio = 200;
$this->assertTrue($supplierProduct2->save(), 'supplier-product-cant-save');

// procesamos la cola de trabajos
$this->processWorkQueue();

// recargamos la variante para comprobar que SI se ha actualizado el coste
$variant->reload();
$this->assertEquals(150, $variant->coste, 'variant-cost-not-average');
Expand Down Expand Up @@ -773,6 +786,15 @@ private function getTestSupplier(): Proveedor
return $supplier;
}

private function processWorkQueue(): void
{
while (true) {
if (false === WorkQueue::run()) {
break;
}
}
}

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