Establish working version of pay all and labor calculations.
This commit is contained in:
@@ -2,14 +2,19 @@ const Dinero = require("dinero.js");
|
||||
const queries = require("../graphql-client/queries");
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
const logger = require("../utils/logger");
|
||||
const {
|
||||
CalculateExpectedHoursForJob,
|
||||
CalculateTicketsHoursForJob,
|
||||
} = require("./pay-all");
|
||||
|
||||
// Dinero.defaultCurrency = "USD";
|
||||
// Dinero.globalLocale = "en-CA";
|
||||
Dinero.globalRoundingMode = "HALF_EVEN";
|
||||
|
||||
exports.calculateLaborTotals = async function (req, res) {
|
||||
exports.calculatelabor = async function (req, res) {
|
||||
const BearerToken = req.headers.authorization;
|
||||
const { jobid } = req.body;
|
||||
logger.log("job-payroll-labor-totals", "DEBUG", req.user.email, jobid, null);
|
||||
const { jobid, calculateOnly } = 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,
|
||||
@@ -24,28 +29,88 @@ exports.calculateLaborTotals = async function (req, res) {
|
||||
});
|
||||
|
||||
//iterate over each ticket, building a hash of team -> employee to calculate total assigned hours.
|
||||
const { employeeHash, assignmentHash } = CalculateExpectedHoursForJob(job);
|
||||
const ticketHash = CalculateTicketsHoursForJob(job);
|
||||
|
||||
const assignmentHash = { unassigned: 0 };
|
||||
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[jobline.mod_lbr_ty] =
|
||||
assignmentHash.unassigned[jobline.mod_lbr_ty] + jobline.mod_lb_hrs;
|
||||
} else {
|
||||
//Line is assigned.
|
||||
if (!assignmentHash[jobline.assigned_team]) {
|
||||
assignmentHash[jobline.assigned_team] = 0;
|
||||
const totals = [];
|
||||
|
||||
//Iteratively go through all 4 levels of the object and create an array that can be presented.
|
||||
// use the employee hash as the golden record (i.e. what they should have), and add what they've claimed.
|
||||
//While going through, delete items from ticket hash.
|
||||
//Anything left in ticket hash is an extra entered item.
|
||||
|
||||
Object.keys(employeeHash).forEach((employeeIdKey) => {
|
||||
//At the employee level.
|
||||
Object.keys(employeeHash[employeeIdKey]).forEach((laborTypeKey) => {
|
||||
//At the labor level
|
||||
Object.keys(employeeHash[employeeIdKey][laborTypeKey]).forEach(
|
||||
(rateKey) => {
|
||||
//At the rate level.
|
||||
const expectedHours =
|
||||
employeeHash[employeeIdKey][laborTypeKey][rateKey];
|
||||
//Will the following line fail? Probably if it doesn't exist.
|
||||
const claimedHours = get(
|
||||
ticketHash,
|
||||
`${employeeIdKey}.${laborTypeKey}.${rateKey}`
|
||||
);
|
||||
if (claimedHours) {
|
||||
delete ticketHash[employeeIdKey][laborTypeKey][rateKey];
|
||||
}
|
||||
|
||||
totals.push({
|
||||
employeeid: employeeIdKey,
|
||||
rate: rateKey,
|
||||
mod_lbr_ty: laborTypeKey,
|
||||
expectedHours,
|
||||
claimedHours: claimedHours || 0,
|
||||
});
|
||||
}
|
||||
assignmentHash[jobline.assigned_team] =
|
||||
assignmentHash[jobline.assigned_team] + jobline.mod_lb_hrs;
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
res.json(assignmentHash);
|
||||
|
||||
Object.keys(ticketHash).forEach((employeeIdKey) => {
|
||||
//At the employee level.
|
||||
Object.keys(ticketHash[employeeIdKey]).forEach((laborTypeKey) => {
|
||||
//At the labor level
|
||||
Object.keys(ticketHash[employeeIdKey][laborTypeKey]).forEach(
|
||||
(rateKey) => {
|
||||
//At the rate level.
|
||||
const expectedHours = 0;
|
||||
//Will the following line fail? Probably if it doesn't exist.
|
||||
const claimedHours = get(
|
||||
ticketHash,
|
||||
`${employeeIdKey}.${laborTypeKey}.${rateKey}`
|
||||
);
|
||||
if (claimedHours) {
|
||||
delete ticketHash[employeeIdKey][laborTypeKey][rateKey];
|
||||
}
|
||||
|
||||
totals.push({
|
||||
employeeid: employeeIdKey,
|
||||
rate: rateKey,
|
||||
mod_lbr_ty: laborTypeKey,
|
||||
expectedHours,
|
||||
claimedHours: claimedHours || 0,
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
if (assignmentHash.unassigned > 0) {
|
||||
totals.push({
|
||||
employeeid: undefined,
|
||||
//rate: rateKey,
|
||||
//mod_lbr_ty: laborTypeKey,
|
||||
expectedHours: assignmentHash.unassigned,
|
||||
claimedHours: 0,
|
||||
});
|
||||
}
|
||||
res.json(totals);
|
||||
//res.json(assignmentHash);
|
||||
} catch (error) {
|
||||
logger.log(
|
||||
"job-payroll-labor-totals-error",
|
||||
"job-payroll-calculate-labor-error",
|
||||
"ERROR",
|
||||
req.user.email,
|
||||
jobid,
|
||||
@@ -57,3 +122,9 @@ exports.calculateLaborTotals = async function (req, res) {
|
||||
res.status(503).send();
|
||||
}
|
||||
};
|
||||
|
||||
get = function (obj, key) {
|
||||
return key.split(".").reduce(function (o, x) {
|
||||
return typeof o == "undefined" || o === null ? o : o[x];
|
||||
}, obj);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user