Passed
Push — main ( a3ca9f...6b6493 )
by Daniel
12:08
created

ElectronicInvoiceWrite::setDecisionElements()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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