Test Failed
Push — main ( a258cb...1dd856 )
by Daniel
02:47
created

ClassElectronicInvoiceRead::getElementsOrdered()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
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 ClassElectronicInvoiceRead
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\ClassElectronicInvoiceRead.
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->getMultipleElementsByKey($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
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
82
            if (isset($arrayDataIn['data']->$value)) {
83
                $arrayOutput[$value] = $arrayDataIn['data']->$value->__toString();
84
            }
85
        }
86
        return $arrayOutput;
87
    }
88
89
    private function getHeader(array $arrayParams): array
90
    {
91
        $arrayDocument = [
92
            'TaxTotal' => $this->getTax($arrayParams['CAC']->TaxTotal),
93
        ];
94
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
95
            if (isset($arrayParams['CAC']->$key)) {
96
                $arrayDocument[$key] = $this->getHeaderComponents($arrayParams, $key, $value);
97
            }
98
        }
99
        return $arrayDocument;
100
    }
101
102
    private function getHeaderComponents(array $arrayParams, string $key, string $value): array | string
103
    {
104
        $arrayDocument = [];
105
        if ($value === 'SingleCompany') {
106
            $arrayDocument = [
107
                'Party' => $this->getAccountingCustomerOrSupplierParty([
108
                    'data' => $arrayParams['CAC']->$key->children('cac', true)->Party,
109
                    'type' => $key,
110
                ])
111
            ];
112
        } else {
113
            $arrayMapping  = [
114
                'Multiple'                  => [
115
                    'getMultipleElementsByKey'
116
                    , $arrayParams['data']->children('cac', true)->$key
117
                ],
118
                'MultipleStandard'          => ['getMultipleElementsStandard', $arrayParams['CAC']->$key],
119
                'Single'                    => ['getElements', $arrayParams['CAC']->$key],
120
                'SingleCompanyWithoutParty' => [
121
                    'getAccountingCustomerOrSupplierParty'
122
                    , [
123
                        'data' => $arrayParams['CAC']->$key,
124
                        'type' => $key,
125
                    ]
126
                ],
127
            ];
128
            $arrayDocument = $this->getRightMethod($arrayMapping[$value][0], $arrayMapping[$value][1]);
129
        }
130
        return $arrayDocument;
131
    }
132
133
    public function readElectronicInvoice(string $strFile): array
134
    {
135
        $this->getProcessingDetails();
136
        $this->getHierarchyTagOrder();
137
        $objFile                        = new \SimpleXMLElement($strFile, null, true);
138
        $arrayDocument                  = $this->getDocumentRoot($objFile);
139
        $arrayCBC                       = explode(':', $arrayDocument['DocumentNameSpaces']['cbc']);
140
        $arrayCommonBasicComponents     = $this->getElementsOrdered([
141
            'data'          => $objFile->children('cbc', true),
142
            'namespace_cbc' => $arrayDocument['DocumentNameSpaces']['cbc'],
143
        ]);
144
        $arrayCAC                       = explode(':', $arrayDocument['DocumentNameSpaces']['cac']);
145
        // CommonAggregateComponents
146
        $arrayCommonAggregateComponents = $this->getHeader([
147
            'CAC'  => $objFile->children('cac', true),
148
            'data' => $objFile,
149
        ]);
150
        $arrayDocument['Header']        = [
151
            $arrayCBC[count($arrayCBC) - 1] => $arrayCommonBasicComponents,
152
            $arrayCAC[count($arrayCAC) - 1] => $arrayCommonAggregateComponents,
153
        ];
154
        $arrayDocument['Lines']         = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
155
        return $arrayDocument;
156
    }
157
}
158