Files
project/app/XmlReader.php

100 lines
6.0 KiB
PHP

<?php
namespace App;
use \DOMDocument;
use \DOMXPath;
class XmlReader
{
private DOMDocument $xml;
private DOMXPath $xpath;
public function __construct(string $xml)
{
$this->xml = new DOMDocument();
$this->xml->loadXML($xml);
$this->xpath = new DOMXPath($this->xml);
}
public function incoming(): array
{
$incoming = [];
$incoming['invoice_number'] = $this->xpath->query('cbc:ID', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['issue_date'] = $this->xpath->query('cbc:IssueDate', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['due_date'] = $this->xpath->query('cbc:DueDate', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['invoice_type_code'] = $this->xpath->query('cbc:InvoiceTypeCode', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['currency_code'] = $this->xpath->query('cbc:DocumentCurrencyCode', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['net'] = $this->xpath->query('cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['gross'] = $this->xpath->query('cac:LegalMonetaryTotal/cbc:TaxInclusiveAmount', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['tax'] = $this->xpath->query('cac:TaxTotal/cbc:TaxAmount', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['pay_name'] = $this->xpath->query('cac:PaymentMeans/cac:PayeeFinancialAccount/cbc:Name', $this->xml->documentElement)->item(0)->nodeValue;
$incoming['pay_bic'] = ($this->xpath->query('cac:PaymentMeans/cac:PayeeFinancialAccount/cac:FinancialInstitutionBranch/cbc:ID', $this->xml->documentElement)->length > 0) ? $this->xpath->query('cac:PaymentMeans/cac:PayeeFinancialAccount/cac:FinancialInstitutionBranch/cbc:ID', $this->xml->documentElement)->item(0)->nodeValue : null;
$incoming['pay_iban'] = $this->xpath->query('cac:PaymentMeans/cac:PayeeFinancialAccount/cbc:ID', $this->xml->documentElement)->item(0)->nodeValue;
return $incoming;
}
public function supplier(): array
{
$supplier = [];
$supplierNode = $this->xpath->query('cac:AccountingSupplierParty', $this->xml->documentElement)->item(0);
$supplier['name'] = $this->xpath->query("cac:Party/cac:PartyName/cbc:Name", $supplierNode)->item(0)->nodeValue;
$supplier['registration_name'] = $this->xpath->query("cac:Party/cac:PartyLegalEntity/cbc:RegistrationName", $supplierNode)->item(0)->nodeValue;
$supplier['email'] = $this->xpath->query("cac:Party/cbc:EndpointID", $supplierNode)->item(0)->nodeValue;
$supplier['address'] = $this->xpath->query("cac:Party/cac:PostalAddress/cbc:StreetName", $supplierNode)->item(0)->nodeValue;
$supplier['city'] = $this->xpath->query("cac:Party/cac:PostalAddress/cbc:CityName", $supplierNode)->item(0)->nodeValue;
$supplier['zip'] = $this->xpath->query("cac:Party/cac:PostalAddress/cbc:PostalZone", $supplierNode)->item(0)->nodeValue;
$supplier['country_code'] = $this->xpath->query("cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode", $supplierNode)->item(0)->nodeValue;
$supplier['tax_fc'] = ($this->xpath->query("cac:Party/cac:PartyTaxScheme[cac:TaxScheme/cbc:ID/text() = 'FC']", $supplierNode)->length === 0) ? null : $this->xpath->query("cac:Party/cac:PartyTaxScheme[cac:TaxScheme/cbc:ID/text() = 'FC']/cbc:CompanyID", $supplierNode)->item(0)->nodeValue;
$supplier['tax_vat'] = ($this->xpath->query("cac:Party/cac:PartyTaxScheme[cac:TaxScheme/cbc:ID/text() = 'VAT']", $supplierNode)->length === 0) ? null : $this->xpath->query("cac:Party/cac:PartyTaxScheme[cac:TaxScheme/cbc:ID/text() = 'VAT']/cbc:CompanyID", $supplierNode)->item(0)->nodeValue;
$supplier['contact_name'] = $this->xpath->query("cac:Party/cac:Contact/cbc:Name", $supplierNode)->item(0)->nodeValue;
$supplier['contact_phone'] = $this->xpath->query("cac:Party/cac:Contact/cbc:Telephone", $supplierNode)->item(0)->nodeValue;
$supplier['contact_email'] = $this->xpath->query("cac:Party/cac:Contact/cbc:ElectronicMail", $supplierNode)->item(0)->nodeValue;
return $supplier;
}
public function items(): array
{
$items = [];
$itemList = $this->xpath->query('cac:InvoiceLine', $this->xml->documentElement);
foreach ($itemList as $itemNode) {
$item = [];
$item['name'] = $this->xpath->query("cac:Item/cbc:Name", $itemNode)->item(0)->nodeValue;
$item['article_number'] = null;
$item['description'] = ($this->xpath->query("cac:Item/cbc:Description", $itemNode)->length === 0) ? null : $this->xpath->query("cac:Item/cbc:Description", $itemNode)->item(0)->nodeValue;
$item['amount'] = $this->xpath->query("cbc:InvoicedQuantity", $itemNode)->item(0)->nodeValue;
$item['discount'] = null;
$item['tax'] = $this->xpath->query("cac:Item/cac:ClassifiedTaxCategory/cbc:Percent", $itemNode)->item(0)->nodeValue;
$item['price'] = $this->xpath->query("cac:Price/cbc:PriceAmount", $itemNode)->item(0)->nodeValue;
$item['total'] = $this->xpath->query("cbc:LineExtensionAmount", $itemNode)->item(0)->nodeValue;
$items[] = $item;
}
return $items;
}
public function taxes(): array
{
$taxes = [];
$taxList = $this->xpath->query('cac:TaxTotal/cac:TaxSubtotal', $this->xml->documentElement);
foreach ($taxList as $taxNode) {
$tax = [];
$tax['taxable_amount'] = $this->xpath->query("cbc:TaxableAmount", $taxNode)->item(0)->nodeValue;
$tax['amount'] = $this->xpath->query("cbc:TaxAmount", $taxNode)->item(0)->nodeValue;
$tax['percentage'] = $this->xpath->query("cac:TaxCategory/cbc:Percent", $taxNode)->item(0)->nodeValue;
$tax['currency'] = $this->xpath->query("cbc:TaxableAmount", $taxNode)->item(0)->getAttribute("currencyID");
$taxes[] = $tax;
}
return $taxes;
}
}