cashbook/wizard_openline.py

177 lines
5.4 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.
from trytond.model import ModelView, fields
from trytond.pyson import PYSONEncoder
2022-08-09 13:08:41 +00:00
from trytond.wizard import Wizard, StateView, StateTransition, StateAction, Button
from trytond.i18n import gettext
from trytond.pool import Pool
2022-10-06 11:28:42 +00:00
from trytond.exceptions import UserError
2022-08-09 13:08:41 +00:00
from trytond.transaction import Transaction
class OLineMixin:
""" mixin to extend action-data
"""
def add_action_data(self, book):
""" add book and cfg
"""
Configuration = Pool().get('cashbook.configuration')
cfg1 = Configuration.get_singleton()
action = {
'pyson_context': PYSONEncoder().encode({
'cashbook': getattr(book, 'id', None),
'date_from': getattr(cfg1, 'date_from', None),
'date_to': getattr(cfg1, 'date_to', None),
'checked': getattr(cfg1, 'checked', None),
'done': getattr(cfg1, 'done', None),
}),
'name' : '%(name)s: %(cashbook)s' % {
'name': gettext('cashbook.msg_name_cashbook'),
'cashbook': getattr(book, 'rec_name', '-/-'),
},
}
return action
# OLineMixin
class OpenCashBookStart(ModelView):
'Open Cashbook'
2022-08-08 12:31:42 +00:00
__name__ = 'cashbook.open_lines.start'
2022-08-08 12:31:42 +00:00
cashbook = fields.Many2One(string='Cashbook', model_name='cashbook.book',
required=True, domain=[('btype', '!=', None)])
2022-08-08 12:31:42 +00:00
checked = fields.Boolean(string='Checked', help="Show cashbook lines in Checked-state.")
done = fields.Boolean(string='Done', help="Show cashbook lines in Done-state")
date_from = fields.Date(string='Start Date')
date_to = fields.Date(string='End Date')
@classmethod
def default_checked(cls):
return True
@classmethod
def default_done(cls):
return False
# end OpenCashBookStart
class OpenCashBook(OLineMixin, Wizard):
'Open Cashbook'
2022-08-08 12:31:42 +00:00
__name__ = 'cashbook.open_lines'
2022-08-09 13:08:41 +00:00
start_state = 'check'
check = StateTransition()
askuser = StateView('cashbook.open_lines.start',
2022-08-08 12:31:42 +00:00
'cashbook.open_lines_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Open', 'open_', 'tryton-ok', default=True),
])
open_ = StateAction('cashbook.act_line_view')
2022-08-09 13:08:41 +00:00
def transition_check(self):
""" dont ask and open cashbook if user has 1x only
"""
Book = Pool().get('cashbook.book')
2022-08-09 13:08:41 +00:00
with Transaction().set_context({
'_check_access': True,
}):
books = Book.search([('btype', '!=', None)])
2022-08-09 13:08:41 +00:00
if len(books) == 1:
return 'open_'
return 'askuser'
2022-08-09 13:08:41 +00:00
def default_askuser(self, fields):
""" setup form
"""
Configuration = Pool().get('cashbook.configuration')
cfg1 = Configuration.get_singleton()
result = {
'date_from': getattr(cfg1, 'date_from', None),
'date_to': getattr(cfg1, 'date_to', None),
'checked': getattr(cfg1, 'checked', True),
'done': getattr(cfg1, 'done', False),
}
return result
def do_open_(self, action):
2022-08-09 13:08:41 +00:00
""" open view, use data from dialog
"""
pool = Pool()
Book = pool.get('cashbook.book')
Configuration = pool.get('cashbook.configuration')
cfg1 = Configuration.get_singleton()
if cfg1 is None:
cfg1 = Configuration()
cfg1.save()
2022-08-09 13:08:41 +00:00
book = getattr(self.askuser, 'cashbook', None)
if book is None:
with Transaction().set_context({
'_check_access': True,
}):
books = Book.search([('btype', '!=', None)])
2022-08-09 13:08:41 +00:00
if len(books) > 0:
book = books[0]
# save settings
cfg1.date_from = getattr(self.askuser, 'date_from', None)
cfg1.date_to = getattr(self.askuser, 'date_to', None)
cfg1.checked = getattr(self.askuser, 'checked', True)
cfg1.done = getattr(self.askuser, 'done', False)
2022-08-09 13:08:41 +00:00
cfg1.save()
action.update(self.add_action_data(book))
return action, {}
def transition_open_(self):
return 'end'
# end OpenCashBook
2022-10-06 11:28:42 +00:00
class OpenCashBookTree(OLineMixin, Wizard):
2022-10-06 11:28:42 +00:00
'Open Cashbook2'
__name__ = 'cashbook.open_lines_tree'
start_state = 'open_'
open_ = StateAction('cashbook.act_line_view')
def do_open_(self, action):
""" open view from doubleclick
"""
pool = Pool()
Book = pool.get('cashbook.book')
Configuration = pool.get('cashbook.configuration')
cfg1 = Configuration.get_singleton()
if cfg1 is None:
cfg1 = Configuration()
cfg1.save()
book = self.record
if book is None:
with Transaction().set_context({
'_check_access': True,
}):
books = Book.search([('btype', '!=', None)])
if len(books) > 0:
book = books[0]
else :
if book.btype is None:
raise UserError(gettext(
'cashbook.msg_book_no_type_noopen',
bookname = book.rec_name,
))
action.update(self.add_action_data(book))
2022-10-06 11:28:42 +00:00
return action, {}
# end OpenCashBookTree