add booking with category + test, todo: booking of asset

This commit is contained in:
Frederik Jaeckel 2024-03-03 21:54:16 +01:00
parent e51abf589b
commit b86e421298
6 changed files with 372 additions and 19 deletions

View file

@ -3,6 +3,7 @@
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from decimal import Decimal
from datetime import date, timedelta
from dateutil.rrule import (
rrule, YEARLY, MONTHLY, WEEKLY, DAILY, MO, TU, WE, TH, FR, SA, SU)
@ -11,9 +12,15 @@ from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.report import Report
from trytond.pyson import Eval, Bool, If, And
from trytond.modules.cashbook.line import sel_bookingtype
from trytond.modules.currency.fields import Monetary
from trytond.modules.cashbook.book import sel_state_book
from trytond.modules.cashbook.line import sel_bookingtype as sel_bookingtype_cb
sel_bookingtype = [
x for x in sel_bookingtype_cb if x[0]
in ['in', 'out', 'mvin', 'mvout']]
DEF_NONE = None
SEL_FREQU = [
('year', 'Yearly'),
@ -97,6 +104,48 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
bookingtype = fields.Selection(
string='Type', selection=sel_bookingtype, required=True,
help='Type of Booking')
currency_cashbook = fields.Function(fields.Many2One(
string='Currency', help='Currency of Cashbook',
model_name='currency.currency'), 'on_change_with_currency_cashbook')
amount = Monetary(
string='Amount', currency='currency_cashbook',
digits='currency_cashbook', required=True)
category = fields.Many2One(
string='Category', model_name='cashbook.category',
help='Category for the planned booking', depends=['bookingtype'],
states={
'required': Eval('bookingtype', '').in_(['in', 'out']),
'invisible': ~Eval('bookingtype', '').in_(['in', 'out'])})
party = fields.Many2One(
string='Party', model_name='party.party', depends=['bookingtype'],
states={
'required': Eval('bookingtype', '').in_(['in', 'out']),
'invisible': ~Eval('bookingtype', '').in_(['in', 'out'])})
booktransf = fields.Many2One(
string='Source/Dest',
ondelete='RESTRICT', model_name='cashbook.book',
domain=[
('owner.id', '=', Eval('owner_cashbook', -1)),
('id', '!=', Eval('cashbook', -1)),
('btype', '!=', None)],
states={
'readonly': Eval('state_cashbook', '') != 'open',
'invisible': ~Eval('bookingtype', '').in_(['mvin', 'mvout']),
'required': Eval('bookingtype', '').in_(['mvin', 'mvout'])},
depends=[
'state_cashbook', 'bookingtype', 'owner_cashbook', 'cashbook'])
owner_cashbook = fields.Function(fields.Many2One(
string='Owner', readonly=True,
states={'invisible': True}, model_name='res.user'),
'on_change_with_owner_cashbook')
state_cashbook = fields.Function(fields.Selection(
string='State of Cashbook',
readonly=True, states={'invisible': True}, selection=sel_state_book),
'on_change_with_state_cashbook')
subject = fields.Text(string='Booking text', required=True)
wfcheck = fields.Boolean(
string="Set to 'Checked'",
help="Switches the booking to the 'Verified' state.")
@classmethod
def __setup__(cls):
@ -177,6 +226,19 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
break
return result
@fields.depends('cashbook', '_parent_cashbook.currency')
def on_change_with_currency_cashbook(self, name=None):
""" get currency of selected cashbook
Args:
name (str, optional): name of field. Defaults to None.
Returns:
int: id of cashbook currency
"""
if self.cashbook:
return self.cashbook.currency.id
@fields.depends('nextrun')
def on_change_with_nextrun_link(self, name=None):
""" get nextrun-record if exist
@ -229,6 +291,30 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
'setpos': self.setpos}
)])
@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
@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
@fields.depends('bookingtype', 'category', 'booktransf')
def on_change_bookingtype(self):
""" reset category/booktransf on change of bookingtype
"""
if self.bookingtype:
if self.bookingtype in ['in', 'out']:
self.booktransf = None
elif self.bookingtype in ['mvin', 'mvout']:
self.category = None
@fields.depends('frequ', 'setpos', 'weekday', 'monthday')
def on_change_frequ(self):
""" update fields
@ -254,6 +340,24 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
"""
self.on_change_frequ()
@classmethod
def default_wfcheck(cls):
""" False as default for wf-state 'checked'
Returns:
bool: False
"""
return False
@classmethod
def default_amount(cls):
""" default for amount
Returns:
Decimal: 0.00
"""
return Decimal('0.0')
@classmethod
def default_interval(cls):
""" get default for interval
@ -387,8 +491,40 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
def run_booking(cls, records):
""" do prepared booking
"""
pool = Pool()
IrDate = pool.get('ir.date')
Line = pool.get('cashbook.line')
to_create = []
to_create_check = []
for record in records:
print('-- booking:', record.rec_name)
line = {
'cashbook': record.cashbook.id,
'bookingtype': record.bookingtype,
'date': IrDate.today(),
'amount': record.amount,
'description': record.subject}
if record.bookingtype in ['in', 'out']:
if record.category:
line['category'] = record.category.id
if record.party:
line['party'] = record.party.id
elif record.bookingtype in ['mvin', 'mvout']:
if record.booktransf:
line['booktransf'] = record.booktransf.id
if record.wfcheck:
to_create_check.append(line)
else:
to_create.append(line)
if to_create_check:
lines = Line.create(to_create_check)
Line.wfcheck(lines)
if to_create:
Line.create(to_create)
@classmethod
def cronjob(cls):