...
- Ensure that the code only runs in the Pre Phase. Items cannot be deleted or moved in the Post Phase.
Find items in the quote that are products and are not already present in the folders. We do not want to touch the items that are already sorted.
Paste code macro language groovy def skus = qp.getQuoteView().lineItems?.findAll{!it.folder && !it.parentId}?.sku
Search for the Product Class of each item that meets the above criteria.
Paste code macro language groovy def prodSrc = [] def result = "start" while (!result.isEmpty()){ result = api.find("P",startRow,rowLimit,"",["sku","attribute3"],Filter.in("sku",skus)) ?: [] startRow += rowLimit result?.each{prodSrc.add(it)} }
Cycle Go in cycles through the line items and delete each of them. They will be re-added later.
Paste code macro language groovy qp.getQuoteView().lineItems.each{ if (!it.folder && !it.parentId) { qp.deleteItem(it.lineId) } }
- Create a new QuoteStructure to add to the quote later.
Determine which folders to create. Some Product Classes may already have folders and we do not want duplicates.
Paste code macro language groovy prodClasses?.each{ prodClass-> def qs = new QuoteStructure() if (help.findAllWithLabel(prodClass).isEmpty()){ folderMap[prodClass] = qs.addFolder(prodClass) }
Find the added skus associated with the particular Product Class.
Paste code macro language groovy def items = prodSrc?.findAll{it.attribute3 == prodClass}?.sku
If the folder needs to be created, add the items to the folder's QuoteStucture which you have created earlier. Be sure to use createPart, as addPart will cause the code to run again and result in duplicate items.
Paste code macro language groovy if (folderMap[prodClass]){ def qsList = [] def folder = folderMap.get(prodClass) items.each{ sku-> qsList << qs.createPart(sku) } folder?.items = qsList }
If the folder already exists, find the folder's line id, and add the items to the folder. Alternatively, you could detect this condition earlier and use moveItem instead of deleting.
Paste code macro language groovy else{ qs.parentId = help.findAllWithLabel(prodClass)[0].lineId items?.each{ sku-> qs.addPart(sku) } }
...