From e0a2a0f9cbb44a2ee31527c466ca3f55afea56b8 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Thu, 16 Feb 2023 16:59:50 +0100 Subject: [PATCH] line: updated get_gainloss_data_sql + todos --- line.py | 51 ++++++++++++++++++++++++++++++++------------- tests/test_yield.py | 12 ++++++++++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/line.py b/line.py index e22433c..151331f 100644 --- a/line.py +++ b/line.py @@ -138,8 +138,9 @@ class Line(SecondUomMixin, metaclass=PoolMeta): AssetSetting = pool.get('cashbook.assetconf') SplitLine = pool.get('cashbook.split') tab_line = cls.__table__() - tab_mv_counterpart1 = cls.__table__() - tab_mv_counterpart2 = cls.__table__() + tab_mvsp_counterpart = cls.__table__() + tab_mvmv_counterpart = cls.__table__() + tab_spmv_counterpart = cls.__table__() tab_mv_spline = SplitLine.__table__() cfg1 = AssetSetting.get_singleton() @@ -150,34 +151,53 @@ class Line(SecondUomMixin, metaclass=PoolMeta): query = tab_line.join(tab_assetline, condition=(tab_assetline.id==tab_line.id), - ).join(tab_mv_counterpart1, - # [MV] transfer booking, select counterpart [1] - a split-booking + + ).join(tab_mvsp_counterpart, + # [MV-SP] transfer booking, select counterpart [1] - a split-booking condition=tab_line.bookingtype.in_(['mvin', 'mvout']) & \ - ((tab_line.reference == tab_mv_counterpart1.id) | \ - (tab_line.id == tab_mv_counterpart1.reference)) & \ - (tab_mv_counterpart1.bookingtype.in_(['spin', 'spout'])), + ((tab_line.reference == tab_mvsp_counterpart.id) | \ + (tab_line.id == tab_mvsp_counterpart.reference)) & \ + (tab_mvsp_counterpart.bookingtype.in_(['spin', 'spout'])), type_ = 'LEFT OUTER', ).join(tab_mv_spline, - # [MV] fee-line is linked to split-booking-line of counterpart [1] - condition=(tab_mv_spline.line == tab_mv_counterpart1.id) & \ + # [MV-SP] line is linked to split-booking-line of counterpart [1] + condition=(tab_mv_spline.line == tab_mvsp_counterpart.id) & \ (tab_mv_spline.splittype == 'tr') & \ (tab_mv_spline.booktransf != None) & \ (tab_mv_spline.booktransf == getattr(cfg1.gainloss_book, 'id', None)), type_ = 'LEFT OUTER', - ).join(tab_mv_counterpart2, + + ).join(tab_spmv_counterpart, + # [SP-MV] split booking, select counterpart [1] - a transfer-booking + condition=tab_line.bookingtype.in_(['spin', 'spout']) & \ + ((tab_line.reference == tab_spmv_counterpart.id) | \ + (tab_line.id == tab_spmv_counterpart.reference)) & \ + tab_spmv_counterpart.bookingtype.in_(['mvin', 'mvout']) & \ + (tab_spmv_counterpart.cashbook == getattr(cfg1.gainloss_book, 'id', None)), + type_ = 'LEFT OUTER', + + ).join(tab_mvmv_counterpart, + # [MV-MV] transfer booking condition=tab_line.bookingtype.in_(['mvin', 'mvout']) & \ - ((tab_mv_counterpart2.reference == tab_line.id) | \ - (tab_mv_counterpart2.id == tab_line.reference)) + ((tab_mvmv_counterpart.reference == tab_line.id) | \ + (tab_mvmv_counterpart.id == tab_line.reference)) & \ + tab_mvmv_counterpart.bookingtype.in_(['mvin', 'mvout']) & \ + (tab_mvmv_counterpart.booktransf != None) & \ + (tab_mvmv_counterpart.booktransf == getattr(cfg1.gainloss_book, 'id', None)), + type_ = 'LEFT OUTER', ).select( tab_line.id, Coalesce( - tab_mv_counterpart2.credit - tab_mv_counterpart2.debit, + tab_mvmv_counterpart.credit - tab_mvmv_counterpart.debit, Case( - (tab_line.bookingtype == 'spin', tab_mv_spline.amount), - (tab_line.bookingtype == 'spout', tab_mv_spline.amount * Decimal('-1.0')), + (tab_line.bookingtype == 'mvin', tab_mv_spline.amount), + (tab_line.bookingtype == 'mvout', tab_mv_spline.amount * Decimal('-1.0')), ), + tab_spmv_counterpart.credit - tab_spmv_counterpart.debit, + Decimal('0.0'), ).as_('gainloss'), ) + print('\n## query:', query, query.params) return (tab_line, query) @classmethod @@ -359,6 +379,7 @@ class Line(SecondUomMixin, metaclass=PoolMeta): query.where = tab_line.id.in_([x.id for x in lines]) cursor.execute(*query) records = cursor.fetchall() + print('\n## asset_gainloss:', query, query.params) for record in records: line = Line2(record[0]) diff --git a/tests/test_yield.py b/tests/test_yield.py index b255a01..5c34eee 100644 --- a/tests/test_yield.py +++ b/tests/test_yield.py @@ -349,17 +349,27 @@ class YieldTestCase(ModuleTestCase): self.assertEqual(lines[0].rec_name, '05/02/2022|Exp/Sp|-23.50 usd|all out [-]|-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]') + self.assertEqual(lines[0].references[1].rec_name, + '05/02/2022|from|-9.40 usd|profit of sale [Depot | 0.00 usd | Open | 0.0000 u]') + + # asset --> cash self.assertEqual(lines[0].splitlines[0].rec_name, 'Exp/Sp|30.40 usd|sale with 40% profit [Cash | 22.90 usd | Open]|3.0000 u') + # asset --> category 'fee' self.assertEqual(lines[0].splitlines[1].rec_name, 'Exp/Sp|2.50 usd|trade fee [Fee]|0.0000 u') + # asset --> profit/loss self.assertEqual(lines[0].splitlines[2].rec_name, 'Exp/Sp|-9.40 usd|profit of sale [Profit-Loss | -9.40 usd | Open]|0.0000 u') print('\n# line:', lines[0].asset_gainloss, lines[0].asset_dividend, lines[0].trade_fee) self.assertEqual(lines[0].asset_gainloss, Decimal('0.0')) self.assertEqual(lines[0].asset_dividend, Decimal('0.0')) - self.assertEqual(lines[0].trade_fee, 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