Commit 397f0928 authored by Xiaowu Zhang's avatar Xiaowu Zhang

erp5_base: show calculation steps

parent 3162deba
......@@ -338,6 +338,14 @@ class BuilderMixin(XMLObject, Amount, Predicate):
from_date=None,
group_by_node=1, allow_intermediate_negative_stock=True,
**kw):
calculation_steps = 'Calculation for %s with resource %s\n' % (supply.getRelativeUrl(), supply.getResourceReference())
resource_dict = {
'reference': supply.getResourceReference(),
'date_list': [],
'quantity_list': [],
'url_list': [],
'portal_type_list': []
}
portal = self.getPortalObject()
if from_date is None:
from_date = DateTime().earliestTime()
......@@ -519,13 +527,14 @@ class BuilderMixin(XMLObject, Amount, Predicate):
# We only consider resources that have consumption movements in
# the future, for those who don't we do not build anything.
has_consumption_movement = False
for date, inventory, quantity, portal_type in history_list:
for date, inventory, quantity, portal_type, _ in history_list:
if quantity < 0:
has_consumption_movement = True
break
if not has_consumption_movement:
return []
calculation_steps = calculation_steps + 'Has no comsumption part in the future, Skip\n'
return [], {'calculation_step': calculation_steps, 'calculation_resource': resource_dict}
from_date = date
# Prepare period_list
......@@ -547,6 +556,8 @@ class BuilderMixin(XMLObject, Amount, Predicate):
node_uid=supply.getParentValue().getDestinationDecisionUidList(),
)
calculation_steps = calculation_steps + 'Begin with status: Future inventory %s at date %s\n' % (future_inventory_to_date, limit_date_list[0])
# We update history_list to include movements from the initial inventory date
history_list = resource_value.Resource_getInventoryHistoryList(
from_date=limit_date_list[0],
......@@ -587,6 +598,7 @@ class BuilderMixin(XMLObject, Amount, Predicate):
# default is one week
default_forward_week = supply.getForwardWeek() or 1
for period_start_date in limit_date_list:
calculation_steps = calculation_steps + '\n\nCalculate For Date: %s\n' % period_start_date
# Prepare history list of the current period
next_period_start_date = selectNextPeriodStartDate(period_start_date, default_forward_week)
......@@ -647,14 +659,30 @@ class BuilderMixin(XMLObject, Amount, Predicate):
factor=1,
forward_week=selected_forward_week
)
calculation_steps = calculation_steps + 'Check With:\n'
for period_history in period_history_list:
resource_dict['date_list'].append('%s' % period_history[0])
resource_dict['quantity_list'].append(period_history[2])
resource_dict['url_list'].append(period_history[4])
resource_dict['portal_type_list'].append(period_history[3])
calculation_steps = calculation_steps + 'Date:%s quantity: %s portal_type: %s url: %s\n' % (period_history[0], period_history[2], period_history[3], period_history[4])
calculation_steps = calculation_steps + 'We have week consumption: %s\n' % week_consumption
calculation_steps = calculation_steps + 'We have Min Stock: %s\n' % min_inventory_end_of_period
min_inventory = min_inventory_end_of_period + week_consumption
calculation_steps = calculation_steps + 'We need stock: %s\n' % min_inventory
quantity = 0
# Only need to order if there are cosumption next week or if the min stock ratio is enabled
calculation_steps = calculation_steps + 'Inventory State: %s\n' % future_inventory_to_date
if (week_consumption!= 0 or factor) and future_inventory_to_date < min_inventory:
quantity = min_inventory - future_inventory_to_date
calculation_steps = calculation_steps + 'Need to order: %s\n' % quantity
ordered_quantity, ordered_unit, effective_date, start_date, delivery_date, quantity = minimalQuantity(selected_supply, supply, quantity, period_start_date)
calculation_steps = calculation_steps + 'Need to order: %s at %s after adjustment\n' % (quantity, start_date)
# XXX CLN This is very naive, it has to be optimized
if start_date > supply.getStartDateRangeMax():
break
......@@ -662,13 +690,17 @@ class BuilderMixin(XMLObject, Amount, Predicate):
# As we are going to need to go further in time to check if new Movements are needed
# we need to keep inventory correct
#future_inventory_to_date += quantity
for date, total_inventory, quantity, portal_type in period_history_list:
calculation_steps = calculation_steps + 'Order date: %s < first_shipment_date: %s\n' % (start_date, first_shipment_date)
calculation_steps = calculation_steps + 'Accumulate stock until end of the period\n'
for date, total_inventory, quantity, portal_type, url in period_history_list:
calculation_steps = calculation_steps + 'At Date: %s Future Inventory: %s + quantity: %s of %s\n' % (date, future_inventory_to_date, quantity, url)
future_inventory_to_date += quantity
continue
#self.log("at %s min: %s, inventory:%s, quantity:%s" % (period_start_date, min_inventory, future_inventory_to_date, quantity))
if quantity != 0:
self.log("Week %s Will order %r at %s for period %s" % (delivery_date.week(), quantity, delivery_date, period_start_date))
calculation_steps = calculation_steps + "Week %s Will order %r at %s for period %s\n" % (delivery_date.week(), quantity, effective_date, period_start_date)
movement_list.append(
newMovement(
effective_date,
......@@ -680,12 +712,17 @@ class BuilderMixin(XMLObject, Amount, Predicate):
)
# calculate inventory at the end of the period
calculation_steps = calculation_steps + 'Accumulate stock until end of the period\n'
calculation_steps = calculation_steps + 'Future Inventory: %s + order quantity: %s\n' % (future_inventory_to_date, quantity)
future_inventory_to_date += quantity
for date, total_inventory, quantity, portal_type in period_history_list:
for date, total_inventory, quantity, portal_type, url in period_history_list:
calculation_steps = calculation_steps + 'At Date: %s Future Inventory: %s + quantity: %s of %s\n' % (date, future_inventory_to_date, quantity, url)
future_inventory_to_date += quantity
#return []
return movement_list
return movement_list, {'calculation_step': calculation_steps, 'calculation_resource': resource_dict}
def _searchMovementList(self, **kw):
"""
......
......@@ -44,11 +44,11 @@
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple>
<string>W:522, 35: Unused variable \'portal_type\' (unused-variable)</string>
<string>W:665, 18: Unused variable \'total_inventory\' (unused-variable)</string>
<string>W:421, 4: Unused variable \'original_min_stock_value\' (unused-variable)</string>
<string>W:422, 4: Unused variable \'original_factor\' (unused-variable)</string>
<string>W:522, 14: Unused variable \'inventory\' (unused-variable)</string>
<string>W:530, 35: Unused variable \'portal_type\' (unused-variable)</string>
<string>W:696, 18: Unused variable \'total_inventory\' (unused-variable)</string>
<string>W:429, 4: Unused variable \'original_min_stock_value\' (unused-variable)</string>
<string>W:430, 4: Unused variable \'original_factor\' (unused-variable)</string>
<string>W:530, 14: Unused variable \'inventory\' (unused-variable)</string>
</tuple>
</value>
</item>
......
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