line: quantity_balance, validate,

reconciliation: start/end-quantity
todo: tests
This commit is contained in:
Frederik Jaeckel 2022-12-31 16:15:23 +01:00
parent e94a869166
commit f86db6dea3
11 changed files with 484 additions and 140 deletions

70
line.py
View file

@ -7,6 +7,8 @@ from decimal import Decimal
from trytond.model import fields
from trytond.pool import PoolMeta
from trytond.pyson import Eval, Or, If
from trytond.exceptions import UserError
from trytond.i18n import gettext
from trytond.modules.cashbook.line import STATES, DEPENDS
STATESQ = {
@ -34,11 +36,17 @@ class Line(metaclass=PoolMeta):
digits=(16, Eval('quantity_digits', 4)),
states=STATESQ, depends=DEPENDSQ)
quantity_credit = fields.Numeric(string='Quantity Credit',
digits=(16, Eval('quantity_digits', 4)),
states=STATESQ, depends=DEPENDSQ)
digits=(16, Eval('quantity_digits', 4)), readonly=True,
states={
'invisible': STATESQ['invisible'],
'required': STATESQ['required'],
}, depends=DEPENDSQ)
quantity_debit = fields.Numeric(string='Quantity Debit',
digits=(16, Eval('quantity_digits', 4)),
states=STATESQ, depends=DEPENDSQ)
digits=(16, Eval('quantity_digits', 4)), readonly=True,
states={
'invisible': STATESQ['invisible'],
'required': STATESQ['required'],
}, depends=DEPENDSQ)
quantity_digits = fields.Function(fields.Integer(string='Digits',
readonly=True, states={'invisible': True}),
@ -55,6 +63,11 @@ class Line(metaclass=PoolMeta):
'invisible': Eval('feature', '') != 'asset',
}, depends=['currency_digits', 'quantity_digits', 'feature']),
'on_change_with_asset_rate')
quantity_balance = fields.Function(fields.Numeric(string='Quantity',
digits=(16, Eval('quantity_digits', 4)),
help='Number of shares in the cashbook up to the current row if the default sort applies.',
readonly=True, depends=['quantity_digits']),
'on_change_with_quantity_balance')
@classmethod
def get_debit_credit(cls, values):
@ -123,4 +136,53 @@ class Line(metaclass=PoolMeta):
return self.cashbook.quantity_digits
return 4
@classmethod
def validate(cls, lines):
""" deny pos/neg mismatch
"""
super(Line, cls).validate(lines)
for line in lines:
# ignore non-asset-lines
if line.cashbook.feature != 'asset':
continue
# quantity must be set
if (line.quantity is None) or \
(line.quantity_credit is None) or \
(line.quantity_debit is None):
raise UserError(gettext(
'cashbook_investment.msg_line_quantity_not_set',
linetxt = line.rec_name,
))
# quantity and amount must with same sign
(amount_sign, a_dig, a_exp) = line.amount.as_tuple()
(quantity_sign, q_dig, q_exp) = line.quantity.as_tuple()
if amount_sign != quantity_sign:
raise UserError(gettext(
'cashbook_investment.msg_line_sign_mismatch',
linetxt = line.rec_name,
))
@classmethod
def write(cls, *args):
""" add or update quanity_debit/credit
"""
actions = iter(args)
to_write = []
for lines, values in zip(actions, actions):
# update debit / credit
if len(set(values.keys()).intersection(set({'quantity', 'bookingtype'}))) > 0:
for line in lines:
values2 = {}
values2.update(values)
values2.update(cls.get_debit_credit({
x:values.get(x, getattr(line, x)) for x in ['quantity', 'bookingtype']
}))
to_write.extend([lines, values2])
else :
to_write.extend([lines, values])
super(Line, cls).write(*to_write)
# end LineContext