-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice.py
136 lines (106 loc) · 4.51 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from logging import getLogger
from requests import Session
from requests.exceptions import ConnectionError
from zeep import Client
from zeep.transports import Transport
from zeep.plugins import HistoryPlugin
from trytond.pool import Pool
from trytond.exceptions import UserError
from .tools import LoggingPlugin
_logger = getLogger(__name__)
wsdl_prod = ('https://www2.agenciatributaria.gob.es/static_files/common/'
'internet/dep/aplicaciones/es/aeat/ssii_1_1_bis/fact/ws/')
wsdl_test = ('https://prewww1.aeat.es/static_files/common/internet/dep/'
'aplicaciones/es/aeat/ssii_1_1_bis/fact/ws/')
def _get_client(wsdl, public_crt, private_key, test=False):
session = Session()
session.cert = (public_crt, private_key)
transport = Transport(session=session)
plugins = [HistoryPlugin()]
# TODO: manually handle sessionId? Not mandatory yet recommended...
# http://www.agenciatributaria.es/AEAT.internet/Inicio/Ayuda/Modelos__Procedimientos_y_Servicios/Ayuda_P_G417____IVA__Llevanza_de_libros_registro__SII_/Ayuda_tecnica/Informacion_tecnica_SII/Preguntas_tecnicas_frecuentes/1__Cuestiones_Generales/16___Como_se_debe_utilizar_el_dato_sesionId__.shtml
if test:
plugins.append(LoggingPlugin())
try:
client = Client(wsdl=wsdl, transport=transport, plugins=plugins)
except ConnectionError as e:
raise UserError(str(e))
return client
def bind_issued_invoices_service(crt, pkey, test=False):
wsdl = wsdl_prod + 'SuministroFactEmitidas.wsdl'
port_name = 'SuministroFactEmitidas'
if test:
wsdl = wsdl_test + 'SuministroFactEmitidas.wsdl'
port_name += 'Pruebas'
cli = _get_client(wsdl, crt, pkey, test)
return _IssuedInvoiceService(
cli.bind('siiService', port_name))
def bind_recieved_invoices_service(crt, pkey, test=False):
wsdl = wsdl_prod + 'SuministroFactRecibidas.wsdl'
port_name = 'SuministroFactRecibidas'
if test:
wsdl = wsdl_test + 'SuministroFactRecibidas.wsdl'
port_name += 'Pruebas'
cli = _get_client(wsdl, crt, pkey, test)
return _RecievedInvoiceService(
cli.bind('siiService', port_name))
class _IssuedInvoiceService(object):
def __init__(self, service):
self.service = service
def submit(self, headers, invoices):
pool = Pool()
IssuedMapper = pool.get('aeat.sii.issued.invoice.mapper')
mapper = IssuedMapper()
body = [mapper.build_submit_request(i) for i in invoices]
_logger.debug(body)
response_ = self.service.SuministroLRFacturasEmitidas(
headers, body)
_logger.debug(response_)
return response_, str(body)
def cancel(self, headers, body):
_logger.debug(body)
response_ = self.service.AnulacionLRFacturasEmitidas(
headers, body)
_logger.debug(response_)
return response_
def query(self, headers, year=None, period=None, last_invoice=None):
pool = Pool()
IssuedMapper = pool.get('aeat.sii.issued.invoice.mapper')
mapper = IssuedMapper()
filter_ = mapper.build_query_filter(year=year, period=period,
last_invoice=last_invoice)
_logger.debug(filter_)
response_ = self.service.ConsultaLRFacturasEmitidas(
headers, filter_)
_logger.debug(response_)
return response_
class _RecievedInvoiceService(object):
def __init__(self, service):
self.service = service
def submit(self, headers, invoices):
pool = Pool()
ReceivedMapper = pool.get('aeat.sii.recieved.invoice.mapper')
mapper = ReceivedMapper()
body = [mapper.build_submit_request(i) for i in invoices]
_logger.debug(body)
response_ = self.service.SuministroLRFacturasRecibidas(
headers, body)
_logger.debug(response_)
return response_, str(body)
def cancel(self, headers, body):
_logger.debug(body)
response_ = self.service.AnulacionLRFacturasRecibidas(
headers, body)
_logger.debug(response_)
return response_
def query(self, headers, year=None, period=None, last_invoice=None):
pool = Pool()
ReceivedMapper = pool.get('aeat.sii.recieved.invoice.mapper')
mapper = ReceivedMapper()
filter_ = mapper.build_query_filter(year=year, period=period,
last_invoice=last_invoice)
_logger.debug(filter_)
response_ = self.service.ConsultaLRFacturasRecibidas(
headers, filter_)
_logger.debug(response_)
return response_