Add manual pricing
This commit is contained in:
@@ -325,20 +325,20 @@ class Component(ModelSQL, ModelView):
|
||||
|
||||
super(Component, cls).delete(components)
|
||||
|
||||
class Pricing(ModelSQL,ModelView):
|
||||
"Pricing"
|
||||
__name__ = 'pricing.pricing'
|
||||
|
||||
pricing_date = fields.Date("Date")
|
||||
price_component = fields.Many2One('pricing.component', "Component")#, domain=[('id', 'in', Eval('line.price_components'))], ondelete='CASCADE')
|
||||
quantity = fields.Numeric("Qt",digits='unit')
|
||||
settl_price = fields.Numeric("Settl. price",digits='unit')
|
||||
fixed_qt = fields.Numeric("Fixed qt",digits='unit',readonly=True)
|
||||
fixed_qt_price = fields.Numeric("Fixed qt price",digits='unit',readonly=True)
|
||||
unfixed_qt = fields.Numeric("Unfixed qt",digits='unit',readonly=True)
|
||||
unfixed_qt_price = fields.Numeric("Unfixed qt price",digits='unit',readonly=True)
|
||||
eod_price = fields.Numeric("EOD price",digits='unit',readonly=True)
|
||||
last = fields.Boolean("Last")
|
||||
class Pricing(ModelSQL,ModelView):
|
||||
"Pricing"
|
||||
__name__ = 'pricing.pricing'
|
||||
|
||||
pricing_date = fields.Date("Date")
|
||||
price_component = fields.Many2One('pricing.component', "Component")#, domain=[('id', 'in', Eval('line.price_components'))], ondelete='CASCADE')
|
||||
quantity = fields.Numeric("Qt",digits='unit')
|
||||
settl_price = fields.Numeric("Settl. price",digits='unit')
|
||||
fixed_qt = fields.Numeric("Fixed qt",digits='unit')
|
||||
fixed_qt_price = fields.Numeric("Fixed qt price",digits='unit')
|
||||
unfixed_qt = fields.Numeric("Unfixed qt",digits='unit')
|
||||
unfixed_qt_price = fields.Numeric("Unfixed qt price",digits='unit')
|
||||
eod_price = fields.Numeric("EOD price",digits='unit',readonly=True)
|
||||
last = fields.Boolean("Last")
|
||||
|
||||
@classmethod
|
||||
def default_fixed_qt(cls):
|
||||
@@ -364,13 +364,86 @@ class Pricing(ModelSQL,ModelView):
|
||||
def default_settl_price(cls):
|
||||
return Decimal(0)
|
||||
|
||||
@classmethod
|
||||
def default_eod_price(cls):
|
||||
return Decimal(0)
|
||||
|
||||
def get_fixed_price(self):
|
||||
price = Decimal(0)
|
||||
Pricing = Pool().get('pricing.pricing')
|
||||
@classmethod
|
||||
def default_eod_price(cls):
|
||||
return Decimal(0)
|
||||
|
||||
@staticmethod
|
||||
def _weighted_average_price(fixed_qt, fixed_price, unfixed_qt, unfixed_price):
|
||||
fixed_qt = Decimal(str(fixed_qt or 0))
|
||||
fixed_price = Decimal(str(fixed_price or 0))
|
||||
unfixed_qt = Decimal(str(unfixed_qt or 0))
|
||||
unfixed_price = Decimal(str(unfixed_price or 0))
|
||||
total_qty = fixed_qt + unfixed_qt
|
||||
if total_qty == 0:
|
||||
return Decimal(0)
|
||||
return round(
|
||||
((fixed_qt * fixed_price) + (unfixed_qt * unfixed_price)) / total_qty,
|
||||
4,
|
||||
)
|
||||
|
||||
def compute_eod_price(self):
|
||||
if getattr(self, 'sale_line', None) and hasattr(self, 'get_eod_price_sale'):
|
||||
return self.get_eod_price_sale()
|
||||
if getattr(self, 'line', None) and hasattr(self, 'get_eod_price_purchase'):
|
||||
return self.get_eod_price_purchase()
|
||||
return self._weighted_average_price(
|
||||
self.fixed_qt,
|
||||
self.fixed_qt_price,
|
||||
self.unfixed_qt,
|
||||
self.unfixed_qt_price,
|
||||
)
|
||||
|
||||
@fields.depends('fixed_qt', 'fixed_qt_price', 'unfixed_qt', 'unfixed_qt_price')
|
||||
def on_change_fixed_qt(self):
|
||||
self.eod_price = self.compute_eod_price()
|
||||
|
||||
@fields.depends('fixed_qt', 'fixed_qt_price', 'unfixed_qt', 'unfixed_qt_price')
|
||||
def on_change_fixed_qt_price(self):
|
||||
self.eod_price = self.compute_eod_price()
|
||||
|
||||
@fields.depends('fixed_qt', 'fixed_qt_price', 'unfixed_qt', 'unfixed_qt_price')
|
||||
def on_change_unfixed_qt(self):
|
||||
self.eod_price = self.compute_eod_price()
|
||||
|
||||
@fields.depends('fixed_qt', 'fixed_qt_price', 'unfixed_qt', 'unfixed_qt_price')
|
||||
def on_change_unfixed_qt_price(self):
|
||||
self.eod_price = self.compute_eod_price()
|
||||
|
||||
@classmethod
|
||||
def create(cls, vlist):
|
||||
records = super(Pricing, cls).create(vlist)
|
||||
cls._sync_eod_price(records)
|
||||
return records
|
||||
|
||||
@classmethod
|
||||
def write(cls, *args):
|
||||
super(Pricing, cls).write(*args)
|
||||
if Transaction().context.get('skip_pricing_eod_sync'):
|
||||
return
|
||||
records = []
|
||||
actions = iter(args)
|
||||
for record_set, values in zip(actions, actions):
|
||||
if values:
|
||||
records.extend(record_set)
|
||||
cls._sync_eod_price(records)
|
||||
|
||||
@classmethod
|
||||
def _sync_eod_price(cls, records):
|
||||
if not records:
|
||||
return
|
||||
with Transaction().set_context(skip_pricing_eod_sync=True):
|
||||
for record in records:
|
||||
eod_price = record.compute_eod_price()
|
||||
if Decimal(str(record.eod_price or 0)) == Decimal(str(eod_price or 0)):
|
||||
continue
|
||||
super(Pricing, cls).write([record], {
|
||||
'eod_price': eod_price,
|
||||
})
|
||||
|
||||
def get_fixed_price(self):
|
||||
price = Decimal(0)
|
||||
Pricing = Pool().get('pricing.pricing')
|
||||
pricings = Pricing.search(['price_component','=',self.price_component.id],order=[('pricing_date', 'ASC')])
|
||||
if pricings:
|
||||
cumul_qt = Decimal(0)
|
||||
|
||||
Reference in New Issue
Block a user