Hedging setup
This commit is contained in:
@@ -9,7 +9,7 @@ from trytond.pyson import Bool, Eval, Id, If, PYSONEncoder
|
||||
from trytond.model import (ModelSQL, ModelView)
|
||||
from trytond.tools import (cursor_dict, is_full_text, lstrip_wildcard)
|
||||
from trytond.transaction import Transaction, inactive_records
|
||||
from decimal import getcontext, Decimal, ROUND_HALF_UP
|
||||
from decimal import getcontext, Decimal, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP
|
||||
from sql.aggregate import Count, Max, Min, Sum, Avg, BoolOr
|
||||
from sql.conditionals import Case, Coalesce
|
||||
from sql import Column, Literal
|
||||
@@ -2903,6 +2903,60 @@ class Line(metaclass=PoolMeta):
|
||||
lot.lot_unit = packing_unit
|
||||
Lot.save([lot])
|
||||
|
||||
@classmethod
|
||||
def _auto_hedge_configuration(cls):
|
||||
Configuration = Pool().get('purchase.configuration')
|
||||
configurations = Configuration.search([], limit=1)
|
||||
if configurations:
|
||||
return configurations[0]
|
||||
|
||||
@classmethod
|
||||
def _auto_hedge_contract_count(cls, line, over_hedge=False):
|
||||
price_index = getattr(line, 'coffee_market_reference', None)
|
||||
if not price_index or not getattr(line, 'unit', None):
|
||||
return 0
|
||||
quantity = Decimal(str(
|
||||
getattr(line, 'quantity_theorical', None)
|
||||
or getattr(line, 'quantity', None)
|
||||
or 0))
|
||||
if quantity <= 0:
|
||||
return 0
|
||||
contract_quantity = Decimal(str(price_index.get_qt(1, line.unit) or 0))
|
||||
if contract_quantity <= 0:
|
||||
return 0
|
||||
rounding = ROUND_CEILING if over_hedge else ROUND_FLOOR
|
||||
return int((quantity / contract_quantity).to_integral_value(
|
||||
rounding=rounding))
|
||||
|
||||
@classmethod
|
||||
def _ensure_auto_hedge_derivative(cls, line):
|
||||
config = cls._auto_hedge_configuration()
|
||||
if not config or not getattr(config, 'auto_hedging', False):
|
||||
return
|
||||
if getattr(line, 'derivatives', None):
|
||||
return
|
||||
price_index = getattr(line, 'coffee_market_reference', None)
|
||||
nb_ct = cls._auto_hedge_contract_count(
|
||||
line, over_hedge=getattr(config, 'auto_hedging_over', False))
|
||||
if not price_index or nb_ct <= 0:
|
||||
return
|
||||
Derivative = Pool().get('derivative.derivative')
|
||||
Date = Pool().get('ir.date')
|
||||
quantity = price_index.get_qt(nb_ct, line.unit)
|
||||
Derivative.create([{
|
||||
'purchase': line.purchase.id if line.purchase else None,
|
||||
'line': line.id,
|
||||
'product': line.product.id if line.product else None,
|
||||
'party': line.purchase.party.id
|
||||
if line.purchase and line.purchase.party else None,
|
||||
'price_index': price_index.id,
|
||||
'nb_ct': nb_ct,
|
||||
'price': getattr(line, 'coffee_market_price', None),
|
||||
'direction': 'short',
|
||||
'trade_date': Date.today(),
|
||||
'open_qty': quantity,
|
||||
}])
|
||||
|
||||
@classmethod
|
||||
def write(cls, *args):
|
||||
actions = iter(args)
|
||||
@@ -3135,6 +3189,7 @@ class Line(metaclass=PoolMeta):
|
||||
fl.line = line.id
|
||||
FeeLots.save([fl])
|
||||
cls._sync_virtual_lot_packing(line)
|
||||
cls._ensure_auto_hedge_derivative(line)
|
||||
|
||||
if line.fee_:
|
||||
if not line.fee_.purchase:
|
||||
|
||||
Reference in New Issue
Block a user