Extensions to job totals calculation.
This commit is contained in:
@@ -1,10 +1,75 @@
|
||||
const Dinero = require("dinero.js");
|
||||
const queries = require("../graphql-client/queries");
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
|
||||
Dinero.defaultCurrency = "USD";
|
||||
Dinero.globalLocale = "en-CA";
|
||||
Dinero.globalRoundingMode = "HALF_UP";
|
||||
|
||||
exports.default = async function (req, res) {
|
||||
exports.totalsSsu = async function (req, res) {
|
||||
const BearerToken = req.headers.authorization;
|
||||
const { id } = req.body;
|
||||
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
headers: {
|
||||
Authorization: BearerToken,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const job = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.GET_JOB_BY_PK, {
|
||||
id: id,
|
||||
});
|
||||
|
||||
const newTotals = await TotalsServerSide(
|
||||
{ body: { job: job.jobs_by_pk } },
|
||||
res,
|
||||
true
|
||||
);
|
||||
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.UPDATE_JOB, {
|
||||
jobId: id,
|
||||
job: {
|
||||
clm_total: newTotals.totals.total_repairs.toFormat("0.00"),
|
||||
owner_owing: newTotals.totals.custPayable.total.toFormat("0.00"),
|
||||
job_totals: newTotals,
|
||||
queued_for_parts: true,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(503);
|
||||
}
|
||||
};
|
||||
|
||||
//IMPORTANT*** These two functions MUST be mirrrored.
|
||||
async function TotalsServerSide(req, res) {
|
||||
const { job } = req.body;
|
||||
console.log(
|
||||
`Calculating Job Totals on the server side for ${job.id} - ${job.ro_number}`
|
||||
);
|
||||
try {
|
||||
let ret = {
|
||||
parts: CalculatePartsTotals(job.joblines),
|
||||
rates: CalculateRatesTotals(job),
|
||||
additional: CalculateAdditional(job),
|
||||
};
|
||||
ret.totals = CalculateTaxesTotals(job, ret);
|
||||
|
||||
return ret;
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
}
|
||||
|
||||
async function Totals(req, res) {
|
||||
const { job } = req.body;
|
||||
console.log(`Calculating Job Totals for ${job.id} - ${job.ro_number}`);
|
||||
try {
|
||||
@@ -14,12 +79,13 @@ exports.default = async function (req, res) {
|
||||
additional: CalculateAdditional(job),
|
||||
};
|
||||
ret.totals = CalculateTaxesTotals(job, ret);
|
||||
|
||||
res.status(200).json(ret);
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
};
|
||||
}
|
||||
function CalculateRatesTotals(ratesList, shoprates) {
|
||||
const jobLines = ratesList.joblines.filter((jl) => !jl.removed);
|
||||
|
||||
@@ -237,11 +303,6 @@ function CalculateAdditional(job) {
|
||||
}
|
||||
|
||||
function CalculateTaxesTotals(job, otherTotals) {
|
||||
console.log(
|
||||
"🚀 ~ file: job-totals.js ~ line 240 ~ otherTotals",
|
||||
JSON.stringify(otherTotals)
|
||||
);
|
||||
|
||||
const subtotal = otherTotals.parts.parts.subtotal
|
||||
.add(otherTotals.parts.sublets.subtotal)
|
||||
.add(otherTotals.rates.rates_subtotal)
|
||||
@@ -327,3 +388,5 @@ function CalculateTaxesTotals(job, otherTotals) {
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
exports.default = Totals;
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
exports.totals = require("./job-totals").default;
|
||||
exports.totalsSsu = require("./job-totals").totalsSsu;
|
||||
|
||||
Reference in New Issue
Block a user