IO-1822 Add manual ATS calculation.

This commit is contained in:
Patrick Fic
2022-04-13 12:34:31 -07:00
parent 44ff032acc
commit 77ed64969e
8 changed files with 209 additions and 3 deletions

View File

@@ -24,7 +24,7 @@ exports.totalsSsu = async function (req, res) {
});
const newTotals = await TotalsServerSide(
{ body: { job: job.jobs_by_pk } },
{ body: { job: job.jobs_by_pk, client: client } },
res,
true
);
@@ -53,7 +53,9 @@ exports.totalsSsu = async function (req, res) {
//IMPORTANT*** These two functions MUST be mirrrored.
async function TotalsServerSide(req, res) {
const { job } = req.body;
const { job, client } = req.body;
await AutoAddAtsIfRequired({ job: job, client: client });
try {
let ret = {
parts: CalculatePartsTotals(job.joblines),
@@ -78,6 +80,16 @@ async function Totals(req, res) {
jobid: job.id,
});
const BearerToken = req.headers.authorization;
const { id } = req.body;
logger.log("job-totals-ssu", "DEBUG", req.user.email, id, null);
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
headers: {
Authorization: BearerToken,
},
});
await AutoAddAtsIfRequired({ job, client });
try {
let ret = {
parts: CalculatePartsTotals(job.joblines),
@@ -96,6 +108,83 @@ async function Totals(req, res) {
}
}
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.
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;
}
return acc;
}, 0);
const atsAmount = atsHours * (job.rate_ats || 0);
//If it does, update it in place, and make sure it is updated for local calculations.
if (atsLineIndex === null) {
const newAtsLine = {
jobid: job.id,
alt_partm: null,
line_no: 35,
unq_seq: 0,
line_ind: "E",
line_desc: "ATS Amount",
line_ref: 0.0,
part_type: null,
oem_partno: null,
db_price: 0.0,
act_price: atsAmount,
part_qty: 1,
mod_lbr_ty: null,
db_hrs: 0.0,
mod_lb_hrs: 0.0,
lbr_op: "OP13",
lbr_amt: 0.0,
op_code_desc: "ADDITIONAL COSTS",
status: null,
location: null,
tax_part: true,
db_ref: null,
manual_line: true,
prt_dsmk_p: 0.0,
prt_dsmk_m: 0.0,
};
const result = await client.request(queries.INSERT_NEW_JOB_LINE, {
lineInput: [newAtsLine],
});
job.joblines.push(newAtsLine);
}
//If it does not, create one for local calculations and insert it.
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;
}
console.log(job.jobLines);
}
}
function CalculateRatesTotals(ratesList) {
const jobLines = ratesList.joblines.filter((jl) => !jl.removed);