Test Failed
Push — main ( 2ad17d...7bbdc7 )
by Daniel
02:34
created

getAccountingCustomerOrSupplierParty()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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