Passed
Push — main ( 375d1e...88b273 )
by Daniel
02:04
created

getMultiplePaymentMeansElements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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 ElectronicInvoiceRead
32
{
33
    use TraitBasic;
34
    use TraitTax;
35
    use TraitLines;
0 ignored issues
show
Bug introduced by
The trait danielgp\efactura\TraitLines requires the property $Item which is not provided by danielgp\efactura\ElectronicInvoiceRead.
Loading history...
36
37
    private function getAccountingCustomerOrSupplierParty(array $arrayIn): array
38
    {
39
        $arrayOut = [];
40
        foreach ($this->arraySettings['CustomOrder'][$arrayIn['type']] as $strElement) {
41
            if (isset($arrayIn['data']->children('cac', true)->$strElement)) {
42
                if ($strElement === 'PartyTaxScheme') {
43
                    $arrayOut[$strElement] = $this->getMultipleElements($arrayIn['data']
44
                            ->children('cac', true)->$strElement);
45
                } else {
46
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']->children('cac', true)->$strElement);
47
                }
48
            }
49
            if (isset($arrayIn['data']->children('cbc', true)->$strElement)) {
50
                if ($strElement === 'EndpointID') {
51
                    $arrayOut['EndpointID'] = [
52
                        'schemeID' => $arrayIn['data']->children('cbc', true)->EndpointID
53
                            ->attributes()->schemeID->__toString(),
54
                        'value'    => $arrayIn['data']->children('cbc', true)->EndpointID->__toString(),
55
                    ];
56
                } else {
57
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']->children('cbc', true)->$strElement);
58
                }
59
            }
60
        }
61
        return $arrayOut;
62
    }
63
64
    private function getDocumentRoot(object $objFile): array
65
    {
66
        $arrayDocument = [
67
            'DocumentTagName'    => $objFile->getName(),
68
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
69
        ];
70
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
71
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
72
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
73
            }
74
        }
75
        return $arrayDocument;
76
    }
77
78
    private function getElementsOrdered(array $arrayDataIn): array
79
    {
80
        $arrayOutput = [];
81
        $arrayCBC    = explode(':', $arrayDataIn['namespace_cbc']);
82
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
83
            if (isset($arrayDataIn['data']->$value)) {
84
                $arrayOutput[$value] = $arrayDataIn['data']->$value->__toString();
85
            }
86
        }
87
        return [$arrayCBC[(count($arrayCBC) - 1)] => $arrayOutput];
88
    }
89
90
    private function getHeader(array $arrayParams): array
91
    {
92
        $arrayDocument                      = $this->getElementsOrdered([
93
            'data'          => $arrayParams['CBC'],
94
            'namespace_cbc' => $arrayParams['DocumentNameSpaces']['cbc'],
95
        ]);
96
        $strCAC                             = $arrayParams['cacName']; // CommonAggregateComponents
97
        $arrayDocument[$strCAC]['TaxTotal'] = $this->getTax($arrayParams['CAC']->TaxTotal);
98
        // optional components =========================================================================================
99
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
100
            if (isset($arrayParams['CAC']->$key)) {
101
                switch ($value) {
102
                    case 'Multiple':
103
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElements($arrayParams['CAC']
104
                            ->$key);
105
                        break;
106
                    case 'MultipleStandard':
107
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElementsStandard($arrayParams['CAC']->$key);
108
                        break;
109
                    case 'Single':
110
                        $arrayDocument[$strCAC][$key] = $this->getElements($arrayParams['CAC']->$key);
111
                        break;
112
                    case 'SingleCompany':
113
                        $arrayDocument[$strCAC][$key] = [
114
                            'Party' => $this->getAccountingCustomerOrSupplierParty([
115
                                'data' => $arrayParams['CAC']->$key->children('cac', true)->Party,
116
                                'type' => $key,
117
                            ])
118
                        ];
119
                        break;
120
                    case 'SingleCompanyWithoutParty':
121
                        $arrayDocument[$strCAC][$key] = $this->getAccountingCustomerOrSupplierParty([
122
                            'data' => $arrayParams['CAC']->$key,
123
                            'type' => $key,
124
                        ]);
125
                        break;
126
                }
127
            }
128
        }
129
        return $arrayDocument;
130
    }
131
132
    private function getMultipleElements(array|\SimpleXMLElement $arrayIn): array
133
    {
134
        $arrayToReturn = [];
135
        $intLineNo     = 0;
136
        foreach ($arrayIn as $child) {
137
            $intLineNo++;
138
            $intLineStr                 = $this->getLineStringFromNumber($intLineNo);
139
            $arrayToReturn[$intLineStr] = $this->getElements($child);
140
        }
141
        return $arrayToReturn;
142
    }
143
144
    public function readElectronicInvoice(string $strFile): array
145
    {
146
        $this->getProcessingDetails();
147
        $this->getHierarchyTagOrder();
148
        $objFile                 = new \SimpleXMLElement($strFile, null, true);
149
        $arrayDocument           = $this->getDocumentRoot($objFile);
150
        $arrayCAC                = explode(':', $arrayDocument['DocumentNameSpaces']['cac']);
151
        $strElementA             = $arrayCAC[count($arrayCAC) - 1]; // CommonAggregateComponents
152
        $arrayDocument['Header'] = $this->getHeader([
153
            'CAC'                => $objFile->children('cac', true),
154
            'cacName'            => $strElementA,
155
            'CBC'                => $objFile->children('cbc', true),
156
            'DocumentNameSpaces' => $arrayDocument['DocumentNameSpaces'],
157
            'DocumentTagName'    => $arrayDocument['DocumentTagName'],
158
        ]);
159
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
160
        return $arrayDocument;
161
    }
162
}
163