/**
* @description To get quota defined for sales rep for particular period
* @param {string} startDate
* @param {string} endDate
* @param {array} salesrep
* @param {string} isSalesManager
* @returns
*/
const getQuota = (startDate, endDate, salesrep, isSalesManager) => {
try {
let quotaMap = {}
const quotaQueryKILM = query.create({
type: query.Type.QUOTA,
});
const entityJoinQNIS = quotaQueryKILM.autoJoin({
fieldId: ‘entity^entity’,
});
const quotaQueryKILMConditionNAMB = quotaQueryKILM.createCondition({
fieldId: ‘date’,
operator: query.Operator.WITHIN,
values: [
startDate,
endDate,
],
});
const quotaQueryKILMConditionXLMF = quotaQueryKILM.createCondition({
fieldId: ‘ismanager’,
operator: query.Operator.IS,
values: [
false,
],
});
const quotaQueryKILMConditionAAAF = entityJoinQNIS.createCondition({
fieldId: ‘id’,
operator: query.Operator.ANY_OF,
values: salesrep,
});
quotaQueryKILM.condition = quotaQueryKILM.and(
quotaQueryKILMConditionAAAF,
quotaQueryKILMConditionXLMF,
quotaQueryKILMConditionNAMB,
);
const quotaQueryKILMColumnNAZN = entityJoinQNIS.createColumn({
fieldId: ‘id’,
groupBy: false,
context: {
name: ‘RAW’,
},
// label: ‘Internal ID’,
alias: ‘id’,
});
const quotaQueryKILMColumnGTFP = quotaQueryKILM.createColumn({
fieldId: ‘mamount’,
groupBy: false,
context: {
name: ‘CURRENCY_CONSOLIDATED’,
},
// label: ‘Amount Monthly’,
alias: ‘mamount’,
});
const quotaQueryKILMColumnNHCJ = quotaQueryKILM.createColumn({
fieldId: ‘mamountquarterly’,
groupBy: false,
context: {
name: ‘CURRENCY_CONSOLIDATED’,
},
// label: ‘Amount Quarterly’,
alias: ‘mamountquarterly’,
});
const quotaQueryKILMColumnPNPL = quotaQueryKILM.createColumn({
fieldId: ‘date’,
groupBy: false,
context: {
name: ‘RAW’,
},
// label: ‘Date’,
alias: ‘date’,
});
const quotaQueryKILMColumnLCFP = entityJoinQNIS.createColumn({
fieldId: ‘entitytitle’,
groupBy: false,
context: {
name: ‘RAW’,
},
// label: ‘Name/ID’,
alias: ‘entitytitle’,
});
const quotaQueryKILMColumnKVME = quotaQueryKILM.createColumn({
fieldId: ‘entity’,
groupBy: false,
context: {
name: ‘DISPLAY’,
},
// label: ‘Rep’,
alias: ‘entity’,
});
quotaQueryKILM.columns = [
quotaQueryKILMColumnNAZN,
quotaQueryKILMColumnGTFP,
quotaQueryKILMColumnNHCJ,
quotaQueryKILMColumnPNPL,
quotaQueryKILMColumnLCFP,
quotaQueryKILMColumnKVME,
];
/* Results */
// // Note: Query.run() is limited to 5,000 results
quotaQueryKILM.run().results.forEach((result) => {
const resultMap = result.asMap();
const date = resultMap.date;
const id = resultMap.id;
const mamount = resultMap.mamount;
const entity = resultMap.entity;
//const [id, mamount, mamountquarterly, date, entitytitle, entity] = resultMap;
// log.debug(“date” + entity, date)
const dateObj = new Date(date);
const year = dateObj.getFullYear();
const month = dateObj.getMonth() + 1; // JavaScript months are zero-based
//log.debug(“month ” + entity, month)
if (!quotaMap[id]) {
quotaMap[id] = {};
}
if (!quotaMap[id][year]) {
quotaMap[id][year] = {};
}
quotaMap[id][year][month] = mamount || 0;
});
return quotaMap;
} catch (error) {
log.error(“Error @getQuota”, error);
return {};
}
}