Skip to content

Commit

Permalink
issue #124 #170 add currency rate to invoice (for foreign currencies)
Browse files Browse the repository at this point in the history
  • Loading branch information
abernardi committed Nov 4, 2019
1 parent 6334ec9 commit 994b623
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
34 changes: 33 additions & 1 deletion currency.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from decimal import Decimal
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
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction

__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.")

@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(Currency, cls).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'
Expand Down
48 changes: 32 additions & 16 deletions invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ class Invoice(metaclass=PoolMeta):
depends=_DEPENDS + ['company_party'])
pyafipws_anulacion = fields.Boolean('FCE Anulación', states=_STATES,
depends=_DEPENDS)
currency_rate = fields.Numeric('Currency rate', digits=(12, 6),
states={'readonly': Eval('state') != 'draft'}, depends=['state'])

@classmethod
def __setup__(cls):
Expand Down Expand Up @@ -445,6 +447,12 @@ def on_change_party(self):
if self.party and self.party.iva_condition:
self.party_iva_condition = self.party.iva_condition

@fields.depends('company', 'currency')
def on_change_currency(self):
if self.company and self.currency:
if self.company.currency != self.currency:
self.currency_rate = self.currency.rate

@fields.depends('pos')
def on_change_with_pos_pos_daily_report(self, name=None):
if self.pos:
Expand Down Expand Up @@ -742,6 +750,7 @@ def _credit(self):
Date = pool.get('ir.date')

credit = super(Invoice, self)._credit()
credit.currency_rate = self.currency_rate
if self.type == 'in':
invoice_type, invoice_type_desc = INVOICE_CREDIT_AFIP_CODE[
str(int(self.tipo_comprobante))
Expand Down Expand Up @@ -838,6 +847,10 @@ def get_next_number(self, pattern=None):
return '%05d-%08d' % (self.pos.number, int(number))
return SequenceStrict.get_id(sequence.id)

def get_move(self):
with Transaction().set_context(currency_rate=self.currency_rate):
return super(Invoice, self).get_move()

def _get_move_line(self, date, amount):
line = super(Invoice, self)._get_move_line(date, amount)
ref_number = self.number if self.type == 'out' else self.reference
Expand Down Expand Up @@ -1315,28 +1328,31 @@ def strip_accents(text):
imp_neto = str('%.2f' % imp_neto)
imp_tot_conc = str('%.2f' % imp_tot_conc)

if self.company.currency.rate == Decimal('0'):
if self.party.vat_number_afip_foreign:
if batch:
logger.error('missing_currency_rate: Invoice: %s, '
'rate is not setted.' % self.id)
return (ws, True)
self.raise_user_error('missing_currency_rate')
else:
ctz = 1
elif self.company.currency.rate == Decimal('1'):
ctz = 1 / self.currency.rate
else:
ctz = self.company.currency.rate / self.currency.rate

if not self.currency.afip_code:
# currency and rate
moneda_id = self.currency.afip_code
if not moneda_id:
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
if moneda_id != "PES":
ctz = self.currency_rate
else:
if self.company.currency.rate == Decimal('0'):
if self.party.vat_number_afip_foreign:
if batch:
logger.error('missing_currency_rate: Invoice: %s, '
'rate is not setted.' % self.id)
return (ws, True)
self.raise_user_error('missing_currency_rate')
else:
ctz = 1
elif self.company.currency.rate == Decimal('1'):
ctz = 1 / self.currency.rate
else:
ctz = self.company.currency.rate / self.currency.rate
moneda_ctz = "{:.{}f}".format(ctz, 6)

# foreign trade data: export permit, country code, etc.:
Expand Down
4 changes: 4 additions & 0 deletions locale/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ msgctxt "field:account.invoice,annulled:"
msgid "Annulled"
msgstr "Anulada"

msgctxt "field:account.invoice,currency_rate:"
msgid "Currency rate"
msgstr "Tasa de cambio"

msgctxt "field:account.invoice,invoice_type:"
msgid "Comprobante"
msgstr "Tipo de factura"
Expand Down
6 changes: 6 additions & 0 deletions view/invoice_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@
<field name="annulled" xexpand="0"/>
</xpath>

<xpath
expr="/form/notebook/page[@id='info']/field[@name='cancel_move']"
position="after">
<label name="currency_rate"/>
<field name="currency_rate"/>
</xpath>
</data>

0 comments on commit 994b623

Please sign in to comment.