How to Do Calculations on Quote Header Level

Quote calculation consists of three phases as described in Calculation Logics Overview

To check if it is the pre-phase:

def qp = quoteProcessor;
qp.isPrePhase()

To check if it is the post-phase:

def qp = quoteProcessor;
qp.isPostPhase()


Example: How to calculate Header Deal Score element for folder Line Items

if (quoteProcessor.isPostPhase()) {
     
  getFolderDealScore("ROOT")
  
}

def getFolderDealScore(folderId) {
  def qp = quoteProcessor
  def h = qp.getHelper()
  def folderScore = 0
  def numItems = 0  
  def folder = null
  
  if(folderId == "ROOT")
  	folder = h.getRoot()
  else
   	folder = h.findByLineId(folderId) 
  
  
  if(folder == null) return 0
  
  for(line in folder.getChildren()) {
    def lineScore = 0

    if(line.isFolder()) {
      lineScore = getFolderDealScore(line.getLineId())
    } else {
      def outputMap = line.getOutputByName("DealScore")
      lineScore = outputMap?.result
    }
    numItems++
    folderScore = folderScore + (lineScore == null ? 0 : lineScore)
  }
  
  if(numItems == 0) return 0
  else {
    folderScore = folderScore/numItems
    qp.addOrUpdateOutput(folderId, ["resultName": "DealScore", "resultLabel": "Deal Score", "result" : folderScore])    
    return folderScore
  }
}