Fixes for Total Discount on invoice - zugferd
This commit is contained in:
parent
1ef08dd27e
commit
e000fa1275
|
|
@ -28,6 +28,7 @@ use App\Services\EDocument\Standards\FatturaPA;
|
||||||
use App\Services\EDocument\Standards\RoEInvoice;
|
use App\Services\EDocument\Standards\RoEInvoice;
|
||||||
use App\Services\EDocument\Standards\OrderXDocument;
|
use App\Services\EDocument\Standards\OrderXDocument;
|
||||||
use App\Services\EDocument\Standards\FacturaEInvoice;
|
use App\Services\EDocument\Standards\FacturaEInvoice;
|
||||||
|
use App\Services\EDocument\Standards\ZugferdEDocument;
|
||||||
use App\Services\EDocument\Standards\ZugferdEDokument;
|
use App\Services\EDocument\Standards\ZugferdEDokument;
|
||||||
|
|
||||||
class CreateEDocument implements ShouldQueue
|
class CreateEDocument implements ShouldQueue
|
||||||
|
|
@ -85,7 +86,10 @@ class CreateEDocument implements ShouldQueue
|
||||||
case "XInvoice-Extended":
|
case "XInvoice-Extended":
|
||||||
case "XInvoice-BasicWL":
|
case "XInvoice-BasicWL":
|
||||||
case "XInvoice-Basic":
|
case "XInvoice-Basic":
|
||||||
$zugferd = (new ZugferdEDokument($this->document))->run();
|
|
||||||
|
// $zugferd = (new ZugferdEDokument($this->document))->run();
|
||||||
|
|
||||||
|
$zugferd = (new ZugferdEDocument($this->document))->run();
|
||||||
|
|
||||||
return $this->returnObject ? $zugferd->xdocument : $zugferd->getXml();
|
return $this->returnObject ? $zugferd->xdocument : $zugferd->getXml();
|
||||||
case "Facturae_3.2":
|
case "Facturae_3.2":
|
||||||
|
|
|
||||||
|
|
@ -110,47 +110,40 @@ class ZugferdEDocument extends AbstractService
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get document level discount
|
// Get document level discount
|
||||||
$document_discount = $this->calc->getTotalDiscount();
|
// $document_discount = $this->calc->getTotalDiscount();
|
||||||
$total_taxable = $this->getTaxable();
|
// $total_taxable = $this->getTaxable();
|
||||||
|
$net_subtotal = $this->calc->getNetSubTotal();
|
||||||
|
|
||||||
// Process each tax rate group
|
// Process each tax rate group
|
||||||
foreach ($this->calc->getTaxMap() as $item) {
|
foreach ($this->calc->getTaxMap() as $item) {
|
||||||
$tax_type = $this->getTaxType($item["tax_id"]);
|
$tax_type = $this->getTaxType($item["tax_id"]);
|
||||||
|
|
||||||
// Calculate proportional discount for this tax group
|
|
||||||
$tax_group_total = $item["base_amount"];
|
|
||||||
$proportional_discount = 0;
|
|
||||||
|
|
||||||
if ($document_discount > 0 && $total_taxable > 0) {
|
|
||||||
$proportional_discount = round(($tax_group_total / $total_taxable) * $document_discount, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adjusted taxable amount for this tax group
|
|
||||||
$adjusted_taxable = $tax_group_total - $proportional_discount;
|
|
||||||
|
|
||||||
// Add document level allowance (discount) if present
|
|
||||||
if ($proportional_discount > 0) {
|
|
||||||
$this->xdocument->addDocumentAllowanceCharge(
|
|
||||||
$proportional_discount,
|
|
||||||
false, // isCharge = false means it's a discount
|
|
||||||
$tax_type,
|
|
||||||
"VAT",
|
|
||||||
$item["tax_rate"]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add tax information
|
// Add tax information
|
||||||
$this->xdocument->addDocumentTax(
|
$this->xdocument->addDocumentTax(
|
||||||
$tax_type,
|
$tax_type,
|
||||||
"VAT",
|
"VAT",
|
||||||
$adjusted_taxable, // Taxable amount after discount
|
$item["base_amount"], // Taxable amount after discount
|
||||||
// ($item["tax_rate"] / 100) * $adjusted_taxable, // Tax amount
|
|
||||||
$item["total"],
|
$item["total"],
|
||||||
$item["tax_rate"],
|
$item["tax_rate"],
|
||||||
$tax_type == ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES
|
$tax_type == ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES
|
||||||
? ctrans('texts.intracommunity_tax_info')
|
? ctrans('texts.intracommunity_tax_info')
|
||||||
: ''
|
: ''
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if ($this->calc->getTotalDiscount() > 0) {
|
||||||
|
|
||||||
|
$ratio = $item["base_amount"] / $net_subtotal;
|
||||||
|
|
||||||
|
$this->xdocument->addDocumentAllowanceCharge(
|
||||||
|
round($this->calc->getTotalDiscount() * $ratio, 2),
|
||||||
|
false,
|
||||||
|
$tax_type,
|
||||||
|
"VAT",
|
||||||
|
$item["tax_rate"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -197,10 +190,22 @@ class ZugferdEDocument extends AbstractService
|
||||||
// Calculate amounts after discount
|
// Calculate amounts after discount
|
||||||
$taxable_amount = $this->getTaxable();
|
$taxable_amount = $this->getTaxable();
|
||||||
|
|
||||||
|
nlog([
|
||||||
|
$this->document->amount, // Total amount with VAT
|
||||||
|
$this->document->balance, // Amount due
|
||||||
|
$this->calc->getSubTotal(), // Sum before tax
|
||||||
|
$this->calc->getTotalSurcharges(), // Total charges
|
||||||
|
$document_discount, // Total allowances
|
||||||
|
$taxable_amount, // Tax basis total (net)
|
||||||
|
$total_tax, // Total tax amount
|
||||||
|
0.0, // Total prepaid amount
|
||||||
|
$this->document->amount - $this->document->balance,
|
||||||
|
]);
|
||||||
|
|
||||||
$this->xdocument->setDocumentSummation(
|
$this->xdocument->setDocumentSummation(
|
||||||
$this->document->amount, // Total amount with VAT
|
$this->document->amount, // Total amount with VAT
|
||||||
$this->document->balance, // Amount due
|
$this->document->balance, // Amount due
|
||||||
$subtotal, // Sum before tax
|
$this->calc->getSubTotal(), // Sum before tax
|
||||||
$this->calc->getTotalSurcharges(), // Total charges
|
$this->calc->getTotalSurcharges(), // Total charges
|
||||||
$document_discount, // Total allowances
|
$document_discount, // Total allowances
|
||||||
$taxable_amount, // Tax basis total (net)
|
$taxable_amount, // Tax basis total (net)
|
||||||
|
|
@ -211,8 +216,9 @@ class ZugferdEDocument extends AbstractService
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
private function setLineItems(): self
|
|
||||||
{
|
private function setLineItems(): self
|
||||||
|
{
|
||||||
foreach ($this->document->line_items as $index => $item) {
|
foreach ($this->document->line_items as $index => $item) {
|
||||||
/** @var InvoiceItem $item **/
|
/** @var InvoiceItem $item **/
|
||||||
|
|
||||||
|
|
@ -227,7 +233,7 @@ private function setLineItems(): self
|
||||||
$item->type_id == 2 ? "HUR" : "H87"
|
$item->type_id == 2 ? "HUR" : "H87"
|
||||||
)
|
)
|
||||||
->setDocumentPositionNetPrice(
|
->setDocumentPositionNetPrice(
|
||||||
$this->document->uses_inclusive_tax ? $item->net_cost : $item->cost
|
$this->document->uses_inclusive_taxes ? $item->net_cost : $item->cost
|
||||||
);
|
);
|
||||||
|
|
||||||
// 2. ALWAYS add tax information (even if zero)
|
// 2. ALWAYS add tax information (even if zero)
|
||||||
|
|
@ -260,16 +266,16 @@ private function setLineItems(): self
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculateTotalItemDiscountAmount($item): float
|
private function calculateTotalItemDiscountAmount($item): float
|
||||||
{
|
{
|
||||||
if ($item->is_amount_discount) {
|
if ($item->is_amount_discount) {
|
||||||
return $item->discount;
|
return $item->discount;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ($item->cost * $item->quantity) * ($item->discount / 100);
|
return ($item->cost * $item->quantity) * ($item->discount / 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function setClientTaxRegistration(): self
|
private function setClientTaxRegistration(): self
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue