Passed
Push — main ( 10585a...87ddea )
by Daniel
02:30
created

ElectronicInvoiceRead   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 80
c 2
b 0
f 0
dl 0
loc 127
rs 10
wmc 22

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccountingCustomerOrSupplierParty() 0 24 6
A getDocumentRoot() 0 12 3
B getHeaderComponents() 0 29 6
A readElectronicInvoice() 0 25 1
A getElementsOrdered() 0 9 3
A getHeader() 0 12 3
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
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
81
            if (isset($arrayDataIn['data']->$value)) {
82
                $arrayOutput[$value] = $arrayDataIn['data']->$value->__toString();
83
            }
84
        }
85
        return $arrayOutput;
86
    }
87
88
    private function getHeader(array $arrayParams): array
89
    {
90
        $arrayDocument = [
91
            'TaxTotal' => $this->getTax($arrayParams['CAC']->TaxTotal),
92
        ];
93
        // optional components =========================================================================================
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
        switch ($value) {
106
            case 'Multiple':
107
                $arrayDocument = $this->getMultipleElementsByKey($arrayParams['data'], $key);
108
                break;
109
            case 'MultipleStandard':
110
                $arrayDocument = $this->getMultipleElementsStandard($arrayParams['CAC']->$key);
111
                break;
112
            case 'Single':
113
                $arrayDocument = $this->getElements($arrayParams['CAC']->$key);
114
                break;
115
            case 'SingleCompany':
116
                $arrayDocument = [
117
                    'Party' => $this->getAccountingCustomerOrSupplierParty([
118
                        'data' => $arrayParams['CAC']->$key->children('cac', true)->Party,
119
                        'type' => $key,
120
                    ])
121
                ];
122
                break;
123
            case 'SingleCompanyWithoutParty':
124
                $arrayDocument = $this->getAccountingCustomerOrSupplierParty([
125
                    'data' => $arrayParams['CAC']->$key,
126
                    'type' => $key,
127
                ]);
128
                break;
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
            'DocumentNameSpaces' => $arrayDocument['DocumentNameSpaces'],
150
            'DocumentTagName'    => $arrayDocument['DocumentTagName'],
151
        ]);
152
        $arrayDocument['Header']        = [
153
            $arrayCBC[count($arrayCBC) - 1] => $arrayCommonBasicComponents,
154
            $arrayCAC[count($arrayCAC) - 1] => $arrayCommonAggregateComponents,
155
        ];
156
        $arrayDocument['Lines']         = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
157
        return $arrayDocument;
158
    }
159
}
160