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 \XMLWriter $objXmlWriter; |
37
|
|
|
|
38
|
|
|
private function loadSettingsAndManageDefaults(array $arrayData, array $arrayFeatures): array |
39
|
|
|
{ |
40
|
|
|
// if no DocumentNameSpaces seen take Default ones from local configuration |
41
|
|
|
$this->getSettingsFromFileIntoMemory($arrayFeatures['Comments']); |
42
|
|
|
$arrayDefaults = $this->getDefaultsIntoDataSet($arrayData, $arrayFeatures['SchemaLocation']); |
43
|
|
|
if ($arrayDefaults !== []) { |
44
|
|
|
$arrayData = array_merge($arrayData, $arrayDefaults['Root']); |
45
|
|
|
if (!array_key_exists('CustomizationID', $arrayData['Header']['CommonBasicComponents-2'])) { |
46
|
|
|
$arrayData['Header']['CommonBasicComponents-2']['CustomizationID'] = 'urn:cen.eu:en16931:2017' |
47
|
|
|
. '#compliant#urn:efactura.mfinante.ro:CIUS-RO:' . $arrayDefaults['CIUS-RO']; |
48
|
|
|
$arrayData['Header']['CommonBasicComponents-2']['UBLVersionID'] = $arrayDefaults['UBL']; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return $arrayData; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function setDocumentTag(array $arrayDocumentData): void |
55
|
|
|
{ |
56
|
|
|
$this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']); |
57
|
|
|
foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) { |
58
|
|
|
if ($key === '') { |
59
|
|
|
$strValue = sprintf($value, $arrayDocumentData['DocumentTagName']); |
60
|
|
|
$this->objXmlWriter->writeAttributeNS(NULL, 'xmlns', NULL, $strValue); |
61
|
|
|
} else { |
62
|
|
|
$this->objXmlWriter->writeAttributeNS('xmlns', $key, NULL, $value); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
if (array_key_exists('SchemaLocation', $arrayDocumentData)) { |
66
|
|
|
$this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function setElementComment(string $strKey): void |
71
|
|
|
{ |
72
|
|
|
if (array_key_exists($strKey, $this->arraySettings['Comments'])) { |
73
|
|
|
$elementComment = $this->arraySettings['Comments'][$strKey]; |
74
|
|
|
if (is_array($elementComment)) { |
75
|
|
|
foreach ($elementComment as $value) { |
76
|
|
|
$this->objXmlWriter->writeComment($value); |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
$this->objXmlWriter->writeComment($elementComment); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function setElementsOrdered(array $arrayInput): void |
85
|
|
|
{ |
86
|
|
|
if ($arrayInput['commentParentKey'] === 'AllowanceCharge~ChargeIndicator0') { |
87
|
|
|
error_log(json_encode($arrayInput)); |
88
|
|
|
} |
89
|
|
|
$this->setElementComment($arrayInput['commentParentKey']); |
90
|
|
|
$this->objXmlWriter->startElement('cac:' . $arrayInput['tag']); |
91
|
|
|
$this->setExtraElement($arrayInput, 'Start'); |
92
|
|
|
$arrayCustomOrder = $this->arraySettings['CustomOrder'][$arrayInput['commentParentKey']]; |
93
|
|
|
foreach ($arrayCustomOrder as $value) { // get the values in expected order |
94
|
|
|
if (array_key_exists($value, $arrayInput['data'])) { // because certain value are optional |
95
|
|
|
$key = implode('_', [$arrayInput['commentParentKey'], $value]); |
96
|
|
|
$matches = []; |
97
|
|
|
preg_match('/^.*(Amount|Quantity)$/', $value, $matches, PREG_OFFSET_CAPTURE); |
98
|
|
|
if ($key === 'Lines_AllowanceCharge') { |
99
|
|
|
foreach ($arrayInput['data'][$value] as $value2) { |
100
|
|
|
$this->setElementsOrdered([ |
101
|
|
|
'commentParentKey' => $key, |
102
|
|
|
'data' => $value2, |
103
|
|
|
'tag' => $value, |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
} elseif (in_array($value, ['EmbeddedDocumentBinaryObject', 'EndpointID']) || in_array($arrayInput['commentParentKey'], ['AccountingCustomerParty_PartyIdentification', 'AccountingSupplierParty_PartyIdentification', 'Lines_Item_SellersItemIdentification', 'Lines_Item_StandardItemIdentification', 'Lines_Item_CommodityClassification', 'PayeeParty_PartyIdentification']) || ($key === 'Delivery_DeliveryLocation_ID')) { |
107
|
|
|
$this->setSingleElementWithAttribute([ |
108
|
|
|
'commentParentKey' => $arrayInput['commentParentKey'], |
109
|
|
|
'data' => $arrayInput['data'][$value], |
110
|
|
|
'tag' => $value, |
111
|
|
|
]); |
112
|
|
|
} elseif (in_array($value, ['AdditionalItemProperty', 'CommodityClassification', 'PartyTaxScheme', 'StandardItemIdentification', 'TaxSubtotal'])) { |
113
|
|
|
$this->setMultipleElementsOrdered([ |
114
|
|
|
'commentParentKey' => $key, |
115
|
|
|
'data' => $arrayInput['data'][$value], |
116
|
|
|
'tag' => $value, |
117
|
|
|
]); |
118
|
|
|
} elseif (($matches !== []) || !is_array($arrayInput['data'][$value])) { |
119
|
|
|
$this->setSingleElementWithAttribute([ |
120
|
|
|
'commentParentKey' => $arrayInput['commentParentKey'], |
121
|
|
|
'data' => $arrayInput['data'][$value], |
122
|
|
|
'tag' => $value, |
123
|
|
|
]); |
124
|
|
|
} elseif (is_array($arrayInput['data'][$value])) { |
125
|
|
|
$this->setElementsOrdered([ |
126
|
|
|
'commentParentKey' => $key, |
127
|
|
|
'data' => $arrayInput['data'][$value], |
128
|
|
|
'tag' => $value, |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
$this->setExtraElement($arrayInput, 'End'); |
134
|
|
|
$this->objXmlWriter->endElement(); // $key |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function setExtraElement(array $arrayInput, string $strType): void |
138
|
|
|
{ |
139
|
|
|
if (in_array($arrayInput['tag'], ['AccountingCustomerParty', 'AccountingSupplierParty'])) { |
140
|
|
|
switch ($strType) { |
141
|
|
|
case 'End': |
142
|
|
|
$this->objXmlWriter->endElement(); |
143
|
|
|
break; |
144
|
|
|
case 'Start': |
145
|
|
|
$this->objXmlWriter->startElement('cac:Party'); |
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function setHeaderCommonBasicComponents(array $arrayElementWithData): void |
152
|
|
|
{ |
153
|
|
|
$arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header_CBC']; |
154
|
|
|
foreach ($arrayCustomOrdered as $value) { |
155
|
|
|
if (array_key_exists($value, $arrayElementWithData)) { |
156
|
|
|
$this->setElementComment($value); |
157
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function setManageComment(string $strCommentParentKey, array $arrayIn): string |
163
|
|
|
{ |
164
|
|
|
if (str_starts_with($strCommentParentKey, 'AllowanceCharge')) { |
165
|
|
|
$arrayCommentPieces = explode('_', $strCommentParentKey); |
166
|
|
|
// carefully manage a child to decide on comment tag |
167
|
|
|
$strChargeIndicator = $arrayIn['ChargeIndicator']; |
168
|
|
|
if (in_array($strChargeIndicator, ['0', '1'])) { |
169
|
|
|
$strChargeIndicator = [ |
170
|
|
|
'0' => 'false', |
171
|
|
|
'1' => 'true', |
172
|
|
|
][$arrayIn['ChargeIndicator']]; |
173
|
|
|
} |
174
|
|
|
array_splice($arrayCommentPieces, 0, 1, 'AllowanceCharge~ChargeIndicator' |
175
|
|
|
. ucfirst($strChargeIndicator)); |
176
|
|
|
$strCommentParentKey = implode('_', $arrayCommentPieces); |
177
|
|
|
} |
178
|
|
|
return $strCommentParentKey; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private function setMultipleElementsOrdered(array $arrayData): void |
182
|
|
|
{ |
183
|
|
|
foreach ($arrayData['data'] as $value) { |
184
|
|
|
$strCommentParentKey = $this->setManageComment($arrayData['commentParentKey'], $value); |
185
|
|
|
$this->setElementsOrdered([ |
186
|
|
|
'commentParentKey' => $strCommentParentKey, |
187
|
|
|
'data' => $value, |
188
|
|
|
'tag' => $arrayData['tag'], |
189
|
|
|
]); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function setNumericValue(string $strTag, array $arrayDataIn): string|float |
194
|
|
|
{ |
195
|
|
|
$sReturn = $arrayDataIn['value']; |
196
|
|
|
$arrayRawTags = ['CreditedQuantity', 'EndpointID', 'InvoicedQuantity', 'ItemClassificationCode', 'PriceAmount']; |
197
|
|
|
if (is_numeric($arrayDataIn['value']) && !in_array($strTag, $arrayRawTags)) { |
198
|
|
|
$fmt = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL); |
199
|
|
|
$fmt->setAttribute(\NumberFormatter::GROUPING_USED, 0); |
200
|
|
|
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0); |
201
|
|
|
// if contains currencyID consider 2 decimals as minimum |
202
|
|
|
if (in_array('currencyID', array_keys($arrayDataIn))) { |
203
|
|
|
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2); |
204
|
|
|
} |
205
|
|
|
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 2); |
206
|
|
|
$sReturn = $fmt->format($arrayDataIn['value']); |
207
|
|
|
} |
208
|
|
|
return $sReturn; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
private function setPrepareXml(string $strFile, int $intIdent = 4): void |
212
|
|
|
{ |
213
|
|
|
$this->objXmlWriter = new \XMLWriter(); |
214
|
|
|
$this->objXmlWriter->openURI($strFile); |
215
|
|
|
$this->objXmlWriter->setIndent(true); |
216
|
|
|
$this->objXmlWriter->setIndentString(str_repeat(' ', $intIdent)); |
217
|
|
|
$this->objXmlWriter->startDocument('1.0', 'UTF-8'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private function setProduceMiddleXml(array $arrayData): void |
221
|
|
|
{ |
222
|
|
|
$arrayAggregates = $arrayData['Header']['CommonAggregateComponents-2']; |
223
|
|
|
$arrayOptionalElementsHeader = [ |
224
|
|
|
'InvoicePeriod' => 'Single', |
225
|
|
|
'OrderReference' => 'Single', |
226
|
|
|
'BillingReference' => 'Single', |
227
|
|
|
'DespatchDocumentReference' => 'Single', |
228
|
|
|
'ReceiptDocumentReference' => 'Single', |
229
|
|
|
'OriginatorDocumentReference' => 'Single', |
230
|
|
|
'ContractDocumentReference' => 'Single', |
231
|
|
|
'AdditionalDocumentReference' => 'Multiple', |
232
|
|
|
'ProjectReference' => 'Single', |
233
|
|
|
'AccountingSupplierParty' => 'SingleCompany', |
234
|
|
|
'AccountingCustomerParty' => 'SingleCompany', |
235
|
|
|
'PayeeParty' => 'Single', |
236
|
|
|
'TaxRepresentativeParty' => 'Single', |
237
|
|
|
'Delivery' => 'Single', |
238
|
|
|
'PaymentMeans' => 'Multiple', |
239
|
|
|
'PaymentTerms' => 'Single', |
240
|
|
|
'DocumentReference' => 'Single', |
241
|
|
|
'AllowanceCharge' => 'Multiple', |
242
|
|
|
'TaxTotal' => 'Multiple', |
243
|
|
|
'LegalMonetaryTotal' => 'Single', |
244
|
|
|
]; |
245
|
|
|
foreach ($arrayOptionalElementsHeader as $key => $strLogicType) { |
246
|
|
|
if (array_key_exists($key, $arrayAggregates)) { |
247
|
|
|
switch ($strLogicType) { |
248
|
|
|
case 'Multiple': |
249
|
|
|
$this->setMultipleElementsOrdered([ |
250
|
|
|
'commentParentKey' => $key, |
251
|
|
|
'data' => $arrayAggregates[$key], |
252
|
|
|
'tag' => $key, |
253
|
|
|
]); |
254
|
|
|
break; |
255
|
|
|
case 'Single': |
256
|
|
|
$this->setElementsOrdered([ |
257
|
|
|
'commentParentKey' => $key, |
258
|
|
|
'data' => $arrayAggregates[$key], |
259
|
|
|
'tag' => $key, |
260
|
|
|
]); |
261
|
|
|
break; |
262
|
|
|
case 'SingleCompany': |
263
|
|
|
$this->setElementsOrdered([ |
264
|
|
|
'commentParentKey' => $key, |
265
|
|
|
'data' => $arrayAggregates[$key]['Party'], |
266
|
|
|
'tag' => $key, |
267
|
|
|
]); |
268
|
|
|
break; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
private function setSingleComment(array $arrayInput): void |
275
|
|
|
{ |
276
|
|
|
if (array_key_exists('commentParentKey', $arrayInput)) { |
277
|
|
|
$this->setElementComment(implode('_', [$arrayInput['commentParentKey'], $arrayInput['tag']])); |
278
|
|
|
if (str_ends_with($arrayInput['tag'], 'Quantity')) { |
279
|
|
|
$this->setElementComment(implode('_', [$arrayInput['commentParentKey'], $arrayInput['tag'] |
280
|
|
|
. 'UnitOfMeasure'])); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
private function setSingleElementWithAttribute(array $arrayInput): void |
286
|
|
|
{ |
287
|
|
|
$this->setSingleComment($arrayInput); |
288
|
|
|
if (is_array($arrayInput['data']) && array_key_exists('value', $arrayInput['data'])) { |
289
|
|
|
$this->objXmlWriter->startElement('cbc:' . $arrayInput['tag']); |
290
|
|
|
foreach ($arrayInput['data'] as $key => $value) { |
291
|
|
|
if ($key !== 'value') { // if is not value, must be an attribute |
292
|
|
|
$this->objXmlWriter->writeAttribute($key, $value); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
$this->objXmlWriter->writeRaw($this->setNumericValue($arrayInput['tag'], $arrayInput['data'])); |
296
|
|
|
$this->objXmlWriter->endElement(); |
297
|
|
|
} else { |
298
|
|
|
$this->objXmlWriter->writeElement('cbc:' . $arrayInput['tag'], $arrayInput['data']); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function writeElectronicInvoice(string $strFile, array $inData, array $arrayFeatures): void |
303
|
|
|
{ |
304
|
|
|
$arrayData = $this->loadSettingsAndManageDefaults($inData, $arrayFeatures); |
305
|
|
|
if (!array_key_exists('Ident', $arrayFeatures)) { |
306
|
|
|
$arrayFeatures['Ident'] = 4; |
307
|
|
|
} |
308
|
|
|
$this->setPrepareXml($strFile, $arrayFeatures['Ident']); |
309
|
|
|
$this->setDocumentTag($arrayData); |
310
|
|
|
$this->setHeaderCommonBasicComponents($arrayData['Header']['CommonBasicComponents-2']); |
311
|
|
|
$this->setProduceMiddleXml($arrayData); |
312
|
|
|
// multiple Lines |
313
|
|
|
$this->setMultipleElementsOrdered([ |
314
|
|
|
'commentParentKey' => 'Lines', |
315
|
|
|
'data' => $arrayData['Lines'], |
316
|
|
|
'tag' => $arrayData['DocumentTagName'] . 'Line', |
317
|
|
|
]); |
318
|
|
|
$this->objXmlWriter->endElement(); // Invoice or CreditNote |
319
|
|
|
$this->objXmlWriter->flush(); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|