Passed
Branch main (a349e7)
by Daniel
13:11
created

getAccountingCustomerOrSupplierParty()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.027

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 10
nop 1
dl 0
loc 30
ccs 20
cts 22
cp 0.9091
crap 6.027
rs 8.9457
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (c) 2024, Daniel Popiniuc and its licensors.
5
 *
6
 * All rights reserved. This program and the accompanying materials
7
 * are made available under the terms of the Eclipse Public License v1.0
8
 * which accompanies this distribution, and is available at
9
 * http://www.eclipse.org/legal/epl-v20.html
10
 *
11
 * Contributors:
12
 *    Daniel Popiniuc
13
 */
14
15
namespace danielgp\efactura;
16
17
class ClassElectronicInvoiceRead
18
{
19
20
    use TraitBasic;
21
    use TraitTax;
22
    use TraitLines;
23
24 13
    private function getAccountingCustomerOrSupplierParty(array $arrayIn): array
25
    {
26 13
        $arrayOut = [];
27 13
        foreach ($this->arraySettings['CustomOrder'][$arrayIn['type']] as $strElement) {
28 13
            if (isset($arrayIn['data']->children($this->arrayProcessing['mapping']['cac'], true)->$strElement)) {
29 13
                if ($strElement === 'PartyTaxScheme') {
30 12
                    $arrayOut[$strElement] = $this->getMultipleElementsByKey($arrayIn['data']
31 12
                            ->children($this->arrayProcessing['mapping']['cac'], true)->$strElement);
32
                } else {
33 13
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']
34 13
                            ->children($this->arrayProcessing['mapping']['cac'], true)->$strElement);
35
                }
36
            }
37 13
            if (isset($arrayIn['data']->children($this->arrayProcessing['mapping']['cbc'], true)->$strElement)) {
38 3
                if ($strElement === 'EndpointID') {
39 3
                    $arrayOut['EndpointID'] = [
40 3
                        'schemeID' => $arrayIn['data']
41 3
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->EndpointID
42 3
                            ->attributes()->schemeID->__toString(),
43 3
                        'value'    => $arrayIn['data']
44 3
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->EndpointID
45 3
                            ->__toString(),
46 3
                    ];
47
                } else {
48
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']
49
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->$strElement);
50
                }
51
            }
52
        }
53 13
        return $arrayOut;
54
    }
55
56 13
    private function getBasicOrAggregateKey(array $arrayDocNmSp, string $strBasOrAggr): string
57
    {
58 13
        $arrayPieces = explode(':', $arrayDocNmSp[$strBasOrAggr]);
59 13
        return $arrayPieces[count($arrayPieces) - 1];
60
    }
61
62 13
    private function getDocumentRoot(object $objFile): array
63
    {
64 13
        $arrayDocument = [
65 13
            'DocumentTagName'    => $objFile->getName(),
66 13
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
67 13
        ];
68 13
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
69 11
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
70 10
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
71
            }
72
        }
73 13
        foreach ($arrayDocument['DocumentNameSpaces'] as $key => $value) {
74 13
            if (str_ends_with($value, ':CommonBasicComponents-2')) {
75 13
                $this->arrayProcessing['mapping']['cbc'] = $key;
76
            }
77 13
            if (str_ends_with($value, ':CommonAggregateComponents-2')) {
78 13
                $this->arrayProcessing['mapping']['cac'] = $key;
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
115 13
                        ->children($this->arrayProcessing['mapping']['cac'], true)->Party,
116 13
                    'type' => $key,
117 13
                ])
118 13
            ];
119
        } else {
120 13
            $arrayMapping  = [
121 13
                'Multiple'                  => [
122 13
                    'getMultipleElementsByKey'
123 13
                    , $arrayParams['data']->children('cac', true)->$key
124 13
                ],
125 13
                'MultipleStandard'          => ['getMultipleElementsStandard', $arrayParams['CAC']->$key],
126 13
                'Single'                    => ['getElements', $arrayParams['CAC']->$key],
127 13
                'SingleCompanyWithoutParty' => [
128 13
                    'getAccountingCustomerOrSupplierParty'
129 13
                    , [
130 13
                        'data' => $arrayParams['CAC']->$key,
131 13
                        'type' => $key,
132 13
                    ]
133 13
                ],
134 13
            ];
135 13
            $arrayDocument = $this->getRightMethod($arrayMapping[$value][0], $arrayMapping[$value][1]);
136
        }
137 13
        return $arrayDocument;
138
    }
139
140 13
    public function readElectronicInvoice(string $strFile): array
141
    {
142 13
        $this->getProcessingDetails();
143 13
        $this->getHierarchyTagOrder();
144 13
        $flags                   = LIBXML_PARSEHUGE | LIBXML_BIGLINES | LIBXML_NOERROR;
145 13
        $bolIsLocal              = is_file($strFile);
146 13
        $objFile                 = new \SimpleXMLElement($strFile, $flags, $bolIsLocal);
147 13
        $arrayDocument           = $this->getDocumentRoot($objFile);
148 13
        $strMap                  = $this->arrayProcessing['mapping'];
149 13
        $arrayBasics             = $this->getElementsOrdered([
150 13
            'data'          => $objFile->children($strMap['cbc'], true),
151 13
            'namespace_cbc' => $arrayDocument['DocumentNameSpaces'][$strMap['cbc']],
152 13
        ]);
153 13
        $arrayAggregates         = $this->getHeader([
154 13
            'CAC'  => $objFile->children($strMap['cac'], true),
155 13
            'data' => $objFile,
156 13
        ]);
157 13
        $arrayDocument['Header'] = [
158 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], $strMap['cbc']) => $arrayBasics,
159 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], $strMap['cac']) => $arrayAggregates,
160 13
        ];
161 13
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
162 13
        return $arrayDocument;
163
    }
164
}
165