Skip to content

Commit

Permalink
issue #124 Add afip_code to currency model. Add method get_afip_rate …
Browse files Browse the repository at this point in the history
…to Rate model.

Teniamos hard-coded los códigos de las monedas de AFIP.
Se agrega el campo afip_code al modelo currency y se cargan algunos
valores vía xml (PES, DOL, Libra esterlina, Peso Uruguay, Real).

Se agrega metodo de consulta de cotización en el modelo Rate. Por ahora
no se hace uso de este método.

Tener en cuenta que la devolución de las cotizaciones son siempre de la forma:
 1 monda extranjera -> XX.XX peso argentino

Si la moneda fuerte es el peso argentino, entonces las cotizaciones en
las monedas extranjeras debería den ser:

 1 peso argentino -> xx.xx moneda extranjera.

REL:
 issue #172

TODO:
 Faltaría agregar al resto de las monedas.
 Consultar cotizacion de la moneda al hacer nota de crédito.
  • Loading branch information
lukio committed Oct 21, 2019
1 parent f79a7b3 commit feee9d3
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from . import pos
from . import bank
from . import party
from . import currency

__all__ = ['register']

Expand All @@ -23,6 +24,7 @@ def register():
invoice.AfipWSTransaction,
bank.BankAccount,
party.Party,
currency.Currency,
module='account_invoice_ar', type_='model')
Pool.register(
invoice.CreditInvoice,
Expand Down
76 changes: 76 additions & 0 deletions currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
import datetime
from pyafipws.wsfev1 import WSFEv1
from pyafipws.wsfexv1 import WSFEXv1
from . import afip_auth

from trytond.model import fields
from trytond.pyson import Eval, If, In
from trytond.pool import PoolMeta

__all__ = ['Currency', 'Rate']


class Currency(metaclass=PoolMeta):
__name__ = 'currency.currency'
afip_code = fields.Char('AFIP Code', size=3,
help="The 3 digits AFIP currency code.")


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')
cls.raise_user_error('company_not_defined')
company = Company(company_id)
# authenticate against AFIP:
auth_data = 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)
cls.raise_user_error('webservice_not_supported', service)

cache_dir = afip_auth.get_cache_dir()
ws.LanzarExcepciones = True
try:
ws.Conectar(wsdl=wsdl, cache=cache_dir)
except Exception as e:
msg = ws.Excepcion + ' ' + str(e)
logger.error('WSAA connecting to afip: %s' % msg)
cls.raise_user_error('wsaa_error', msg)
ws.Cuit = vat_number
ws.Token = auth_data['token']
ws.Sign = auth_data['sign']

if not date:
Date = pool.get('ir.date')
today = Date.today().strftime("%Y%m%d")
if not self.currency.afip_code:
logger.error('AFIP code is empty %s', self.currency.code)
cls.raise_user_error('afip_code_empty')

self.rate = Decimal(ws.GetParamCtz('DOL'))
self.date = datetime.datetime.strptime(ws.FchCotiz, '%Y%m%d').date()
26 changes: 26 additions & 0 deletions currency.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<tryton>
<data>

<record model="ir.ui.view" id="currency_view_form">
<field name="model">currency.currency</field>
<field name="inherit" ref="currency.currency_view_form"/>
<field name="name">currency_form</field>
</record>

</data>

<data skiptest="1" grouped="1">

<record model="currency.currency" id="currency.usd">
<field name="symbol">U$S</field>
<field name="afip_code">DOL</field>
</record>
<record model="currency.currency" id="currency.ars">
<field name="afip_code">PES</field>
</record>
<record model="currency.currency" id="currency.eur">
<field name="afip_code">060</field>
</record>
<record model="currency.currency" id="currency.gbp">
<field name="afip_code">021</field>
</record>
<record model="currency.currency" id="currency.brl">
<field name="afip_code">012</field>
</record>
<record model="currency.currency" id="currency.uyu">
<field name="afip_code">011</field>
</record>
</data>

Expand Down
11 changes: 7 additions & 4 deletions invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,11 +1329,14 @@ def strip_accents(text):
else:
ctz = self.company.currency.rate / self.currency.rate

if self.currency.code == 'ARS':
moneda_id = 'PES'
else:
moneda_id = {'USD': 'DOL', 'EUR': '060'}[self.currency.code]
if not self.currency.afip_code:
if batch:
logger.error('missing_currency_afip_code: Invoice: %s, '
'currency afip code is not setted.' % self.id)
return (ws, True)
self.raise_user_error('missing_currency_afip_code')

moneda_id = self.currency.afip_code
moneda_ctz = "{:.{}f}".format(ctz, 6)

# foreign trade data: export permit, country code, etc.:
Expand Down
9 changes: 9 additions & 0 deletions view/currency_form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<!-- The COPYRIGHT file at the top level of this repository contains
the full copyright notices and license terms. -->
<data>
<xpath expr="/form/field[@name='numeric_code']" position="after">
<label name="afip_code"/>
<field name="afip_code"/>
</xpath>
</data>

0 comments on commit feee9d3

Please sign in to comment.