2022-08-24 15:12:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# This file is part of the cashbook-module from m-ds for Tryton.
|
|
|
|
# The COPYRIGHT file at the top level of this repository contains the
|
|
|
|
# full copyright notices and license terms.
|
|
|
|
|
|
|
|
|
|
|
|
from trytond.model import ModelView, ModelSQL, Workflow, fields, Check
|
|
|
|
from trytond.pool import Pool
|
|
|
|
from trytond.pyson import Eval, If
|
2022-08-25 13:55:24 +00:00
|
|
|
from trytond.report import Report
|
|
|
|
from trytond.i18n import gettext
|
|
|
|
from trytond.transaction import Transaction
|
2022-08-24 15:12:32 +00:00
|
|
|
from .line import sel_linetype, sel_bookingtype, STATES, DEPENDS
|
|
|
|
from .book import sel_state_book
|
|
|
|
|
|
|
|
|
2022-09-10 19:21:17 +00:00
|
|
|
sel_linetype = [
|
|
|
|
('cat', 'Category'),
|
|
|
|
('tr', 'Transfer'),
|
|
|
|
]
|
|
|
|
|
|
|
|
sel_target = [
|
|
|
|
('cashbook.book', 'Cashbook'),
|
|
|
|
('cashbook.category', 'Category'),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-08-24 15:12:32 +00:00
|
|
|
class SplitLine(ModelSQL, ModelView):
|
|
|
|
'Split booking line'
|
|
|
|
__name__ = 'cashbook.split'
|
|
|
|
|
|
|
|
line = fields.Many2One(string='Line', required=True,
|
|
|
|
select=True, ondelete='CASCADE', model_name='cashbook.line',
|
|
|
|
readonly=True)
|
|
|
|
description = fields.Text(string='Description',
|
|
|
|
states=STATES, depends=DEPENDS)
|
2022-09-10 19:21:17 +00:00
|
|
|
splittype = fields.Selection(string='Type', required=True,
|
|
|
|
help='Type of split booking line', selection=sel_linetype,
|
|
|
|
states=STATES, depends=DEPENDS)
|
2022-08-24 15:12:32 +00:00
|
|
|
category = fields.Many2One(string='Category',
|
|
|
|
model_name='cashbook.category', ondelete='RESTRICT',
|
2022-09-10 19:21:17 +00:00
|
|
|
states={
|
|
|
|
'readonly': STATES['readonly'],
|
|
|
|
'required': Eval('splittype', '') == 'cat',
|
|
|
|
'invisible': Eval('splittype', '') != 'cat',
|
|
|
|
}, depends=DEPENDS+['bookingtype', 'splittype'],
|
2022-08-24 15:12:32 +00:00
|
|
|
domain=[
|
|
|
|
If(
|
2022-08-25 13:55:24 +00:00
|
|
|
Eval('bookingtype', '') == 'spin',
|
2022-08-24 15:12:32 +00:00
|
|
|
('cattype', '=', 'in'),
|
|
|
|
('cattype', '=', 'out'),
|
|
|
|
)])
|
2022-08-25 13:55:24 +00:00
|
|
|
category_view = fields.Function(fields.Char(string='Category', readonly=True),
|
|
|
|
'on_change_with_category_view')
|
2022-09-10 19:21:17 +00:00
|
|
|
booktransf = fields.Many2One(string='Source/Dest',
|
|
|
|
ondelete='RESTRICT', model_name='cashbook.book',
|
|
|
|
domain=[
|
|
|
|
('owner.id', '=', Eval('owner_cashbook', -1)),
|
|
|
|
('id', '!=', Eval('cashbook', -1)),
|
|
|
|
],
|
|
|
|
states={
|
|
|
|
'readonly': STATES['readonly'],
|
|
|
|
'invisible': Eval('splittype', '') != 'tr',
|
|
|
|
'required': Eval('splittype', '') == 'tr',
|
|
|
|
}, depends=DEPENDS+['bookingtype', 'owner_cashbook', 'cashbook'])
|
|
|
|
|
2022-08-24 15:12:32 +00:00
|
|
|
amount = fields.Numeric(string='Amount', digits=(16, Eval('currency_digits', 2)),
|
|
|
|
required=True, states=STATES, depends=DEPENDS+['currency_digits'])
|
|
|
|
|
2022-09-27 15:54:28 +00:00
|
|
|
date = fields.Function(fields.Date(string='Date', readonly=True),
|
|
|
|
'on_change_with_date')
|
2022-09-10 19:21:17 +00:00
|
|
|
target = fields.Function(fields.Reference(string='Target', readonly=True,
|
|
|
|
selection=sel_target), 'on_change_with_target')
|
2022-08-24 15:12:32 +00:00
|
|
|
currency = fields.Function(fields.Many2One(model_name='currency.currency',
|
2022-08-25 13:55:24 +00:00
|
|
|
string="Currency", readonly=True), 'on_change_with_currency')
|
|
|
|
currency_digits = fields.Function(fields.Integer(string='Currency Digits',
|
|
|
|
readonly=True), 'on_change_with_currency_digits')
|
2022-08-24 15:12:32 +00:00
|
|
|
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')
|
2022-09-10 19:21:17 +00:00
|
|
|
cashbook = fields.Function(fields.Many2One(string='Cashbook',
|
|
|
|
readonly=True, states={'invisible': True}, model_name='cashbook.book'),
|
|
|
|
'on_change_with_cashbook')
|
2022-08-24 15:12:32 +00:00
|
|
|
state_cashbook = fields.Function(fields.Selection(string='State of Cashbook',
|
|
|
|
readonly=True, states={'invisible': True}, selection=sel_state_book),
|
2022-08-25 13:55:24 +00:00
|
|
|
'on_change_with_state_cashbook')
|
2022-09-10 19:21:17 +00:00
|
|
|
owner_cashbook = fields.Function(fields.Many2One(string='Owner', readonly=True,
|
|
|
|
states={'invisible': True}, model_name='res.user'),
|
|
|
|
'on_change_with_owner_cashbook')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def default_splittype(cls):
|
|
|
|
""" default category
|
|
|
|
"""
|
|
|
|
return 'cat'
|
2022-08-25 13:55:24 +00:00
|
|
|
|
|
|
|
def get_rec_name(self, name):
|
|
|
|
""" short + name
|
|
|
|
"""
|
2022-09-10 19:21:17 +00:00
|
|
|
return '%(type)s|%(amount)s %(symbol)s|%(desc)s [%(target)s]' % {
|
2022-08-25 13:55:24 +00:00
|
|
|
'desc': (self.description or '-')[:40],
|
|
|
|
'amount': Report.format_number(self.amount, None),
|
|
|
|
'symbol': getattr(self.currency, 'symbol', '-'),
|
2022-09-10 19:21:17 +00:00
|
|
|
'target': self.category_view if self.splittype == 'cat' else self.booktransf.rec_name,
|
2022-08-25 13:55:24 +00:00
|
|
|
'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
|
|
|
|
|
2022-09-10 19:21:17 +00:00
|
|
|
@fields.depends('splittype', 'category', 'booktransf')
|
|
|
|
def on_change_splittype(self):
|
|
|
|
""" clear category if not valid type
|
|
|
|
"""
|
|
|
|
if self.splittype:
|
|
|
|
if self.splittype == 'cat':
|
|
|
|
self.booktransf = None
|
|
|
|
if self.splittype == 'tr':
|
|
|
|
self.category = None
|
|
|
|
|
2022-09-27 15:54:28 +00:00
|
|
|
@fields.depends('line', '_parent_line.date')
|
|
|
|
def on_change_with_date(self, name=None):
|
|
|
|
""" get date of line
|
|
|
|
"""
|
|
|
|
if self.line:
|
|
|
|
if self.line.date is not None:
|
|
|
|
return self.line.date
|
|
|
|
|
2022-09-10 19:21:17 +00:00
|
|
|
@fields.depends('splittype', 'category', 'booktransf')
|
|
|
|
def on_change_with_target(self, name=None):
|
|
|
|
""" get category or cashbook
|
|
|
|
"""
|
|
|
|
if self.splittype:
|
|
|
|
if self.splittype == 'cat':
|
|
|
|
if self.category:
|
|
|
|
return 'cashbook.category,%d' % self.category.id
|
|
|
|
elif self.splittype == 'tr':
|
|
|
|
if self.booktransf:
|
|
|
|
return 'cashbook.book,%d' % self.booktransf.id
|
|
|
|
|
2022-08-25 13:55:24 +00:00
|
|
|
@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
|
2022-08-24 15:12:32 +00:00
|
|
|
|
|
|
|
@fields.depends('line', '_parent_line.state')
|
|
|
|
def on_change_with_state(self, name=None):
|
|
|
|
""" get state
|
|
|
|
"""
|
|
|
|
if self.line:
|
|
|
|
return self.line.state
|
|
|
|
|
2022-09-10 19:21:17 +00:00
|
|
|
@fields.depends('line', '_parent_line.cashbook')
|
|
|
|
def on_change_with_cashbook(self, name=None):
|
|
|
|
""" get cashbook
|
|
|
|
"""
|
|
|
|
if self.line:
|
|
|
|
return self.line.cashbook.id
|
|
|
|
|
2022-08-24 15:12:32 +00:00
|
|
|
@fields.depends('line', '_parent_line.cashbook')
|
|
|
|
def on_change_with_state_cashbook(self, name=None):
|
|
|
|
""" get state of cashbook
|
|
|
|
"""
|
|
|
|
if self.line:
|
|
|
|
return self.line.cashbook.state
|
|
|
|
|
2022-09-10 19:21:17 +00:00
|
|
|
@fields.depends('line', '_parent_line.cashbook')
|
|
|
|
def on_change_with_owner_cashbook(self, name=None):
|
|
|
|
""" get current owner
|
|
|
|
"""
|
|
|
|
if self.line:
|
|
|
|
return self.line.cashbook.owner.id
|
|
|
|
|
2022-08-24 15:12:32 +00:00
|
|
|
@fields.depends('line', '_parent_line.bookingtype')
|
|
|
|
def on_change_with_bookingtype(self, name=None):
|
|
|
|
""" get type
|
|
|
|
"""
|
|
|
|
if self.line:
|
|
|
|
return self.line.bookingtype
|
|
|
|
|
|
|
|
@fields.depends('line', '_parent_line.cashbook')
|
|
|
|
def on_change_with_currency(self, name=None):
|
|
|
|
""" currency of cashbook
|
|
|
|
"""
|
|
|
|
if self.line:
|
|
|
|
return self.line.cashbook.currency.id
|
|
|
|
|
|
|
|
@fields.depends('line', '_parent_line.cashbook')
|
|
|
|
def on_change_with_currency_digits(self, name=None):
|
|
|
|
""" currency-digits of cashbook
|
|
|
|
"""
|
|
|
|
if self.line:
|
|
|
|
return self.line.cashbook.currency.digits
|
|
|
|
else:
|
|
|
|
return 2
|
|
|
|
|
2022-08-25 13:55:24 +00:00
|
|
|
@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)
|
2022-09-28 14:02:24 +00:00
|
|
|
return records
|
2022-08-25 13:55:24 +00:00
|
|
|
|
|
|
|
@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)
|
|
|
|
|
2022-08-24 15:12:32 +00:00
|
|
|
# end SplitLine
|