cashbook/line.py

258 lines
7.5 KiB
Python
Raw Normal View History

# -*- 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.
2022-08-08 12:31:42 +00:00
from trytond.model import ModelView, ModelSQL, Workflow, fields, Check
from trytond.pool import Pool
2022-08-08 12:31:42 +00:00
from trytond.pyson import Eval, If, Or
from trytond.transaction import Transaction
2022-08-08 12:31:42 +00:00
from trytond.report import Report
from trytond.exceptions import UserError
from trytond.i18n import gettext
from .book import sel_state_book
2022-08-09 13:08:41 +00:00
sel_linetype = [
('edit', 'Edit'),
('check', 'Checked'),
('done', 'Done'),
]
STATES = {
2022-08-08 12:31:42 +00:00
'readonly': Or(
Eval('state', '') != 'edit',
Eval('state_cashbook', '') != 'open',
),
}
2022-08-08 12:31:42 +00:00
DEPENDS=['state', 'state_cashbook']
class LineContext(ModelView):
'Line Context'
__name__ = 'cashbook.line.context'
2022-08-08 12:31:42 +00:00
cashbook = fields.Many2One(string='Cashbook', required=True,
model_name='cashbook.book',
states={
2022-08-08 12:31:42 +00:00
'readonly': Eval('num_cashbook', 0) < 2,
}, depends=['num_cashbook'])
date_from = fields.Date(string='Start Date', depends=['date_to'],
domain=[
If(Eval('date_to') & Eval('date_from'),
('date_from', '<=', Eval('date_to')),
()),
])
date_to = fields.Date(string='End Date', depends=['date_from'],
domain=[
If(Eval('date_to') & Eval('date_from'),
('date_from', '<=', Eval('date_to')),
()),
])
checked = fields.Boolean(string='Checked',
help='Show account lines in Checked-state.')
done = fields.Boolean(string='Done',
help='Show account lines in Done-state.')
2022-08-08 12:31:42 +00:00
num_cashbook = fields.Function(fields.Integer(string='Number of Cashbook',
readonly=True, states={'invisible': True}),
2022-08-08 12:31:42 +00:00
'on_change_with_num_cashbook')
@classmethod
2022-08-08 12:31:42 +00:00
def default_cashbook(cls):
""" get default from context
"""
context = Transaction().context
2022-08-08 12:31:42 +00:00
return context.get('cashbook', None)
@classmethod
def default_date_from(cls):
""" get default from context
"""
context = Transaction().context
return context.get('date_from', None)
@classmethod
def default_date_to(cls):
""" get default from context
"""
context = Transaction().context
return context.get('date_to', None)
@classmethod
def default_checked(cls):
""" get default from context
"""
context = Transaction().context
return context.get('checked', False)
2022-08-09 13:08:41 +00:00
@classmethod
def default_num_cashbook(cls):
""" get default from context
"""
CashBook = Pool().get('cashbook.book')
with Transaction().set_context({
'_check_access': True,
}):
return CashBook.search_count([])
@classmethod
def default_done(cls):
""" get default from context
"""
context = Transaction().context
return context.get('done', False)
2022-08-08 12:31:42 +00:00
def on_change_with_num_cashbook(self, name=None):
""" get number of accessible cashbooks,
depends on user-permissions
"""
2022-08-09 13:08:41 +00:00
LineContext = Pool().get('cashbook.line.context')
return LineContext.default_num_cashbook()
# end LineContext
class Line(Workflow, ModelSQL, ModelView):
2022-08-08 12:31:42 +00:00
'Cashbook Line'
__name__ = 'cashbook.line'
2022-08-08 12:31:42 +00:00
cashbook = fields.Many2One(string='Cashbook', required=True, select=True,
model_name='cashbook.book', ondelete='CASCADE', readonly=True)
date = fields.Date(string='Date', required=True, select=True,
states=STATES, depends=DEPENDS)
description = fields.Char(string='Description',
states=STATES, depends=DEPENDS)
state = fields.Selection(string='State', required=True, readonly=True,
select=True, selection=sel_linetype)
2022-08-08 12:31:42 +00:00
state_string = state.translated('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')
2022-08-09 13:08:41 +00:00
#image = fields.Binary...
@classmethod
def __setup__(cls):
super(Line, cls).__setup__()
cls._order.insert(0, ('date', 'ASC'))
2022-08-08 12:31:42 +00:00
t = cls.__table__()
cls._sql_constraints.extend([
('state_val',
Check(t, t.state.in_(['edit', 'check', 'done'])),
'cashbook.msg_line_wrong_state_value'),
])
cls._transitions |= set((
('edit', 'check'),
('check', 'done'),
('check', 'edit'),
))
cls._buttons.update({
'wfedit': {
'invisible': Eval('state', '') != 'check',
'depends': ['state'],
},
'wfcheck': {
'invisible': Eval('state') != 'edit',
'depends': ['state'],
},
'wfdone': {
'invisible': Eval('state') != 'check',
'depends': ['state'],
},
})
@classmethod
@ModelView.button
@Workflow.transition('edit')
def wfedit(cls, lines):
""" edit line
"""
pass
@classmethod
@ModelView.button
@Workflow.transition('check')
def wfcheck(cls, lines):
""" line is checked
"""
pass
@classmethod
@ModelView.button
@Workflow.transition('done')
def wfdone(cls, lines):
""" line is done
"""
pass
@classmethod
def default_state(cls):
""" default: edit
"""
return 'edit'
@classmethod
def default_date(cls):
""" default: today
"""
IrDate = Pool().get('ir.date')
return IrDate.today()
@classmethod
2022-08-08 12:31:42 +00:00
def default_cashbook(cls):
""" get default from context
"""
context = Transaction().context
2022-08-08 12:31:42 +00:00
return context.get('cashbook', None)
def get_rec_name(self, name):
""" short + name
"""
return '%(date)s %(desc)s' % {
'date': Report.format_date(self.date),
'desc': (self.description or '-')[:40],
}
@classmethod
def search_rec_name(cls, name, clause):
""" search in description +...
"""
return [('description',) + tuple(clause[1:])]
@fields.depends('cashbook', '_parent_cashbook.state')
def on_change_with_state_cashbook(self, name=None):
""" get state of cashbook
"""
if self.cashbook:
return self.cashbook.state
@classmethod
def search_state_cashbook(cls, names, clause):
""" search in state of cashbook
"""
return [('cashbook.state',) + tuple(clause[1:])]
@classmethod
def delete(cls, lines):
""" deny delete if book is not 'open' or wf is not 'edit'
"""
for line in lines:
if line.cashbook.state == 'closed':
raise UserError(gettext(
'cashbook.msg_line_deny_delete1',
linetxt = line.rec_name,
bookname = line.cashbook.rec_name,
bookstate = line.cashbook.state_string,
))
if line.state != 'edit':
raise UserError(gettext(
'cashbook.msg_line_deny_delete2',
linetxt = line.rec_name,
linestate = line.state_string,
))
return super(Line, cls).delete(lines)
# end Line