Passed
Push — main ( be0efc...81eb9b )
by Daniel
02:08
created

ElectornicInvoiceWrite::setElementWithAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
introduced by
The trait danielgp\efactura\TraitVersions requires some properties which are not provided by danielgp\efactura\ElectornicInvoiceWrite: $currencyID, $unitCode
Loading history...
35
36
    protected $objXmlWriter;
37
38
    private function setCompanyElementsOrdered(array $arrayInput): void {
39
        $this->setElementComment($arrayInput['commentParentKey']);
40
        $this->objXmlWriter->startElement('cac:' . $arrayInput['tag']);
41
        $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$arrayInput['commentParentKey']];
42
        foreach ($arrayCustomOrder as $value) {
43
            if (array_key_exists($value, $arrayInput['data'])) {
44
                $this->setElementComment(implode('_', [$arrayInput['commentParentKey'], $value]));
45
                if (is_array($arrayInput['data'][$value])) {
46
                    $this->objXmlWriter->startElement('cac:' . $value);
47
                    foreach ($arrayInput['data'][$value] as $key2 => $value2) {
48
                        $this->setElementComment(implode('_', [$arrayInput['commentParentKey'], $value, $key2]));
49
                        $this->objXmlWriter->writeElement('cbc:' . $key2, $value2);
50
                    }
51
                    $this->objXmlWriter->endElement();
52
                } else {
53
                    $this->objXmlWriter->writeElement('cbc:' . $value, $arrayInput['data'][$value]);
54
                }
55
            }
56
        }
57
        $this->objXmlWriter->endElement(); // $key
58
    }
59
60
    private function setDocumentTag(array $arrayDocumentData): void {
61
        $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']);
62
        foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) {
63
            if ($key === '') {
64
                $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']);
65
                $this->objXmlWriter->writeAttributeNS(NULL, 'xmlns', NULL, $strValue);
66
            } else {
67
                $this->objXmlWriter->writeAttributeNS('xmlns', $key, NULL, $value);
68
            }
69
        }
70
        if (array_key_exists('SchemaLocation', $arrayDocumentData)) {
71
            $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']);
72
        }
73
    }
74
75
    private function setHeaderCommonAggregateComponentsCompanies(array $arrayParameters): void {
76
        $key              = $arrayParameters['tag'];
77
        $this->setElementComment($key);
78
        $this->objXmlWriter->startElement('cac:' . $key);
79
        $this->objXmlWriter->startElement('cac:Party');
80
        $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$key];
81
        foreach ($arrayCustomOrder as $value) {
82
            if (array_key_exists($value, $arrayParameters['data'])) {
83
                $this->setCompanyElementsOrdered([
84
                    'commentParentKey' => implode('_', [$key, $value]),
85
                    'data'             => $arrayParameters['data'][$value],
86
                    'tag'              => $value,
87
                ]);
88
            }
89
        }
90
        $this->objXmlWriter->endElement(); // Party
91
        $this->objXmlWriter->endElement(); // $key
92
    }
93
94
    private function setElementWithAttribute(array $arrayParameters): void {
95
        $this->setElementComment(implode('_', [$arrayParameters['commentParentKey'], $arrayParameters['tag']]));
96
        $this->objXmlWriter->startElement('cbc:' . $arrayParameters['tag']);
97
        $this->objXmlWriter->writeAttribute('currencyID', $arrayParameters['data']['currencyID']);
98
        $this->objXmlWriter->writeRaw($arrayParameters['data']['value']);
99
        $this->objXmlWriter->endElement(); // TaxAmount
100
    }
101
102
    private function setHeaderCommonAggregateComponentsTaxTotal(array $arrayParameters): void {
103
        $key = $arrayParameters['tag'];
104
        $this->setElementComment($key);
105
        $this->objXmlWriter->startElement('cac:' . $key);
106
        $this->setElementWithAttribute([
107
            'commentParentKey' => $key,
108
            'data'             => $arrayParameters['data']['TaxAmount'],
109
            'tag'              => 'TaxAmount',
110
        ]);
111
        foreach ($arrayParameters['data']['TaxSubtotal'] as $value) {
112
            $this->objXmlWriter->startElement('cac:TaxSubtotal');
113
            $this->setElementWithAttribute([
114
                'commentParentKey' => implode('_', [$key, 'TaxSubtotal']),
115
                'data'             => $value['TaxAmount'],
116
                'tag'              => 'TaxableAmount',
117
            ]);
118
            $this->setElementWithAttribute([
119
                'commentParentKey' => implode('_', [$key, 'TaxSubtotal']),
120
                'data'             => $value['TaxAmount'],
121
                'tag'              => 'TaxAmount',
122
            ]);
123
            $this->setTaxCategory([
124
                'commentParentKey' => implode('_', [$key, 'TaxSubtotal', 'TaxCategory']),
125
                'data'             => $value['TaxCategory'],
126
                'tag'              => 'TaxCategory',
127
            ]);
128
            $this->objXmlWriter->endElement(); // TaxSubtotal
129
        }
130
        $this->objXmlWriter->endElement(); // $key
131
    }
132
133
    private function setHeaderCommonBasicComponents(array $arrayElementWithData): void {
134
        $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header']['CBC'];
135
        foreach ($arrayCustomOrdered as $value) {
136
            $this->setElementComment($value);
137
            $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]);
138
        }
139
    }
140
141
    private function setTaxCategory(array $arrayParameters): void {
142
        $key = $arrayParameters['tag'];
143
        $this->objXmlWriter->startElement('cac:' . $key);
144
        $this->setElementComment(implode('_', [$arrayParameters['commentParentKey'], 'ID']));
145
        $this->objXmlWriter->writeElement('cbc:ID', $arrayParameters['data']['ID']);
146
        $this->setElementComment(implode('_', [$arrayParameters['commentParentKey'], 'Percent']));
147
        $this->objXmlWriter->writeElement('cbc:Percent', $arrayParameters['data']['Percent']);
148
        $this->objXmlWriter->startElement('cac:TaxScheme');
149
        $this->objXmlWriter->writeElement('cbc:ID', $arrayParameters['data']['TaxScheme']['ID']);
150
        $this->objXmlWriter->endElement(); // $key
151
        $this->objXmlWriter->endElement(); // $key
152
    }
153
154
    public function writeElectronicInvoice(string $strFile, array $arrayData, bool $bolComments): void {
155
        $this->objXmlWriter = new \XMLWriter();
156
        $this->objXmlWriter->openURI($strFile);
157
        $this->objXmlWriter->setIndent(true);
158
        $this->objXmlWriter->setIndentString(str_repeat(' ', 4));
159
        $this->objXmlWriter->startDocument('1.0', 'UTF-8');
160
        // if no DocumentNameSpaces seen take Default ones from local configuration
161
        $this->getSettingsFromFileIntoMemory($bolComments);
162
        $arrayDefaults      = $this->getDefaultsIntoDataSet($arrayData);
163
        if ($arrayDefaults !== []) {
164
            $arrayData = array_merge($arrayData, $arrayDefaults['Root']);
165
            if (!array_key_exists('CustomizationID', $arrayData['Header']['CommonBasicComponents-2'])) {
166
                $arrayData['Header']['CommonBasicComponents-2']['CustomizationID'] = $arrayDefaults['CIUS-RO'];
167
                $arrayData['Header']['CommonBasicComponents-2']['UBLVersionID']    = $arrayDefaults['UBL'];
168
            }
169
        }
170
        $this->setDocumentTag($arrayData);
171
        $this->setHeaderCommonBasicComponents($arrayData['Header']['CommonBasicComponents-2'], $bolComments);
0 ignored issues
show
Unused Code introduced by
The call to danielgp\efactura\Electo...CommonBasicComponents() has too many arguments starting with $bolComments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

171
        $this->/** @scrutinizer ignore-call */ 
172
               setHeaderCommonBasicComponents($arrayData['Header']['CommonBasicComponents-2'], $bolComments);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
172
        $arrayAggegateComponents = $arrayData['Header']['CommonAggregateComponents-2'];
173
        foreach (['AccountingSupplierParty', 'AccountingCustomerParty'] as $strCompanyType) {
174
            $this->setHeaderCommonAggregateComponentsCompanies([
175
                'data'   => $arrayAggegateComponents[$strCompanyType]['Party'],
176
                'tag'    => $strCompanyType,
177
                'subTag' => 'Party',
178
            ]);
179
        }
180
        // multiple accounts can be specified within PaymentMeans
181
        if ($arrayData['DocumentTagName'] === 'Invoice') {
182
            foreach ($arrayAggegateComponents['PaymentMeans'] as $value) {
183
                $this->setHeaderCommonAggregateComponentsOrdered([
184
                    'data' => $value,
185
                    'tag'  => 'PaymentMeans',
186
                ]);
187
            }
188
        }
189
        $this->setHeaderCommonAggregateComponentsTaxTotal([
190
            'data' => $arrayAggegateComponents['TaxTotal'],
191
            'tag'  => 'TaxTotal',
192
        ]);
193
        $this->setHeaderCommonAggregateComponentsOthers([
194
            'data' => $arrayAggegateComponents['LegalMonetaryTotal'],
195
            'tag'  => 'LegalMonetaryTotal',
196
        ]);
197
        // multiple Lines
198
        foreach ($arrayData['Lines'] as $value) {
199
            $this->setHeaderCommonAggregateComponentsOthers([
200
                'data'           => $value,
201
                'tagForComments' => 'Lines',
202
                'tag'            => $arrayData['DocumentTagName'] . 'Line',
203
            ]);
204
        }
205
        $this->objXmlWriter->endElement(); // Invoice or CreditNote
206
        $this->objXmlWriter->flush();
207
    }
208
209
    private function setHeaderCommonAggregateComponentsOrdered(array $arrayParameters): void {
210
        $key              = $arrayParameters['tag'];
211
        $this->setElementComment($key);
212
        $this->objXmlWriter->startElement('cac:' . $key);
213
        $arrayCustomOrder = $this->arraySettings['CustomOrder']['Header']['CAC'][$key];
214
        foreach ($arrayCustomOrder as $value) {
215
            if (array_key_exists($value, $arrayParameters['data'])) {
216
                $this->setElementComment(implode('_', [$key, $value]));
217
                if (is_array($arrayParameters['data'][$value])) {
218
                    $this->objXmlWriter->startElement('cac:' . $value);
219
                    foreach ($arrayParameters['data'][$value] as $key2 => $value2) {
220
                        $this->setElementComment(implode('_', [$key, $value, $key2]));
221
                        $this->objXmlWriter->writeElement('cbc:' . $key2, $value2);
222
                    }
223
                    $this->objXmlWriter->endElement(); // $value
224
                } else {
225
                    $this->objXmlWriter->writeElement('cbc:' . $value, $arrayParameters['data'][$value]);
226
                }
227
            }
228
        }
229
        $this->objXmlWriter->endElement(); // $key
230
    }
231
232
    private function setHeaderCommonAggregateComponentsOthers(array $arrayParameters): void {
233
        if (array_key_exists('tagForComments', $arrayParameters)) {
234
            $key = $arrayParameters['tagForComments'];
235
        } else {
236
            $key = $arrayParameters['tag'];
237
        }
238
        $this->setElementComment($key);
239
        $this->objXmlWriter->startElement('cac:' . $arrayParameters['tag']);
240
        foreach ($arrayParameters['data'] as $key2 => $value2) {
241
            $this->setElementComment(implode('_', [$key, $key2]));
242
            if (is_array($value2)) {
243
                if (substr($key2, -6) === 'Amount') {
244
                    $this->objXmlWriter->startElement('cbc:' . $key2);
245
                    $this->objXmlWriter->writeAttribute('currencyID', $value2['currencyID']);
246
                    $this->objXmlWriter->writeRaw($value2['value']);
247
                } elseif (substr($key2, -8) === 'Quantity') {
248
                    $this->objXmlWriter->startElement('cbc:' . $key2);
249
                    $this->objXmlWriter->writeAttribute('unitCode', $value2['unitCode']);
250
                    $this->objXmlWriter->writeRaw($value2['value']);
251
                } else {
252
                    $this->objXmlWriter->startElement('cac:' . $key2);
253
                    foreach ($value2 as $key3 => $value3) {
254
                        $this->setElementComment(implode('_', [$key, $key2, $key3]));
255
                        if (substr($key3, -6) === 'Amount') {
256
                            $this->objXmlWriter->startElement('cbc:' . $key3);
257
                            $this->objXmlWriter->writeAttribute('currencyID', $value3['currencyID']);
258
                            $this->objXmlWriter->writeRaw($value3['value']);
259
                            $this->objXmlWriter->endElement(); // $key3
260
                        } elseif (substr($key3, -8) === 'Quantity') {
261
                            $this->objXmlWriter->startElement('cbc:' . $key3);
262
                            $this->objXmlWriter->writeAttribute('unitCode', $value3['unitCode']);
263
                            $this->objXmlWriter->writeRaw($value3['value']);
264
                            $this->objXmlWriter->endElement(); // $key3
265
                        } elseif (is_array($value3)) {
266
                            $this->objXmlWriter->startElement('cac:' . $key3);
267
                            foreach ($value3 as $key4 => $value4) {
268
                                $this->setElementComment(implode('_', [$key, $key2, $key3, $key4]));
269
                                if (is_array($value4)) {
270
                                    $this->objXmlWriter->startElement('cac:' . $key4);
271
                                    foreach ($value4 as $key5 => $value5) {
272
                                        $this->setElementComment(implode('_', [$key, $key2, $key3, $key4, $key5]));
273
                                        $this->objXmlWriter->writeElement('cbc:' . $key5, $value5);
274
                                    }
275
                                    $this->objXmlWriter->endElement(); // $key4
276
                                } else {
277
                                    $this->objXmlWriter->writeElement('cbc:' . $key4, $value4);
278
                                }
279
                            }
280
                            $this->objXmlWriter->endElement(); // $key3
281
                        } else {
282
                            $this->objXmlWriter->writeElement('cbc:' . $key3, $value3);
283
                        }
284
                    }
285
                }
286
                $this->objXmlWriter->endElement(); // $key2
287
            } else {
288
                $this->objXmlWriter->writeElement('cbc:' . $key2, $value2);
289
            }
290
        }
291
        $this->objXmlWriter->endElement(); // $key
292
    }
293
}
294