|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2024, Daniel Popiniuc and its licensors. |
|
5
|
|
|
* |
|
6
|
|
|
* All rights reserved. This program and the accompanying materials |
|
7
|
|
|
* are made available under the terms of the Eclipse Public License v1.0 |
|
8
|
|
|
* which accompanies this distribution, and is available at |
|
9
|
|
|
* http://www.eclipse.org/legal/epl-v20.html |
|
10
|
|
|
* |
|
11
|
|
|
* Contributors: |
|
12
|
|
|
* Daniel Popiniuc |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace danielgp\efactura; |
|
16
|
|
|
|
|
17
|
|
|
trait TraitUserInterfaceLogic |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
use \danielgp\io_operations\InputOutputFiles; |
|
21
|
|
|
|
|
22
|
|
|
protected array $arrayConfiguration; |
|
23
|
|
|
protected $translation; |
|
24
|
|
|
|
|
25
|
|
|
public function actionAnalyzeZIPfromANAFfromLocalFolder(string $strFilePath): array |
|
26
|
|
|
{ |
|
27
|
|
|
$arrayFiles = new \RecursiveDirectoryIterator($strFilePath, \FilesystemIterator::SKIP_DOTS); |
|
28
|
|
|
$arrayInvoices = []; |
|
29
|
|
|
$intFileNo = 0; |
|
30
|
|
|
foreach ($arrayFiles as $strFile) { |
|
31
|
|
|
if ($strFile->isFile()) { |
|
32
|
|
|
$arrayFileDetails = $this->handleResponseFile($strFile); |
|
|
|
|
|
|
33
|
|
|
if ($arrayFileDetails !== []) { |
|
34
|
|
|
$arrayInvoices[$intFileNo] = $arrayFileDetails; |
|
35
|
|
|
$intFileNo++; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
return $arrayInvoices; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function getConfiguration() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->arrayConfiguration = $this->getArrayFromJsonFile(__DIR__ |
|
45
|
|
|
. DIRECTORY_SEPARATOR . 'config', 'BasicConfiguration.json'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Archived document is read as content in memory since 2024-02-16 |
|
50
|
|
|
* (prior to this date a temporary local file was saved, processed and finally removed when done with it) |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $arrayData |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
private function getDocumentDetails(array $arrayData): array |
|
56
|
|
|
{ |
|
57
|
|
|
$appR = new \danielgp\efactura\ClassElectronicInvoiceRead(); |
|
58
|
|
|
$arrayElectronicInv = $appR->readElectronicInvoice($arrayData['strInvoiceContent']); |
|
59
|
|
|
$arrayBasic = $arrayElectronicInv['Header']['CommonBasicComponents-2']; |
|
60
|
|
|
$arrayAggregate = $arrayElectronicInv['Header']['CommonAggregateComponents-2']; |
|
61
|
|
|
$arrayStandardized = [ |
|
62
|
|
|
'Customer' => $arrayAggregate['AccountingCustomerParty']['Party'], |
|
63
|
|
|
'ID' => $arrayBasic['ID'], |
|
64
|
|
|
'IssueDate' => $arrayBasic['IssueDate'], |
|
65
|
|
|
'No_of_Lines' => count($arrayElectronicInv['Lines']), |
|
66
|
|
|
'Supplier' => $arrayAggregate['AccountingSupplierParty']['Party'], |
|
67
|
|
|
'TOTAL' => (float) $arrayAggregate['LegalMonetaryTotal']['TaxInclusiveAmount']['value'], |
|
68
|
|
|
'wo_VAT' => (float) $arrayAggregate['LegalMonetaryTotal']['TaxExclusiveAmount']['value'], |
|
69
|
|
|
]; |
|
70
|
|
|
return $arrayStandardized; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function handleResponseFile(\SplFileInfo $strFile): array |
|
74
|
|
|
{ |
|
75
|
|
|
$arrayToReturn = []; |
|
76
|
|
|
$strFileMime = mime_content_type($strFile->getRealPath()); |
|
77
|
|
|
switch ($strFileMime) { |
|
78
|
|
|
case 'application/json': |
|
79
|
|
|
$arrayError = $this->getArrayFromJsonFile($strFile->getPath(), $strFile->getFilename()); |
|
80
|
|
|
$arrayToReturn = $this->setStandardizedFeedbackArray([ |
|
81
|
|
|
'Error' => $arrayError['eroare'] . ' ===> ' . $arrayError['titlu'], |
|
82
|
|
|
'Response_Index' => pathinfo($strFile)['filename'], |
|
83
|
|
|
]); |
|
84
|
|
|
break; |
|
85
|
|
|
case 'application/zip': |
|
86
|
|
|
$arrayToReturn = $this->setArchiveFromAnaf($strFile->getRealPath()); |
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
return $arrayToReturn; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function handleArchiveContent(\ZipArchive $classZip): array |
|
93
|
|
|
{ |
|
94
|
|
|
$arrayToReturn = []; |
|
95
|
|
|
$intFilesArchived = $classZip->numFiles; |
|
96
|
|
|
for ($intArchivedFile = 0; $intArchivedFile < $intFilesArchived; $intArchivedFile++) { |
|
97
|
|
|
$strArchivedFile = $classZip->getNameIndex($intArchivedFile); |
|
98
|
|
|
$strFileStats = $classZip->statIndex($intArchivedFile); |
|
99
|
|
|
$matches = []; |
|
100
|
|
|
preg_match('/^[0-9]{5,20}\.xml$/', $strArchivedFile, $matches, PREG_OFFSET_CAPTURE); |
|
101
|
|
|
$matches2 = []; |
|
102
|
|
|
preg_match('/^semnatura_[0-9]{5,20}\.xml$/', $strArchivedFile, $matches2, PREG_OFFSET_CAPTURE); |
|
103
|
|
|
if ($matches !== []) { |
|
104
|
|
|
$resInvoice = $classZip->getStream($strArchivedFile); |
|
105
|
|
|
$strInvoiceContent = stream_get_contents($resInvoice); |
|
106
|
|
|
fclose($resInvoice); |
|
107
|
|
|
$arrayToReturn = $this->setStandardizedFeedbackArray([ |
|
108
|
|
|
'Response_Index' => pathinfo($strFile)['filename'], |
|
|
|
|
|
|
109
|
|
|
'Size' => $strFileStats['size'], |
|
110
|
|
|
'FileDate' => date('Y-m-d H:i:s', $strFileStats['mtime']), |
|
111
|
|
|
'Matches' => $matches, |
|
112
|
|
|
'strArchivedFileName' => $strArchivedFile, |
|
113
|
|
|
'strInvoiceContent' => $strInvoiceContent, |
|
114
|
|
|
]); |
|
115
|
|
|
} elseif ($matches2 === []) { |
|
116
|
|
|
echo vsprintf('<div>' . $this->arrayConfiguration['Feedback']['DifferentFile'] . '</div>', [ |
|
117
|
|
|
$strArchivedFile, |
|
118
|
|
|
$strFile->getBasename(), |
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
return $arrayToReturn; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function setArchiveFromAnaf(string $strFile) |
|
126
|
|
|
{ |
|
127
|
|
|
$arrayToReturn = []; |
|
128
|
|
|
$classZip = new \ZipArchive(); |
|
129
|
|
|
$res = $classZip->open($strFile, \ZipArchive::RDONLY); |
|
130
|
|
|
if ($res) { |
|
131
|
|
|
$arrayToReturn = $this->handleArchiveContent($classZip); |
|
132
|
|
|
} else { |
|
133
|
|
|
// @codeCoverageIgnoreStart |
|
134
|
|
|
$arrayToReturn = $this->setStandardizedFeedbackArray([ |
|
135
|
|
|
'Response_Index' => pathinfo($strFile)['filename'], |
|
136
|
|
|
'Error' => $this->translation->find(null, 'i18n_Msg_InvalidZip')->getTranslation(), |
|
137
|
|
|
]); |
|
138
|
|
|
// @codeCoverageIgnoreEnd |
|
139
|
|
|
} |
|
140
|
|
|
return $arrayToReturn; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function setDataSupplierOrCustomer(array $arrayData) |
|
144
|
|
|
{ |
|
145
|
|
|
$strCustomerCui = ''; |
|
146
|
|
|
if (isset($arrayData['PartyTaxScheme']['01']['CompanyID'])) { |
|
147
|
|
|
$strCustomerCui = $arrayData['PartyTaxScheme']['01']['CompanyID']; |
|
148
|
|
|
} else { |
|
149
|
|
|
$strCustomerCui = $arrayData['PartyLegalEntity']['CompanyID']; |
|
150
|
|
|
} |
|
151
|
|
|
if (is_numeric($strCustomerCui)) { |
|
152
|
|
|
$strCustomerCui = 'RO' . $strCustomerCui; |
|
153
|
|
|
} |
|
154
|
|
|
return $strCustomerCui; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
private function setDaysElapsed(string $strFirstDate, string $strLaterDate): string |
|
158
|
|
|
{ |
|
159
|
|
|
$origin = new \DateTimeImmutable($strFirstDate); |
|
160
|
|
|
$target = new \DateTimeImmutable($strLaterDate); |
|
161
|
|
|
$interval = $origin->diff($target); |
|
162
|
|
|
return $interval->format('%R%a'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function setLocalization(): void |
|
166
|
|
|
{ |
|
167
|
|
|
if (!array_key_exists('language_COUNTRY', $_GET)) { |
|
168
|
|
|
$_GET['language_COUNTRY'] = 'ro_RO'; |
|
169
|
|
|
} |
|
170
|
|
|
$loader = new \Gettext\Loader\PoLoader(); |
|
171
|
|
|
$this->translation = $loader->loadFile(__DIR__ . '/locale/' . $_GET['language_COUNTRY'] |
|
172
|
|
|
. '/LC_MESSAGES/eFactura.po'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
private function setStandardizedFeedbackArray(array $arrayData): array |
|
176
|
|
|
{ |
|
177
|
|
|
$arrayToReturn = [ |
|
178
|
|
|
'Response_Date' => '', |
|
179
|
|
|
'Response_Index' => $arrayData['Response_Index'], |
|
180
|
|
|
'Loading_Index' => '', |
|
181
|
|
|
'Size' => '', |
|
182
|
|
|
'Document_No' => '', |
|
183
|
|
|
'Issue_Date' => '', |
|
184
|
|
|
'Issue_YearMonth' => '', |
|
185
|
|
|
'Amount_wo_VAT' => '', |
|
186
|
|
|
'Amount_TOTAL' => '', |
|
187
|
|
|
'Amount_VAT' => '', |
|
188
|
|
|
'Supplier_CUI' => '', |
|
189
|
|
|
'Supplier_Name' => '', |
|
190
|
|
|
'Customer_CUI' => '', |
|
191
|
|
|
'Customer_Name' => '', |
|
192
|
|
|
'No_of_Lines' => '', |
|
193
|
|
|
'Error' => '', |
|
194
|
|
|
'Days_Between' => '', |
|
195
|
|
|
]; |
|
196
|
|
|
$strErrorTag = '<div style="max-width:200px;font-size:0.8rem;">%s</div>'; |
|
197
|
|
|
if (array_key_exists('Error', $arrayData)) { |
|
198
|
|
|
$arrayToReturn['Error'] = sprintf($strErrorTag, $arrayData['Error']); |
|
199
|
|
|
$arrayToReturn['Size'] = 0; |
|
200
|
|
|
} elseif ($arrayData['Size'] > 1000) { |
|
201
|
|
|
$strTimeZone = $this->translation->find(null, 'i18n_TimeZone')->getTranslation(); |
|
202
|
|
|
$strFormatter = new \IntlDateFormatter( |
|
203
|
|
|
$_GET['language_COUNTRY'], |
|
204
|
|
|
\IntlDateFormatter::FULL, |
|
205
|
|
|
\IntlDateFormatter::FULL, |
|
206
|
|
|
$strTimeZone, |
|
207
|
|
|
\IntlDateFormatter::GREGORIAN, |
|
208
|
|
|
'r-MM__MMMM' |
|
209
|
|
|
); |
|
210
|
|
|
$arrayAttr = $this->getDocumentDetails($arrayData); |
|
211
|
|
|
$arrayToReturn['Loading_Index'] = substr($arrayData['Matches'][0][0], 0, -4); |
|
212
|
|
|
$arrayToReturn['Size'] = $arrayData['Size']; |
|
213
|
|
|
$arrayToReturn['Document_No'] = $arrayAttr['ID']; |
|
214
|
|
|
$arrayToReturn['Issue_Date'] = $arrayAttr['IssueDate']; |
|
215
|
|
|
$arrayToReturn['Issue_YearMonth'] = $strFormatter->format(new \DateTime($arrayAttr['IssueDate'])); |
|
216
|
|
|
$arrayToReturn['Response_Date'] = $arrayData['FileDate']; |
|
217
|
|
|
$arrayToReturn['Amount_wo_VAT'] = $arrayAttr['wo_VAT']; |
|
218
|
|
|
$arrayToReturn['Amount_TOTAL'] = $arrayAttr['TOTAL']; |
|
219
|
|
|
$arrayToReturn['Amount_VAT'] = round(($arrayAttr['TOTAL'] - $arrayAttr['wo_VAT']), 2); |
|
220
|
|
|
$arrayToReturn['Supplier_CUI'] = $this->setDataSupplierOrCustomer($arrayAttr['Supplier']); |
|
221
|
|
|
$arrayToReturn['Supplier_Name'] = $arrayAttr['Supplier']['PartyLegalEntity']['RegistrationName']; |
|
222
|
|
|
$arrayToReturn['Customer_CUI'] = $this->setDataSupplierOrCustomer($arrayAttr['Customer']); |
|
223
|
|
|
$arrayToReturn['Customer_Name'] = $arrayAttr['Customer']['PartyLegalEntity']['RegistrationName']; |
|
224
|
|
|
$arrayToReturn['No_of_Lines'] = $arrayAttr['No_of_Lines']; |
|
225
|
|
|
$arrayToReturn['Days_Between'] = $this->setDaysElapsed($arrayAttr['IssueDate'], $arrayData['FileDate']); |
|
226
|
|
|
} elseif ($arrayData['Size'] > 0) { |
|
227
|
|
|
$objErrors = new \SimpleXMLElement($arrayData['strInvoiceContent']); |
|
228
|
|
|
$arrayToReturn['Loading_Index'] = $objErrors->attributes()->Index_incarcare->__toString(); |
|
229
|
|
|
$arrayToReturn['Size'] = $arrayData['Size']; |
|
230
|
|
|
$arrayToReturn['Response_Date'] = $arrayData['FileDate']; |
|
231
|
|
|
$arrayToReturn['Supplier_CUI'] = 'RO' . $objErrors->attributes()->Cif_emitent->__toString(); |
|
232
|
|
|
$arrayToReturn['Supplier_Name'] = '??????????'; |
|
233
|
|
|
$arrayToReturn['Error'] = sprintf($strErrorTag, $objErrors |
|
234
|
|
|
->Error->attributes()->errorMessage->__toString()); |
|
235
|
|
|
} |
|
236
|
|
|
return $arrayToReturn; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|