-
Notifications
You must be signed in to change notification settings - Fork 3
/
company.py
77 lines (62 loc) · 2.66 KB
/
company.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
# This file is part of the party_ar module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
import logging
import os
from trytond.model import fields
from trytond.pool import PoolMeta, Pool
from trytond.exceptions import UserError
from trytond.i18n import gettext
logger = logging.getLogger(__name__)
class Company(metaclass=PoolMeta):
__name__ = 'company.company'
pyafipws_certificate = fields.Text('Certificado AFIP WS',
help='Certificado (.crt) de la empresa para webservices AFIP')
pyafipws_private_key = fields.Text('Clave Privada AFIP WS',
help='Clave Privada (.key) de la empresa para webservices AFIP')
pyafipws_mode_cert = fields.Selection([
('', 'n/a'),
('homologacion', 'Homologación'),
('produccion', 'Producción'),
], 'Modo de certificacion',
help=('El objetivo de Homologación (testing), es facilitar las '
'pruebas. Los certificados de Homologación y Producción son '
'distintos.'))
@staticmethod
def default_pyafipws_mode_cert():
return ''
@classmethod
def validate(cls, companies):
super().validate(companies)
for company in companies:
company.check_pyafipws_mode_cert()
def check_pyafipws_mode_cert(self):
if self.pyafipws_mode_cert == '':
return
ta = self.pyafipws_authenticate(service='wsfe')
@classmethod
def get_cache_dir(cls):
def get_module_install_dir():
basepath = __file__
return os.path.dirname(os.path.abspath(basepath))
return os.path.join(get_module_install_dir(), 'cache')
def pyafipws_authenticate(self, service='wsfe', cache=''):
'Authenticate against AFIP, returns token, sign, err_msg (dict)'
pool = Pool()
crt = str(self.pyafipws_certificate)
key = str(self.pyafipws_private_key)
if self.pyafipws_mode_cert == 'homologacion':
WSAA_URL = 'https://wsaahomo.afip.gov.ar/ws/services/LoginCms?wsdl'
elif self.pyafipws_mode_cert == 'produccion':
WSAA_URL = 'https://wsaa.afip.gov.ar/ws/services/LoginCms?wsdl'
else:
raise UserError(gettext(
'party_ar.msg_wrong_pyafipws_mode',
message=('El modo de certificación no es ni producción, ni '
'homologación. Configure su Empresa')))
if not cache:
cache = self.get_cache_dir()
PyAfipWsWrapper = pool.get('afip.wrapper')
ta = PyAfipWsWrapper.authenticate(service, crt, key, wsdl=WSAA_URL,
cache=cache)
return ta