Test Failed
Push — main ( 41eab8...0a6cd6 )
by Daniel
02:07
created

ElectornicInvoiceRead::getHeader()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 50
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 38
c 0
b 0
f 0
nc 18
nop 1
dl 0
loc 50
rs 8.0675
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
        $arrayOutput   = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $arrayOutput is dead and can be removed.
Loading history...
69
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
70
            if (isset($arrayParams['CAC']->$key)) {
71
                switch ($value) {
72
                    case 'Single':
73
                        $arrayDocument[$strCAC][$key] = $this->getElements($arrayParams['CAC']->$key);
74
                        break;
75
                    case 'Multiple':
76
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElements($arrayParams['CAC']->$key);
77
                        break;
78
                    case 'MultipleStandard':
79
                        $arrayDocument[$strCAC][$key] = $this->getMultipleElementsStandard($arrayParams['CAC']->$key);
80
                        break;
81
                }
82
            }
83
        }
84
        if (isset($arrayParams['CAC']->Delivery)) {
85
            $strEl                                                             = $arrayParams['CAC']->Delivery;
86
            $strElement                                                        = $strEl->children('cac', true)
87
                ->DeliveryLocation->children('cac', true)->Address;
88
            $arrayDocument[$strCAC]['Delivery']['DeliveryLocation']['Address'] = [
89
                'StreetName' => $strElement->children('cbc', true)->StreetName->__toString(),
90
                'CityName'   => $strElement->children('cbc', true)->CityName->__toString(),
91
                'PostalZone' => $strElement->children('cbc', true)->PostalZone->__toString(),
92
                'Country'    => [
93
                    'IdentificationCode' => $strElement->children('cac', true)->Country->children('cbc', true)->IdentificationCode->__toString(),
94
                ],
95
            ];
96
            if (isset($strEl->children('cbc', true)->ActualDeliveryDate)) {
97
                $arrayDocument[$strCAC]['Delivery']['ActualDeliveryDate'] = $strEl
98
                        ->children('cbc', true)->ActualDeliveryDate->__toString();
99
            }
100
        }
101
        return $arrayDocument;
102
    }
103
104
    private function getHeaderCommonBasicComponents(string $strType, $objCommonBasicComponents): array
105
    {
106
        $arrayOutput = [];
107
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
108
            if (isset($objCommonBasicComponents->$value)) {
109
                $arrayOutput[$value] = $objCommonBasicComponents->$value->__toString();
110
            }
111
        }
112
        return array_merge($arrayOutput, $this->getHeaderTypeCode($strType, $objCommonBasicComponents));
113
    }
114
115
    private function getHeaderTypeCode(string $strType, $objCommonBasicComponents): array
116
    {
117
        $arrayOutput = [];
118
        switch ($strType) {
119
            case 'CreditNote':
120
                $arrayOutput['CreditNoteTypeCode'] = (integer) $objCommonBasicComponents
121
                    ->CreditNoteTypeCode->__toString();
122
                break;
123
            case 'Invoice':
124
                $arrayOutput['InvoiceTypeCode']    = (integer) $objCommonBasicComponents
125
                    ->InvoiceTypeCode->__toString();
126
                break;
127
        }
128
        return $arrayOutput;
129
    }
130
131
    public function readElectronicInvoice(string $strFile): array
132
    {
133
        $this->getHierarchyTagOrder();
134
        $objFile                 = new \SimpleXMLElement($strFile, NULL, TRUE);
135
        $arrayDocument           = $this->getDocumentRoot($objFile);
136
        $arrayCAC                = explode(':', $arrayDocument['DocumentNameSpaces']['cac']);
137
        $strElementA             = $arrayCAC[count($arrayCAC) - 1]; // CommonAggregateComponents
138
        $arrayDocument['Header'] = $this->getHeader([
139
            'CAC'                => $objFile->children('cac', true),
140
            'cacName'            => $strElementA,
141
            'CBC'                => $objFile->children('cbc', true),
142
            'DocumentNameSpaces' => $arrayDocument['DocumentNameSpaces'],
143
            'DocumentTagName'    => $arrayDocument['DocumentTagName'],
144
        ]);
145
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
146
        return $arrayDocument;
147
    }
148
}
149