# -*- 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.wizard import Wizard, StateView, StateTransition, Button from trytond.i18n import gettext from trytond.pool import Pool from trytond.transaction import Transaction from trytond.pyson import Eval from decimal import Decimal from .line import sel_bookingtype class EnterBookingStart(ModelView): 'Enter Booking' __name__ = 'cashbook.enterbooking.start' cashbook = fields.Many2One(string='Cashbook', model_name='cashbook.book', domain=[('id', 'in', Eval('cashbooks', []))], depends=['cashbooks'], required=True) cashbooks = fields.One2Many(string='Cashbooks', field=None, model_name='cashbook.book', readonly=True, states={'invisible': True}) balance = fields.Numeric(string='Balance', readonly=True, digits=(16, Eval('currency_digits', 2)), depends=['currency_digits']) currency = fields.Function(fields.Many2One(string='Currency', model_name='currency.currency', states={'invisible': True}), 'on_change_with_currency') currency_digits = fields.Function(fields.Integer(string='Currency Digits', readonly=True, states={'invisible': True}), 'on_change_with_currency_digits') bookingtype = fields.Selection(string='Type', required=True, selection=sel_bookingtype) amount = fields.Numeric(string='Amount', depends=['currency_digits', 'bookingtype'], digits=(16, Eval('currency_digits', 2)), domain=[('amount', '>=', Decimal('0.0'))], states={ 'readonly': Eval('bookingtype', '').in_(['spin', 'spout']), 'required': Eval('bookingtype', '').in_(['in', 'out', 'mvin', 'mvout']), }) @fields.depends('cashbook', 'balance') def on_change_cashbook(self): """ get balance of selected cashbook """ if self.cashbook: self.balance = self.cashbook.balance else : self.balance = None @fields.depends('cashbook', '_parent_cashbook.currency') def on_change_with_currency(self, name=None): """ digits """ if self.cashbook: return self.cashbook.currency.id @fields.depends('cashbook', '_parent_cashbook.currency') def on_change_with_currency_digits(self, name=None): """ digits """ if self.cashbook: return self.cashbook.currency.digits else : return 2 # end EnterBookingStart class EnterBookingWizard(Wizard): 'Enter Booking' __name__ = 'cashbook.enterbooking' start_state = 'start' start = StateView('cashbook.enterbooking.start', 'cashbook.enterbooking_start_form', [ Button('Cancel', 'end', 'tryton-cancel'), Button('Save', 'save_', 'tryton-add', default=True), ]) save_ = StateTransition() def default_start(self, fields): """ setup form """ pool = Pool() Cashbook = pool.get('cashbook.book') result = { 'cashbooks': [x.id for x in Cashbook.search([ ('state', '=', 'open'), ('owner.id', '=', Transaction().user), ])], 'bookingtype': 'out', } return result def transition_save(self): """ store booking """ return 'end' # end EnterBookingWizard