|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* The MIT License (MIT) |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (c) 2024 Daniel Popiniuc |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
11
|
|
|
* in the Software without restriction, including without limitation the rights |
|
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
17
|
|
|
* copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
25
|
|
|
* SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace danielgp\efactura; |
|
30
|
|
|
|
|
31
|
|
|
class ElectornicInvoiceWrite |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
use traitVersions; |
|
35
|
|
|
|
|
36
|
|
|
protected $objXmlWriter; |
|
37
|
|
|
|
|
38
|
|
|
private function setDocumentTag(array $arrayDocumentData): void { |
|
39
|
|
|
$this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']); |
|
40
|
|
|
foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) { |
|
41
|
|
|
if ($key === '') { |
|
42
|
|
|
$strValue = sprintf($value, $arrayDocumentData['DocumentTagName']); |
|
43
|
|
|
$this->objXmlWriter->writeAttributeNS(NULL, 'xmlns', NULL, $strValue); |
|
44
|
|
|
} else { |
|
45
|
|
|
$this->objXmlWriter->writeAttributeNS('xmlns', $key, NULL, $value); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
if (array_key_exists('SchemaLocation', $arrayDocumentData)) { |
|
49
|
|
|
$this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function setElementComment(string $strKey, string $strSection, bool $includeComments): void { |
|
54
|
|
|
if ($includeComments && array_key_exists($strKey, $this->arraySettings['Comments'][$strSection])) { |
|
55
|
|
|
switch ($strSection) { |
|
56
|
|
|
case 'CAC': |
|
57
|
|
|
$elementComment = $this->arraySettings['Comments'][$strSection][$strKey]; |
|
58
|
|
|
if (is_array($elementComment)) { |
|
59
|
|
|
foreach ($elementComment as $value) { |
|
60
|
|
|
$this->objXmlWriter->writeComment($value); |
|
61
|
|
|
} |
|
62
|
|
|
} else { |
|
63
|
|
|
$this->objXmlWriter->writeComment($elementComment); |
|
64
|
|
|
} |
|
65
|
|
|
break; |
|
66
|
|
|
case 'CBC': |
|
67
|
|
|
$this->objXmlWriter->writeComment($this->arraySettings['Comments'][$strSection][$strKey]); |
|
68
|
|
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function setCompanyElementsOrdered(array $arrayParameters): void { |
|
74
|
|
|
$this->setElementComment($arrayParameters['commentParentKey'], 'CAC', $arrayParameters['comments']); |
|
75
|
|
|
$this->objXmlWriter->startElement('cac:' . $arrayParameters['tag']); |
|
76
|
|
|
$arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$arrayParameters['commentParentKey']]; |
|
77
|
|
|
foreach ($arrayCustomOrder as $value) { |
|
78
|
|
|
if (array_key_exists($value, $arrayParameters['data'])) { |
|
79
|
|
|
$this->setElementComment(implode('_', [ |
|
80
|
|
|
$arrayParameters['commentParentKey'], $value]), 'CAC', $arrayParameters['comments']); |
|
81
|
|
|
if (is_array($arrayParameters['data'][$value])) { |
|
82
|
|
|
$this->objXmlWriter->startElement('cac:' . $value); |
|
83
|
|
|
foreach ($arrayParameters['data'][$value] as $key2 => $value2) { |
|
84
|
|
|
$this->setElementComment(implode('_', [ |
|
85
|
|
|
$arrayParameters['commentParentKey'], $value, $key2]), 'CAC', $arrayParameters['comments']); |
|
86
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $key2, $value2); |
|
87
|
|
|
} |
|
88
|
|
|
$this->objXmlWriter->endElement(); |
|
89
|
|
|
} else { |
|
90
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $value, $arrayParameters['data'][$value]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
$this->objXmlWriter->endElement(); // $key |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function setHeaderCommonAggregateComponentsCompanies(array $arrayParameters): void { |
|
98
|
|
|
$key = $arrayParameters['tag']; |
|
99
|
|
|
$this->setElementComment($key, 'CAC', $arrayParameters['comments']); |
|
100
|
|
|
$this->objXmlWriter->startElement('cac:' . $key); |
|
101
|
|
|
$this->objXmlWriter->startElement('cac:Party'); |
|
102
|
|
|
$arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$key]; |
|
103
|
|
|
foreach ($arrayCustomOrder as $value) { |
|
104
|
|
|
if (array_key_exists($value, $arrayParameters['data'])) { |
|
105
|
|
|
$this->setCompanyElementsOrdered([ |
|
106
|
|
|
'comments' => $arrayParameters['comments'], |
|
107
|
|
|
'commentParentKey' => implode('_', [$key, $value]), |
|
108
|
|
|
'data' => $arrayParameters['data'][$value], |
|
109
|
|
|
'tag' => $value, |
|
110
|
|
|
]); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
$this->objXmlWriter->endElement(); // Party |
|
114
|
|
|
$this->objXmlWriter->endElement(); // $key |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private function setHeaderCommonAggregateComponentsTaxTotal(array $arrayParameters): void { |
|
118
|
|
|
$key = $arrayParameters['tag']; |
|
119
|
|
|
$this->setElementComment($key, 'CAC', $arrayParameters['comments']); |
|
120
|
|
|
$this->objXmlWriter->startElement('cac:' . $key); |
|
121
|
|
|
$this->setElementComment(implode('_', [$key, 'TaxAmount']), 'CAC', $arrayParameters['comments']); |
|
122
|
|
|
$this->objXmlWriter->startElement('cbc:TaxAmount'); |
|
123
|
|
|
$this->objXmlWriter->writeAttribute('currencyID', $arrayParameters['data']['TaxAmount']['currencyID']); |
|
124
|
|
|
$this->objXmlWriter->writeRaw($arrayParameters['data']['TaxAmount']['value']); |
|
125
|
|
|
$this->objXmlWriter->endElement(); // TaxAmount |
|
126
|
|
|
foreach ($arrayParameters['data']['TaxSubtotal'] as $value) { |
|
127
|
|
|
$this->objXmlWriter->startElement('cac:TaxSubtotal'); |
|
128
|
|
|
$this->setElementComment(implode('_', [$key, 'TaxSubtotal', 'TaxableAmount']), 'CAC', $arrayParameters['comments']); |
|
129
|
|
|
$this->objXmlWriter->startElement('cbc:TaxableAmount'); |
|
130
|
|
|
$this->objXmlWriter->writeAttribute('currencyID', $value['TaxableAmount']['currencyID']); |
|
131
|
|
|
$this->objXmlWriter->writeRaw($value['TaxableAmount']['value']); |
|
132
|
|
|
$this->objXmlWriter->endElement(); // TaxableAmount |
|
133
|
|
|
$this->setElementComment(implode('_', [$key, 'TaxSubtotal', 'TaxAmount']), 'CAC', $arrayParameters['comments']); |
|
134
|
|
|
$this->objXmlWriter->startElement('cbc:TaxAmount'); |
|
135
|
|
|
$this->objXmlWriter->writeAttribute('currencyID', $value['TaxAmount']['currencyID']); |
|
136
|
|
|
$this->objXmlWriter->writeRaw($value['TaxAmount']['value']); |
|
137
|
|
|
$this->objXmlWriter->endElement(); // TaxAmount |
|
138
|
|
|
$this->setTaxCategory([ |
|
139
|
|
|
'comments' => $arrayParameters['comments'], |
|
140
|
|
|
'commentParentKey' => implode('_', [$key, 'TaxSubtotal', 'TaxCategory']), |
|
141
|
|
|
'data' => $value['TaxCategory'], |
|
142
|
|
|
'tag' => 'TaxCategory', |
|
143
|
|
|
]); |
|
144
|
|
|
$this->objXmlWriter->endElement(); // TaxSubtotal |
|
145
|
|
|
} |
|
146
|
|
|
$this->objXmlWriter->endElement(); // $key |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private function setHeaderCommonBasicComponents(array $arrayElementWithData, bool $includeComments): void { |
|
150
|
|
|
$arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header']['CBC']; |
|
151
|
|
|
foreach ($arrayCustomOrdered as $value) { |
|
152
|
|
|
$this->setElementComment($value, 'CBC', $includeComments); |
|
153
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
private function setTaxCategory(array $arrayParameters): void { |
|
158
|
|
|
$key = $arrayParameters['tag']; |
|
159
|
|
|
$this->objXmlWriter->startElement('cac:' . $key); |
|
160
|
|
|
$this->setElementComment(implode('_', [ |
|
161
|
|
|
$arrayParameters['commentParentKey'], 'ID']), 'CAC', $arrayParameters['comments']); |
|
162
|
|
|
$this->objXmlWriter->writeElement('cbc:ID', $arrayParameters['data']['ID']); |
|
163
|
|
|
$this->setElementComment(implode('_', [ |
|
164
|
|
|
$arrayParameters['commentParentKey'], 'Percent']), 'CAC', $arrayParameters['comments']); |
|
165
|
|
|
$this->objXmlWriter->writeElement('cbc:Percent', $arrayParameters['data']['Percent']); |
|
166
|
|
|
$this->objXmlWriter->startElement('cac:TaxScheme'); |
|
167
|
|
|
$this->objXmlWriter->writeElement('cbc:ID', $arrayParameters['data']['TaxScheme']['ID']); |
|
168
|
|
|
$this->objXmlWriter->endElement(); // $key |
|
169
|
|
|
$this->objXmlWriter->endElement(); // $key |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function writeElectronicInvoice(string $strFile, array $arrayData, bool $bolComments): void { |
|
173
|
|
|
$this->objXmlWriter = new \XMLWriter(); |
|
174
|
|
|
$this->objXmlWriter->openURI($strFile); |
|
175
|
|
|
$this->objXmlWriter->setIndent(true); |
|
176
|
|
|
$this->objXmlWriter->setIndentString(str_repeat(' ', 4)); |
|
177
|
|
|
$this->objXmlWriter->startDocument('1.0', 'UTF-8'); |
|
178
|
|
|
// if no DocumentNameSpaces seen take Default ones from local configuration |
|
179
|
|
|
$this->getSettingsFromFileIntoMemory(); |
|
180
|
|
|
$arrayDefaults = $this->getDefaultsIntoDataSet($arrayData); |
|
181
|
|
|
if ($arrayDefaults !== []) { |
|
182
|
|
|
$arrayData = array_merge($arrayData, $arrayDefaults['Root']); |
|
183
|
|
|
if (!array_key_exists('CustomizationID', $arrayData['Header']['CommonBasicComponents-2'])) { |
|
184
|
|
|
$arrayData['Header']['CommonBasicComponents-2']['CustomizationID'] = $arrayDefaults['CIUS-RO']; |
|
185
|
|
|
$arrayData['Header']['CommonBasicComponents-2']['UBLVersionID'] = $arrayDefaults['UBL']; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
$this->setDocumentTag($arrayData); |
|
189
|
|
|
$this->setHeaderCommonBasicComponents($arrayData['Header']['CommonBasicComponents-2'], $bolComments); |
|
190
|
|
|
$arrayAggegateComponents = $arrayData['Header']['CommonAggregateComponents-2']; |
|
191
|
|
|
$this->setHeaderCommonAggregateComponentsCompanies([ |
|
192
|
|
|
'data' => $arrayAggegateComponents['AccountingSupplierParty']['Party'], |
|
193
|
|
|
'tag' => 'AccountingSupplierParty', |
|
194
|
|
|
'comments' => $bolComments, |
|
195
|
|
|
]); |
|
196
|
|
|
$this->setHeaderCommonAggregateComponentsCompanies([ |
|
197
|
|
|
'data' => $arrayAggegateComponents['AccountingCustomerParty']['Party'], |
|
198
|
|
|
'tag' => 'AccountingCustomerParty', |
|
199
|
|
|
'comments' => $bolComments, |
|
200
|
|
|
]); |
|
201
|
|
|
// multiple accounts can be specified within PaymentMeans |
|
202
|
|
|
if (is_array($arrayAggegateComponents['PaymentMeans'])) { |
|
203
|
|
|
foreach ($arrayAggegateComponents['PaymentMeans'] as $value) { |
|
204
|
|
|
$this->setHeaderCommonAggregateComponentsOthers([ |
|
205
|
|
|
'data' => $value, |
|
206
|
|
|
'tag' => 'PaymentMeans', |
|
207
|
|
|
'comments' => $bolComments, |
|
208
|
|
|
]); |
|
209
|
|
|
} |
|
210
|
|
|
} else { |
|
211
|
|
|
$this->setHeaderCommonAggregateComponentsOthers([ |
|
212
|
|
|
'data' => $arrayAggegateComponents['PaymentMeans'], |
|
213
|
|
|
'tag' => 'PaymentMeans', |
|
214
|
|
|
'comments' => $bolComments, |
|
215
|
|
|
]); |
|
216
|
|
|
} |
|
217
|
|
|
$this->setHeaderCommonAggregateComponentsTaxTotal([ |
|
218
|
|
|
'data' => $arrayAggegateComponents['TaxTotal'], |
|
219
|
|
|
'tag' => 'TaxTotal', |
|
220
|
|
|
'comments' => $bolComments, |
|
221
|
|
|
]); |
|
222
|
|
|
$this->setHeaderCommonAggregateComponentsOthers([ |
|
223
|
|
|
'data' => $arrayAggegateComponents['LegalMonetaryTotal'], |
|
224
|
|
|
'tag' => 'LegalMonetaryTotal', |
|
225
|
|
|
'comments' => $bolComments, |
|
226
|
|
|
]); |
|
227
|
|
|
// multiple Lines |
|
228
|
|
|
/* foreach ($arrayData['Lines'] as $key => $value) { |
|
229
|
|
|
$this->setHeaderCommonAggregateComponentsOthers([ |
|
230
|
|
|
'data' => $value, |
|
231
|
|
|
'tagForComments' => 'Lines', |
|
232
|
|
|
'tag' => $arrayData['DocumentTagName'] . 'Line', |
|
233
|
|
|
'comments' => $bolComments, |
|
234
|
|
|
]); |
|
235
|
|
|
} */ |
|
236
|
|
|
$this->objXmlWriter->endElement(); // Invoice or CreditNote |
|
237
|
|
|
$this->objXmlWriter->flush(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
private function setHeaderCommonAggregateComponentsOthers(array $arrayParameters): void { |
|
241
|
|
|
if (array_key_exists('tagForComments', $arrayParameters)) { |
|
242
|
|
|
$key = $arrayParameters['tagForComments']; |
|
243
|
|
|
} else { |
|
244
|
|
|
$key = $arrayParameters['tag']; |
|
245
|
|
|
} |
|
246
|
|
|
$this->setElementComment($key, 'CAC', $arrayParameters['comments']); |
|
247
|
|
|
$this->objXmlWriter->startElement('cac:' . $arrayParameters['tag']); |
|
248
|
|
|
foreach ($arrayParameters['data'] as $key2 => $value2) { |
|
249
|
|
|
$this->setElementComment(implode('_', [$key, $key2]), 'CAC', $arrayParameters['comments']); |
|
250
|
|
|
if (is_array($value2)) { |
|
251
|
|
|
if (substr($key2, -6) === 'Amount') { |
|
252
|
|
|
$this->objXmlWriter->startElement('cbc:' . $key2); |
|
253
|
|
|
$this->objXmlWriter->writeAttribute('currencyID', $value2['currencyID']); |
|
254
|
|
|
$this->objXmlWriter->writeRaw($value2['value']); |
|
255
|
|
|
} elseif (substr($key2, -8) === 'Quantity') { |
|
256
|
|
|
$this->objXmlWriter->startElement('cbc:' . $key2); |
|
257
|
|
|
$this->objXmlWriter->writeAttribute('unitCode', $value2['unitCode']); |
|
258
|
|
|
$this->objXmlWriter->writeRaw($value2['value']); |
|
259
|
|
|
} else { |
|
260
|
|
|
$this->objXmlWriter->startElement('cac:' . $key2); |
|
261
|
|
|
foreach ($value2 as $key3 => $value3) { |
|
262
|
|
|
$this->setElementComment(implode('_', [ |
|
263
|
|
|
$key, $key2, $key3]), 'CAC', $arrayParameters['comments']); |
|
264
|
|
|
if (substr($key3, -6) === 'Amount') { |
|
265
|
|
|
$this->objXmlWriter->startElement('cbc:' . $key3); |
|
266
|
|
|
$this->objXmlWriter->writeAttribute('currencyID', $value3['currencyID']); |
|
267
|
|
|
$this->objXmlWriter->writeRaw($value3['value']); |
|
268
|
|
|
$this->objXmlWriter->endElement(); // $key3 |
|
269
|
|
|
} elseif (substr($key3, -8) === 'Quantity') { |
|
270
|
|
|
$this->objXmlWriter->startElement('cbc:' . $key3); |
|
271
|
|
|
$this->objXmlWriter->writeAttribute('unitCode', $value3['unitCode']); |
|
272
|
|
|
$this->objXmlWriter->writeRaw($value3['value']); |
|
273
|
|
|
$this->objXmlWriter->endElement(); // $key3 |
|
274
|
|
|
} elseif (is_array($value3)) { |
|
275
|
|
|
$this->objXmlWriter->startElement('cac:' . $key3); |
|
276
|
|
|
foreach ($value3 as $key4 => $value4) { |
|
277
|
|
|
$this->setElementComment(implode('_', [ |
|
278
|
|
|
$key, $key2, $key3, $key4]), 'CAC', $arrayParameters['comments']); |
|
279
|
|
|
if (is_array($value4)) { |
|
280
|
|
|
$this->objXmlWriter->startElement('cac:' . $key4); |
|
281
|
|
|
foreach ($value4 as $key5 => $value5) { |
|
282
|
|
|
$this->setElementComment(implode('_', [ |
|
283
|
|
|
$key, $key2, $key3, $key4, $key5]), 'CAC', $arrayParameters['comments']); |
|
284
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $key5, $value5); |
|
285
|
|
|
} |
|
286
|
|
|
$this->objXmlWriter->endElement(); // $key4 |
|
287
|
|
|
} else { |
|
288
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $key4, $value4); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
$this->objXmlWriter->endElement(); // $key3 |
|
292
|
|
|
} else { |
|
293
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $key3, $value3); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
$this->objXmlWriter->endElement(); // $key2 |
|
298
|
|
|
} else { |
|
299
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $key2, $value2); |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
$this->objXmlWriter->endElement(); // $key |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|