Passed
Branch develop (5fc816)
by JAIME ELMER
01:58
created

Catalogo::getCatItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 DOMDocument;
14
use Sabre\Xml\Reader;
15
16
class Catalogo {
17
18
    const CAT_TAX_DOCUMENT_TYPE    = 1;
19
    const CAT_CURRENCY_TYPE        = 2;
20
    const CAT_MEASUREMENT_UNIT     = 3;
21
    const CAT_COUNTRY_CODE         = 4;
22
    const CAT_TAX_TYPE             = 5;
23
    const CAT_IDENT_DOCUMENT_TYPE  = 6;
24
    const CAT_IGV_AFFECTATION_TYPE = 7;
25
    const CAT_ISC_CALC_SYSTEM_TYPE = 8;
26
    const CAT_NOTA_CREDITO_TYPE    = 9;
27
    const CAT_NOTA_DEBITO_TYPE     = 10;
28
    const CAT_FACTURA_TYPE         = 51;
29
    /** @CAT1 Código tipo de documento */
30
    const CAT1_FACTURA      = '01';
31
    const CAT1_BOLETA       = '03';
32
    const CAT1_NOTA_CREDITO = '07';
33
    const CAT1_NOTA_DEBITO  = '08';
34
35
    /** @CAT5 Tipo de impuesto*/
36
    const CAT5_IGV   = '1000';
37
    const CAT5_IVAP  = '1016';
38
    const CAT5_ISC   = '2000';
39
    const CAT5_EXP   = '9995';
40
    const CAT5_GRA   = '9996';
41
    const CAT5_EXO   = '9997';
42
    const CAT5_INA   = '9998';
43
    const CAT5_OTROS = '9999';
44
45
    /** @CAT7 Tipo de afectación del IGV */
46
    const CAT7_GRA_IGV        = '10';
47
    /** @CAT16 Tipo de precio */
48
    const CAT16_UNITARY_PRICE = '01';
49
    const CAT16_REF_VALUE     = '02';
50
51
    /** @CAT6*/
52
    const IDENTIFICATION_DOC_DNI = '1';
53
    const IDENTIFICATION_DOC_RUC = '6';
54
55
    private static $_CAT = [];
56
57
    public static function itemExist($catNumber, $itemID) {
58
        $items = self::getCatItems($catNumber);
59
        return key_exists($itemID, $items);
60
    }
61
    
62
    public static function getCatItem($catNumber, $itemID, $key = 'id') {
63
        $items = self::getCatItems($catNumber);
64
        foreach ($items as $item) {
65
            if ($item[$key] === strval($itemID)) {
66
                return $item;
67
            }
68
        }
69
        return null;
70
    }
71
72
    public static function getCatItems($catNumber) {
73
        // returns from cache
74
        if (isset(self::$_CAT['CAT_' . $catNumber])) {
75
            return self::$_CAT['CAT_' . $catNumber];
76
        }
77
        // Load the XML
78
        $xmlName = self::getXmlName($catNumber);
79
        $doc = new DOMDocument();
80
        $doc->load(SunatVars::DIR_CATS . "/$xmlName");
81
82
        $reader = new Reader();
83
        $reader->xml($doc->saveXML());
84
85
        $catData = $reader->parse();
86
        $items = $catData['value'];
87
        $itemsO = [];
88
        foreach ($items as &$item) {
89
            unset($item['name']); // Here because the item may contain the name attribute!
90
            foreach ($item['attributes'] as $attKey => $att) {
91
                $item[$attKey] = $att;
92
            }
93
            unset($item['attributes']);
94
            $itemsO[$item['id']] = $item;
95
        }
96
        // Cache
97
        self::$_CAT['CAT_' . $catNumber] = $itemsO;
98
        return self::$_CAT['CAT_' . $catNumber];
99
    }
100
101
    private static function getXmlName($catNumeber) {
102
        return 'cat_' . str_pad($catNumeber, 2, '0', STR_PAD_LEFT) . '.xml';
103
    }
104
105
}
106