| Total Complexity | 47 |
| Total Lines | 247 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Catalogo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Catalogo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Catalogo { |
||
| 19 | |||
| 20 | const CAT_TAX_DOCUMENT_TYPE = 1; |
||
| 21 | const CAT_CURRENCY_TYPE = 2; |
||
| 22 | const CAT_MEASUREMENT_UNIT = 3; |
||
| 23 | const CAT_COUNTRY_CODE = 4; |
||
| 24 | const CAT_TAX_TYPE = 5; |
||
| 25 | const CAT_IDENT_DOCUMENT_TYPE = 6; |
||
| 26 | const CAT_IGV_AFFECTATION_TYPE = 7; |
||
| 27 | const CAT_ISC_CALC_SYSTEM_TYPE = 8; |
||
| 28 | const CAT_NOTA_CREDITO_TYPE = 9; |
||
| 29 | const CAT_NOTA_DEBITO_TYPE = 10; |
||
| 30 | const CAT_PERCEPCION_REGIME = 22; |
||
| 31 | const CAT_RETENCION_REGIME = 23; |
||
| 32 | const CAT_FACTURA_TYPE = 51; |
||
| 33 | |||
| 34 | /** @CAT1 Código tipo de documento */ |
||
| 35 | const DOCTYPE_FACTURA = '01'; |
||
| 36 | const DOCTYPE_BOLETA = '03'; |
||
| 37 | const DOCTYPE_NOTA_CREDITO = '07'; |
||
| 38 | const DOCTYPE_NOTA_DEBITO = '08'; |
||
| 39 | const DOCTYPE_VOUCHER = '12'; |
||
| 40 | const DOCTYPE_SC_FACTURA = 'FAC'; |
||
| 41 | const DOCTYPE_SC_BOLETA = 'BOL'; |
||
| 42 | const DOCTYPE_SC_NOTA_CREDITO = 'NCR'; |
||
| 43 | const DOCTYPE_SC_NOTA_DEBITO = 'NDE'; |
||
| 44 | |||
| 45 | /** @CAT5 Tipo de impuesto */ |
||
| 46 | const CAT5_IGV = '1000'; |
||
| 47 | const CAT5_IVAP = '1016'; |
||
| 48 | const CAT5_ISC = '2000'; |
||
| 49 | const CAT5_EXP = '9995'; |
||
| 50 | const CAT5_GRA = '9996'; |
||
| 51 | const CAT5_EXO = '9997'; |
||
| 52 | const CAT5_INA = '9998'; |
||
| 53 | const CAT5_OTROS = '9999'; |
||
| 54 | |||
| 55 | /** @CAT7 Tipo de afectación del IGV */ |
||
| 56 | const CAT7_GRA_IGV = '10'; |
||
| 57 | |||
| 58 | /** @CAT16 Tipo de precio */ |
||
| 59 | const CAT16_UNITARY_PRICE = '01'; |
||
| 60 | const CAT16_REF_VALUE = '02'; |
||
| 61 | |||
| 62 | /** @CAT6 */ |
||
| 63 | const IDENTIFICATION_DOC_DNI = '1'; |
||
| 64 | const IDENTIFICATION_DOC_RUC = '6'; |
||
| 65 | |||
| 66 | private static $_CAT = []; |
||
| 67 | private static $_LIST = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * |
||
| 71 | * @param string $documentType 01|03|07|08 |
||
| 72 | * @param string $affectedDocumentType 01|03 |
||
| 73 | * @return string F|B |
||
| 74 | */ |
||
| 75 | public static function getDocumentSeriesPrefix($documentType, $affectedDocumentType = null) { |
||
| 76 | if ($documentType == self::DOCTYPE_FACTURA) { |
||
| 77 | return 'F'; |
||
| 78 | } |
||
| 79 | if ($documentType == self::DOCTYPE_BOLETA) { |
||
| 80 | return 'B'; |
||
| 81 | } |
||
| 82 | if ($documentType == self::DOCTYPE_NOTA_CREDITO) { |
||
| 83 | return self::getDocumentSeriesPrefix($affectedDocumentType); |
||
| 84 | } |
||
| 85 | if ($documentType == self::DOCTYPE_NOTA_DEBITO) { |
||
| 86 | return self::getDocumentSeriesPrefix($affectedDocumentType); |
||
| 87 | } |
||
| 88 | if ($documentType == self::DOCTYPE_VOUCHER) { |
||
| 89 | throw new InvalidArgumentException("Error: Cash register ticket isn't supported yet."); |
||
| 90 | } |
||
| 91 | throw new InvalidArgumentException("Error: $documentType isn't a valid document type"); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * |
||
| 96 | * @param string $documentType 01|03|07|08 |
||
| 97 | * @return string FACTURA|BOLETA|NOTA DE CRÉDITO|NOTA DE DÉBITO |
||
| 98 | */ |
||
| 99 | public static function getOfficialDocumentName($documentType) { |
||
| 100 | switch ($documentType) { |
||
| 101 | case self::DOCTYPE_FACTURA : return 'FACTURA'; |
||
| 102 | case self::DOCTYPE_BOLETA : return 'BOLETA DE VENTA'; |
||
| 103 | case self::DOCTYPE_NOTA_CREDITO : return 'NOTA DE CRÉDITO'; |
||
| 104 | case self::DOCTYPE_NOTA_DEBITO : return 'NOTA DE DÉBITO'; |
||
| 105 | } |
||
| 106 | throw new InvalidArgumentException("Error: $documentType isn't a valid document type"); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * |
||
| 111 | * @param string $shortCode BOL|FAC|NCR|NDE |
||
| 112 | * @return string 01|03|07|08 |
||
| 113 | */ |
||
| 114 | public static function getDocumentType($shortCode) { |
||
| 115 | switch ($shortCode) { |
||
| 116 | case self::DOCTYPE_SC_FACTURA: return self::DOCTYPE_FACTURA; |
||
| 117 | case self::DOCTYPE_SC_BOLETA: return self::DOCTYPE_BOLETA; |
||
| 118 | case self::DOCTYPE_SC_NOTA_CREDITO: return self::DOCTYPE_NOTA_CREDITO; |
||
| 119 | case self::DOCTYPE_SC_NOTA_DEBITO: return self::DOCTYPE_NOTA_DEBITO; |
||
| 120 | } |
||
| 121 | throw new InvalidArgumentException("Error: $shortCode isn't valid short code use (BOL|FAC|NCR|NDE)"); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * |
||
| 126 | * @param string $docType 01|03|07|08 |
||
| 127 | * @return string BOL|FAC|NCR|NDE |
||
| 128 | */ |
||
| 129 | public static function getDocumentShortCode($docType) { |
||
| 130 | switch ($docType) { |
||
| 131 | case self::DOCTYPE_FACTURA: return self::DOCTYPE_SC_FACTURA; |
||
| 132 | case self::DOCTYPE_BOLETA: return self::DOCTYPE_SC_BOLETA; |
||
| 133 | case self::DOCTYPE_NOTA_CREDITO: return self::DOCTYPE_SC_NOTA_CREDITO; |
||
| 134 | case self::DOCTYPE_NOTA_DEBITO: return self::DOCTYPE_SC_NOTA_DEBITO; |
||
| 135 | } |
||
| 136 | throw new InvalidArgumentException("Error: $docType isn't valid document type use (01|03|07|08)"); |
||
| 137 | } |
||
| 138 | |||
| 139 | public static function itemExist($catNumber, $itemID) { |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Valor de item de catálogo |
||
| 146 | * @param int $catNumber |
||
| 147 | * @param string $itemID |
||
| 148 | * @return string |
||
| 149 | * @throws InvalidArgumentException cuando el item no existe en el catálogo. |
||
| 150 | */ |
||
| 151 | public static function getCatItemValue($catNumber, $itemID) { |
||
| 152 | $item = self::getCatItem($catNumber, $itemID); |
||
| 153 | if ($item) { |
||
| 154 | return $item['value']; |
||
| 155 | } |
||
| 156 | throw new InvalidArgumentException("Catálogo Error, no existe un item con ID='$itemID' en el cátalogo='$catNumber'"); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Valor de item de catálogo |
||
| 161 | * @param int $catNumber |
||
| 162 | * @param string $itemID |
||
| 163 | * @return string |
||
| 164 | * @throws InvalidArgumentException cuando el item no existe en el catálogo. |
||
| 165 | */ |
||
| 166 | public static function getCatItemFieldValue($catNumber, $itemID, $field) { |
||
| 167 | $item = self::getCatItem($catNumber, $itemID); |
||
| 168 | if ($item) { |
||
| 169 | if(isset($item[$field])){ |
||
| 170 | return $item[$field]; |
||
| 171 | } |
||
| 172 | throw new InvalidArgumentException("Catálogo Error, no existe el campo '$field' en el cátalogo='$catNumber'"); |
||
| 173 | } |
||
| 174 | throw new InvalidArgumentException("Catálogo Error, no existe un item con ID='$itemID' en el cátalogo='$catNumber'"); |
||
| 175 | } |
||
| 176 | |||
| 177 | public static function getCatItem($catNumber, $itemID, $key = 'id') { |
||
| 178 | $items = self::getCatItems($catNumber); |
||
| 179 | foreach ($items as $item) { |
||
| 180 | if ($item[$key] === strval($itemID)) { |
||
| 181 | return $item; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | return null; |
||
| 185 | } |
||
| 186 | |||
| 187 | public static function getCatItems($catNumber, $useXmlFiles = false) { |
||
| 188 | // returns from cache |
||
| 189 | if (isset(self::$_CAT['CAT_' . $catNumber])) { |
||
| 190 | return self::$_CAT['CAT_' . $catNumber]; |
||
| 191 | } |
||
| 192 | // Load from file |
||
| 193 | $fileName = strtoupper(self::getCatFileName($catNumber)); |
||
| 194 | $items = require F72X::getCatalogoSunatDir() . "/$fileName.php"; |
||
| 195 | self::$_CAT['CAT_' . $catNumber] = $items; |
||
| 196 | return $items; |
||
| 197 | } |
||
| 198 | |||
| 199 | private static function getCatFileName($catNumeber) { |
||
| 200 | return 'cat_' . str_pad($catNumeber, 2, '0', STR_PAD_LEFT); |
||
| 201 | } |
||
| 202 | |||
| 203 | public static function getCurrencyPlural($currencyCode) { |
||
| 204 | $currencies = self::getCustomList('currencies'); |
||
| 205 | if (isset($currencies[$currencyCode])) { |
||
| 206 | return $currencies[$currencyCode]; |
||
| 207 | } |
||
| 208 | throw new ConfigException("El código de moneda $currencyCode aún no ha sido configurado para su uso."); |
||
| 209 | } |
||
| 210 | |||
| 211 | public static function getUnitName($unitCode) { |
||
| 213 | } |
||
| 214 | |||
| 215 | public static function getCustomListItem($listName, $itemId) { |
||
| 216 | $customList = self::getCustomList($listName); |
||
| 217 | if (isset($customList[$itemId])) { |
||
| 218 | return $customList[$itemId]; |
||
| 219 | } |
||
| 220 | throw new ConfigException("El codigó de item '$itemId' no existe en la lista $listName"); |
||
| 221 | } |
||
| 222 | |||
| 223 | public static function getCustomList($listName) { |
||
| 224 | // returns from cache |
||
| 225 | if (isset(self::$_LIST['LIST_' . $listName])) { |
||
| 226 | return self::$_LIST['LIST_' . $listName]; |
||
| 227 | } |
||
| 228 | // Company customization |
||
| 229 | $customListsPath = Company::getListsPath(); |
||
| 230 | $fileName = "$customListsPath/$listName.php"; |
||
| 231 | if (file_exists($fileName)) { |
||
| 232 | $customListsPath = Company::getListsPath(); |
||
| 233 | $list = require $fileName; |
||
| 234 | // Cache |
||
| 235 | self::$_CAT['LIST_' . $listName] = $list; |
||
| 236 | return $list; |
||
| 237 | } |
||
| 238 | // Defaults |
||
| 239 | $defaultListsPath = F72X::getDefaultListsPath(); |
||
| 240 | $fileName2 = "$defaultListsPath/$listName.php"; |
||
| 241 | $list = require $fileName2; |
||
| 242 | // Cache |
||
| 243 | self::$_CAT['LIST_' . $listName] = $list; |
||
| 244 | return $list; |
||
| 245 | } |
||
| 246 | |||
| 247 | public static function getAllCatNumbers() { |
||
| 265 | } |
||
| 266 | |||
| 267 | } |
||
| 268 |