noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
xmlcreditnote_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/**
27 * @file
28 * @brief extract information from UBL21
29 *
30 */
31
32/**
33 * @class
34 * @brief Get information from an XML
35 * Exception code :
36 * - 55 : XML Invalid
37 * - 62 : filename don't exist
38 * - 140: not a credit note
39 * Namespace standard (from XSD)
40 *
41 * Array
42 (
43 *
44 'cbc' => string 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'
45 'ns2' => string 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'
46 'cac' => string 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'
47 'ns4' => string 'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2'
48 )
49 *
50 */
52{
53
54 public function __construct(\DOMDocument $domDocument)
55 {
56 parent::__construct($domDocument);
57 }
58
59 /**
60 * @brief Build an XMLInvoice_Reader object from an XML string
61 * @param $string (string) XML
62 * @return \Noalyss\XMLDocument\XMLInvoice_Reader
63 * @throws \Exception if xml not valid
64 */
65 static function build_from_string($string)
66 {
67 $dm = new \DOMDocument();
68 if ($dm->loadXML($string) != false)
69 {
70 return new XMLCreditNote_Reader($dm);
71 } else
72 {
73 throw new \Exception("XC55: not a valid XML", 55);
74 }
75 }
76
77 /**
78 * @brief Build an XMLInvoice_Reader object from an XML file
79 * @param $filename (string) file and path to the file
80 * @return \Noalyss\XMLDocument\XMLInvoice_Reader
81 * @throws \Exception if xml not valid
82 */
83 static function build_from_file($filename)
84 {
85 if (!file_exists($filename))
86 {
87 throw new \Exception("XC62: file not found $filename", 62);
88 }
89 $dm = new \DOMDocument();
90 if ($dm->load($filename) != false)
91 {
92 return new XMLCreditNote_Reader($dm);
93 } else
94 {
95 throw new \Exception("XC55: not a valid XML", 55);
96 }
97 }
98
99 /**
100 * @brief retrieve InvoiceLines
101 @code
102 <ns3:CreditNoteLine>
103 <ID>2</ID>
104 <CreditedQuantity unitCode="DAY">-3</CreditedQuantity>
105 <LineExtensionAmount currencyID="EUR">-1500</LineExtensionAmount>
106 <ns3:OrderLineReference>
107 <LineID>123</LineID>
108 </ns3:OrderLineReference>
109 <ns3:Item>
110 <Description>Description 2</Description>
111 <Name>item name 2</Name>
112 <ns3:StandardItemIdentification>
113 <ID schemeID="0088">21382183120983</ID>
114 </ns3:StandardItemIdentification>
115 <ns3:OriginCountry>
116 <IdentificationCode>NO</IdentificationCode>
117 </ns3:OriginCountry>
118 <ns3:CommodityClassification>
119 <ItemClassificationCode listID="SRV">09348023</ItemClassificationCode>
120 </ns3:CommodityClassification>
121 <ns3:ClassifiedTaxCategory>
122 <ID>S</ID>
123 <Percent>25.0</Percent>
124 <ns3:TaxScheme>
125 <ID>VAT</ID>
126 </ns3:TaxScheme>
127 </ns3:ClassifiedTaxCategory>
128 </ns3:Item>
129 <ns3:Price>
130 <PriceAmount currencyID="EUR">500</PriceAmount>
131 </ns3:Price>
132 </ns3:CreditNoteLine>
133 @endcode
134 */
135 function get_invoiceLine(): array
136 {
137 $result = [];
138 $node = $this->get_node("//cac:CreditNoteLine");
139 if ($node == null )
140 {
141 throw new \Exception ("XC140 invalide document",140);
142 }
143 for ($e = 0; $e < $node->length; $e++)
144 {
145 $row = [];
146 $xml = simplexml_import_dom($node->item($e));
147 $row ['quantity'] = $this->get_node_value("//cbc:CreditedQuantity", $e);
148 $row ['amount'] = $this->get_node_value("//cbc:LineExtensionAmount", $e);
149 $row ['description'] = $this->get_node_value("//cac:Item/cbc:Description", $e);
150 $row ['name'] = $this->get_node_value("//cac:CreditNoteLine/cac:Item/cbc:Name", $e);
151 $row ['unit_price'] = $this->get_node_value("//cac:CreditNoteLine/cac:Price/cbc:PriceAmount", $e);
152 $row ['tva_id'] = $this->get_node_value("//cac:CreditNoteLine/cac:Item/cac:ClassifiedTaxCategory/cbc:ID", $e);
153 $row ['tva_percent'] = $this->get_node_value("//cac:CreditNoteLine/cac:Item/cac:ClassifiedTaxCategory/cbc:Percent", $e);
154
155 $result[] = $row;
156 }
157 return $result;
158 }
159 /**
160 * @brief return the code of the document
161 * @return string credit_node + code
162 */
163
165 {
166 return "credit note ".$this->get_node_value('cbc:CreditNoteTypeCode');
167 }
168 /**
169 * @brief before executing xpath->query the namespace must be registered
170 * the NS are different for each type of doc
171 */
172
173 public function get_namespace()
174 {
175
176 $a_namespace=array(
177 "cac"=> 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'
178 ,"cbc"=> 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'
179 ,"ns4"=>'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2'
180 ,"ns2"=>'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'
181 );
182 return $a_namespace;
183 }
184 /**
185 * @brief get the Taxes info from XML
186 * @return array
187 */
188 function get_taxes(): array
189 {
190 $result = [];
191 $node = $this->get_node("//cac:TaxTotal/cac:TaxSubtotal");
192
193 for ($e = 0; $e < $node->length; $e++)
194 {
195 $row = [];
196 $xml = simplexml_import_dom($node->item($e));
197 // /Invoice/cac:TaxTotal[1]/cac:TaxSubtotal[1]/cbc:TaxableAmount[1]
198 $this->registerNS($xml);
199
200 $row ['taxable_amount'] = $xml->xpath("//cbc:TaxableAmount")[$e] . "";
201 $row ['tax'] = $xml->xpath("//cbc:TaxAmount")[$e] . "";
202 $row ['tax_id'] = $xml->xpath("//cac:TaxCategory/cbc:ID")[$e] . "";
203 $row ['tax_percent'] = $xml->xpath("//cac:TaxCategory/cbc:Percent")[$e] . "";
204 if (isset($xml->xpath("//cac:CreditNoteLine/cac:Item/cbc:Name")[$e]))
205 $row ['name'] =$xml->xpath("//cac:CreditNoteLine/cac:Item/cbc:Name")[$e]."";
206 else
207 $row['name']="";
208
209 if ( isset ($xml->xpath("//cbc:TaxExemptionReasonCode")[$e]))
210 {
211 $row['vatex']=$xml->xpath("//cbc:TaxExemptionReasonCode")[$e];
212 }else {
213 $row['vatex']="";
214 }
215 $result[] = $row;
216 }
217 return $result;
218 }
219
220}
switch($op2) $xml
Get information from an XML Exception code :
get_document_type_code()
return the code of the document
get_namespace()
before executing xpath->query the namespace must be registered the NS are different for each type of ...
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.