# -*- 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 Workflow, ModelView, ModelSQL, fields, Check from trytond.pyson import Eval from trytond.exceptions import UserError from trytond.i18n import gettext STATES = { 'readonly': Eval('state', '') != 'open', } DEPENDS=['state'] sel_state_book = [ ('open', 'Open'), ('closed', 'Closed'), ('archive', 'Archive'), ] class Book(Workflow, ModelSQL, ModelView): 'Cashbook' __name__ = 'cashbook.book' name = fields.Char(string='Name', required=True, states=STATES, depends=DEPENDS) btype = fields.Many2One(string='Type', required=True, model_name='cashbook.type', ondelete='RESTRICT', states=STATES, depends=DEPENDS) lines = fields.One2Many(string='Lines', field='cashbook', model_name='cashbook.line', states=STATES, depends=DEPENDS) state = fields.Selection(string='State', required=True, readonly=True, selection=sel_state_book) state_string = state.translated('state') @classmethod def __setup__(cls): super(Book, cls).__setup__() cls._order.insert(0, ('name', 'ASC')) t = cls.__table__() cls._sql_constraints.extend([ ('state_val', Check(t, t.state.in_(['open', 'closed', 'archive'])), 'cashbook.msg_book_wrong_state_value'), ]) cls._transitions |= set(( ('open', 'closed'), ('closed', 'open'), ('closed', 'archive'), )) cls._buttons.update({ 'wfopen': { 'invisible': Eval('state', '') != 'closed', 'depends': ['state'], }, 'wfclosed': { 'invisible': Eval('state') != 'open', 'depends': ['state'], }, 'wfarchive': { 'invisible': Eval('state') != 'closed', 'depends': ['state'], }, }) @classmethod def default_state(cls): return 'open' @classmethod @ModelView.button @Workflow.transition('open') def wfopen(cls, books): """ open cashbook """ pass @classmethod @ModelView.button @Workflow.transition('closed') def wfclosed(cls, books): """ cashbook is closed """ pass @classmethod @ModelView.button @Workflow.transition('archive') def wfarchive(cls, books): """ cashbook is archived """ pass @classmethod def write(cls, *args): """ deny update if book is not 'open' """ actions = iter(args) for books, values in zip(actions, actions): for book in books: if book.state != 'open': # allow state-update, if its the only action if not (('state' in values.keys()) and (len(values.keys()) == 1)): raise UserError(gettext( 'cashbook.msg_book_deny_write', bookname = book.rec_name, state_txt = book.state_string, )) super(Book, cls).write(*args) @classmethod def delete(cls, books): """ deny delete if book has lines """ for book in books: if (len(book.lines) > 0) and (book.state != 'archive'): raise UserError(gettext( 'cashbook.msg_book_deny_delete', bookname = book.rec_name, booklines = len(book.lines), )) return super(Book, cls).delete(books) # end Book