Commit 0ffd47a6 authored by Aurel's avatar Aurel

modify reindexation way so that we can defined the way we want to

reindex by a script specifying request parameter and properties used


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@17489 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent dcf8d749
...@@ -70,6 +70,23 @@ class Inventory(Delivery): ...@@ -70,6 +70,23 @@ class Inventory(Delivery):
""" """
return self.immediateReindexObject(**kw) return self.immediateReindexObject(**kw)
# method used to build category list that willbe set on tmp line
def appendToCategoryListFromUid(self, category_list, uid, base_category):
object_list = [x.getObject() for x in self.portal_catalog(uid=uid)]
if len(object_list):
category_list.append("%s/%s" %(base_category, object_list[0].getRelativeUrl()))
def appendToCategoryList(self, category_list, value, base_category):
category_list.append("%s/%s" %(base_category, value))
def splitAndExtendToCategoryList(self, category_list, value, *args, **kw):
if value is not None:
value_list = value.split('\n')
else:
value_list = []
category_list.extend(value_list)
def immediateReindexObject(self, temp_constructor=None, **kw): def immediateReindexObject(self, temp_constructor=None, **kw):
""" """
Rewrite reindexObject so that we can insert lines in stock table Rewrite reindexObject so that we can insert lines in stock table
...@@ -92,145 +109,196 @@ class Inventory(Delivery): ...@@ -92,145 +109,196 @@ class Inventory(Delivery):
and 'transactionless' not in method.connection_id: and 'transactionless' not in method.connection_id:
connection_id = method.connection_id connection_id = method.connection_id
break break
resource_and_variation_dict = {} default_inventory_calculation_list = ({ "inventory_params" : {"node" : self.getDestination(),
stock_object_list = [] "group_by_sub_variation" : 1,
stock_append = stock_object_list.append "group_by_variation" : 1,
"group_by_resource" : 1,
},
"list_method" : "getMovementList",
"first_level" : ({'key' : 'resource_relative_url',
'getter' : 'getResource',
'setter' : ("appendToCategoryList", "resource")},
{'key' : 'variation_text',
'getter' : 'getVariationText',
'setter' : "splitAndExtendToCategoryList"},
),
"second_level" : ({'key' : 'sub_variation_text',
'getter' : 'getSubVariationText',
'setter' : "splitAndExtendToCategoryList"},
),
},
)
method = self._getTypeBasedMethod('getDefaultInventoryCalculationList')
if method is not None:
default_inventory_calculation_list = method()
if temp_constructor is None: if temp_constructor is None:
from Products.ERP5Type.Document import newTempDeliveryLine from Products.ERP5Type.Document import newTempDeliveryLine
temp_constructor = newTempDeliveryLine temp_constructor = newTempDeliveryLine
start_date = self.getStartDate() stop_date = self.getStopDate()
node = self.getDestination()
# build a dict containing all inventory for this node stock_object_list = []
# group by resource/variation and then subvariation stock_append = stock_object_list.append
current_inventory_list = \
self.getPortalObject().portal_simulation.getInventoryList( for inventory_calculation_dict in default_inventory_calculation_list:
to_date=start_date,
node=node, # build a dict containing all inventory for this node
simulation_state=self.getPortalCurrentInventoryStateList(), # group by resource/variation and then subvariation
group_by_sub_variation=1, current_inventory_list = \
group_by_variation=1, self.getPortalObject().portal_simulation.getInventoryList(
group_by_resource=1, to_date=stop_date,
connection_id=connection_id, simulation_state=self.getPortalCurrentInventoryStateList(),
) connection_id=connection_id,
current_inventory_dict = {} **inventory_calculation_dict['inventory_params']
current_inventory_key_id_list = ('resource_relative_url', 'variation_text') )
for line in current_inventory_list: current_inventory_dict = {}
current_inventory_key = [line[x] for x in current_inventory_key_id_list] current_inventory_key_id_list = [x["key"] for x in inventory_calculation_dict['first_level']]
for x in xrange(len(current_inventory_key)): for line in current_inventory_list:
if current_inventory_key[x] is None: current_inventory_key = tuple(
current_inventory_key[x] = "" [line[x] for x in current_inventory_key_id_list])
current_inventory_key = tuple(current_inventory_key) if current_inventory_key[1] is None:
try: # To be consistent
current_inventory_by_sub_variation = \ current_inventory_key = (current_inventory_key[0], "")
current_inventory_dict[current_inventory_key]
except KeyError: if inventory_calculation_dict.has_key("second_level"):
current_inventory_by_sub_variation = \ # two level of variation
current_inventory_dict[current_inventory_key] = {} try:
current_inventory_by_sub_variation[line['sub_variation_text']] = \ current_inventory_by_sub_variation = \
line['total_quantity'] current_inventory_dict[current_inventory_key]
except KeyError:
def getCurrentInventoryBySubVariation(**criterion_dict): current_inventory_by_sub_variation = \
current_inventory_key = tuple( current_inventory_dict[current_inventory_key] = {}
[criterion_dict[x] for x in current_inventory_key_id_list]) second_level_key_id_list = [x['key'] for x in inventory_calculation_dict['second_level']]
return current_inventory_dict.get(current_inventory_key, {}) second_level_key = tuple([line[x] for x in second_level_key_id_list])
current_inventory_by_sub_variation[second_level_key] = line['total_quantity']
# Browse all movements on inventory and create diff line when necessary
not_used_inventory_dict = {}
inventory_id = self.getId()
for movement in self.getMovementList():
if movement.getResourceValue() is not None and \
movement.getQuantity() not in (None, ''):
resource_path = movement.getResource()
variation_text = movement.getVariationText()
movement_quantity = movement.getQuantity()
destination_payment_path = movement.getDestinationPayment()
resource_and_variation_key = (resource_path, variation_text)
inventory_by_subvariation_dict = getCurrentInventoryBySubVariation(
resource_relative_url=resource_path,
variation_text=variation_text)
movement_sub_variation_text = movement.getSubVariationText()
# Check wath is the quantity difference
if movement_sub_variation_text in \
inventory_by_subvariation_dict.keys():
total_quantity = inventory_by_subvariation_dict.pop(
movement_sub_variation_text)
# Put remaining subvariation in a dict to know which one
# to removed at end
not_used_inventory_dict[resource_and_variation_key] = \
inventory_by_subvariation_dict
diff_quantity = movement_quantity - total_quantity
else: else:
# Inventory for new resource/variation/sub_variation # only one level of variation
diff_quantity = movement_quantity current_inventory_dict[current_inventory_key] = line['total_quantity']
# Put remaining subvariation in a dict to know which one
# to removed at end # Browse all movements on inventory and create diff line when necessary
not_used_inventory_dict[resource_and_variation_key] = \ not_used_inventory_dict = {}
inventory_by_subvariation_dict inventory_id = self.getId()
list_method = inventory_calculation_dict['list_method']
# Create tmp movement with only diff between inventory method = getattr(self, list_method)
# and previous stock values for movement in method():
if diff_quantity != 0: if movement.getResourceValue() is not None and \
kwd = {'uid': movement.getUid(), movement.getQuantity() not in (None, ''):
'start_date': start_date}
if variation_text is not None: movement_quantity = movement.getQuantity()
variation_list = variation_text.split('\n') # construct key to retrieve inventory into dict
getter_list = [x['getter'] for x in inventory_calculation_dict['first_level']]
key_list = []
for getter in getter_list:
method = getattr(movement, getter, None)
if method is not None:
key_list.append(method())
inventory_value = current_inventory_dict.get(tuple(key_list), 0)
second_key_list = []
if inventory_calculation_dict.has_key('second_level'):
if inventory_value == 0:
inventory_value = {}
# two level
second_getter_list = [x['getter'] for x in inventory_calculation_dict['second_level']]
for getter in second_getter_list:
method = getattr(movement, getter, None)
if method is not None:
second_key_list.append(method())
second_key_list = tuple(second_key_list)
if inventory_value.has_key(second_key_list):
total_quantity = inventory_value.pop(second_key_list)
# Put remaining subvariation in a dict to know which one
# to removed at end
not_used_inventory_dict[tuple(key_list)] = inventory_value
diff_quantity = movement_quantity - total_quantity
else:
# Inventory for new resource/variation/sub_variation
diff_quantity = movement_quantity
# Put remaining subvariation in a dict to know which one
# to removed at end
not_used_inventory_dict[tuple(key_list)] = inventory_value
else: else:
variation_list = [] # we got the quantity from first level key
category_list = self.getCategoryList() diff_quantity = movement_quantity - inventory_value
sub_variation_list = []
if movement_sub_variation_text is not None: # Create tmp movement with only diff between inventory
sub_variation_list = movement_sub_variation_text.split('\n') # and previous stock values
temp_delivery_line = temp_constructor(self, if diff_quantity != 0:
inventory_id) kwd = {'uid': movement.getUid(),
kwd['quantity'] = diff_quantity 'start_date': stop_date}
category_list.append('resource/%s' % resource_path)
category_list.append( # create the tmp line and set category on it
'destination_payment/%s' % destination_payment_path) temp_delivery_line = temp_constructor(self,
category_list.extend(variation_list) inventory_id)
category_list.extend(sub_variation_list) kwd['quantity'] = diff_quantity
kwd['category_list'] = category_list category_list = self.getCategoryList()
temp_delivery_line.edit(**kwd)
stock_append(temp_delivery_line) setter_list = [x['setter'] for x in inventory_calculation_dict['first_level']]
if inventory_calculation_dict.has_key("second_level"):
# Now create line to remove some subvariation text not present setter_list.extend([x['setter'] for x in inventory_calculation_dict['second_level']])
# in new inventory value_list = list(key_list) + list(second_key_list)
inventory_uid = self.getUid() for x in xrange(len(setter_list)):
for resource_and_variation_key in not_used_inventory_dict.keys(): value = value_list[x]
inventory_by_subvariation_dict = \ setter = setter_list[x]
not_used_inventory_dict[resource_and_variation_key] base_category = ""
for sub_variation_text in inventory_by_subvariation_dict.keys(): if isinstance(setter, (tuple, list)):
category_list = self.getCategoryList() base_category = setter[1]
quantity = inventory_by_subvariation_dict[sub_variation_text] setter = setter[0]
resource_path, variation_text = resource_and_variation_key method = getattr(self, setter, None)
kwd = {'uid': inventory_uid, if method is not None:
'start_date': start_date} method(category_list, value, base_category)
if variation_text is not None:
variation_list = variation_text.split('\n') kwd['category_list'] = category_list
else: temp_delivery_line.edit(**kwd)
variation_list = [] stock_append(temp_delivery_line)
sub_variation_list = sub_variation_text.split('\n')
diff_quantity = - quantity # Now create line to remove some subvariation text not present
temp_delivery_line = temp_constructor(self, inventory_id) # in new inventory
kwd['quantity'] = diff_quantity if len(not_used_inventory_dict):
category_list.append('resource/%s' % resource_path) inventory_uid = self.getUid()
category_list.extend(variation_list) for first_level_key in not_used_inventory_dict.keys():
category_list.extend(sub_variation_list) inventory_value = \
kwd['category_list'] = category_list not_used_inventory_dict[tuple(first_level_key)]
temp_delivery_line.edit(**kwd) for second_level_key in inventory_value.keys():
stock_append(temp_delivery_line) diff_quantity = - inventory_value[tuple(second_level_key)]
kwd = {'uid': inventory_uid,
'start_date': stop_date}
# create the tmp line and set category on it
temp_delivery_line = temp_constructor(self,
inventory_id)
kwd['quantity'] = diff_quantity
category_list = self.getCategoryList()
setter_list = [x['setter'] for x in inventory_calculation_dict['first_level']]
if inventory_calculation_dict.has_key("second_level"):
setter_list.extend([x['setter'] for x in inventory_calculation_dict['second_level']])
value_list = list(first_level_key) + list(second_level_key)
for x in xrange(len(setter_list)):
value = value_list[x]
setter = setter_list[x]
base_category = ""
if isinstance(setter, (tuple, list)):
base_category = setter[1]
setter = setter[0]
method = getattr(self, setter, None)
if method is not None:
method(category_list, value, base_category)
kwd['category_list'] = category_list
temp_delivery_line.edit(**kwd)
stock_append(temp_delivery_line)
# Reindex objects # Reindex objects
object_list = [self] object_list = [self]
self.portal_catalog.catalogObjectList(object_list, self.portal_catalog.catalogObjectList(object_list,
sql_catalog_id = sql_catalog_id, sql_catalog_id = sql_catalog_id,
disable_archive=disable_archive) disable_archive=disable_archive)
if len(stock_object_list)==0:
# Make sure to remove all lines
from Products.ERP5Type.Document import newTempBase
stock_append(temp_constructor(self, inventory_id, uid=inventory_uid))
self.portal_catalog.catalogObjectList( self.portal_catalog.catalogObjectList(
stock_object_list, method_id_list=('z_catalog_stock_list', ), stock_object_list, method_id_list=('z_catalog_stock_list', ),
sql_catalog_id = sql_catalog_id, sql_catalog_id = sql_catalog_id,
disable_cache=1, check_uid=0, disable_archive=disable_archive) disable_cache=1, check_uid=0, disable_archive=disable_archive)
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