Passed
Push — main ( a8d720...b18f8c )
by Daniel
02:10
created

TraitHeader::getHeaderCommonBasicComponents()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
c 0
b 0
f 0
nc 16
nop 2
dl 0
loc 20
rs 9.4888
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
trait TraitHeader
32
{
33
34
    use TraitCompanies,
35
        TraitTax;
36
37
    private function getHeader(array $arrayParams): array {
38
        $arrayCBC      = explode(':', $arrayParams['DocumentNameSpaces']['cbc']);
39
        $strCBC        = $arrayCBC[count($arrayCBC) - 1]; // CommonBasicComponents
40
        $strCAC        = $arrayParams['cacName']; // CommonAggregateComponents
41
        $arrayDocument = [
42
            $strCBC => $this->getHeaderCommonBasicComponents($arrayParams['DocumentTagName'], $arrayParams['CBC']),
43
            $strCAC => [
44
                'AccountingCustomerParty' => $this->getAccountingCustomerParty($arrayParams['CAC']
45
                    ->AccountingCustomerParty->children('cac', true)->Party),
46
                'AccountingSupplierParty' => $this->getAccountingSupplierParty($arrayParams['CAC']
47
                    ->AccountingSupplierParty->children('cac', true)->Party),
48
                'LegalMonetaryTotal'      => $this->getLegalMonetaryTotal($arrayParams['CAC']->LegalMonetaryTotal),
49
                'TaxTotal'                => $this->getTaxTotal($arrayParams['CAC']->TaxTotal),
50
            ],
51
        ];
52
        $intLineNo     = 0;
53
        if (isset($arrayParams['CAC']->PaymentMeans)) {
54
            foreach ($arrayParams['CAC']->PaymentMeans as $child) {
55
                $intLineNo++;
56
                $intLineStr                                          = ($intLineNo < 10 ? '0' : '')
57
                    . $intLineNo;
58
                $arrayDocument[$strCAC]['PaymentMeans'][$intLineStr] = $this->getPaymentMeans($child);
59
            }
60
        }
61
        // optional components =========================================================================================
62
        if (isset($arrayParams['CAC']->InvoicePeriod)) {
63
            $arrayDocument[$strCAC]['InvoicePeriod'] = $this->getLegalInvoicePeriod($arrayParams['CAC']
64
                ->InvoicePeriod);
65
        }
66
        return $arrayDocument;
67
    }
68
69
    private function getHeaderCommonBasicComponents(string $strType, $objCommonBasicComponents): array {
70
        $arrayOutput = [
71
            'CustomizationID'      => $objCommonBasicComponents->CustomizationID->__toString(),
72
            'DocumentCurrencyCode' => $objCommonBasicComponents->DocumentCurrencyCode->__toString(),
73
            'ID'                   => $objCommonBasicComponents->ID->__toString(),
74
            'IssueDate'            => $objCommonBasicComponents->IssueDate->__toString(),
75
        ];
76
        if (isset($objCommonBasicComponents->DueDate)) {
77
            $arrayOutput['DueDate'] = $objCommonBasicComponents->DueDate->__toString();
78
        }
79
        if (isset($objCommonBasicComponents->Note)) {
80
            $arrayOutput['Note'] = $objCommonBasicComponents->Note->__toString();
81
        }
82
        if (isset($objCommonBasicComponents->TaxPointDate)) {
83
            $arrayOutput['TaxPointDate'] = $objCommonBasicComponents->TaxPointDate->__toString();
84
        }
85
        if (isset($objCommonBasicComponents->UBLVersionID)) {
86
            $arrayOutput['UBLVersionID'] = $objCommonBasicComponents->UBLVersionID->__toString();
87
        }
88
        return array_merge($arrayOutput, $this->getHeaderTypeCode($strType, $objCommonBasicComponents));
89
    }
90
91
    private function getHeaderTypeCode(string $strType, $objCommonBasicComponents) {
92
        $arrayOutput = [];
93
        switch ($strType) {
94
            case 'CreditNote':
95
                $arrayOutput['CreditNoteTypeCode'] = (integer) $objCommonBasicComponents
96
                    ->CreditNoteTypeCode->__toString();
97
                break;
98
            case 'Invoice':
99
                $arrayOutput['InvoiceTypeCode']    = (integer) $objCommonBasicComponents
100
                    ->InvoiceTypeCode->__toString();
101
                break;
102
        }
103
        return $arrayOutput;
104
    }
105
106
    private function getLegalInvoicePeriod($child2): array {
107
        $arrayOutput = [];
108
        if (isset($child2->children('cbc', true)->StartDate)) {
109
            $arrayOutput['StartDate'] = $child2->children('cbc', true)->StartDate->__toString();
110
        }
111
        if (isset($child2->children('cbc', true)->EndDate)) {
112
            $arrayOutput['EndDate'] = $child2->children('cbc', true)->EndDate->__toString();
113
        }
114
        return $arrayOutput;
115
    }
116
117
    private function getLegalMonetaryTotal($child2): array {
118
        $objCBC      = $child2->children('cbc', true);
119
        $arrayOutput = [
120
            'LineExtensionAmount' => $this->getTagWithCurrencyParameter($objCBC->LineExtensionAmount),
121
            'TaxExclusiveAmount'  => $this->getTagWithCurrencyParameter($objCBC->TaxExclusiveAmount),
122
            'TaxInclusiveAmount'  => $this->getTagWithCurrencyParameter($objCBC->TaxInclusiveAmount),
123
            'PayableAmount'       => $this->getTagWithCurrencyParameter($objCBC->PayableAmount),
124
        ];
125
        // optional components =========================================================================================
126
        if (isset($child2->children('cbc', true)->AllowanceTotalAmount)) {
127
            $arrayOutput['AllowanceTotalAmount'] = $this->getTagWithCurrencyParameter($objCBC->AllowanceTotalAmount);
128
        }
129
        if (isset($child2->children('cbc', true)->ChargeTotalAmount)) {
130
            $arrayOutput['ChargeTotalAmount'] = $this->getTagWithCurrencyParameter($objCBC->ChargeTotalAmount);
131
        }
132
        if (isset($child2->children('cbc', true)->PrepaidAmount)) {
133
            $arrayOutput['PrepaidAmount'] = $this->getTagWithCurrencyParameter($objCBC->PrepaidAmount);
134
        }
135
        return $arrayOutput;
136
    }
137
}
138