Commit 09252af2 authored by Xiaowu Zhang's avatar Xiaowu Zhang

erp5_core: support date on line

idea is do a loop for different stop date
parent 0f780638
Pipeline #38199 passed with stage
in 0 seconds
...@@ -143,12 +143,11 @@ class Inventory(Delivery): ...@@ -143,12 +143,11 @@ class Inventory(Delivery):
if method is not None: if method is not None:
default_inventory_calculation_list = method() default_inventory_calculation_list = method()
if temp_constructor is None: if temp_constructor is None:
temp_constructor = lambda self, id, *args, **kw: self.newContent( temp_constructor = lambda self, id, *args, **kw: self.newContent(
temp_object=True, portal_type='Movement', temp_object=True, portal_type='Movement',
id=id, *args, **kw) id=id, *args, **kw)
stop_date = self.getStopDate() #stop_date = self.getStopDate()
stock_object_list = [] stock_object_list = []
stock_append = stock_object_list.append stock_append = stock_object_list.append
...@@ -158,176 +157,189 @@ class Inventory(Delivery): ...@@ -158,176 +157,189 @@ class Inventory(Delivery):
to_delete_list_append = to_delete_list.append to_delete_list_append = to_delete_list.append
for inventory_calculation_dict in default_inventory_calculation_list: for inventory_calculation_dict in default_inventory_calculation_list:
stop_date_list = []
# build a dict containing all inventory for this node
# group by resource/variation and then subvariation
current_inventory_list = \
portal.portal_simulation.getCurrentInventoryList(
to_date=stop_date,
connection_id=connection_id,
**inventory_calculation_dict['inventory_params']
)
current_inventory_dict = {}
current_inventory_key_id_list = [x["key"] for x in inventory_calculation_dict['first_level']]
for line in current_inventory_list:
current_inventory_key = [line[x] for x in current_inventory_key_id_list]
for x in xrange(len(current_inventory_key)):
if current_inventory_key[x] is None:
current_inventory_key[x] = ""
current_inventory_key = tuple(current_inventory_key)
if "second_level" in inventory_calculation_dict:
# two level of variation
try:
current_inventory_by_sub_variation = \
current_inventory_dict[current_inventory_key]
except KeyError:
current_inventory_by_sub_variation = \
current_inventory_dict[current_inventory_key] = {}
second_level_key_id_list = [x['key'] for x in inventory_calculation_dict['second_level']]
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']
else:
# only one level of variation
current_inventory_dict[current_inventory_key] = line['total_quantity']
# Browse all movements on inventory and create diff line when necessary
if self.isFullInventory():
not_used_inventory_dict = current_inventory_dict
else:
not_used_inventory_dict = {}
inventory_id = self.getId()
list_method = inventory_calculation_dict['list_method'] list_method = inventory_calculation_dict['list_method']
method = getattr(self, list_method) method = getattr(self, list_method)
__order_id_counter_list = [0]
def getOrderIdCounter():
value = __order_id_counter_list[0] # pylint: disable=cell-var-from-loop
__order_id_counter_list[0] = value + 1 # pylint: disable=cell-var-from-loop
return value
for movement in method(): for movement in method():
if movement.getResourceValue() is not None and \ if (movement.getResourceValue() is not None) and (movement.getInventoriatedQuantity() not in (None, '')):
movement.getInventoriatedQuantity() not in (None, ''): stop_date = movement.getStopDate()
if stop_date not in stop_date_list:
movement_quantity = movement.getInventoriatedQuantity() stop_date_list.append(stop_date)
# construct key to retrieve inventory into dict stop_date_list = sorted(stop_date_list)
getter_list = [x['getter'] for x in inventory_calculation_dict['first_level']] for stop_date in stop_date_list:
key_list = []
for getter in getter_list: # build a dict containing all inventory for this node
method = getattr(movement, getter, None) # group by resource/variation and then subvariation
if method is not None: current_inventory_list = \
key_list.append(method()) portal.portal_simulation.getCurrentInventoryList(
inventory_value = current_inventory_dict.get(tuple(key_list), 0) to_date=stop_date,
second_key_list = [] connection_id=connection_id,
if 'second_level' in inventory_calculation_dict: **inventory_calculation_dict['inventory_params']
if inventory_value == 0: )
inventory_value = {} current_inventory_dict = {}
# two level current_inventory_key_id_list = [x["key"] for x in inventory_calculation_dict['first_level']]
second_getter_list = [x['getter'] for x in inventory_calculation_dict['second_level']] for line in current_inventory_list:
for getter in second_getter_list: current_inventory_key = [line[x] for x in current_inventory_key_id_list]
method = getattr(movement, getter, None) for x in xrange(len(current_inventory_key)):
if method is not None: if current_inventory_key[x] is None:
second_key_list.append(method()) current_inventory_key[x] = ""
second_key_list = tuple(second_key_list) current_inventory_key = tuple(current_inventory_key)
if second_key_list in inventory_value:
total_quantity = inventory_value.pop(second_key_list) if "second_level" in inventory_calculation_dict:
# Put remaining subvariation in a dict to know which one # two level of variation
# to removed at end try:
not_used_inventory_dict[tuple(key_list)] = inventory_value current_inventory_by_sub_variation = \
diff_quantity = movement_quantity - total_quantity current_inventory_dict[current_inventory_key]
else: except KeyError:
# Inventory for new resource/variation/sub_variation current_inventory_by_sub_variation = \
diff_quantity = movement_quantity current_inventory_dict[current_inventory_key] = {}
# Put remaining subvariation in a dict to know which one second_level_key_id_list = [x['key'] for x in inventory_calculation_dict['second_level']]
# to removed at end second_level_key = tuple([line[x] for x in second_level_key_id_list])
not_used_inventory_dict[tuple(key_list)] = inventory_value current_inventory_by_sub_variation[second_level_key] = line['total_quantity']
else: else:
# we got the quantity from first level key # only one level of variation
diff_quantity = movement_quantity - inventory_value current_inventory_dict[current_inventory_key] = line['total_quantity']
# Create tmp movement
kwd = {'uid': movement.getUid(),
'start_date': stop_date,
'order_id': getOrderIdCounter(),
'mirror_order_id':getOrderIdCounter()
}
temp_delivery_line = temp_constructor(self,
inventory_id)
# set category on it only if quantity not null
# thus line with same uid will be deleted but we
# don't insert line with null quantity as we test
# some categories like resource/destination/source
# before insert but not before delete
if diff_quantity != 0:
kwd['quantity'] = diff_quantity
category_list = self.getCategoryList()
setter_list = [x['setter'] for x in inventory_calculation_dict['first_level']]
if "second_level" in inventory_calculation_dict:
setter_list.extend([x['setter'] for x in inventory_calculation_dict['second_level']])
value_list = list(key_list) + list(second_key_list)
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 # Browse all movements on inventory and create diff line when necessary
temp_delivery_line.edit(**kwd) if self.isFullInventory():
stock_append(temp_delivery_line) not_used_inventory_dict = current_inventory_dict
to_delete_list_append(temp_delivery_line)
else: else:
# Make sure we remove any any value not_used_inventory_dict = {}
to_delete_stock_uid_add(movement.getUid()) inventory_id = self.getId()
list_method = inventory_calculation_dict['list_method']
# Now create line to remove some subvariation text not present method = getattr(self, list_method)
# in new inventory
if len(not_used_inventory_dict): __order_id_counter_list = [0]
inventory_uid = self.getUid() def getOrderIdCounter():
for first_level_key in not_used_inventory_dict.keys(): value = __order_id_counter_list[0] # pylint: disable=cell-var-from-loop
inventory_value = \ __order_id_counter_list[0] = value + 1 # pylint: disable=cell-var-from-loop
not_used_inventory_dict[tuple(first_level_key)] return value
# XXX-Aurel : this code does not work with only one level of variation
for second_level_key in inventory_value.keys(): for movement in method():
diff_quantity = - inventory_value[tuple(second_level_key)] if movement.getResourceValue() is not None and \
movement.getInventoriatedQuantity() not in (None, ''):
kwd = {'uid': inventory_uid, if movement.getStopDate() != stop_date:
continue
movement_quantity = movement.getInventoriatedQuantity()
# 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 'second_level' in inventory_calculation_dict:
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 second_key_list in inventory_value:
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:
# we got the quantity from first level key
diff_quantity = movement_quantity - inventory_value
# Create tmp movement
kwd = {'uid': movement.getUid(),
'start_date': stop_date, 'start_date': stop_date,
'stop_date': stop_date,
'order_id': getOrderIdCounter(), 'order_id': getOrderIdCounter(),
'mirror_order_id':getOrderIdCounter() 'mirror_order_id':getOrderIdCounter()
} }
# create the tmp line and set category on it
temp_delivery_line = temp_constructor(self, temp_delivery_line = temp_constructor(self,
inventory_id) inventory_id)
kwd['quantity'] = diff_quantity
category_list = self.getCategoryList()
setter_list = [x['setter'] for x in inventory_calculation_dict['first_level']]
if "second_level" in inventory_calculation_dict:
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 # set category on it only if quantity not null
# thus line with same uid will be deleted but we
# don't insert line with null quantity as we test
# some categories like resource/destination/source
# before insert but not before delete
if diff_quantity != 0:
kwd['quantity'] = diff_quantity
category_list = self.getCategoryList()
setter_list = [x['setter'] for x in inventory_calculation_dict['first_level']]
if "second_level" in inventory_calculation_dict:
setter_list.extend([x['setter'] for x in inventory_calculation_dict['second_level']])
value_list = list(key_list) + list(second_key_list)
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) temp_delivery_line.edit(**kwd)
stock_append(temp_delivery_line) stock_append(temp_delivery_line)
to_delete_list_append(temp_delivery_line)
else:
# Make sure we remove any any value
to_delete_stock_uid_add(movement.getUid())
# Now create line to remove some subvariation text not present
# in new inventory
if len(not_used_inventory_dict):
inventory_uid = self.getUid()
for first_level_key in not_used_inventory_dict.keys():
inventory_value = \
not_used_inventory_dict[tuple(first_level_key)]
# XXX-Aurel : this code does not work with only one level of variation
for second_level_key in inventory_value.keys():
diff_quantity = - inventory_value[tuple(second_level_key)]
kwd = {'uid': inventory_uid,
'start_date': stop_date,
'stop_date': stop_date,
'order_id': getOrderIdCounter(),
'mirror_order_id':getOrderIdCounter()
}
# 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 "second_level" in inventory_calculation_dict:
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
immediate_reindex_archive = sql_catalog_id is not None immediate_reindex_archive = sql_catalog_id is not None
...@@ -357,4 +369,10 @@ class Inventory(Delivery): ...@@ -357,4 +369,10 @@ class Inventory(Delivery):
# Then insert new records without delete. # Then insert new records without delete.
portal.portal_catalog.catalogObjectList( portal.portal_catalog.catalogObjectList(
stock_object_list[:], method_id_list=('z_catalog_stock_list_without_delete_for_inventory_virtual_movement', ), stock_object_list[:], method_id_list=('z_catalog_stock_list_without_delete_for_inventory_virtual_movement', ),
**catalog_kw) **catalog_kw)
\ No newline at end of file
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