102 lines
3.2 KiB
JavaScript
102 lines
3.2 KiB
JavaScript
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.claimtask = async function (req, res) {
|
|
const BearerToken = req.headers.authorization;
|
|
const { jobid, task, 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,
|
|
},
|
|
});
|
|
|
|
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: `*Claimed 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: {
|
|
completed_tasks: [...job.completed_tasks, task],
|
|
},
|
|
});
|
|
}
|
|
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();
|
|
}
|
|
};
|