1
|
|
|
import logging |
2
|
|
|
import os |
3
|
|
|
import pickle |
4
|
|
|
from enum import Enum |
5
|
|
|
from uuid import UUID |
6
|
|
|
|
7
|
|
|
import diskcache |
8
|
|
|
from satcfdi.accounting import SatCFDI |
9
|
|
|
from satcfdi.pacs import sat |
10
|
|
|
|
11
|
|
|
from . import DATA_DIRECTORY |
12
|
|
|
from .log_tools import print_yaml |
13
|
|
|
from . import PPD |
14
|
|
|
|
15
|
|
|
LIQUIDATED = 0 |
16
|
|
|
NOTIFIED = 2 |
17
|
|
|
STATUS_SAT = 3 |
18
|
|
|
FOLIO = 5 |
19
|
|
|
|
20
|
|
|
sat_manager = sat.SAT() |
21
|
|
|
|
22
|
|
|
logger = logging.getLogger(__name__) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class LocalDB(diskcache.Cache): |
26
|
|
|
def __init__(self): |
27
|
|
|
super().__init__(directory=os.path.join(DATA_DIRECTORY, 'cache')) |
28
|
|
|
|
29
|
|
|
def folio(self): |
30
|
|
|
return self.get(FOLIO, 1) |
31
|
|
|
|
32
|
|
|
def folio_set(self, value: int): |
33
|
|
|
self[FOLIO] = value |
34
|
|
|
|
35
|
|
|
def liquidated(self, uuid: UUID): |
36
|
|
|
return self.get((LIQUIDATED, uuid)) |
37
|
|
|
|
38
|
|
|
def liquidated_set(self, uuid: UUID, value: bool): |
39
|
|
|
self[(LIQUIDATED, uuid)] = value |
40
|
|
|
|
41
|
|
|
def notified(self, uuid: UUID): |
42
|
|
|
return self.get((NOTIFIED, uuid)) |
43
|
|
|
|
44
|
|
|
def notified_set(self, uuid: UUID, value: bool): |
45
|
|
|
self[(NOTIFIED, uuid)] = value |
46
|
|
|
|
47
|
|
|
def status_sat(self, uuid: UUID): |
48
|
|
|
return self.get((STATUS_SAT, uuid), {}) |
49
|
|
|
|
50
|
|
|
def status_sat_set(self, uuid: UUID, value: dict): |
51
|
|
|
if value: |
52
|
|
|
self[(STATUS_SAT, uuid)] = value |
53
|
|
|
else: |
54
|
|
|
try: |
55
|
|
|
del self[(STATUS_SAT, uuid)] |
|
|
|
|
56
|
|
|
except KeyError: |
57
|
|
|
pass |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
class LiquidatedState(Enum): |
61
|
|
|
NONE = 1 |
62
|
|
|
YES = 2 |
63
|
|
|
NO = 3 |
64
|
|
|
IGNORED = 4 |
65
|
|
|
CANCELLED = 5 |
66
|
|
|
|
67
|
|
|
def __str__(self): |
68
|
|
|
if self.name == "NONE": |
69
|
|
|
return "" |
70
|
|
|
if self.name == "IGNORED": |
71
|
|
|
return "Ignorada" |
72
|
|
|
if self.name == "YES": |
73
|
|
|
return "Si" |
74
|
|
|
if self.name == "NO": |
75
|
|
|
return "No" |
76
|
|
|
if self.name == "CANCELLED": |
77
|
|
|
return "Cancelada" |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
class LocalDBSatCFDI(LocalDB): |
81
|
|
|
def __init__(self, enviar_a_partir, pagar_a_partir): |
82
|
|
|
super().__init__() |
83
|
|
|
self.enviar_a_partir = enviar_a_partir |
84
|
|
|
self.pagar_a_partir = pagar_a_partir |
85
|
|
|
|
86
|
|
|
def notified(self, cfdi: SatCFDI): |
87
|
|
|
v = super().notified(cfdi.uuid) |
88
|
|
|
if v is None and cfdi["Fecha"] < self.enviar_a_partir: |
89
|
|
|
return True |
90
|
|
|
return v |
91
|
|
|
|
92
|
|
|
def notified_flip(self, cfdi: SatCFDI): |
93
|
|
|
v = not self.notified(cfdi) |
94
|
|
|
self.notified_set(cfdi.uuid, v) |
95
|
|
|
return v |
96
|
|
|
|
97
|
|
|
def liquidated(self, cfdi: SatCFDI): |
98
|
|
|
v = super().liquidated(cfdi.uuid) |
99
|
|
|
if v is None and cfdi["Fecha"] < self.pagar_a_partir[cfdi["MetodoPago"]]: |
100
|
|
|
return True |
101
|
|
|
return v |
102
|
|
|
|
103
|
|
|
def liquidated_flip(self, cfdi: SatCFDI): |
104
|
|
|
v = not self.liquidated(cfdi) |
105
|
|
|
self.liquidated_set(cfdi.uuid, v) |
106
|
|
|
return v |
107
|
|
|
|
108
|
|
|
def status_sat(self, cfdi: SatCFDI, update=False): |
109
|
|
|
if update: |
110
|
|
|
res = sat_manager.status(cfdi) |
111
|
|
|
if res["ValidacionEFOS"] == "200": |
112
|
|
|
self.status_sat_set(cfdi.uuid, res) |
113
|
|
|
return res |
114
|
|
|
else: |
115
|
|
|
return super().status_sat(cfdi.uuid) |
116
|
|
|
|
117
|
|
|
def liquidated_state(self, cfdi: SatCFDI): |
118
|
|
|
if cfdi.estatus == '0': |
119
|
|
|
return LiquidatedState.CANCELLED |
120
|
|
|
|
121
|
|
|
if cfdi["TipoDeComprobante"] != "I": |
122
|
|
|
return LiquidatedState.NONE |
123
|
|
|
|
124
|
|
|
mpago = cfdi["MetodoPago"] |
125
|
|
|
if cfdi['Total'] == 0 or (mpago == PPD and cfdi.saldo_pendiente == 0): |
126
|
|
|
return LiquidatedState.YES |
127
|
|
|
|
128
|
|
|
if self.liquidated(cfdi): |
129
|
|
|
if mpago == PPD: |
130
|
|
|
return LiquidatedState.IGNORED |
131
|
|
|
return LiquidatedState.YES |
132
|
|
|
|
133
|
|
|
return LiquidatedState.NO |
134
|
|
|
|
135
|
|
|
def describe(self, cfdi: SatCFDI): |
136
|
|
|
print_yaml({ |
137
|
|
|
'saldada': self.liquidated(cfdi), |
138
|
|
|
'enviada': self.notified(cfdi), |
139
|
|
|
'status_sat': self.status_sat(cfdi) |
140
|
|
|
}) |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
def save_data(file, data): |
144
|
|
|
with open(os.path.join(DATA_DIRECTORY, file), 'wb') as f: |
145
|
|
|
pickle.dump(data, f) |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
def load_data(file, default=None): |
149
|
|
|
try: |
150
|
|
|
with open(os.path.join(DATA_DIRECTORY, file), 'rb') as f: |
151
|
|
|
return pickle.load(f) |
152
|
|
|
except FileNotFoundError: |
153
|
|
|
return default |
154
|
|
|
|