|
9 | 9 | use FacturaScripts\Core\Lib\Calculator; |
10 | 10 | use FacturaScripts\Core\Model\AlbaranCliente; |
11 | 11 | use FacturaScripts\Core\Model\FacturaCliente; |
| 12 | +use FacturaScripts\Dinamic\Model\DocTransformation; |
12 | 13 | use FacturaScripts\Core\Where; |
13 | 14 | use FacturaScripts\Dinamic\Lib\BusinessDocumentGenerator; |
14 | 15 | use FacturaScripts\Dinamic\Model\AlbaranProveedor; |
|
18 | 19 | use FacturaScripts\Dinamic\Model\PedidoProveedor; |
19 | 20 | use FacturaScripts\Dinamic\Model\PresupuestoCliente; |
20 | 21 | use FacturaScripts\Dinamic\Model\Proveedor; |
21 | | -use FacturaScripts\Plugins\Proyectos\Model\Proyecto; |
| 22 | +use FacturaScripts\Dinamic\Model\Proyecto; |
22 | 23 | use FacturaScripts\Test\Traits\DefaultSettingsTrait; |
23 | 24 | use FacturaScripts\Test\Traits\LogErrorsTrait; |
24 | 25 | use PHPUnit\Framework\TestCase; |
@@ -570,6 +571,109 @@ protected function trackSupplier(Proveedor $supplier): void |
570 | 571 | $this->createdSuppliers[] = $supplier; |
571 | 572 | } |
572 | 573 |
|
| 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 | + |
573 | 677 | protected function tearDown(): void |
574 | 678 | { |
575 | 679 | $this->logErrors(); |
|
0 commit comments