diff --git a/line.py b/line.py index 5c6dd01..d0dfc75 100644 --- a/line.py +++ b/line.py @@ -349,20 +349,7 @@ class Line(SecondCurrencyMixin, Workflow, ModelSQL, ModelView): if line.reference is None: if line.bookingtype in ['mvout', 'mvin']: # in case of 'mvin' or 'mvout' - add counterpart - values = { - 'cashbook': line.booktransf.id, - 'bookingtype': 'mvin' if line.bookingtype == 'mvout' else 'mvout', - 'date': line.date, - 'description': line.description, - 'booktransf': line.cashbook.id, - 'reference': line.id, - 'amount': line.amount \ - if line.currency.id == line.booktransf.currency.id \ - else line.amount_2nd_currency, - 'amount_2nd_currency': line.amount \ - if line.currency.id != line.booktransf.currency.id \ - else None, - } + values = cls.get_counterpart_values(line) values.update(cls.get_debit_credit(values)) to_create_line.append(values) elif line.bookingtype in ['spout', 'spin']: @@ -371,23 +358,18 @@ class Line(SecondCurrencyMixin, Workflow, ModelSQL, ModelView): if sp_line.splittype != 'tr': continue - values = { + values = cls.get_counterpart_values(line, { 'cashbook': sp_line.booktransf.id, - 'date': line.date, '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' + 'bookingtype': 'mvin' \ + if line.bookingtype.endswith('out') else 'mvout', + }) values.update(cls.get_debit_credit(values)) to_create_line.append(values) @@ -784,6 +766,32 @@ class Line(SecondCurrencyMixin, Workflow, ModelSQL, ModelView): values2['category'] = None return values2 + @classmethod + def get_counterpart_values(cls, line, values={}): + """ get values to create counter-part of + transfer booking + """ + line_currency = getattr(line.currency, 'id', None) + booktransf_currency = getattr(getattr(line.booktransf, 'currency', {}), 'id', None) + + result = { + 'cashbook': getattr(line.booktransf, 'id', None), + 'bookingtype': 'mvin' if line.bookingtype == 'mvout' else 'mvout', + 'date': line.date, + 'description': line.description, + 'booktransf': line.cashbook.id, + 'reference': line.id, + 'amount': line.amount \ + if line_currency == booktransf_currency \ + else line.amount_2nd_currency, + 'amount_2nd_currency': line.amount \ + if line_currency != booktransf_currency \ + else None, + } + # update values from 'values' + result.update(values) + return result + @classmethod def get_debit_credit(cls, values): """ compute debit/credit from amount