Skip to content

Commit

Permalink
FacturX and Zugferd : Fix totalBasis and totalTax when taxRate is null
Browse files Browse the repository at this point in the history
  • Loading branch information
rahal committed Feb 9, 2025
1 parent 8c5151d commit 0093266
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/FacturX.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,16 @@ protected function calculateTotals()
foreach ($this->taxLines as $rate => $items) {
// turn back rate to float
$rate = (float) $rate;
$sum = array_sum($items);
$totalBasis += $sum;
$tax += $calculated = $sum * $rate / 100;
// and skip tax 0
if ($rate > 0) {
$sum = array_sum($items);
$tradeTax = new TradeTax();
$tradeTax->typeCode = TaxTypeCodeContent::VAT->value;
$tradeTax->categoryCode = VatCategory::STANDARD->value;
$totalBasis += $sum;
$tradeTax->basisAmount = Amount::create(self::decimalFormat($sum));
$tradeTax->rateApplicablePercent = self::decimalFormat($rate) ;
$tax += $calculated = $sum * $rate / 100;
$tradeTax->calculatedAmount = Amount::create(self::decimalFormat($calculated));
if ($this->getProfileLevel() >= self::LEVEL_BASIC_WL) {
$this->invoice->supplyChainTradeTransaction->applicableHeaderTradeSettlement->tradeTaxes[] = $tradeTax;
Expand Down Expand Up @@ -355,9 +355,7 @@ public function validate(string $xml, $schematron)

public function addItem(string $name, float $price, float $taxRatePercent, float $quantity, UnitOfMeasurement $unit, ?string $globalID = null, string $globalIDCode = null): float
{
if ($this->getProfileLevel() < self::LEVEL_BASIC) {
return 0;
}

$item = new SupplyChainTradeLineItem();
$lineNumber = count($this->items) + 1;

Expand Down
2 changes: 1 addition & 1 deletion src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function addPaymentMean(string $typeCode, ?string $ibanId = null, ?string
try {
$typeCode = PaymentMeansCode::from($typeCode);
} catch (\ValueError $e) {
throw new \Exception("$typeCode is not a valide Unit of Unit Of Measurement");
throw new \Exception("$typeCode is not a valid PaymentMeans Code");
}

$this->xmlGenerator->addPaymentMean($typeCode, $ibanId, $accountName, $bicId);
Expand Down
14 changes: 13 additions & 1 deletion test/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ public function testFacturXCalculateTotals()

public function testFacturXCalculateTotalsWithFloatsShouldRound()
{
$invoice = new Invoice('123', new \Datetime('2023-11-07'), null, CurrencyCode::EURO, FacturX::BASIC_WL);
$invoice = new Invoice('123', new \Datetime('2023-11-07'), null, CurrencyCode::EURO, FacturX::BASIC);
// add some tax lines
$invoice->xmlGenerator->addTaxLine(20, 200);
// at this point, it would be considered as 10%
$invoice->xmlGenerator->addTaxLine(9.999, 200);
$xml = $invoice->getXml();
//Ensure the total is correctly calculated
$this->assertEquals($invoice->xmlGenerator->invoice->supplyChainTradeTransaction->applicableHeaderTradeSettlement->specifiedTradeSettlementHeaderMonetarySummation->duePayableAmount->value, "460.00");
}

public function testFacturXCalculateTotalsInMinimum()
{
$invoice = new Invoice('123', new \Datetime('2023-11-07'), null, CurrencyCode::EURO, FacturX::MINIMUM);
// add some tax lines
$invoice->xmlGenerator->addTaxLine(20, 200);
// at this point, it would be considered as 10%
Expand Down

0 comments on commit 0093266

Please sign in to comment.