Files
musadaq-saas/app/Services/JoFotara/UBLGeneratorService.php

202 lines
9.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\JoFotara;
use DOMDocument;
use DOMElement;
final class UBLGeneratorService
{
public function generate(array $invoice, array $lines, array $company): string
{
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$root = $dom->createElementNS('urn:oasis:names:specification:ubl:schema:xsd:Invoice-2', 'Invoice');
$root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cac', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2');
$root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cbc', 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2');
$dom->appendChild($root);
// 1. Basic Information
$root->appendChild($dom->createElement('cbc:UBLVersionID', '2.1'));
$root->appendChild($dom->createElement('cbc:CustomizationID', 'TRADACO-2.1'));
$root->appendChild($dom->createElement('cbc:ProfileID', 'reporting:1.0'));
$root->appendChild($dom->createElement('cbc:ID', $invoice['invoice_number']));
$root->appendChild($dom->createElement('cbc:IssueDate', $invoice['invoice_date']));
$typeCode = $dom->createElement('cbc:InvoiceTypeCode', $invoice['ubl_type_code'] ?? '388');
$typeCode->setAttribute('name', $invoice['invoice_category'] ?? '01');
$root->appendChild($typeCode);
$root->appendChild($dom->createElement('cbc:DocumentCurrencyCode', 'JOD'));
$root->appendChild($dom->createElement('cbc:TaxCurrencyCode', 'JOD'));
// 2. AccountingSupplierParty
$supplierParty = $dom->createElement('cac:AccountingSupplierParty');
$party = $dom->createElement('cac:Party');
$partyId = $dom->createElement('cac:PartyIdentification');
$id = $dom->createElement('cbc:ID', $company['tax_identification_number']);
$id->setAttribute('schemeID', 'TN');
$partyId->appendChild($id);
$party->appendChild($partyId);
$partyName = $dom->createElement('cac:PartyName');
$partyName->appendChild($dom->createElement('cbc:Name', $company['name']));
$party->appendChild($partyName);
$postalAddr = $dom->createElement('cac:PostalAddress');
$postalAddr->appendChild($dom->createElement('cbc:CityName', $company['city'] ?? 'Amman'));
$country = $dom->createElement('cac:Country');
$country->appendChild($dom->createElement('cbc:IdentificationCode', 'JO'));
$postalAddr->appendChild($country);
$party->appendChild($postalAddr);
$taxScheme = $dom->createElement('cac:PartyTaxScheme');
$taxScheme->appendChild($dom->createElement('cbc:RegistrationName', $company['name']));
$taxScheme->appendChild($dom->createElement('cbc:CompanyID', $company['tax_identification_number']));
$ts = $dom->createElement('cac:TaxScheme');
$ts->appendChild($dom->createElement('cbc:ID', 'VAT'));
$taxScheme->appendChild($ts);
$party->appendChild($taxScheme);
$legalEntity = $dom->createElement('cac:PartyLegalEntity');
$legalEntity->appendChild($dom->createElement('cbc:RegistrationName', $company['name']));
$legalEntity->appendChild($dom->createElement('cbc:CompanyID', $company['tax_identification_number']));
$party->appendChild($legalEntity);
$supplierParty->appendChild($party);
$root->appendChild($supplierParty);
// 3. AccountingCustomerParty
$customerParty = $dom->createElement('cac:AccountingCustomerParty');
$cparty = $dom->createElement('cac:Party');
if (!empty($invoice['buyer_tin']) || !empty($invoice['buyer_national_id'])) {
$cpartyId = $dom->createElement('cac:PartyIdentification');
$cid = $dom->createElement('cbc:ID', $invoice['buyer_tin'] ?: $invoice['buyer_national_id']);
$cid->setAttribute('schemeID', $invoice['buyer_tin'] ? 'TN' : 'NID');
$cpartyId->appendChild($cid);
$cparty->appendChild($cpartyId);
}
$cpartyName = $dom->createElement('cac:PartyName');
$cpartyName->appendChild($dom->createElement('cbc:Name', $invoice['buyer_name'] ?? 'General Customer'));
$cparty->appendChild($cpartyName);
$ctaxScheme = $dom->createElement('cac:PartyTaxScheme');
$cts = $dom->createElement('cac:TaxScheme');
$cts->appendChild($dom->createElement('cbc:ID', 'VAT'));
$ctaxScheme->appendChild($cts);
$cparty->appendChild($ctaxScheme);
$customerParty->appendChild($cparty);
$root->appendChild($customerParty);
// 4. PaymentMeans
$paymentMeans = $dom->createElement('cac:PaymentMeans');
$paymentMeans->appendChild($dom->createElement('cbc:PaymentMeansCode', $invoice['payment_method_code'] ?? '013'));
$root->appendChild($paymentMeans);
// 5. TaxTotal
$taxTotal = $dom->createElement('cac:TaxTotal');
$taxAmt = $dom->createElement('cbc:TaxAmount', number_format((float)$invoice['tax_amount'], 3, '.', ''));
$taxAmt->setAttribute('currencyID', 'JOD');
$taxTotal->appendChild($taxAmt);
// Group lines by tax rate
$taxGroups = [];
foreach ($lines as $line) {
$rate = number_format((float)$line['tax_rate'], 2, '.', '');
if (!isset($taxGroups[$rate])) {
$taxGroups[$rate] = ['taxable' => 0, 'tax' => 0];
}
$taxGroups[$rate]['taxable'] += ($line['quantity'] * $line['unit_price']) - $line['discount'];
$taxGroups[$rate]['tax'] += $line['tax_amount'];
}
foreach ($taxGroups as $rate => $data) {
$taxSubtotal = $dom->createElement('cac:TaxSubtotal');
$subtaxable = $dom->createElement('cbc:TaxableAmount', number_format($data['taxable'], 3, '.', ''));
$subtaxable->setAttribute('currencyID', 'JOD');
$taxSubtotal->appendChild($subtaxable);
$subtaxamt = $dom->createElement('cbc:TaxAmount', number_format($data['tax'], 3, '.', ''));
$subtaxamt->setAttribute('currencyID', 'JOD');
$taxSubtotal->appendChild($subtaxamt);
$taxCategory = $dom->createElement('cac:TaxCategory');
$taxCategory->appendChild($dom->createElement('cbc:ID', 'S'));
$taxCategory->appendChild($dom->createElement('cbc:Percent', number_format((float)$rate * 100, 2, '.', '')));
$tcs = $dom->createElement('cac:TaxScheme');
$tcs->appendChild($dom->createElement('cbc:ID', 'VAT'));
$taxCategory->appendChild($tcs);
$taxSubtotal->appendChild($taxCategory);
$taxTotal->appendChild($taxSubtotal);
}
$root->appendChild($taxTotal);
// 6. LegalMonetaryTotal
$lmt = $dom->createElement('cac:LegalMonetaryTotal');
$fields = [
'LineExtensionAmount' => $invoice['subtotal'] - $invoice['discount_total'],
'TaxExclusiveAmount' => $invoice['subtotal'] - $invoice['discount_total'],
'TaxInclusiveAmount' => $invoice['grand_total'],
'AllowanceTotalAmount' => $invoice['discount_total'],
'PayableAmount' => $invoice['grand_total']
];
foreach ($fields as $field => $value) {
$f = $dom->createElement('cbc:' . $field, number_format((float)$value, 3, '.', ''));
$f->setAttribute('currencyID', 'JOD');
$lmt->appendChild($f);
}
$root->appendChild($lmt);
// 7. InvoiceLine
foreach ($lines as $line) {
$invLine = $dom->createElement('cac:InvoiceLine');
$invLine->appendChild($dom->createElement('cbc:ID', (string)$line['line_number']));
$qty = $dom->createElement('cbc:InvoicedQuantity', number_format((float)$line['quantity'], 3, '.', ''));
$qty->setAttribute('unitCode', 'PCE');
$invLine->appendChild($qty);
$lineExt = $dom->createElement('cbc:LineExtensionAmount', number_format($line['line_total'], 3, '.', ''));
$lineExt->setAttribute('currencyID', 'JOD');
$invLine->appendChild($lineExt);
// Line Tax
$lineTax = $dom->createElement('cac:TaxTotal');
$ltaxAmt = $dom->createElement('cbc:TaxAmount', number_format((float)$line['tax_amount'], 3, '.', ''));
$ltaxAmt->setAttribute('currencyID', 'JOD');
$lineTax->appendChild($ltaxAmt);
$invLine->appendChild($lineTax);
$item = $dom->createElement('cac:Item');
$item->appendChild($dom->createElement('cbc:Description', $line['description']));
$itc = $dom->createElement('cac:TaxCategory');
$itc->appendChild($dom->createElement('cbc:ID', 'S'));
$itc->appendChild($dom->createElement('cbc:Percent', number_format((float)$line['tax_rate'] * 100, 2, '.', '')));
$its = $dom->createElement('cac:TaxScheme');
$its->appendChild($dom->createElement('cbc:ID', 'VAT'));
$itc->appendChild($its);
$item->appendChild($itc);
$invLine->appendChild($item);
$price = $dom->createElement('cac:Price');
$pamt = $dom->createElement('cbc:PriceAmount', number_format((float)$line['unit_price'], 3, '.', ''));
$pamt->setAttribute('currencyID', 'JOD');
$price->appendChild($pamt);
$invLine->appendChild($price);
$root->appendChild($invLine);
}
return $dom->saveXML();
}
}