Passed
Push — main ( fea1d9...10585a )
by Daniel
02:09
created

ElectronicInvoiceRead::getMultipleElements()   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
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
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->getMultipleElementsByKey($arrayIn['data'], $strElement);
44
                } else {
45
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']->children('cac', true)->$strElement);
46
                }
47
            }
48
            if (isset($arrayIn['data']->children('cbc', true)->$strElement)) {
49
                if ($strElement === 'EndpointID') {
50
                    $arrayOut['EndpointID'] = [
51
                        'schemeID' => $arrayIn['data']->children('cbc', true)->EndpointID
52
                            ->attributes()->schemeID->__toString(),
53
                        'value'    => $arrayIn['data']->children('cbc', true)->EndpointID->__toString(),
54
                    ];
55
                } else {
56
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']->children('cbc', true)->$strElement);
57
                }
58
            }
59
        }
60
        return $arrayOut;
61
    }
62
63
    private function getDocumentRoot(object $objFile): array
64
    {
65
        $arrayDocument = [
66
            'DocumentTagName'    => $objFile->getName(),
67
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
68
        ];
69
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
70
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
71
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
72
            }
73
        }
74
        return $arrayDocument;
75
    }
76
77
    private function getElementsOrdered(array $arrayDataIn): array
78
    {
79
        $arrayOutput = [];
80
        $arrayCBC    = explode(':', $arrayDataIn['namespace_cbc']);
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 [$arrayCBC[(count($arrayCBC) - 1)] => $arrayOutput];
87
    }
88
89
    private function getHeader(array $arrayParams): array
90
    {
91
        $arrayDocument                      = $this->getElementsOrdered([
92
            'data'          => $arrayParams['CBC'],
93
            'namespace_cbc' => $arrayParams['DocumentNameSpaces']['cbc'],
94
        ]);
95
        $strCAC                             = $arrayParams['cacName']; // CommonAggregateComponents
96
        $arrayDocument[$strCAC]['TaxTotal'] = $this->getTax($arrayParams['CAC']->TaxTotal);
97
        // optional components =========================================================================================
98
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
99
            if (isset($arrayParams['CAC']->$key)) {
100
                switch ($value) {
101
                    case 'Multiple':
102
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElementsByKey($arrayParams['data'], $key);
103
                        break;
104
                    case 'MultipleStandard':
105
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElementsStandard($arrayParams['CAC']->$key);
106
                        break;
107
                    case 'Single':
108
                        $arrayDocument[$strCAC][$key] = $this->getElements($arrayParams['CAC']->$key);
109
                        break;
110
                    case 'SingleCompany':
111
                        $arrayDocument[$strCAC][$key] = [
112
                            'Party' => $this->getAccountingCustomerOrSupplierParty([
113
                                'data' => $arrayParams['CAC']->$key->children('cac', true)->Party,
114
                                'type' => $key,
115
                            ])
116
                        ];
117
                        break;
118
                    case 'SingleCompanyWithoutParty':
119
                        $arrayDocument[$strCAC][$key] = $this->getAccountingCustomerOrSupplierParty([
120
                            'data' => $arrayParams['CAC']->$key,
121
                            'type' => $key,
122
                        ]);
123
                        break;
124
                }
125
            }
126
        }
127
        return $arrayDocument;
128
    }
129
130
    public function readElectronicInvoice(string $strFile): array
131
    {
132
        $this->getProcessingDetails();
133
        $this->getHierarchyTagOrder();
134
        $objFile                 = new \SimpleXMLElement($strFile, null, true);
135
        $arrayDocument           = $this->getDocumentRoot($objFile);
136
        $arrayCAC                = explode(':', $arrayDocument['DocumentNameSpaces']['cac']);
137
        $strElementA             = $arrayCAC[count($arrayCAC) - 1]; // CommonAggregateComponents
138
        $arrayDocument['Header'] = $this->getHeader([
139
            'CAC'                => $objFile->children('cac', true),
140
            'cacName'            => $strElementA,
141
            'CBC'                => $objFile->children('cbc', true),
142
            'data'               => $objFile,
143
            'DocumentNameSpaces' => $arrayDocument['DocumentNameSpaces'],
144
            'DocumentTagName'    => $arrayDocument['DocumentTagName'],
145
        ]);
146
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
147
        return $arrayDocument;
148
    }
149
}
150