Kit Explosion Example

The Bill of Materials Kit Explosion example is a complex example that illustrates a recursive deep copy, using reactive logic. The Bill of Materials Kit Explosion example is a requirement within the Place Order example.
lac42
The Bill of Materials Kit Explosion example is a complex example that illustrates a recursive deep copy, using reactive logic. The Bill of Materials Kit Explosion example is a requirement within the Place Order example.
For more information:
Requirement
When a kit (Product consisting of other component products) is ordered, reduce the quantity of all the components parts, recursively. For example, ordering a Boing 747 reduces the bolts quantities repeatedly, for the fuselage, wings, and so forth. 
The following image illustrates the database structure:
  sample_groups_diagram.png  
Solution
When ordering a Product that is a kit (that is, composed of other Parts, such as a plane with Wings), you want to reduce inventory for each of the components of the kit.
To view the Kit Explosion topic:
  1. Open the Sample API.
  2. In the Manage section, click 
    Rules
    .
  3. Click the 
    By topic
     tab.
  4. Select 
    Kit Explosion
     from the Topic drop-down.
    The rules for the Kit Explosion topic display. The following image shows the solution:
    Screen Shot 2017-04-04 at 12.38.53 PM.png  
The requirement is to populate the 
SubItems
 for a kit-based 
Lineitem
, by copying the 
ProductBillofMaterials
 to the subitems. You can do this with the following 
explodeBillofMaterials
 event rule on the 
lineitems
 entity:
if (logicContext.verb == "INSERT" && row.product.count_components > 0) {
SysLogic.insertChildrenFrom("lineitems", row.product.components, logicContext);
}
The remaining logic (four rules) is to properly arrange for quantities (that is, if you buy two planes, you need eight engines). SubItems are a child of 
LineItem
 objects, representing the bill of materials explosion. The solution is to use the Insert Into From rule extension: see the first action rule in the following image. The 
qtyOrdered
 rule computes computes the item quantity for kits (for non-kits, it returns/defaults the entered value).
The solution requires four rules. The following image shows the 
Kit copies product.components to subItems event
 on the 
sample:LineItems
 table in the Sample API:
  Screen Shot 2017-06-19 at 8.50.13 AM.png  
This event definition recursively creates the sub-items from each item. The complete definition of 
quantity_ordered
 is a good illustration of conditional logic, depending on whether the lineitem is a sub-item.
Explore the Bill of Materials Kit Explosion Example in the REST Lab
You can use the REST Lab to POST the following to a URL such as:
http://.../APICreator/rest/demo/sample/v1/PartnerOrder
With the following request data:
{ "customer" : "Alpha and Sons" , "isReady" : false, "LineItems" : [ { "productName" : "Boing 747" , "quantityOrdered" : 2 } ] }