From 7a50f2a2fe50d7c1402c7fff053e9f58f79a44e4 Mon Sep 17 00:00:00 2001 From: Allan Carr Date: Mon, 11 May 2026 22:11:31 -0700 Subject: [PATCH] IO-3691 Job Totals Issues Signed-off-by: Allan Carr --- server/job/job-totals-USA.js | 33 +++++++++++++++++++-------------- server/job/job-totals.js | 20 ++++++++++++-------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/server/job/job-totals-USA.js b/server/job/job-totals-USA.js index 29d541e2e..4c280cdf2 100644 --- a/server/job/job-totals-USA.js +++ b/server/job/job-totals-USA.js @@ -31,7 +31,7 @@ exports.totalsSsu = async function (req, res) { id: id }); - const newTotals = await TotalsServerSide({ body: { job: job.jobs_by_pk, client: client } }, res, true); + const newTotals = await TotalsServerSide({ body: { job: job.jobs_by_pk, client: client } }); const result = await client.setHeaders({ Authorization: BearerToken }).request(queries.UPDATE_JOB, { jobId: id, @@ -54,12 +54,15 @@ exports.totalsSsu = async function (req, res) { error: error.message, stack: error.stack }); - res.status(503).json({ error: "Failed to calculate totals" }); + res.status(503).json({ + error: "Failed to calculate totals", + message: error?.message || "Unknown error" + }); } }; //IMPORTANT*** These two functions MUST be mirrored. -async function TotalsServerSide(req, res) { +async function TotalsServerSide(req) { const { job, client } = req.body; await AtsAdjustmentsIfRequired({ job: job, client: client, user: req?.user }); @@ -134,10 +137,11 @@ async function TotalsServerSide(req, res) { .filter((v) => v != null); const taxRate = Math.max(...laborRates, ...materialRates, ...partsRates); - const totalTaxes = ret.totals.taxableAmounts.total.multiply(taxRate > 1 ? taxRate / 100 : taxRate); - ret.totals.taxableAmounts.total = ret.totals.taxableAmounts.total - .multiply(emsTaxTotal) - .divide(totalTaxes.toUnit()); + if (taxRate > 0) { + ret.totals.taxableAmounts.total = Dinero({ + amount: Math.round((emsTaxTotal / (taxRate > 1 ? taxRate / 100 : taxRate)) * 100) + }); + } } else { ret.totals.taxableAmounts.total = ret.totals.taxableAmounts.total.multiply(emsTaxTotal).divide(totalUsTaxes); } @@ -155,7 +159,7 @@ async function TotalsServerSide(req, res) { error: error.message, stack: error.stack }); - res.status(400).send(JSON.stringify(error)); + throw error; } } @@ -168,7 +172,7 @@ async function Totals(req, res) { const logger = req.logger; const client = req.userGraphQLClient; - logger.log("job-totals-ssu-USA", "debug", req.user.email, job.id, { + logger.log("job-totals-ssu-USA", "debug", req?.user?.email, job.id, { jobid: job.id, id: id }); @@ -185,7 +189,7 @@ async function Totals(req, res) { res.status(200).json(ret); } catch (error) { - logger.log("job-totals-ssu-USA-error", "error", req.user.email, job.id, { + logger.log("job-totals-ssu-USA-error", "error", req?.user?.email, job.id, { jobid: job.id, error: error.message, stack: error.stack @@ -788,7 +792,7 @@ function IsAdditionalCost(jobLine) { return ( (jobLine.lbr_op === "OP13" || //Added to resolve manual job lines coming into other totals because they have no reference. - (jobLine.part_type === null && (jobLine.act_price || 0 > 0)) || + (jobLine.part_type === null && (jobLine.act_price || 0) > 0) || (jobLine.db_ref && jobLine.db_ref.startsWith("9360"))) && //This ref works in Canada, but DB_REFS in the US do not fill in. !isPaintOrShopMat ); @@ -993,12 +997,13 @@ function CalculateTaxesTotals(job, otherTotals) { }) ); - if (stlStorage) + if (stlStorage) { taxableAmounts.STOR = taxableAmounts.STOR.add( - (taxableAmounts.STOR = Dinero({ + Dinero({ amount: Math.round(stlStorage.t_amt * 100) - })) + }) ); + } if (!stlStorage && !job.ciecaid && job.storage_payable) taxableAmounts.STOR = taxableAmounts.STOR.add( diff --git a/server/job/job-totals.js b/server/job/job-totals.js index 1a578c724..cf1dcf633 100644 --- a/server/job/job-totals.js +++ b/server/job/job-totals.js @@ -23,7 +23,7 @@ exports.totalsSsu = async function (req, res) { const BearerToken = req.BearerToken; const client = req.userGraphQLClient; - logger.log("job-totals-ssu", "debug", req.user.email, id, null); + logger.log("job-totals-ssu", "debug", req?.user?.email, id, null); try { const job = await client.setHeaders({ Authorization: BearerToken }).request(queries.GET_JOB_BY_PK, { @@ -31,7 +31,7 @@ exports.totalsSsu = async function (req, res) { }); // Capture the output of TotalsServerSide - const newTotals = await TotalsServerSide({ body: { job: job.jobs_by_pk, client: client } }, res, true); + const newTotals = await TotalsServerSide({ body: { job: job.jobs_by_pk, client: client } }); const result = await client.setHeaders({ Authorization: BearerToken }).request(queries.UPDATE_JOB, { jobId: id, @@ -49,7 +49,7 @@ exports.totalsSsu = async function (req, res) { res.status(200).json({ success: true }); } catch (error) { - logger.log("job-totals-ssu-error", "error", req.user.email, id, { + logger.log("job-totals-ssu-error", "error", req?.user?.email, id, { jobid: id, error: error.message, stack: error.stack @@ -59,7 +59,7 @@ exports.totalsSsu = async function (req, res) { }; //IMPORTANT*** These two functions MUST be mirrored. -async function TotalsServerSide(req, res) { +async function TotalsServerSide(req) { const { job, client } = req.body; await AtsAdjustmentsIfRequired({ job: job, client: client, user: req?.user }); @@ -78,7 +78,7 @@ async function TotalsServerSide(req, res) { error: error.message, stack: error.stack }); - res.status(400).send(JSON.stringify(error)); + throw error; } } @@ -91,7 +91,7 @@ async function Totals(req, res) { const logger = req.logger; const client = req.userGraphQLClient; - logger.log("job-totals-ssu", "debug", req.user.email, job.id, { + logger.log("job-totals-ssu", "debug", req?.user?.email, job.id, { jobid: job.id, id: id }); @@ -108,12 +108,15 @@ async function Totals(req, res) { res.status(200).json(ret); } catch (error) { - logger.log("job-totals-ssu-error", "error", req.user.email, job.id, { + logger.log("job-totals-ssu-error", "error", req?.user?.email, job.id, { jobid: job.id, error: error.message, stack: error.stack }); - res.status(400).send(JSON.stringify(error)); + res.status(503).json({ + error: "Failed to calculate totals", + message: error?.message || "Unknown error" + }); } } @@ -626,6 +629,7 @@ function CalculateTaxesTotals(job, otherTotals) { (val.part_type && val.part_type.startsWith("PAG") && BackupGlassTax && BackupGlassTax.prt_tax_rt) || (!val.part_type && val.db_ref === "900510" && + job.parts_tax_rates && job.parts_tax_rates["PAN"] && job.parts_tax_rates["PAN"].prt_tax_rt) || 0) * 100