-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcurrency.py
110 lines (93 loc) · 3.94 KB
/
currency.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
# This file is part of the account_invoice_ar module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
import logging
from decimal import Decimal
import datetime
from pyafipws.wsfev1 import WSFEv1
from pyafipws.wsfexv1 import WSFEXv1
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
from trytond.exceptions import UserError
from trytond.i18n import gettext
logger = logging.getLogger(__name__)
class Currency(metaclass=PoolMeta):
__name__ = 'currency.currency'
afip_code = fields.Char('AFIP Code', size=3,
help="The 3 digits AFIP currency code.")
@classmethod
def compute(cls, from_currency, amount, to_currency, round=True):
pool = Pool()
Company = pool.get('company.company')
currency_rate = Transaction().context.get('currency_rate')
if not currency_rate:
return super().compute(from_currency, amount, to_currency, round)
if to_currency == from_currency:
if round:
return to_currency.round(amount)
else:
return amount
company = Company(Transaction().context['company'])
if from_currency == company.currency:
from_currency_rate = currency_rate
currency_rate = Decimal('1.0')
else:
from_currency_rate = Decimal('1.0')
if round:
return to_currency.round(
amount * currency_rate / from_currency_rate)
else:
return amount * currency_rate / from_currency_rate
class Rate(metaclass=PoolMeta):
__name__ = 'currency.currency.rate'
def get_afip_rate(self, service='wsfex'):
'''
get rate from afip webservice.
'''
pool = Pool()
Company = pool.get('company.company')
company_id = Transaction().context.get('company')
if not company_id:
logger.error('The company is not defined')
raise UserError(gettext(
'account_invoice_ar.msg_company_not_defined'))
company = Company(company_id)
# authenticate against AFIP:
ta = company.pyafipws_authenticate(service=service)
if service == 'wsfe':
ws = WSFEv1()
if company.pyafipws_mode_cert == 'homologacion':
WSDL = 'https://wswhomo.afip.gov.ar/wsfev1/service.asmx?WSDL'
elif company.pyafipws_mode_cert == 'produccion':
WSDL = (
'https://servicios1.afip.gov.ar/wsfev1/service.asmx?WSDL')
elif service == 'wsfex':
ws = WSFEXv1()
if company.pyafipws_mode_cert == 'homologacion':
WSDL = 'https://wswhomo.afip.gov.ar/wsfexv1/service.asmx?WSDL'
elif company.pyafipws_mode_cert == 'produccion':
WSDL = (
'https://servicios1.afip.gov.ar/wsfexv1/service.asmx?WSDL')
else:
logger.critical('AFIP ws is not yet supported! %s', service)
raise UserError(gettext(
'account_invoice_ar.msg_webservice_not_supported',
service=service))
cache = Company.get_cache_dir()
ws.LanzarExcepciones = True
try:
ws.Conectar(wsdl=WSDL, cache=cache, cacert=True)
except Exception as e:
msg = ws.Excepcion + ' ' + str(e)
logger.error('WSAA connecting to afip: %s' % msg)
raise UserError(gettext(
'account_invoice_ar.msg_wsaa_error', msg=msg))
ws.SetTicketAcceso(ta)
ws.Cuit = company.party.vat_number
if not self.currency.afip_code:
logger.error('AFIP code is empty %s', self.currency.code)
raise UserError(gettext(
'account_invoice_ar.msg_afip_code_empty'))
self.rate = Decimal(ws.GetParamCtz('DOL'))
self.date = datetime.datetime.strptime(ws.FchCotiz, '%Y%m%d').date()