@@ -3,8 +3,8 @@ const queries = require("../graphql-client/queries");
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
const logger = require("../utils/logger");
|
||||
const {
|
||||
CalculateExpectedHoursForJob,
|
||||
CalculateTicketsHoursForJob,
|
||||
CalculateExpectedHoursForJob,
|
||||
CalculateTicketsHoursForJob,
|
||||
} = require("./pay-all");
|
||||
|
||||
// Dinero.defaultCurrency = "USD";
|
||||
@@ -12,116 +12,116 @@ const {
|
||||
Dinero.globalRoundingMode = "HALF_EVEN";
|
||||
|
||||
exports.calculatelabor = async function (req, res) {
|
||||
const { jobid, calculateOnly } = req.body;
|
||||
logger.log("job-payroll-calculate-labor", "DEBUG", req.user.email, jobid, null);
|
||||
const {jobid, calculateOnly} = req.body;
|
||||
logger.log("job-payroll-calculate-labor", "DEBUG", req.user.email, jobid, null);
|
||||
|
||||
const BearerToken = req.BearerToken;
|
||||
const client = req.userGraphQLClient;
|
||||
|
||||
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 { employeeHash, assignmentHash } = CalculateExpectedHoursForJob(job);
|
||||
const ticketHash = CalculateTicketsHoursForJob(job);
|
||||
|
||||
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,
|
||||
try {
|
||||
const {jobs_by_pk: job} = await client
|
||||
.setHeaders({Authorization: BearerToken})
|
||||
.request(queries.QUERY_JOB_PAYROLL_DATA, {
|
||||
id: jobid,
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
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];
|
||||
}
|
||||
//iterate over each ticket, building a hash of team -> employee to calculate total assigned hours.
|
||||
const {employeeHash, assignmentHash} = CalculateExpectedHoursForJob(job);
|
||||
const ticketHash = CalculateTicketsHoursForJob(job);
|
||||
|
||||
totals.push({
|
||||
employeeid: employeeIdKey,
|
||||
rate: rateKey,
|
||||
mod_lbr_ty: laborTypeKey,
|
||||
expectedHours,
|
||||
claimedHours: claimedHours || 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,
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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-calculate-labor-error",
|
||||
"ERROR",
|
||||
req.user.email,
|
||||
jobid,
|
||||
{
|
||||
jobid: jobid,
|
||||
error,
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
if (assignmentHash.unassigned > 0) {
|
||||
totals.push({
|
||||
employeeid: undefined,
|
||||
//rate: rateKey,
|
||||
//mod_lbr_ty: laborTypeKey,
|
||||
expectedHours: assignmentHash.unassigned,
|
||||
claimedHours: 0,
|
||||
});
|
||||
res.status(503).send();
|
||||
}
|
||||
res.json(totals);
|
||||
//res.json(assignmentHash);
|
||||
} catch (error) {
|
||||
logger.log(
|
||||
"job-payroll-calculate-labor-error",
|
||||
"ERROR",
|
||||
req.user.email,
|
||||
jobid,
|
||||
{
|
||||
jobid: jobid,
|
||||
error,
|
||||
}
|
||||
);
|
||||
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);
|
||||
return key.split(".").reduce(function (o, x) {
|
||||
return typeof o == "undefined" || o === null ? o : o[x];
|
||||
}, obj);
|
||||
};
|
||||
|
||||
@@ -3,8 +3,8 @@ const queries = require("../graphql-client/queries");
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
const logger = require("../utils/logger");
|
||||
const {
|
||||
CalculateExpectedHoursForJob,
|
||||
CalculateTicketsHoursForJob,
|
||||
CalculateExpectedHoursForJob,
|
||||
CalculateTicketsHoursForJob,
|
||||
} = require("./pay-all");
|
||||
const moment = require("moment");
|
||||
// Dinero.defaultCurrency = "USD";
|
||||
@@ -12,96 +12,96 @@ const moment = require("moment");
|
||||
Dinero.globalRoundingMode = "HALF_EVEN";
|
||||
|
||||
exports.claimtask = async function (req, res) {
|
||||
const { jobid, task, calculateOnly, employee } = req.body;
|
||||
logger.log("job-payroll-pay-all", "DEBUG", req.user.email, jobid, null);
|
||||
const {jobid, task, calculateOnly, employee} = req.body;
|
||||
logger.log("job-payroll-pay-all", "DEBUG", req.user.email, jobid, null);
|
||||
|
||||
const BearerToken = req.BearerToken;
|
||||
const client = req.userGraphQLClient;
|
||||
const BearerToken = req.BearerToken;
|
||||
const client = req.userGraphQLClient;
|
||||
|
||||
try {
|
||||
const { jobs_by_pk: job } = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QUERY_JOB_PAYROLL_DATA, {
|
||||
id: jobid,
|
||||
});
|
||||
|
||||
const theTaskPreset = job.bodyshop.md_tasks_presets.presets.find(
|
||||
(tp) => tp.name === task
|
||||
);
|
||||
if (!theTaskPreset) {
|
||||
res
|
||||
.status(400)
|
||||
.json({ success: false, error: "Provided task preset not found." });
|
||||
return;
|
||||
}
|
||||
|
||||
//Get all of the assignments that are filtered.
|
||||
const { assignmentHash, employeeHash } = CalculateExpectedHoursForJob(
|
||||
job,
|
||||
theTaskPreset.hourstype
|
||||
);
|
||||
const ticketsToInsert = [];
|
||||
//Then add them in based on a percentage to each employee.
|
||||
|
||||
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] *
|
||||
(theTaskPreset.percent / 100);
|
||||
|
||||
ticketsToInsert.push({
|
||||
task_name: task,
|
||||
jobid: job.id,
|
||||
bodyshopid: job.bodyshop.id,
|
||||
employeeid: employeeIdKey,
|
||||
productivehrs: expectedHours,
|
||||
rate: rateKey,
|
||||
ciecacode: laborTypeKey,
|
||||
flat_rate: true,
|
||||
cost_center:
|
||||
job.bodyshop.md_responsibility_centers.defaults.costs[
|
||||
laborTypeKey
|
||||
],
|
||||
memo: `*Flagged Task* ${theTaskPreset.memo}`,
|
||||
try {
|
||||
const {jobs_by_pk: job} = await client
|
||||
.setHeaders({Authorization: BearerToken})
|
||||
.request(queries.QUERY_JOB_PAYROLL_DATA, {
|
||||
id: jobid,
|
||||
});
|
||||
}
|
||||
|
||||
const theTaskPreset = job.bodyshop.md_tasks_presets.presets.find(
|
||||
(tp) => tp.name === task
|
||||
);
|
||||
});
|
||||
});
|
||||
if (!calculateOnly) {
|
||||
//Insert the time ticekts if we're not just calculating them.
|
||||
const insertResult = await client.request(queries.INSERT_TIME_TICKETS, {
|
||||
timetickets: ticketsToInsert.filter(
|
||||
(ticket) => ticket.productivehrs !== 0
|
||||
),
|
||||
});
|
||||
const updateResult = await client.request(queries.UPDATE_JOB, {
|
||||
jobId: job.id,
|
||||
job: {
|
||||
status: theTaskPreset.nextstatus,
|
||||
completed_tasks: [
|
||||
...job.completed_tasks,
|
||||
{
|
||||
name: task,
|
||||
completedat: moment(),
|
||||
completed_by: employee,
|
||||
useremail: req.user.email,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
if (!theTaskPreset) {
|
||||
res
|
||||
.status(400)
|
||||
.json({success: false, error: "Provided task preset not found."});
|
||||
return;
|
||||
}
|
||||
|
||||
//Get all of the assignments that are filtered.
|
||||
const {assignmentHash, employeeHash} = CalculateExpectedHoursForJob(
|
||||
job,
|
||||
theTaskPreset.hourstype
|
||||
);
|
||||
const ticketsToInsert = [];
|
||||
//Then add them in based on a percentage to each employee.
|
||||
|
||||
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] *
|
||||
(theTaskPreset.percent / 100);
|
||||
|
||||
ticketsToInsert.push({
|
||||
task_name: task,
|
||||
jobid: job.id,
|
||||
bodyshopid: job.bodyshop.id,
|
||||
employeeid: employeeIdKey,
|
||||
productivehrs: expectedHours,
|
||||
rate: rateKey,
|
||||
ciecacode: laborTypeKey,
|
||||
flat_rate: true,
|
||||
cost_center:
|
||||
job.bodyshop.md_responsibility_centers.defaults.costs[
|
||||
laborTypeKey
|
||||
],
|
||||
memo: `*Flagged Task* ${theTaskPreset.memo}`,
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
if (!calculateOnly) {
|
||||
//Insert the time ticekts if we're not just calculating them.
|
||||
const insertResult = await client.request(queries.INSERT_TIME_TICKETS, {
|
||||
timetickets: ticketsToInsert.filter(
|
||||
(ticket) => ticket.productivehrs !== 0
|
||||
),
|
||||
});
|
||||
const updateResult = await client.request(queries.UPDATE_JOB, {
|
||||
jobId: job.id,
|
||||
job: {
|
||||
status: theTaskPreset.nextstatus,
|
||||
completed_tasks: [
|
||||
...job.completed_tasks,
|
||||
{
|
||||
name: task,
|
||||
completedat: moment(),
|
||||
completed_by: employee,
|
||||
useremail: req.user.email,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
res.json({unassignedHours: assignmentHash.unassigned, ticketsToInsert});
|
||||
} catch (error) {
|
||||
logger.log("job-payroll-claim-task-error", "ERROR", req.user.email, jobid, {
|
||||
jobid: jobid,
|
||||
error,
|
||||
});
|
||||
res.status(503).send();
|
||||
}
|
||||
res.json({ unassignedHours: assignmentHash.unassigned, ticketsToInsert });
|
||||
} catch (error) {
|
||||
logger.log("job-payroll-claim-task-error", "ERROR", req.user.email, jobid, {
|
||||
jobid: jobid,
|
||||
error,
|
||||
});
|
||||
res.status(503).send();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -5,321 +5,321 @@ const _ = require("lodash");
|
||||
const rdiff = require("recursive-diff");
|
||||
|
||||
const logger = require("../utils/logger");
|
||||
const { json } = require("body-parser");
|
||||
const {json} = require("body-parser");
|
||||
// Dinero.defaultCurrency = "USD";
|
||||
// Dinero.globalLocale = "en-CA";
|
||||
Dinero.globalRoundingMode = "HALF_EVEN";
|
||||
|
||||
exports.payall = async function (req, res) {
|
||||
const { jobid, calculateOnly } = req.body;
|
||||
logger.log("job-payroll-pay-all", "DEBUG", req.user.email, jobid, null);
|
||||
const {jobid, calculateOnly} = req.body;
|
||||
logger.log("job-payroll-pay-all", "DEBUG", req.user.email, jobid, null);
|
||||
|
||||
const BearerToken = req.BearerToken;
|
||||
const client = req.userGraphQLClient;
|
||||
const BearerToken = req.BearerToken;
|
||||
const client = req.userGraphQLClient;
|
||||
|
||||
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 { 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 = [];
|
||||
|
||||
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") {
|
||||
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})`,
|
||||
try {
|
||||
const {jobs_by_pk: job} = await client
|
||||
.setHeaders({Authorization: BearerToken})
|
||||
.request(queries.QUERY_JOB_PAYROLL_DATA, {
|
||||
id: jobid,
|
||||
});
|
||||
});
|
||||
} 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})`,
|
||||
});
|
||||
|
||||
//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;
|
||||
}
|
||||
} 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.
|
||||
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,
|
||||
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})`,
|
||||
|
||||
//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 = [];
|
||||
|
||||
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") {
|
||||
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})`,
|
||||
});
|
||||
}
|
||||
} 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.
|
||||
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,
|
||||
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})`,
|
||||
});
|
||||
} 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})`,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
} 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 insertResult = await client.request(queries.INSERT_TIME_TICKETS, {
|
||||
timetickets: ticketsToInsert.filter(
|
||||
(ticket) => ticket.productivehrs !== 0
|
||||
),
|
||||
});
|
||||
|
||||
res.json(ticketsToInsert.filter((ticket) => ticket.productivehrs !== 0));
|
||||
} catch (error) {
|
||||
logger.log(
|
||||
"job-payroll-labor-totals-error",
|
||||
"ERROR",
|
||||
req.user.email,
|
||||
jobid,
|
||||
{
|
||||
jobid: jobid,
|
||||
error: JSON.stringify(error),
|
||||
}
|
||||
);
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
res.json(ticketsToInsert.filter((ticket) => ticket.productivehrs !== 0));
|
||||
} catch (error) {
|
||||
logger.log(
|
||||
"job-payroll-labor-totals-error",
|
||||
"ERROR",
|
||||
req.user.email,
|
||||
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;
|
||||
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]];
|
||||
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;
|
||||
}
|
||||
} 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;
|
||||
//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.
|
||||
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))
|
||||
);
|
||||
}
|
||||
})
|
||||
.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;
|
||||
}
|
||||
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] = {};
|
||||
const assignmentHash = {unassigned: 0};
|
||||
const employeeHash = {}; // employeeid => Cieca labor type => rate => hours. Contains how many hours each person should be paid.
|
||||
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 (!employeeHash[tm.employee.id][jobline.mod_lbr_ty]) {
|
||||
employeeHash[tm.employee.id][jobline.mod_lbr_ty] = {};
|
||||
})
|
||||
.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;
|
||||
}
|
||||
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 (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 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return { assignmentHash, employeeHash };
|
||||
return {assignmentHash, employeeHash};
|
||||
}
|
||||
|
||||
function CalculateTicketsHoursForJob(job) {
|
||||
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;
|
||||
});
|
||||
return ticketHash;
|
||||
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;
|
||||
});
|
||||
return ticketHash;
|
||||
}
|
||||
|
||||
exports.CalculateExpectedHoursForJob = CalculateExpectedHoursForJob;
|
||||
|
||||
Reference in New Issue
Block a user