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
|
|
|
use \danielgp\io_operations\InputOutputFiles; |
20
|
|
|
|
21
|
|
|
protected array $arrayConfiguration; |
22
|
|
|
|
23
|
|
|
protected function getConfiguration() |
24
|
|
|
{ |
25
|
|
|
$this->arrayConfiguration = $this->getArrayFromJsonFile(__DIR__ |
26
|
|
|
. DIRECTORY_SEPARATOR . 'config', 'BasicConfiguration.json'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Archived document interpretation requires a temporary files to be stored |
31
|
|
|
* and upon processing file is removed immediately |
32
|
|
|
* |
33
|
|
|
* @param array $arrayData |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
private function getDocumentDetails(array $arrayData): array |
37
|
|
|
{ |
38
|
|
|
file_put_contents($arrayData['strArchivedFileName'], $arrayData['strInvoiceContent']); |
39
|
|
|
$appR = new \danielgp\efactura\ClassElectronicInvoiceRead(); |
40
|
|
|
$arrayElectronicInv = $appR->readElectronicInvoice($arrayData['strArchivedFileName']); |
41
|
|
|
$arrayBasic = $arrayElectronicInv['Header']['CommonBasicComponents-2']; |
42
|
|
|
$arrayAggregate = $arrayElectronicInv['Header']['CommonAggregateComponents-2']; |
43
|
|
|
$arrayStandardized = [ |
44
|
|
|
'Customer' => $arrayAggregate['AccountingCustomerParty']['Party'], |
45
|
|
|
'ID' => $arrayBasic['ID'], |
46
|
|
|
'IssueDate' => $arrayBasic['IssueDate'], |
47
|
|
|
'No_of_Lines' => count($arrayElectronicInv['Lines']), |
48
|
|
|
'Supplier' => $arrayAggregate['AccountingSupplierParty']['Party'], |
49
|
|
|
'TOTAL' => (float) $arrayAggregate['LegalMonetaryTotal']['TaxInclusiveAmount']['value'], |
50
|
|
|
'wo_VAT' => (float) $arrayAggregate['LegalMonetaryTotal']['TaxExclusiveAmount']['value'], |
51
|
|
|
]; |
52
|
|
|
unlink($arrayData['strArchivedFileName']); |
53
|
|
|
return $arrayStandardized; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function actionAnalyzeZIPfromANAFfromLocalFolder(string $strFilePath): array |
57
|
|
|
{ |
58
|
|
|
$arrayFiles = new \RecursiveDirectoryIterator($strFilePath, \FilesystemIterator::SKIP_DOTS); |
59
|
|
|
$arrayInvoices = []; |
60
|
|
|
$intFileNo = 0; |
61
|
|
|
foreach ($arrayFiles as $strFile) { |
62
|
|
|
if ($strFile->isFile() && ($strFile->getExtension() === 'zip')) { |
63
|
|
|
$arrayInvoices[$intFileNo] = $this->setArchiveFromAnaf($strFile->getRealPath()); |
64
|
|
|
$intFileNo++; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return $arrayInvoices; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function setArchiveFromAnaf(string $strFile) |
71
|
|
|
{ |
72
|
|
|
$classZip = new \ZipArchive(); |
73
|
|
|
$arrayToReturn = []; |
74
|
|
|
$res = $classZip->open($strFile, \ZipArchive::RDONLY); |
75
|
|
|
if ($res === true) { |
76
|
|
|
$intFilesArchived = $classZip->numFiles; |
77
|
|
|
for ($intArchivedFile = 0; $intArchivedFile < $intFilesArchived; $intArchivedFile++) { |
78
|
|
|
$strArchivedFile = $classZip->getNameIndex($intArchivedFile); |
79
|
|
|
$strFileStats = $classZip->statIndex($intArchivedFile); |
80
|
|
|
$matches = []; |
81
|
|
|
preg_match('/^[0-9]{5,20}\.xml$/', $strArchivedFile, $matches, PREG_OFFSET_CAPTURE); |
82
|
|
|
$matches2 = []; |
83
|
|
|
preg_match('/^semnatura_[0-9]{5,20}\.xml$/', $strArchivedFile, $matches2, PREG_OFFSET_CAPTURE); |
84
|
|
|
if ($matches !== []) { |
85
|
|
|
$resInvoice = $classZip->getStream($strArchivedFile); |
86
|
|
|
$strInvoiceContent = stream_get_contents($resInvoice); |
87
|
|
|
fclose($resInvoice); |
88
|
|
|
$arrayToReturn = $this->setStandardizedFeedbackArray([ |
89
|
|
|
'Response_Index' => pathinfo($strFile)['filename'], |
90
|
|
|
'Size' => $strFileStats['size'], |
91
|
|
|
'FileDate' => date('Y-m-d H:i:s', $strFileStats['mtime']), |
92
|
|
|
'Matches' => $matches, |
93
|
|
|
'strArchivedFileName' => $strArchivedFile, |
94
|
|
|
'strInvoiceContent' => $strInvoiceContent, |
95
|
|
|
]); |
96
|
|
|
} elseif ($matches2 === []) { |
97
|
|
|
echo vsprintf('<div>' . $this->arrayConfiguration['Feedback']['DifferentFile'] . '</div>', [ |
98
|
|
|
$strArchivedFile, |
99
|
|
|
$strFile->getBasename(), |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
// @codeCoverageIgnoreStart |
105
|
|
|
throw new \RuntimeException(sprintf('Archive %s could not be opened!', $strFile)); |
106
|
|
|
// @codeCoverageIgnoreEnd |
107
|
|
|
} |
108
|
|
|
return $arrayToReturn; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function setDataSupplierOrCustomer(array $arrayData) |
112
|
|
|
{ |
113
|
|
|
$strCustomerCui = ''; |
114
|
|
|
if (isset($arrayData['PartyTaxScheme']['01']['CompanyID'])) { |
115
|
|
|
$strCustomerCui = $arrayData['PartyTaxScheme']['01']['CompanyID']; |
116
|
|
|
} else { |
117
|
|
|
$strCustomerCui = $arrayData['PartyLegalEntity']['CompanyID']; |
118
|
|
|
} |
119
|
|
|
if (is_numeric($strCustomerCui)) { |
120
|
|
|
$strCustomerCui = 'RO' . $strCustomerCui; |
121
|
|
|
} |
122
|
|
|
return $strCustomerCui; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function setDaysElapsed(string $strFirstDate, string $strLaterDate): string |
126
|
|
|
{ |
127
|
|
|
$origin = new \DateTimeImmutable($strFirstDate); |
128
|
|
|
$target = new \DateTimeImmutable($strLaterDate); |
129
|
|
|
$interval = $origin->diff($target); |
130
|
|
|
return $interval->format('%R%a'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function setStandardizedFeedbackArray(array $arrayData): array |
134
|
|
|
{ |
135
|
|
|
$arrayToReturn = [ |
136
|
|
|
'Response_Date' => '', |
137
|
|
|
'Response_Index' => $arrayData['Response_Index'], |
138
|
|
|
'Loading_Index' => '', |
139
|
|
|
'Size' => '', |
140
|
|
|
'Document_No' => '', |
141
|
|
|
'Issue_Date' => '', |
142
|
|
|
'Issue_YearMonth' => '', |
143
|
|
|
'Amount_wo_VAT' => '', |
144
|
|
|
'Amount_TOTAL' => '', |
145
|
|
|
'Amount_VAT' => '', |
146
|
|
|
'Supplier_CUI' => '', |
147
|
|
|
'Supplier_Name' => '', |
148
|
|
|
'Customer_CUI' => '', |
149
|
|
|
'Customer_Name' => '', |
150
|
|
|
'No_of_Lines' => '', |
151
|
|
|
'Error' => '', |
152
|
|
|
'Days_Between' => '', |
153
|
|
|
]; |
154
|
|
|
if ($arrayData['Size'] > 1000) { |
155
|
|
|
$arrayAttr = $this->getDocumentDetails($arrayData); |
156
|
|
|
$arrayToReturn['Loading_Index'] = substr($arrayData['Matches'][0][0], 0, -4); |
157
|
|
|
$arrayToReturn['Size'] = $arrayData['Size']; |
158
|
|
|
$arrayToReturn['Document_No'] = $arrayAttr['ID']; |
159
|
|
|
$arrayToReturn['Issue_Date'] = $arrayAttr['IssueDate']; |
160
|
|
|
$arrayToReturn['Issue_YearMonth'] = (new \IntlDateFormatter( |
161
|
|
|
$_GET['language_COUNTRY'], |
162
|
|
|
\IntlDateFormatter::FULL, |
163
|
|
|
\IntlDateFormatter::FULL, |
164
|
|
|
$this->translation->find(null, 'i18n_TimeZone')->getTranslation(), |
165
|
|
|
\IntlDateFormatter::GREGORIAN, |
166
|
|
|
'r-MM__MMMM' |
167
|
|
|
))->format(new \DateTime($arrayAttr['IssueDate'])); |
168
|
|
|
$arrayToReturn['Response_Date'] = $arrayData['FileDate']; |
169
|
|
|
$arrayToReturn['Amount_wo_VAT'] = $arrayAttr['wo_VAT']; |
170
|
|
|
$arrayToReturn['Amount_TOTAL'] = $arrayAttr['TOTAL']; |
171
|
|
|
$arrayToReturn['Amount_VAT'] = round(($arrayAttr['TOTAL'] - $arrayAttr['wo_VAT']), 2); |
172
|
|
|
$arrayToReturn['Supplier_CUI'] = $this->setDataSupplierOrCustomer($arrayAttr['Supplier']); |
173
|
|
|
$arrayToReturn['Supplier_Name'] = $arrayAttr['Supplier']['PartyLegalEntity']['RegistrationName']; |
174
|
|
|
$arrayToReturn['Customer_CUI'] = $this->setDataSupplierOrCustomer($arrayAttr['Customer']); |
175
|
|
|
$arrayToReturn['Customer_Name'] = $arrayAttr['Customer']['PartyLegalEntity']['RegistrationName']; |
176
|
|
|
$arrayToReturn['No_of_Lines'] = $arrayAttr['No_of_Lines']; |
177
|
|
|
$arrayToReturn['Days_Between'] = $this->setDaysElapsed($arrayAttr['IssueDate'], $arrayData['FileDate']); |
178
|
|
|
} elseif ($arrayData['Size'] > 0) { |
179
|
|
|
$objErrors = new \SimpleXMLElement($arrayData['strInvoiceContent']); |
180
|
|
|
$arrayToReturn['Loading_Index'] = $objErrors->attributes()->Index_incarcare->__toString(); |
181
|
|
|
$arrayToReturn['Size'] = $arrayData['Size']; |
182
|
|
|
$arrayToReturn['Response_Date'] = $arrayData['FileDate']; |
183
|
|
|
$arrayToReturn['Supplier_CUI'] = 'RO' . $objErrors->attributes()->Cif_emitent->__toString(); |
184
|
|
|
$arrayToReturn['Supplier_Name'] = '??????????'; |
185
|
|
|
$arrayToReturn['Error'] = '<div style="max-width:200px;font-size:0.8rem;">' |
186
|
|
|
. $objErrors->Error->attributes()->errorMessage->__toString() . '</div>'; |
187
|
|
|
} |
188
|
|
|
return $arrayToReturn; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|