splitbuchungen + test

This commit is contained in:
Frederik Jaeckel 2022-08-25 15:55:24 +02:00
parent bdbc9dc27f
commit aefb5cde51
9 changed files with 618 additions and 124 deletions

View file

@ -7,6 +7,9 @@
from trytond.model import ModelView, ModelSQL, Workflow, fields, Check
from trytond.pool import Pool
from trytond.pyson import Eval, If
from trytond.report import Report
from trytond.i18n import gettext
from trytond.transaction import Transaction
from .line import sel_linetype, sel_bookingtype, STATES, DEPENDS
from .book import sel_state_book
@ -22,28 +25,74 @@ class SplitLine(ModelSQL, ModelView):
states=STATES, depends=DEPENDS)
category = fields.Many2One(string='Category',
model_name='cashbook.category', ondelete='RESTRICT',
states=STATES, depends=DEPENDS+['bookingtype'],
required=True,
states=STATES, depends=DEPENDS+['bookingtype'], required=True,
domain=[
If(
Eval('bookingtype', '').in_(['in', 'mvin']),
Eval('bookingtype', '') == 'spin',
('cattype', '=', 'in'),
('cattype', '=', 'out'),
)])
category_view = fields.Function(fields.Char(string='Category', readonly=True),
'on_change_with_category_view')
amount = fields.Numeric(string='Amount', digits=(16, Eval('currency_digits', 2)),
required=True, states=STATES, depends=DEPENDS+['currency_digits'])
currency = fields.Function(fields.Many2One(model_name='currency.currency',
string="Currency"), 'on_change_with_currency')
currency_digits = fields.Function(fields.Integer(string='Currency Digits'),
'on_change_with_currency_digits')
string="Currency", readonly=True), 'on_change_with_currency')
currency_digits = fields.Function(fields.Integer(string='Currency Digits',
readonly=True), 'on_change_with_currency_digits')
bookingtype = fields.Function(fields.Selection(string='Type', readonly=True,
selection=sel_bookingtype), 'on_change_with_bookingtype')
state = fields.Function(fields.Selection(string='State', readonly=True,
selection=sel_linetype), 'on_change_with_state')
state_cashbook = fields.Function(fields.Selection(string='State of Cashbook',
readonly=True, states={'invisible': True}, selection=sel_state_book),
'on_change_with_state_cashbook', searcher='search_state_cashbook')
'on_change_with_state_cashbook')
def get_rec_name(self, name):
""" short + name
"""
return '%(type)s|%(amount)s %(symbol)s|%(desc)s [%(category)s]' % {
'desc': (self.description or '-')[:40],
'amount': Report.format_number(self.amount, None),
'symbol': getattr(self.currency, 'symbol', '-'),
'category': self.category_view,
'type': gettext('cashbook.msg_line_bookingtype_%s' % self.line.bookingtype),
}
def get_amount_by_second_currency(self, to_currency):
""" get amount, calculate credit/debit from currency of current
cashbook to 'to_currency'
"""
Currency = Pool().get('currency.currency')
values = {
'amount': self.amount,
}
if to_currency.id != self.line.cashbook.currency.id:
with Transaction().set_context({
'date': self.line.date,
}):
values['amount'] = Currency.compute(
self.line.cashbook.currency,
self.amount,
to_currency)
return values
@fields.depends('category')
def on_change_with_category_view(self, name=None):
""" show optimizef form of category for list-view
"""
Configuration = Pool().get('cashbook.configuration')
if self.category:
cfg1 = Configuration.get_singleton()
if getattr(cfg1, 'catnamelong', True) == True:
return self.category.rec_name
else :
return self.category.name
@fields.depends('line', '_parent_line.state')
def on_change_with_state(self, name=None):
@ -82,4 +131,51 @@ class SplitLine(ModelSQL, ModelView):
else:
return 2
@classmethod
def create(cls, vlist):
""" add debit/credit
"""
Line2 = Pool().get('cashbook.line')
vlist = [x.copy() for x in vlist]
records = super(SplitLine, cls).create(vlist)
to_update_line = []
for record in records:
if not record.line in to_update_line:
to_update_line.append(record.line)
if len(to_update_line) > 0:
Line2.update_amount_by_splitlines(to_update_line)
@classmethod
def write(cls, *args):
""" deny update if cashbook.line!='open',
add or update debit/credit
"""
Line2 = Pool().get('cashbook.line')
actions = iter(args)
to_update_line = []
for records, values in zip(actions, actions):
Line2.check_permission_write([x.line for x in records])
if 'amount' in values.keys():
for record in records:
if not record.line in to_update_line:
to_update_line.append(record.line)
super(SplitLine, cls).write(*args)
if len(to_update_line) > 0:
Line2.update_amount_by_splitlines(to_update_line)
@classmethod
def delete(cls, splitlines):
""" deny delete if book is not 'open' or wf is not 'edit'
"""
Line2 = Pool().get('cashbook.line')
Line2.check_permission_delete([x.line for x in splitlines])
return super(SplitLine, cls).delete(splitlines)
# end SplitLine