Day 4: Add Invoice Line Items to UBL XML for IRB #invoiceline #ubl21

SEO Title: Day 4: Add Invoice Line Items to UBL XML for IRB #invoiceline #ubl21
Focus Keyphrase: IRB e-invoice integration
Meta Description: Learn how to add invoice line items in UBL 2.1 XML for IRB e-invoice integration using Laravel. Include quantity, unit price, tax category, and totals.

Building Line Items in Your IRB UBL Invoice

Today we’ll enhance the IRB e-invoice integration by adding <cac:InvoiceLine> elements. These include:

  • 📦 Quantity and unit
  • 💵 Unit price and line extension
  • 📊 Tax category and rate
  • 🧾 Item name and description

Step 1: Sample Line Item Data

$invoiceData['items'] = [
    [
        'name' => 'Vehicle GPS Plan - 12 Months',
        'quantity' => 1,
        'unit' => 'EA',
        'unit_price' => 300.00,
        'tax_percent' => 8.0,
        'currency' => 'MYR'
    ]
];

Step 2: Add “ to the XML

In your generate() method, after appending customer data:

$lineId = 1;
foreach ($invoiceData['items'] as $item) {
    $line = $doc->createElementNS($cac, 'cac:InvoiceLine');

    // Line identification and quantity
    $line->appendChild($doc->createElementNS($cbc, 'cbc:ID', $lineId++));
    $line->appendChild($doc->createElementNS($cbc, 'cbc:InvoicedQuantity', $item['quantity']));
    $line->appendChild($doc->createElementNS($cbc, 'cbc:LineExtensionAmount'))
         ->appendChild($doc->createTextNode(number_format($item['unit_price'] * $item['quantity'], 2, '.', '')));

    // Item description
    $itemNode = $doc->createElementNS($cac, 'cac:Item');
    $itemNode->appendChild($doc->createElementNS($cbc, 'cbc:Name', $item['name']));
    $line->appendChild($itemNode);

    // Price information
    $priceNode = $doc->createElementNS($cac, 'cac:Price');
    $priceNode->appendChild($doc->createElementNS($cbc, 'cbc:PriceAmount', number_format($item['unit_price'], 2, '.', '')));
    $line->appendChild($priceNode);

    // Tax information
    $taxCategory = $doc->createElementNS($cac, 'cac:TaxCategory');
    $taxCategory->appendChild($doc->createElementNS($cbc, 'cbc:Percent', $item['tax_percent']));
    $taxScheme = $doc->createElementNS($cac, 'cac:TaxScheme');
    $taxScheme->appendChild($doc->createElementNS($cbc, 'cbc:ID', 'SST'));
    $taxCategory->appendChild($taxScheme);

    $taxSubtotal = $doc->createElementNS($cac, 'cac:TaxSubtotal');
    $taxSubtotal->appendChild($doc->createElementNS($cbc, 'cbc:TaxAmount',
        number_format($item['unit_price'] * ($item['tax_percent'] / 100), 2, '.', '')));
    $taxSubtotal->appendChild($taxCategory);

    $taxTotal = $doc->createElementNS($cac, 'cac:TaxTotal');
    $taxTotal->appendChild($taxSubtotal);
    $line->appendChild($taxTotal);

    $invoice->appendChild($line);
}

Step 3: Output the Updated UBL Invoice

Visit [http://localhost:8000/irb/xml-preview](http://localhost:8000/irb/xml-preview) and you’ll now see:

  • <cac:InvoiceLine> with product details
  • ✅ Tax amounts per item
  • ✅ PriceAmount and Quantity

UBL Tip: Use Correct Decimal Format

Always format numeric values with 2 decimal places using dot (`.`), e.g., 300.00 — not 300.

Coming Tomorrow

In Day 5: Add Summary Totals and Tax Totals to UBL #ublsummary #irbtax, we’ll calculate the full invoice total, SST tax, and grand total at the bottom of the UBL.


Tags: #UBL21 #IRBIntegration #InvoiceLine #TaxBreakdown #LaravelXML

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.