1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MÓDULO DE EMISIÓN ELECTRÓNICA F72X |
5
|
|
|
* UBL 2.1 |
6
|
|
|
* Version 1.1 |
7
|
|
|
* |
8
|
|
|
* Copyright 2018, Jaime Cruz |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace F72X; |
12
|
|
|
|
13
|
|
|
use ZipArchive; |
14
|
|
|
use F72X\Company; |
15
|
|
|
use F72X\Exception\FileException; |
16
|
|
|
|
17
|
|
|
class Repository { |
18
|
|
|
|
19
|
|
|
public static function saveBill($billName, $billContent) { |
20
|
|
|
self::saveFile("bill/$billName.xml", $billContent); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* @param string $billName |
26
|
|
|
* @param boolean $throwEx set to *false*, to avoid an exception if the file doesn't exist |
27
|
|
|
*/ |
28
|
|
|
public static function removeBillDocs($billName, $throwEx = true) { |
29
|
|
|
$rp = self::getRepositoryPath(); |
30
|
|
|
self::removeFile("$rp/bill/$billName.xml", $throwEx); |
31
|
|
|
self::removeFile("$rp/billinput/$billName.json", $throwEx); |
32
|
|
|
self::removeFile("$rp/signedbill/S-$billName.xml", $throwEx); |
33
|
|
|
self::removeFile("$rp/zippedbill/$billName.zip", $throwEx); |
34
|
|
|
self::removeFile("$rp/printable/$billName.pdf", $throwEx); |
35
|
|
|
self::removeFile("$rp/cdr/R$billName.zip", $throwEx); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function saveBillInput($billName, $billContent) { |
39
|
|
|
self::saveFile("billinput/$billName.json", $billContent); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function saveSignedBill($billName, $billContent) { |
43
|
|
|
self::saveFile("signedbill/S-$billName.xml", $billContent); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function saveCdr($billName, $billContent) { |
47
|
|
|
self::saveFile("cdr/R$billName.zip", $billContent); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function savePDF($billName, $fileContent) { |
51
|
|
|
self::saveFile("printable/$billName.pdf", $fileContent); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function zipBill($billName) { |
55
|
|
|
$rp = self::getRepositoryPath(); |
56
|
|
|
$zip = new ZipArchive(); |
57
|
|
|
if ($zip->open("$rp/zippedbill/$billName.zip", ZipArchive::CREATE) === TRUE) { |
58
|
|
|
$zip->addFile("$rp/signedbill/S-$billName.xml", "$billName.xml"); |
59
|
|
|
$zip->close(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function billExist($billName) { |
64
|
|
|
$rp = self::getRepositoryPath(); |
65
|
|
|
return fileExists("$rp/bill/$billName.xml"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function cdrExist($billName) { |
69
|
|
|
$rp = self::getRepositoryPath(); |
70
|
|
|
return fileExists("$rp/cdr/R$billName.zip"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function getRepositoryPath() { |
74
|
|
|
return Company::getRepositoryPath(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function getBillPath($billName) { |
78
|
|
|
$rp = self::getRepositoryPath(); |
79
|
|
|
return "$rp/bill/$billName.xml"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function getBillInputPath($billName) { |
83
|
|
|
$rp = self::getRepositoryPath(); |
84
|
|
|
return "$rp/billinput/$billName.json"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function getSignedBillPath($billName) { |
88
|
|
|
$rp = self::getRepositoryPath(); |
89
|
|
|
return "$rp/signedbill/S-$billName.xml"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function getZippedBillPath($billName) { |
93
|
|
|
$rp = self::getRepositoryPath(); |
94
|
|
|
return "$rp/zippedbill/$billName.zip"; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function getZippedBillContent($billName) { |
98
|
|
|
return file_get_contents(self::getZippedBillPath($billName)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function getPdfPath($billName) { |
102
|
|
|
$rp = self::getRepositoryPath(); |
103
|
|
|
return "$rp/printable/$billName.pdf"; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private static function saveFile($filePath, $fileContent) { |
107
|
|
|
$rp = self::getRepositoryPath(); |
108
|
|
|
self::writeFile("$rp/$filePath", $fileContent); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public static function removeFile($filePath, $throwEx = true) { |
112
|
|
|
if (!file_exists($filePath)) { |
113
|
|
|
if ($throwEx) { |
114
|
|
|
throw new FileException("El archivo: $filePath no existe."); |
115
|
|
|
} |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
unlink($filePath); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function getCdrInfo($billName) { |
122
|
|
|
$rp = self::getRepositoryPath(); |
123
|
|
|
$zip = new ZipArchive(); |
124
|
|
|
$info = null; |
|
|
|
|
125
|
|
|
if ($zip->open("$rp/cdr/R$billName.zip") === true) { |
126
|
|
|
$xmlString = $zip->getFromName("R-$billName.xml"); |
127
|
|
|
$info = self::getMapCdr($xmlString); |
128
|
|
|
$zip->close(); |
129
|
|
|
} else { |
130
|
|
|
throw new FileException("No se encontró el archivo R$billName.zip"); |
131
|
|
|
} |
132
|
|
|
return $info; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private static function getMapCdr($xmlString) { |
136
|
|
|
$xmlStringI1 = str_replace(['ar:', 'ext:', 'cac:', 'cbc:'], '', $xmlString); |
137
|
|
|
$SimpleXml = simplexml_load_string($xmlStringI1); |
138
|
|
|
$origin = json_decode(json_encode($SimpleXml), 1); |
139
|
|
|
$respNode = $origin['DocumentResponse']; |
140
|
|
|
return [ |
141
|
|
|
'id' => $origin['ID'], |
142
|
|
|
'invoiceId' => $respNode['DocumentReference']['ID'], |
143
|
|
|
'receiverId' => $respNode['RecipientParty']['PartyIdentification']['ID'], |
144
|
|
|
'issueDate' => $origin['IssueDate'], |
145
|
|
|
'issueTime' => $origin['IssueTime'], |
146
|
|
|
'responseDate' => $origin['ResponseDate'], |
147
|
|
|
'responseTime' => $origin['ResponseTime'], |
148
|
|
|
'responseCode' => $respNode['Response']['ResponseCode'], |
149
|
|
|
'responseDesc' => $respNode['Response']['Description'] |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public static function xmlStream($billName) { |
154
|
|
|
$filePath = self::getBillPath($billName); |
155
|
|
|
if (file_exists($filePath)) { |
156
|
|
|
header('Content-Type: application/xml'); |
157
|
|
|
header("Content-Disposition: attachment;filename=$billName.xml"); |
158
|
|
|
header('Cache-Control:max-age=0'); |
159
|
|
|
echo file_get_contents($filePath); |
160
|
|
|
exit(); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
throw new FileException("El archivo: $filePath no existe."); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public static function signedXmlStream($billName) { |
166
|
|
|
$filePath = self::getSignedBillPath($billName); |
167
|
|
|
if (file_exists($filePath)) { |
168
|
|
|
header('Content-Type: application/xml'); |
169
|
|
|
header("Content-Disposition: attachment;filename=S-$billName.xml"); |
170
|
|
|
header('Cache-Control:max-age=0'); |
171
|
|
|
echo file_get_contents($filePath); |
172
|
|
|
exit(); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
throw new FileException("El archivo: $filePath no existe."); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public static function pdfStream($billName, $browserView = false) { |
178
|
|
|
$filePath = self::getPdfPath($billName); |
179
|
|
|
if (file_exists($filePath)) { |
180
|
|
|
header('Content-Type: application/pdf'); |
181
|
|
|
if(!$browserView){ |
182
|
|
|
header("Content-Disposition: attachment;filename=$billName.pdf"); |
183
|
|
|
} |
184
|
|
|
header('Cache-Control:max-age=0'); |
185
|
|
|
echo file_get_contents($filePath); |
186
|
|
|
exit(); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
throw new FileException("El archivo: $filePath no existe."); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public static function billInputStream($billName) { |
192
|
|
|
$filePath = self::getBillInputPath($billName); |
193
|
|
|
if (file_exists($filePath)) { |
194
|
|
|
header('Content-Type: application/json'); |
195
|
|
|
header("Content-Disposition: attachment;filename=$billName.json"); |
196
|
|
|
header('Cache-Control:max-age=0'); |
197
|
|
|
echo file_get_contents($filePath); |
198
|
|
|
exit(); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
throw new FileException("El archivo: $filePath no existe."); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public static function writeFile($filePath, $fileContent, $overwrite = false) { |
204
|
|
|
if (file_exists($filePath) && !$overwrite) { |
205
|
|
|
throw new FileException("El archivo: $filePath ya existe."); |
206
|
|
|
} |
207
|
|
|
file_put_contents($filePath, $fileContent); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|