### Add Allowances and Charges to Invoice in PHP Source: https://context7.com/blafasthq/ubl-invoice/llms.txt Demonstrates how to add document-level discounts (allowances) and surcharges (charges) to an invoice. This involves creating AllowanceCharge objects, setting their properties like amount, reason, and tax information, and then adding them to the invoice object. The example also highlights how these affect the legal monetary total. ```php setId('VAT'); $discount = (new AllowanceCharge()) ->setChargeIndicator(false) // false = allowance/discount ->setAllowanceChargeReason('Volume discount') ->setAmount(5.00) ->setBaseAmount(100.00) ->setMultiplierFactorNumeric(0.05); // 5% discount $discountTaxCategory = (new TaxCategory()) ->setId('S') ->setPercent(21.00) ->setTaxScheme($taxScheme); $discount->setTaxCategory($discountTaxCategory); // Create a charge (surcharge) $shippingCharge = (new AllowanceCharge()) ->setChargeIndicator(true) // true = charge/surcharge ->setAllowanceChargeReason('Shipping fee') ->setAmount(10.00); $chargeTaxCategory = (new TaxCategory()) ->setId('S') ->setPercent(21.00) ->setTaxScheme($taxScheme); $shippingCharge->setTaxCategory($chargeTaxCategory); // Add to invoice (assuming $invoice is already created) $invoice = new Invoice(); // ... set other invoice properties ... $invoice->setAllowanceCharges([$discount, $shippingCharge]); // Update legal monetary total to reflect allowances/charges // LineExtension: 100.00 // Allowance: -5.00 // Charge: +10.00 // Tax Exclusive: 105.00 // Tax (21%): 22.05 // Tax Inclusive: 127.05 ``` -------------------------------- ### Install UBL-Invoice PHP Library with Composer Source: https://github.com/blafasthq/ubl-invoice/blob/master/README.md Installs the UBL-Invoice PHP library version 2.0-beta using Composer. This is the recommended way to add the library to your project. ```zsh $ composer require num-num/ubl-invoice:^2.0@beta ``` -------------------------------- ### PHP: Create UBL Invoice with Multiple Line Items and Tax Rates Source: https://context7.com/blafasthq/ubl-invoice/llms.txt This PHP code snippet demonstrates how to build a UBL invoice object using the `numnum/ubl` library. It includes creating multiple `InvoiceLine` objects, each associated with a specific `Item`, `Price`, and `ClassifiedTaxCategory` that defines its tax rate. The example also shows how to aggregate these lines and calculate the `TaxTotal` and `LegalMonetaryTotal` based on the different tax categories applied. ```php setId('VAT'); // Line 1: Standard rate (21%) $item1 = (new Item()) ->setName('Laptop Computer') ->setDescription('Business laptop with 16GB RAM') ->setSellersItemIdentification('LAP-001'); $taxCategory1 = (new ClassifiedTaxCategory()) ->setId('S') ->setPercent(21.00) ->setTaxScheme($taxScheme); $item1->setClassifiedTaxCategory($taxCategory1); $price1 = (new Price()) ->setBaseQuantity(1) ->setUnitCode(UnitCode::UNIT) ->setPriceAmount(1000.00); $line1 = (new InvoiceLine()) ->setId('1') ->setItem($item1) ->setPrice($price1) ->setLineExtensionAmount(2000.00) ->setInvoicedQuantity(2); // Line 2: Reduced rate (6%) $item2 = (new Item()) ->setName('Technical Book') ->setDescription('Professional development book') ->setSellersItemIdentification('BOOK-042'); $taxCategory2 = (new ClassifiedTaxCategory()) ->setId('AA') // Reduced rate code ->setPercent(6.00) ->setTaxScheme($taxScheme); $item2->setClassifiedTaxCategory($taxCategory2); $price2 = (new Price()) ->setBaseQuantity(1) ->setUnitCode(UnitCode::UNIT) ->setPriceAmount(50.00); $line2 = (new InvoiceLine()) ->setId('2') ->setItem($item2) ->setPrice($price2) ->setLineExtensionAmount(50.00) ->setInvoicedQuantity(1); // Line 3: Zero-rated export (0%) $item3 = (new Item()) ->setName('Export Service') ->setDescription('Consulting services - EU export'); $taxCategory3 = (new ClassifiedTaxCategory()) ->setId('E') // Exempt/Export code ->setPercent(0.00) ->setTaxScheme($taxScheme); $item3->setClassifiedTaxCategory($taxCategory3); $price3 = (new Price()) ->setBaseQuantity(8) ->setUnitCode(UnitCode::HOUR) ->setPriceAmount(100.00); $line3 = (new InvoiceLine()) ->setId('3') ->setItem($item3) ->setPrice($price3) ->setLineExtensionAmount(800.00) ->setInvoicedQuantity(8); // Calculate tax subtotals $taxSubTotal1 = (new TaxSubTotal()) ->setTaxableAmount(2000.00) ->setTaxAmount(420.00) ->setTaxCategory((new TaxCategory()) ->setId('S') ->setPercent(21.00) ->setTaxScheme($taxScheme)); $taxSubTotal2 = (new TaxSubTotal()) ->setTaxableAmount(50.00) ->setTaxAmount(3.00) ->setTaxCategory((new TaxCategory()) ->setId('AA') ->setPercent(6.00) ->setTaxScheme($taxScheme)); $taxSubTotal3 = (new TaxSubTotal()) ->setTaxableAmount(800.00) ->setTaxAmount(0.00) ->setTaxCategory((new TaxCategory()) ->setId('E') ->setPercent(0.00) ->setTaxScheme($taxScheme)); $taxTotal = (new TaxTotal()) ->addTaxSubTotal($taxSubTotal1) ->addTaxSubTotal($taxSubTotal2) ->addTaxSubTotal($taxSubTotal3) ->setTaxAmount(423.00); // 420 + 3 + 0 // Legal monetary total $legalMonetaryTotal = (new LegalMonetaryTotal()) ->setLineExtensionAmount(2850.00) // 2000 + 50 + 800 ->setTaxExclusiveAmount(2850.00) ->setTaxInclusiveAmount(3273.00) // 2850 + 423 ->setPayableAmount(3273.00); // Add lines to invoice $invoice->setInvoiceLines([$line1, $line2, $line3]) ->setTaxTotal($taxTotal) ->setLegalMonetaryTotal($legalMonetaryTotal); ``` -------------------------------- ### Configure Payment Terms and Delivery Information in UBL Invoice (PHP) Source: https://context7.com/blafasthq/ubl-invoice/llms.txt Sets up payment terms, including settlement discounts and periods, along with delivery details such as dates and addresses. It also configures order references and invoice periods. Dependencies include classes from the NumNum/UBL library. Inputs are DateTime objects, strings, and other UBL objects. Outputs an Invoice object with these configurations. ```php setStartDate(new DateTime()) ->setEndDate((new DateTime())->modify('+10 days')); $paymentTerms = (new PaymentTerms()) ->setNote('Payment within 30 days. 2% discount if paid within 10 days.') ->setSettlementDiscountPercent(2.0) ->setSettlementPeriod($settlementPeriod); // Delivery information $deliveryCountry = (new Country())->setIdentificationCode('NL'); $deliveryAddress = (new Address()) ->setStreetName('Delivery Street 42') ->setCityName('Amsterdam') ->setPostalZone('1000AA') ->setCountry($deliveryCountry); $delivery = (new Delivery()) ->setActualDeliveryDate(new DateTime('2024-02-01')) ->setDeliveryLocation($deliveryAddress); // Order reference $orderReference = (new OrderReference()) ->setId('PO-2024-001') ->setSalesOrderId('SO-2024-042'); // Invoice period $invoicePeriod = (new InvoicePeriod()) ->setStartDate(new DateTime('2024-01-01')) ->setEndDate(new DateTime('2024-01-31')); // Add to invoice $invoice = new Invoice(); $invoice ->setPaymentTerms($paymentTerms) ->setDelivery($delivery) ->setOrderReference($orderReference) ->setInvoicePeriod($invoicePeriod) ->setDueDate((new DateTime())->modify('+30 days')); ``` -------------------------------- ### Create Credit Note with Billing Reference in PHP Source: https://context7.com/blafasthq/ubl-invoice/llms.txt Generates a credit note XML file that references an original invoice. This involves setting up parties, defining the billing reference, creating credit note lines, and calculating tax and monetary totals. The output is an XML string that can be saved to a file. ```php setIdentificationCode('BE'); $address = (new Address()) ->setStreetName('Korenmarkt') ->setBuildingNumber(1) ->setCityName('Gent') ->setPostalZone('9000') ->setCountry($country); $supplierParty = (new Party()) ->setName('Supplier Company Name') ->setPhysicalLocation($address) ->setPostalAddress($address); $clientParty = (new Party()) ->setName('My client') ->setPostalAddress($address); // Reference to original invoice $billingReference = (new BillingReference()) ->setInvoiceDocumentReference( (new InvoiceDocumentReference()) ->setOriginalInvoiceId('INV-1234') ->setIssueDate(new DateTime('2024-01-15')) ); // Credit note line $taxScheme = (new TaxScheme())->setId('VAT'); $productItem = (new Item()) ->setName('Product Name') ->setDescription('Product Description') ->setSellersItemIdentification('SELLERID') ->setBuyersItemIdentification('BUYERID'); $price = (new Price()) ->setBaseQuantity(1) ->setUnitCode(UnitCode::UNIT) ->setPriceAmount(10.00); $lineTaxTotal = (new TaxTotal())->setTaxAmount(2.10); $creditNoteLine = (new CreditNoteLine()) ->setId(0) ->setItem($productItem) ->setPrice($price) ->setTaxTotal($lineTaxTotal) ->setInvoicedQuantity(1); // Tax totals $taxCategory = (new TaxCategory()) ->setId('S') ->setName('VAT21%') ->setPercent(0.21) ->setTaxScheme($taxScheme); $taxSubTotal = (new TaxSubTotal()) ->setTaxableAmount(10.00) ->setTaxAmount(2.10) ->setTaxCategory($taxCategory); $taxTotal = (new TaxTotal()) ->addTaxSubTotal($taxSubTotal) ->setTaxAmount(2.10); $legalMonetaryTotal = (new LegalMonetaryTotal()) ->setPayableAmount(12.10) ->setAllowanceTotalAmount(0); // Create credit note $accountingSupplierParty = (new AccountingParty())->setParty($supplierParty); $accountingCustomerParty = (new AccountingParty())->setParty($clientParty); $creditNote = (new CreditNote()) ->setUBLVersionId('2.1') ->setId('CN-5678') ->setIssueDate(new DateTime()) ->setNote('Credit note for returned goods') ->setBillingReference($billingReference) ->setAccountingSupplierParty($accountingSupplierParty) ->setAccountingCustomerParty($accountingCustomerParty) ->setCreditNoteLines([$creditNoteLine]) ->setLegalMonetaryTotal($legalMonetaryTotal) ->setTaxTotal($taxTotal); // Generate XML $generator = new Generator(); $xmlString = $generator->creditNote($creditNote, 'EUR'); file_put_contents('creditnote.xml', $xmlString); echo "Credit note generated successfully\n"; ``` -------------------------------- ### Create UBL 2.1 Invoice in PHP Source: https://context7.com/blafasthq/ubl-invoice/llms.txt This snippet shows how to programmatically create a UBL 2.1 invoice in PHP. It includes defining supplier and customer parties, address details, tax schemes, invoice lines with items and prices, tax totals, and payment means. The `NumNumUBL` library is used for this purpose. The output is an XML string representing the invoice. ```php setId('VAT'); // Supplier country and address $country = (new Country())->setIdentificationCode('BE'); $address = (new Address()) ->setStreetName('Korenmarkt 1') ->setAdditionalStreetName('Building A') ->setCityName('Gent') ->setPostalZone('9000') ->setCountry($country); // Supplier party with legal entity and tax info $supplierLegalEntity = (new LegalEntity()) ->setRegistrationName('Supplier Company Name') ->setCompanyId('BE123456789'); $supplierPartyTaxScheme = (new PartyTaxScheme()) ->setTaxScheme($taxScheme) ->setCompanyId('BE123456789'); $supplierParty = (new Party()) ->setName('Supplier Company Name') ->setLegalEntity($supplierLegalEntity) ->setPartyTaxScheme($supplierPartyTaxScheme) ->setPartyIdentificationId('BE123456789') ->setPostalAddress($address); // Customer party $clientLegalEntity = (new LegalEntity()) ->setRegistrationName('Client Company Name') ->setCompanyId('Client Company Registration'); $clientPartyTaxScheme = (new PartyTaxScheme()) ->setTaxScheme($taxScheme) ->setCompanyId('BE123456789'); $clientParty = (new Party()) ->setName('Client Company Name') ->setLegalEntity($clientLegalEntity) ->setPartyTaxScheme($clientPartyTaxScheme) ->setPartyIdentificationId('BE123456789') ->setPostalAddress($address); // Payment means with bank account $financialInstitutionBranch = (new FinancialInstitutionBranch()) ->setId('RABONL2U'); $payeeFinancialAccount = (new PayeeFinancialAccount()) ->setFinancialInstitutionBranch($financialInstitutionBranch) ->setName('Customer Account Holder') ->setId('NL00RABO0000000000'); $paymentMeans = (new PaymentMeans()) ->setPayeeFinancialAccount($payeeFinancialAccount) ->setPaymentMeansCode(UNCL4461::DEBIT_TRANSFER, []) ->setPaymentId('our invoice 1234'); // Invoice line with item and price $classifiedTaxCategory = (new ClassifiedTaxCategory()) ->setId('S') ->setPercent(21.00) ->setTaxScheme($taxScheme); $productItem = (new Item()) ->setName('Product Name') ->setClassifiedTaxCategory($classifiedTaxCategory) ->setDescription('Product Description'); $price = (new Price()) ->setBaseQuantity(1) ->setUnitCode(UnitCode::UNIT) ->setPriceAmount(10.00); $invoiceLine = (new InvoiceLine()) ->setId(0) ->setItem($productItem) ->setPrice($price) ->setLineExtensionAmount(10.00) ->setInvoicedQuantity(1); // Tax totals $taxCategory = (new TaxCategory()) ->setId('S', []) ->setPercent(21.00) ->setTaxScheme($taxScheme); $taxSubTotal = (new TaxSubTotal()) ->setTaxableAmount(10.00) ->setTaxAmount(2.10) ->setTaxCategory($taxCategory); $taxTotal = (new TaxTotal()) ->addTaxSubTotal($taxSubTotal) ->setTaxAmount(2.10); // Legal monetary total $legalMonetaryTotal = (new LegalMonetaryTotal()) ->setPayableAmount(12.10) ->setPayableRoundingAmount(0) ->setTaxInclusiveAmount(12.10) ->setLineExtensionAmount(10.00) ->setTaxExclusiveAmount(10.00); // Create invoice $accountingSupplierParty = (new AccountingParty())->setParty($supplierParty); $accountingCustomerParty = (new AccountingParty())->setParty($clientParty); $invoice = (new Invoice()) ->setUBLVersionId('2.1') ->setCustomizationId('urn:cen.eu:en16931:2017') ->setId('INV-1234') ->setIssueDate(new DateTime()) ->setDueDate((new DateTime())->modify('+30 days')) ->setNote('Payment within 30 days') ->setAccountingSupplierParty($accountingSupplierParty) ->setAccountingCustomerParty($accountingCustomerParty) ->setInvoiceLines([$invoiceLine]) ->setLegalMonetaryTotal($legalMonetaryTotal) ->setTaxTotal($taxTotal) ->setPaymentMeans([$paymentMeans]) ->setBuyerReference('BUYER_REF'); // Generate XML $generator = new Generator(); $xmlString = $generator->invoice($invoice, 'EUR'); // Save to file or output file_put_contents('invoice.xml', $xmlString); echo "Invoice generated successfully\n"; ``` -------------------------------- ### Parse UBL Invoice/Credit Note XML to PHP Objects Source: https://context7.com/blafasthq/ubl-invoice/llms.txt This PHP code demonstrates how to initialize the UBL reader, parse an XML content string representing either an invoice or a credit note, and access various properties of the parsed document. It includes accessing IDs, dates, parties, addresses, line items, monetary totals, and tax information. Error handling for specific document types (Invoice or CreditNote) is also shown. ```php parse($xmlContent); // Access invoice properties if ($invoice instanceof Invoice) { echo "Invoice ID: " . $invoice->getId() . "\n"; echo "Issue Date: " . $invoice->getIssueDate()->format('Y-m-d') . "\n"; echo "Currency: " . $invoice->getDocumentCurrencyCode() . "\n"; // Access supplier information $supplier = $invoice->getAccountingSupplierParty(); if ($supplier) { $supplierParty = $supplier->getParty(); echo "Supplier: " . $supplierParty->getName() . "\n"; $supplierAddress = $supplierParty->getPostalAddress(); if ($supplierAddress) { echo "Address: " . $supplierAddress->getStreetName() . ", "; echo $supplierAddress->getCityName() . "\n"; } } // Access customer information $customer = $invoice->getAccountingCustomerParty(); if ($customer) { $customerParty = $customer->getParty(); echo "Customer: " . $customerParty->getName() . "\n"; } // Access invoice lines $invoiceLines = $invoice->getInvoiceLines(); if ($invoiceLines) { foreach ($invoiceLines as $line) { $item = $line->getItem(); $price = $line->getPrice(); echo "Line " . $line->getId() . ": " . $item->getName(); echo " - Qty: " . $line->getInvoicedQuantity(); echo " - Price: " . $price->getPriceAmount() . "\n"; } } // Access monetary totals $monetaryTotal = $invoice->getLegalMonetaryTotal(); if ($monetaryTotal) { echo "Tax Exclusive: " . $monetaryTotal->getTaxExclusiveAmount() . "\n"; echo "Tax Inclusive: " . $monetaryTotal->getTaxInclusiveAmount() . "\n"; echo "Payable Amount: " . $monetaryTotal->getPayableAmount() . "\n"; } // Access tax information $taxTotal = $invoice->getTaxTotal(); if ($taxTotal) { echo "Total Tax: " . $taxTotal->getTaxAmount() . "\n"; $taxSubtotals = $taxTotal->getTaxSubtotals(); foreach ($taxSubtotals as $subtotal) { $category = $subtotal->getTaxCategory(); echo "Tax Rate: " . $category->getPercent() . "%\n"; } } } // Handle credit notes similarly if ($invoice instanceof CreditNote) { echo "Credit Note ID: " . $invoice->getId() . "\n"; $creditNoteLines = $invoice->getCreditNoteLines(); // Process credit note lines... } ``` -------------------------------- ### Add Document Attachments and References to UBL Invoice (PHP) Source: https://context7.com/blafasthq/ubl-invoice/llms.txt Allows attaching documents, both embedded (like PDFs) and referenced via external URLs. It also supports adding references to other types of documents like timesheets. Dependencies include classes from the NumNum/UBL library. Inputs are Attachment and AdditionalDocumentReference objects. Outputs an Invoice object with the added document references. ```php setExternalReference('technical-specs.pdf') ->setFilename('technical-specs.pdf') ->setEmbeddedDocumentBinaryObject( base64_encode(file_get_contents('path/to/technical-specs.pdf')), 'application/pdf' ); $documentReference1 = (new AdditionalDocumentReference()) ->setId('DOC-001') ->setDocumentType('Technical Specification') ->setAttachment($attachment); // Add external URL reference $externalAttachment = (new Attachment()) ->setExternalReference('https://example.com/contract.pdf'); $documentReference2 = (new AdditionalDocumentReference()) ->setId('CONTRACT-2024') ->setDocumentType('Contract') ->setAttachment($externalAttachment); // Add timesheet reference (no attachment) $documentReference3 = (new AdditionalDocumentReference()) ->setId('TS-2024-001') ->setDocumentType('130'); // Code for timesheet // Add to invoice $invoice = new Invoice(); $invoice->setAdditionalDocumentReferences([ $documentReference1, $documentReference2, $documentReference3 ]); // Or add one at a time $invoice->addAdditionalDocumentReference($documentReference1); ``` -------------------------------- ### Create UBL Invoice Object in PHP Source: https://github.com/blafasthq/ubl-invoice/blob/master/README.md Demonstrates how to create a UBL Invoice object using the UBL-Invoice PHP library. It initializes an Invoice object and sets basic properties like UBL version and ID. Further properties can be added as needed. ```php $invoice = (new \NumNum\UBL\Invoice()) ->setUBLVersionId('2.4') ->setId(123); // ... etc, all other props you need $generator = new \NumNum\UBL\Generator(); $ublXml = $generator->invoice($invoice); ``` -------------------------------- ### Read UBL File Content in PHP Source: https://github.com/blafasthq/ubl-invoice/blob/master/README.md Shows how to read and parse UBL files using the UBL-Invoice PHP library. It utilizes the UBL reader to convert the file content into an \NumNum\UBL\Invoice object. ```php $ublReader = \NumNum\UBL\Reader::ubl(); $invoice = $ublReader->parse(file_get_contents($fileName)); var_dump($invoice); // An \NumNum\UBL\Invoice instance with filled properties! ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.