Test Failed
Push — main ( 0a6cd6...1eb369 )
by Daniel
02:10
created

ElectornicInvoiceRead::getMultipleElements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
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 TraitCompanies,
0 ignored issues
show
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, $Note, $OrderLineReference, $AccountingCost, $TaxCategory, $ChargeIndicator, $MultiplierFactorNumeric, $DocumentReference, $AllowanceChargeReasonCode, $TaxableAmount, $Price, $Value, $Amount, $AllowanceChargeReason, $TaxSubtotal, $TaxAmount, $Item, $InvoicedQuantity, $BaseAmount, $TaxScheme, $LineID, $LineExtensionAmount, $Percent
Loading history...
introduced by
The trait danielgp\efactura\TraitCompanies requires some properties which are not provided by danielgp\efactura\ElectornicInvoiceRead: $ID, $AdditionalStreetName, $EndpointID, $PartyLegalEntity, $PostalAddress, $StreetName, $Name, $Country, $CityName, $Contact, $RegistrationName, $PayeeFinancialAccount, $PartyIdentification, $CompanyLegalForm, $FinancialInstitutionBranch, $ElectronicMail, $IdentificationCode, $CompanyID, $PaymentID, $Telephone, $CountrySubentity, $schemeID, $PartyTaxScheme, $TaxScheme, $PostalZone, $PaymentMeansCode, $PartyName
Loading history...
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...
35
        TraitTax,
36
        TraitLines;
37
38
    private function getDocumentRoot(object $objFile): array
39
    {
40
        $arrayDocument = [
41
            'DocumentTagName'    => $objFile->getName(),
42
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
43
        ];
44
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
45
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
46
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
47
            }
48
        }
49
        return $arrayDocument;
50
    }
51
52
    private function getHeader(array $arrayParams): array
53
    {
54
        $arrayCBC      = explode(':', $arrayParams['DocumentNameSpaces']['cbc']);
55
        $strCBC        = $arrayCBC[count($arrayCBC) - 1]; // CommonBasicComponents
56
        $strCAC        = $arrayParams['cacName']; // CommonAggregateComponents
57
        $arrayDocument = [
58
            $strCBC => $this->getHeaderCommonBasicComponents($arrayParams['DocumentTagName'], $arrayParams['CBC']),
59
            $strCAC => [
60
                'AccountingCustomerParty' => $this->getAccountingCustomerParty($arrayParams['CAC']
61
                    ->AccountingCustomerParty->children('cac', true)->Party),
62
                'AccountingSupplierParty' => $this->getAccountingSupplierParty($arrayParams['CAC']
63
                    ->AccountingSupplierParty->children('cac', true)->Party),
64
                'TaxTotal'                => $this->getTaxTotal($arrayParams['CAC']->TaxTotal),
65
            ],
66
        ];
67
        // optional components =========================================================================================
68
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
69
            if (isset($arrayParams['CAC']->$key)) {
70
                switch ($value) {
71
                    case 'Single':
72
                        $arrayDocument[$strCAC][$key] = $this->getElements($arrayParams['CAC']->$key);
73
                        break;
74
                    case 'Multiple':
75
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElements($arrayParams['CAC']->$key);
76
                        break;
77
                    case 'MultipleStandard':
78
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElementsStandard($arrayParams['CAC']->$key);
79
                        break;
80
                }
81
            }
82
        }
83
        if (isset($arrayParams['CAC']->Delivery)) {
84
            $strEl                                                             = $arrayParams['CAC']->Delivery;
85
            $strElement                                                        = $strEl->children('cac', true)
86
                ->DeliveryLocation->children('cac', true)->Address;
87
            $arrayDocument[$strCAC]['Delivery']['DeliveryLocation']['Address'] = [
88
                'StreetName' => $strElement->children('cbc', true)->StreetName->__toString(),
89
                'CityName'   => $strElement->children('cbc', true)->CityName->__toString(),
90
                'PostalZone' => $strElement->children('cbc', true)->PostalZone->__toString(),
91
                'Country'    => [
92
                    'IdentificationCode' => $strElement->children('cac', true)->Country->children('cbc', true)->IdentificationCode->__toString(),
93
                ],
94
            ];
95
            if (isset($strEl->children('cbc', true)->ActualDeliveryDate)) {
96
                $arrayDocument[$strCAC]['Delivery']['ActualDeliveryDate'] = $strEl
97
                        ->children('cbc', true)->ActualDeliveryDate->__toString();
98
            }
99
        }
100
        return $arrayDocument;
101
    }
102
103
    private function getHeaderCommonBasicComponents(string $strType, $objCommonBasicComponents): array
104
    {
105
        $arrayOutput = [];
106
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
107
            if (isset($objCommonBasicComponents->$value)) {
108
                $arrayOutput[$value] = $objCommonBasicComponents->$value->__toString();
109
            }
110
        }
111
        return array_merge($arrayOutput, $this->getHeaderTypeCode($strType, $objCommonBasicComponents));
112
    }
113
114
    private function getHeaderTypeCode(string $strType, $objCommonBasicComponents): array
115
    {
116
        $arrayOutput = [];
117
        switch ($strType) {
118
            case 'CreditNote':
119
                $arrayOutput['CreditNoteTypeCode'] = (integer) $objCommonBasicComponents
120
                    ->CreditNoteTypeCode->__toString();
121
                break;
122
            case 'Invoice':
123
                $arrayOutput['InvoiceTypeCode']    = (integer) $objCommonBasicComponents
124
                    ->InvoiceTypeCode->__toString();
125
                break;
126
        }
127
        return $arrayOutput;
128
    }
129
130
    private function getMultipleElements(array|\SimpleXMLElement $arrayIn): array
131
    {
132
        $arrayToReturn = [];
133
        $intLineNo     = 0;
134
        foreach ($arrayIn as $child) {
135
            $intLineNo++;
136
            $intLineStr                 = ($intLineNo < 10 ? '0' : '') . $intLineNo;
137
            $arrayToReturn[$intLineStr] = $this->getPaymentMeans($child);
138
        }
139
        return $arrayToReturn;
140
    }
141
142
    public function readElectronicInvoice(string $strFile): array
143
    {
144
        $this->getHierarchyTagOrder();
145
        $objFile                 = new \SimpleXMLElement($strFile, NULL, TRUE);
146
        $arrayDocument           = $this->getDocumentRoot($objFile);
147
        $arrayCAC                = explode(':', $arrayDocument['DocumentNameSpaces']['cac']);
148
        $strElementA             = $arrayCAC[count($arrayCAC) - 1]; // CommonAggregateComponents
149
        $arrayDocument['Header'] = $this->getHeader([
150
            'CAC'                => $objFile->children('cac', true),
151
            'cacName'            => $strElementA,
152
            'CBC'                => $objFile->children('cbc', true),
153
            'DocumentNameSpaces' => $arrayDocument['DocumentNameSpaces'],
154
            'DocumentTagName'    => $arrayDocument['DocumentTagName'],
155
        ]);
156
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
157
        return $arrayDocument;
158
    }
159
}
160