line/splitline: fremdwährung ok+test+migration
This commit is contained in:
parent
0f6180ebdb
commit
25dcdde09b
10 changed files with 485 additions and 217 deletions
257
line.py
257
line.py
|
@ -10,12 +10,12 @@ 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 import Literal
|
||||
from sql.functions import DatePart
|
||||
from sql.conditionals import Case
|
||||
from .book import sel_state_book
|
||||
from .mixin import SecondCurrencyMixin
|
||||
|
||||
|
||||
sel_payee = [
|
||||
|
@ -47,7 +47,7 @@ STATES = {
|
|||
DEPENDS=['state', 'state_cashbook']
|
||||
|
||||
|
||||
class Line(Workflow, ModelSQL, ModelView):
|
||||
class Line(SecondCurrencyMixin, Workflow, ModelSQL, ModelView):
|
||||
'Cashbook Line'
|
||||
__name__ = 'cashbook.line'
|
||||
|
||||
|
@ -121,23 +121,6 @@ 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={
|
||||
|
@ -180,10 +163,6 @@ 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)
|
||||
|
@ -203,6 +182,7 @@ class Line(Workflow, ModelSQL, ModelView):
|
|||
|
||||
table = cls.__table_handler__(module_name)
|
||||
table.drop_constraint('amount_val')
|
||||
cls.migrate_amount_2nd_currency()
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
|
@ -236,6 +216,48 @@ class Line(Workflow, ModelSQL, ModelView):
|
|||
},
|
||||
})
|
||||
|
||||
@classmethod
|
||||
def migrate_amount_2nd_currency(cls):
|
||||
""" add amount-2nd-currency
|
||||
"""
|
||||
pool = Pool()
|
||||
Line2 = pool.get('cashbook.line')
|
||||
Book = pool.get('cashbook.book')
|
||||
Book2 = pool.get('cashbook.book')
|
||||
tab_line = Line2.__table__()
|
||||
tab_book = Book.__table__() # cashbook of line
|
||||
tab_book2 = Book2.__table__() # transfer-target
|
||||
cursor = Transaction().connection.cursor()
|
||||
|
||||
query = tab_line.join(tab_book,
|
||||
condition=tab_line.cashbook == tab_book.id,
|
||||
).join(tab_book2,
|
||||
condition=tab_line.booktransf== tab_book2.id,
|
||||
).select(tab_line.id,
|
||||
where=tab_line.bookingtype.in_(['mvin', 'mvout']) & \
|
||||
(tab_line.amount_2nd_currency == None) & \
|
||||
(tab_book.currency != tab_book2.currency)
|
||||
)
|
||||
lines = Line2.search([('id', 'in', query)])
|
||||
to_write = []
|
||||
for line in lines:
|
||||
values = Line2.add_2nd_currency({
|
||||
'date': line.date,
|
||||
'booktransf': line.booktransf.id,
|
||||
'amount': line.amount,
|
||||
}, line.currency)
|
||||
if 'amount_2nd_currency' in values.keys():
|
||||
values['id'] = line.id
|
||||
to_write.append(values)
|
||||
|
||||
for line in to_write:
|
||||
qu1 = tab_line.update(
|
||||
columns = [tab_line.amount_2nd_currency],
|
||||
values = [line['amount_2nd_currency']],
|
||||
where = tab_line.id == line['id'],
|
||||
)
|
||||
cursor.execute(*qu1)
|
||||
|
||||
@classmethod
|
||||
@ModelView.button
|
||||
@Workflow.transition('edit')
|
||||
|
@ -315,10 +337,10 @@ class Line(Workflow, ModelSQL, ModelView):
|
|||
'booktransf': line.cashbook.id,
|
||||
'reference': line.id,
|
||||
'amount': line.amount \
|
||||
if line.cashbook.currency.id == line.booktransf.currency.id \
|
||||
if line.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 \
|
||||
if line.currency.id != line.booktransf.currency.id \
|
||||
else None,
|
||||
}
|
||||
values.update(cls.get_debit_credit(values))
|
||||
|
@ -335,15 +357,17 @@ class Line(Workflow, ModelSQL, ModelView):
|
|||
'description': sp_line.description,
|
||||
'booktransf': line.cashbook.id,
|
||||
'reference': line.id,
|
||||
'amount': sp_line.amount \
|
||||
if sp_line.currency.id == sp_line.booktransf.currency.id \
|
||||
else sp_line.amount_2nd_currency,
|
||||
'amount_2nd_currency': sp_line.amount \
|
||||
if sp_line.currency.id != sp_line.booktransf.currency.id \
|
||||
else None,
|
||||
}
|
||||
if line.bookingtype.endswith('out'):
|
||||
values['bookingtype'] = 'mvin'
|
||||
else :
|
||||
values['bookingtype'] = 'mvout'
|
||||
values.update(line.get_amount_by_second_currency(
|
||||
sp_line.booktransf.currency,
|
||||
amount = sp_line.amount,
|
||||
))
|
||||
values.update(cls.get_debit_credit(values))
|
||||
to_create_line.append(values)
|
||||
|
||||
|
@ -431,26 +455,6 @@ class Line(Workflow, ModelSQL, ModelView):
|
|||
'type': gettext('cashbook.msg_line_bookingtype_%s' % self.bookingtype),
|
||||
}
|
||||
|
||||
def get_amount_by_second_currency(self, to_currency, amount=None):
|
||||
""" get amount, calculate credit/debit from currency of current
|
||||
cashbook to 'to_currency'
|
||||
"""
|
||||
Currency = Pool().get('currency.currency')
|
||||
|
||||
values = {
|
||||
'amount': amount if amount is not None else self.amount,
|
||||
}
|
||||
|
||||
if to_currency.id != self.cashbook.currency.id:
|
||||
with Transaction().set_context({
|
||||
'date': self.date,
|
||||
}):
|
||||
values['amount'] = Currency.compute(
|
||||
self.cashbook.currency,
|
||||
values['amount'],
|
||||
to_currency)
|
||||
return values
|
||||
|
||||
@staticmethod
|
||||
def order_state(tables):
|
||||
""" edit = 0, check/done = 1
|
||||
|
@ -541,7 +545,8 @@ class Line(Workflow, ModelSQL, ModelView):
|
|||
"""
|
||||
self.amount = sum([x.amount for x in self.splitlines if x.amount is not None])
|
||||
|
||||
@fields.depends('bookingtype', 'category', 'splitlines')
|
||||
@fields.depends('bookingtype', 'category', 'splitlines', 'booktransf',\
|
||||
'currency2nd')
|
||||
def on_change_bookingtype(self):
|
||||
""" clear category if not valid type
|
||||
"""
|
||||
|
@ -567,98 +572,7 @@ class Line(Workflow, ModelSQL, ModelView):
|
|||
else : # category
|
||||
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):
|
||||
""" update amount_2nd_currency
|
||||
"""
|
||||
self.on_change_rate_2nd_currency()
|
||||
|
||||
@fields.depends('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) or \
|
||||
(self.booktransf is None):
|
||||
return
|
||||
|
||||
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', '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):
|
||||
if self.amount != Decimal('0.0'):
|
||||
exp = Decimal(Decimal(1) / 10 ** Rate.rate.digits[1])
|
||||
return (self.amount_2nd_currency / self.amount).quantize(exp)
|
||||
self.currency2nd = self.on_change_with_currency2nd()
|
||||
|
||||
@fields.depends('description')
|
||||
def on_change_with_descr_short(self, name=None):
|
||||
|
@ -733,24 +647,6 @@ 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',\
|
||||
|
@ -872,38 +768,6 @@ 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')
|
||||
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 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
|
||||
def update_amount_by_splitlines(cls, lines):
|
||||
""" update amounts from split-lines
|
||||
|
@ -1028,11 +892,16 @@ class Line(Workflow, ModelSQL, ModelView):
|
|||
def create(cls, vlist):
|
||||
""" add debit/credit
|
||||
"""
|
||||
Cashbook = Pool().get('cashbook.book')
|
||||
|
||||
vlist = [x.copy() for x in vlist]
|
||||
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))
|
||||
|
||||
cashbook = values.get('cashbook', None)
|
||||
if cashbook:
|
||||
values.update(cls.add_2nd_currency(values, Cashbook(cashbook).currency))
|
||||
|
||||
# deny add to reconciliation if state is not 'check' or 'done'
|
||||
if values.get('reconciliation', None):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue