Files
tradon/modules/purchase_trade/ctrm_reporting.py
2026-05-24 17:25:46 +02:00

120 lines
4.1 KiB
Python

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)