diff --git a/line.py b/line.py index e30b455..80987c7 100644 --- a/line.py +++ b/line.py @@ -283,7 +283,10 @@ class Line(SecondUomMixin, metaclass=PoolMeta): ).select( tab_line.id, Sum(Coalesce( - tab_inout_fee.credit - tab_inout_fee.debit, + # out-booking, positive amount = fee positive + tab_inout_fee.debit - tab_inout_fee.credit, + # a category-out on splitbooking as counterpart of + # transfer = fee is positive tab_mv_spline_fee.amount, Case( (tab_line.bookingtype == 'spin', tab_spline_fee.amount), diff --git a/tests/test_yield.py b/tests/test_yield.py index 8584e64..28c2de9 100644 --- a/tests/test_yield.py +++ b/tests/test_yield.py @@ -262,7 +262,7 @@ class YieldTestCase(ModuleTestCase): # fee payed from asset-account [BK01] self.assertEqual(book_asset.lines[2].rec_name, '05/01/2022|Exp|-2.00 usd|Fee [Fee]|0.0000 u') - self.assertEqual(book_asset.lines[2].trade_fee, Decimal('-2.0')) + self.assertEqual(book_asset.lines[2].trade_fee, Decimal('2.0')) self.assertEqual(book_asset.lines[2].asset_dividend, Decimal('0.0')) # dividend (2) added to asset-account [BK02] @@ -293,6 +293,173 @@ class YieldTestCase(ModuleTestCase): self.assertEqual(lines[0].rec_name, '05/01/2022|Rev/Sp|7.00 usd|Dividend [-]|0.0000 u') + @with_transaction() + def test_yield_category_fee_in_out(self): + """ check out-booking, category in/out + """ + 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_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(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].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') + + @with_transaction() + def test_yield_transfer_from_splitbooking_fee_in_out(self): + """ check out-booking, transfer to/from asset-cashbook, + fee on counterpart of splitbooking + """ + pool = Pool() + Cashbook = pool.get('cashbook.book') + BType = pool.get('cashbook.type') + Category = pool.get('cashbook.category') + Line = pool.get('cashbook.line') + + company = self.prep_company() + as_cfg = self.prep_yield_config('Fee', 'Dividend', 'Profit-Loss', company) + + type_depot = self.prep_type('Depot', 'D') + type_cash = self.prep_type('Cash', 'C') + BType.write(*[ + [type_depot], + { + 'feature': 'asset', + }]) + + asset = self.prep_asset_item( + company=company, + product = self.prep_asset_product(name='Product 1')) + self.assertEqual(asset.symbol, 'usd/u') + + 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], + { + '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) + + 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].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_gainloss_spout(self): """ check out-booking, split with fee and profit (1) @@ -768,7 +935,7 @@ class YieldTestCase(ModuleTestCase): '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].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')