Passed
Push — main ( 1dd856...8e456c )
by Daniel
02:57
created

readElectronicInvoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 20
ccs 19
cts 19
cp 1
crap 1
rs 9.7666
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;
36
37 13
    private function getAccountingCustomerOrSupplierParty(array $arrayIn): array
38
    {
39 13
        $arrayOut = [];
40 13
        foreach ($this->arraySettings['CustomOrder'][$arrayIn['type']] as $strElement) {
41 13
            if (isset($arrayIn['data']->children('cac', true)->$strElement)) {
42 13
                if ($strElement === 'PartyTaxScheme') {
43 12
                    $arrayOut[$strElement] = $this->getMultipleElementsByKey($arrayIn['data']
44 12
                            ->children('cac', true)->$strElement);
45
                } else {
46 13
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']->children('cac', true)->$strElement);
47
                }
48
            }
49 13
            if (isset($arrayIn['data']->children('cbc', true)->$strElement)) {
50 3
                if ($strElement === 'EndpointID') {
51 3
                    $arrayOut['EndpointID'] = [
52 3
                        'schemeID' => $arrayIn['data']->children('cbc', true)->EndpointID
53 3
                            ->attributes()->schemeID->__toString(),
54 3
                        'value'    => $arrayIn['data']->children('cbc', true)->EndpointID->__toString(),
55 3
                    ];
56
                } else {
57
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']->children('cbc', true)->$strElement);
58
                }
59
            }
60
        }
61 13
        return $arrayOut;
62
    }
63
64 13
    private function getBasicOrAggregateKey(array $arrayDocNmSp, string $strBasOrAggr): string
65
    {
66 13
        $arrayPieces = explode(':', $arrayDocNmSp[$strBasOrAggr]);
67 13
        return $arrayPieces[count($arrayPieces) - 1];
68
    }
69
70 13
    private function getDocumentRoot(object $objFile): array
71
    {
72 13
        $arrayDocument = [
73 13
            'DocumentTagName'    => $objFile->getName(),
74 13
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
75 13
        ];
76 13
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
77 11
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
78 10
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
79
            }
80
        }
81 13
        return $arrayDocument;
82
    }
83
84 13
    private function getElementsOrdered(array $arrayDataIn): array
85
    {
86 13
        $arrayOutput = [];
87 13
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
88 13
            if (isset($arrayDataIn['data']->$value)) {
89 13
                $arrayOutput[$value] = $arrayDataIn['data']->$value->__toString();
90
            }
91
        }
92 13
        return $arrayOutput;
93
    }
94
95 13
    private function getHeader(array $arrayParams): array
96
    {
97 13
        $arrayDocument = [
98 13
            'TaxTotal' => $this->getTax($arrayParams['CAC']->TaxTotal),
99 13
        ];
100 13
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
101 13
            if (isset($arrayParams['CAC']->$key)) {
102 13
                $arrayDocument[$key] = $this->getHeaderComponents($arrayParams, $key, $value);
103
            }
104
        }
105 13
        return $arrayDocument;
106
    }
107
108 13
    private function getHeaderComponents(array $arrayParams, string $key, string $value): array | string
109
    {
110 13
        $arrayDocument = [];
111 13
        if ($value === 'SingleCompany') {
112 13
            $arrayDocument = [
113 13
                'Party' => $this->getAccountingCustomerOrSupplierParty([
114 13
                    'data' => $arrayParams['CAC']->$key->children('cac', true)->Party,
115 13
                    'type' => $key,
116 13
                ])
117 13
            ];
118
        } else {
119 13
            $arrayMapping  = [
120 13
                'Multiple'                  => [
121 13
                    'getMultipleElementsByKey'
122 13
                    , $arrayParams['data']->children('cac', true)->$key
123 13
                ],
124 13
                'MultipleStandard'          => ['getMultipleElementsStandard', $arrayParams['CAC']->$key],
125 13
                'Single'                    => ['getElements', $arrayParams['CAC']->$key],
126 13
                'SingleCompanyWithoutParty' => [
127 13
                    'getAccountingCustomerOrSupplierParty'
128 13
                    , [
129 13
                        'data' => $arrayParams['CAC']->$key,
130 13
                        'type' => $key,
131 13
                    ]
132 13
                ],
133 13
            ];
134 13
            $arrayDocument = $this->getRightMethod($arrayMapping[$value][0], $arrayMapping[$value][1]);
135
        }
136 13
        return $arrayDocument;
137
    }
138
139 13
    public function readElectronicInvoice(string $strFile): array
140
    {
141 13
        $this->getProcessingDetails();
142 13
        $this->getHierarchyTagOrder();
143 13
        $objFile                 = new \SimpleXMLElement($strFile, null, true);
144 13
        $arrayDocument           = $this->getDocumentRoot($objFile);
145 13
        $arrayBasics             = $this->getElementsOrdered([
146 13
            'data'          => $objFile->children('cbc', true),
147 13
            'namespace_cbc' => $arrayDocument['DocumentNameSpaces']['cbc'],
148 13
        ]);
149 13
        $arrayAggregates         = $this->getHeader([
150 13
            'CAC'  => $objFile->children('cac', true),
151 13
            'data' => $objFile,
152 13
        ]);
153 13
        $arrayDocument['Header'] = [
154 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], 'cbc') => $arrayBasics,
155 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], 'cac') => $arrayAggregates,
156 13
        ];
157 13
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
158 13
        return $arrayDocument;
159
    }
160
}
161