PHP code example of youniwemi / digital-invoice

1. Go to this page and download the library: Download youniwemi/digital-invoice library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

youniwemi / digital-invoice example snippets


use DigitalInvoice\Invoice;
use DigitalInvoice\CurrencyCode;
use DigitalInvoice\FacturX;

$invoice = new Invoice('INV-2024-001', new DateTime(), null, CurrencyCode::EURO, FacturX::BASIC);

$invoice->setSeller('12345', '0002', 'ACME Corp', 'ACME');
$invoice->setSellerAddress('1 rue de la Paix', '75001', 'Paris', 'FR');
$invoice->setSellerTaxRegistration('FR12312345678', 'VA');

$invoice->setBuyer('', 'Client SARL');
$invoice->setBuyerOrderReference('BC-42'); // purchase order (BT-13)
$invoice->setBuyerAddress('2 avenue de la Gare', '69001', 'Lyon', 'FR');

$invoice->addItem('Consulting', 200.0, 20.0, 2);
$invoice->addPaymentMean('58', 'FR7630006000011234567890189', 'ACME Corp');
$invoice->setPaymentTerms(new DateTime('+30 days'), 'Net 30');

// XML only
$xml = $invoice->getXml();

// PDF with embedded XML (

use DigitalInvoice\Ubl;

$invoice = new Invoice('INV-2024-001', new DateTime(), null, CurrencyCode::EURO, Ubl::PEPPOL);
// same setter API …
$xml = $invoice->getXml();

$invoice->setBillingMode('S1');                          // BT-23: B1, S1, M1, B2, S2, M2, S3, B4 …
$invoice->setSellerElectronicAddress('123456789', '0225'); // BT-34, after setSeller
$invoice->setBuyerElectronicAddress('[email protected]', 'EM'); // BT-49, after setBuyer
$invoice->setVatDueDateTypeCode(VatDueDateTypeCode::INVOICE_DATE); // BT-8, optional: VAT on debits, Factur-X only

$invoice->addNote('Indemnité forfaitaire pour frais de recouvrement : 40 €', 'PMT');
$invoice->addNote('Pénalités de retard : 3 fois le taux d\'intérêt légal', 'PMD');
$invoice->addNote('Pas d\'escompte pour paiement anticipé', 'AAB');

$creditNote = new Invoice('AV-2024-001', new DateTime(), null, CurrencyCode::EURO, FacturX::EN16931, InvoiceTypeCode::CREDIT_NOTE);
$creditNote->addPrecedingInvoiceReference('INV-2024-001', new DateTime('2024-01-15')); // BT-25, BT-26
$creditNote->isCreditNote(); // true

$invoice->addAllowance(7, 20);                          // 7 € excl. VAT at 20 %, reason "Remise", code 95 (Discount)
$invoice->addAllowance(18, 20, 'Remise fidélité', '95'); // reason (BT-97) and UNTDID 5189 code (BT-98)

$invoice->addEmbeddedAttachment(
    'BL-42',                // BT-122 document reference
    null,                   // scheme, UBL only
    'bon-de-livraison.pdf', // filename
    file_get_contents('bon-de-livraison.pdf'),
    AttachmentMimeCode::PDF->value,
    AttachmentDescription::BON_LIVRAISON->value // BT-123
);

$pdf = $invoice->getPdf(file_get_contents('template.pdf'), false, [], true);

use DigitalInvoice\InvoiceReader;

// From XML string
$data = InvoiceReader::fromXml($xml);

// From PDF (extracts embedded CII/FacturX XML automatically)
$data = InvoiceReader::read(file_get_contents('invoice.pdf'));

$data->invoiceId;               // string
$data->issueDate;               // ?DateTime
$data->dueDate;                 // ?DateTime
$data->currency;                // string  e.g. 'EUR'
$data->profile;                 // string  URN or format identifier
$data->invoiceType;             // string  e.g. '380', '381'
$data->isCreditNote();          // bool
$data->precedingInvoices;       // array  [{id, issueDate}] (BG-3)

$data->seller;                  // ?PartyData
$data->buyer;                   // ?PartyData
$data->buyerReference;          // ?string
$data->buyerOrderReference;     // ?string  purchase order (BT-13)

$data->notes;                   // array  [{content, subjectCode, contentCode}]
$data->items;                   // InvoiceItemData[]
$data->paymentMeans;            // PaymentMeanData[]
$data->paymentTermsDescription; // ?string

// Monetary totals
$data->lineTotal;               // ?float  sum of line net amounts (BT-106)
$data->allowanceTotal;          // ?float  sum of document level allowances (BT-107)
$data->taxBasisTotal;           // ?float  net amount (excl. VAT)
$data->taxTotal;                // ?float  total VAT amount
$data->grandTotal;              // ?float  total incl. VAT
$data->duePayable;              // ?float

// Tax breakdown — one entry per rate/category
$data->taxBreakdown;            // TaxBreakdownData[]

// Document level allowances (BG-20), e.g. global discounts
$data->allowances;              // AllowanceData[] {amount, taxRate, categoryCode, reason, reasonCode}

// Supporting documents (BG-24), contents decoded
$data->attachments;             // AttachmentData[] {id, description, filename, mimeCode, contents}

$tb->rate;             // float   e.g. 20.0
$tb->basisAmount;      // ?float  taxable base for this rate
$tb->calculatedAmount; // ?float  VAT amount for this rate
$tb->categoryCode;     // ?string S, Z, E, AE, K …
$tb->exemptionReason;  // ?string

$party->name;             // string
$party->tradingName;      // ?string
$party->id;               // ?string  legal/company ID value
$party->idType;           // ?string  ISO 6523 scheme code e.g. '0002'
$party->address;          // ?AddressData  (lineOne, postCode, city, countryCode …)
$party->contact;          // ?ContactData  (name, phone, email)
$party->taxRegistrations; // array  [{id, schemeID}]  e.g. VAT number
$party->identifiers;      // array  [{id, idType}]    additional IDs

use DigitalInvoice\InvoiceRenderer;

$data = InvoiceReader::fromXml($xml);

// Default (English)
$html = (new InvoiceRenderer())->render($data);

// French labels
$html = (new InvoiceRenderer(lang: 'fr'))->render($data);

// German labels
$html = (new InvoiceRenderer(lang: 'de'))->render($data);

// Custom template and/or CSS
$html = (new InvoiceRenderer('/path/to/template.php', '/path/to/styles.css'))->render($data);
bash
php -S localhost:8000
# open http://localhost:8000/viewer.php