amount/rate/amount_2nd - ok+test

This commit is contained in:
Frederik Jaeckel 2022-10-03 23:36:04 +02:00
parent 517e2c5ad3
commit 0f6180ebdb
3 changed files with 106 additions and 49 deletions

107
line.py
View file

@ -105,6 +105,7 @@ class Line(Workflow, ModelSQL, ModelView):
domain=[
('owner.id', '=', Eval('owner_cashbook', -1)),
('id', '!=', Eval('cashbook', -1)),
('btype', '!=', None),
],
states={
'readonly': STATES['readonly'],
@ -313,8 +314,13 @@ class Line(Workflow, ModelSQL, ModelView):
'description': line.description,
'booktransf': line.cashbook.id,
'reference': line.id,
'amount': line.amount \
if line.cashbook.currency.id == line.booktransf.currency.id \
else line.amount_2nd_currency,
'amount_2nd_currency': line.amount \
if line.cashbook.currency.id != line.booktransf.currency.id \
else None,
}
values.update(line.get_amount_by_second_currency(line.booktransf.currency))
values.update(cls.get_debit_credit(values))
to_create_line.append(values)
elif line.bookingtype in ['spout', 'spin']:
@ -562,6 +568,31 @@ class Line(Workflow, ModelSQL, ModelView):
self.splitlines = []
self.booktransf = None
@fields.depends('booktransf', '_parent_booktransf.currency', 'date',\
'currency', 'amount', 'amount_2nd_currency')
def on_change_booktransf(self):
""" update amount_2nd_currency
"""
pool = Pool()
Currency = pool.get('currency.currency')
IrDate = pool.get('ir.date')
if (self.booktransf is None) or (self.currency is None):
return
if self.amount is not None:
if self.booktransf.currency.id != self.currency.id:
with Transaction().set_context({
'date': self.date or IrDate.today(),
}):
self.amount_2nd_currency = Currency.compute(
self.currency,
self.amount,
self.booktransf.currency
)
return
self.amount_2nd_currency = None
@fields.depends('currency', 'booktransf', '_parent_booktransf.currency', \
'amount', 'amount_2nd_currency', 'rate_2nd_currency')
def on_change_amount(self):
@ -570,38 +601,18 @@ class Line(Workflow, ModelSQL, ModelView):
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):
if (self.amount is None) or (self.rate_2nd_currency is None) or \
(self.booktransf 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
)
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):
@ -631,15 +642,20 @@ class Line(Workflow, ModelSQL, ModelView):
if len(to_write) > 0:
Line2.write(*to_write)
@fields.depends('amount', 'amount_2nd_currency', 'currency2nd')
@fields.depends('amount', 'amount_2nd_currency', 'rate_2nd_currency')
def on_change_amount_2nd_currency(self):
""" update rate_2nd_currency by rate
"""
self.rate_2nd_currency = self.on_change_with_rate_2nd_currency()
@fields.depends('amount', 'amount_2nd_currency')
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):
(self.amount_2nd_currency 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)
@ -863,24 +879,29 @@ class Line(Workflow, ModelSQL, ModelView):
pool = Pool()
Currency = pool.get('currency.currency')
Cashbook = pool.get('cashbook.book')
IrDate = pool.get('ir.date')
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,
)
if (cashbook is None) or (booktransf is None):
return values
if amount is not None:
if amount_2nd_currency is None:
cashbook = Cashbook(cashbook)
booktransf = Cashbook(booktransf)
if cashbook.currency.id != booktransf.currency.id:
with Transaction().set_context({
'date': values.get('date', IrDate.today()),
}):
values['amount_2nd_currency'] = Currency.compute(
cashbook.currency,
amount,
booktransf.currency,
)
return values
@classmethod