PHP code example of pnscripts / pn-invoice

1. Go to this page and download the library: Download pnscripts/pn-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/ */

    

pnscripts / pn-invoice example snippets


use Pnscripts\Invoice\Model\{Address, AllowanceCharge, Identifier, Invoice, Line, Party, PaymentMeans, Specification, TaxCategory};

$seller = new Party(
    name: 'Example Supplier Ltd',
    address: new Address(countryCode: 'BG', line1: 'Example Street 1', city: 'Sofia', postalCode: '1000'),
    vatId: 'BG000000000',
    electronicAddress: new Identifier('[email protected]', 'EM'),
);

$buyer = new Party(
    name: 'Example Buyer GmbH',
    address: new Address(countryCode: 'DE', line1: 'Musterstrasse 2', city: 'Berlin', postalCode: '10115'),
    vatId: 'DE000000000',
);

$vat20 = TaxCategory::standard('20');   // the rate is your data; nothing is hard-coded

$invoice = new Invoice(
    number: 'INV-2026-0001',
    issueDate: new DateTimeImmutable('2026-10-01'),
    currency: 'EUR',
    seller: $seller,
    buyer: $buyer,
    lines: [
        new Line(id: '1', itemName: 'Consulting', quantity: '10', unitCode: 'HUR', netPrice: '85.50', taxCategory: $vat20),
        new Line(id: '2', itemName: 'Licence', quantity: '1', unitCode: 'C62', netPrice: '400', taxCategory: $vat20),
    ],
    specification: Specification::en16931(),           // or Specification::peppolBis3(), or new Specification('urn:...')
    dueDate: new DateTimeImmutable('2026-10-31'),
    buyerReference: 'PO-778',
    paymentMeans: [PaymentMeans::creditTransfer('BG80BNBG96611020345678', 'Example Supplier Ltd')],
    allowanceCharges: [AllowanceCharge::charge('15', 'Freight', 'FC', $vat20)],
);

$totals = $invoice->totals();            // line total, VAT breakdown, payable amount (bcmath, rounded per EN 16931)
echo $totals->payableAmount->toFixed(2); // "1524.00"

use Pnscripts\Invoice\Writer\{CiiWriter, UblWriter};

$ublXml = (new UblWriter())->write($invoice);   // <Invoice> or <CreditNote> depending on $invoice->kind
$ciiXml = (new CiiWriter())->write($invoice);   // <rsm:CrossIndustryInvoice>

use Pnscripts\Invoice\Validation\InvoiceValidator;

$result = (new InvoiceValidator())->validateXml($ublXml);   // or validateFile($path), validateInvoice($invoice)

if (!$result->isValid()) {
    foreach ($result->errors() as $violation) {
        printf("%s [%s] %s at %s\n", $violation->ruleId, $violation->layer->value, $violation->message, $violation->location);
    }
}

use Pnscripts\Invoice\Validation\InvoiceValidator;
use Pnscripts\Invoice\Validation\Schematron\CommandSchematronValidator;
use Pnscripts\Invoice\Validation\Syntax;

// Saxon-HE 12 needs its xmlresolver jars on the class path ("java -jar" alone fails with
// NoClassDefFoundError: org/xmlresolver/Resolver). On Windows separate the jars with ";".
$saxon = ['java', '-cp', '/opt/saxon/Saxon-HE-12.9.jar:/opt/saxon/xmlresolver-5.3.3.jar:/opt/saxon/xmlresolver-5.3.3-data.jar', 'net.sf.saxon.Transform', '-s:{file}'];

$schematron = new CommandSchematronValidator([
    Syntax::UblInvoice->value => [...$saxon, '-xsl:/opt/en16931/ubl/xslt/EN16931-UBL-validation.xslt'],
    Syntax::UblCreditNote->value => [...$saxon, '-xsl:/opt/en16931/ubl/xslt/EN16931-UBL-validation.xslt'],
    Syntax::Cii->value => [...$saxon, '-xsl:/opt/en16931/cii/xslt/EN16931-CII-validation.xslt'],
]);

$result = (new InvoiceValidator(schematron: $schematron))->validateFile('invoice.xml');