Day 5: Add Summary Totals and Tax Totals to UBL #ublsummary #irbtax

SEO Title: Day 5: Add Summary Totals and Tax Totals to UBL #ublsummary #irbtax
Focus Keyphrase: IRB e-invoice integration
Meta Description: Add tax totals and monetary summary fields like SST and grand total to your IRB e-invoice UBL XML using Laravel.

Adding Totals to Your IRB UBL Invoice

Your UBL invoice must include tax totals (SST) and monetary totals (subtotal, tax, grand total). Today, we’ll add the required cac:TaxTotal and cac:LegalMonetaryTotal nodes at the bottom of the invoice.

Step 1: Update Dummy Invoice Data

$invoiceData['items'] = [
  [
    'name' => 'Monthly Service Plan',
    'quantity' => 2,
    'unit' => 'EA',
    'unit_price' => 150.00,
    'tax_percent' => 8.0,
    'currency' => 'MYR'
  ]
];

Step 2: Calculate Totals Before Generating XML


$totalExclTax = 0;
$totalTax = 0;

foreach ($invoiceData['items'] as $item) {
    $lineTotal = $item['quantity'] * $item['unit_price'];
    $taxAmount = $lineTotal * ($item['tax_percent'] / 100);

    $totalExclTax += $lineTotal;
    $totalTax += $taxAmount;
}

$grandTotal = $totalExclTax + $totalTax;

Step 3: Add TaxTotal and LegalMonetaryTotal to XML


$taxTotalNode = $doc->createElementNS($cac, 'cac:TaxTotal');
$taxTotalNode->appendChild($doc->createElementNS($cbc, 'cbc:TaxAmount', number_format($totalTax, 2, '.', '')));
$invoice->appendChild($taxTotalNode);

$legalTotalNode = $doc->createElementNS($cac, 'cac:LegalMonetaryTotal');
$legalTotalNode->appendChild($doc->createElementNS($cbc, 'cbc:LineExtensionAmount', number_format($totalExclTax, 2, '.', '')));
$legalTotalNode->appendChild($doc->createElementNS($cbc, 'cbc:TaxExclusiveAmount', number_format($totalExclTax, 2, '.', '')));
$legalTotalNode->appendChild($doc->createElementNS($cbc, 'cbc:TaxInclusiveAmount', number_format($grandTotal, 2, '.', '')));
$legalTotalNode->appendChild($doc->createElementNS($cbc, 'cbc:PayableAmount', number_format($grandTotal, 2, '.', '')));
$invoice->appendChild($legalTotalNode);

Step 4: Verify the UBL XML

Open your browser to /irb/xml-preview. You should see:

  • cac:TaxTotal with calculated SST amount
  • cac:LegalMonetaryTotal with all subtotal and payable amounts

Best Practices

  • Use 2 decimal precision consistently
  • TaxExclusiveAmount = LineExtensionAmount
  • PayableAmount = TaxInclusiveAmount

Coming Up in Day 6

Tomorrow, we’ll digitally sign the XML using your IRB certificate with XMLSecLibs and add a ds:Signature block.


Tags: #UBL #IRBIntegration #LaravelXML #TaxTotals #EInvoice

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.