Skip to content

Commit

Permalink
Solucionado bug al calcular costes y beneficios cuando el descuento e…
Browse files Browse the repository at this point in the history
…s del 100%.
  • Loading branch information
NeoRazorX committed Apr 10, 2024
1 parent a1fa2e7 commit d8b821e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
12 changes: 6 additions & 6 deletions Core/Base/Calculator.php
Expand Up @@ -111,6 +111,12 @@ public static function getSubtotals(BusinessDocument $doc, array $lines): array

// acumulamos por cada línea
foreach ($lines as $line) {
// coste
$totalCoste = isset($line->coste) ? $line->cantidad * $line->coste : 0.0;
if (isset($line->coste)) {
$subtotals['totalcoste'] += $totalCoste;
}

$pvpTotal = $line->pvptotal * (100 - $doc->dtopor1) / 100 * (100 - $doc->dtopor2) / 100;
if (empty($pvpTotal)) {
continue;
Expand Down Expand Up @@ -140,12 +146,6 @@ public static function getSubtotals(BusinessDocument $doc, array $lines): array
];
}

// coste
$totalCoste = isset($line->coste) ? $line->cantidad * $line->coste : 0.0;
if (isset($line->coste)) {
$subtotals['totalcoste'] += $totalCoste;
}

// si es una venta de segunda mano, calculamos el beneficio y el IVA
if (self::applyUsedGoods($subtotals, $doc, $line, $ivaKey, $pvpTotal, $totalCoste)) {
continue;
Expand Down
60 changes: 46 additions & 14 deletions Test/Core/Base/CalculatorTest.php
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2022 Carlos Garcia Gomez <carlos@facturascripts.com>
* Copyright (C) 2017-2024 Carlos Garcia Gomez <carlos@facturascripts.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
Expand Down Expand Up @@ -34,7 +34,7 @@ final class CalculatorTest extends TestCase
{
use RandomDataTrait;

public function testEmptyDoc()
public function testEmptyDoc(): void
{
$doc = new PresupuestoCliente();
$lines = [];
Expand All @@ -48,9 +48,11 @@ public function testEmptyDoc()
$this->assertEquals(0.0, $doc->totalirpf, 'bad-totalirpf');
$this->assertEquals(0.0, $doc->totalrecargo, 'bad-totalrecargo');
$this->assertEquals(0.0, $doc->totalsuplidos, 'bad-totalsuplidos');
$this->assertEquals(0.0, $doc->totalcoste, 'bad-totalcoste');
$this->assertEquals(0.0, $doc->totalbeneficio, 'bad-totalbeneficio');
}

public function testEmptyLine()
public function testEmptyLine(): void
{
$doc = new PresupuestoCliente();
$lines = [$doc->getNewLine()];
Expand All @@ -64,13 +66,15 @@ public function testEmptyLine()
$this->assertEquals(0.0, $doc->totalirpf, 'bad-totalirpf');
$this->assertEquals(0.0, $doc->totalrecargo, 'bad-totalrecargo');
$this->assertEquals(0.0, $doc->totalsuplidos, 'bad-totalsuplidos');
$this->assertEquals(0.0, $doc->totalcoste, 'bad-totalcoste');
$this->assertEquals(0.0, $doc->totalbeneficio, 'bad-totalbeneficio');

// comprobamos la línea
$this->assertEquals(0.0, $lines[0]->pvpsindto, 'bad-line-pvpsindto');
$this->assertEquals(0.0, $lines[0]->pvptotal, 'bad-line-pvptotal');
}

public function testLines()
public function testLines(): void
{
$doc = new PresupuestoCliente();

Expand Down Expand Up @@ -99,6 +103,7 @@ public function testLines()
$this->assertEquals(0.0, $doc->totalrecargo, 'bad-totalrecargo');
$this->assertEquals(0.0, $doc->totalsuplidos, 'bad-totalsuplidos');
$this->assertEquals(6.0, $doc->totalcoste, 'bad-totalcoste');
$this->assertEquals(114.0, $doc->totalbeneficio, 'bad-totalbeneficio');

// comprobamos la primera línea
$this->assertEquals(100.0, $lines[0]->pvpsindto, 'bad-line1-pvpsindto');
Expand All @@ -109,7 +114,7 @@ public function testLines()
$this->assertEquals(20.0, $lines[1]->pvptotal, 'bad-line2-pvptotal');
}

public function testDiscounts()
public function testDiscounts(): void
{
$doc = new PresupuestoCliente();
$doc->dtopor1 = 5;
Expand Down Expand Up @@ -150,7 +155,34 @@ public function testDiscounts()
$this->assertEquals(17.1, $lines[1]->pvptotal, 'bad-line2-pvptotal');
}

public function testRetention()
public function testCostWithFullDiscount(): void
{
// creamos el documento
$doc = new PresupuestoCliente();

// primera línea
$line1 = $doc->getNewLine();
$line1->cantidad = 1;
$line1->pvpunitario = 100;
$line1->coste = 50;
$line1->iva = 21;
$line1->dtopor = 100;

$lines = [$line1];
$this->assertFalse(Calculator::calculate($doc, $lines, false), 'doc-saved');

// comprobamos el documento
$this->assertEquals(0.0, $doc->neto, 'bad-neto');
$this->assertEquals(0.0, $doc->netosindto, 'bad-netosindto');
$this->assertEquals(0.0, $doc->total, 'bad-total');
$this->assertEquals(0.0, $doc->totaliva, 'bad-totaliva');
$this->assertEquals(0.0, $doc->totalirpf, 'bad-totalirpf');
$this->assertEquals(0.0, $doc->totalrecargo, 'bad-totalrecargo');
$this->assertEquals(0.0, $doc->totalsuplidos, 'bad-totalsuplidos');
$this->assertEquals(50, $doc->totalcoste, 'bad-totalcoste');
}

public function testRetention(): void
{
$doc = new PresupuestoCliente();

Expand Down Expand Up @@ -187,7 +219,7 @@ public function testRetention()
$this->assertEquals(0.0, $doc->totalsuplidos, 'bad-totalsuplidos');
}

public function testCustomerRe()
public function testCustomerRe(): void
{
// creamos un cliente con recargo de equivalencia
$subject = $this->getRandomCustomer();
Expand Down Expand Up @@ -235,7 +267,7 @@ public function testCustomerRe()
$this->assertTrue($subject->delete(), 'cliente-cant-delete');
}

public function testSupplierRe()
public function testSupplierRe(): void
{
// creamos un proveedor con recargo de equivalencia
$subject = $this->getRandomSupplier();
Expand Down Expand Up @@ -269,7 +301,7 @@ public function testSupplierRe()
$this->assertTrue($subject->delete(), 'proveedor-cant-delete');
}

public function testSupplied()
public function testSupplied(): void
{
$doc = new PresupuestoCliente();

Expand Down Expand Up @@ -306,7 +338,7 @@ public function testSupplied()
$this->assertEquals(50.0, $doc->totalsuplidos, 'bad-totalsuplidos');
}

public function testNoTaxSerie()
public function testNoTaxSerie(): void
{
// creamos una serie sin impuestos
$serie = new Serie();
Expand Down Expand Up @@ -352,7 +384,7 @@ public function testNoTaxSerie()
$serie->delete();
}

public function testCustomerExempt()
public function testCustomerExempt(): void
{
// creamos un cliente exento
$subject = $this->getRandomCustomer();
Expand Down Expand Up @@ -386,7 +418,7 @@ public function testCustomerExempt()
$this->assertTrue($subject->delete(), 'cliente-cant-delete');
}

public function testSupplierExempt()
public function testSupplierExempt(): void
{
// creamos un proveedor exento
$subject = $this->getRandomSupplier();
Expand Down Expand Up @@ -420,7 +452,7 @@ public function testSupplierExempt()
$this->assertTrue($subject->delete(), 'proveedor-cant-delete');
}

public function testTaxZone()
public function testTaxZone(): void
{
// creamos un impuesto del 20%
$tax1 = new Impuesto();
Expand Down Expand Up @@ -486,7 +518,7 @@ public function testTaxZone()
$tax2->delete();
}

public function testNoTaxZone()
public function testNoTaxZone(): void
{
// creamos un impuesto del 20%
$tax1 = new Impuesto();
Expand Down

0 comments on commit d8b821e

Please sign in to comment.