line: field 'asset_gainloss' ok + test

This commit is contained in:
Frederik Jaeckel 2023-02-18 20:55:04 +01:00
parent 313892d2d5
commit 6b042bca3b
2 changed files with 294 additions and 60 deletions

24
line.py
View file

@ -139,6 +139,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
SplitLine = pool.get('cashbook.split')
tab_line = cls.__table__()
tab_mvsp_counterpart = cls.__table__()
tab_mvsp_local = cls.__table__()
tab_mvmv_counterpart = cls.__table__()
tab_spmv_counterpart = cls.__table__()
tab_mv_spline = SplitLine.__table__()
@ -186,15 +187,19 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
type_ = 'LEFT OUTER',
).select(
tab_line.id,
Coalesce(
(Coalesce(
tab_mvmv_counterpart.credit - tab_mvmv_counterpart.debit,
Case(
(tab_line.bookingtype == 'mvin', tab_mv_spline.amount),
(tab_line.bookingtype == 'mvout', tab_mv_spline.amount * Decimal('-1.0')),
),
Case(
(tab_mvsp_counterpart.cashbook == getattr(cfg1.gainloss_book, 'id', None),
tab_line.debit - tab_line.credit),
),
tab_spmv_counterpart.credit - tab_spmv_counterpart.debit,
Decimal('0.0'),
).as_('gainloss'),
) * Decimal('-1.0')).as_('gainloss'),
)
return (tab_line, query)
@ -640,13 +645,14 @@ class Line(SecondUomMixin, metaclass=PoolMeta):
))
# 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,
))
if (line.amount != Decimal('0.0')) and (line.quantity != Decimal('0.0')):
(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 update_values_by_splitlines(cls, lines):

View file

@ -295,7 +295,7 @@ class YieldTestCase(ModuleTestCase):
@with_transaction()
def test_yield_gainloss_spout(self):
""" check out-booking, split with fee and progit
""" check out-booking, split with fee and profit (1)
"""
pool = Pool()
Cashbook = pool.get('cashbook.book')
@ -319,11 +319,6 @@ class YieldTestCase(ModuleTestCase):
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,
@ -348,7 +343,7 @@ class YieldTestCase(ModuleTestCase):
'amount': Decimal('23.50'),
'quantity': Decimal('3.0'),
'category': as_cfg.dividend_category.id,
'description': 'Initial',
'description': 'Initial (1)',
}, ])],
}])
@ -371,23 +366,23 @@ class YieldTestCase(ModuleTestCase):
'cashbook': book_asset.id,
'date': date(2022, 5, 2),
'bookingtype': 'spout',
'description': 'all out',
'description': 'all out (1)',
'splitlines': [('create', [{
'splittype': 'tr',
'booktransf': book_cash.id,
'description': 'sale with 40% profit',
'description': 'sale with 40% profit (1)',
'quantity': Decimal('3.0'),
'amount': Decimal('30.4'),
}, {
'splittype': 'cat',
'description': 'trade fee',
'description': 'trade fee (1)',
'category': as_cfg.fee_category.id,
'amount': Decimal('2.5'),
'quantity': Decimal('0.0'),
}, {
'splittype': 'tr',
'booktransf': book_gainloss.id,
'description': 'profit of sale',
'description': 'profit of sale (1)',
'amount': Decimal('-9.4'),
'quantity': Decimal('0.0'),
}])],
@ -395,20 +390,20 @@ class YieldTestCase(ModuleTestCase):
self.assertEqual(len(lines), 1)
self.assertEqual(lines[0].rec_name,
'05/02/2022|Exp/Sp|-23.50 usd|all out [-]|-3.0000 u')
'05/02/2022|Exp/Sp|-23.50 usd|all out (1) [-]|-3.0000 u')
Line.wfcheck(lines)
self.assertEqual(lines[0].rec_name,
'05/02/2022|Exp/Sp|-23.50 usd|all out [-]|-3.0000 u')
'05/02/2022|Exp/Sp|-23.50 usd|all out (1) [-]|-3.0000 u')
self.assertEqual(len(lines[0].splitlines), 3)
self.assertEqual(lines[0].reference, None)
self.assertEqual(len(lines[0].references), 2)
self.assertEqual(lines[0].references[0].rec_name,
'05/02/2022|from|30.40 usd|sale with 40% profit [Depot | 0.00 usd | Open | 0.0000 u]')
'05/02/2022|from|30.40 usd|sale with 40% profit (1) [Depot | 0.00 usd | Open | 0.0000 u]')
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]')
'05/02/2022|from|-9.40 usd|profit of sale (1) [Depot | 0.00 usd | Open | 0.0000 u]')
self.assertEqual(lines[0].asset_gainloss, Decimal('-9.4'))
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'))
@ -417,15 +412,14 @@ class YieldTestCase(ModuleTestCase):
self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | -9.40 usd | Open')
# check searcher
lines = Line.search([('asset_gainloss', '=', Decimal('-9.4'))])
lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))])
self.assertEqual(len(lines), 1)
self.assertEqual(lines[0].rec_name,
'05/02/2022|Exp/Sp|-23.50 usd|all out [-]|-3.0000 u')
'05/02/2022|Exp/Sp|-23.50 usd|all out (1) [-]|-3.0000 u')
@with_transaction()
def test_yield_gainloss_mvout(self):
""" check out-booking, transfer with profit-account, fee
2x transfer, 1x category, 3x transfers
def test_yield_gainloss_spin(self):
""" check in-booking, split with profit (2)
"""
pool = Pool()
Cashbook = pool.get('cashbook.book')
@ -449,11 +443,6 @@ class YieldTestCase(ModuleTestCase):
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,
@ -478,7 +467,250 @@ class YieldTestCase(ModuleTestCase):
'amount': Decimal('23.50'),
'quantity': Decimal('3.0'),
'category': as_cfg.dividend_category.id,
'description': 'Initial',
'description': 'Initial (2)',
}, ])],
}])
# add counter-account for profit or loss of
# depot-account
book_gainloss = as_cfg.gainloss_book
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': 'spin',
'description': 'all out (2)',
'splitlines': [('create', [{
'splittype': 'tr',
'booktransf': book_asset.id,
'description': 'sale with 40% profit (2)',
'quantity': Decimal('3.0'),
'amount': Decimal('30.4'),
}, ])],
}, {
'cashbook': book_gainloss.id,
'date': date(2022, 5, 2),
'bookingtype': 'spin',
'description': 'profit (2)',
'splitlines': [('create', [{
'splittype': 'tr',
'booktransf': book_asset.id,
'description': 'profit of sale (2)',
'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|Rev/Sp|30.40 usd|all out (2) [-]')
self.assertEqual(len(lines[0].splitlines), 1)
self.assertEqual(lines[0].reference, None)
self.assertEqual(len(lines[0].references), 1)
self.assertEqual(lines[0].references[0].rec_name,
'05/02/2022|to|-30.40 usd|sale with 40% profit (2) [Cash | 30.40 usd | Open]|-3.0000 u')
self.assertEqual(lines[0].asset_gainloss, None)
self.assertEqual(lines[0].asset_dividend, None)
self.assertEqual(lines[0].trade_fee, None)
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('0.0'))
self.assertEqual(lines[1].rec_name,
'05/02/2022|Rev/Sp|-9.40 usd|profit (2) [-]')
self.assertEqual(len(lines[1].splitlines), 1)
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|profit of sale (2) [Profit-Loss | -9.40 usd | Open]|0.0000 u')
self.assertEqual(lines[1].asset_gainloss, None)
self.assertEqual(lines[1].asset_dividend, None)
self.assertEqual(lines[1].trade_fee, None)
self.assertEqual(lines[1].references[0].asset_gainloss, Decimal('9.4'))
self.assertEqual(lines[1].references[0].asset_dividend, Decimal('0.0'))
self.assertEqual(lines[1].references[0].trade_fee, Decimal('0.0'))
self.assertEqual(book_asset.rec_name, 'Depot | 2.50 usd | Open | 0.0000 u')
# negative amount on profit/loss-account means success
self.assertEqual(book_gainloss.rec_name, 'Profit-Loss | -9.40 usd | Open')
# check searcher
lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))])
self.assertEqual(len(lines), 1)
self.assertEqual(lines[0].rec_name,
'05/02/2022|to|9.40 usd|profit of sale (2) [Profit-Loss | -9.40 usd | Open]|0.0000 u')
@with_transaction()
def test_yield_gainloss_spin2(self):
""" check in-booking, split with profit (5)
"""
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')
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 (2)',
}, ])],
}])
# add counter-account for profit or loss of
# depot-account
book_gainloss = as_cfg.gainloss_book
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',
'description': 'guv (5)',
'splitlines': [('create', [{
'splittype': 'tr',
'booktransf': book_asset.id,
'description': 'profit/loss (5)',
'quantity': Decimal('-3.0'),
'amount': Decimal('-23.5'),
}, {
'splittype': 'tr',
'booktransf': book_gainloss.id,
'description': 'profit/loss (5)',
'amount': Decimal('-9.4'),
}, ])],
}])
self.assertEqual(len(lines), 1)
Line.wfcheck(lines)
self.assertEqual(lines[0].rec_name,
'05/02/2022|Exp/Sp|32.90 usd|guv (5) [-]')
self.assertEqual(len(lines[0].splitlines), 2)
self.assertEqual(lines[0].splitlines[0].rec_name,
'Exp/Sp|-23.50 usd|profit/loss (5) [Depot | 0.00 usd | Open | 0.0000 u]')
self.assertEqual(lines[0].splitlines[1].rec_name,
'Exp/Sp|-9.40 usd|profit/loss (5) [Profit-Loss | -9.40 usd | Open]')
self.assertEqual(lines[0].reference, None)
self.assertEqual(len(lines[0].references), 2)
self.assertEqual(lines[0].references[0].rec_name,
'05/02/2022|from|-23.50 usd|profit/loss (5) [Cash | 32.90 usd | Open]|-3.0000 u')
self.assertEqual(lines[0].references[1].rec_name,
'05/02/2022|from|-9.40 usd|profit/loss (5) [Cash | 32.90 usd | Open]')
self.assertEqual(lines[0].asset_gainloss, None)
self.assertEqual(lines[0].asset_dividend, None)
self.assertEqual(lines[0].trade_fee, None)
self.assertEqual(lines[0].references[0].asset_gainloss, Decimal('9.4'))
self.assertEqual(lines[0].references[0].asset_dividend, Decimal('0.0'))
self.assertEqual(lines[0].references[0].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')
# check searcher
lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))])
self.assertEqual(len(lines), 1)
self.assertEqual(lines[0].rec_name,
'05/02/2022|from|-23.50 usd|profit/loss (5) [Cash | 32.90 usd | Open]|-3.0000 u')
@with_transaction()
def test_yield_gainloss_mvout(self):
""" check out-booking, transfer with profit-account, fee
2x transfer, 1x category, 3x transfers (3)
"""
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')
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 (3)',
}, ])],
}])
@ -502,14 +734,14 @@ class YieldTestCase(ModuleTestCase):
'date': date(2022, 5, 2),
'bookingtype': 'mvout',
'booktransf': book_cash.id,
'description': 'sale with 40% profit',
'description': 'sale with 40% profit (3)',
'quantity': Decimal('3.0'),
'amount': Decimal('30.4'),
}, {
'cashbook': book_asset.id,
'date': date(2022, 5, 2),
'bookingtype': 'out',
'description': 'trade fee',
'description': 'trade fee (3)',
'category': as_cfg.fee_category.id,
'quantity': Decimal('0.0'),
'amount': Decimal('2.5'),
@ -517,7 +749,7 @@ class YieldTestCase(ModuleTestCase):
'cashbook': book_asset.id,
'date': date(2022, 5, 2),
'bookingtype': 'mvin',
'description': 'profit of sale',
'description': 'profit of sale (3)',
'booktransf': book_gainloss.id,
'quantity': Decimal('0.0'),
'amount': Decimal('9.4'),
@ -527,20 +759,20 @@ class YieldTestCase(ModuleTestCase):
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')
'05/02/2022|to|-30.40 usd|sale with 40% profit (3) [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')
'05/02/2022|Exp|-2.50 usd|trade fee (3) [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'))
'05/02/2022|from|9.40 usd|profit of sale (3) [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'))
@ -549,13 +781,13 @@ class YieldTestCase(ModuleTestCase):
self.assertEqual(book_cash.rec_name, 'Cash | 30.40 usd | Open')
# check searcher
lines = Line.search([('asset_gainloss', '=', Decimal('-9.4'))])
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
2x transfer, 1x category, 1x transfer to splitbooking (4)
"""
pool = Pool()
Cashbook = pool.get('cashbook.book')
@ -579,11 +811,6 @@ class YieldTestCase(ModuleTestCase):
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,
@ -608,7 +835,7 @@ class YieldTestCase(ModuleTestCase):
'amount': Decimal('23.50'),
'quantity': Decimal('3.0'),
'category': as_cfg.dividend_category.id,
'description': 'Initial',
'description': 'Initial (4)',
}, ])],
}])
@ -631,17 +858,17 @@ class YieldTestCase(ModuleTestCase):
'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
'description': 'all out (4)', # category 'as_cfg.fee_category' is for
'splitlines': [('create', [{ # out-only
'splittype': 'tr',
'booktransf': book_asset.id,
'description': 'sale with 40% profit',
'description': 'sale with 40% profit (4)',
'quantity': Decimal('-3.0'),
'amount': Decimal('-32.9'), # profit + fee
}, {
'splittype': 'cat',
'category': as_cfg.fee_category.id,
'description': 'trade fee',
'description': 'trade fee (4)',
'amount': Decimal('2.5'), # fee
}])],
}, {
@ -651,40 +878,41 @@ class YieldTestCase(ModuleTestCase):
'booktransf': book_gainloss.id,
'amount': Decimal('9.4'),
'quantity': Decimal('0.0'),
'description': 'gainloss_mv_sp (4)',
}])
self.assertEqual(len(lines), 2)
Line.wfcheck(lines)
self.assertEqual(lines[0].rec_name,
'05/02/2022|Exp/Sp|30.40 usd|all out [-]')
'05/02/2022|Exp/Sp|30.40 usd|all out (4) [-]')
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')
'05/02/2022|from|-32.90 usd|sale with 40% profit (4) [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'))
'05/02/2022|from|9.40 usd|gainloss_mv_sp (4) [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]')
'05/02/2022|to|-9.40 usd|gainloss_mv_sp (4) [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'))])
lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))])
self.assertEqual(len(lines), 1)
# end YieldTestCase