Passed
Push — main ( fcd80e...779a2b )
by Daniel
02:06
created

TraitHeader::getHeaderTypeCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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