Skip to content

Commit 8cabc54

Browse files
author
Daniel Fernández Giménez
committed
Mejora la gestión de totales en ProjectTotalManager y añade pruebas para agrupaciones de estimaciones en albaranes
1 parent 65e0ae8 commit 8cabc54

File tree

2 files changed

+118
-9
lines changed

2 files changed

+118
-9
lines changed

Lib/ProjectTotalManager.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ protected static function purchaseDeliveryNotes(int $idproyecto): float
8787
}
8888
}
8989

90-
$total += ($delivery->total - $totalHijos);
90+
// si el hijo tiene un total mayor que el padre, restamos solo el total del padre
91+
$total += max(0.0, $delivery->total - $totalHijos);
9192
}
9293

9394
return $total;
@@ -121,7 +122,8 @@ protected static function purchaseOrders(int $idproyecto): float
121122
}
122123
}
123124

124-
$total += ($order->total - $totalHijos);
125+
// si el hijo tiene un total mayor que el padre, restamos solo el total del padre
126+
$total += max(0.0, $order->total - $totalHijos);
125127
}
126128

127129
return $total;
@@ -149,8 +151,9 @@ protected static function salesDeliveryNotes(int $idproyecto, float &$neto): flo
149151
}
150152
}
151153

152-
$total += ($delivery->total - $totalHijos);
153-
$neto += ($delivery->neto - $netoHijos);
154+
// si el hijo tiene un total mayor que el padre, restamos solo el total del padre
155+
$total += max(0.0, $delivery->total - $totalHijos);
156+
$neto += max(0.0, $delivery->neto - $netoHijos);
154157
}
155158

156159
return $total;
@@ -178,8 +181,9 @@ protected static function salesEstimations(int $idproyecto, float &$neto): float
178181
}
179182
}
180183

181-
$total += ($estimation->total - $totalHijos);
182-
$neto += ($estimation->neto - $netoHijos);
184+
// si el hijo tiene un total mayor que el padre, restamos solo el total del padre
185+
$total += max(0.0, $estimation->total - $totalHijos);
186+
$neto += max(0.0, $estimation->neto - $netoHijos);
183187
}
184188

185189
return $total;
@@ -220,8 +224,9 @@ protected static function salesOrders(int $idproyecto, float &$neto): float
220224
}
221225
}
222226

223-
$total += ($order->total - $totalHijos);
224-
$neto += ($order->neto - $netoHijos);
227+
// si el hijo tiene un total mayor que el padre, restamos solo el total del padre
228+
$total += max(0.0, $order->total - $totalHijos);
229+
$neto += max(0.0, $order->neto - $netoHijos);
225230
}
226231

227232
return $total;

Test/main/ProjectTotalManagerTest.php

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use FacturaScripts\Core\Lib\Calculator;
1010
use FacturaScripts\Core\Model\AlbaranCliente;
1111
use FacturaScripts\Core\Model\FacturaCliente;
12+
use FacturaScripts\Dinamic\Model\DocTransformation;
1213
use FacturaScripts\Core\Where;
1314
use FacturaScripts\Dinamic\Lib\BusinessDocumentGenerator;
1415
use FacturaScripts\Dinamic\Model\AlbaranProveedor;
@@ -18,7 +19,7 @@
1819
use FacturaScripts\Dinamic\Model\PedidoProveedor;
1920
use FacturaScripts\Dinamic\Model\PresupuestoCliente;
2021
use FacturaScripts\Dinamic\Model\Proveedor;
21-
use FacturaScripts\Plugins\Proyectos\Model\Proyecto;
22+
use FacturaScripts\Dinamic\Model\Proyecto;
2223
use FacturaScripts\Test\Traits\DefaultSettingsTrait;
2324
use FacturaScripts\Test\Traits\LogErrorsTrait;
2425
use PHPUnit\Framework\TestCase;
@@ -570,6 +571,109 @@ protected function trackSupplier(Proveedor $supplier): void
570571
$this->createdSuppliers[] = $supplier;
571572
}
572573

574+
public function testGroupedEstimationsToDeliveryNote(): void
575+
{
576+
// creamos un proyecto
577+
$project = new Proyecto();
578+
$this->assertTrue($project->save(), 'No se pudo guardar el proyecto');
579+
$this->trackProject($project);
580+
581+
// creamos un cliente
582+
$customer = $this->getRandomCustomer();
583+
$this->assertTrue($customer->save(), 'No se pudo guardar el cliente');
584+
$this->trackCustomer($customer);
585+
586+
// creamos el presupuesto 1 y lo vinculamos al proyecto
587+
$estimation1 = $this->getRandomCustomerEstimation($customer);
588+
$estimation1->idproyecto = $project->id();
589+
$this->assertTrue($estimation1->save(), 'No se pudo guardar el presupuesto 1');
590+
591+
// creamos el presupuesto 2 y lo vinculamos al proyecto
592+
$estimation2 = $this->getRandomCustomerEstimation($customer);
593+
$estimation2->idproyecto = $project->id();
594+
$this->assertTrue($estimation2->save(), 'No se pudo guardar el presupuesto 2');
595+
596+
// con ambos presupuestos sin facturar, el proyecto debe reflejar la suma de los dos
597+
// total = 181.5 * 2 = 363, pendiente (neto) = 150 * 2 = 300
598+
$this->checkProject($project, 0, 363, 300);
599+
600+
// generamos un único albarán a partir del presupuesto 1 (todas sus líneas)
601+
$generator = new BusinessDocumentGenerator();
602+
$lines1 = $estimation1->getLines();
603+
$qty1 = [];
604+
foreach ($lines1 as $line) {
605+
$qty1[$line->idlinea] = $line->cantidad;
606+
}
607+
$this->assertTrue($generator->generate(
608+
$estimation1,
609+
'AlbaranCliente',
610+
$lines1,
611+
$qty1,
612+
['idproyecto' => $project->id()]
613+
), 'No se pudo generar el albarán desde el presupuesto 1');
614+
615+
/** @var AlbaranCliente $deliveryNote */
616+
$deliveryNote = $generator->getLastDocs()[0];
617+
618+
// añadimos las líneas del presupuesto 2 al mismo albarán, simulando la agrupación
619+
$docTrans = new DocTransformation();
620+
$lines2 = $estimation2->getLines();
621+
foreach ($lines2 as $line2) {
622+
$arrayLine = [
623+
'cantidad' => $line2->cantidad,
624+
'pvpunitario' => $line2->pvpunitario,
625+
'codimpuesto' => $line2->codimpuesto,
626+
'iva' => $line2->iva,
627+
'recargo' => $line2->recargo,
628+
'descripcion' => $line2->descripcion,
629+
];
630+
$newLine = $deliveryNote->getNewLine($arrayLine);
631+
$this->assertTrue($newLine->save(), 'No se pudo guardar línea de albarán desde presupuesto 2');
632+
633+
// registramos la relación entre presupuesto 2 y el albarán
634+
$docTrans->clear();
635+
$docTrans->cantidad = $newLine->cantidad;
636+
$docTrans->model1 = $estimation2->modelClassName();
637+
$docTrans->iddoc1 = $estimation2->id();
638+
$docTrans->idlinea1 = $line2->idlinea;
639+
$docTrans->model2 = $deliveryNote->modelClassName();
640+
$docTrans->iddoc2 = $deliveryNote->id();
641+
$docTrans->idlinea2 = $newLine->idlinea;
642+
$this->assertTrue($docTrans->save(), 'No se pudo guardar la relación DocTransformation');
643+
}
644+
645+
// recalculamos el albarán para que actualice sus totales y dispare el recálculo del proyecto
646+
$deliveryNoteLines = $deliveryNote->getLines();
647+
$this->assertTrue(
648+
Calculator::calculate($deliveryNote, $deliveryNoteLines, true),
649+
'No se pudo recalcular el albarán'
650+
);
651+
652+
// el albarán agrupa ambos presupuestos: total = 363, neto = 300
653+
// con el fix: cada presupuesto contribuye 0 a totalventas (el hijo tiene más que el padre)
654+
// el albarán sin hijos contribuye 363 → totalventas = 363, pendiente = 300
655+
$this->checkProject($project, 0, 363, 300);
656+
657+
// generamos una factura a partir del albarán completo
658+
$deliveryNoteLines = $deliveryNote->getLines();
659+
$qtyDn = [];
660+
foreach ($deliveryNoteLines as $line) {
661+
$qtyDn[$line->idlinea] = $line->cantidad;
662+
}
663+
$this->assertTrue($generator->generate(
664+
$deliveryNote,
665+
'FacturaCliente',
666+
$deliveryNoteLines,
667+
$qtyDn,
668+
['idproyecto' => $project->id()]
669+
), 'No se pudo generar la factura desde el albarán');
670+
671+
// con factura generada: totalventas = 363, pendiente = 0
672+
$this->checkProject($project, 0, 363, 0);
673+
674+
$this->cleanupCreatedEntities();
675+
}
676+
573677
protected function tearDown(): void
574678
{
575679
$this->logErrors();

0 commit comments

Comments
 (0)