Test Failed
Push — main ( 3a3fc1...d13b9f )
by Daniel
02:16
created

getAccountingCustomerOrSupplierParty()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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