Passed
Push — main ( 996ebb...3a3fc1 )
by Daniel
12:26
created

ElectornicInvoiceRead   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 74
dl 0
loc 111
rs 10
c 1
b 0
f 0
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A readElectronicInvoice() 0 16 1
A getMultiplePaymentMeansElements() 0 10 3
B getHeader() 0 48 8
A getDocumentRoot() 0 12 3
A getElementsOrdered() 0 9 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 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\TraitCompanies requires some properties which are not provided by danielgp\efactura\ElectornicInvoiceRead: $ID, $EndpointID, $PartyLegalEntity, $currencyID, $PostalAddress, $Name, $unitCode, $Contact, $RegistrationName, $PartyIdentification, $CompanyLegalForm, $ElectronicMail, $CompanyID, $Telephone, $schemeID, $PartyTaxScheme, $TaxScheme, $PartyName
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
        TraitCompanies,
36
        TraitTax,
37
        TraitLines;
38
39
    private function getDocumentRoot(object $objFile): array
40
    {
41
        $arrayDocument = [
42
            'DocumentTagName'    => $objFile->getName(),
43
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
44
        ];
45
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
46
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
47
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
48
            }
49
        }
50
        return $arrayDocument;
51
    }
52
53
    private function getElementsOrdered(string $strKeyOrderedElements, \SimpleXMLElement $arrayDataIn): array
54
    {
55
        $arrayOutput = [];
56
        foreach ($this->arraySettings['CustomOrder'][$strKeyOrderedElements] as $value) {
57
            if (isset($arrayDataIn->$value)) {
58
                $arrayOutput[$value] = $arrayDataIn->$value->__toString();
59
            }
60
        }
61
        return $arrayOutput;
62
    }
63
64
    private function getHeader(array $arrayParams): array
65
    {
66
        $arrayCBC               = explode(':', $arrayParams['DocumentNameSpaces']['cbc']);
67
        $strCBC                 = $arrayCBC[(count($arrayCBC) - 1)]; // CommonBasicComponents
68
        $arrayDocument[$strCBC] = $this->getElementsOrdered('Header_CBC', $arrayParams['CBC']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$arrayDocument was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arrayDocument = array(); before regardless.
Loading history...
69
        $strCAC                 = $arrayParams['cacName']; // CommonAggregateComponents
70
        $arrayDocument[$strCAC] = [
71
            'AccountingCustomerParty' => $this->getAccountingCustomerParty($arrayParams['CAC']
72
                ->AccountingCustomerParty->children('cac', true)->Party),
73
            'AccountingSupplierParty' => $this->getAccountingSupplierParty($arrayParams['CAC']
74
                ->AccountingSupplierParty->children('cac', true)->Party),
75
            'TaxTotal'                => $this->getTaxTotal($arrayParams['CAC']->TaxTotal),
76
        ];
77
        // optional components =========================================================================================
78
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
79
            if (isset($arrayParams['CAC']->$key)) {
80
                switch ($value) {
81
                    case 'Single':
82
                        $arrayDocument[$strCAC][$key] = $this->getElements($arrayParams['CAC']->$key);
83
                        break;
84
                    case 'Multiple':
85
                        $arrayDocument[$strCAC][$key] = $this->getMultiplePaymentMeansElements($arrayParams['CAC']->$key);
86
                        break;
87
                    case 'MultipleStandard':
88
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElementsStandard($arrayParams['CAC']->$key);
89
                        break;
90
                }
91
            }
92
        }
93
        if (isset($arrayParams['CAC']->Delivery)) {
94
            $strEl                                                             = $arrayParams['CAC']->Delivery;
95
            $strElement                                                        = $strEl->children('cac', true)
96
                ->DeliveryLocation->children('cac', true)->Address;
97
            $arrayDocument[$strCAC]['Delivery']['DeliveryLocation']['Address'] = [
98
                'StreetName' => $strElement->children('cbc', true)->StreetName->__toString(),
99
                'CityName'   => $strElement->children('cbc', true)->CityName->__toString(),
100
                'PostalZone' => $strElement->children('cbc', true)->PostalZone->__toString(),
101
                'Country'    => [
102
                    'IdentificationCode' => $strElement->children('cac', true)->Country->children('cbc', true)
103
                    ->IdentificationCode->__toString(),
104
                ],
105
            ];
106
            if (isset($strEl->children('cbc', true)->ActualDeliveryDate)) {
107
                $arrayDocument[$strCAC]['Delivery']['ActualDeliveryDate'] = $strEl
108
                        ->children('cbc', true)->ActualDeliveryDate->__toString();
109
            }
110
        }
111
        return $arrayDocument;
112
    }
113
114
    private function getMultiplePaymentMeansElements(array|\SimpleXMLElement $arrayIn): array
115
    {
116
        $arrayToReturn = [];
117
        $intLineNo     = 0;
118
        foreach ($arrayIn as $child) {
119
            $intLineNo++;
120
            $intLineStr                 = ($intLineNo < 10 ? '0' : '') . $intLineNo;
121
            $arrayToReturn[$intLineStr] = $this->getElements($child);
122
        }
123
        return $arrayToReturn;
124
    }
125
126
    public function readElectronicInvoice(string $strFile): array
127
    {
128
        $this->getHierarchyTagOrder();
129
        $objFile                 = new \SimpleXMLElement($strFile, NULL, TRUE);
130
        $arrayDocument           = $this->getDocumentRoot($objFile);
131
        $arrayCAC                = explode(':', $arrayDocument['DocumentNameSpaces']['cac']);
132
        $strElementA             = $arrayCAC[count($arrayCAC) - 1]; // CommonAggregateComponents
133
        $arrayDocument['Header'] = $this->getHeader([
134
            'CAC'                => $objFile->children('cac', true),
135
            'cacName'            => $strElementA,
136
            'CBC'                => $objFile->children('cbc', true),
137
            'DocumentNameSpaces' => $arrayDocument['DocumentNameSpaces'],
138
            'DocumentTagName'    => $arrayDocument['DocumentTagName'],
139
        ]);
140
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
141
        return $arrayDocument;
142
    }
143
}
144