Test Failed
Push — main ( a258cb...1dd856 )
by Daniel
02:47
created

ClassElectronicInvoiceWrite   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 122
dl 0
loc 199
rs 9.36
c 0
b 0
f 0
wmc 38

11 Methods

Rating   Name   Duplication   Size   Complexity  
A writeElectronicInvoice() 0 18 2
A setExtraElement() 0 10 4
A setElementsOrdered() 0 21 3
A setElementComment() 0 10 4
A setSingleComment() 0 7 3
A setProduceMiddleXml() 0 20 4
A setPrepareXml() 0 18 4
A setHeaderCommonBasicComponents() 0 7 3
A setSingleElementWithAttribute() 0 14 5
A setMultipleElementsOrdered() 0 8 2
A setDecisionElements() 0 24 4
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 ClassElectronicInvoiceWrite
32
{
33
    use TraitVersions;
34
35
    protected \XMLWriter $objXmlWriter;
36
37
    private function setDecisionElements(array $arrayInput, string $strKey, string $strTag, string $strCategory): void
38
    {
39
        switch ($strCategory) {
40
            case 'ElementsOrdered':
41
                $this->setElementsOrdered([
42
                    'commentParentKey' => $strKey,
43
                    'data'             => $arrayInput['data'][$strTag],
44
                    'tag'              => $strTag,
45
                ]);
46
                break;
47
            case 'MultipleElementsOrdered':
48
                $this->setMultipleElementsOrdered([
49
                    'commentParentKey' => $strKey,
50
                    'data'             => $arrayInput['data'][$strTag],
51
                    'tag'              => $strTag,
52
                ]);
53
                break;
54
            case 'SingleElementWithAttribute':
55
                $this->setSingleElementWithAttribute([
56
                    'commentParentKey' => $arrayInput['commentParentKey'],
57
                    'data'             => $arrayInput['data'][$strTag],
58
                    'tag'              => $strTag,
59
                ]);
60
                break;
61
        }
62
    }
63
64
    private function setElementComment(string $strKey): void
65
    {
66
        if (array_key_exists($strKey, $this->arraySettings['Comments'])) {
67
            $elementComment = $this->arraySettings['Comments'][$strKey];
68
            if (is_array($elementComment)) {
69
                foreach ($elementComment as $value) {
70
                    $this->objXmlWriter->writeComment($value);
71
                }
72
            } else {
73
                $this->objXmlWriter->writeComment($elementComment);
74
            }
75
        }
76
    }
77
78
    private function setElementsOrdered(array $arrayInput): void
79
    {
80
        $this->setElementComment($arrayInput['commentParentKey']);
81
        $this->objXmlWriter->startElement('cac:' . $arrayInput['tag']);
82
        $this->setExtraElement($arrayInput, 'Start');
83
        foreach ($this->arraySettings['CustomOrder'][$arrayInput['commentParentKey']] as $value) {
84
            if (array_key_exists($value, $arrayInput['data'])) { // because certain value are optional
85
                $key         = implode('_', [$arrayInput['commentParentKey'], $value]);
86
                $matches     = [];
87
                preg_match('/^.*(Amount|Quantity)$/', $value, $matches, PREG_OFFSET_CAPTURE);
88
                $strCategory = $this->setCategorizedVerifications([
89
                    'commentParentKey' => $arrayInput['commentParentKey'],
90
                    'data'             => $arrayInput['data'][$value],
91
                    'matches'          => $matches,
92
                    'tag'              => $value,
93
                ]);
94
                $this->setDecisionElements($arrayInput, $key, $value, $strCategory);
95
            }
96
        }
97
        $this->setExtraElement($arrayInput, 'End');
98
        $this->objXmlWriter->endElement(); // $arrayInput['tag']
99
    }
100
101
    private function setExtraElement(array $arrayInput, string $strType): void
102
    {
103
        if (in_array($arrayInput['tag'], ['AccountingCustomerParty', 'AccountingSupplierParty'])) {
104
            switch ($strType) {
105
                case 'End':
106
                    $this->objXmlWriter->endElement();
107
                    break;
108
                case 'Start':
109
                    $this->objXmlWriter->startElement('cac:Party');
110
                    break;
111
            }
112
        }
113
    }
114
115
    private function setHeaderCommonBasicComponents(array $arrayElementWithData): void
116
    {
117
        $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header_CBC'];
118
        foreach ($arrayCustomOrdered as $value) {
119
            if (array_key_exists($value, $arrayElementWithData)) {
120
                $this->setElementComment($value);
121
                $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]);
122
            }
123
        }
124
    }
125
126
    private function setMultipleElementsOrdered(array $arrayData): void
127
    {
128
        foreach ($arrayData['data'] as $value) {
129
            $strCommentParentKey = $this->setManageComment($arrayData['commentParentKey'], $value);
130
            $this->setElementsOrdered([
131
                'commentParentKey' => $strCommentParentKey,
132
                'data'             => $value,
133
                'tag'              => $arrayData['tag'],
134
            ]);
135
        }
136
    }
137
138
    private function setPrepareXml(string $strFile, array $arrayDocumentData, int $intIdent = 4): void
139
    {
140
        $this->objXmlWriter = new \XMLWriter();
141
        $this->objXmlWriter->openURI($strFile);
142
        $this->objXmlWriter->setIndent(true);
143
        $this->objXmlWriter->setIndentString(str_repeat(' ', $intIdent));
144
        $this->objXmlWriter->startDocument('1.0', 'UTF-8');
145
        $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']);
146
        foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) {
147
            if ($key === '') {
148
                $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']);
149
                $this->objXmlWriter->writeAttributeNS(null, 'xmlns', null, $strValue);
150
            } else {
151
                $this->objXmlWriter->writeAttributeNS('xmlns', $key, null, $value);
152
            }
153
        }
154
        if (array_key_exists('SchemaLocation', $arrayDocumentData)) {
155
            $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']);
156
        }
157
    }
158
159
    private function setProduceMiddleXml(array $arrayData): void
160
    {
161
        foreach ($this->arrayProcessing['OptionalElementsHeader'] as $key => $strLogicType) {
162
            if (array_key_exists($key, $arrayData)) {
163
                switch ($strLogicType) {
164
                    case 'SingleCompany':
165
                        $this->setElementsOrdered([
166
                            'commentParentKey' => $key,
167
                            'data'             => $arrayData[$key]['Party'],
168
                            'tag'              => $key,
169
                        ]);
170
                        break;
171
                    default:
172
                        $arrayInput = [
173
                            'commentParentKey' => $key,
174
                            'data'             => $arrayData,
175
                            'tag'              => $key
176
                        ];
177
                        $this->setDecisionElements($arrayInput, $key, $key, $strLogicType);
178
                        break;
179
                }
180
            }
181
        }
182
    }
183
184
    private function setSingleComment(array $arrayInput): void
185
    {
186
        if (array_key_exists('commentParentKey', $arrayInput)) {
187
            $this->setElementComment(implode('_', [$arrayInput['commentParentKey'], $arrayInput['tag']]));
188
            if (str_ends_with($arrayInput['tag'], 'Quantity')) {
189
                $this->setElementComment(implode('_', [$arrayInput['commentParentKey'], $arrayInput['tag']
190
                    . 'UnitOfMeasure']));
191
            }
192
        }
193
    }
194
195
    private function setSingleElementWithAttribute(array $arrayInput): void
196
    {
197
        $this->setSingleComment($arrayInput);
198
        if (is_array($arrayInput['data']) && array_key_exists('value', $arrayInput['data'])) {
199
            $this->objXmlWriter->startElement('cbc:' . $arrayInput['tag']);
200
            foreach ($arrayInput['data'] as $key => $value) {
201
                if ($key !== 'value') { // if is not value, must be an attribute
202
                    $this->objXmlWriter->writeAttribute($key, $value);
203
                }
204
            }
205
            $this->objXmlWriter->writeRaw($this->setNumericValue($arrayInput['tag'], $arrayInput['data']));
206
            $this->objXmlWriter->endElement();
207
        } else {
208
            $this->objXmlWriter->writeElement('cbc:' . $arrayInput['tag'], $arrayInput['data']);
209
        }
210
    }
211
212
    public function writeElectronicInvoice(string $strFile, array $inData, array $arrayFeatures): void
213
    {
214
        $this->getProcessingDetails();
215
        $arrayData = $this->loadSettingsAndManageDefaults($inData, $arrayFeatures);
216
        if (!array_key_exists('Ident', $arrayFeatures)) {
217
            $arrayFeatures['Ident'] = 4;
218
        }
219
        $this->setPrepareXml($strFile, $arrayData, $arrayFeatures['Ident']);
220
        $this->setHeaderCommonBasicComponents($arrayData['Header']['CommonBasicComponents-2']);
221
        $this->setProduceMiddleXml($arrayData['Header']['CommonAggregateComponents-2']);
222
        // multiple Lines
223
        $this->setMultipleElementsOrdered([
224
            'commentParentKey' => 'Lines',
225
            'data'             => $arrayData['Lines'],
226
            'tag'              => $arrayData['DocumentTagName'] . 'Line',
227
        ]);
228
        $this->objXmlWriter->endElement(); // Invoice or CreditNote
229
        $this->objXmlWriter->flush();
230
    }
231
}
232