From 6495e0317eac73ad5626b7117b3c1a43382a5e69 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Thu, 8 Jun 2023 14:34:56 +0200 Subject: [PATCH] tests: formatting --- tests/test_module.py | 4 +- tests/yieldtest.py | 2143 ++++++++++++++++++++++-------------------- 2 files changed, 1148 insertions(+), 999 deletions(-) diff --git a/tests/test_module.py b/tests/test_module.py index d8459d7..2b73b65 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -4,8 +4,8 @@ # full copyright notices and license terms. -from trytond.modules.cashbook.tests import CashbookTestCase -from trytond.modules.investment.tests import InvestmentTestCase +from trytond.modules.cashbook.tests.test_module import CashbookTestCase +from trytond.modules.investment.tests.test_module import InvestmentTestCase from .yieldtest import YieldTestCase from .book import CbInvTestCase diff --git a/tests/yieldtest.py b/tests/yieldtest.py index 7a6a7b1..5bf99f6 100644 --- a/tests/yieldtest.py +++ b/tests/yieldtest.py @@ -27,7 +27,7 @@ class YieldTestCase(object): fee_cat = Category.search([('name', '=', fee)]) if len(fee_cat) > 0: fee_cat = fee_cat[0] - else : + else: fee_cat, = Category.create([{ 'name': fee, 'company': company.id, @@ -37,7 +37,7 @@ class YieldTestCase(object): dividend_cat = Category.search([('name', '=', dividend)]) if len(dividend_cat) > 0: dividend_cat = dividend_cat[0] - else : + else: dividend_cat, = Category.create([{ 'name': dividend, 'company': company.id, @@ -47,7 +47,7 @@ class YieldTestCase(object): gainloss_book = Cashbook.search([('name', '=', gainloss)]) if len(gainloss_book) > 0: gainloss_book = gainloss_book[0] - else : + else: types = self.prep_type() gainloss_book, = Cashbook.create([{ 'name': gainloss, @@ -60,8 +60,7 @@ class YieldTestCase(object): as_cfg = None with Transaction().set_context({ - 'company': company.id, - }): + 'company': company.id}): as_cfg, = AssetConf.create([{ 'fee_category': fee_cat.id, 'dividend_category': dividend_cat.id, @@ -73,7 +72,8 @@ class YieldTestCase(object): 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, + self.assertEqual( + as_cfg.gainloss_book.rec_name, '%s | 0.00 usd | Open' % gainloss) return as_cfg @@ -81,14 +81,16 @@ class YieldTestCase(object): def test_yield_config(self): """ check config """ - pool = Pool() - AssetConf = pool.get('cashbook.assetconf') company = self.prep_company() - as_cfg = self.prep_yield_config('Fee', 'Dividend', 'GainLoss', company) - self.assertEqual(as_cfg.fee_category.rec_name, 'Fee') - self.assertEqual(as_cfg.dividend_category.rec_name, 'Dividend') - self.assertEqual(as_cfg.gainloss_book.rec_name, 'GainLoss | 0.00 usd | Open') + with Transaction().set_context({'company': company.id}): + as_cfg = self.prep_yield_config( + 'Fee', 'Dividend', 'GainLoss', company) + self.assertEqual(as_cfg.fee_category.rec_name, 'Fee') + self.assertEqual(as_cfg.dividend_category.rec_name, 'Dividend') + self.assertEqual( + as_cfg.gainloss_book.rec_name, + 'GainLoss | 0.00 usd | Open') @with_transaction() def test_yield_fee_category_in_out(self): @@ -97,70 +99,77 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + as_cfg = self.prep_yield_config( + 'Fee', 'Dividend', 'Profit-Loss', company) - type_depot = self.prep_type('Depot', 'D') - BType.write(*[ - [type_depot], - { - 'feature': 'asset', - }]) + type_depot = self.prep_type('Depot', 'D') + 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') + asset = self.prep_asset_item( + company=company, + product=self.prep_asset_product(name='Product 1')) + self.assertEqual(asset.symbol, 'usd/u') - 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 (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 (1)', + }, ])], + }]) - self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') - lines = Line.create([{ - 'cashbook': book_asset.id, - 'date': date(2022, 5, 2), - 'bookingtype': 'out', - 'description': 'trade fee, payed from asset', - 'category': as_cfg.fee_category.id, - 'amount': Decimal('4.0'), - 'quantity': Decimal('0.0'), - }]) + self.assertEqual( + book_asset.rec_name, + 'Depot | 23.50 usd | Open | 3.0000 u') + lines = Line.create([{ + 'cashbook': book_asset.id, + 'date': date(2022, 5, 2), + 'bookingtype': 'out', + 'description': 'trade fee, payed from asset', + 'category': as_cfg.fee_category.id, + 'amount': Decimal('4.0'), + 'quantity': Decimal('0.0'), + }]) - self.assertEqual(len(lines), 1) - Line.wfcheck(lines) + self.assertEqual(len(lines), 1) + Line.wfcheck(lines) - self.assertEqual(lines[0].rec_name, - '05/02/2022|Exp|-4.00 usd|trade fee, payed from asset [Fee]|0.0000 u') + self.assertEqual( + lines[0].rec_name, + '05/02/2022|Exp|-4.00 usd|trade fee, payed from asset' + + ' [Fee]|0.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('4.0')) - self.assertEqual(book_asset.rec_name, 'Depot | 19.50 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('4.0')) + self.assertEqual( + book_asset.rec_name, + 'Depot | 19.50 usd | Open | 3.0000 u') - self.assertEqual(book_asset.yield_dividend_total, Decimal('23.5')) - self.assertEqual(book_asset.yield_fee_total, Decimal('4.0')) - self.assertEqual(book_asset.yield_sales, Decimal('0.0')) - self.assertEqual(book_asset.diff_amount, Decimal('-23.5')) - self.assertEqual(book_asset.yield_balance, Decimal('0.0')) + self.assertEqual(book_asset.yield_dividend_total, Decimal('23.5')) + self.assertEqual(book_asset.yield_fee_total, Decimal('4.0')) + self.assertEqual(book_asset.yield_sales, Decimal('0.0')) + self.assertEqual(book_asset.diff_amount, Decimal('-23.5')) + self.assertEqual(book_asset.yield_balance, Decimal('0.0')) @with_transaction() def test_yield_fee_transfer_from_splitbooking_cash_out(self): @@ -170,97 +179,106 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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') + 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_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', - }, ])], - }]) - - self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') - self.assertEqual(len(book_asset.lines), 1) - self.assertEqual(book_cash.rec_name, 'Cash | 0.00 usd | Open') - self.assertEqual(len(book_cash.lines), 0) - - Cashbook.write(*[ - [book_cash], - { + 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': 'spout', - 'date': date(2022, 5, 2), - 'description': 'buy shares, fee', - 'splitlines': [('create', [{ - 'splittype': 'tr', - 'booktransf': book_asset.id, - 'description': 'buy shares', - 'quantity': Decimal('1.0'), - 'amount': Decimal('30.0'), - }, { - 'splittype': 'cat', - 'category': as_cfg.fee_category.id, - 'description': 'trade fee', - 'amount': Decimal('3.0'), + 'bookingtype': 'in', + 'date': date(2022, 5, 1), + 'amount': Decimal('23.50'), + 'quantity': Decimal('3.0'), + 'category': as_cfg.dividend_category.id, + 'description': 'Initial', + }, ])], + }]) + + self.assertEqual( + book_asset.rec_name, + 'Depot | 23.50 usd | Open | 3.0000 u') + self.assertEqual(len(book_asset.lines), 1) + self.assertEqual(book_cash.rec_name, 'Cash | 0.00 usd | Open') + self.assertEqual(len(book_cash.lines), 0) + + Cashbook.write(*[ + [book_cash], + { + 'lines': [('create', [{ + 'bookingtype': 'spout', + 'date': date(2022, 5, 2), + 'description': 'buy shares, fee', + 'splitlines': [('create', [{ + 'splittype': 'tr', + 'booktransf': book_asset.id, + 'description': 'buy shares', + 'quantity': Decimal('1.0'), + 'amount': Decimal('30.0'), + }, { + 'splittype': 'cat', + 'category': as_cfg.fee_category.id, + 'description': 'trade fee', + 'amount': Decimal('3.0'), + }])], }])], - }])], - }]) + }]) - Line.wfcheck(book_cash.lines) + Line.wfcheck(book_cash.lines) - self.assertEqual(book_asset.rec_name, 'Depot | 53.50 usd | Open | 4.0000 u') - self.assertEqual(len(book_asset.lines), 2) - self.assertEqual(book_cash.rec_name, 'Cash | -33.00 usd | Open') - self.assertEqual(len(book_cash.lines), 1) + self.assertEqual( + book_asset.rec_name, + 'Depot | 53.50 usd | Open | 4.0000 u') + self.assertEqual(len(book_asset.lines), 2) + self.assertEqual(book_cash.rec_name, 'Cash | -33.00 usd | Open') + self.assertEqual(len(book_cash.lines), 1) - self.assertEqual(book_asset.lines[0].rec_name, - '05/02/2022|from|30.00 usd|buy shares [Cash | -33.00 usd | Open]|1.0000 u') - self.assertEqual(book_asset.lines[1].rec_name, - '05/01/2022|Rev|23.50 usd|Initial [Dividend]|3.0000 u') - self.assertEqual(book_cash.lines[0].rec_name, - '05/02/2022|Exp/Sp|-33.00 usd|buy shares, fee [-]') + self.assertEqual( + book_asset.lines[0].rec_name, + '05/02/2022|from|30.00 usd|buy shares [Cash | ' + + '-33.00 usd | Open]|1.0000 u') + self.assertEqual( + book_asset.lines[1].rec_name, + '05/01/2022|Rev|23.50 usd|Initial [Dividend]|3.0000 u') + self.assertEqual( + book_cash.lines[0].rec_name, + '05/02/2022|Exp/Sp|-33.00 usd|buy shares, fee [-]') - self.assertEqual(book_asset.lines[0].asset_gainloss, Decimal('0.0')) - self.assertEqual(book_asset.lines[0].asset_dividend, Decimal('0.0')) - self.assertEqual(book_asset.lines[0].trade_fee, Decimal('3.0')) + self.assertEqual(book_asset.lines[0].asset_gainloss, Decimal('0.0')) + self.assertEqual(book_asset.lines[0].asset_dividend, Decimal('0.0')) + self.assertEqual(book_asset.lines[0].trade_fee, Decimal('3.0')) @with_transaction() def test_yield_fee_transfer_from_splitbooking_asset_out(self): @@ -270,98 +288,106 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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') + 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_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', - }, ])], - }]) - - self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') - self.assertEqual(len(book_asset.lines), 1) - self.assertEqual(book_cash.rec_name, 'Cash | 0.00 usd | Open') - self.assertEqual(len(book_cash.lines), 0) - - Cashbook.write(*[ - [book_asset], - { + 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': 'spout', - 'date': date(2022, 5, 2), - 'description': 'sell shares, fee', - 'splitlines': [('create', [{ - 'splittype': 'tr', - 'booktransf': book_cash.id, - 'description': 'sell shares', - 'quantity': Decimal('1.0'), - 'amount': Decimal('20.0'), - }, { - 'splittype': 'cat', - 'category': as_cfg.fee_category.id, - 'description': 'trade fee', - 'amount': Decimal('3.0'), - 'quantity': Decimal('0.0'), + 'bookingtype': 'in', + 'date': date(2022, 5, 1), + 'amount': Decimal('23.50'), + 'quantity': Decimal('3.0'), + 'category': as_cfg.dividend_category.id, + 'description': 'Initial', + }, ])], + }]) + + self.assertEqual( + book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') + self.assertEqual(len(book_asset.lines), 1) + self.assertEqual(book_cash.rec_name, 'Cash | 0.00 usd | Open') + self.assertEqual(len(book_cash.lines), 0) + + Cashbook.write(*[ + [book_asset], + { + 'lines': [('create', [{ + 'bookingtype': 'spout', + 'date': date(2022, 5, 2), + 'description': 'sell shares, fee', + 'splitlines': [('create', [{ + 'splittype': 'tr', + 'booktransf': book_cash.id, + 'description': 'sell shares', + 'quantity': Decimal('1.0'), + 'amount': Decimal('20.0'), + }, { + 'splittype': 'cat', + 'category': as_cfg.fee_category.id, + 'description': 'trade fee', + 'amount': Decimal('3.0'), + 'quantity': Decimal('0.0'), + }])], }])], - }])], - }]) + }]) - Line.wfcheck(book_asset.lines) + Line.wfcheck(book_asset.lines) - self.assertEqual(book_asset.rec_name, 'Depot | 0.50 usd | Open | 2.0000 u') - self.assertEqual(len(book_asset.lines), 2) - self.assertEqual(book_cash.rec_name, 'Cash | 20.00 usd | Open') - self.assertEqual(len(book_cash.lines), 1) + self.assertEqual( + book_asset.rec_name, + 'Depot | 0.50 usd | Open | 2.0000 u') + self.assertEqual(len(book_asset.lines), 2) + self.assertEqual(book_cash.rec_name, 'Cash | 20.00 usd | Open') + self.assertEqual(len(book_cash.lines), 1) - self.assertEqual(book_asset.lines[0].rec_name, - '05/01/2022|Rev|23.50 usd|Initial [Dividend]|3.0000 u') - self.assertEqual(book_asset.lines[1].rec_name, - '05/02/2022|Exp/Sp|-23.00 usd|sell shares, fee [-]|-1.0000 u') - self.assertEqual(book_cash.lines[0].rec_name, - '05/02/2022|from|20.00 usd|sell shares [Depot | 0.50 usd | Open | 2.0000 u]') + self.assertEqual( + book_asset.lines[0].rec_name, + '05/01/2022|Rev|23.50 usd|Initial [Dividend]|3.0000 u') + self.assertEqual( + book_asset.lines[1].rec_name, + '05/02/2022|Exp/Sp|-23.00 usd|sell shares, fee [-]|-1.0000 u') + self.assertEqual( + book_cash.lines[0].rec_name, + '05/02/2022|from|20.00 usd|sell shares [Depot | ' + + '0.50 usd | Open | 2.0000 u]') - self.assertEqual(book_asset.lines[1].asset_gainloss, Decimal('0.0')) - self.assertEqual(book_asset.lines[1].asset_dividend, Decimal('0.0')) - self.assertEqual(book_asset.lines[1].trade_fee, Decimal('3.0')) + self.assertEqual(book_asset.lines[1].asset_gainloss, Decimal('0.0')) + self.assertEqual(book_asset.lines[1].asset_dividend, Decimal('0.0')) + self.assertEqual(book_asset.lines[1].trade_fee, Decimal('3.0')) @with_transaction() def test_yield_dividend_category_in(self): @@ -370,66 +396,72 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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') - category_in = self.prep_category(name='Income', cattype='in') - BType.write(*[ - [type_depot], - { - 'feature': 'asset', - }]) + type_depot = self.prep_type('Depot', 'D') + self.prep_type('Cash', 'C') + category_in = self.prep_category(name='Income', cattype='in') + 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') + asset = self.prep_asset_item( + company=company, + product=self.prep_asset_product(name='Product 1')) + self.assertEqual(asset.symbol, 'usd/u') - 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', + 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, 2), + 'amount': Decimal('23.50'), + 'quantity': Decimal('3.0'), + 'category': category_in.id, + 'description': 'Initial', + }, ])], + }]) + + self.assertEqual( + book_asset.rec_name, + 'Depot | 23.50 usd | Open | 3.0000 u') + lines = Line.create([{ + 'cashbook': book_asset.id, 'date': date(2022, 5, 2), - 'amount': Decimal('23.50'), - 'quantity': Decimal('3.0'), - 'category': category_in.id, - 'description': 'Initial', - }, ])], - }]) + 'bookingtype': 'in', + 'description': 'dividend', + 'category': as_cfg.dividend_category.id, + 'amount': Decimal('4.0'), + 'quantity': Decimal('0.0'), + }]) - self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') - lines = Line.create([{ - 'cashbook': book_asset.id, - 'date': date(2022, 5, 2), - 'bookingtype': 'in', - 'description': 'dividend', - 'category': as_cfg.dividend_category.id, - 'amount': Decimal('4.0'), - 'quantity': Decimal('0.0'), - }]) + self.assertEqual(len(lines), 1) + Line.wfcheck(lines) - self.assertEqual(len(lines), 1) - Line.wfcheck(lines) + self.assertEqual( + lines[0].rec_name, + '05/02/2022|Rev|4.00 usd|dividend [Dividend]|0.0000 u') - self.assertEqual(lines[0].rec_name, - '05/02/2022|Rev|4.00 usd|dividend [Dividend]|0.0000 u') - - self.assertEqual(lines[0].asset_gainloss, Decimal('0.0')) - self.assertEqual(lines[0].asset_dividend, Decimal('4.0')) - self.assertEqual(lines[0].trade_fee, Decimal('0.0')) - self.assertEqual(book_asset.rec_name, 'Depot | 27.50 usd | Open | 3.0000 u') + self.assertEqual(lines[0].asset_gainloss, Decimal('0.0')) + self.assertEqual(lines[0].asset_dividend, Decimal('4.0')) + self.assertEqual(lines[0].trade_fee, Decimal('0.0')) + self.assertEqual( + book_asset.rec_name, + 'Depot | 27.50 usd | Open | 3.0000 u') @with_transaction() def test_yield_dividend_category_out(self): @@ -442,68 +474,75 @@ class YieldTestCase(object): Line = pool.get('cashbook.line') company = self.prep_company() - as_cfg = self.prep_yield_config('Fee', 'Dividend', 'Profit-Loss', company) + with Transaction().set_context({'company': company.id}): + 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') - category_in = self.prep_category(name='Income', cattype='in') - BType.write(*[ - [type_depot], - { - 'feature': 'asset', - }]) + type_depot = self.prep_type('Depot', 'D') + self.prep_type('Cash', 'C') + category_in = self.prep_category(name='Income', cattype='in') + 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') + asset = self.prep_asset_item( + company=company, + product=self.prep_asset_product(name='Product 1')) + self.assertEqual(asset.symbol, 'usd/u') - 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', + 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, 2), + 'amount': Decimal('23.50'), + 'quantity': Decimal('3.0'), + 'category': category_in.id, + 'description': 'Initial', + }, ])], + }]) + + Category.write(*[ + [as_cfg.dividend_category], + { + 'cattype': 'out', + }]) + + self.assertEqual( + book_asset.rec_name, + 'Depot | 23.50 usd | Open | 3.0000 u') + lines = Line.create([{ + 'cashbook': book_asset.id, 'date': date(2022, 5, 2), - 'amount': Decimal('23.50'), - 'quantity': Decimal('3.0'), - 'category': category_in.id, - 'description': 'Initial', - }, ])], - }]) + 'bookingtype': 'out', + 'description': 'dividend minus', + 'category': as_cfg.dividend_category.id, + 'amount': Decimal('4.0'), + 'quantity': Decimal('0.0'), + }]) - Category.write(*[ - [as_cfg.dividend_category], - { - 'cattype': 'out', - }]) + self.assertEqual(len(lines), 1) + Line.wfcheck(lines) - self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') - lines = Line.create([{ - 'cashbook': book_asset.id, - 'date': date(2022, 5, 2), - 'bookingtype': 'out', - 'description': 'dividend minus', - 'category': as_cfg.dividend_category.id, - 'amount': Decimal('4.0'), - 'quantity': Decimal('0.0'), - }]) + self.assertEqual( + lines[0].rec_name, + '05/02/2022|Exp|-4.00 usd|dividend minus [Dividend]|0.0000 u') - self.assertEqual(len(lines), 1) - Line.wfcheck(lines) - - self.assertEqual(lines[0].rec_name, - '05/02/2022|Exp|-4.00 usd|dividend minus [Dividend]|0.0000 u') - - self.assertEqual(lines[0].asset_gainloss, Decimal('0.0')) - self.assertEqual(lines[0].asset_dividend, Decimal('-4.0')) - self.assertEqual(lines[0].trade_fee, Decimal('0.0')) - self.assertEqual(book_asset.rec_name, 'Depot | 19.50 usd | Open | 3.0000 u') + self.assertEqual(lines[0].asset_gainloss, Decimal('0.0')) + self.assertEqual(lines[0].asset_dividend, Decimal('-4.0')) + self.assertEqual(lines[0].trade_fee, Decimal('0.0')) + self.assertEqual( + book_asset.rec_name, + 'Depot | 19.50 usd | Open | 3.0000 u') @with_transaction() def test_yield_dividend_transfer_from_splitbooking_cash_out(self): @@ -513,98 +552,108 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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_in = self.prep_category(name='Income', cattype='in') + asset = self.prep_asset_item( + company=company, + product=self.prep_asset_product(name='Product 1')) + self.assertEqual(asset.symbol, 'usd/u') + category_in = self.prep_category(name='Income', cattype='in') - 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_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': category_in.id, - 'description': 'Initial', - }, ])], - }]) - - self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') - self.assertEqual(len(book_asset.lines), 1) - self.assertEqual(book_cash.rec_name, 'Cash | 0.00 usd | Open') - self.assertEqual(len(book_cash.lines), 0) - - Cashbook.write(*[ - [book_cash], - { + 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': 'spin', - 'date': date(2022, 5, 2), - 'description': 'buy shares, dividend', - 'splitlines': [('create', [{ - 'splittype': 'tr', - 'booktransf': book_asset.id, - 'description': 'sell shares', - 'quantity': Decimal('1.0'), - 'amount': Decimal('10.0'), - }, { - 'splittype': 'cat', - 'category': as_cfg.dividend_category.id, - 'description': 'dividend', - 'amount': Decimal('10.0'), + 'bookingtype': 'in', + 'date': date(2022, 5, 1), + 'amount': Decimal('23.50'), + 'quantity': Decimal('3.0'), + 'category': category_in.id, + 'description': 'Initial', + }, ])], + }]) + + self.assertEqual( + book_asset.rec_name, + 'Depot | 23.50 usd | Open | 3.0000 u') + self.assertEqual(len(book_asset.lines), 1) + self.assertEqual(book_cash.rec_name, 'Cash | 0.00 usd | Open') + self.assertEqual(len(book_cash.lines), 0) + + Cashbook.write(*[ + [book_cash], + { + 'lines': [('create', [{ + 'bookingtype': 'spin', + 'date': date(2022, 5, 2), + 'description': 'buy shares, dividend', + 'splitlines': [('create', [{ + 'splittype': 'tr', + 'booktransf': book_asset.id, + 'description': 'sell shares', + 'quantity': Decimal('1.0'), + 'amount': Decimal('10.0'), + }, { + 'splittype': 'cat', + 'category': as_cfg.dividend_category.id, + 'description': 'dividend', + 'amount': Decimal('10.0'), + }])], }])], - }])], - }]) + }]) - Line.wfcheck(book_cash.lines) + Line.wfcheck(book_cash.lines) - self.assertEqual(book_asset.rec_name, 'Depot | 13.50 usd | Open | 2.0000 u') - self.assertEqual(len(book_asset.lines), 2) - self.assertEqual(book_cash.rec_name, 'Cash | 20.00 usd | Open') - self.assertEqual(len(book_cash.lines), 1) + self.assertEqual( + book_asset.rec_name, + 'Depot | 13.50 usd | Open | 2.0000 u') + self.assertEqual(len(book_asset.lines), 2) + self.assertEqual(book_cash.rec_name, 'Cash | 20.00 usd | Open') + self.assertEqual(len(book_cash.lines), 1) - self.assertEqual(book_asset.lines[0].rec_name, - '05/02/2022|to|-10.00 usd|sell shares [Cash | 20.00 usd | Open]|-1.0000 u') - self.assertEqual(book_asset.lines[1].rec_name, - '05/01/2022|Rev|23.50 usd|Initial [Income]|3.0000 u') - self.assertEqual(book_cash.lines[0].rec_name, - '05/02/2022|Rev/Sp|20.00 usd|buy shares, dividend [-]') + self.assertEqual( + book_asset.lines[0].rec_name, + '05/02/2022|to|-10.00 usd|sell shares [Cash | ' + + '20.00 usd | Open]|-1.0000 u') + self.assertEqual( + book_asset.lines[1].rec_name, + '05/01/2022|Rev|23.50 usd|Initial [Income]|3.0000 u') + self.assertEqual( + book_cash.lines[0].rec_name, + '05/02/2022|Rev/Sp|20.00 usd|buy shares, dividend [-]') - self.assertEqual(book_asset.lines[0].asset_gainloss, Decimal('0.0')) - self.assertEqual(book_asset.lines[0].asset_dividend, Decimal('10.0')) - self.assertEqual(book_asset.lines[0].trade_fee, Decimal('0.0')) + self.assertEqual(book_asset.lines[0].asset_gainloss, Decimal('0.0')) + self.assertEqual( + book_asset.lines[0].asset_dividend, Decimal('10.0')) + self.assertEqual(book_asset.lines[0].trade_fee, Decimal('0.0')) @with_transaction() def test_yield_dividend_transfer_to_splitbooking_aset(self): @@ -614,103 +663,116 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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_in = self.prep_category(name='Income', cattype='in') + asset = self.prep_asset_item( + company=company, + product=self.prep_asset_product(name='Product 1')) + self.assertEqual(asset.symbol, 'usd/u') + category_in = self.prep_category(name='Income', cattype='in') - 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_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': category_in.id, - 'description': 'Initial', - }, ])], - }]) - - self.assertEqual(book_asset.rec_name, 'Depot | 23.50 usd | Open | 3.0000 u') - self.assertEqual(len(book_asset.lines), 1) - self.assertEqual(book_cash.rec_name, 'Cash | 0.00 usd | Open') - self.assertEqual(len(book_cash.lines), 0) - - Cashbook.write(*[ - [book_asset], - { + 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': 'spin', - 'date': date(2022, 5, 2), - 'description': 'buy shares, dividend', - 'splitlines': [('create', [{ - 'splittype': 'tr', - 'booktransf': book_cash.id, - 'description': 'buy shares', - 'quantity': Decimal('1.0'), - 'amount': Decimal('10.0'), - }, { - 'splittype': 'cat', - 'category': as_cfg.dividend_category.id, - 'description': 'dividend', - 'amount': Decimal('10.0'), - 'quantity': Decimal('0.0'), + 'bookingtype': 'in', + 'date': date(2022, 5, 1), + 'amount': Decimal('23.50'), + 'quantity': Decimal('3.0'), + 'category': category_in.id, + 'description': 'Initial', + }, ])], + }]) + + self.assertEqual( + book_asset.rec_name, + 'Depot | 23.50 usd | Open | 3.0000 u') + self.assertEqual(len(book_asset.lines), 1) + self.assertEqual(book_cash.rec_name, 'Cash | 0.00 usd | Open') + self.assertEqual(len(book_cash.lines), 0) + + Cashbook.write(*[ + [book_asset], + { + 'lines': [('create', [{ + 'bookingtype': 'spin', + 'date': date(2022, 5, 2), + 'description': 'buy shares, dividend', + 'splitlines': [('create', [{ + 'splittype': 'tr', + 'booktransf': book_cash.id, + 'description': 'buy shares', + 'quantity': Decimal('1.0'), + 'amount': Decimal('10.0'), + }, { + 'splittype': 'cat', + 'category': as_cfg.dividend_category.id, + 'description': 'dividend', + 'amount': Decimal('10.0'), + 'quantity': Decimal('0.0'), + }])], }])], - }])], - }]) + }]) - Line.wfcheck(book_asset.lines) + Line.wfcheck(book_asset.lines) - self.assertEqual(book_asset.rec_name, 'Depot | 43.50 usd | Open | 4.0000 u') - self.assertEqual(len(book_asset.lines), 2) - self.assertEqual(book_cash.rec_name, 'Cash | -10.00 usd | Open') - self.assertEqual(len(book_cash.lines), 1) + self.assertEqual( + book_asset.rec_name, + 'Depot | 43.50 usd | Open | 4.0000 u') + self.assertEqual(len(book_asset.lines), 2) + self.assertEqual(book_cash.rec_name, 'Cash | -10.00 usd | Open') + self.assertEqual(len(book_cash.lines), 1) - self.assertEqual(book_asset.lines[0].rec_name, - '05/01/2022|Rev|23.50 usd|Initial [Income]|3.0000 u') - self.assertEqual(book_asset.lines[1].rec_name, - '05/02/2022|Rev/Sp|20.00 usd|buy shares, dividend [-]|1.0000 u') - self.assertEqual(book_cash.lines[0].rec_name, - '05/02/2022|to|-10.00 usd|buy shares [Depot | 43.50 usd | Open | 4.0000 u]') + self.assertEqual( + book_asset.lines[0].rec_name, + '05/01/2022|Rev|23.50 usd|Initial [Income]|3.0000 u') + self.assertEqual( + book_asset.lines[1].rec_name, + '05/02/2022|Rev/Sp|20.00 usd|buy shares, dividend [-]|1.0000 u') + self.assertEqual( + book_cash.lines[0].rec_name, + '05/02/2022|to|-10.00 usd|buy shares [Depot | ' + + '43.50 usd | Open | 4.0000 u]') - self.assertEqual(book_asset.lines[0].asset_gainloss, Decimal('0.0')) - self.assertEqual(book_asset.lines[0].asset_dividend, Decimal('00.0')) - self.assertEqual(book_asset.lines[0].trade_fee, Decimal('0.0')) + self.assertEqual(book_asset.lines[0].asset_gainloss, Decimal('0.0')) + self.assertEqual( + book_asset.lines[0].asset_dividend, + Decimal('00.0')) + self.assertEqual(book_asset.lines[0].trade_fee, Decimal('0.0')) - self.assertEqual(book_asset.lines[1].asset_gainloss, Decimal('0.0')) - self.assertEqual(book_asset.lines[1].asset_dividend, Decimal('10.0')) - self.assertEqual(book_asset.lines[1].trade_fee, Decimal('0.0')) + self.assertEqual(book_asset.lines[1].asset_gainloss, Decimal('0.0')) + self.assertEqual( + book_asset.lines[1].asset_dividend, + Decimal('10.0')) + self.assertEqual(book_asset.lines[1].trade_fee, Decimal('0.0')) @with_transaction() def test_yield_gainloss_spout(self): @@ -719,122 +781,138 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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') + 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_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 (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 (1)', + }, ])], + }]) - # 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': 'spout', - 'description': 'all out (1)', - 'splitlines': [('create', [{ - 'splittype': 'tr', - 'booktransf': book_cash.id, - 'description': 'sale with 40% profit (1)', - 'quantity': Decimal('3.0'), - 'amount': Decimal('30.4'), - }, { - 'splittype': 'cat', - '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 (1)', - 'amount': Decimal('-9.4'), - 'quantity': Decimal('0.0'), - }])], - }]) + # 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': 'spout', + 'description': 'all out (1)', + 'splitlines': [('create', [{ + 'splittype': 'tr', + 'booktransf': book_cash.id, + 'description': 'sale with 40% profit (1)', + 'quantity': Decimal('3.0'), + 'amount': Decimal('30.4'), + }, { + 'splittype': 'cat', + '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 (1)', + 'amount': Decimal('-9.4'), + 'quantity': Decimal('0.0'), + }])], + }]) - self.assertEqual(len(lines), 1) - self.assertEqual(lines[0].rec_name, - '05/02/2022|Exp/Sp|-23.50 usd|all out (1) [-]|-3.0000 u') - Line.wfcheck(lines) + self.assertEqual(len(lines), 1) + self.assertEqual( + lines[0].rec_name, + '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 (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 (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 (1) [Depot | 0.00 usd | Open | 0.0000 u]') + self.assertEqual( + lines[0].rec_name, + '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 (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 (1) ' + + '[Depot | 0.00 usd | Open | 0.0000 u]') - 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')) + 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')) - self.assertEqual(book_asset.rec_name, 'Depot | 0.00 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') + self.assertEqual( + book_asset.rec_name, + 'Depot | 0.00 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|Exp/Sp|-23.50 usd|all out (1) [-]|-3.0000 u') + # check searcher + 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 (1) [-]|-3.0000 u') @with_transaction() def test_yield_gainloss_spin(self): @@ -843,127 +921,148 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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') + 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_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)', - }, ])], - }]) + 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 + # 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( + 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(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].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[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].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(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') + 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') + # 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): @@ -972,111 +1071,132 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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') + 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_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)', - }, ])], - }]) + 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 + # 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( + 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(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].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].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(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') + 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') + # 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): @@ -1086,122 +1206,135 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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') + 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_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'), + 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)', + }, ])], + }]) + + # 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 (3)', 'quantity': Decimal('3.0'), - 'category': as_cfg.dividend_category.id, - 'description': 'Initial (3)', - }, ])], - }]) + 'amount': Decimal('30.4'), + }, { + 'cashbook': book_asset.id, + 'date': date(2022, 5, 2), + 'bookingtype': 'out', + 'description': 'trade fee (3)', + '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 (3)', + 'booktransf': book_gainloss.id, + 'quantity': Decimal('0.0'), + 'amount': Decimal('9.4'), + }]) - # 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 (3)', - 'quantity': Decimal('3.0'), - 'amount': Decimal('30.4'), - }, { - 'cashbook': book_asset.id, - 'date': date(2022, 5, 2), - 'bookingtype': 'out', - 'description': 'trade fee (3)', - '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 (3)', - 'booktransf': book_gainloss.id, - 'quantity': Decimal('0.0'), - 'amount': Decimal('9.4'), - }]) + self.assertEqual(len(lines), 3) + Line.wfcheck(lines) - 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 (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[0].rec_name, - '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 (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[1].rec_name, - '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 (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')) - self.assertEqual(lines[2].rec_name, - '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')) + 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') - 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'))]) - self.assertEqual(len(lines), 1) + # check searcher + lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))]) + self.assertEqual(len(lines), 1) @with_transaction() def test_yield_gainloss_mv_sp(self): @@ -1211,127 +1344,143 @@ class YieldTestCase(object): 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) + with Transaction().set_context({'company': company.id}): + 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', - }]) + 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') + 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_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 (4)', - }, ])], - }]) + 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 (4)', + }, ])], + }]) - # 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 (4)', # category 'as_cfg.fee_category' is for - 'splitlines': [('create', [{ # out-only - 'splittype': 'tr', - 'booktransf': book_asset.id, - 'description': 'sale with 40% profit (4)', - 'quantity': Decimal('-3.0'), - 'amount': Decimal('-32.9'), # profit + fee + # 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 (4)', # category 'as_cfg.fee_category' + 'splitlines': [('create', [{ # is for out-only + 'splittype': 'tr', + 'booktransf': book_asset.id, + '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 (4)', + 'amount': Decimal('2.5'), # fee + }])], }, { - 'splittype': 'cat', - 'category': as_cfg.fee_category.id, - 'description': 'trade fee (4)', - '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'), - 'description': 'gainloss_mv_sp (4)', - }]) + 'cashbook': book_asset.id, + 'date': date(2022, 5, 2), + 'bookingtype': 'mvin', + '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(len(lines), 2) + Line.wfcheck(lines) - self.assertEqual(lines[0].rec_name, - '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 (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[0].rec_name, + '05/02/2022|Exp/Sp|30.40 usd|all out (4) [-]') + # non-asset cashbook + 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].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 (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|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|gainloss_mv_sp (4) [Depot | 0.00 usd | Open | 0.0000 u]') + self.assertEqual( + lines[1].rec_name, + '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|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') + 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'))]) - self.assertEqual(len(lines), 1) + # check searcher + lines = Line.search([('asset_gainloss', '=', Decimal('9.4'))]) + self.assertEqual(len(lines), 1) # end YieldTestCase