Commit 8534ca7e authored by Titouan Soulard's avatar Titouan Soulard

erp5_trade: FIXUP script to compute offset

parent 6780bdf2
Pipeline #37921 failed with stage
in 0 seconds
...@@ -4,29 +4,28 @@ inventory_calculation_dict = { ...@@ -4,29 +4,28 @@ inventory_calculation_dict = {
"inventory_kw": { "inventory_kw": {
"group_by_resource": 1, "group_by_resource": 1,
"group_by_variation": 1, "group_by_variation": 1,
"group_by_sub_variation": 1, #"group_by_sub_variation": 1,
"resourceType": portal.getPortalProductTypeList(), "resourceType": portal.getPortalProductTypeList(),
}, },
"group_by": [ "group_by": [
{ {
"getter": "getResource",
"key": "resource_relative_url", "key": "resource_relative_url",
"getter": "getResource",
"property": "resource", "property": "resource",
"portal_type": "Inventory Offset Line"
}, },
{ {
"key": "variation_text",
"getter": "getVariationCategoryList", "getter": "getVariationCategoryList",
# Without the lambda expression, one gets the following error: # Without the lambda expression, one gets the following error:
# > You are not allowed to access 'join' in this context. # > You are not allowed to access 'join' in this context.
# pylint: disable=unnecessary-lambda # pylint: disable=unnecessary-lambda
"post_getter": lambda variation_list: "\n".join(variation_list), "post_getter": lambda variation_list: "\n".join(variation_list),
"key": "variation_text",
"property": "variation", "property": "variation",
"portal_type": "Inventory Offset Cell"
} }
] ]
} }
# Create first dictionary of what is currently in the stock
section_uid = context.getDestinationSectionUid() section_uid = context.getDestinationSectionUid()
node_uid = context.getDestinationUid() node_uid = context.getDestinationUid()
...@@ -38,7 +37,6 @@ inventory_list = portal.portal_simulation.getCurrentInventoryList( ...@@ -38,7 +37,6 @@ inventory_list = portal.portal_simulation.getCurrentInventoryList(
) )
inventory_dict = {} inventory_dict = {}
browsed_keys = []
for inventory in inventory_list: for inventory in inventory_list:
total_quantity = inventory.total_quantity total_quantity = inventory.total_quantity
...@@ -53,18 +51,12 @@ for inventory in inventory_list: ...@@ -53,18 +51,12 @@ for inventory in inventory_list:
if partial_key: if partial_key:
key.append(partial_key) key.append(partial_key)
key = tuple(key) # Xavier's magic method: traverse dictionnary and assign to last
if key in inventory_dict: inventory_element = reduce(lambda t, x: t.setdefault(x, {}), key, inventory_dict)
total_quantity += inventory_dict[key]["quantity"] inventory_element["quantity"] = total_quantity
total_price += inventory_dict[key]["price"] inventory_element["price"] = total_price
inventory_dict[key] = { # Create second dictionary of what is reported on inventory
"quantity": total_quantity,
"price": total_price
}
print(inventory_dict)
# Collect pseudo-movements (lines and cells that might create movements)
movement_generator_list = [] movement_generator_list = []
for inventory_line in context.objectValues(portal_type="Inventory Line"): for inventory_line in context.objectValues(portal_type="Inventory Line"):
if inventory_line.hasCellContent(): if inventory_line.hasCellContent():
...@@ -72,6 +64,7 @@ for inventory_line in context.objectValues(portal_type="Inventory Line"): ...@@ -72,6 +64,7 @@ for inventory_line in context.objectValues(portal_type="Inventory Line"):
else: else:
movement_generator_list.append(inventory_line) movement_generator_list.append(inventory_line)
report_dict = {}
for movement_generator in movement_generator_list: for movement_generator in movement_generator_list:
# Browse dictionnary by creating a key for each pseudo-movement # Browse dictionnary by creating a key for each pseudo-movement
key = [] key = []
...@@ -84,39 +77,62 @@ for movement_generator in movement_generator_list: ...@@ -84,39 +77,62 @@ for movement_generator in movement_generator_list:
partial_key = group_dict["post_getter"](partial_key) partial_key = group_dict["post_getter"](partial_key)
key.append(partial_key) key.append(partial_key)
key = tuple(key) report_element = reduce(lambda t, x: t.setdefault(x, {}), key, report_dict)
report_element["quantity"] = movement_generator.getInventory()
report_element["price"] = movement_generator.getTotalPrice()
# Case 1 (below): object in inventory API and in Inventory # Normal case: iterate over report dictionary and compare with inventory
# Case 2 (XXX) : object in inventory API but not in Inventory -> only full # When full inventory, take into account all objects in inventory and compare with report
# Case 3 (XXX) : object not in inventory API but in Inventory -> else clause base_dict = inventory_dict if context.isFullInventory() else report_dict
if key in inventory_dict: compare_dict = report_dict if context.isFullInventory() else inventory_dict
browsed_keys.append(key)
# If missing quantity is negative, then there is extra quantity def compareDict(base, compare, path):
missing_quantity = movement_generator.getQuantity() - inventory_dict[key]["quantity"] # Traverse dictionary to return item or None
base_element = reduce(lambda d, x: d[x] if d and x in d else None, path, base)
compare_element = reduce(lambda d, x: d[x] if d and x in d else None, path, compare)
if missing_quantity != 0.0: if base_element is None:
last_object = context raise AssertionError("Since iterating on `base_dict` gives the key, element is expected to exist in `base_dict`")
# Create lines and eventually cells depending on the key if compare_element is None:
for (key_index, group_dict) in enumerate(inventory_calculation_dict["group_by"]): # Case (normal): object not in inventory API but in Inventory
if key_index > len(key) - 1: return base_element["quantity"]
break
# Case (normal): object in inventory API and in Inventory
object_properties = { return base_element["quantity"] - compare_element["quantity"]
"portal_type": group_dict["portal_type"],
}
object_properties[group_dict["property"]] = key[key_index]
last_object = last_object.newContent(**object_properties) for product_relative_url, base_product_content in base_dict.items():
print(object_properties) if not "price" in base_product_content:
offset_line = None
variation_list = []
last_object.edit( for variation_partial_text, base_variation_content in base_product_content.items():
missing_quantity = compareDict(base_dict, compare_dict, [product_relative_url, variation_partial_text])
if missing_quantity != 0.0:
if offset_line is None:
offset_line = context.newContent(
portal_type="Inventory Offset Line",
resource=product_relative_url,
)
variation = variation_partial_text.split("/", 1)[1]
offset_line.newContent(
portal_type="Inventory Offset Cell",
quantity=missing_quantity, quantity=missing_quantity,
quantity_unit=movement_generator.getQuantityUnit(), variation=variation,
# XXX-Titouan: is this correct? Maybe we should not have price=base_variation_content["price"],
# any price on lines overall.
price=movement_generator.getPrice()
) )
print((missing_quantity, movement_generator.getPrice())) variation_list.append(variation)
return printed if offset_line is not None:
offset_line.setVariationList(variation_list)
else:
missing_quantity = compareDict(base_dict, compare_dict, [product_relative_url])
if missing_quantity != 0.0:
context.newContent(
portal_type="Inventory Offset Line",
quantity=missing_quantity,
resource=product_relative_url,
price=base_product_content["price"],
)
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