noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
xmlinvoice_reader.class.php
Go to the documentation of this file.
1<?php
2
3namespace Noalyss\XMLDocument;
4
5/*
6 * This file is part of NOALYSS.
7 *
8 * NOALYSS is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NOALYSS is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NOALYSS; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22// Copyright Author Dany De Bontridder danydb@aevalys.eu 22/10/23
23
24
25/**
26 * @file
27 * @brief extract information from UBL21
28 *
29 */
30
31/**
32 * @class
33 * @brief Get information from an XML
34 * Exception code :
35 * - 55 : XML Invalid
36 * - 62 : filename don't exist
37 * - 143 : not an invoice
38 * Namespace standard (from XSD)
39 *
40 * Array
41 (
42 [xml] => http://www.w3.org/XML/1998/namespace
43 [cbc] => urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2
44 [cac] => urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2
45 [xmlns] => urn:oasis:names:specification:ubl:schema:xsd:Invoice-2
46 )
47
48 *
49 */
51{
52
53 public function __construct(\DOMDocument $domDocument)
54 {
55 parent::__construct($domDocument);
56
57 }
58 /**
59 * @brief Build an XMLInvoice_Reader object from an XML string
60 * @param $string (string) XML
61 * @return \Noalyss\XMLDocument\XMLInvoice_Reader
62 * @throws \Exception if xml not valid
63 */
64 static function build_from_string($string)
65 {
66 $dm = new \DOMDocument();
67 if ($dm->loadXML($string) != false)
68 {
69 return new XMLInvoice_Reader($dm);
70 } else
71 {
72 throw new \Exception("XR55: not a valid XML",55);
73 }
74 }
75 /**
76 * @brief Build an XMLInvoice_Reader object from an XML file
77 * @param $filename (string) file and path to the file
78 * @return \Noalyss\XMLDocument\XMLInvoice_Reader
79 * @throws \Exception if xml not valid
80 */
81 static function build_from_file($filename)
82 {
83 if (!file_exists($filename))
84 {
85 throw new \Exception("XR62: file not found $filename", 62);
86 }
87 $dm = new \DOMDocument();
88 if ($dm->load($filename) != false)
89 {
90 return new XMLInvoice_Reader($dm);
91 } else
92 {
93 throw new \Exception("XR55: not a valid XML", 55);
94 }
95 }
96 /**
97 * @brief retrieve InvoiceLines
98 */
99 function get_invoiceLine(): array
100 {
101 $result = [];
102 $node = $this->get_node("//cac:InvoiceLine");
103
104 // read Invoice
105 if ( $node == null )
106 {
107 throw new \Exception ("XR143 unknow document",143);
108 }
109 for ($e = 0; $e < $node->length; $e++)
110 {
111 $row = [];
112 $row ['quantity'] = $this->get_node_value("//cbc:InvoicedQuantity", $e);
113 $row ['amount'] = $this->get_node_value("//cbc:LineExtensionAmount", $e);
114 $row ['description'] = $this->get_node_value("//cac:Item/cbc:Description", $e);
115 $row ['name'] = $this->get_node_value("//cac:InvoiceLine/cac:Item/cbc:Name", $e);
116 $row ['unit_price'] = $this->get_node_value("//cac:InvoiceLine/cac:Price/cbc:PriceAmount", $e);
117 $row ['tva_id'] = $this->get_node_value("//cac:InvoiceLine/cac:Item/cac:ClassifiedTaxCategory/cbc:ID", $e);
118 $row ['tva_percent'] = $this->get_node_value("//cac:InvoiceLine/cac:Item/cac:ClassifiedTaxCategory/cbc:Percent", $e);
119
120 $result[] = $row;
121 return $result;
122 }
123 }
124 /**
125 * @brief return the code of the document
126 * @return string invoice + code
127 */
129 {
130 return "invoice ".$this->get_node_value('cbc:InvoiceTypeCode');
131 }
132 /**
133 * @brief before executing xpath->query the namespace must be registered
134 * the NS are different for each type of doc
135 * @param $xml (null or simpleXML)
136 */
137
138 public function get_namespace()
139 {
140 $a_namespace=array(
141 "cac"=>'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'
142 ,"cbc"=>'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'
143 ,"ns4"=>'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2'
144 );
145 return $a_namespace;
146
147 }
148 /**
149 * @brief get the Taxes info from XML
150 * @return array
151 */
152 function get_taxes(): array
153 {
154 $result = [];
155 $node = $this->get_node("//cac:TaxTotal/cac:TaxSubtotal");
156
157 for ($e = 0; $e < $node->length; $e++)
158 {
159 $row = [];
160 $xml = simplexml_import_dom($node->item($e));
161 // /Invoice/cac:TaxTotal[1]/cac:TaxSubtotal[1]/cbc:TaxableAmount[1]
162 $this->registerNS($xml);
163
164 $row ['taxable_amount'] = $xml->xpath("//cbc:TaxableAmount")[$e] . "";
165 $row ['tax'] = $xml->xpath("//cbc:TaxAmount")[$e] . "";
166 $row ['tax_id'] = $xml->xpath("//cac:TaxCategory/cbc:ID")[$e] . "";
167 $row ['tax_percent'] = $xml->xpath("//cac:TaxCategory/cbc:Percent")[$e] . "";
168 if (isset($xml->xpath("//cac:InvoiceLine/cac:Item/cbc:Name")[$e]))
169 {
170 $row ['name'] =$xml->xpath("//cac:InvoiceLine/cac:Item/cbc:Name")[$e]."";
171 }else {
172 $row ['name'] ="";
173 }
174 if ( isset ($xml->xpath("//cbc:TaxExemptionReasonCode")[$e]))
175 {
176 $row['vatex']=$xml->xpath("//cbc:TaxExemptionReasonCode")[$e];
177 }else {
178 $row['vatex']="";
179 }
180 $result[] = $row;
181 }
182 return $result;
183 }
184}
switch($op2) $xml
Get information from an XML Exception code :
get_document_type_code()
return the code of the document
static build_from_string($string)
Build an XMLInvoice_Reader object from an XML string.
static build_from_file($filename)
Build an XMLInvoice_Reader object from an XML file.
get_namespace()
before executing xpath->query the namespace must be registered the NS are different for each type of ...