Passed
Push — main ( cb235a...69c3d0 )
by Daniel
02:03
created

traitLines::getLinePrice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
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
trait traitLines
32
{
33
34
    use traitTax;
35
36
    private function getDocumentLines($objFile, string $strTag) {
37
        $arrayLines = [];
38
        $intLineNo  = 0;
39
        foreach ($objFile->children('cac', true) as $strNodeName => $child) {
40
            if ($strNodeName === ($strTag . 'Line')) {
41
                $intLineNo++;
42
                $intLineStr              = ($intLineNo < 10 ? '0' : '') . $intLineNo;
43
                $arrayLines[$intLineStr] = $this->getLine($strTag, $child);
44
            }
45
        }
46
        return $arrayLines;
47
    }
48
49
    private function getLine(string $strType, $child): array {
50
        $arrayOutput = [
51
            'ID'                  => $child->children('cbc', true)->ID->__toString(),
52
            'LineExtensionAmount' => $this->getTagWithCurrencyParameter($child->children('cbc', true)
53
                ->LineExtensionAmount),
54
        ];
55
        // optional components =========================================================================================
56
        if (isset($child->children('cbc', true)->Note)) {
57
            $arrayOutput['Note'] = $child->children('cbc', true)->Note->__toString();
58
        }
59
        switch ($strType) {
60
            case 'CreditNote':
61
                $arrayOutput['CreditedQuantity'] = $this->getTagWithUnitCodeParameter($child->children('cbc', true)
62
                    ->CreditedQuantity);
63
                break;
64
            case 'Invoice':
65
                $arrayOutput['InvoicedQuantity'] = $this->getTagWithUnitCodeParameter($child->children('cbc', true)
66
                    ->InvoicedQuantity);
67
                break;
68
        }
69
        if (isset($child->children('cac', true)->AllowanceCharge)) {
70
            $arrayOutput['AllowanceCharge'] = $this->getLineItemAllowanceCharge($child
71
                    ->children('cac', true)->AllowanceCharge);
72
        }
73
        $arrayOutput['Item']  = $this->getLineItem($child->children('cac', true)->Item);
74
        $arrayOutput['Price'] = $this->getLinePrice($child->children('cac', true)->Price);
75
        return $arrayOutput;
76
    }
77
78
    private function getLineItem($child3): array {
79
        $arrayOutput = [
80
            'Name'                  => $child3->children('cbc', true)->Name->__toString(),
81
            'ClassifiedTaxCategory' => $this->getTaxCategory($child3->children('cac', true)->ClassifiedTaxCategory),
82
        ];
83
        // optional components =========================================================================================
84
        if (isset($child3->children('cbc', true)->Description)) {
85
            $arrayOutput['Description'] = $child3->children('cac', true)->Description->__toString();
86
        }
87
        $arrayAggregate = $this->getLineItemAggregate($child3);
88
        return array_merge($arrayOutput, $arrayAggregate);
89
    }
90
91
    private function getLineItemAllowanceCharge($child3): array {
92
        $arrayOutput = [];
93
        $intLineNo   = 0;
94
        foreach ($child3 as $child4) {
95
            $intLineNo++;
96
            $intLineStr               = ($intLineNo < 10 ? '0' : '') . $intLineNo;
97
            $arrayOutput[$intLineStr] = [
98
                'Amount'                    => $this->getTagWithCurrencyParameter($child4
99
                        ->children('cbc', true)->Amount),
100
                'AllowanceChargeReasonCode' => $child4->children('cbc', true)->AllowanceChargeReasonCode->__toString(),
101
                'ChargeIndicator'           => $child4->children('cbc', true)->ChargeIndicator->__toString(),
102
            ];
103
            if (isset($child4->children('cbc', true)->AllowanceChargeReason)) {
104
                $arrayOutput[$intLineStr]['AllowanceChargeReason'] = $this->getTagWithCurrencyParameter($child4
105
                        ->children('cbc', true)->AllowanceChargeReason);
106
            }
107
            if (isset($child4->children('cbc', true)->BaseAmount)) {
108
                $arrayOutput[$intLineStr]['BaseAmount'] = $this->getTagWithCurrencyParameter($child4
109
                        ->children('cbc', true)->BaseAmount);
110
            }
111
        }
112
        return $arrayOutput;
113
    }
114
115
    private function getLineItemAggregate($child3): array {
116
        $arrayOutput = [];
117
        foreach ($child3->children('cac', true) as $strName => $value) {
118
            switch ($strName) {
119
                case 'CommodityClassification':
120
                    $child4                                          = $value->children('cbc', true)
121
                        ->ItemClassificationCode;
122
                    $arrayOutput[$strName]['ItemClassificationCode'] = [
123
                        'listID' => $child4->attributes()->listID->__toString(),
124
                        'value'  => $child4->__toString(),
125
                    ];
126
                    break;
127
                case 'SellersItemIdentification':
128
                    $arrayOutput[$strName]['ID']                     = $value->children('cbc', true)->ID->__toString();
129
                    break;
130
            }
131
        }
132
        return $arrayOutput;
133
    }
134
135
    private function getLinePrice($child2): array {
136
        $arrayOutput = [
137
            'PriceAmount' => $this->getTagWithCurrencyParameter($child2->children('cbc', true)->PriceAmount),
138
        ];
139
        // optional components =========================================================================================
140
        if (isset($child2->children('cbc', true)->BaseQuantity)) {
141
            $arrayOutput['BaseQuantity'] = $this->getTagWithUnitCodeParameter($child2->children('cbc', true)
142
                ->BaseQuantity);
143
        }
144
        return $arrayOutput;
145
    }
146
}
147