2022-09-06 15:26:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-01-15 10:03:50 +00:00
|
|
|
# This file is part of the cashbook-module from m-ds.de for Tryton.
|
2022-09-06 15:26:53 +00:00
|
|
|
# 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.pool import Pool
|
|
|
|
from trytond.transaction import Transaction
|
2024-05-30 10:44:11 +00:00
|
|
|
from trytond.pyson import Eval, Bool, If, And
|
2022-09-06 15:26:53 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
from .line import sel_bookingtype
|
|
|
|
|
2022-09-07 09:36:37 +00:00
|
|
|
sel_booktypewiz = [x for x in sel_bookingtype if not x[0] in ['spin', 'spout']]
|
|
|
|
|
2022-09-06 15:26:53 +00:00
|
|
|
|
|
|
|
class EnterBookingStart(ModelView):
|
|
|
|
'Enter Booking'
|
|
|
|
__name__ = 'cashbook.enterbooking.start'
|
|
|
|
|
2023-05-18 10:15:53 +00:00
|
|
|
cashbook = fields.Many2One(
|
|
|
|
string='Cashbook', model_name='cashbook.book',
|
2022-09-16 08:15:51 +00:00
|
|
|
domain=[('id', 'in', Eval('cashbooks', [])), ('btype', '!=', None)],
|
2022-09-06 15:26:53 +00:00
|
|
|
depends=['cashbooks'], required=True)
|
2023-05-18 10:15:53 +00:00
|
|
|
cashbooks = fields.One2Many(
|
|
|
|
string='Cashbooks', field=None,
|
2022-09-06 15:26:53 +00:00
|
|
|
model_name='cashbook.book', readonly=True,
|
|
|
|
states={'invisible': True})
|
2023-05-18 10:15:53 +00:00
|
|
|
owner_cashbook = fields.Function(fields.Many2One(
|
|
|
|
string='Owner', readonly=True,
|
2022-09-07 09:36:37 +00:00
|
|
|
states={'invisible': True}, model_name='res.user'),
|
|
|
|
'on_change_with_owner_cashbook')
|
2023-05-18 10:15:53 +00:00
|
|
|
currency = fields.Function(fields.Many2One(
|
|
|
|
string='Currency',
|
2022-09-06 15:26:53 +00:00
|
|
|
model_name='currency.currency', states={'invisible': True}),
|
|
|
|
'on_change_with_currency')
|
2023-05-18 10:15:53 +00:00
|
|
|
currency_digits = fields.Function(fields.Integer(
|
|
|
|
string='Currency Digits',
|
2022-09-06 15:26:53 +00:00
|
|
|
readonly=True, states={'invisible': True}),
|
|
|
|
'on_change_with_currency_digits')
|
2023-05-18 10:15:53 +00:00
|
|
|
bookingtype = fields.Selection(
|
|
|
|
string='Type', required=True, selection=sel_booktypewiz)
|
|
|
|
amount = fields.Numeric(
|
|
|
|
string='Amount',
|
2022-09-06 15:26:53 +00:00
|
|
|
depends=['currency_digits', 'bookingtype'],
|
2022-09-07 09:36:37 +00:00
|
|
|
digits=(16, Eval('currency_digits', 2)), required=True,
|
|
|
|
domain=[('amount', '>=', Decimal('0.0'))])
|
2024-05-30 10:44:11 +00:00
|
|
|
description = fields.Text(
|
|
|
|
string='Description', states={'required': Bool(Eval('fixate'))},
|
|
|
|
depends=['fixate'])
|
2023-05-18 10:15:53 +00:00
|
|
|
category = fields.Many2One(
|
|
|
|
string='Category',
|
2022-09-07 09:36:37 +00:00
|
|
|
model_name='cashbook.category', depends=['bookingtype'],
|
|
|
|
states={
|
2023-11-29 14:19:38 +00:00
|
|
|
'readonly': ~Bool(Eval('bookingtype')),
|
2022-09-07 09:36:37 +00:00
|
|
|
'required': Eval('bookingtype', '').in_(['in', 'out']),
|
|
|
|
'invisible': ~Eval('bookingtype', '').in_(['in', 'out']),
|
|
|
|
},
|
|
|
|
domain=[
|
|
|
|
If(
|
|
|
|
Eval('bookingtype', '').in_(['in', 'mvin']),
|
|
|
|
('cattype', '=', 'in'),
|
|
|
|
('cattype', '=', 'out'),
|
|
|
|
)])
|
2024-05-30 10:44:11 +00:00
|
|
|
fixate = fields.Boolean(
|
|
|
|
string='Fixate', help='The booking is fixed immediately.')
|
2022-09-07 09:36:37 +00:00
|
|
|
|
|
|
|
# party or cashbook as counterpart
|
2023-05-18 10:15:53 +00:00
|
|
|
booktransf = fields.Many2One(
|
|
|
|
string='Source/Dest',
|
2022-09-07 09:36:37 +00:00
|
|
|
model_name='cashbook.book',
|
|
|
|
domain=[
|
|
|
|
('owner.id', '=', Eval('owner_cashbook', -1)),
|
|
|
|
('id', '!=', Eval('cashbook', -1)),
|
|
|
|
],
|
|
|
|
states={
|
|
|
|
'invisible': ~Eval('bookingtype', '').in_(['mvin', 'mvout']),
|
|
|
|
'required': Eval('bookingtype', '').in_(['mvin', 'mvout']),
|
|
|
|
}, depends=['bookingtype', 'owner_cashbook', 'cashbook'])
|
2023-05-18 10:15:53 +00:00
|
|
|
party = fields.Many2One(
|
|
|
|
string='Party', model_name='party.party',
|
2022-09-06 15:26:53 +00:00
|
|
|
states={
|
2022-09-07 09:36:37 +00:00
|
|
|
'invisible': ~Eval('bookingtype', '').in_(['in', 'out']),
|
2024-05-30 10:44:11 +00:00
|
|
|
'required': And(
|
|
|
|
Bool(Eval('fixate')),
|
|
|
|
Eval('bookingtype', '').in_(['in', 'out']))},
|
|
|
|
depends=['bookingtype', 'fixate'])
|
2022-09-07 09:36:37 +00:00
|
|
|
|
|
|
|
@fields.depends('bookingtype', 'category')
|
|
|
|
def on_change_bookingtype(self):
|
|
|
|
""" clear category if not valid type
|
|
|
|
"""
|
|
|
|
types = {
|
|
|
|
'in': ['in', 'mvin'],
|
|
|
|
'out': ['out', 'mvout'],
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.bookingtype:
|
|
|
|
if self.category:
|
2023-11-29 14:19:38 +00:00
|
|
|
if self.bookingtype not in types.get(
|
|
|
|
self.category.cattype, ''):
|
2022-09-07 09:36:37 +00:00
|
|
|
self.category = None
|
2022-09-06 15:26:53 +00:00
|
|
|
|
2022-09-07 09:36:37 +00:00
|
|
|
@fields.depends('cashbook', '_parent_cashbook.owner')
|
|
|
|
def on_change_with_owner_cashbook(self, name=None):
|
|
|
|
""" get current owner
|
|
|
|
"""
|
|
|
|
if self.cashbook:
|
|
|
|
return self.cashbook.owner.id
|
|
|
|
|
2022-09-06 15:26:53 +00:00
|
|
|
@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
|
2023-05-18 10:15:53 +00:00
|
|
|
else:
|
2022-09-06 15:26:53 +00:00
|
|
|
return 2
|
|
|
|
|
|
|
|
# end EnterBookingStart
|
|
|
|
|
|
|
|
|
|
|
|
class EnterBookingWizard(Wizard):
|
|
|
|
'Enter Booking'
|
|
|
|
__name__ = 'cashbook.enterbooking'
|
|
|
|
|
|
|
|
start_state = 'start'
|
2023-05-18 10:15:53 +00:00
|
|
|
start = StateView(
|
|
|
|
'cashbook.enterbooking.start',
|
2022-09-06 15:26:53 +00:00
|
|
|
'cashbook.enterbooking_start_form', [
|
|
|
|
Button('Cancel', 'end', 'tryton-cancel'),
|
2022-09-07 09:36:37 +00:00
|
|
|
Button('Save', 'save_', 'tryton-save', default=True),
|
|
|
|
Button('Save & Next', 'savenext_', 'tryton-forward'),
|
2022-09-06 15:26:53 +00:00
|
|
|
])
|
|
|
|
save_ = StateTransition()
|
2022-09-07 09:36:37 +00:00
|
|
|
savenext_ = StateTransition()
|
2022-09-06 15:26:53 +00:00
|
|
|
|
|
|
|
def default_start(self, fields):
|
|
|
|
""" setup form
|
|
|
|
"""
|
|
|
|
pool = Pool()
|
|
|
|
Cashbook = pool.get('cashbook.book')
|
2022-09-07 09:36:37 +00:00
|
|
|
Configuration = pool.get('cashbook.configuration')
|
|
|
|
|
|
|
|
cfg1 = Configuration.get_singleton()
|
2022-09-06 15:26:53 +00:00
|
|
|
|
2022-09-18 10:52:31 +00:00
|
|
|
book_ids = []
|
|
|
|
for x in ['defbook', 'book1', 'book2', 'book3', 'book4', 'book5']:
|
|
|
|
if getattr(cfg1, x, None) is not None:
|
|
|
|
book_ids.append(getattr(cfg1, x, None).id)
|
|
|
|
|
2022-09-06 15:26:53 +00:00
|
|
|
result = {
|
2024-06-01 09:13:47 +00:00
|
|
|
'fixate': cfg1.fixate
|
|
|
|
if cfg1 and cfg1.fixate is not None else False,
|
2022-09-06 15:26:53 +00:00
|
|
|
'cashbooks': [x.id for x in Cashbook.search([
|
|
|
|
('state', '=', 'open'),
|
2022-09-16 08:15:51 +00:00
|
|
|
('btype', '!=', None),
|
2023-07-24 15:31:34 +00:00
|
|
|
('owner', '=', Transaction().user),
|
2022-09-18 10:52:31 +00:00
|
|
|
('id', 'in', book_ids),
|
2022-09-06 15:26:53 +00:00
|
|
|
])],
|
2022-09-07 09:36:37 +00:00
|
|
|
'bookingtype': getattr(self.start, 'bookingtype', 'out'),
|
|
|
|
'cashbook': getattr(getattr(cfg1, 'defbook', None), 'id', None),
|
|
|
|
'amount': None,
|
|
|
|
'party': None,
|
|
|
|
'booktransf': None,
|
|
|
|
'description': None,
|
|
|
|
'category': None,
|
2022-09-06 15:26:53 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
|
2022-09-07 09:36:37 +00:00
|
|
|
def transition_save_(self):
|
2022-09-06 15:26:53 +00:00
|
|
|
""" store booking
|
|
|
|
"""
|
2022-09-07 09:36:37 +00:00
|
|
|
pool = Pool()
|
|
|
|
Line = pool.get('cashbook.line')
|
|
|
|
IrDate = pool.get('ir.date')
|
|
|
|
|
|
|
|
query = {
|
|
|
|
'cashbook': self.start.cashbook.id,
|
|
|
|
'description': self.start.description,
|
|
|
|
'date': IrDate.today(),
|
|
|
|
'bookingtype': self.start.bookingtype,
|
|
|
|
'amount': self.start.amount,
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.start.bookingtype in ['in', 'out']:
|
|
|
|
query['category'] = self.start.category.id
|
|
|
|
query['party'] = getattr(self.start.party, 'id', None)
|
|
|
|
elif self.start.bookingtype in ['mvin', 'mvout']:
|
|
|
|
query['booktransf'] = self.start.booktransf.id
|
|
|
|
|
2024-05-30 10:44:11 +00:00
|
|
|
lines = Line.create([query])
|
|
|
|
if self.start.fixate:
|
|
|
|
Line.wfcheck(lines)
|
2022-09-06 15:26:53 +00:00
|
|
|
return 'end'
|
|
|
|
|
2022-09-07 09:36:37 +00:00
|
|
|
def transition_savenext_(self):
|
|
|
|
""" store booking & restart
|
|
|
|
"""
|
|
|
|
self.transition_save_()
|
|
|
|
return 'start'
|
|
|
|
|
2022-09-06 15:26:53 +00:00
|
|
|
# end EnterBookingWizard
|