Passed
Push — main ( 7c1548...e3a6d2 )
by Sat CFDI
01:47
created

satdigitalinvoice.local   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 36
eloc 102
dl 0
loc 137
rs 9.52
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A LocalDB.__init__() 0 2 1
A LocalDB.folio() 0 2 1
A LocalDBSatCFDI.status_sat() 0 8 3
A LocalDBSatCFDI.saldar_flip() 0 4 1
A LocalDBSatCFDI.notificar_flip() 0 4 1
A LocalDB.status_sat_set() 0 8 3
A LocalDB.status_sat() 0 3 1
A LocalDB.notificar_set() 0 8 3
A LocalDB.saldar_set() 0 8 3
B LocalDBSatCFDI.saldar() 0 12 7
A LocalDB.folio_set() 0 2 1
A LocalDB.notificar() 0 3 1
A LocalDBSatCFDI.notificar() 0 4 2
A LocalDBSatCFDI.describe() 0 5 1
A LocalDBSatCFDI.__init__() 0 4 1
A LocalDB.saldar() 0 3 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
A save_data() 0 3 2
A load_data() 0 6 3
1
import logging
2
import os
3
import pickle
4
from uuid import UUID
5
6
import diskcache
7
from satcfdi.accounting import SatCFDI
8
from satcfdi.pacs import sat
9
from .log_tools import print_yaml
10
11
EMAIL_NOTIFICADA = 2
12
STATUS_SAT = 3
13
PENDIENTE = 4
14
FOLIO = 5
15
16
sat_manager = sat.SAT()
17
18
logger = logging.getLogger(__name__)
19
DATA_DIR = '.data'
20
21
22
class LocalDB(diskcache.Cache):
23
    def __init__(self):
24
        super().__init__(directory=os.path.join(DATA_DIR, 'cache'))
25
26
    def folio(self):
27
        return self.get(FOLIO)
28
29
    def folio_set(self, value: int):
30
        self[FOLIO] = value
31
32
    def saldar(self, uuid: UUID):
33
        return self.get(
34
            (PENDIENTE, uuid), True
35
        )
36
37
    def saldar_set(self, uuid: UUID, value: bool):
38
        if value:
39
            try:
40
                del self[(PENDIENTE, uuid)]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable PENDIENTE does not seem to be defined.
Loading history...
41
            except KeyError:
42
                pass
43
        else:
44
            self[(PENDIENTE, uuid)] = value
45
46
    def notificar(self, uuid: UUID):
47
        return self.get(
48
            (EMAIL_NOTIFICADA, uuid), True
49
        )
50
51
    def notificar_set(self, uuid: UUID, value: bool):
52
        if value:
53
            try:
54
                del self[(EMAIL_NOTIFICADA, uuid)]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable EMAIL_NOTIFICADA does not seem to be defined.
Loading history...
55
            except KeyError:
56
                pass
57
        else:
58
            self[(EMAIL_NOTIFICADA, uuid)] = value
59
60
    def status_sat(self, uuid: UUID):
61
        return self.get(
62
            (STATUS_SAT, uuid), {}
63
        )
64
65
    def status_sat_set(self, uuid: UUID, value: dict):
66
        if value:
67
            self[(STATUS_SAT, uuid)] = value
68
        else:
69
            try:
70
                del self[(STATUS_SAT, uuid)]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable STATUS_SAT does not seem to be defined.
Loading history...
71
            except KeyError:
72
                pass
73
74
75
class LocalDBSatCFDI(LocalDB):
76
    def __init__(self, enviar_a_partir, saldar_a_partir):
77
        super().__init__()
78
        self.enviar_a_partir = enviar_a_partir
79
        self.saldar_a_partir = saldar_a_partir
80
81
    def notificar(self, cfdi: SatCFDI):
82
        if cfdi["Fecha"] >= self.enviar_a_partir:
83
            return super().notificar(cfdi.uuid)
84
        return False
85
86
    def notificar_flip(self, cfdi: SatCFDI):
87
        v = not self.notificar(cfdi)
88
        self.notificar_set(cfdi.uuid, v)
89
        return v
90
91
    def saldar(self, cfdi: SatCFDI):
92
        if cfdi["TipoDeComprobante"] != "I":
93
            return None
94
        if cfdi["MetodoPago"] == "PPD" and cfdi.saldo_pendiente == 0:
95
            return 0
96
        if cfdi["Fecha"] >= self.saldar_a_partir[cfdi["MetodoPago"]]:
97
            if super().saldar(cfdi.uuid):
98
                if cfdi["MetodoPago"] == "PPD":
99
                    return cfdi.saldo_pendiente
100
                else:
101
                    return cfdi["Total"]
102
        return 0
103
104
    def saldar_flip(self, cfdi: SatCFDI):
105
        v = not self.saldar(cfdi)
106
        self.saldar_set(cfdi.uuid, v)
107
        return v
108
109
    def status_sat(self, cfdi: SatCFDI, update=False):
110
        if update:
111
            res = sat_manager.status(cfdi)
112
            if res["ValidacionEFOS"] == "200":
113
                self.status_sat_set(cfdi.uuid, res)
114
            return res
115
        else:
116
            return super().status_sat(cfdi.uuid)
117
118
    def describe(self, cfdi: SatCFDI):
119
        print_yaml({
120
            'saldar': self.saldar(cfdi),
121
            'enviar': self.notificar(cfdi),
122
            'status_sat': self.status_sat(cfdi)
123
        })
124
125
126
def save_data(file, data):
127
    with open(os.path.join(DATA_DIR, file), 'wb') as f:
128
        pickle.dump(data, f)
129
130
131
def load_data(file, default=None):
132
    try:
133
        with open(os.path.join(DATA_DIR, file), 'rb') as f:
134
            return pickle.load(f)
135
    except FileNotFoundError:
136
        return default
137