diff --git a/modules/purchase_trade/ctrm_reporting.py b/modules/purchase_trade/ctrm_reporting.py index 4d51f5d..ac40c71 100644 --- a/modules/purchase_trade/ctrm_reporting.py +++ b/modules/purchase_trade/ctrm_reporting.py @@ -450,12 +450,25 @@ class CTRMLongShortMixin: @classmethod def _period_record(cls, period_id): + period_id = cls._context_record_id(period_id) if not period_id: return None if hasattr(period_id, 'beg_date'): return period_id return Pool().get('product.month')(period_id) + @staticmethod + def _context_record_id(value): + if not value: + return None + if hasattr(value, 'id'): + return value.id + if isinstance(value, int): + return value + if isinstance(value, str) and value.isdigit(): + return int(value) + return None + @classmethod def _period_ids_for_context(cls, prefix): context = Transaction().context @@ -463,7 +476,7 @@ class CTRMLongShortMixin: if mode == 'all': return None - exact = context.get('%s_period' % prefix) + exact = cls._context_record_id(context.get('%s_period' % prefix)) if mode == 'exact': return [exact] if exact else None @@ -1005,6 +1018,16 @@ class CTRMLongShortDetail(ModelSQL, ModelView): or getattr(currency, 'code', None) or getattr(currency, 'rec_name', None)) + @classmethod + def _context_id(cls, name): + return CTRMLongShortMixin._context_record_id( + Transaction().context.get(name)) + + @classmethod + def _context_selection(cls, name, values, default=None): + value = Transaction().context.get(name) + return value if value in values else default + @classmethod def _category_query(cls): Product = Pool().get('product.product') @@ -1041,51 +1064,63 @@ class CTRMLongShortDetail(ModelSQL, ModelView): @classmethod def _detail_source(cls): - context = Transaction().context LotQt = Pool().get('lot.qt') return LotQt.getQuery( - purchase=context.get('purchase'), - sale=context.get('sale'), - shipment=context.get('shipment'), - type=context.get('matching_status'), - state=context.get('availability'), + purchase=cls._context_id('purchase'), + sale=cls._context_id('sale'), + shipment=cls._context_id('shipment'), + type=cls._context_selection('matching_status', { + 'all', 'matched', 'not matched'}), + state=cls._context_selection('availability', { + 'all', 'available', 'reserved', 'locked'}), qttype=None, - supplier=context.get('supplier'), - client=context.get('client'), + supplier=cls._context_id('supplier'), + client=cls._context_id('client'), ps='all', - lot_status=context.get('lot_status'), + lot_status=cls._context_selection('lot_status', { + 'all', 'forecast', 'loading', 'destination', 'stock', + 'delivered'}), group='all', - product=context.get('product'), - location=context.get('location'), - origin=context.get('lot_origin'), - finished=context.get('display_finished'), - shipping_status=context.get('shipping_status'), - asof=context.get('asof'), - todate=context.get('todate'), - dimension=context.get('dimension'), - strategy=context.get('strategy'), - vessel=context.get('vessel'), - reference=context.get('reference')) + product=cls._context_id('product'), + location=cls._context_id('location'), + origin=cls._context_selection('lot_origin', { + 'all', 'open', 'physic'}), + finished=bool(Transaction().context.get('display_finished')), + shipping_status=cls._context_selection('shipping_status', { + 'all', 'unshipped', 'planned', 'scheduled', 'shipped', + 'received'}), + asof=Transaction().context.get('asof'), + todate=Transaction().context.get('todate'), + dimension=cls._context_id('dimension'), + strategy=cls._context_id('strategy'), + vessel=cls._context_id('vessel'), + reference=Transaction().context.get('reference')) @classmethod def _apply_common_filters(cls, where, product, commodity, pricing, direction, period, prefix, counterparty, side): - context = Transaction().context - if context.get('commodity'): - where &= commodity == context['commodity'] - if context.get('pricing_status'): - where &= pricing == context['pricing_status'] - if context.get('direction'): - if context['direction'] == 'flat': + commodity_id = cls._context_id('commodity') + pricing_status = cls._context_selection( + 'pricing_status', {'fixed', 'unfixed', 'partly'}) + direction_filter = cls._context_selection( + 'direction', {'long', 'short', 'flat'}) + shipment_period_id = cls._context_id('shipment_period') + counterparty_id = cls._context_id('counterparty') + if commodity_id: + where &= commodity == commodity_id + if pricing_status: + where &= pricing == pricing_status + if direction_filter: + if direction_filter == 'flat': where &= Literal(False) - elif context['direction'] != direction: + elif direction_filter != direction: where &= Literal(False) - if context.get('shipment_period'): - where &= period == context['shipment_period'] + if shipment_period_id: + where &= period == shipment_period_id where = CTRMLongShortMixin._apply_period_filter( where, period, prefix) - if context.get('counterparty'): - where &= counterparty == context['counterparty'] + if counterparty_id: + where &= counterparty == counterparty_id return where @classmethod