Pricing manual

This commit is contained in:
2026-04-10 07:52:59 +02:00
parent 90eab73430
commit b68f475e22
6 changed files with 177 additions and 63 deletions

View File

@@ -333,10 +333,10 @@ class Pricing(ModelSQL,ModelView):
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')
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")
@@ -413,6 +413,7 @@ class Pricing(ModelSQL,ModelView):
@classmethod
def create(cls, vlist):
records = super(Pricing, cls).create(vlist)
cls._sync_manual_values(records)
cls._sync_manual_last(records)
cls._sync_eod_price(records)
return records
@@ -428,6 +429,7 @@ class Pricing(ModelSQL,ModelView):
for record_set, values in zip(actions, actions):
if values:
records.extend(record_set)
cls._sync_manual_values(records)
cls._sync_manual_last(records)
cls._sync_eod_price(records)
@@ -445,30 +447,109 @@ class Pricing(ModelSQL,ModelView):
})
@classmethod
def _get_manual_last_group_domain(cls, record):
def _is_manual_pricing_record(cls, record):
component = getattr(record, 'price_component', None)
if component is None:
return True
return not bool(getattr(component, 'auto', False))
@classmethod
def _get_pricing_group_domain(cls, record):
component = getattr(record, 'price_component', None)
if getattr(record, 'sale_line', None):
domain = [
return [
('sale_line', '=', record.sale_line.id),
('price_component', '=',
component.id if getattr(component, 'id', None) else None),
]
domain.append((
'price_component',
'=',
component.id if getattr(component, 'id', None) else None,
))
return domain
if getattr(record, 'line', None):
domain = [
return [
('line', '=', record.line.id),
('price_component', '=',
component.id if getattr(component, 'id', None) else None),
]
domain.append((
'price_component',
'=',
component.id if getattr(component, 'id', None) else None,
))
return domain
return None
@classmethod
def _get_base_quantity(cls, record):
owner = getattr(record, 'sale_line', None) or getattr(record, 'line', None)
if not owner:
return Decimal(0)
if hasattr(owner, '_get_pricing_base_quantity'):
return Decimal(str(owner._get_pricing_base_quantity() or 0))
quantity = getattr(owner, 'quantity_theorical', None)
if quantity is None:
quantity = getattr(owner, 'quantity', None)
return Decimal(str(quantity or 0))
@classmethod
def _sync_manual_values(cls, records):
if (not records
or Transaction().context.get('skip_pricing_manual_sync')):
return
domains = []
seen = set()
for record in records:
if not cls._is_manual_pricing_record(record):
continue
domain = cls._get_pricing_group_domain(record)
if not domain:
continue
key = tuple(domain)
if key in seen:
continue
seen.add(key)
domains.append(domain)
if not domains:
return
with Transaction().set_context(
skip_pricing_manual_sync=True,
skip_pricing_last_sync=True,
skip_pricing_eod_sync=True):
for domain in domains:
pricings = cls.search(
domain,
order=[('pricing_date', 'ASC'), ('id', 'ASC')])
if not pricings:
continue
base_quantity = cls._get_base_quantity(pricings[0])
cumul_qt = Decimal(0)
cumul_qt_price = Decimal(0)
total = len(pricings)
for index, pricing in enumerate(pricings):
quantity = Decimal(str(pricing.quantity or 0))
settl_price = Decimal(str(pricing.settl_price or 0))
cumul_qt += quantity
cumul_qt_price += quantity * settl_price
fixed_qt = cumul_qt
if fixed_qt > 0:
fixed_qt_price = round(cumul_qt_price / fixed_qt, 4)
else:
fixed_qt_price = Decimal(0)
unfixed_qt = base_quantity - fixed_qt
if unfixed_qt < Decimal('0.001'):
unfixed_qt = Decimal(0)
fixed_qt = base_quantity
values = {
'fixed_qt': fixed_qt,
'fixed_qt_price': fixed_qt_price,
'unfixed_qt': unfixed_qt,
'unfixed_qt_price': settl_price,
'last': index == (total - 1),
}
eod_price = cls._weighted_average_price(
values['fixed_qt'],
values['fixed_qt_price'],
values['unfixed_qt'],
values['unfixed_qt_price'],
)
values['eod_price'] = eod_price
super(Pricing, cls).write([pricing], values)
@classmethod
def _get_manual_last_group_domain(cls, record):
return cls._get_pricing_group_domain(record)
@classmethod
def _sync_manual_last(cls, records):
if not records:
@@ -505,17 +586,22 @@ class Pricing(ModelSQL,ModelView):
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)
cumul_qt_price = Decimal(0)
for pr in pricings:
cumul_qt += pr.quantity
cumul_qt_price += pr.quantity * pr.settl_price
if pr.id == self.id:
break
if cumul_qt > 0:
price = cumul_qt_price / cumul_qt
domain = self._get_pricing_group_domain(self)
if not domain:
return price
pricings = Pricing.search(domain, order=[('pricing_date', 'ASC'), ('id', 'ASC')])
if pricings:
cumul_qt = Decimal(0)
cumul_qt_price = Decimal(0)
for pr in pricings:
quantity = Decimal(str(pr.quantity or 0))
settl_price = Decimal(str(pr.settl_price or 0))
cumul_qt += quantity
cumul_qt_price += quantity * settl_price
if pr.id == self.id:
break
if cumul_qt > 0:
price = cumul_qt_price / cumul_qt
return round(price,4)