line: calculate quantity on changes in splitlines + tests

todo: tests
This commit is contained in:
Frederik Jaeckel 2023-01-17 22:17:41 +01:00
parent bf84b092fc
commit 7ff631d850
4 changed files with 185 additions and 2 deletions

37
line.py
View file

@ -30,6 +30,14 @@ STATESQ1 = {
DEPENDSQ1 = ['feature', 'booktransf_feature', 'quantity_digits', 'bookingtype'] DEPENDSQ1 = ['feature', 'booktransf_feature', 'quantity_digits', 'bookingtype']
DEPENDSQ1.extend(DEPENDS) DEPENDSQ1.extend(DEPENDS)
STATESQ1B = {}
STATESQ1B.update(STATESQ1)
STATESQ1B['invisible'] = And(
Eval('feature', '') != 'asset',
Eval('booktransf_feature', '') != 'asset',
Eval('splitline_has_quantity', False) == False,
)
STATESQ2 = { STATESQ2 = {
'invisible': Eval('feature', '') != 'asset', 'invisible': Eval('feature', '') != 'asset',
@ -43,7 +51,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
quantity = fields.Numeric(string='Quantity', quantity = fields.Numeric(string='Quantity',
digits=(16, Eval('quantity_digits', 4)), digits=(16, Eval('quantity_digits', 4)),
states=STATESQ1, depends=DEPENDSQ1) states=STATESQ1B, depends=DEPENDSQ1+['splitline_has_quantity'])
quantity_credit = fields.Numeric(string='Quantity Credit', quantity_credit = fields.Numeric(string='Quantity Credit',
digits=(16, Eval('quantity_digits', 4)), readonly=True, digits=(16, Eval('quantity_digits', 4)), readonly=True,
states=STATESQ2, depends=DEPENDSQ2) states=STATESQ2, depends=DEPENDSQ2)
@ -73,6 +81,9 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
'invisible': Eval('feature', '') != 'asset', 'invisible': Eval('feature', '') != 'asset',
}, depends=['quantity_digits', 'feature']), }, depends=['quantity_digits', 'feature']),
'on_change_with_quantity_balance') 'on_change_with_quantity_balance')
splitline_has_quantity = fields.Function(fields.Boolean(
string='has quantity', readonly=True, states={'invisible': True}),
'on_change_with_splitline_has_quantity')
def get_rec_name(self, name): def get_rec_name(self, name):
""" add quantities - if its a asset-cashbook """ add quantities - if its a asset-cashbook
@ -165,6 +176,30 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
}) })
return result return result
@fields.depends('amount', 'splitlines', 'quantity')
def on_change_splitlines(self):
""" update amount if splitlines change
"""
super(Line, self).on_change_splitlines()
quantity = sum([x.quantity for x in self.splitlines if x.quantity is not None])
cnt1 = sum([1 for x in self.splitlines if x.quantity is not None])
if cnt1 > 0:
self.quantity = quantity
@fields.depends('splitlines')
def on_change_with_splitline_has_quantity(self, name=None):
""" get True if splitlines are linked to asset-cashbooks
"""
result = False
for line in self.splitlines:
if line.splittype != 'tr':
continue
if line.booktransf:
if line.booktransf.feature == 'asset':
result = True
break
return result
@fields.depends('id', 'date', 'cashbook', 'feature',\ @fields.depends('id', 'date', 'cashbook', 'feature',\
'_parent_cashbook.id', 'reconciliation', \ '_parent_cashbook.id', 'reconciliation', \
'_parent_reconciliation.start_quantity',\ '_parent_reconciliation.start_quantity',\

View file

@ -214,6 +214,10 @@ msgctxt "help:cashbook.line,factor_2nd_uom:"
msgid "Conversion factor between the units of the participating cash books." msgid "Conversion factor between the units of the participating cash books."
msgstr "Umrechnungsfaktor zwischen den Einheiten der teilnehmenden Kassenbücher." msgstr "Umrechnungsfaktor zwischen den Einheiten der teilnehmenden Kassenbücher."
msgctxt "field:cashbook.line,splitline_has_quantity:"
msgid "has quantity"
msgstr "hat Anzahl"
################## ##################
# cashbook.recon # # cashbook.recon #

View file

@ -198,6 +198,10 @@ msgctxt "help:cashbook.line,factor_2nd_uom:"
msgid "Conversion factor between the units of the participating cash books." msgid "Conversion factor between the units of the participating cash books."
msgstr "Conversion factor between the units of the participating cash books." msgstr "Conversion factor between the units of the participating cash books."
msgctxt "field:cashbook.line,splitline_has_quantity:"
msgid "has quantity"
msgstr "has quantity"
msgctxt "field:cashbook.recon,start_quantity:" msgctxt "field:cashbook.recon,start_quantity:"
msgid "Start Quantity" msgid "Start Quantity"
msgstr "Start Quantity" msgstr "Start Quantity"

View file

@ -741,6 +741,7 @@ class CbInvTestCase(CashbookTestCase, InvestmentTestCase):
self.assertEqual(book.lines[0].quantity_debit, None) self.assertEqual(book.lines[0].quantity_debit, None)
self.assertEqual(book.lines[0].feature, 'gen') self.assertEqual(book.lines[0].feature, 'gen')
self.assertEqual(book.lines[0].booktransf_feature, 'asset') self.assertEqual(book.lines[0].booktransf_feature, 'asset')
self.assertEqual(book.lines[0].splitline_has_quantity, False)
self.assertEqual(len(book2.lines), 0) self.assertEqual(len(book2.lines), 0)
self.assertEqual(book.lines[0].rec_name, self.assertEqual(book.lines[0].rec_name,
'05/01/2022|from|1.00 usd|Transfer In [Asset-Book | 0.00 usd | Open | 0.0000 u]') '05/01/2022|from|1.00 usd|Transfer In [Asset-Book | 0.00 usd | Open | 0.0000 u]')
@ -1241,7 +1242,7 @@ class CbInvTestCase(CashbookTestCase, InvestmentTestCase):
}, { }, {
'amount': Decimal('6.0'), 'amount': Decimal('6.0'),
'splittype': 'cat', 'splittype': 'cat',
'description': 'from cashbook', 'description': 'from category',
'category': category.id, 'category': category.id,
'quantity': Decimal('2.5'), 'quantity': Decimal('2.5'),
}])], }])],
@ -1255,6 +1256,145 @@ class CbInvTestCase(CashbookTestCase, InvestmentTestCase):
self.assertEqual(book.lines[0].amount, Decimal('11.0')) self.assertEqual(book.lines[0].amount, Decimal('11.0'))
self.assertEqual(book.lines[0].quantity, Decimal('4.0')) self.assertEqual(book.lines[0].quantity, Decimal('4.0'))
self.assertEqual(book.lines[0].quantity_uom.symbol, 'u') self.assertEqual(book.lines[0].quantity_uom.symbol, 'u')
self.assertEqual(book.lines[0].splitline_has_quantity, False)
self.assertEqual(book.lines[0].rec_name, '05/01/2022|Rev/Sp|11.00 usd|- [-]|4.00 u') self.assertEqual(book.lines[0].rec_name, '05/01/2022|Rev/Sp|11.00 usd|- [-]|4.00 u')
self.assertEqual(len(book.lines[0].splitlines), 2)
self.assertEqual(book.lines[0].splitlines[0].splittype, 'cat')
self.assertEqual(book.lines[0].splitlines[0].amount, Decimal('5.0'))
self.assertEqual(book.lines[0].splitlines[0].quantity, Decimal('1.5'))
self.assertEqual(book.lines[0].splitlines[1].splittype, 'cat')
self.assertEqual(book.lines[0].splitlines[1].amount, Decimal('6.0'))
self.assertEqual(book.lines[0].splitlines[1].quantity, Decimal('2.5'))
@with_transaction()
def test_assetbook_split_in_category_and_assetbook(self):
""" splitbooking incoming to asset-cahbook,
from category and asset-cashbook
"""
pool = Pool()
Book = pool.get('cashbook.book')
Line = pool.get('cashbook.line')
BType = pool.get('cashbook.type')
Asset = pool.get('investment.asset')
ProdTempl = pool.get('product.template')
Uom = pool.get('product.uom')
types = self.prep_type()
BType.write(*[
[types],
{
'feature': 'asset',
}])
category = self.prep_category(cattype='in')
company = self.prep_company()
party = self.prep_party()
asset = self.prep_asset_item(
company=company,
product = self.prep_asset_product(name='Product 1'))
self.assertEqual(asset.symbol, 'usd/u')
books = Book.create([{
'start_date': date(2022, 4, 1),
'name': 'Book 1',
'btype': types.id,
'company': company.id,
'currency': company.currency.id,
'number_sequ': self.prep_sequence().id,
'asset': asset.id,
'quantity_uom': asset.uom.id,
'quantity_digits': 2,
}, {
'start_date': date(2022, 4, 1),
'name': 'Book 2',
'btype': types.id,
'company': company.id,
'currency': company.currency.id,
'number_sequ': self.prep_sequence().id,
'asset': asset.id,
'quantity_uom': asset.uom.id,
'quantity_digits': 2,
}])
Book.write(*[
[books[0]],
{
'lines': [('create', [{
'bookingtype': 'spin',
'date': date(2022, 5, 1),
'splitlines': [('create', [{
'amount': Decimal('5.0'),
'splittype': 'cat',
'description': 'from category',
'category': category.id,
'quantity': Decimal('1.5'),
}, {
'amount': Decimal('6.0'),
'splittype': 'tr',
'description': 'from cashbook',
'booktransf': books[1].id,
'quantity': Decimal('2.5'),
}])],
}])],
}])
self.assertEqual(books[0].rec_name, 'Book 1 | 11.00 usd | Open | 4.00 u')
self.assertEqual(books[0].balance_all, Decimal('11.0'))
self.assertEqual(len(books[0].lines), 1)
self.assertEqual(books[0].lines[0].amount, Decimal('11.0'))
self.assertEqual(books[0].lines[0].quantity, Decimal('4.0'))
self.assertEqual(books[0].lines[0].quantity_uom.symbol, 'u')
self.assertEqual(books[0].lines[0].splitline_has_quantity, True)
self.assertEqual(books[0].lines[0].rec_name, '05/01/2022|Rev/Sp|11.00 usd|- [-]|4.00 u')
self.assertEqual(len(books[0].lines[0].splitlines), 2)
self.assertEqual(books[0].lines[0].splitlines[0].splittype, 'cat')
self.assertEqual(books[0].lines[0].splitlines[0].amount, Decimal('5.0'))
self.assertEqual(books[0].lines[0].splitlines[0].quantity, Decimal('1.5'))
self.assertEqual(books[0].lines[0].splitlines[0].category.rec_name, 'Cat1')
self.assertEqual(books[0].lines[0].splitlines[0].booktransf, None)
self.assertEqual(books[0].lines[0].splitlines[1].splittype, 'tr')
self.assertEqual(books[0].lines[0].splitlines[1].amount, Decimal('6.0'))
self.assertEqual(books[0].lines[0].splitlines[1].quantity, Decimal('2.5'))
self.assertEqual(books[0].lines[0].splitlines[1].booktransf.rec_name,
'Book 2 | 0.00 usd | Open | 0.00 u')
self.assertEqual(len(books[0].lines[0].references), 0)
self.assertEqual(books[0].lines[0].reference, None)
self.assertEqual(books[1].rec_name, 'Book 2 | 0.00 usd | Open | 0.00 u')
self.assertEqual(books[1].balance_all, Decimal('0.0'))
self.assertEqual(len(books[1].lines), 0)
Line.wfcheck([books[0].lines[0]])
self.assertEqual(books[0].rec_name, 'Book 1 | 11.00 usd | Open | 4.00 u')
self.assertEqual(books[0].balance_all, Decimal('11.0'))
self.assertEqual(len(books[0].lines), 1)
self.assertEqual(books[0].lines[0].amount, Decimal('11.0'))
self.assertEqual(books[0].lines[0].quantity, Decimal('4.0'))
self.assertEqual(books[0].lines[0].quantity_uom.symbol, 'u')
self.assertEqual(books[0].lines[0].splitline_has_quantity, True)
self.assertEqual(books[0].lines[0].rec_name, '05/01/2022|Rev/Sp|11.00 usd|- [-]|4.00 u')
self.assertEqual(len(books[0].lines[0].splitlines), 2)
self.assertEqual(books[0].lines[0].splitlines[0].splittype, 'cat')
self.assertEqual(books[0].lines[0].splitlines[0].amount, Decimal('5.0'))
self.assertEqual(books[0].lines[0].splitlines[0].quantity, Decimal('1.5'))
self.assertEqual(books[0].lines[0].splitlines[0].category.rec_name, 'Cat1')
self.assertEqual(books[0].lines[0].splitlines[0].booktransf, None)
self.assertEqual(books[0].lines[0].splitlines[1].splittype, 'tr')
self.assertEqual(books[0].lines[0].splitlines[1].amount, Decimal('6.0'))
self.assertEqual(books[0].lines[0].splitlines[1].quantity, Decimal('2.5'))
self.assertEqual(books[0].lines[0].splitlines[1].booktransf.rec_name,
'Book 2 | -6.00 usd | Open | -2.50 u')
self.assertEqual(len(books[0].lines[0].references), 0)
self.assertEqual(books[0].lines[0].reference, None)
self.assertEqual(books[1].rec_name, 'Book 2 | 0.00 usd | Open | 0.00 u')
self.assertEqual(books[1].balance_all, Decimal('0.0'))
self.assertEqual(len(books[1].lines), 0)
# end CbInvTestCase # end CbInvTestCase