From a1ede237404f9300f203cdc7e306d86fa7bbf65f Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Tue, 14 Feb 2023 23:03:12 +0100 Subject: [PATCH] add: query for gain/loss --- line.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/line.py b/line.py index d1203a2..7990f65 100644 --- a/line.py +++ b/line.py @@ -123,12 +123,62 @@ class Line(SecondUomMixin, metaclass=PoolMeta): 'invisible': Eval('feature', '') != 'asset', }, depends=['currency_digits', 'feature']), 'get_yield_data', searcher='search_asset_dividend') - # ~ asset_gainloss = fields.Function(fields.Numeric(string='Profit/Loss', - # ~ readonly=True, digits=(16, Eval('currency_digits', 2)), - # ~ states = { - # ~ 'invisible': Eval('feature', '') != 'asset', - # ~ }, depends=['currency_digits', 'feature']), - # ~ 'get_yield_data') + asset_gainloss = fields.Function(fields.Numeric(string='Profit/Loss', + readonly=True, digits=(16, Eval('currency_digits', 2)), + states = { + 'invisible': Eval('feature', '') != 'asset', + }, depends=['currency_digits', 'feature']), + 'get_gainloss_data') + + @classmethod + def get_gainloss_data_sql(cls): + """ query for gain/loss on sell of shares + """ + pool = Pool() + 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_mv_spline = SplitLine.__table__() + + cfg1 = AssetSetting.get_singleton() + + tab_assetline = cls.search([ + ('cashbook.btype.feature', '=', 'asset'), + ], query=True) + + 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 + 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'])), + 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) & \ + (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, + condition=tab_line.bookingtype.in_(['mvin', 'mvout']) & \ + ((tab_mv_counterpart2.reference == tab_line.id) | \ + (tab_mv_counterpart2.id == tab_line.reference)) + ).select( + tab_line.id, + Coalesce( + tab_mv_counterpart2.credit - tab_mv_counterpart2.debit, + Case( + (tab_line.bookingtype == 'spin', tab_mv_spline.amount), + (tab_line.bookingtype == 'spout', tab_mv_spline.amount * Decimal('-1.0')), + ), + ).as_('gainloss'), + ) + return (tab_line, query) @classmethod def get_yield_data_sql(cls):