Fixes for taxes with peppol

This commit is contained in:
David Bomba 2024-12-09 13:29:35 +11:00
parent 5970d80874
commit b459551536
8 changed files with 93 additions and 43 deletions

View File

@ -32,4 +32,4 @@ R2_DEFAULT_REGION_BACKUP=us-east-1
R2_BUCKET_BACKUP=
R2_URL_BACKUP=
R2_ENDPOINT_BACKUP=
R2_USE_PATH_STYLE_ENDPOINT_BACKUP=
R2_USE_PATH_STYLE_ENDPOINT_BACKUP=false

View File

@ -488,8 +488,6 @@ class InvoiceItemSum
$amount = $this->item->line_total;
}
// $amount = round($amount,2);
$item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount);
$item_tax += $item_tax_rate1_total;
@ -523,7 +521,7 @@ class InvoiceItemSum
}
$this->getPeppolSurchargeTaxes();
return $this;
}

View File

@ -183,10 +183,10 @@ class BaseRepository
$model->fill($tmp_data);
$model->custom_surcharge_tax1 = $client->company->custom_surcharge_taxes1;
$model->custom_surcharge_tax2 = $client->company->custom_surcharge_taxes2;
$model->custom_surcharge_tax3 = $client->company->custom_surcharge_taxes3;
$model->custom_surcharge_tax4 = $client->company->custom_surcharge_taxes4;
$model->custom_surcharge_tax1 = $client->getSetting('e_invoice_type') == 'PEPPOL' ? true : $client->company->custom_surcharge_taxes1;
$model->custom_surcharge_tax2 = $client->getSetting('e_invoice_type') == 'PEPPOL' ? true : $client->company->custom_surcharge_taxes2;
$model->custom_surcharge_tax3 = $client->getSetting('e_invoice_type') == 'PEPPOL' ? true : $client->company->custom_surcharge_taxes3;
$model->custom_surcharge_tax4 = $client->getSetting('e_invoice_type') == 'PEPPOL' ? true : $client->company->custom_surcharge_taxes4;
if (!$model->id) {
$this->new_model = true;

View File

@ -345,6 +345,7 @@ class Peppol extends AbstractService
$xml .= $suffix;
nlog($xml);
return $xml;
}
@ -473,12 +474,13 @@ class Peppol extends AbstractService
// Add Allowance Charge to Price
$allowanceCharge = new \InvoiceNinja\EInvoice\Models\Peppol\AllowanceChargeType\AllowanceCharge();
$allowanceCharge->ChargeIndicator = 'true';
$allowanceCharge->Amount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\Amount();
$allowanceCharge->Amount->currencyID = $this->invoice->client->currency()->code;
$allowanceCharge->Amount->amount = number_format($this->invoice->custom_surcharge1, 2, '.', '');
$allowanceCharge->BaseAmount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\BaseAmount();
$allowanceCharge->BaseAmount->currencyID = $this->invoice->client->currency()->code;
$allowanceCharge->BaseAmount->amount = (string) $this->calc->getSubtotalWithSurcharges();
// $allowanceCharge->BaseAmount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\BaseAmount();
// $allowanceCharge->BaseAmount->currencyID = $this->invoice->client->currency()->code;
// $allowanceCharge->BaseAmount->amount = (string) $this->calc->getSubtotalWithSurcharges();
$this->calculateTaxMap($this->invoice->custom_surcharge1);
@ -492,12 +494,13 @@ class Peppol extends AbstractService
// Add Allowance Charge to Price
$allowanceCharge = new \InvoiceNinja\EInvoice\Models\Peppol\AllowanceChargeType\AllowanceCharge();
$allowanceCharge->ChargeIndicator = 'true';
$allowanceCharge->Amount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\Amount();
$allowanceCharge->Amount->currencyID = $this->invoice->client->currency()->code;
$allowanceCharge->Amount->amount = number_format($this->invoice->custom_surcharge2, 2, '.', '');
$allowanceCharge->BaseAmount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\BaseAmount();
$allowanceCharge->BaseAmount->currencyID = $this->invoice->client->currency()->code;
$allowanceCharge->BaseAmount->amount = (string) $this->calc->getSubtotalWithSurcharges();
// $allowanceCharge->BaseAmount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\BaseAmount();
// $allowanceCharge->BaseAmount->currencyID = $this->invoice->client->currency()->code;
// $allowanceCharge->BaseAmount->amount = (string) $this->calc->getSubtotalWithSurcharges();
$this->calculateTaxMap($this->invoice->custom_surcharge2);
@ -512,12 +515,13 @@ class Peppol extends AbstractService
// Add Allowance Charge to Price
$allowanceCharge = new \InvoiceNinja\EInvoice\Models\Peppol\AllowanceChargeType\AllowanceCharge();
$allowanceCharge->ChargeIndicator = 'true';
$allowanceCharge->Amount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\Amount();
$allowanceCharge->Amount->currencyID = $this->invoice->client->currency()->code;
$allowanceCharge->Amount->amount = number_format($this->invoice->custom_surcharge3, 2, '.', '');
$allowanceCharge->BaseAmount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\BaseAmount();
$allowanceCharge->BaseAmount->currencyID = $this->invoice->client->currency()->code;
$allowanceCharge->BaseAmount->amount = (string) $this->calc->getSubtotalWithSurcharges();
// $allowanceCharge->BaseAmount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\BaseAmount();
// $allowanceCharge->BaseAmount->currencyID = $this->invoice->client->currency()->code;
// $allowanceCharge->BaseAmount->amount = (string) $this->calc->getSubtotalWithSurcharges();
$this->calculateTaxMap($this->invoice->custom_surcharge3);
@ -531,12 +535,13 @@ class Peppol extends AbstractService
// Add Allowance Charge to Price
$allowanceCharge = new \InvoiceNinja\EInvoice\Models\Peppol\AllowanceChargeType\AllowanceCharge();
$allowanceCharge->ChargeIndicator = 'true';
$allowanceCharge->Amount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\Amount();
$allowanceCharge->Amount->currencyID = $this->invoice->client->currency()->code;
$allowanceCharge->Amount->amount = number_format($this->invoice->custom_surcharge4, 2, '.', '');
$allowanceCharge->BaseAmount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\BaseAmount();
$allowanceCharge->BaseAmount->currencyID = $this->invoice->client->currency()->code;
$allowanceCharge->BaseAmount->amount = (string) $this->calc->getSubtotalWithSurcharges();
// $allowanceCharge->BaseAmount = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\BaseAmount();
// $allowanceCharge->BaseAmount->currencyID = $this->invoice->client->currency()->code;
// $allowanceCharge->BaseAmount->amount = (string) $this->calc->getSubtotalWithSurcharges();
$this->calculateTaxMap($this->invoice->custom_surcharge4);
@ -563,7 +568,7 @@ class Peppol extends AbstractService
$lea = new LineExtensionAmount();
$lea->currencyID = $this->invoice->client->currency()->code;
$lea->amount = $this->invoice->uses_inclusive_taxes ? round($this->invoice->amount - $this->invoice->total_taxes, 2) : $this->calc->getSubTotal();
$lea->amount = $this->invoice->uses_inclusive_taxes ? round($this->invoice->amount - $this->invoice->total_taxes, 2) : $this->calc->getSubtotal();
$lmt->LineExtensionAmount = $lea;
$tea = new TaxExclusiveAmount();
@ -587,6 +592,11 @@ class Peppol extends AbstractService
$am->amount = number_format($this->calc->getTotalDiscount(), 2, '.', '');
$lmt->AllowanceTotalAmount = $am;
$cta = new \InvoiceNinja\EInvoice\Models\Peppol\AmountType\ChargeTotalAmount();
$cta->currencyID = $this->invoice->client->currency()->code;
$cta->amount = number_format($this->calc->getTotalSurcharges(), 2, '.', '');
$lmt->ChargeTotalAmount = $cta;
return $lmt;
}
@ -1200,19 +1210,20 @@ class Peppol extends AbstractService
}
}
if ($this->invoice->custom_surcharge1 && $this->invoice->custom_surcharge_tax1) {
//** Surcharges are taxable regardless, if control is needed over taxable components, add it as a line item! */
if ($this->invoice->custom_surcharge1 > 0) {
$total += $this->invoice->custom_surcharge1;
}
if ($this->invoice->custom_surcharge2 && $this->invoice->custom_surcharge_tax2) {
if ($this->invoice->custom_surcharge2 > 0) {
$total += $this->invoice->custom_surcharge2;
}
if ($this->invoice->custom_surcharge3 && $this->invoice->custom_surcharge_tax3) {
if ($this->invoice->custom_surcharge3 > 0) {
$total += $this->invoice->custom_surcharge3;
}
if ($this->invoice->custom_surcharge4 && $this->invoice->custom_surcharge_tax4) {
if ($this->invoice->custom_surcharge4 > 0) {
$total += $this->invoice->custom_surcharge4;
}

View File

@ -394,16 +394,21 @@ class PdfConfiguration
$v = rtrim(sprintf('%f', $value), '0');
$parts = explode('.', $v);
/* 08-02-2023 special if block to render $0.5 to $0.50*/
if ($v < 1 && strlen($v) == 3) {
$precision = 2;
} elseif ($v < 1) {
$precision = strlen($v) - strrpos($v, '.') - 1;
}
/** 2024-12-09 improve resolution of unit cost precision */
if (strlen($parts[1] ?? '') > 2)
$precision = strlen($parts[1]);
if (is_array($parts) && $parts[0] != 0) {
$precision = 2;
}
/* 08-02-2023 special if block to render $0.5 to $0.50*/
// if ($v < 1 && strlen($v) == 3) {
// $precision = 2;
// } elseif ($v < 1) {
// $precision = strlen($v) - strrpos($v, '.') - 1;
// } elseif ($v > 1) {
// $precision = strlen($v) - strrpos($v, '.') - 1;
// }
// if (is_array($parts) && $parts[0] != 0) {
// $precision = 2;
// }
//04-04-2023 if currency = JPY override precision to 0
if ($this->currency->code == 'JPY') {

View File

@ -323,16 +323,21 @@ class Number
$v = rtrim(sprintf('%f', $value), '0');
$parts = explode('.', $v);
/* 08-02-2023 special if block to render $0.5 to $0.50*/
if ($v < 1 && strlen($v) == 3) {
$precision = 2;
} elseif ($v < 1) {
$precision = strlen($v) - strrpos($v, '.') - 1;
/* 2024-12-09 improved decimal resolution
if (strlen($parts[1] ?? '') > 2) {
$precision = strlen($parts[1]);
}
if (is_array($parts) && $parts[0] != 0) {
$precision = 2;
}
/* 08-02-2023 special if block to render $0.5 to $0.50*/
// if ($v < 1 && strlen($v) == 3) {
// $precision = 2;
// } elseif ($v < 1) {
// $precision = strlen($v) - strrpos($v, '.') - 1;
// }
// if (is_array($parts) && $parts[0] != 0) {
// $precision = 2;
// }
//04-04-2023 if currency = JPY override precision to 0
if ($currency->code == 'JPY') {

View File

@ -72,6 +72,37 @@ class NumberTest extends TestCase
}
public function testNumberPrecisionFormatting()
{
$data = [
"5.1" => "5.10",
"5.12345656" => "5.12345656",
"0.13" => "0.13",
"0.1" => "0.10",
"0.5" => "0.50",
"10.5" => "10.50",
"10100.00" => "10,100.00",
"10100.1" => "10,100.10",
];
foreach($data as $key => $value)
{
$parts = explode(".", $key);
$precision_part = $parts[1];
$precision = 2;
if(strlen($precision_part) > 2)
$precision = strlen($precision_part);
$formatted_value = number_format($key, $precision, ".", ",");
$this->assertEquals($value, $formatted_value);
}
}
public function testThreeDecimalFloatAsTax()
{