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\Sunat; |
12
|
|
|
|
13
|
|
|
use DateTime; |
14
|
|
|
use F72X\Company; |
15
|
|
|
use F72X\Sunat\Catalogo; |
16
|
|
|
use F72X\Sunat\DocumentGenerator; |
17
|
|
|
/** |
18
|
|
|
* DataMap |
19
|
|
|
* |
20
|
|
|
* Esta clase es una representación interna de una factura o boleta, la cual se |
21
|
|
|
* encarga de aplicar toda la logica de negocio en cuanto a cálculos de tributos |
22
|
|
|
* y totales |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class DataMap { |
26
|
|
|
|
27
|
|
|
private $_rawData; |
28
|
|
|
private $documentType; |
29
|
|
|
private $currencyCode; |
30
|
|
|
private $documentId; |
31
|
|
|
private $documentName; |
32
|
|
|
private $documentSeries; |
33
|
|
|
private $documentNumber; |
34
|
|
|
|
35
|
|
|
/** @var DateTime */ |
36
|
|
|
private $issueDate; |
37
|
|
|
|
38
|
|
|
/** @var DateTime */ |
39
|
|
|
private $DueDate; |
40
|
|
|
private $operationType; |
41
|
|
|
private $customerDocType; |
42
|
|
|
private $customerDocNumber; |
43
|
|
|
private $customerRegName; |
44
|
|
|
private $customerAddress; |
45
|
|
|
private $purchaseOrder; |
46
|
|
|
|
47
|
|
|
/** @var InvoiceItems */ |
48
|
|
|
private $_items; |
49
|
|
|
private $_rawItems; |
50
|
|
|
private $allowancesAndCharges = []; |
51
|
|
|
|
52
|
|
|
/** NOTES FIELDS */ |
53
|
|
|
private $noteType; |
54
|
|
|
private $noteAffectedDocType; |
55
|
|
|
private $noteAffectedDocId; |
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* @param array $data |
59
|
|
|
* @param string $type 01|03|07|08 |
60
|
|
|
*/ |
61
|
|
|
public function __construct(array $data, $type) { |
62
|
|
|
// Items |
63
|
|
|
$items = new InvoiceItems(); |
64
|
|
|
$items->populate($data['items'], $data['currencyCode']); |
65
|
|
|
|
66
|
|
|
$this->_rawData = $data; |
67
|
|
|
$this->_rawItems = $data['items']; |
68
|
|
|
$this->setDefaults($data); |
69
|
|
|
$this->currencyCode = $data['currencyCode']; |
70
|
|
|
$this->documentType = $type; |
71
|
|
|
$this->documentSeries = DocumentGenerator::buildDocumentSeries($type, $data['affectedDocType'], $data['documentSeries']); |
72
|
|
|
$this->documentNumber = str_pad($data['documentNumber'], 8, '0', STR_PAD_LEFT); |
73
|
|
|
$this->documentName = Catalogo::getDocumentName($type); |
74
|
|
|
$this->documentId = $this->documentSeries . '-' . $this->documentNumber; |
75
|
|
|
$this->issueDate = new DateTime($data['issueDate']); |
76
|
|
|
$this->customerDocType = $data['customerDocType']; |
77
|
|
|
$this->customerDocNumber = $data['customerDocNumber']; |
78
|
|
|
$this->customerRegName = mb_strtoupper($data['customerRegName']); |
79
|
|
|
$this->customerAddress = mb_strtoupper($data['customerAddress']); |
80
|
|
|
$this->_items = $items; |
81
|
|
|
$this->allowancesAndCharges = $data['allowancesCharges']; |
82
|
|
|
$this->note = $data['note']; |
|
|
|
|
83
|
|
|
$this->setSpecificFields($data, $type); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function setSpecificFields(array $data, $type) { |
87
|
|
|
if (in_array($type, [Catalogo::DOCTYPE_FACTURA, Catalogo::DOCTYPE_BOLETA])) { |
88
|
|
|
$this->operationType = $data['operationType']; |
89
|
|
|
$this->purchaseOrder = $data['purchaseOrder']; |
90
|
|
|
} else { |
91
|
|
|
$this->noteType = $data['noteType']; |
92
|
|
|
$this->noteAffectedDocType = $data['affectedDocType']; |
93
|
|
|
$this->noteAffectedDocId = $data['affectedDocId']; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function setDefaults(array &$data) { |
98
|
|
|
$data['allowancesCharges'] = isset($data['allowancesCharges']) ? $data['allowancesCharges'] : []; |
99
|
|
|
$data['purchaseOrder'] = isset($data['purchaseOrder']) ? $data['purchaseOrder'] : null; |
100
|
|
|
$data['affectedDocType'] = isset($data['affectedDocType']) ? $data['affectedDocType'] : null; |
101
|
|
|
$data['note'] = isset($data['note']) ? $data['note'] : ''; |
102
|
|
|
} |
103
|
|
|
public function getNoteType() { |
104
|
|
|
return $this->noteType; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getNoteDescription() { |
108
|
|
|
return $this->note; |
109
|
|
|
} |
110
|
|
|
public function getNote() { |
111
|
|
|
return $this->note; |
112
|
|
|
} |
113
|
|
|
public function getNoteAffectedDocType() { |
114
|
|
|
return $this->noteAffectedDocType; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getNoteAffectedDocId() { |
118
|
|
|
return $this->noteAffectedDocId; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getDocumentId() { |
122
|
|
|
return $this->documentId; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getRawData() { |
126
|
|
|
return $this->_rawData; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Boleta o Factura @CAT1 |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getDocumentType() { |
134
|
|
|
return $this->documentType; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getCurrencyCode() { |
138
|
|
|
return $this->currencyCode; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getDocumentName() { |
142
|
|
|
return $this->documentName; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getDocumentSeries() { |
146
|
|
|
return $this->documentSeries; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function setDocumentSeries($documentSeries) { |
150
|
|
|
$this->documentSeries = $documentSeries; |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getDocumentNumber() { |
155
|
|
|
return $this->documentNumber; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function setDocumentNumber($documentNumber) { |
159
|
|
|
$this->documentNumber = $documentNumber; |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getIssueDate() { |
164
|
|
|
return $this->issueDate; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function setIssueDate(DateTime $IssueDate) { |
168
|
|
|
$this->issueDate = $IssueDate; |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getDueDate() { |
173
|
|
|
return $this->DueDate; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setDueDate(DateTime $DueDate) { |
177
|
|
|
$this->DueDate = $DueDate; |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getOperationType() { |
182
|
|
|
return $this->operationType; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setOperationType($operationType) { |
186
|
|
|
$this->operationType = $operationType; |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getCustomerDocType() { |
191
|
|
|
return $this->customerDocType; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function setCustomerDocType($customerDocType) { |
195
|
|
|
$this->customerDocType = $customerDocType; |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function getCustomerDocNumber() { |
200
|
|
|
return $this->customerDocNumber; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function setCustomerDocNumber($customerDocNumber) { |
204
|
|
|
$this->customerDocNumber = $customerDocNumber; |
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getCustomerRegName() { |
209
|
|
|
return $this->customerRegName; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function setCustomerRegName($customerRegName) { |
213
|
|
|
$this->customerRegName = $customerRegName; |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
public function getCustomerAddress() { |
217
|
|
|
return $this->customerAddress; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function setCustomerAddress($customerAddress) { |
221
|
|
|
$this->customerAddress = $customerAddress; |
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function getPurchaseOrder() { |
226
|
|
|
return $this->purchaseOrder; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function setPurchaseOrder($purchaseOrder) { |
230
|
|
|
$this->purchaseOrder = $purchaseOrder; |
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* |
236
|
|
|
* @return InvoiceItems |
237
|
|
|
*/ |
238
|
|
|
public function getItems() { |
239
|
|
|
return $this->_items; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Numero de items del documento |
244
|
|
|
* @return int |
245
|
|
|
*/ |
246
|
|
|
public function getTotalItems() { |
247
|
|
|
return $this->_items->getCount(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function getRawItems() { |
251
|
|
|
return $this->_rawItems; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function getAllowancesAndCharges() { |
255
|
|
|
return $this->allowancesAndCharges; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function setAllowancesAndCharges($allowancesAndCharges) { |
259
|
|
|
$this->allowancesAndCharges = $allowancesAndCharges; |
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Base imponible |
265
|
|
|
* |
266
|
|
|
* Formula = SUM(BASE_IMPONIBLE_X_ITEM) - DESCUENTOS_GLOBALES + CARGOS_GLOBALES |
267
|
|
|
* |
268
|
|
|
* @return float |
269
|
|
|
*/ |
270
|
|
|
public function getTaxableAmount() { |
271
|
|
|
$totalItems = $this->_items->getTotalTaxableOperations(); |
272
|
|
|
return $this->applyAllowancesAndCharges($totalItems); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Monto con impuestos |
277
|
|
|
* |
278
|
|
|
* Formula = BASE_IMPONIBLE + IGV |
279
|
|
|
* |
280
|
|
|
* @return float |
281
|
|
|
*/ |
282
|
|
|
public function getTaxedAmount() { |
283
|
|
|
$totalTaxableAmount = $this->getTaxableAmount(); |
284
|
|
|
$totalIgv = $this->getIGV(); |
285
|
|
|
return $totalTaxableAmount + $totalIgv; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Total operaciones gravadas |
290
|
|
|
* @return float |
291
|
|
|
*/ |
292
|
|
|
public function getTotalTaxableOperations() { |
293
|
|
|
return $this->getTaxableAmount(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Total operaciones gratuitas |
298
|
|
|
* @return float |
299
|
|
|
*/ |
300
|
|
|
public function getTotalFreeOperations() { |
301
|
|
|
return $this->_items->getTotalFreeOperations(); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Total operationes exoneradas |
306
|
|
|
* |
307
|
|
|
* Formula = SUM(EXEMPTED_OPERATIONS_X_ITEM) - DESCUENTOS_GLOBALES + CARGOS_GLOBALES |
308
|
|
|
* |
309
|
|
|
* @return float |
310
|
|
|
*/ |
311
|
|
|
public function getTotalExemptedOperations() { |
312
|
|
|
$totalItems = $this->_items->getTotalExemptedOperations(); |
313
|
|
|
return $this->applyAllowancesAndCharges($totalItems); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Total operaciones inafectas |
318
|
|
|
* @return float |
319
|
|
|
*/ |
320
|
|
|
public function getTotalUnaffectedOperations() { |
321
|
|
|
$totalItems = $this->_items->getTotalUnaffectedOperations(); |
322
|
|
|
return $this->applyAllowancesAndCharges($totalItems); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Valor de venta |
327
|
|
|
* |
328
|
|
|
* Valor total de la factura sin considerar descuentos impuestos u otros tributos |
329
|
|
|
* @return float |
330
|
|
|
*/ |
331
|
|
|
public function getBillableValue() { |
332
|
|
|
return $this->_items->getTotalBillableValue(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Total descuentos |
337
|
|
|
* |
338
|
|
|
* Formula: SUM(DESCUENTOS_X_ITEM) + DESCUENTOS_GLOBALES |
339
|
|
|
* @return float |
340
|
|
|
*/ |
341
|
|
|
public function getTotalAllowances() { |
342
|
|
|
$totalItems = $this->_items->getTotalAllowances(); |
343
|
|
|
$totalTaxableAmountItems = $this->_items->getTotalTaxableAmount(); |
344
|
|
|
$globalAllowancesAmount = Operations::getTotalAllowances($totalTaxableAmountItems, $this->allowancesAndCharges); |
345
|
|
|
return $totalItems + $globalAllowancesAmount; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Total a pagar |
350
|
|
|
* |
351
|
|
|
* El importe que el usuario está obligado a pagar |
352
|
|
|
* |
353
|
|
|
* Formula = OPERACIONES_GRAVADAS + IGV + OPERACIONES_EXONERADAS + OPERACIONES_INAFECTAS |
354
|
|
|
* |
355
|
|
|
* @return float |
356
|
|
|
*/ |
357
|
|
|
public function getPayableAmount() { |
358
|
|
|
// Totals |
359
|
|
|
$totalTaxableOperations = $this->getTotalTaxableOperations(); |
360
|
|
|
$totalIGV = $this->getIGV(); |
361
|
|
|
$totalExemptedOperations = $this->getTotalExemptedOperations(); |
362
|
|
|
return $totalTaxableOperations + $totalIGV + $totalExemptedOperations; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* |
367
|
|
|
* Total impuestos |
368
|
|
|
* |
369
|
|
|
* Fórmula: IGV + ISC + IVAP |
370
|
|
|
* |
371
|
|
|
* @return float |
372
|
|
|
*/ |
373
|
|
|
public function getTotalTaxes() { |
374
|
|
|
$IGV = $this->getIGV(); |
375
|
|
|
$ISC = $this->getISC(); |
376
|
|
|
$IVAP = $this->getIVAP(); |
377
|
|
|
return $IGV + $ISC + $IVAP; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* IGV |
382
|
|
|
* @return float |
383
|
|
|
*/ |
384
|
|
|
public function getIGV() { |
385
|
|
|
$baseAmount = $this->getTaxableAmount(); |
386
|
|
|
return Operations::calcIGV($baseAmount); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* ISC |
391
|
|
|
* @IMP |
392
|
|
|
* @return float |
393
|
|
|
*/ |
394
|
|
|
public function getISC() { |
395
|
|
|
return Operations::calcISC(); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* IVAP |
400
|
|
|
* @IMP |
401
|
|
|
* @return float |
402
|
|
|
*/ |
403
|
|
|
public function getIVAP() { |
404
|
|
|
return Operations::calcIVAP(); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* |
409
|
|
|
* @param float $amount |
410
|
|
|
* @return float |
411
|
|
|
*/ |
412
|
|
|
private function applyAllowancesAndCharges($amount) { |
413
|
|
|
return Operations::applyAllowancesAndCharges($amount, $this->allowancesAndCharges); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
public function getBillName() { |
417
|
|
|
return Company::getRUC() . '-' . $this->documentType . '-' . $this->documentId; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
} |
421
|
|
|
|