Document toolboxDocument toolbox

(8.12.X 2) Calculation: incurred cost

Description

Sums the total time spent of the current project issues, then it calculates the cost per the total time spent

Configuration

1. Create a Projectrak Number field called "Cost". 

2. Create a Projectrak Script field called "Incurred cost".

3. Insert the following Groovy script and modify according to your instance into "Incurred cost" field

COST_FIELD_ID: Line 12 in the script. In the example we are using 21. Please use the ID that corresponds to your "Cost Size" field

Groovy Script

incurred-cost.groovy
package deiser.profields.scripts

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.JiraAuthenticationContext
import com.deiser.jira.profields.api.field.FieldService
import com.deiser.jira.profields.api.field.number.NumberField
import com.deiser.jira.profields.api.value.ValueService

import java.text.NumberFormat

// Configuration
def COST_FIELD_ID = 21

// Components
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class)
def valueService = ComponentAccessor.getOSGiComponentInstanceOfType(ValueService.class)
def issueManager = ComponentAccessor.issueManager
def jiraAuthenticationContext = ComponentAccessor.jiraAuthenticationContext

// Get the fields
def costField = fieldService.get(COST_FIELD_ID)

// Get the field values in the current project
def cost = valueService.getValue(project, (NumberField) costField) ?: 0

// Get the total time spent
def totalTimeSpent = issueManager.getIssueIdsForProject(project.id)
        .collect { issueManager.getIssueObject(it) }
        .collect { it.timeSpent ?: 0 }

totalTimeSpent = totalTimeSpent ? totalTimeSpent.sum() : 0

// Seconds to hours
totalTimeSpent /= 3600

// Return the cost per the total time spent
def numberFormat = NumberFormat.getInstance(jiraAuthenticationContext.locale)
numberFormat.maximumFractionDigits = 0
numberFormat.minimumFractionDigits = 0
numberFormat.groupingUsed = false
return numberFormat.format((cost * totalTimeSpent).longValue())

4. Configure the project's layout including all of the fields created,.

5. Associate the Layout to the project you need this information to be applied to.

6. The result should be something like this: (please, keep in mind that this is only an example and your results should vary from what it is shown in the screenshot).