🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 16:43
This commit is contained in:
@@ -1,112 +1,147 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\JoFotara;
|
||||
|
||||
/**
|
||||
* UBLGeneratorService
|
||||
*
|
||||
* Generates UBL 2.1 compliant XML for the Jordanian Income and Sales Tax Department (ISTD).
|
||||
* Based on the JoFotara Technical Specifications.
|
||||
* Generates UBL 2.1 compliant XML using DOMDocument for precise namespace control.
|
||||
*/
|
||||
final class UBLGeneratorService
|
||||
{
|
||||
public function generate(array $invoice, array $lines, array $company): string
|
||||
{
|
||||
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"></Invoice>');
|
||||
$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
|
||||
$xml->addChild('cbc:UBLVersionID', '2.1');
|
||||
$xml->addChild('cbc:CustomizationID', 'TRADACO-2.1');
|
||||
$xml->addChild('cbc:ProfileID', 'reporting:1.0');
|
||||
$xml->addChild('cbc:ID', $invoice['invoice_number']);
|
||||
$xml->addChild('cbc:IssueDate', $invoice['invoice_date']);
|
||||
$xml->addChild('cbc:InvoiceTypeCode', $invoice['ubl_type_code'] ?? '388')->addAttribute('name', $invoice['invoice_category'] ?? '01');
|
||||
$xml->addChild('cbc:DocumentCurrencyCode', 'JOD');
|
||||
$xml->addChild('cbc:TaxCurrencyCode', 'JOD');
|
||||
|
||||
// 2. AccountingSupplierParty (The Seller/Company)
|
||||
$supplier = $xml->addChild('cac:AccountingSupplierParty');
|
||||
$sParty = $supplier->addChild('cac:Party');
|
||||
$sParty->addChild('cac:PartyIdentification')->addChild('cbc:ID', $company['tax_identification_number'])->addAttribute('schemeID', 'TN');
|
||||
$sName = $sParty->addChild('cac:PartyName');
|
||||
$sName->addChild('cbc:Name', $company['name']);
|
||||
$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']));
|
||||
|
||||
$sAddr = $sParty->addChild('cac:PostalAddress');
|
||||
$sAddr->addChild('cbc:CityName', $company['city'] ?? 'Amman');
|
||||
$sAddr->addChild('cac:Country')->addChild('cbc:IdentificationCode', 'JO');
|
||||
$typeCode = $dom->createElement('cbc:InvoiceTypeCode', $invoice['ubl_type_code'] ?? '388');
|
||||
$typeCode->setAttribute('name', $invoice['invoice_category'] ?? '01');
|
||||
$root->appendChild($typeCode);
|
||||
|
||||
$sTaxScheme = $sParty->addChild('cac:PartyTaxScheme');
|
||||
$sTaxScheme->addChild('cbc:RegistrationName', $company['name']);
|
||||
$sTaxScheme->addChild('cbc:CompanyID', $company['tax_identification_number']);
|
||||
$sTaxScheme->addChild('cac:TaxScheme')->addChild('cbc:ID', 'VAT');
|
||||
$root->appendChild($dom->createElement('cbc:DocumentCurrencyCode', 'JOD'));
|
||||
$root->appendChild($dom->createElement('cbc:TaxCurrencyCode', 'JOD'));
|
||||
|
||||
$sLegalEntity = $sParty->addChild('cac:PartyLegalEntity');
|
||||
$sLegalEntity->addChild('cbc:RegistrationName', $company['name']);
|
||||
$sLegalEntity->addChild('cbc:CompanyID', $company['tax_identification_number']);
|
||||
|
||||
// 3. AccountingCustomerParty (The Buyer)
|
||||
$customer = $xml->addChild('cac:AccountingCustomerParty');
|
||||
$cParty = $customer->addChild('cac:Party');
|
||||
// 2. AccountingSupplierParty
|
||||
$supplierParty = $dom->createElement('cac:AccountingSupplierParty');
|
||||
$party = $dom->createElement('cac:Party');
|
||||
|
||||
$partyId = $dom->createElement('cac:PartyIdentification');
|
||||
$idNode = $dom->createElement('cbc:ID', $company['tax_identification_number']);
|
||||
$idNode->setAttribute('schemeID', 'TN');
|
||||
$partyId->appendChild($idNode);
|
||||
$party->appendChild($partyId);
|
||||
|
||||
$partyName = $dom->createElement('cac:PartyName');
|
||||
$partyName->appendChild($dom->createElement('cbc:Name', $company['name']));
|
||||
$party->appendChild($partyName);
|
||||
|
||||
$addr = $dom->createElement('cac:PostalAddress');
|
||||
$addr->appendChild($dom->createElement('cbc:CityName', $company['city'] ?? 'Amman'));
|
||||
$country = $dom->createElement('cac:Country');
|
||||
$country->appendChild($dom->createElement('cbc:IdentificationCode', 'JO'));
|
||||
$addr->appendChild($country);
|
||||
$party->appendChild($addr);
|
||||
|
||||
$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']));
|
||||
$party->appendChild($legalEntity);
|
||||
|
||||
$supplierParty->appendChild($party);
|
||||
$root->appendChild($supplierParty);
|
||||
|
||||
// 3. AccountingCustomerParty
|
||||
$customerParty = $dom->createElement('cac:AccountingCustomerParty');
|
||||
$cParty = $dom->createElement('cac:Party');
|
||||
|
||||
$cName = $dom->createElement('cac:PartyName');
|
||||
$cName->appendChild($dom->createElement('cbc:Name', $invoice['buyer_name'] ?? 'عميل عام'));
|
||||
$cParty->appendChild($cName);
|
||||
|
||||
if (!empty($invoice['buyer_tin'])) {
|
||||
$cParty->addChild('cac:PartyIdentification')->addChild('cbc:ID', $invoice['buyer_tin'])->addAttribute('schemeID', 'TN');
|
||||
} elseif (!empty($invoice['buyer_national_id'])) {
|
||||
$cParty->addChild('cac:PartyIdentification')->addChild('cbc:ID', $invoice['buyer_national_id'])->addAttribute('schemeID', 'NID');
|
||||
$cId = $dom->createElement('cac:PartyIdentification');
|
||||
$cidNode = $dom->createElement('cbc:ID', $invoice['buyer_tin']);
|
||||
$cidNode->setAttribute('schemeID', 'TN');
|
||||
$cId->appendChild($cidNode);
|
||||
$cParty->appendChild($cId);
|
||||
}
|
||||
|
||||
$cName = $cParty->addChild('cac:PartyName');
|
||||
$cName->addChild('cbc:Name', $invoice['buyer_name'] ?? 'General Customer');
|
||||
|
||||
$cTaxScheme = $cParty->addChild('cac:PartyTaxScheme');
|
||||
$cTaxScheme->addChild('cac:TaxScheme')->addChild('cbc:ID', 'VAT');
|
||||
$customerParty->appendChild($cParty);
|
||||
$root->appendChild($customerParty);
|
||||
|
||||
// 4. PaymentMeans
|
||||
$payment = $xml->addChild('cac:PaymentMeans');
|
||||
$payment->addChild('cbc:PaymentMeansCode', $invoice['payment_method_code'] ?? '10');
|
||||
$paymentMeans = $dom->createElement('cac:PaymentMeans');
|
||||
$paymentMeans->appendChild($dom->createElement('cbc:PaymentMeansCode', $invoice['payment_method_code'] ?? '10'));
|
||||
$root->appendChild($paymentMeans);
|
||||
|
||||
// 5. TaxTotal
|
||||
$taxTotal = $xml->addChild('cac:TaxTotal');
|
||||
$taxTotal->addChild('cbc:TaxAmount', number_format((float)$invoice['tax_amount'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
|
||||
$taxSubtotal = $taxTotal->addChild('cac:TaxSubtotal');
|
||||
$taxSubtotal->addChild('cbc:TaxableAmount', number_format((float)$invoice['subtotal'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$taxSubtotal->addChild('cbc:TaxAmount', number_format((float)$invoice['tax_amount'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$taxCategory = $taxSubtotal->addChild('cac:TaxCategory');
|
||||
$taxCategory->addChild('cbc:ID', 'S');
|
||||
$taxCategory->addChild('cbc:Percent', '16.00'); // Default Jordan VAT
|
||||
$taxCategory->addChild('cac:TaxScheme')->addChild('cbc:ID', 'VAT');
|
||||
$taxTotal = $dom->createElement('cac:TaxTotal');
|
||||
$taxAmt = $dom->createElement('cbc:TaxAmount', number_format((float)$invoice['tax_amount'], 3, '.', ''));
|
||||
$taxAmt->setAttribute('currencyID', 'JOD');
|
||||
$taxTotal->appendChild($taxAmt);
|
||||
$root->appendChild($taxTotal);
|
||||
|
||||
// 6. LegalMonetaryTotal
|
||||
$legalMonetaryTotal = $xml->addChild('cac:LegalMonetaryTotal');
|
||||
$legalMonetaryTotal->addChild('cbc:LineExtensionAmount', number_format((float)$invoice['subtotal'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$legalMonetaryTotal->addChild('cbc:TaxExclusiveAmount', number_format((float)$invoice['subtotal'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$legalMonetaryTotal->addChild('cbc:TaxInclusiveAmount', number_format((float)$invoice['grand_total'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$legalMonetaryTotal->addChild('cbc:AllowanceTotalAmount', number_format((float)($invoice['discount_total'] ?? 0), 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$legalMonetaryTotal->addChild('cbc:PayableAmount', number_format((float)$invoice['grand_total'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$monetaryTotal = $dom->createElement('cac:LegalMonetaryTotal');
|
||||
$fields = [
|
||||
'LineExtensionAmount' => $invoice['subtotal'],
|
||||
'TaxExclusiveAmount' => $invoice['subtotal'],
|
||||
'TaxInclusiveAmount' => $invoice['grand_total'],
|
||||
'AllowanceTotalAmount' => $invoice['discount_total'] ?? 0,
|
||||
'PayableAmount' => $invoice['grand_total']
|
||||
];
|
||||
foreach ($fields as $field => $val) {
|
||||
$node = $dom->createElement('cbc:' . $field, number_format((float)$val, 3, '.', ''));
|
||||
$node->setAttribute('currencyID', 'JOD');
|
||||
$monetaryTotal->appendChild($node);
|
||||
}
|
||||
$root->appendChild($monetaryTotal);
|
||||
|
||||
// 7. Invoice Lines
|
||||
foreach ($lines as $line) {
|
||||
$invoiceLine = $xml->addChild('cac:InvoiceLine');
|
||||
$invoiceLine->addChild('cbc:ID', (string)$line['line_number']);
|
||||
$invoiceLine->addChild('cbc:InvoicedQuantity', number_format((float)$line['quantity'], 3, '.', ''))->addAttribute('unitCode', 'PCE');
|
||||
$invoiceLine->addChild('cbc:LineExtensionAmount', number_format((float)$line['line_total'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$iLine = $dom->createElement('cac:InvoiceLine');
|
||||
$iLine->appendChild($dom->createElement('cbc:ID', (string)$line['line_number']));
|
||||
|
||||
$item = $invoiceLine->addChild('cac:Item');
|
||||
$item->addChild('cbc:Description', $line['description']);
|
||||
$itemTaxCategory = $item->addChild('cac:TaxCategory');
|
||||
$itemTaxCategory->addChild('cbc:ID', 'S');
|
||||
$itemTaxCategory->addChild('cbc:Percent', '16.00');
|
||||
$itemTaxCategory->addChild('cac:TaxScheme')->addChild('cbc:ID', 'VAT');
|
||||
$qty = $dom->createElement('cbc:InvoicedQuantity', number_format((float)$line['quantity'], 3, '.', ''));
|
||||
$qty->setAttribute('unitCode', 'PCE');
|
||||
$iLine->appendChild($qty);
|
||||
|
||||
$lineExt = $dom->createElement('cbc:LineExtensionAmount', number_format((float)$line['line_total'], 3, '.', ''));
|
||||
$lineExt->setAttribute('currencyID', 'JOD');
|
||||
$iLine->appendChild($lineExt);
|
||||
|
||||
$price = $invoiceLine->addChild('cac:Price');
|
||||
$price->addChild('cbc:PriceAmount', number_format((float)$line['unit_price'], 3, '.', ''))->addAttribute('currencyID', 'JOD');
|
||||
$item = $dom->createElement('cac:Item');
|
||||
$item->appendChild($dom->createElement('cbc:Description', $line['description']));
|
||||
$iLine->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);
|
||||
$iLine->appendChild($price);
|
||||
|
||||
$root->appendChild($iLine);
|
||||
}
|
||||
|
||||
// Return formatted XML
|
||||
$dom = dom_import_simplexml($xml)->ownerDocument;
|
||||
$dom->formatOutput = true;
|
||||
return $dom->saveXML();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user