Passed
Push — main ( 3edd16...be0efc )
by Daniel
02:07
created

ElectornicInvoiceWrite::writeElectronicInvoice()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 58
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 43
c 3
b 0
f 0
nc 24
nop 3
dl 0
loc 58
rs 8.2986

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ElectornicInvoiceWrite
32
{
33
34
    use traitVersions;
35
36
    protected $objXmlWriter;
37
38
    private function setCompanyElementsOrdered(array $arrayParameters): void {
39
        $this->setElementComment($arrayParameters['commentParentKey'], 'CAC', $arrayParameters['comments']);
40
        $this->objXmlWriter->startElement('cac:' . $arrayParameters['tag']);
41
        $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$arrayParameters['commentParentKey']];
42
        foreach ($arrayCustomOrder as $value) {
43
            if (array_key_exists($value, $arrayParameters['data'])) {
44
                $this->setElementComment(implode('_', [
45
                    $arrayParameters['commentParentKey'], $value]), 'CAC', $arrayParameters['comments']);
46
                if (is_array($arrayParameters['data'][$value])) {
47
                    $this->objXmlWriter->startElement('cac:' . $value);
48
                    foreach ($arrayParameters['data'][$value] as $key2 => $value2) {
49
                        $this->setElementComment(implode('_', [
50
                            $arrayParameters['commentParentKey'], $value, $key2]), 'CAC', $arrayParameters['comments']);
51
                        $this->objXmlWriter->writeElement('cbc:' . $key2, $value2);
52
                    }
53
                    $this->objXmlWriter->endElement();
54
                } else {
55
                    $this->objXmlWriter->writeElement('cbc:' . $value, $arrayParameters['data'][$value]);
56
                }
57
            }
58
        }
59
        $this->objXmlWriter->endElement(); // $key
60
    }
61
62
    private function setDocumentTag(array $arrayDocumentData): void {
63
        $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']);
64
        foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) {
65
            if ($key === '') {
66
                $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']);
67
                $this->objXmlWriter->writeAttributeNS(NULL, 'xmlns', NULL, $strValue);
68
            } else {
69
                $this->objXmlWriter->writeAttributeNS('xmlns', $key, NULL, $value);
70
            }
71
        }
72
        if (array_key_exists('SchemaLocation', $arrayDocumentData)) {
73
            $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']);
74
        }
75
    }
76
77
    private function setElementComment(string $strKey, string $strSection, bool $includeComments): void {
78
        if ($includeComments && array_key_exists($strKey, $this->arraySettings['Comments'][$strSection])) {
79
            $elementComment = $this->arraySettings['Comments'][$strSection][$strKey];
80
            if (is_array($elementComment)) {
81
                foreach ($elementComment as $value) {
82
                    $this->objXmlWriter->writeComment($value);
83
                }
84
            } else {
85
                $this->objXmlWriter->writeComment($elementComment);
86
            }
87
        }
88
    }
89
90
    private function setHeaderCommonAggregateComponentsCompanies(array $arrayParameters): void {
91
        $key              = $arrayParameters['tag'];
92
        $this->setElementComment($key, 'CAC', $arrayParameters['comments']);
93
        $this->objXmlWriter->startElement('cac:' . $key);
94
        $this->objXmlWriter->startElement('cac:Party');
95
        $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$key];
96
        foreach ($arrayCustomOrder as $value) {
97
            if (array_key_exists($value, $arrayParameters['data'])) {
98
                $this->setCompanyElementsOrdered([
99
                    'comments'         => $arrayParameters['comments'],
100
                    'commentParentKey' => implode('_', [$key, $value]),
101
                    'data'             => $arrayParameters['data'][$value],
102
                    'tag'              => $value,
103
                ]);
104
            }
105
        }
106
        $this->objXmlWriter->endElement(); // Party
107
        $this->objXmlWriter->endElement(); // $key
108
    }
109
110
    private function setHeaderCommonAggregateComponentsTaxTotal(array $arrayParameters): void {
111
        $key = $arrayParameters['tag'];
112
        $this->setElementComment($key, 'CAC', $arrayParameters['comments']);
113
        $this->objXmlWriter->startElement('cac:' . $key);
114
        $this->setElementComment(implode('_', [$key, 'TaxAmount']), 'CAC', $arrayParameters['comments']);
115
        $this->objXmlWriter->startElement('cbc:TaxAmount');
116
        $this->objXmlWriter->writeAttribute('currencyID', $arrayParameters['data']['TaxAmount']['currencyID']);
117
        $this->objXmlWriter->writeRaw($arrayParameters['data']['TaxAmount']['value']);
118
        $this->objXmlWriter->endElement(); // TaxAmount
119
        foreach ($arrayParameters['data']['TaxSubtotal'] as $value) {
120
            $this->objXmlWriter->startElement('cac:TaxSubtotal');
121
            $this->setElementComment(implode('_', [$key, 'TaxSubtotal', 'TaxableAmount']), 'CAC', $arrayParameters['comments']);
122
            $this->objXmlWriter->startElement('cbc:TaxableAmount');
123
            $this->objXmlWriter->writeAttribute('currencyID', $value['TaxableAmount']['currencyID']);
124
            $this->objXmlWriter->writeRaw($value['TaxableAmount']['value']);
125
            $this->objXmlWriter->endElement(); // TaxableAmount
126
            $this->setElementComment(implode('_', [$key, 'TaxSubtotal', 'TaxAmount']), 'CAC', $arrayParameters['comments']);
127
            $this->objXmlWriter->startElement('cbc:TaxAmount');
128
            $this->objXmlWriter->writeAttribute('currencyID', $value['TaxAmount']['currencyID']);
129
            $this->objXmlWriter->writeRaw($value['TaxAmount']['value']);
130
            $this->objXmlWriter->endElement(); // TaxAmount
131
            $this->setTaxCategory([
132
                'comments'         => $arrayParameters['comments'],
133
                'commentParentKey' => implode('_', [$key, 'TaxSubtotal', 'TaxCategory']),
134
                'data'             => $value['TaxCategory'],
135
                'tag'              => 'TaxCategory',
136
            ]);
137
            $this->objXmlWriter->endElement(); // TaxSubtotal
138
        }
139
        $this->objXmlWriter->endElement(); // $key
140
    }
141
142
    private function setHeaderCommonBasicComponents(array $arrayElementWithData, bool $includeComments): void {
143
        $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header']['CBC'];
144
        foreach ($arrayCustomOrdered as $value) {
145
            $this->setElementComment($value, 'CBC', $includeComments);
146
            $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]);
147
        }
148
    }
149
150
    private function setTaxCategory(array $arrayParameters): void {
151
        $key = $arrayParameters['tag'];
152
        $this->objXmlWriter->startElement('cac:' . $key);
153
        $this->setElementComment(implode('_', [
154
            $arrayParameters['commentParentKey'], 'ID']), 'CAC', $arrayParameters['comments']);
155
        $this->objXmlWriter->writeElement('cbc:ID', $arrayParameters['data']['ID']);
156
        $this->setElementComment(implode('_', [
157
            $arrayParameters['commentParentKey'], 'Percent']), 'CAC', $arrayParameters['comments']);
158
        $this->objXmlWriter->writeElement('cbc:Percent', $arrayParameters['data']['Percent']);
159
        $this->objXmlWriter->startElement('cac:TaxScheme');
160
        $this->objXmlWriter->writeElement('cbc:ID', $arrayParameters['data']['TaxScheme']['ID']);
161
        $this->objXmlWriter->endElement(); // $key
162
        $this->objXmlWriter->endElement(); // $key
163
    }
164
165
    public function writeElectronicInvoice(string $strFile, array $arrayData, bool $bolComments): void {
166
        $this->objXmlWriter = new \XMLWriter();
167
        $this->objXmlWriter->openURI($strFile);
168
        $this->objXmlWriter->setIndent(true);
169
        $this->objXmlWriter->setIndentString(str_repeat(' ', 4));
170
        $this->objXmlWriter->startDocument('1.0', 'UTF-8');
171
        // if no DocumentNameSpaces seen take Default ones from local configuration
172
        $this->getSettingsFromFileIntoMemory();
173
        $arrayDefaults      = $this->getDefaultsIntoDataSet($arrayData);
174
        if ($arrayDefaults !== []) {
175
            $arrayData = array_merge($arrayData, $arrayDefaults['Root']);
176
            if (!array_key_exists('CustomizationID', $arrayData['Header']['CommonBasicComponents-2'])) {
177
                $arrayData['Header']['CommonBasicComponents-2']['CustomizationID'] = $arrayDefaults['CIUS-RO'];
178
                $arrayData['Header']['CommonBasicComponents-2']['UBLVersionID']    = $arrayDefaults['UBL'];
179
            }
180
        }
181
        $this->setDocumentTag($arrayData);
182
        $this->setHeaderCommonBasicComponents($arrayData['Header']['CommonBasicComponents-2'], $bolComments);
183
        $arrayAggegateComponents = $arrayData['Header']['CommonAggregateComponents-2'];
184
        foreach (['AccountingSupplierParty', 'AccountingCustomerParty'] as $strCompanyType) {
185
            $this->setHeaderCommonAggregateComponentsCompanies([
186
                'data'     => $arrayAggegateComponents[$strCompanyType]['Party'],
187
                'tag'      => $strCompanyType,
188
                'subTag'   => 'Party',
189
                'comments' => $bolComments,
190
            ]);
191
        }
192
        // multiple accounts can be specified within PaymentMeans
193
        if ($arrayData['DocumentTagName'] === 'Invoice') {
194
            foreach ($arrayAggegateComponents['PaymentMeans'] as $value) {
195
                $this->setHeaderCommonAggregateComponentsOrdered([
196
                    'data'     => $value,
197
                    'tag'      => 'PaymentMeans',
198
                    'comments' => $bolComments,
199
                ]);
200
            }
201
        }
202
        $this->setHeaderCommonAggregateComponentsTaxTotal([
203
            'data'     => $arrayAggegateComponents['TaxTotal'],
204
            'tag'      => 'TaxTotal',
205
            'comments' => $bolComments,
206
        ]);
207
        $this->setHeaderCommonAggregateComponentsOthers([
208
            'data'     => $arrayAggegateComponents['LegalMonetaryTotal'],
209
            'tag'      => 'LegalMonetaryTotal',
210
            'comments' => $bolComments,
211
        ]);
212
        // multiple Lines
213
        foreach ($arrayData['Lines'] as $value) {
214
            $this->setHeaderCommonAggregateComponentsOthers([
215
                'data'           => $value,
216
                'tagForComments' => 'Lines',
217
                'tag'            => $arrayData['DocumentTagName'] . 'Line',
218
                'comments'       => $bolComments,
219
            ]);
220
        }
221
        $this->objXmlWriter->endElement(); // Invoice or CreditNote
222
        $this->objXmlWriter->flush();
223
    }
224
225
    private function setHeaderCommonAggregateComponentsOrdered(array $arrayParameters): void {
226
        $key              = $arrayParameters['tag'];
227
        $this->setElementComment($key, 'CAC', $arrayParameters['comments']);
228
        $this->objXmlWriter->startElement('cac:' . $key);
229
        $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$key];
230
        foreach ($arrayCustomOrder as $value) {
231
            if (array_key_exists($value, $arrayParameters['data'])) {
232
                $this->setElementComment(implode('_', [$key, $value]), 'CAC', $arrayParameters['comments']);
233
                if (is_array($arrayParameters['data'][$value])) {
234
                    $this->objXmlWriter->startElement('cac:' . $value);
235
                    foreach ($arrayParameters['data'][$value] as $key2 => $value2) {
236
                        $this->setElementComment(implode('_', [$key, $value, $key2]), 'CAC', $arrayParameters['comments']);
237
                        $this->objXmlWriter->writeElement('cbc:' . $key2, $value2);
238
                    }
239
                    $this->objXmlWriter->endElement(); // $value
240
                } else {
241
                    $this->objXmlWriter->writeElement('cbc:' . $value, $arrayParameters['data'][$value]);
242
                }
243
            }
244
        }
245
        $this->objXmlWriter->endElement(); // $key
246
    }
247
248
    private function setHeaderCommonAggregateComponentsOthers(array $arrayParameters): void {
249
        if (array_key_exists('tagForComments', $arrayParameters)) {
250
            $key = $arrayParameters['tagForComments'];
251
        } else {
252
            $key = $arrayParameters['tag'];
253
        }
254
        $this->setElementComment($key, 'CAC', $arrayParameters['comments']);
255
        $this->objXmlWriter->startElement('cac:' . $arrayParameters['tag']);
256
        foreach ($arrayParameters['data'] as $key2 => $value2) {
257
            $this->setElementComment(implode('_', [$key, $key2]), 'CAC', $arrayParameters['comments']);
258
            if (is_array($value2)) {
259
                if (substr($key2, -6) === 'Amount') {
260
                    $this->objXmlWriter->startElement('cbc:' . $key2);
261
                    $this->objXmlWriter->writeAttribute('currencyID', $value2['currencyID']);
262
                    $this->objXmlWriter->writeRaw($value2['value']);
263
                } elseif (substr($key2, -8) === 'Quantity') {
264
                    $this->objXmlWriter->startElement('cbc:' . $key2);
265
                    $this->objXmlWriter->writeAttribute('unitCode', $value2['unitCode']);
266
                    $this->objXmlWriter->writeRaw($value2['value']);
267
                } else {
268
                    $this->objXmlWriter->startElement('cac:' . $key2);
269
                    foreach ($value2 as $key3 => $value3) {
270
                        $this->setElementComment(implode('_', [
271
                            $key, $key2, $key3]), 'CAC', $arrayParameters['comments']);
272
                        if (substr($key3, -6) === 'Amount') {
273
                            $this->objXmlWriter->startElement('cbc:' . $key3);
274
                            $this->objXmlWriter->writeAttribute('currencyID', $value3['currencyID']);
275
                            $this->objXmlWriter->writeRaw($value3['value']);
276
                            $this->objXmlWriter->endElement(); // $key3
277
                        } elseif (substr($key3, -8) === 'Quantity') {
278
                            $this->objXmlWriter->startElement('cbc:' . $key3);
279
                            $this->objXmlWriter->writeAttribute('unitCode', $value3['unitCode']);
280
                            $this->objXmlWriter->writeRaw($value3['value']);
281
                            $this->objXmlWriter->endElement(); // $key3
282
                        } elseif (is_array($value3)) {
283
                            $this->objXmlWriter->startElement('cac:' . $key3);
284
                            foreach ($value3 as $key4 => $value4) {
285
                                $this->setElementComment(implode('_', [
286
                                    $key, $key2, $key3, $key4]), 'CAC', $arrayParameters['comments']);
287
                                if (is_array($value4)) {
288
                                    $this->objXmlWriter->startElement('cac:' . $key4);
289
                                    foreach ($value4 as $key5 => $value5) {
290
                                        $this->setElementComment(implode('_', [
291
                                            $key, $key2, $key3, $key4, $key5]), 'CAC', $arrayParameters['comments']);
292
                                        $this->objXmlWriter->writeElement('cbc:' . $key5, $value5);
293
                                    }
294
                                    $this->objXmlWriter->endElement(); // $key4
295
                                } else {
296
                                    $this->objXmlWriter->writeElement('cbc:' . $key4, $value4);
297
                                }
298
                            }
299
                            $this->objXmlWriter->endElement(); // $key3
300
                        } else {
301
                            $this->objXmlWriter->writeElement('cbc:' . $key3, $value3);
302
                        }
303
                    }
304
                }
305
                $this->objXmlWriter->endElement(); // $key2
306
            } else {
307
                $this->objXmlWriter->writeElement('cbc:' . $key2, $value2);
308
            }
309
        }
310
        $this->objXmlWriter->endElement(); // $key
311
    }
312
}
313