326 lines
11 KiB
JavaScript
326 lines
11 KiB
JavaScript
const Dinero = require("dinero.js");
|
|
const queries = require("../graphql-client/queries");
|
|
const GraphQLClient = require("graphql-request").GraphQLClient;
|
|
const _ = require("lodash");
|
|
const rdiff = require("recursive-diff");
|
|
|
|
const logger = require("../utils/logger");
|
|
// Dinero.defaultCurrency = "USD";
|
|
// Dinero.globalLocale = "en-CA";
|
|
Dinero.globalRoundingMode = "HALF_EVEN";
|
|
|
|
exports.payall = async function (req, res) {
|
|
const BearerToken = req.headers.authorization;
|
|
const { jobid } = req.body;
|
|
logger.log("job-payroll-pay-all", "DEBUG", req.user.email, jobid, null);
|
|
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
|
headers: {
|
|
Authorization: BearerToken,
|
|
},
|
|
});
|
|
|
|
try {
|
|
const { jobs_by_pk: job } = await client
|
|
.setHeaders({ Authorization: BearerToken })
|
|
.request(queries.QUERY_JOB_PAYROLL_DATA, {
|
|
id: jobid,
|
|
});
|
|
|
|
//iterate over each ticket, building a hash of team -> employee to calculate total assigned hours.
|
|
|
|
const assignmentHash = { unassigned: 0 };
|
|
const employeeHash = {}; // employeeid => Cieca labor type => rate => hours. Contains how many hours each person should be paid.
|
|
job.joblines.forEach((jobline) => {
|
|
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
|
|
);
|
|
|
|
theTeam.employee_team_members.forEach((tm) => {
|
|
//Figure out how many hours they are owed at this line, and at what rate.
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
const ticketHash = {}; // employeeid => Cieca labor type => rate => hours.
|
|
//Calculate how much each employee has been paid so far.
|
|
job.timetickets.forEach((ticket) => {
|
|
if (!ticketHash[ticket.employeeid]) {
|
|
ticketHash[ticket.employeeid] = {};
|
|
}
|
|
if (!ticketHash[ticket.employeeid][ticket.ciecacode]) {
|
|
ticketHash[ticket.employeeid][ticket.ciecacode] = {};
|
|
}
|
|
if (!ticketHash[ticket.employeeid][ticket.ciecacode][ticket.rate]) {
|
|
ticketHash[ticket.employeeid][ticket.ciecacode][ticket.rate] = 0;
|
|
}
|
|
ticketHash[ticket.employeeid][ticket.ciecacode][ticket.rate] =
|
|
ticketHash[ticket.employeeid][ticket.ciecacode][ticket.rate] +
|
|
ticket.productivehrs;
|
|
//Add the rate
|
|
});
|
|
|
|
if (assignmentHash.unassigned > 0) {
|
|
res.json({ success: false, error: "Unassigned hours." });
|
|
return;
|
|
}
|
|
|
|
//Calculate how much time each tech should have by labor type.
|
|
// const comparison = compare(employeeHash, ticketHash);
|
|
const recursiveDiff = rdiff.getDiff(ticketHash, employeeHash, true); //Doing this order creates a diff of changes on the ticket hash to make it the same as the employee hash.
|
|
|
|
const ticketsToInsert = [];
|
|
|
|
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);
|
|
|
|
if (diff.op === "add") {
|
|
if (typeof diff.val === "object") {
|
|
//Multiple values to add.
|
|
Object.keys(diff.val).forEach((key) => {
|
|
ticketsToInsert.push({
|
|
jobid: job.id,
|
|
employeeid: path.employeeid,
|
|
productivehrs: diff.val[key][Object.keys(diff.val[key])[0]],
|
|
rate: Object.keys(diff.val[key])[0],
|
|
ciecacode: key,
|
|
flat_rate: true,
|
|
memo: "Add hours.",
|
|
});
|
|
});
|
|
} else {
|
|
//Only the 1 value to add.
|
|
ticketsToInsert.push({
|
|
jobid: job.id,
|
|
employeeid: path.employeeid,
|
|
productivehrs: path.hours,
|
|
rate: path.rate,
|
|
ciecacode: path.mod_lbr_ty,
|
|
flat_rate: true,
|
|
memo: "Add hours.",
|
|
});
|
|
}
|
|
} else if (diff.op === "update") {
|
|
} else {
|
|
//Has to be a delete
|
|
ticketsToInsert.push({
|
|
jobid: job.id,
|
|
employeeid: path.employeeid,
|
|
productivehrs: diff.oldVal * -1,
|
|
rate: path.rate,
|
|
ciecacode: path.mod_lbr_ty,
|
|
flat_rate: true,
|
|
memo: "Remove unneeded hours. (Rate/Unassigned).",
|
|
});
|
|
}
|
|
});
|
|
|
|
// //Check the ones that are different first. Source of truth will be the employee hash.
|
|
// comparison.different.forEach((differentKey) => {
|
|
// const empVal = fetchFromObject(employeeHash, differentKey) || 0;
|
|
// const ticketVal = fetchFromObject(ticketHash, differentKey) || 0;
|
|
// const splitKey = splitEmployeeKey(differentKey);
|
|
|
|
// ticketsToInsert.push({
|
|
// jobid: job.id,
|
|
// employeeid: splitKey.employeeid,
|
|
// productivehrs: empVal - ticketVal,
|
|
// rate: splitKey.rate,
|
|
// ciecacode: splitKey.mod_lbr_ty,
|
|
// flat_rate: true,
|
|
// memo: "Adjustment between expected and entered values.",
|
|
// });
|
|
// });
|
|
|
|
// comparison.missing_from_first //Missing from the employee hash, but present in the ticket hash.
|
|
// .filter((differentKey) => differentKey.split(".").length == 3)
|
|
// .forEach((differentKey) => {
|
|
// const empVal = fetchFromObject(employeeHash, differentKey) || 0;
|
|
// const ticketVal = fetchFromObject(ticketHash, differentKey) || 0;
|
|
// const splitKey = splitEmployeeKey(differentKey);
|
|
|
|
// ticketsToInsert.push({
|
|
// jobid: job.id,
|
|
// employeeid: differentKey.split(".")[0],
|
|
// productivehrs: empVal - ticketVal * -1,
|
|
// rate: splitKey.rate,
|
|
// ciecacode: splitKey.mod_lbr_ty,
|
|
// flat_rate: true,
|
|
// memo: "Entered ticket reversed to match system payroll.",
|
|
// });
|
|
// });
|
|
// comparison.missing_from_second //Missing from the ticket hash, but present in the employee hash.
|
|
// .filter((differentKey) => differentKey.split(".").length == 3)
|
|
// .forEach((differentKey) => {
|
|
// const empVal = fetchFromObject(employeeHash, differentKey) || 0;
|
|
// const ticketVal = fetchFromObject(ticketHash, differentKey) || 0;
|
|
// const splitKey = splitEmployeeKey(differentKey);
|
|
|
|
// ticketsToInsert.push({
|
|
// jobid: job.id,
|
|
// employeeid: differentKey.split(".")[0],
|
|
// productivehrs: empVal - ticketVal * -1,
|
|
// rate: splitKey.rate,
|
|
// ciecacode: splitKey.mod_lbr_ty,
|
|
// flat_rate: true,
|
|
// memo: "Entered ticket reversed to match system payroll.",
|
|
// });
|
|
// });
|
|
|
|
res.json(ticketsToInsert);
|
|
} catch (error) {
|
|
logger.log(
|
|
"job-payroll-labor-totals-error",
|
|
"ERROR",
|
|
req.user.email,
|
|
jobid,
|
|
{
|
|
jobid: jobid,
|
|
error,
|
|
}
|
|
);
|
|
res.status(503).send();
|
|
}
|
|
};
|
|
|
|
// var compare = function (a, b) {
|
|
// var result = {
|
|
// different: [],
|
|
// missing_from_first: [],
|
|
// missing_from_second: [],
|
|
// };
|
|
|
|
// _.reduce(
|
|
// a,
|
|
// function (result, value, key) {
|
|
// if (b.hasOwnProperty(key)) {
|
|
// if (_.isEqual(value, b[key])) {
|
|
// return result;
|
|
// } else {
|
|
// if (typeof a[key] != typeof {} || typeof b[key] != typeof {}) {
|
|
// //dead end.
|
|
// result.different.push(key);
|
|
// return result;
|
|
// } else {
|
|
// var deeper = compare(a[key], b[key]);
|
|
// result.different = result.different.concat(
|
|
// _.map(deeper.different, (sub_path) => {
|
|
// return key + "." + sub_path;
|
|
// })
|
|
// );
|
|
|
|
// result.missing_from_second = result.missing_from_second.concat(
|
|
// _.map(deeper.missing_from_second, (sub_path) => {
|
|
// return key + "." + sub_path;
|
|
// })
|
|
// );
|
|
|
|
// result.missing_from_first = result.missing_from_first.concat(
|
|
// _.map(deeper.missing_from_first, (sub_path) => {
|
|
// return key + "." + sub_path;
|
|
// })
|
|
// );
|
|
// return result;
|
|
// }
|
|
// }
|
|
// } else {
|
|
// result.missing_from_second.push(key);
|
|
// return result;
|
|
// }
|
|
// },
|
|
// result
|
|
// );
|
|
|
|
// _.reduce(
|
|
// b,
|
|
// function (result, value, key) {
|
|
// if (a.hasOwnProperty(key)) {
|
|
// return result;
|
|
// } else {
|
|
// result.missing_from_first.push(key);
|
|
// return result;
|
|
// }
|
|
// },
|
|
// result
|
|
// );
|
|
|
|
// return result;
|
|
// };
|
|
|
|
// function fetchFromObject(obj, prop) {
|
|
// if (typeof obj === "undefined") {
|
|
// return false;
|
|
// }
|
|
|
|
// var _index = prop.indexOf(".");
|
|
// if (_index > -1) {
|
|
// return fetchFromObject(
|
|
// obj[prop.substring(0, _index)],
|
|
// prop.substr(_index + 1)
|
|
// );
|
|
// }
|
|
|
|
// return obj[prop];
|
|
// }
|
|
|
|
// function splitEmployeeKey(key) {
|
|
// const splitArray = differentKey.split(".");
|
|
// // employeeid => Cieca labor type => rate
|
|
// return {
|
|
// employeeid: splitArray[0],
|
|
// mod_lbr_ty: splitArray[1],
|
|
// rate: splitArray[2],
|
|
// hours: splitArray[3],
|
|
// };
|
|
// }
|
|
|
|
function diffParser(diff) {
|
|
return {
|
|
employeeid: diff.path[0],
|
|
mod_lbr_ty: diff.path[1],
|
|
rate: diff.path[2],
|
|
hours: diff.val,
|
|
};
|
|
}
|