feature/IO-3587-Comission-Cut - Implement
This commit is contained in:
@@ -1,15 +1,196 @@
|
||||
const Dinero = require("dinero.js");
|
||||
const queries = require("../graphql-client/queries");
|
||||
const rdiff = require("recursive-diff");
|
||||
|
||||
const logger = require("../utils/logger");
|
||||
|
||||
// Dinero.defaultCurrency = "USD";
|
||||
// Dinero.globalLocale = "en-CA";
|
||||
Dinero.globalRoundingMode = "HALF_EVEN";
|
||||
Dinero.globalFormatRoundingMode = "HALF_EVEN";
|
||||
|
||||
const PAYOUT_METHODS = {
|
||||
hourly: "hourly",
|
||||
commission: "commission"
|
||||
};
|
||||
|
||||
const CURRENCY_PRECISION = 2;
|
||||
const HOURS_PRECISION = 5;
|
||||
|
||||
const toNumber = (value) => {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : 0;
|
||||
};
|
||||
|
||||
const normalizeNumericString = (value) => {
|
||||
if (typeof value === "string") {
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
const asString = value.toString();
|
||||
|
||||
if (!asString.toLowerCase().includes("e")) {
|
||||
return asString;
|
||||
}
|
||||
|
||||
return value.toFixed(12).replace(/0+$/, "").replace(/\.$/, "");
|
||||
}
|
||||
|
||||
return `${value ?? ""}`.trim();
|
||||
};
|
||||
|
||||
const decimalToDinero = (value, errorMessage = "Invalid numeric value.") => {
|
||||
const normalizedValue = normalizeNumericString(value);
|
||||
const parsedValue = Number(normalizedValue);
|
||||
|
||||
if (!Number.isFinite(parsedValue)) {
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
const isNegative = normalizedValue.startsWith("-");
|
||||
const unsignedValue = normalizedValue.replace(/^[+-]/, "");
|
||||
const [wholePart = "0", fractionPartRaw = ""] = unsignedValue.split(".");
|
||||
const wholeDigits = wholePart.replace(/\D/g, "") || "0";
|
||||
const fractionDigits = fractionPartRaw.replace(/\D/g, "");
|
||||
const amount = Number(`${wholeDigits}${fractionDigits}` || "0") * (isNegative ? -1 : 1);
|
||||
|
||||
return Dinero({
|
||||
amount,
|
||||
precision: fractionDigits.length
|
||||
});
|
||||
};
|
||||
|
||||
const roundValueWithDinero = (value, precision, errorMessage) =>
|
||||
decimalToDinero(value, errorMessage).convertPrecision(precision, Dinero.globalRoundingMode).toUnit();
|
||||
|
||||
const roundCurrency = (value, errorMessage = "Invalid currency value.") =>
|
||||
roundValueWithDinero(value, CURRENCY_PRECISION, errorMessage);
|
||||
|
||||
const roundHours = (value, errorMessage = "Invalid hours value.") => roundValueWithDinero(value, HOURS_PRECISION, errorMessage);
|
||||
|
||||
const normalizePayoutMethod = (value) =>
|
||||
value === PAYOUT_METHODS.commission ? PAYOUT_METHODS.commission : PAYOUT_METHODS.hourly;
|
||||
|
||||
const hasOwnValue = (obj, key) => Object.prototype.hasOwnProperty.call(obj || {}, key);
|
||||
|
||||
const getJobSaleRateField = (laborType) => `rate_${String(laborType || "").toLowerCase()}`;
|
||||
|
||||
const getTeamMemberLabel = (teamMember) => {
|
||||
const fullName = `${teamMember?.employee?.first_name || ""} ${teamMember?.employee?.last_name || ""}`.trim();
|
||||
return fullName || teamMember?.employee?.id || teamMember?.employeeid || "unknown employee";
|
||||
};
|
||||
|
||||
const parseRequiredNumber = (value, errorMessage) => {
|
||||
const parsed = Number(value);
|
||||
|
||||
if (!Number.isFinite(parsed)) {
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return parsed;
|
||||
};
|
||||
|
||||
const buildFallbackPayoutContext = ({ laborType, rate }) => ({
|
||||
payout_type: "legacy",
|
||||
payout_method: "legacy",
|
||||
cut_percent_applied: null,
|
||||
source_labor_rate: null,
|
||||
source_labor_type: laborType,
|
||||
effective_rate: roundCurrency(rate)
|
||||
});
|
||||
|
||||
function BuildPayoutDetails(job, teamMember, laborType) {
|
||||
const payoutMethod = normalizePayoutMethod(teamMember?.payout_method);
|
||||
const teamMemberLabel = getTeamMemberLabel(teamMember);
|
||||
const sourceLaborRateField = getJobSaleRateField(laborType);
|
||||
|
||||
if (payoutMethod === PAYOUT_METHODS.hourly && !hasOwnValue(teamMember?.labor_rates, laborType)) {
|
||||
throw new Error(`Missing hourly payout rate for ${teamMemberLabel} on labor type ${laborType}.`);
|
||||
}
|
||||
|
||||
if (payoutMethod === PAYOUT_METHODS.commission && !hasOwnValue(teamMember?.commission_rates, laborType)) {
|
||||
throw new Error(`Missing commission percent for ${teamMemberLabel} on labor type ${laborType}.`);
|
||||
}
|
||||
|
||||
if (payoutMethod === PAYOUT_METHODS.commission && !hasOwnValue(job, sourceLaborRateField)) {
|
||||
throw new Error(`Missing sale rate ${sourceLaborRateField} for labor type ${laborType}.`);
|
||||
}
|
||||
|
||||
const hourlyRate =
|
||||
payoutMethod === PAYOUT_METHODS.hourly
|
||||
? roundCurrency(
|
||||
parseRequiredNumber(
|
||||
teamMember?.labor_rates?.[laborType],
|
||||
`Invalid hourly payout rate for ${teamMemberLabel} on labor type ${laborType}.`
|
||||
)
|
||||
)
|
||||
: null;
|
||||
|
||||
const commissionPercent =
|
||||
payoutMethod === PAYOUT_METHODS.commission
|
||||
? roundCurrency(
|
||||
parseRequiredNumber(
|
||||
teamMember?.commission_rates?.[laborType],
|
||||
`Invalid commission percent for ${teamMemberLabel} on labor type ${laborType}.`
|
||||
)
|
||||
)
|
||||
: null;
|
||||
|
||||
if (commissionPercent !== null && (commissionPercent < 0 || commissionPercent > 100)) {
|
||||
throw new Error(`Commission percent for ${teamMemberLabel} on labor type ${laborType} must be between 0 and 100.`);
|
||||
}
|
||||
|
||||
const sourceLaborRate =
|
||||
payoutMethod === PAYOUT_METHODS.commission
|
||||
? roundCurrency(
|
||||
parseRequiredNumber(job?.[sourceLaborRateField], `Invalid sale rate ${sourceLaborRateField} for labor type ${laborType}.`)
|
||||
)
|
||||
: null;
|
||||
|
||||
const effectiveRate =
|
||||
payoutMethod === PAYOUT_METHODS.commission
|
||||
? roundCurrency((sourceLaborRate * toNumber(commissionPercent)) / 100)
|
||||
: hourlyRate;
|
||||
|
||||
return {
|
||||
effectiveRate,
|
||||
payoutContext: {
|
||||
payout_type: payoutMethod === PAYOUT_METHODS.commission ? "cut" : "hourly",
|
||||
payout_method: payoutMethod,
|
||||
cut_percent_applied: commissionPercent,
|
||||
source_labor_rate: sourceLaborRate,
|
||||
source_labor_type: laborType,
|
||||
effective_rate: effectiveRate
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function BuildGeneratedPayoutContext({ baseContext, generatedBy, generatedFrom, taskName, usedTicketFallback }) {
|
||||
return {
|
||||
...(baseContext || {}),
|
||||
generated_by: generatedBy,
|
||||
generated_at: new Date().toISOString(),
|
||||
generated_from: generatedFrom,
|
||||
task_name: taskName,
|
||||
used_ticket_fallback: Boolean(usedTicketFallback)
|
||||
};
|
||||
}
|
||||
|
||||
function getAllKeys(...objects) {
|
||||
return [...new Set(objects.flatMap((obj) => (obj ? Object.keys(obj) : [])))];
|
||||
}
|
||||
|
||||
function buildPayAllMemo({ deltaHours, hasExpected, hasClaimed, userEmail }) {
|
||||
if (!hasClaimed && deltaHours > 0) {
|
||||
return `Add unflagged hours. (${userEmail})`;
|
||||
}
|
||||
|
||||
if (!hasExpected && deltaHours < 0) {
|
||||
return `Remove flagged hours per assignment. (${userEmail})`;
|
||||
}
|
||||
|
||||
return `Adjust flagged hours per assignment. (${userEmail})`;
|
||||
}
|
||||
|
||||
exports.payall = async function (req, res) {
|
||||
const { jobid, calculateOnly } = req.body;
|
||||
const { jobid } = req.body;
|
||||
logger.log("job-payroll-pay-all", "DEBUG", req.user.email, jobid, null);
|
||||
|
||||
const BearerToken = req.BearerToken;
|
||||
@@ -22,253 +203,183 @@ exports.payall = async function (req, res) {
|
||||
id: jobid
|
||||
});
|
||||
|
||||
//iterate over each ticket, building a hash of team -> employee to calculate total assigned hours.
|
||||
|
||||
const { employeeHash, assignmentHash } = CalculateExpectedHoursForJob(job);
|
||||
const ticketHash = CalculateTicketsHoursForJob(job);
|
||||
|
||||
if (assignmentHash.unassigned > 0) {
|
||||
res.json({ success: false, error: "Not all hours have been assigned." });
|
||||
return;
|
||||
}
|
||||
|
||||
//Calculate how much time each tech should have by labor type.
|
||||
//Doing this order creates a diff of changes on the ticket hash to make it the same as the employee hash.
|
||||
const recursiveDiff = rdiff.getDiff(ticketHash, employeeHash, true);
|
||||
|
||||
const ticketsToInsert = [];
|
||||
const employeeIds = getAllKeys(employeeHash, ticketHash);
|
||||
|
||||
recursiveDiff.forEach((diff) => {
|
||||
//Every iteration is what we would need to insert into the time ticket hash
|
||||
//so that it would match the employee hash exactly.
|
||||
const path = diffParser(diff);
|
||||
employeeIds.forEach((employeeId) => {
|
||||
const expectedByLabor = employeeHash[employeeId] || {};
|
||||
const claimedByLabor = ticketHash[employeeId] || {};
|
||||
|
||||
if (diff.op === "add") {
|
||||
// console.log(Object.keys(diff.val));
|
||||
if (typeof diff.val === "object" && Object.keys(diff.val).length > 1) {
|
||||
//Multiple values to add.
|
||||
Object.keys(diff.val).forEach((key) => {
|
||||
// console.log("Hours", diff.val[key][Object.keys(diff.val[key])[0]]);
|
||||
// console.log("Rate", Object.keys(diff.val[key])[0]);
|
||||
ticketsToInsert.push({
|
||||
task_name: "Pay All",
|
||||
jobid: job.id,
|
||||
bodyshopid: job.bodyshop.id,
|
||||
employeeid: path.employeeid,
|
||||
productivehrs: diff.val[key][Object.keys(diff.val[key])[0]],
|
||||
rate: Object.keys(diff.val[key])[0],
|
||||
ciecacode: key,
|
||||
cost_center: job.bodyshop.md_responsibility_centers.defaults.costs[key],
|
||||
flat_rate: true,
|
||||
memo: `Add unflagged hours. (${req.user.email})`
|
||||
});
|
||||
});
|
||||
} else {
|
||||
//Only the 1 value to add.
|
||||
ticketsToInsert.push({
|
||||
task_name: "Pay All",
|
||||
jobid: job.id,
|
||||
bodyshopid: job.bodyshop.id,
|
||||
employeeid: path.employeeid,
|
||||
productivehrs: path.hours,
|
||||
rate: path.rate,
|
||||
ciecacode: path.mod_lbr_ty,
|
||||
flat_rate: true,
|
||||
cost_center: job.bodyshop.md_responsibility_centers.defaults.costs[path.mod_lbr_ty],
|
||||
memo: `Add unflagged hours. (${req.user.email})`
|
||||
});
|
||||
getAllKeys(expectedByLabor, claimedByLabor).forEach((laborType) => {
|
||||
const expected = expectedByLabor[laborType];
|
||||
const claimed = claimedByLabor[laborType];
|
||||
const deltaHours = roundHours((expected?.hours || 0) - (claimed?.hours || 0));
|
||||
|
||||
if (deltaHours === 0) {
|
||||
return;
|
||||
}
|
||||
} else if (diff.op === "update") {
|
||||
//An old ticket amount isn't sufficient
|
||||
//We can't modify the existing ticket, it might already be committed. So let's add a new one instead.
|
||||
|
||||
const effectiveRate = roundCurrency(expected?.rate ?? claimed?.rate);
|
||||
const payoutContext = BuildGeneratedPayoutContext({
|
||||
baseContext:
|
||||
expected?.payoutContext ||
|
||||
claimed?.payoutContext ||
|
||||
buildFallbackPayoutContext({ laborType, rate: effectiveRate }),
|
||||
generatedBy: req.user.email,
|
||||
generatedFrom: "payall",
|
||||
taskName: "Pay All",
|
||||
usedTicketFallback: !expected && Boolean(claimed)
|
||||
});
|
||||
|
||||
ticketsToInsert.push({
|
||||
task_name: "Pay All",
|
||||
jobid: job.id,
|
||||
bodyshopid: job.bodyshop.id,
|
||||
employeeid: path.employeeid,
|
||||
productivehrs: diff.val - diff.oldVal,
|
||||
rate: path.rate,
|
||||
ciecacode: path.mod_lbr_ty,
|
||||
employeeid: employeeId,
|
||||
productivehrs: deltaHours,
|
||||
rate: effectiveRate,
|
||||
ciecacode: laborType,
|
||||
cost_center: job.bodyshop.md_responsibility_centers.defaults.costs[laborType],
|
||||
flat_rate: true,
|
||||
cost_center: job.bodyshop.md_responsibility_centers.defaults.costs[path.mod_lbr_ty],
|
||||
memo: `Adjust flagged hours per assignment. (${req.user.email})`
|
||||
created_by: req.user.email,
|
||||
payout_context: payoutContext,
|
||||
memo: buildPayAllMemo({
|
||||
deltaHours,
|
||||
hasExpected: Boolean(expected),
|
||||
hasClaimed: Boolean(claimed),
|
||||
userEmail: req.user.email
|
||||
})
|
||||
});
|
||||
} else {
|
||||
//Has to be a delete
|
||||
if (typeof diff.oldVal === "object" && Object.keys(diff.oldVal).length > 1) {
|
||||
//Multiple oldValues to add.
|
||||
Object.keys(diff.oldVal).forEach((key) => {
|
||||
ticketsToInsert.push({
|
||||
task_name: "Pay All",
|
||||
jobid: job.id,
|
||||
bodyshopid: job.bodyshop.id,
|
||||
employeeid: path.employeeid,
|
||||
productivehrs: diff.oldVal[key][Object.keys(diff.oldVal[key])[0]] * -1,
|
||||
rate: Object.keys(diff.oldVal[key])[0],
|
||||
ciecacode: key,
|
||||
cost_center: job.bodyshop.md_responsibility_centers.defaults.costs[key],
|
||||
flat_rate: true,
|
||||
memo: `Remove flagged hours per assignment. (${req.user.email})`
|
||||
});
|
||||
});
|
||||
} else {
|
||||
//Only the 1 value to add.
|
||||
ticketsToInsert.push({
|
||||
task_name: "Pay All",
|
||||
jobid: job.id,
|
||||
bodyshopid: job.bodyshop.id,
|
||||
employeeid: path.employeeid,
|
||||
productivehrs: path.hours * -1,
|
||||
rate: path.rate,
|
||||
ciecacode: path.mod_lbr_ty,
|
||||
cost_center: job.bodyshop.md_responsibility_centers.defaults.costs[path.mod_lbr_ty],
|
||||
flat_rate: true,
|
||||
memo: `Remove flagged hours per assignment. (${req.user.email})`
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const insertResult = await client.request(queries.INSERT_TIME_TICKETS, {
|
||||
timetickets: ticketsToInsert.filter((ticket) => ticket.productivehrs !== 0)
|
||||
});
|
||||
const filteredTickets = ticketsToInsert.filter((ticket) => ticket.productivehrs !== 0);
|
||||
|
||||
res.json(ticketsToInsert.filter((ticket) => ticket.productivehrs !== 0));
|
||||
if (filteredTickets.length > 0) {
|
||||
await client.request(queries.INSERT_TIME_TICKETS, {
|
||||
timetickets: filteredTickets
|
||||
});
|
||||
}
|
||||
|
||||
res.json(filteredTickets);
|
||||
} catch (error) {
|
||||
logger.log("job-payroll-labor-totals-error", "ERROR", req.user.email, jobid, {
|
||||
jobid: jobid,
|
||||
jobid,
|
||||
error: JSON.stringify(error)
|
||||
});
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
function diffParser(diff) {
|
||||
const type = typeof diff.oldVal;
|
||||
let mod_lbr_ty, rate, hours;
|
||||
|
||||
if (diff.path.length === 1) {
|
||||
if (diff.op === "add") {
|
||||
mod_lbr_ty = Object.keys(diff.val)[0];
|
||||
rate = Object.keys(diff.val[mod_lbr_ty])[0];
|
||||
// hours = diff.oldVal[mod_lbr_ty][rate];
|
||||
} else {
|
||||
mod_lbr_ty = Object.keys(diff.oldVal)[0];
|
||||
rate = Object.keys(diff.oldVal[mod_lbr_ty])[0];
|
||||
// hours = diff.oldVal[mod_lbr_ty][rate];
|
||||
}
|
||||
} else if (diff.path.length === 2) {
|
||||
mod_lbr_ty = diff.path[1];
|
||||
if (diff.op === "add") {
|
||||
rate = Object.keys(diff.val)[0];
|
||||
} else {
|
||||
rate = Object.keys(diff.oldVal)[0];
|
||||
}
|
||||
} else if (diff.path.length === 3) {
|
||||
mod_lbr_ty = diff.path[1];
|
||||
rate = diff.path[2];
|
||||
//hours = 0;
|
||||
}
|
||||
|
||||
//Set the hours
|
||||
if (typeof diff.val === "number" && diff.val !== null && diff.val !== undefined) {
|
||||
hours = diff.val;
|
||||
} else if (diff.val !== null && diff.val !== undefined) {
|
||||
if (diff.path.length === 1) {
|
||||
hours = diff.val[Object.keys(diff.val)[0]][Object.keys(diff.val[Object.keys(diff.val)[0]])];
|
||||
} else {
|
||||
hours = diff.val[Object.keys(diff.val)[0]];
|
||||
}
|
||||
} else if (typeof diff.oldVal === "number" && diff.oldVal !== null && diff.oldVal !== undefined) {
|
||||
hours = diff.oldVal;
|
||||
} else {
|
||||
hours = diff.oldVal[Object.keys(diff.oldVal)[0]];
|
||||
}
|
||||
|
||||
const ret = {
|
||||
multiVal: false,
|
||||
employeeid: diff.path[0], // Always True
|
||||
mod_lbr_ty,
|
||||
rate,
|
||||
hours
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
function CalculateExpectedHoursForJob(job, filterToLbrTypes) {
|
||||
const assignmentHash = { unassigned: 0 };
|
||||
const employeeHash = {}; // employeeid => Cieca labor type => rate => hours. Contains how many hours each person should be paid.
|
||||
const employeeHash = {}; // employeeid => Cieca labor type => { hours, rate, payoutContext }
|
||||
const laborTypeFilter = Array.isArray(filterToLbrTypes) ? filterToLbrTypes : null;
|
||||
|
||||
job.joblines
|
||||
.filter((jobline) => {
|
||||
if (!filterToLbrTypes) return true;
|
||||
else {
|
||||
return (
|
||||
filterToLbrTypes.includes(jobline.mod_lbr_ty) ||
|
||||
(jobline.convertedtolbr && filterToLbrTypes.includes(jobline.convertedtolbr_data.mod_lbr_ty))
|
||||
);
|
||||
if (!laborTypeFilter) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const convertedLaborType = jobline.convertedtolbr ? jobline.convertedtolbr_data?.mod_lbr_ty : null;
|
||||
return laborTypeFilter.includes(jobline.mod_lbr_ty) || (convertedLaborType && laborTypeFilter.includes(convertedLaborType));
|
||||
})
|
||||
.forEach((jobline) => {
|
||||
if (jobline.convertedtolbr) {
|
||||
// Line has been converte to labor. Temporarily re-assign the hours.
|
||||
jobline.mod_lbr_ty = jobline.convertedtolbr_data.mod_lbr_ty;
|
||||
jobline.mod_lb_hrs += jobline.convertedtolbr_data.mod_lb_hrs;
|
||||
const laborType = jobline.convertedtolbr ? jobline.convertedtolbr_data?.mod_lbr_ty || jobline.mod_lbr_ty : jobline.mod_lbr_ty;
|
||||
const laborHours = roundHours(
|
||||
toNumber(jobline.mod_lb_hrs) + (jobline.convertedtolbr ? toNumber(jobline.convertedtolbr_data?.mod_lb_hrs) : 0)
|
||||
);
|
||||
|
||||
if (laborHours === 0) {
|
||||
return;
|
||||
}
|
||||
if (jobline.mod_lb_hrs != 0) {
|
||||
//Check if the line is assigned. If not, keep track of it as an unassigned line by type.
|
||||
if (jobline.assigned_team === null) {
|
||||
assignmentHash.unassigned = assignmentHash.unassigned + jobline.mod_lb_hrs;
|
||||
} else {
|
||||
//Line is assigned.
|
||||
if (!assignmentHash[jobline.assigned_team]) {
|
||||
assignmentHash[jobline.assigned_team] = 0;
|
||||
}
|
||||
assignmentHash[jobline.assigned_team] = assignmentHash[jobline.assigned_team] + jobline.mod_lb_hrs;
|
||||
|
||||
//Create the assignment breakdown.
|
||||
const theTeam = job.bodyshop.employee_teams.find((team) => team.id === jobline.assigned_team);
|
||||
if (jobline.assigned_team === null) {
|
||||
assignmentHash.unassigned = roundHours(assignmentHash.unassigned + laborHours);
|
||||
return;
|
||||
}
|
||||
|
||||
theTeam.employee_team_members.forEach((tm) => {
|
||||
//Figure out how many hours they are owed at this line, and at what rate.
|
||||
const theTeam = job.bodyshop.employee_teams.find((team) => team.id === jobline.assigned_team);
|
||||
|
||||
if (!employeeHash[tm.employee.id]) {
|
||||
employeeHash[tm.employee.id] = {};
|
||||
}
|
||||
if (!employeeHash[tm.employee.id][jobline.mod_lbr_ty]) {
|
||||
employeeHash[tm.employee.id][jobline.mod_lbr_ty] = {};
|
||||
}
|
||||
if (!employeeHash[tm.employee.id][jobline.mod_lbr_ty][tm.labor_rates[jobline.mod_lbr_ty]]) {
|
||||
employeeHash[tm.employee.id][jobline.mod_lbr_ty][tm.labor_rates[jobline.mod_lbr_ty]] = 0;
|
||||
}
|
||||
if (!theTeam) {
|
||||
assignmentHash.unassigned = roundHours(assignmentHash.unassigned + laborHours);
|
||||
return;
|
||||
}
|
||||
|
||||
const hoursOwed = (tm.percentage * jobline.mod_lb_hrs) / 100;
|
||||
employeeHash[tm.employee.id][jobline.mod_lbr_ty][tm.labor_rates[jobline.mod_lbr_ty]] =
|
||||
employeeHash[tm.employee.id][jobline.mod_lbr_ty][tm.labor_rates[jobline.mod_lbr_ty]] + hoursOwed;
|
||||
});
|
||||
assignmentHash[jobline.assigned_team] = roundHours((assignmentHash[jobline.assigned_team] || 0) + laborHours);
|
||||
|
||||
theTeam.employee_team_members.forEach((teamMember) => {
|
||||
const employeeId = teamMember.employee.id;
|
||||
const { effectiveRate, payoutContext } = BuildPayoutDetails(job, teamMember, laborType);
|
||||
|
||||
if (!employeeHash[employeeId]) {
|
||||
employeeHash[employeeId] = {};
|
||||
}
|
||||
}
|
||||
|
||||
if (!employeeHash[employeeId][laborType]) {
|
||||
employeeHash[employeeId][laborType] = {
|
||||
hours: 0,
|
||||
rate: effectiveRate,
|
||||
payoutContext
|
||||
};
|
||||
}
|
||||
|
||||
const hoursOwed = roundHours((toNumber(teamMember.percentage) * laborHours) / 100);
|
||||
employeeHash[employeeId][laborType].hours = roundHours(employeeHash[employeeId][laborType].hours + hoursOwed);
|
||||
employeeHash[employeeId][laborType].rate = effectiveRate;
|
||||
employeeHash[employeeId][laborType].payoutContext = payoutContext;
|
||||
});
|
||||
});
|
||||
|
||||
return { assignmentHash, employeeHash };
|
||||
}
|
||||
|
||||
function CalculateTicketsHoursForJob(job) {
|
||||
const ticketHash = {}; // employeeid => Cieca labor type => rate => hours.
|
||||
//Calculate how much each employee has been paid so far.
|
||||
const ticketHash = {}; // employeeid => Cieca labor type => { hours, rate, payoutContext }
|
||||
|
||||
job.timetickets.forEach((ticket) => {
|
||||
if (!ticket?.employeeid || !ticket?.ciecacode) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ticketHash[ticket.employeeid]) {
|
||||
ticketHash[ticket.employeeid] = {};
|
||||
}
|
||||
|
||||
if (!ticketHash[ticket.employeeid][ticket.ciecacode]) {
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode] = {};
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode] = {
|
||||
hours: 0,
|
||||
rate: roundCurrency(ticket.rate),
|
||||
payoutContext: ticket.payout_context || null
|
||||
};
|
||||
}
|
||||
if (!ticketHash[ticket.employeeid][ticket.ciecacode][ticket.rate]) {
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode][ticket.rate] = 0;
|
||||
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode].hours = roundHours(
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode].hours + toNumber(ticket.productivehrs)
|
||||
);
|
||||
|
||||
if (ticket.rate !== null && ticket.rate !== undefined) {
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode].rate = roundCurrency(ticket.rate);
|
||||
}
|
||||
|
||||
if (ticket.payout_context) {
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode].payoutContext = ticket.payout_context;
|
||||
}
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode][ticket.rate] =
|
||||
ticketHash[ticket.employeeid][ticket.ciecacode][ticket.rate] + ticket.productivehrs;
|
||||
});
|
||||
|
||||
return ticketHash;
|
||||
}
|
||||
|
||||
exports.BuildPayoutDetails = BuildPayoutDetails;
|
||||
exports.CalculateExpectedHoursForJob = CalculateExpectedHoursForJob;
|
||||
exports.CalculateTicketsHoursForJob = CalculateTicketsHoursForJob;
|
||||
exports.RoundPayrollHours = roundHours;
|
||||
|
||||
Reference in New Issue
Block a user