From 6dfb12c125808787b8b8c4fc49bb0c849aa78505 Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Sun, 24 May 2026 17:25:46 +0200 Subject: [PATCH] Global reporting --- modules/purchase_trade/__init__.py | 13 +- modules/purchase_trade/ctrm_reporting.py | 119 +++++++++++++ modules/purchase_trade/ctrm_reporting.xml | 165 ++++++++++++++++++ modules/purchase_trade/tryton.cfg | 7 +- .../ctrm_position_physical_context_form.xml | 14 ++ .../view/ctrm_position_physical_list.xml | 15 ++ 6 files changed, 325 insertions(+), 8 deletions(-) create mode 100644 modules/purchase_trade/ctrm_reporting.py create mode 100644 modules/purchase_trade/ctrm_reporting.xml create mode 100644 modules/purchase_trade/view/ctrm_position_physical_context_form.xml create mode 100644 modules/purchase_trade/view/ctrm_position_physical_list.xml diff --git a/modules/purchase_trade/__init__.py b/modules/purchase_trade/__init__.py index 071a8ca..5a9b8b2 100755 --- a/modules/purchase_trade/__init__.py +++ b/modules/purchase_trade/__init__.py @@ -7,9 +7,10 @@ from . import ( account, configuration, purchase, - sale, - global_reporting, - stock, + sale, + global_reporting, + ctrm_reporting, + stock, derivative, lot, pricing, @@ -56,8 +57,10 @@ def register(): lc.LCMT700, lc.LCMessage, lc.CreateLCStart, - global_reporting.GRConfiguration, - configuration.Configuration, + global_reporting.GRConfiguration, + ctrm_reporting.CTRMPhysicalPosition, + ctrm_reporting.CTRMPhysicalPositionContext, + configuration.Configuration, pricing.ImportPricesStart, pricing.ImportPricesResult, module='purchase_trade', type_='model') diff --git a/modules/purchase_trade/ctrm_reporting.py b/modules/purchase_trade/ctrm_reporting.py new file mode 100644 index 0000000..7ff4ee3 --- /dev/null +++ b/modules/purchase_trade/ctrm_reporting.py @@ -0,0 +1,119 @@ +from sql import Literal, Null +from sql.aggregate import Max, Min, Sum +from sql.functions import CurrentTimestamp + +from trytond.model import ModelSQL, ModelView, fields +from trytond.pool import Pool +from trytond.transaction import Transaction + + +class CTRMPhysicalPositionContext(ModelView): + "CTRM Physical Position Context" + __name__ = 'ctrm.reporting.position.physical.context' + + as_of = fields.Date("As of") + product = fields.Many2One('product.product', "Product") + supplier = fields.Many2One('party.party', "Supplier") + client = fields.Many2One('party.party', "Client") + currency = fields.Many2One('currency.currency', "Currency") + position_type = fields.Selection([ + (None, ""), + ('open', 'Open'), + ('physic', 'Physic'), + ('shipped', 'Shipped'), + ('hedge', 'Hedge'), + ('priced', 'Priced'), + ], "Position Type") + + @classmethod + def default_as_of(cls): + Date = Pool().get('ir.date') + return Date.today() + + +class CTRMPhysicalPosition(ModelSQL, ModelView): + "CTRM Physical Position" + __name__ = 'ctrm.reporting.position.physical' + + product = fields.Many2One('product.product', "Product") + supplier = fields.Many2One('party.party', "Supplier") + client = fields.Many2One('party.party', "Client") + currency = fields.Many2One('currency.currency', "Currency") + uom = fields.Many2One('product.uom', "Unit") + position_type = fields.Selection([ + ('open', 'Open'), + ('physic', 'Physic'), + ('shipped', 'Shipped'), + ('hedge', 'Hedge'), + ('priced', 'Priced'), + ], "Position Type") + physical_qty = fields.Numeric("Physical Quantity", digits=(16, 5)) + hedged_qty = fields.Numeric("Hedged Quantity", digits=(16, 5)) + net_exposure = fields.Numeric("Net Exposure", digits=(16, 5)) + amount = fields.Numeric("Amount", digits=(16, 2)) + mtm = fields.Numeric("MTM", digits=(16, 2)) + pnl = fields.Numeric("P&L", digits=(16, 2)) + period_start = fields.Date("Period Start") + period_end = fields.Date("Period End") + + @classmethod + def table_query(cls): + OpenPosition = Pool().get('open.position') + op = OpenPosition.__table__() + + context = Transaction().context + as_of = context.get('as_of') + product = context.get('product') + supplier = context.get('supplier') + client = context.get('client') + currency = context.get('currency') + position_type = context.get('position_type') + + where = Literal(True) + if as_of: + where &= ((op.period_start == Null) | (op.period_start <= as_of)) + where &= ((op.period_end == Null) | (op.period_end >= as_of)) + if product: + where &= op.product == product + if supplier: + where &= op.supplier == supplier + if client: + where &= op.client == client + if currency: + where &= op.currency == currency + if position_type: + where &= op.type == position_type + + group_by = [ + op.product, + op.supplier, + op.client, + op.currency, + op.uom, + op.type, + op.period_start, + op.period_end, + ] + + return op.select( + Literal(0).as_('create_uid'), + CurrentTimestamp().as_('create_date'), + Literal(None).as_('write_uid'), + Literal(None).as_('write_date'), + Min(op.id).as_('id'), + op.product.as_('product'), + op.supplier.as_('supplier'), + op.client.as_('client'), + op.currency.as_('currency'), + op.uom.as_('uom'), + op.type.as_('position_type'), + Sum(op.physical_qty).as_('physical_qty'), + Sum(op.hedged_qty).as_('hedged_qty'), + Sum(op.net_exposure).as_('net_exposure'), + Sum(op.amount).as_('amount'), + Sum(op.mtm).as_('mtm'), + Sum(op.amount - op.mtm).as_('pnl'), + Max(op.period_start).as_('period_start'), + Max(op.period_end).as_('period_end'), + where=where, + group_by=group_by) diff --git a/modules/purchase_trade/ctrm_reporting.xml b/modules/purchase_trade/ctrm_reporting.xml new file mode 100644 index 0000000..8ac5896 --- /dev/null +++ b/modules/purchase_trade/ctrm_reporting.xml @@ -0,0 +1,165 @@ + + + + ctrm.reporting.position.physical.context + form + ctrm_position_physical_context_form + + + ctrm.reporting.position.physical + tree + ctrm_position_physical_list + + + 1.1 Physical Position + ctrm.reporting.position.physical + ctrm.reporting.position.physical.context + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/modules/purchase_trade/tryton.cfg b/modules/purchase_trade/tryton.cfg index 1828ee0..e1cd16a 100755 --- a/modules/purchase_trade/tryton.cfg +++ b/modules/purchase_trade/tryton.cfg @@ -30,9 +30,10 @@ xml: credit_risk.xml cron.xml party.xml - forex.xml - global_reporting.xml - derivative.xml + forex.xml + global_reporting.xml + ctrm_reporting.xml + derivative.xml valuation.xml weight_report.xml dimension.xml diff --git a/modules/purchase_trade/view/ctrm_position_physical_context_form.xml b/modules/purchase_trade/view/ctrm_position_physical_context_form.xml new file mode 100644 index 0000000..fd74540 --- /dev/null +++ b/modules/purchase_trade/view/ctrm_position_physical_context_form.xml @@ -0,0 +1,14 @@ +
+