1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2024 - 2025 Daniel Popiniuc. |
5
|
|
|
* All rights reserved. This program and the accompanying materials |
6
|
|
|
* are made available under the terms of the Eclipse Public License v1.0 |
7
|
|
|
* which accompanies this distribution, and is available at |
8
|
|
|
* http://www.eclipse.org/legal/epl-v10.html |
9
|
|
|
* |
10
|
|
|
* Contributors: |
11
|
|
|
* Daniel Popiniuc |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace danielgp\efactura; |
15
|
|
|
|
16
|
|
|
class ClassElectronicInvoiceRead |
17
|
|
|
{ |
18
|
|
|
use TraitBasic; |
19
|
|
|
use TraitTax; |
20
|
|
|
use TraitLines; |
21
|
|
|
|
22
|
13 |
|
private function getAccountingCustomerOrSupplierParty(array $arrayIn): array |
23
|
|
|
{ |
24
|
13 |
|
$arrayOut = []; |
25
|
13 |
|
foreach ($this->arraySettings['CustomOrder'][$arrayIn['type']] as $strElement) { |
26
|
13 |
|
if (isset($arrayIn['data']->children($this->arrayProcessing['mapping']['cac'], true)->$strElement)) { |
27
|
13 |
|
if ($strElement === 'PartyTaxScheme') { |
28
|
12 |
|
$arrayOut[$strElement] = $this->getMultipleElementsByKey($arrayIn['data'] |
29
|
12 |
|
->children($this->arrayProcessing['mapping']['cac'], true)->$strElement); |
30
|
|
|
} else { |
31
|
13 |
|
$arrayOut[$strElement] = $this->getElements($arrayIn['data'] |
32
|
13 |
|
->children($this->arrayProcessing['mapping']['cac'], true)->$strElement); |
33
|
|
|
} |
34
|
|
|
} |
35
|
13 |
|
if (isset($arrayIn['data']->children($this->arrayProcessing['mapping']['cbc'], true)->$strElement)) { |
36
|
3 |
|
if ($strElement === 'EndpointID') { |
37
|
3 |
|
$arrayOut['EndpointID'] = [ |
38
|
3 |
|
'schemeID' => $arrayIn['data'] |
39
|
3 |
|
->children($this->arrayProcessing['mapping']['cbc'], true)->EndpointID |
40
|
3 |
|
->attributes()->schemeID->__toString(), |
41
|
3 |
|
'value' => $arrayIn['data'] |
42
|
3 |
|
->children($this->arrayProcessing['mapping']['cbc'], true)->EndpointID |
43
|
3 |
|
->__toString(), |
44
|
3 |
|
]; |
45
|
|
|
} else { |
46
|
|
|
$arrayOut[$strElement] = $this->getElements($arrayIn['data'] |
47
|
|
|
->children($this->arrayProcessing['mapping']['cbc'], true)->$strElement); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
13 |
|
return $arrayOut; |
52
|
|
|
} |
53
|
|
|
|
54
|
13 |
|
private function getBasicOrAggregateKey(array $arrayDocNmSp, string $strBasOrAggr): string |
55
|
|
|
{ |
56
|
13 |
|
$arrayPieces = explode(':', $arrayDocNmSp[$strBasOrAggr]); |
57
|
13 |
|
return $arrayPieces[count($arrayPieces) - 1]; |
58
|
|
|
} |
59
|
|
|
|
60
|
13 |
|
public function getDocumentRoot(object $objFile): array |
61
|
|
|
{ |
62
|
13 |
|
$arrayDocument = [ |
63
|
13 |
|
'DocumentTagName' => $objFile->getName(), |
64
|
13 |
|
'DocumentNameSpaces' => $objFile->getDocNamespaces(true), |
65
|
13 |
|
]; |
66
|
13 |
|
if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) { |
67
|
11 |
|
if (isset($objFile->attributes('xsi', true)->schemaLocation)) { |
68
|
10 |
|
$arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation; |
69
|
|
|
} |
70
|
|
|
} |
71
|
13 |
|
if ($arrayDocument['DocumentTagName'] === 'header') { |
72
|
|
|
foreach ($objFile->attributes() as $attributeName => $attributeValue) { |
73
|
|
|
$arrayDocument['header'][$attributeName] = $attributeValue->__toString(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
13 |
|
return $arrayDocument; |
77
|
|
|
} |
78
|
|
|
|
79
|
13 |
|
private function getElementsOrdered(array $arrayDataIn): array |
80
|
|
|
{ |
81
|
13 |
|
$arrayOutput = []; |
82
|
13 |
|
foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) { |
83
|
13 |
|
if (isset($arrayDataIn['data']->$value)) { |
84
|
13 |
|
$arrayOutput[$value] = $arrayDataIn['data']->$value->__toString(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
13 |
|
return $arrayOutput; |
88
|
|
|
} |
89
|
|
|
|
90
|
13 |
|
private function getHeader(array $arrayParams): array |
91
|
|
|
{ |
92
|
13 |
|
$arrayDocument = [ |
93
|
13 |
|
'TaxTotal' => $this->getTax($arrayParams['CAC']->TaxTotal), |
94
|
13 |
|
]; |
95
|
13 |
|
foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) { |
96
|
13 |
|
if (isset($arrayParams['CAC']->$key)) { |
97
|
13 |
|
$arrayDocument[$key] = $this->getHeaderComponents($arrayParams, $key, $value); |
98
|
|
|
} |
99
|
|
|
} |
100
|
13 |
|
return $arrayDocument; |
101
|
|
|
} |
102
|
|
|
|
103
|
13 |
|
private function getHeaderComponents(array $arrayParams, string $key, string $value): array | string |
104
|
|
|
{ |
105
|
13 |
|
$arrayDocument = []; |
106
|
13 |
|
if ($value === 'SingleCompany') { |
107
|
13 |
|
$arrayDocument = [ |
108
|
13 |
|
'Party' => $this->getAccountingCustomerOrSupplierParty([ |
109
|
13 |
|
'data' => $arrayParams['CAC']->$key |
110
|
13 |
|
->children($this->arrayProcessing['mapping']['cac'], true)->Party, |
111
|
13 |
|
'type' => $key, |
112
|
13 |
|
]) |
113
|
13 |
|
]; |
114
|
|
|
} else { |
115
|
13 |
|
$arrayMapping = [ |
116
|
13 |
|
'Multiple' => [ |
117
|
13 |
|
'getMultipleElementsByKey' |
118
|
13 |
|
, $arrayParams['data']->children('cac', true)->$key |
119
|
13 |
|
], |
120
|
13 |
|
'MultipleStandard' => ['getMultipleElementsStandard', $arrayParams['CAC']->$key], |
121
|
13 |
|
'Single' => ['getElements', $arrayParams['CAC']->$key], |
122
|
13 |
|
'SingleCompanyWithoutParty' => [ |
123
|
13 |
|
'getAccountingCustomerOrSupplierParty' |
124
|
13 |
|
, [ |
125
|
13 |
|
'data' => $arrayParams['CAC']->$key, |
126
|
13 |
|
'type' => $key, |
127
|
13 |
|
] |
128
|
13 |
|
], |
129
|
13 |
|
]; |
130
|
13 |
|
$arrayDocument = $this->getRightMethod($arrayMapping[$value][0], $arrayMapping[$value][1]); |
131
|
|
|
} |
132
|
13 |
|
return $arrayDocument; |
133
|
|
|
} |
134
|
|
|
|
135
|
13 |
|
public function readElectronicXmlHeader(string $strFile): \SimpleXMLElement |
136
|
|
|
{ |
137
|
13 |
|
$this->getProcessingDetails(); |
138
|
13 |
|
$this->getHierarchyTagOrder(); |
139
|
13 |
|
$flags = LIBXML_PARSEHUGE | LIBXML_BIGLINES | LIBXML_NOERROR; |
140
|
13 |
|
$bolIsLocal = is_file($strFile); |
141
|
13 |
|
return new \SimpleXMLElement($strFile, $flags, $bolIsLocal); |
142
|
|
|
} |
143
|
|
|
|
144
|
13 |
|
public function readElectronicInvoice(string $strFile): array |
145
|
|
|
{ |
146
|
13 |
|
$objFile = $this->readElectronicXmlHeader($strFile); |
147
|
13 |
|
$arrayDocument = $this->getDocumentRoot($objFile); |
148
|
13 |
|
$this->setArrayProcessing($arrayDocument['DocumentNameSpaces']); |
149
|
13 |
|
$strMap = $this->arrayProcessing['mapping']; |
150
|
13 |
|
$arrayBasics = $this->getElementsOrdered([ |
151
|
13 |
|
'data' => $objFile->children($strMap['cbc'], true), |
152
|
13 |
|
'namespace_cbc' => $arrayDocument['DocumentNameSpaces'][$strMap['cbc']], |
153
|
13 |
|
]); |
154
|
13 |
|
$arrayAggregates = $this->getHeader([ |
155
|
13 |
|
'CAC' => $objFile->children($strMap['cac'], true), |
156
|
13 |
|
'data' => $objFile, |
157
|
13 |
|
]); |
158
|
13 |
|
$arrayDocument['Header'] = [ |
159
|
13 |
|
$this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], $strMap['cbc']) => $arrayBasics, |
160
|
13 |
|
$this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], $strMap['cac']) => $arrayAggregates, |
161
|
13 |
|
]; |
162
|
13 |
|
$arrayDocument['Lines'] = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']); |
163
|
13 |
|
return $arrayDocument; |
164
|
|
|
} |
165
|
|
|
|
166
|
13 |
|
private function setArrayProcessing(array $arrayDocumentNameSpaces): void |
167
|
|
|
{ |
168
|
13 |
|
$bolMappingsNotSet = [ |
169
|
13 |
|
'cac' => true, |
170
|
13 |
|
'cbc' => true, |
171
|
13 |
|
]; |
172
|
13 |
|
foreach ($arrayDocumentNameSpaces as $key => $value) { |
173
|
13 |
|
if (str_ends_with($value, ':CommonAggregateComponents-2') && $bolMappingsNotSet['cac']) { |
174
|
13 |
|
$this->arrayProcessing['mapping']['cac'] = $key; |
175
|
13 |
|
$bolMappingsNotSet['cac'] = false; |
176
|
|
|
} |
177
|
13 |
|
if (str_ends_with($value, ':CommonBasicComponents-2') && $bolMappingsNotSet['cbc']) { |
178
|
13 |
|
$this->arrayProcessing['mapping']['cbc'] = $key; |
179
|
13 |
|
$bolMappingsNotSet['cbc'] = false; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|