Feld 'rate_2nd_currency' + 'amount_2nd_currency' ok + test

This commit is contained in:
Frederik Jaeckel 2022-10-03 08:47:55 +02:00
parent 59dfb94bee
commit 517e2c5ad3
5 changed files with 302 additions and 0 deletions

150
line.py
View file

@ -10,6 +10,7 @@ from trytond.transaction import Transaction
from trytond.report import Report
from trytond.exceptions import UserError
from trytond.i18n import gettext
from trytond.modules.currency.ir import rate_decimal
from decimal import Decimal
from sql import Cast, Literal
from sql.functions import DatePart
@ -119,6 +120,23 @@ class Line(Workflow, ModelSQL, ModelView):
payee = fields.Function(fields.Reference(string='Payee', readonly=True,
selection=sel_payee), 'on_change_with_payee', searcher='search_payee')
amount_2nd_currency = fields.Numeric(string='Amount Second Currency',
digits=(16, Eval('currency2nd_digits', 2)),
states={
'readonly': STATES['readonly'],
'required': Bool(Eval('currency2nd')),
'invisible': ~Bool(Eval('currency2nd')),
}, depends=DEPENDS+['currency2nd_digits', 'currency2nd'])
rate_2nd_currency = fields.Function(fields.Numeric(string='Rate',
help='Exchange rate between the currencies of the participating cashbooks.',
digits=(rate_decimal * 2, rate_decimal),
states={
'readonly': STATES['readonly'],
'required': Bool(Eval('currency2nd')),
'invisible': ~Bool(Eval('currency2nd')),
}, depends=DEPENDS+['currency2nd_digits', 'currency2nd']),
'on_change_with_rate_2nd_currency', setter='set_rate_2nd_currency')
# link to lines created by this record
reference = fields.Many2One(string='Reference', readonly=True, select=True,
states={
@ -161,6 +179,10 @@ class Line(Workflow, ModelSQL, ModelView):
string="Currency", readonly=True), 'on_change_with_currency')
currency_digits = fields.Function(fields.Integer(string='Currency Digits',
readonly=True), 'on_change_with_currency_digits')
currency2nd = fields.Function(fields.Many2One(model_name='currency.currency',
string="2nd Currency", readonly=True), 'on_change_with_currency2nd')
currency2nd_digits = fields.Function(fields.Integer(string='2nd Currency Digits',
readonly=True), 'on_change_with_currency2nd_digits')
state = fields.Selection(string='State', required=True, readonly=True,
select=True, selection=sel_linetype)
@ -540,6 +562,88 @@ class Line(Workflow, ModelSQL, ModelView):
self.splitlines = []
self.booktransf = None
@fields.depends('currency', 'booktransf', '_parent_booktransf.currency', \
'amount', 'amount_2nd_currency', 'rate_2nd_currency')
def on_change_amount(self):
""" update amount_2nd_currency
"""
self.on_change_rate_2nd_currency()
@fields.depends('booktransf', '_parent_booktransf.currency', \
'currency', 'amount', 'amount_2nd_currency', 'rate_2nd_currency')
def on_change_booktransf(self):
""" update amount_2nd_currency
"""
Currency = Pool().get('currency.currency')
if self.booktransf:
if self.currency:
if self.amount is not None:
if self.booktransf.currency.id != self.currency.id:
self.amount_2nd_currency = Currency.compute(
self.currency,
self.amount,
self.booktransf.currency
)
self.rate_2nd_currency = self.on_change_with_rate_2nd_currency()
return
self.amount_2nd_currency = None
@fields.depends('currency', 'booktransf', '_parent_booktransf.currency', \
'amount', 'amount_2nd_currency', 'rate_2nd_currency')
def on_change_rate_2nd_currency(self):
""" update amount_2nd_currency by rate
"""
if (self.amount is None) or (self.rate_2nd_currency is None):
return
if self.currency:
if self.booktransf:
if self.currency.id != self.booktransf.currency.id:
self.amount_2nd_currency = self.booktransf.currency.round(
self.amount * self.rate_2nd_currency
)
@classmethod
def set_rate_2nd_currency(cls, lines, name, value):
""" compute amount_2nd_currency, write to db
"""
Line2 = Pool().get('cashbook.line')
to_write = []
if not name == 'rate_2nd_currency':
return
for line in lines:
if line.booktransf is None:
continue
if line.cashbook.currency.id == line.booktransf.currency.id:
continue
to_write.extend([
[line],
{
'amount_2nd_currency': line.booktransf.currency.round(
line.amount * value),
}])
if len(to_write) > 0:
Line2.write(*to_write)
@fields.depends('amount', 'amount_2nd_currency', 'currency2nd')
def on_change_with_rate_2nd_currency(self, name=None):
""" get current rate from amount
"""
Rate = Pool().get('currency.currency.rate')
if (self.amount is not None) and \
(self.amount_2nd_currency is not None) and \
(self.currency2nd is not None):
if self.amount != Decimal('0.0'):
exp = Decimal(Decimal(1) / 10 ** Rate.rate.digits[1])
return (self.amount_2nd_currency / self.amount).quantize(exp)
@fields.depends('description')
def on_change_with_descr_short(self, name=None):
""" to speed up list-view
@ -613,6 +717,24 @@ class Line(Workflow, ModelSQL, ModelView):
else:
return 2
@fields.depends('currency', 'booktransf', '_parent_booktransf.currency')
def on_change_with_currency2nd(self, name=None):
""" currency of transfer-target
"""
if self.booktransf:
if self.currency:
if self.currency.id != self.booktransf.currency.id:
return self.booktransf.currency.id
@fields.depends('currency', 'booktransf', '_parent_booktransf.currency')
def on_change_with_currency2nd_digits(self, name=None):
""" currency of transfer-target
"""
if self.booktransf:
return self.booktransf.currency.digits
else:
return 2
@fields.depends('id', 'date', 'cashbook', \
'_parent_cashbook.id', 'reconciliation', \
'_parent_reconciliation.start_amount',\
@ -734,6 +856,33 @@ class Line(Workflow, ModelSQL, ModelView):
raise ValueError('invalid "bookingtype"')
return {}
@classmethod
def add_2nd_currency(cls, values):
""" add second currency amount if missing
"""
pool = Pool()
Currency = pool.get('currency.currency')
Cashbook = pool.get('cashbook.book')
cashbook = values.get('cashbook', None)
booktransf = values.get('booktransf', None)
amount = values.get('amount', None)
amount_2nd_currency = values.get('amount_2nd_currency', None)
if cashbook:
if booktransf:
if amount is not None:
if amount_2nd_currency is None:
cashbook = Cashbook(cashbook)
booktransf = Cashbook(booktransf)
if cashbook.currency.id != booktransf.currency.id:
values['amount_2nd_currency'] = Currency.compute(
cashbook.currency,
amount,
booktransf.currency,
)
return values
@classmethod
def update_amount_by_splitlines(cls, lines):
""" update amounts from split-lines
@ -862,6 +1011,7 @@ class Line(Workflow, ModelSQL, ModelView):
for values in vlist:
values.update(cls.get_debit_credit(values))
values.update(cls.clear_by_bookingtype(values))
values.update(cls.add_2nd_currency(values))
# deny add to reconciliation if state is not 'check' or 'done'
if values.get('reconciliation', None):