line: gain/loss ok + test

This commit is contained in:
Frederik Jaeckel 2023-02-17 10:20:06 +01:00
parent c82f37729b
commit 313892d2d5
2 changed files with 138 additions and 4 deletions

View file

@ -182,8 +182,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
((tab_mvmv_counterpart.reference == tab_line.id) | \
(tab_mvmv_counterpart.id == tab_line.reference)) & \
tab_mvmv_counterpart.bookingtype.in_(['mvin', 'mvout']) & \
(tab_mvmv_counterpart.booktransf != None) & \
(tab_mvmv_counterpart.booktransf == getattr(cfg1.gainloss_book, 'id', None)),
(tab_mvmv_counterpart.cashbook == getattr(cfg1.gainloss_book, 'id', None)),
type_ = 'LEFT OUTER',
).select(
tab_line.id,

View file

@ -425,7 +425,7 @@ class YieldTestCase(ModuleTestCase):
@with_transaction()
def test_yield_gainloss_mvout(self):
""" check out-booking, transfer with profit-account, fee
2x transfer, 1x category
2x transfer, 1x category, 3x transfers
"""
pool = Pool()
Cashbook = pool.get('cashbook.book')
@ -546,7 +546,142 @@ class YieldTestCase(ModuleTestCase):
self.assertEqual(book_asset.rec_name, 'Depot | 0.00 usd | Open | 0.0000 u')
self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | -9.40 usd | Open')
self.assertEqual(book_cash.rec_name, 'Profit-Loss | 30.40 usd | Open')
self.assertEqual(book_cash.rec_name, 'Cash | 30.40 usd | Open')
# check searcher
lines = Line.search([('asset_gainloss', '=', Decimal('-9.4'))])
self.assertEqual(len(lines), 1)
@with_transaction()
def test_yield_gainloss_mv_sp(self):
""" check out-booking, transfer with profit-account, fee
2x transfer, 1x category, 1x transfer to splitbooking
"""
pool = Pool()
Cashbook = pool.get('cashbook.book')
BType = pool.get('cashbook.type')
Category = pool.get('cashbook.category')
Line = pool.get('cashbook.line')
company = self.prep_company()
as_cfg = self.prep_yield_config('Fee', 'Dividend', 'Profit-Loss', company)
type_depot = self.prep_type('Depot', 'D')
type_cash = self.prep_type('Cash', 'C')
BType.write(*[
[type_depot],
{
'feature': 'asset',
}])
asset = self.prep_asset_item(
company=company,
product = self.prep_asset_product(name='Product 1'))
self.assertEqual(asset.symbol, 'usd/u')
category_office, = Category.create([{
'name': 'Office',
'company': company.id,
}])
book_cash, = Cashbook.create([{
'name': 'Cash',
'btype': type_cash.id,
'company': company.id,
'currency': company.currency.id,
'number_sequ': self.prep_sequence().id,
'start_date': date(2022, 5, 1),
}])
book_asset, = Cashbook.create([{
'name': 'Depot',
'btype': type_depot.id,
'company': company.id,
'currency': company.currency.id,
'number_sequ': self.prep_sequence().id,
'asset': asset.id,
'quantity_uom': asset.uom.id,
'start_date': date(2022, 5, 1),
'lines': [('create', [{
'bookingtype': 'in',
'date': date(2022, 5, 1),
'amount': Decimal('23.50'),
'quantity': Decimal('3.0'),
'category': as_cfg.dividend_category.id,
'description': 'Initial',
}, ])],
}])
# add counter-account for profit or loss of
# depot-account
book_gainloss = as_cfg.gainloss_book
# sale all shares with profit and fee
# buy: 23.50
# sale: 32.90 (+40%)
# fee: 2.50
# asset (buy amount): 23.50
# booking: asset -> cash: - 30.40
# asset -> (category) fee: - 2.50
# asset <- gain-loss: 9.40
# -------
# 0.00
self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u')
self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | 0.00 usd | Open')
lines = Line.create([{
'cashbook': book_cash.id,
'date': date(2022, 5, 2),
'bookingtype': 'spout', # negate transaction, because the
'description': 'all out', # category 'as_cfg.fee_category' is for
'splitlines': [('create', [{ # out-only
'splittype': 'tr',
'booktransf': book_asset.id,
'description': 'sale with 40% profit',
'quantity': Decimal('-3.0'),
'amount': Decimal('-32.9'), # profit + fee
}, {
'splittype': 'cat',
'category': as_cfg.fee_category.id,
'description': 'trade fee',
'amount': Decimal('2.5'), # fee
}])],
}, {
'cashbook': book_asset.id,
'date': date(2022, 5, 2),
'bookingtype': 'mvin',
'booktransf': book_gainloss.id,
'amount': Decimal('9.4'),
'quantity': Decimal('0.0'),
}])
self.assertEqual(len(lines), 2)
Line.wfcheck(lines)
self.assertEqual(lines[0].rec_name,
'05/02/2022|Exp/Sp|30.40 usd|all out [-]')
self.assertEqual(lines[0].asset_gainloss, None) # non-asset cashbook
self.assertEqual(lines[0].asset_dividend, None)
self.assertEqual(lines[0].trade_fee, None)
self.assertEqual(lines[0].reference, None)
self.assertEqual(len(lines[0].references), 1)
self.assertEqual(lines[0].references[0].rec_name,
'05/02/2022|from|-32.90 usd|sale with 40% profit [Cash | 30.40 usd | Open]|-3.0000 u')
self.assertEqual(lines[0].references[0].asset_gainloss, Decimal('0.0'))
self.assertEqual(lines[0].references[0].asset_dividend, Decimal('0.0'))
self.assertEqual(lines[0].references[0].trade_fee, Decimal('2.5'))
self.assertEqual(lines[1].rec_name,
'05/02/2022|from|9.40 usd|- [Profit-Loss | -9.40 usd | Open]|0.0000 u')
self.assertEqual(lines[1].asset_gainloss, Decimal('-9.4'))
self.assertEqual(lines[1].asset_dividend, Decimal('0.0'))
self.assertEqual(lines[1].trade_fee, Decimal('0.0'))
self.assertEqual(lines[1].reference, None)
self.assertEqual(len(lines[1].references), 1)
self.assertEqual(lines[1].references[0].rec_name,
'05/02/2022|to|-9.40 usd|- [Depot | 0.00 usd | Open | 0.0000 u]')
self.assertEqual(book_asset.rec_name, 'Depot | 0.00 usd | Open | 0.0000 u')
self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | -9.40 usd | Open')
self.assertEqual(book_cash.rec_name, 'Cash | 30.40 usd | Open')
# check searcher
lines = Line.search([('asset_gainloss', '=', Decimal('-9.4'))])