Commit fa3bacdc authored by Titouan Soulard's avatar Titouan Soulard

erp5_trade: add script to compute Inventory Offset

parent 30f28453
Pipeline #37144 failed with stage
in 0 seconds
portal = context.getPortalObject()
inventory_calculation_dict = {
"inventory_kw": {
"group_by_resource": 1,
"group_by_variation": 1,
"group_by_sub_variation": 1,
"resourceType": portal.getPortalProductTypeList(),
},
"group_by": [
{
"getter": "getResource",
"key": "resource_relative_url",
"property": "resource",
"portal_type": "Inventory Offset Line"
},
{
"getter": "getVariationCategoryList",
# Without the lambda expression, one gets the following error:
# > You are not allowed to access 'join' in this context.
# pylint: disable=unnecessary-lambda
"post_getter": lambda variation_list: "\n".join(variation_list),
"key": "variation_text",
"property": "variation",
"portal_type": "Inventory Offset Cell"
}
]
}
section_uid = context.getDestinationSectionUid()
node_uid = context.getDestinationUid()
inventory_list = portal.portal_simulation.getCurrentInventoryList(
section_uid=section_uid,
node_uid=node_uid,
at_date=context.getStartDate(),
**inventory_calculation_dict["inventory_kw"]
)
inventory_dict = {}
browsed_keys = []
for inventory in inventory_list:
total_quantity = inventory.total_quantity
total_price = inventory.total_price
# Build a key, usually by going from most general (product) to most specific (sub-variation)
key = []
for group_dict in inventory_calculation_dict["group_by"]:
key_method = group_dict["key"]
partial_key = inventory[key_method]
if partial_key:
key.append(partial_key)
key = tuple(key)
if key in inventory_dict:
total_quantity += inventory_dict[key]["quantity"]
total_price += inventory_dict[key]["price"]
inventory_dict[key] = {
"quantity": total_quantity,
"price": total_price
}
print(inventory_dict)
# Collect pseudo-movements (lines and cells that might create movements)
movement_generator_list = []
for inventory_line in context.objectValues(portal_type="Inventory Line"):
if inventory_line.hasCellContent():
movement_generator_list.extend(inventory_line.objectValues(portal_type="Inventory Cell"))
else:
movement_generator_list.append(inventory_line)
for movement_generator in movement_generator_list:
# Browse dictionnary by creating a key for each pseudo-movement
key = []
for group_dict in inventory_calculation_dict["group_by"]:
getter_method = getattr(movement_generator, group_dict["getter"], None)
partial_key = getter_method() if getter_method else None
if partial_key:
if "post_getter" in group_dict:
partial_key = group_dict["post_getter"](partial_key)
key.append(partial_key)
key = tuple(key)
# Case 1 (below): object in inventory API and in Inventory
# Case 2 (XXX) : object in inventory API but not in Inventory -> only full
# Case 3 (XXX) : object not in inventory API but in Inventory -> else clause
if key in inventory_dict:
browsed_keys.append(key)
# If missing quantity is negative, then there is extra quantity
missing_quantity = movement_generator.getQuantity() - inventory_dict[key]["quantity"]
if missing_quantity != 0.0:
last_object = context
# Create lines and eventually cells depending on the key
for (key_index, group_dict) in enumerate(inventory_calculation_dict["group_by"]):
if key_index > len(key) - 1:
break
object_properties = {
"portal_type": group_dict["portal_type"],
}
object_properties[group_dict["property"]] = key[key_index]
last_object = last_object.newContent(**object_properties)
print(object_properties)
last_object.edit(
quantity=missing_quantity,
quantity_unit=movement_generator.getQuantityUnit(),
# XXX-Titouan: is this correct? Maybe we should not have
# any price on lines overall.
price=movement_generator.getPrice()
)
print((missing_quantity, movement_generator.getPrice()))
return printed
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="_reconstructor" module="copy_reg"/>
</klass>
<tuple>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
<global name="object" module="__builtin__"/>
<none/>
</tuple>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Inventory_computeOffset</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
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