In attachment, you will find an Excel sheet with 5 columns.
Columns A and E are not important.
Column B are the parents of their children in columns C in quantity of column D.
For exemple, parent 12 has one child which is 13 (qty 3), which in turn has 4 children 15,16,17,18 (qty 1 each).
Beware : 13 has another parent (11 - qty 1), which means that the cumulated quantity of 15, 16, 17, 18 is 4 each in total, if we consider that 13 goes into 11 (1x) and into 13 (3x).
I need two recursive functions :
1) Create a tree where each root includes a "children" array containing its respective child_id and qty. For example, 13 and it children must appear twice, once under 11 and once under 12.
Parameters :
- array of links with parent_id, child_id, quantity
- root node (if not mentionned, take the one with parent = null)
2) Create a list of use cases. For each child, determine the path to the root and the cumulated quantity.
Parameters :
- array of links with parent_id, child_id, quantity
- root node (if not mentionned, take the one with parent = null)
Example for root = 11:
child_id: 15, root_id: 13, cumulatedQty: 1, path: [{level:1, child_id: 15, parent_id: 13, qty: 1}]
child_id: 15, root_id: 12, cumulatedQty: 3, path: [{level:1, child_id: 15, parent_id: 13, qty: 1}, {level:2, child_id: 13, parent_id: 12, qty: 3}]
child_id: 15, root_id: 11, cumulatedQty: 4, path: [{level:1, child_id: 15, parent_id: 13, qty: 1}, {level:2, child_id: 13, parent_id: 12, qty: 3}, {level:2, child_id: 13, parent_id: 11, qty: 1}, {level:3, child_id: 12, parent_id: 11, qty: 1}]
Deliverables : 2 functions, testable in JSFiddle or similar.
Very ugent. Thanks.
Didier