|
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 InvalidArgumentException; |
|
14
|
|
|
use DOMDocument; |
|
15
|
|
|
use F72X\Company; |
|
16
|
|
|
use F72X\Exception\ConfigException; |
|
17
|
|
|
use Sabre\Xml\Reader; |
|
18
|
|
|
|
|
19
|
|
|
class Catalogo { |
|
20
|
|
|
|
|
21
|
|
|
const CAT_TAX_DOCUMENT_TYPE = 1; |
|
22
|
|
|
const CAT_CURRENCY_TYPE = 2; |
|
23
|
|
|
const CAT_MEASUREMENT_UNIT = 3; |
|
24
|
|
|
const CAT_COUNTRY_CODE = 4; |
|
25
|
|
|
const CAT_TAX_TYPE = 5; |
|
26
|
|
|
const CAT_IDENT_DOCUMENT_TYPE = 6; |
|
27
|
|
|
const CAT_IGV_AFFECTATION_TYPE = 7; |
|
28
|
|
|
const CAT_ISC_CALC_SYSTEM_TYPE = 8; |
|
29
|
|
|
const CAT_NOTA_CREDITO_TYPE = 9; |
|
30
|
|
|
const CAT_NOTA_DEBITO_TYPE = 10; |
|
31
|
|
|
const CAT_FACTURA_TYPE = 51; |
|
32
|
|
|
/** @CAT1 Código tipo de documento */ |
|
33
|
|
|
const DOCTYPE_FACTURA = '01'; |
|
34
|
|
|
const DOCTYPE_BOLETA = '03'; |
|
35
|
|
|
const DOCTYPE_NOTA_CREDITO = '07'; |
|
36
|
|
|
const DOCTYPE_NOTA_DEBITO = '08'; |
|
37
|
|
|
const DOCTYPE_SC_FACTURA = 'FAC'; |
|
38
|
|
|
const DOCTYPE_SC_BOLETA = 'BOL'; |
|
39
|
|
|
const DOCTYPE_SC_NOTA_CREDITO = 'NCR'; |
|
40
|
|
|
const DOCTYPE_SC_NOTA_DEBITO = 'NDE'; |
|
41
|
|
|
/** @CAT5 Tipo de impuesto*/ |
|
42
|
|
|
const CAT5_IGV = '1000'; |
|
43
|
|
|
const CAT5_IVAP = '1016'; |
|
44
|
|
|
const CAT5_ISC = '2000'; |
|
45
|
|
|
const CAT5_EXP = '9995'; |
|
46
|
|
|
const CAT5_GRA = '9996'; |
|
47
|
|
|
const CAT5_EXO = '9997'; |
|
48
|
|
|
const CAT5_INA = '9998'; |
|
49
|
|
|
const CAT5_OTROS = '9999'; |
|
50
|
|
|
|
|
51
|
|
|
/** @CAT7 Tipo de afectación del IGV */ |
|
52
|
|
|
const CAT7_GRA_IGV = '10'; |
|
53
|
|
|
/** @CAT16 Tipo de precio */ |
|
54
|
|
|
const CAT16_UNITARY_PRICE = '01'; |
|
55
|
|
|
const CAT16_REF_VALUE = '02'; |
|
56
|
|
|
|
|
57
|
|
|
/** @CAT6*/ |
|
58
|
|
|
const IDENTIFICATION_DOC_DNI = '1'; |
|
59
|
|
|
const IDENTIFICATION_DOC_RUC = '6'; |
|
60
|
|
|
|
|
61
|
|
|
private static $_CAT = []; |
|
62
|
|
|
private static $_LIST = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $documentType 01|03|07|08 |
|
67
|
|
|
* @return string FACTURA|BOLETA|NOTA DE CRÉDITO|NOTA DE DÉBITO |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function getDocumentName($documentType) { |
|
70
|
|
|
switch ($documentType) { |
|
71
|
|
|
case self::DOCTYPE_FACTURA : return 'FACTURA'; |
|
72
|
|
|
case self::DOCTYPE_BOLETA : return 'BOLETA'; |
|
73
|
|
|
case self::DOCTYPE_NOTA_CREDITO : return 'NOTA DE CRÉDITO'; |
|
74
|
|
|
case self::DOCTYPE_NOTA_DEBITO : return 'NOTA DE DÉBITO'; |
|
75
|
|
|
} |
|
76
|
|
|
throw new InvalidArgumentException("Error: $documentType isn't valid document type"); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $shortCode BOL|FAC|NCR|NDE |
|
82
|
|
|
* @return string 01|03|07|08 |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function getDocumentType($shortCode) { |
|
85
|
|
|
switch ($shortCode) { |
|
86
|
|
|
case self::DOCTYPE_SC_FACTURA: return self::DOCTYPE_FACTURA ; |
|
87
|
|
|
case self::DOCTYPE_SC_BOLETA: return self::DOCTYPE_BOLETA; |
|
88
|
|
|
case self::DOCTYPE_SC_NOTA_CREDITO: return self::DOCTYPE_NOTA_CREDITO; |
|
89
|
|
|
case self::DOCTYPE_SC_NOTA_DEBITO: return self::DOCTYPE_NOTA_DEBITO; |
|
90
|
|
|
} |
|
91
|
|
|
throw new InvalidArgumentException("Error: $shortCode isn't valid short code"); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public static function itemExist($catNumber, $itemID) { |
|
95
|
|
|
$items = self::getCatItems($catNumber); |
|
96
|
|
|
return key_exists($itemID, $items); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function getCatItem($catNumber, $itemID, $key = 'id') { |
|
100
|
|
|
$items = self::getCatItems($catNumber); |
|
101
|
|
|
foreach ($items as $item) { |
|
102
|
|
|
if ($item[$key] === strval($itemID)) { |
|
103
|
|
|
return $item; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public static function getCatItems($catNumber) { |
|
110
|
|
|
// returns from cache |
|
111
|
|
|
if (isset(self::$_CAT['CAT_' . $catNumber])) { |
|
112
|
|
|
return self::$_CAT['CAT_' . $catNumber]; |
|
113
|
|
|
} |
|
114
|
|
|
// Load the XML |
|
115
|
|
|
$xmlName = self::getXmlName($catNumber); |
|
116
|
|
|
$doc = new DOMDocument(); |
|
117
|
|
|
$doc->load(SunatVars::DIR_CATS . "/$xmlName"); |
|
118
|
|
|
|
|
119
|
|
|
$reader = new Reader(); |
|
120
|
|
|
$reader->xml($doc->saveXML()); |
|
121
|
|
|
|
|
122
|
|
|
$catData = $reader->parse(); |
|
123
|
|
|
$items = $catData['value']; |
|
124
|
|
|
$itemsO = []; |
|
125
|
|
|
foreach ($items as &$item) { |
|
126
|
|
|
unset($item['name']); // Here because the item may contain the name attribute! |
|
127
|
|
|
foreach ($item['attributes'] as $attKey => $att) { |
|
128
|
|
|
$item[$attKey] = $att; |
|
129
|
|
|
} |
|
130
|
|
|
unset($item['attributes']); |
|
131
|
|
|
$itemsO[$item['id']] = $item; |
|
132
|
|
|
} |
|
133
|
|
|
// Cache |
|
134
|
|
|
self::$_CAT['CAT_' . $catNumber] = $itemsO; |
|
135
|
|
|
return self::$_CAT['CAT_' . $catNumber]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private static function getXmlName($catNumeber) { |
|
139
|
|
|
return 'cat_' . str_pad($catNumeber, 2, '0', STR_PAD_LEFT) . '.xml'; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public static function getCurrencyPlural($currencyCode) { |
|
143
|
|
|
$currencies = self::getCustomList('currencies'); |
|
144
|
|
|
if(isset($currencies[$currencyCode])){ |
|
145
|
|
|
return $currencies[$currencyCode]; |
|
146
|
|
|
} |
|
147
|
|
|
throw new ConfigException("El código de moneda $currencyCode aún no ha sido configurado para su uso."); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public static function getUnitName($unitCode) { |
|
151
|
|
|
return self::getCustomListItem('unitcode', $unitCode); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public static function getCustomListItem($listName, $itemId) { |
|
155
|
|
|
$customList = self::getCustomList($listName); |
|
156
|
|
|
if(isset($customList[$itemId])){ |
|
157
|
|
|
return $customList[$itemId]; |
|
158
|
|
|
} |
|
159
|
|
|
throw new ConfigException("El codigó de item $itemId no existe en la lista $listName"); |
|
160
|
|
|
} |
|
161
|
|
|
public static function getCustomList($listName) { |
|
162
|
|
|
// returns from cache |
|
163
|
|
|
if (isset(self::$_LIST['LIST_' . $listName])) { |
|
164
|
|
|
return self::$_LIST['LIST_' . $listName]; |
|
165
|
|
|
} |
|
166
|
|
|
$path = Company::getListsPath(); |
|
167
|
|
|
$fileName = "$path/$listName.php"; |
|
168
|
|
|
if(file_exists($fileName)){ |
|
169
|
|
|
$path = Company::getListsPath(); |
|
170
|
|
|
$list = require $fileName; |
|
171
|
|
|
// Cache |
|
172
|
|
|
self::$_CAT['LIST_' . $listName] = $list; |
|
173
|
|
|
return $list; |
|
174
|
|
|
} |
|
175
|
|
|
throw new ConfigException("No se encontró el archivo $listName.php dentro de su directorio de listas personalizadas."); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public static function catItemsToPhpArray($catNumber, $resultPath) { |
|
179
|
|
|
$items = Catalogo::getCatItems($catNumber); |
|
180
|
|
|
$lines = []; |
|
181
|
|
|
$ENTER = chr(13) . chr(10); |
|
182
|
|
|
foreach ($items as $item) { |
|
183
|
|
|
$lines[] = " ['id' => '" . $item['id'] . "', 'value' => '" . $item['value'] . "']"; |
|
184
|
|
|
} |
|
185
|
|
|
$joinedLines = implode(',' . $ENTER, $lines); |
|
186
|
|
|
$result = "[" . $ENTER . $joinedLines . $ENTER . "]"; |
|
187
|
|
|
file_put_contents($resultPath, $result); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|