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 ElectornicInvoiceRead |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
use TraitBasic, |
|
|
|
|
35
|
|
|
TraitCompanies, |
36
|
|
|
TraitTax, |
37
|
|
|
TraitLines; |
38
|
|
|
|
39
|
|
|
private function getDocumentRoot(object $objFile): array |
40
|
|
|
{ |
41
|
|
|
$arrayDocument = [ |
42
|
|
|
'DocumentTagName' => $objFile->getName(), |
43
|
|
|
'DocumentNameSpaces' => $objFile->getDocNamespaces(true), |
44
|
|
|
]; |
45
|
|
|
if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) { |
46
|
|
|
if (isset($objFile->attributes('xsi', true)->schemaLocation)) { |
47
|
|
|
$arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
return $arrayDocument; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function getElementsOrdered(string $strKeyOrderedElements, \SimpleXMLElement $arrayDataIn): array |
54
|
|
|
{ |
55
|
|
|
$arrayOutput = []; |
56
|
|
|
foreach ($this->arraySettings['CustomOrder'][$strKeyOrderedElements] as $value) { |
57
|
|
|
if (isset($arrayDataIn->$value)) { |
58
|
|
|
$arrayOutput[$value] = $arrayDataIn->$value->__toString(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return $arrayOutput; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function getHeader(array $arrayParams): array |
65
|
|
|
{ |
66
|
|
|
$arrayCBC = explode(':', $arrayParams['DocumentNameSpaces']['cbc']); |
67
|
|
|
$strCBC = $arrayCBC[(count($arrayCBC) - 1)]; // CommonBasicComponents |
68
|
|
|
$arrayDocument[$strCBC] = $this->getElementsOrdered('Header_CBC', $arrayParams['CBC']); |
|
|
|
|
69
|
|
|
$strCAC = $arrayParams['cacName']; // CommonAggregateComponents |
70
|
|
|
$arrayDocument[$strCAC] = [ |
71
|
|
|
'AccountingCustomerParty' => $this->getAccountingCustomerParty($arrayParams['CAC'] |
72
|
|
|
->AccountingCustomerParty->children('cac', true)->Party), |
73
|
|
|
'AccountingSupplierParty' => $this->getAccountingSupplierParty($arrayParams['CAC'] |
74
|
|
|
->AccountingSupplierParty->children('cac', true)->Party), |
75
|
|
|
'TaxTotal' => $this->getTaxTotal($arrayParams['CAC']->TaxTotal), |
76
|
|
|
]; |
77
|
|
|
// optional components ========================================================================================= |
78
|
|
|
foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) { |
79
|
|
|
if (isset($arrayParams['CAC']->$key)) { |
80
|
|
|
switch ($value) { |
81
|
|
|
case 'Single': |
82
|
|
|
$arrayDocument[$strCAC][$key] = $this->getElements($arrayParams['CAC']->$key); |
83
|
|
|
break; |
84
|
|
|
case 'Multiple': |
85
|
|
|
$arrayDocument[$strCAC][$key] = $this->getMultiplePaymentMeansElements($arrayParams['CAC']->$key); |
86
|
|
|
break; |
87
|
|
|
case 'MultipleStandard': |
88
|
|
|
$arrayDocument[$strCAC][$key] = $this->getMultipleElementsStandard($arrayParams['CAC']->$key); |
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
if (isset($arrayParams['CAC']->Delivery)) { |
94
|
|
|
$strEl = $arrayParams['CAC']->Delivery; |
95
|
|
|
$strElement = $strEl->children('cac', true) |
96
|
|
|
->DeliveryLocation->children('cac', true)->Address; |
97
|
|
|
$arrayDocument[$strCAC]['Delivery']['DeliveryLocation']['Address'] = [ |
98
|
|
|
'StreetName' => $strElement->children('cbc', true)->StreetName->__toString(), |
99
|
|
|
'CityName' => $strElement->children('cbc', true)->CityName->__toString(), |
100
|
|
|
'PostalZone' => $strElement->children('cbc', true)->PostalZone->__toString(), |
101
|
|
|
'Country' => [ |
102
|
|
|
'IdentificationCode' => $strElement->children('cac', true)->Country->children('cbc', true) |
103
|
|
|
->IdentificationCode->__toString(), |
104
|
|
|
], |
105
|
|
|
]; |
106
|
|
|
if (isset($strEl->children('cbc', true)->ActualDeliveryDate)) { |
107
|
|
|
$arrayDocument[$strCAC]['Delivery']['ActualDeliveryDate'] = $strEl |
108
|
|
|
->children('cbc', true)->ActualDeliveryDate->__toString(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
return $arrayDocument; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function getMultiplePaymentMeansElements(array|\SimpleXMLElement $arrayIn): array |
115
|
|
|
{ |
116
|
|
|
$arrayToReturn = []; |
117
|
|
|
$intLineNo = 0; |
118
|
|
|
foreach ($arrayIn as $child) { |
119
|
|
|
$intLineNo++; |
120
|
|
|
$intLineStr = ($intLineNo < 10 ? '0' : '') . $intLineNo; |
121
|
|
|
$arrayToReturn[$intLineStr] = $this->getElements($child); |
122
|
|
|
} |
123
|
|
|
return $arrayToReturn; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function readElectronicInvoice(string $strFile): array |
127
|
|
|
{ |
128
|
|
|
$this->getHierarchyTagOrder(); |
129
|
|
|
$objFile = new \SimpleXMLElement($strFile, NULL, TRUE); |
130
|
|
|
$arrayDocument = $this->getDocumentRoot($objFile); |
131
|
|
|
$arrayCAC = explode(':', $arrayDocument['DocumentNameSpaces']['cac']); |
132
|
|
|
$strElementA = $arrayCAC[count($arrayCAC) - 1]; // CommonAggregateComponents |
133
|
|
|
$arrayDocument['Header'] = $this->getHeader([ |
134
|
|
|
'CAC' => $objFile->children('cac', true), |
135
|
|
|
'cacName' => $strElementA, |
136
|
|
|
'CBC' => $objFile->children('cbc', true), |
137
|
|
|
'DocumentNameSpaces' => $arrayDocument['DocumentNameSpaces'], |
138
|
|
|
'DocumentTagName' => $arrayDocument['DocumentTagName'], |
139
|
|
|
]); |
140
|
|
|
$arrayDocument['Lines'] = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']); |
141
|
|
|
return $arrayDocument; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|