line: gain/loss field ...

This commit is contained in:
Frederik Jaeckel 2023-02-16 23:12:48 +01:00
parent e0a2a0f9cb
commit c82f37729b
2 changed files with 192 additions and 24 deletions

View file

@ -197,7 +197,6 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
Decimal('0.0'),
).as_('gainloss'),
)
print('\n## query:', query, query.params)
return (tab_line, query)
@classmethod
@ -379,12 +378,10 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
query.where = tab_line.id.in_([x.id for x in lines])
cursor.execute(*query)
records = cursor.fetchall()
print('\n## asset_gainloss:', query, query.params)
for record in records:
line = Line2(record[0])
result['asset_gainloss'][record[0]] = quantize_val(record[1], line)
return result
def get_rec_name(self, name):

View file

@ -56,6 +56,7 @@ class YieldTestCase(ModuleTestCase):
'company': company.id,
'currency': company.currency.id,
'number_sequ': self.prep_sequence().id,
'start_date': date(2022, 5, 1),
}])
as_cfg = None
@ -67,14 +68,14 @@ class YieldTestCase(ModuleTestCase):
'dividend_category': dividend_cat.id,
'gainloss_book': gainloss_book.id,
}])
self.assertEqual(as_cfg.fee_category.rec_name, 'Fee')
self.assertEqual(as_cfg.fee_category.rec_name, fee)
self.assertEqual(as_cfg.fee_category.cattype, 'out')
self.assertEqual(as_cfg.dividend_category.rec_name, 'Dividend')
self.assertEqual(as_cfg.dividend_category.rec_name, dividend)
self.assertEqual(as_cfg.dividend_category.cattype, 'in')
self.assertEqual(as_cfg.gainloss_book.rec_name,
'GainLoss | 0.00 usd | Open')
'%s | 0.00 usd | Open' % gainloss)
return as_cfg
@with_transaction()
@ -102,7 +103,7 @@ class YieldTestCase(ModuleTestCase):
Line = pool.get('cashbook.line')
company = self.prep_company()
as_cfg = self.prep_yield_config('Fee', 'Dividend', 'GainLoss', 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')
@ -292,10 +293,39 @@ class YieldTestCase(ModuleTestCase):
self.assertEqual(lines[0].rec_name,
'05/01/2022|Rev/Sp|7.00 usd|Dividend [-]|0.0000 u')
# add counter-account for profit or loss of
# depot-account
book_gainloss, = Cashbook.create([{
'name': 'Profit-Loss',
@with_transaction()
def test_yield_gainloss_spout(self):
""" check out-booking, split with fee and progit
"""
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,
@ -303,6 +333,28 @@ class YieldTestCase(ModuleTestCase):
'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%)
@ -356,18 +408,7 @@ class YieldTestCase(ModuleTestCase):
self.assertEqual(lines[0].references[1].rec_name,
'05/02/2022|from|-9.40 usd|profit of sale [Depot | 0.00 usd | Open | 0.0000 u]')
# asset --> cash
self.assertEqual(lines[0].splitlines[0].rec_name,
'Exp/Sp|30.40 usd|sale with 40% profit [Cash | 22.90 usd | Open]|3.0000 u')
# asset --> category 'fee'
self.assertEqual(lines[0].splitlines[1].rec_name,
'Exp/Sp|2.50 usd|trade fee [Fee]|0.0000 u')
# asset --> profit/loss
self.assertEqual(lines[0].splitlines[2].rec_name,
'Exp/Sp|-9.40 usd|profit of sale [Profit-Loss | -9.40 usd | Open]|0.0000 u')
print('\n# line:', lines[0].asset_gainloss, lines[0].asset_dividend, lines[0].trade_fee)
self.assertEqual(lines[0].asset_gainloss, Decimal('0.0'))
self.assertEqual(lines[0].asset_gainloss, Decimal('-9.4'))
self.assertEqual(lines[0].asset_dividend, Decimal('0.0'))
self.assertEqual(lines[0].trade_fee, Decimal('-2.5'))
@ -379,6 +420,136 @@ class YieldTestCase(ModuleTestCase):
lines = Line.search([('asset_gainloss', '=', Decimal('-9.4'))])
self.assertEqual(len(lines), 1)
self.assertEqual(lines[0].rec_name,
'Exp/Sp|-9.40 usd|profit of sale [Profit-Loss | -9.40 usd | Open]|0.0000 u')
'05/02/2022|Exp/Sp|-23.50 usd|all out [-]|-3.0000 u')
@with_transaction()
def test_yield_gainloss_mvout(self):
""" check out-booking, transfer with profit-account, fee
2x transfer, 1x category
"""
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_asset.id,
'date': date(2022, 5, 2),
'bookingtype': 'mvout',
'booktransf': book_cash.id,
'description': 'sale with 40% profit',
'quantity': Decimal('3.0'),
'amount': Decimal('30.4'),
}, {
'cashbook': book_asset.id,
'date': date(2022, 5, 2),
'bookingtype': 'out',
'description': 'trade fee',
'category': as_cfg.fee_category.id,
'quantity': Decimal('0.0'),
'amount': Decimal('2.5'),
}, {
'cashbook': book_asset.id,
'date': date(2022, 5, 2),
'bookingtype': 'mvin',
'description': 'profit of sale',
'booktransf': book_gainloss.id,
'quantity': Decimal('0.0'),
'amount': Decimal('9.4'),
}])
self.assertEqual(len(lines), 3)
Line.wfcheck(lines)
self.assertEqual(lines[0].rec_name,
'05/02/2022|to|-30.40 usd|sale with 40% profit [Cash | 30.40 usd | Open]|-3.0000 u')
self.assertEqual(lines[0].asset_gainloss, Decimal('0.0'))
self.assertEqual(lines[0].asset_dividend, Decimal('0.0'))
self.assertEqual(lines[0].trade_fee, Decimal('0.0'))
self.assertEqual(lines[1].rec_name,
'05/02/2022|Exp|-2.50 usd|trade fee [Fee]|0.0000 u')
self.assertEqual(lines[1].asset_gainloss, Decimal('0.0'))
self.assertEqual(lines[1].asset_dividend, Decimal('0.0'))
self.assertEqual(lines[1].trade_fee, Decimal('-2.5'))
self.assertEqual(lines[2].rec_name,
'05/02/2022|from|9.40 usd|profit of sale [Profit-Loss | -9.40 usd | Open]|0.0000 u')
self.assertEqual(lines[2].asset_gainloss, Decimal('-9.4'))
self.assertEqual(lines[2].asset_dividend, Decimal('0.0'))
self.assertEqual(lines[2].trade_fee, Decimal('0.0'))
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')
# check searcher
lines = Line.search([('asset_gainloss', '=', Decimal('-9.4'))])
self.assertEqual(len(lines), 1)
# end YieldTestCase