Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this example, the folders are based on Product Class.Image Removed

...

The following code will be in the Quote header logic:

  1. Ensure that the code only runs in the pre-phase. Items cannot be deleted or moved in the post-phase.

  2. 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.

...

  1. Code Block

...

  1. languagegroovy
    def skus = qp.getQuoteView().lineItems?.findAll{!it.folder && !it.parentId}?.sku
  2. Search for the Product Class of each item that meets the above criteria.

...

  1. Code Block

...

  1. languagegroovy
    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)}
    }
  2. Go in cycles through the line items and delete each of them. They will be re-added later.

...

  1. Code Block

...

  1. languagegroovy
    qp.getQuoteView().lineItems.each{
    if (!it.folder && !it.parentId) {
    qp.deleteItem(it.lineId)
    } 
    }
  2. Create a new QuoteStructure to add to the quote later.

  3. Determine which folders to create. Some Product Classes may already have folders and we do not want duplicates.

...

  1. Code Block

...

  1. languagegroovy
    prodClasses?.each{
    prodClass->
    def qs = new QuoteStructure()
    
    if (help.findAllWithLabel(prodClass).isEmpty()){
    folderMap[prodClass] = qs.addFolder(prodClass)
    }
  2. Find the added SKUs associated with the particular Product Class.

...

  1. Code Block

...

  1. languagegroovy
    def items = prodSrc?.findAll{it.attribute3 == prodClass}?.sku
  2. If the folder needs to be created, add the items to the folder's QuoteStructure which you have created earlier. Be sure to use createPart, as addPart will cause the code to run again and result in duplicate items.

...

  1. Code Block

...

  1. languagegroovy
    if (folderMap[prodClass]){
    
    def qsList = []
    
    def folder = folderMap.get(prodClass)
    items.each{
    sku->
    qsList << qs.createPart(sku)
    }
    
    folder?.items = qsList
    }
  2. 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.

...

  1. Code Block

...

  1. languagegroovy
    else{
    qs.parentId = help.findAllWithLabel(prodClass)[0].lineId
    
    items?.each{
    sku->
    qs.addPart(sku)
    }
    }

Finally, add the QuoteStructure to the quoteProcessor item.

...

paste-code-macro
languagegroovy
qp.addQuoteStructure(qs)

The end result will look like this:Image Removed

...

The full code:

paste-code-macro
languagegroovy
def qp = quoteProcessor
def help = qp.getHelper()

if (qp.isPrePhase()){


def skus = qp.getQuoteView().lineItems?.findAll{!it.folder && !it.parentId}?.sku
def folderMap = [:]

def rowLimit = api.getMaxFindResultsLimit()
def startRow = 0


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)}
}

def prodClasses = prodSrc?.attribute3?.unique()


//Delete items. They will be readded later.
qp.getQuoteView().lineItems.each{
if (!it.folder && !it.parentId) {
qp.deleteItem(it.lineId)
} 
}

prodClasses?.each{
prodClass->
def qs = new QuoteStructure()

if (help.findAllWithLabel(prodClass).isEmpty()){
folderMap[prodClass] = qs.addFolder(prodClass)
}

def items = prodSrc?.findAll{it.attribute3 == prodClass}?.sku


//newly created folder
if (folderMap[prodClass]){

def qsList = []

def folder = folderMap.get(prodClass)
items.each{
sku->
qsList << qs.createPart(sku)
}

folder?.items = qsList
}


//folder already exists
else{
qs.parentId = help.findAllWithLabel(prodClass)[0].lineId

items?.each{
sku->
qs.addPart(sku)
}
}

qp.addQuoteStructure(qs)


}


}