1
|
|
|
import logging |
2
|
|
|
from decimal import Decimal |
3
|
|
|
from html import escape as html_escape |
4
|
|
|
|
5
|
|
|
import jinja2 |
6
|
|
|
import yaml |
7
|
|
|
from jinja2 import Environment |
8
|
|
|
from jinja2.filters import do_mark_safe |
9
|
|
|
from satcfdi import Code |
10
|
|
|
from satcfdi.pacs import sat |
11
|
|
|
from satcfdi.transform.helpers import Xint |
12
|
|
|
|
13
|
|
|
logger = logging.getLogger() |
14
|
|
|
sat_manager = sat.SAT() |
15
|
|
|
|
16
|
|
|
environment_default = Environment( |
17
|
|
|
loader=jinja2.FileSystemLoader(searchpath=['templates']), |
18
|
|
|
autoescape=False, |
19
|
|
|
trim_blocks=True, |
20
|
|
|
lstrip_blocks=True, |
21
|
|
|
undefined=jinja2.StrictUndefined, |
22
|
|
|
) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def finalize_html(val): |
26
|
|
|
return do_mark_safe( |
27
|
|
|
tag(html_escape(val), "b") |
28
|
|
|
) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
environment_bold_escaped = Environment( |
32
|
|
|
loader=jinja2.FileSystemLoader(searchpath=['templates']), |
33
|
|
|
autoescape=True, |
34
|
|
|
trim_blocks=True, |
35
|
|
|
lstrip_blocks=True, |
36
|
|
|
undefined=jinja2.StrictUndefined, |
37
|
|
|
finalize=finalize_html |
38
|
|
|
) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class LocalData(dict): |
42
|
|
|
file_source = None |
43
|
|
|
|
44
|
|
|
def __init__(self): |
45
|
|
|
with open(self.file_source, "r", encoding="utf-8") as fs: |
46
|
|
|
super().__init__(yaml.safe_load(fs)) |
47
|
|
|
|
48
|
|
|
def save(self): |
49
|
|
|
with open(self.file_source, "w", encoding="utf-8") as fs: |
50
|
|
|
yaml.dump_all([self], fs, Dumper=yaml.SafeDumper, encoding="utf-8", allow_unicode=True, sort_keys=False) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
class PaymentsManager(LocalData): |
54
|
|
|
file_source = "pagos.yaml" |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class NotificationsManager(LocalData): |
58
|
|
|
file_source = "notifications.yaml" |
59
|
|
|
|
60
|
|
|
def folio(self): |
61
|
|
|
return self["Folio"] |
62
|
|
|
|
63
|
|
|
def serie(self): |
64
|
|
|
return self["Serie"] |
65
|
|
|
|
66
|
|
|
def inc_folio(self): |
67
|
|
|
self["Folio"] += 1 |
68
|
|
|
self.save() |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
class ClientsManager(LocalData): |
72
|
|
|
file_source = "clients.yaml" |
73
|
|
|
|
74
|
|
|
def __init__(self): |
75
|
|
|
super().__init__() |
76
|
|
|
for k in self: |
77
|
|
|
self[k]["Rfc"] = k |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
class FacturasManager(LocalData): |
81
|
|
|
file_source = "facturas.yaml" |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
class CanceladosManager(LocalData): |
85
|
|
|
file_source = "cancelados.yaml" |
86
|
|
|
|
87
|
|
|
def get_state(self, cfdi, only_cache=True): |
88
|
|
|
uuid = str(cfdi.uuid) |
89
|
|
|
|
90
|
|
|
res = self.get(uuid) |
91
|
|
|
if res: |
92
|
|
|
return res |
93
|
|
|
|
94
|
|
|
if only_cache: |
95
|
|
|
return {} |
96
|
|
|
|
97
|
|
|
try: |
98
|
|
|
res = sat_manager.status(cfdi) |
99
|
|
|
if res["ValidacionEFOS"] != "200": |
100
|
|
|
logger.error("CFDI No Encontrado '%s' %s", uuid, res) |
101
|
|
|
else: |
102
|
|
|
logger.info("CFDI Encontrado '%s' %s", uuid, res) |
103
|
|
|
|
104
|
|
|
self[uuid] = res |
105
|
|
|
return res |
106
|
|
|
except Exception: |
107
|
|
|
logger.exception("Failed to get Status for Invoice: ", uuid) |
108
|
|
|
return {} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def tag(text, tag): |
112
|
|
|
return '<' + tag + '>' + text + '</' + tag + '>' |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
yaml.SafeDumper.add_multi_representer(dict, lambda dumper, data: dumper.represent_dict(data)) |
116
|
|
|
yaml.SafeLoader.add_constructor("!decimal", lambda loader, node: Decimal(loader.construct_scalar(node))) |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
def represent_decimal(dumper, data): |
120
|
|
|
return dumper.represent_scalar('tag:yaml.org,2002:str', str(data)) |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
def represent_str(dumper, data): |
124
|
|
|
return dumper.represent_scalar('tag:yaml.org,2002:str', str(data)) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
yaml.SafeDumper.add_representer(Decimal, represent_decimal) |
128
|
|
|
yaml.SafeDumper.add_representer(Code, represent_str) |
129
|
|
|
yaml.SafeDumper.add_representer(Xint, represent_str) |
130
|
|
|
|