IO-3178 Flat Rate ATS

Signed-off-by: Allan Carr <allan.carr@thinkimex.com>
This commit is contained in:
Allan Carr
2025-03-24 09:59:41 -07:00
parent 4c6d28f612
commit 8bb86b9caa
14 changed files with 593 additions and 447 deletions

View File

@@ -1,7 +1,5 @@
const Dinero = require("dinero.js");
const queries = require("../graphql-client/queries");
const adminClient = require("../graphql-client/graphql-client").client;
const _ = require("lodash");
const logger = require("../utils/logger");
//****************************************************** */
@@ -44,11 +42,16 @@ exports.totalsSsu = async function (req, res) {
}
});
if (!result) {
throw new Error("Failed to update job totals");
}
res.status(200).send();
} catch (error) {
logger.log("job-totals-ssu-error", "ERROR", req.user.email, id, {
jobid: id,
error
error: error.message,
stack: error.stack
});
res.status(503).send();
}
@@ -57,7 +60,7 @@ exports.totalsSsu = async function (req, res) {
//IMPORTANT*** These two functions MUST be mirrrored.
async function TotalsServerSide(req, res) {
const { job, client } = req.body;
await AutoAddAtsIfRequired({ job: job, client: client });
await AtsAdjustmentsIfRequired({ job: job, client: client, user: req?.user });
try {
let ret = {
@@ -71,7 +74,8 @@ async function TotalsServerSide(req, res) {
} catch (error) {
logger.log("job-totals-ssu-error", "ERROR", req?.user?.email, job.id, {
jobid: job.id,
error
error: error.message,
stack: error.stack
});
res.status(400).send(JSON.stringify(error));
}
@@ -83,13 +87,12 @@ async function Totals(req, res) {
const logger = req.logger;
const client = req.userGraphQLClient;
logger.log("job-totals", "DEBUG", req.user.email, job.id, {
jobid: job.id
logger.log("job-totals-", "DEBUG", req.user.email, job.id, {
jobid: job.id,
id: id
});
logger.log("job-totals-ssu", "DEBUG", req.user.email, id, null);
await AutoAddAtsIfRequired({ job, client });
await AtsAdjustmentsIfRequired({ job, client, user: req.user });
try {
let ret = {
@@ -103,46 +106,52 @@ async function Totals(req, res) {
} catch (error) {
logger.log("job-totals-error", "ERROR", req.user.email, job.id, {
jobid: job.id,
error
error: error.message,
stack: error.stack
});
res.status(400).send(JSON.stringify(error));
}
}
async function AutoAddAtsIfRequired({ job, client }) {
//Check if ATS should be automatically added.
if (job.auto_add_ats) {
//Get the total sum of hours that should be the ATS amount.
//Check to see if an ATS line exists.
async function AtsAdjustmentsIfRequired({ job, client, user }) {
if (job.auto_add_ats || job.flat_rate_ats) {
let atsAmount = 0;
let atsLineIndex = null;
const atsHours = job.joblines.reduce((acc, val, index) => {
if (val.line_desc && val.line_desc.toLowerCase() === "ats amount") {
atsLineIndex = index;
}
if (
val.mod_lbr_ty !== "LA1" &&
val.mod_lbr_ty !== "LA2" &&
val.mod_lbr_ty !== "LA3" &&
val.mod_lbr_ty !== "LA4" &&
val.mod_lbr_ty !== "LAU" &&
val.mod_lbr_ty !== "LAG" &&
val.mod_lbr_ty !== "LAS" &&
val.mod_lbr_ty !== "LAA"
) {
acc = acc + val.mod_lb_hrs;
}
//Check if ATS should be automatically added.
if (job.auto_add_ats) {
const excludedLaborTypes = new Set(["LAA", "LAG", "LAS", "LAU", "LA1", "LA2", "LA3", "LA4"]);
return acc;
}, 0);
//Get the total sum of hours that should be the ATS amount.
//Check to see if an ATS line exists.
const atsHours = job.joblines.reduce((acc, val, index) => {
if (val.line_desc?.toLowerCase() === "ats amount") {
atsLineIndex = index;
}
const atsAmount = atsHours * (job.rate_ats || 0);
//If it does, update it in place, and make sure it is updated for local calculations.
if (!excludedLaborTypes.has(val.mod_lbr_ty)) {
acc = acc + val.mod_lb_hrs;
}
return acc;
}, 0);
atsAmount = atsHours * (job.rate_ats || 0);
}
//Check if a Flat Rate ATS should be added.
if (job.flat_rate_ats) {
atsLineIndex = ((i) => (i === -1 ? null : i))(
job.joblines.findIndex((line) => line.line_desc?.toLowerCase() === "ats amount")
);
atsAmount = job.rate_ats_flat || 0;
}
//If it does not, create one for local calculations and insert it.
if (atsLineIndex === null) {
const newAtsLine = {
jobid: job.id,
alt_partm: null,
line_no: 35,
unq_seq: 0,
line_ind: "E",
line_desc: "ATS Amount",
@@ -167,22 +176,41 @@ async function AutoAddAtsIfRequired({ job, client }) {
prt_dsmk_m: 0.0
};
const result = await client.request(queries.INSERT_NEW_JOB_LINE, {
lineInput: [newAtsLine]
});
try {
const result = await client.request(queries.INSERT_NEW_JOB_LINE, {
lineInput: [newAtsLine]
});
job.joblines.push(newAtsLine);
if (result) {
job.joblines.push(newAtsLine);
}
} catch (error) {
logger.log("job-totals-ssu-ats-error", "ERROR", user?.email, job.id, {
jobid: job.id,
error: error.message,
stack: error.stack
});
}
}
//If it does not, create one for local calculations and insert it.
//If it does, update it in place, and make sure it is updated for local calculations.
else {
const result = await client.request(queries.UPDATE_JOB_LINE, {
line: { act_price: atsAmount },
lineId: job.joblines[atsLineIndex].id
});
job.joblines[atsLineIndex].act_price = atsAmount;
try {
const result = await client.request(queries.UPDATE_JOB_LINE, {
line: { act_price: atsAmount },
lineId: job.joblines[atsLineIndex].id
});
if (result) {
job.joblines[atsLineIndex].act_price = atsAmount;
}
} catch (error) {
logger.log("job-totals-ssu-ats-error", "ERROR", user?.email, job.id, {
jobid: job.id,
atsLineIndex: atsLineIndex,
error: error.message,
stack: error.stack
});
}
}
//console.log(job.jobLines);
}
}