Commit b98bf37c authored by Xiaowu Zhang's avatar Xiaowu Zhang

erp5_core: add MovingAverageSetToZeroWhenNegatif valuation method

parent 72528d3c
......@@ -1847,6 +1847,7 @@ class SimulationTool(BaseTool):
WeightedAverage
MonthlyWeightedAverage
MovingAverage
MovingAverageSetToZeroWhenNegatif
When using a specific valuation method, a resource_uid is expected
as well as one of (section_uid or node_uid).
"""
......@@ -1868,7 +1869,7 @@ class SimulationTool(BaseTool):
return total_result
if valuation_method not in ('Fifo', 'Filo', 'WeightedAverage',
'MonthlyWeightedAverage', 'MovingAverage'):
'MonthlyWeightedAverage', 'MovingAverage', 'MovingAverageSetToZeroWhenNegatif'):
raise ValueError("Invalid valuation method: %s" % valuation_method)
if unit_price and valuation_method in ('Fifo', 'Filo'):
......
......@@ -64,6 +64,26 @@ where
<dtml-var where_expression>
order by date
<dtml-elif "'MovingAverageSetToZeroWhenNegatif'==valuation_method">
/*
Very similar to WeightedAverage except it set total_asset_price to zero
when total_quantity is negatif
*/
set @total_asset_price=0, @total_quantity=0
<dtml-var sql_delimiter>
select
(@incoming_total_price:=IF(quantity>0, total_price, 0)) as incoming_total_price,
(@base_total_quantity:= @total_quantity+GREATEST(0, quantity)) as dummy,
(@unit_price:=IF(@base_total_quantity=0, 0, (@total_asset_price+@incoming_total_price)/@base_total_quantity)) as unit_price,
(@total_asset_price:= GREATEST(@total_asset_price + @incoming_total_price + LEAST(0, quantity) * @unit_price, 0)) as total_asset_price,
(@total_quantity:=GREATEST(@total_quantity+quantity, 0)) as dummy2
from
stock, catalog
where
<dtml-var where_expression>
order by date
<dtml-elif "valuation_method=='Fifo'">
/*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment