IO-2835 CDK Calculate Allocation Route

Signed-off-by: Allan Carr <allan.carr@thinkimex.com>
This commit is contained in:
Allan Carr
2024-07-08 18:07:33 -07:00
parent 84bb25985b
commit ff33b924b2
2 changed files with 390 additions and 389 deletions

View File

@@ -16,10 +16,37 @@ const { DiscountNotAlreadyCounted } = InstanceManager({
promanager: "USE_ROME" promanager: "USE_ROME"
}); });
exports.defaultRoute = async function (req, res) {
try {
CdkBase.createLogEvent(req, "DEBUG", `Received request to calculate allocations for ${req.body.jobid}`);
const jobData = await QueryJobData(req, req.BearerToken, req.body.jobid);
return res.status(200).json({ data: calculateAllocations(req, jobData) });
} catch (error) {
console.log(error);
CdkBase.createLogEvent(req, "ERROR", `Error encountered in CdkCalculateAllocations. ${error}`);
res.status(500).json({ error: `Error encountered in CdkCalculateAllocations. ${error}` });
}
};
exports.default = async function (socket, jobid) { exports.default = async function (socket, jobid) {
try { try {
CdkBase.createLogEvent(socket, "DEBUG", `Received request to calculate allocations for ${jobid}`); const jobData = await QueryJobData(socket, "Bearer " + socket.handshake.auth.token, jobid);
const job = await QueryJobData(socket, jobid); return calculateAllocations(socket, jobData);
} catch (error) {
console.log(error);
CdkBase.createLogEvent(socket, "ERROR", `Error encountered in CdkCalculateAllocations. ${error}`);
}
};
async function QueryJobData(connectionData, token, jobid) {
CdkBase.createLogEvent(connectionData, "DEBUG", `Querying job data for id ${jobid}`);
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
const result = await client.setHeaders({ Authorization: token }).request(queries.GET_CDK_ALLOCATIONS, { id: jobid });
CdkBase.createLogEvent(connectionData, "TRACE", `Job data query result ${JSON.stringify(result, null, 2)}`);
return result.jobs_by_pk;
}
function calculateAllocations(connectionData, job) {
const { bodyshop } = job; const { bodyshop } = job;
const taxAllocations = InstanceManager({ const taxAllocations = InstanceManager({
@@ -145,7 +172,7 @@ exports.default = async function (socket, jobid) {
(d) => d.name === job.dms_allocation (d) => d.name === job.dms_allocation
); );
CdkBase.createLogEvent( CdkBase.createLogEvent(
socket, connectionData,
"DEBUG", "DEBUG",
`Using DMS Allocation ${selectedDmsAllocationConfig && selectedDmsAllocationConfig.name} for cost export.` `Using DMS Allocation ${selectedDmsAllocationConfig && selectedDmsAllocationConfig.name} for cost export.`
); );
@@ -225,10 +252,10 @@ exports.default = async function (socket, jobid) {
// console.log("NO MASH ACCOUNT FOUND!!"); // console.log("NO MASH ACCOUNT FOUND!!");
} }
} }
console.log( // console.log(
Number.isInteger(bodyshop?.cdk_configuration?.sendmaterialscosting), // Number.isInteger(bodyshop?.cdk_configuration?.sendmaterialscosting),
typeof Number.isInteger(bodyshop?.cdk_configuration?.sendmaterialscosting) // typeof Number.isInteger(bodyshop?.cdk_configuration?.sendmaterialscosting)
); // );
if (!!bodyshop?.cdk_configuration?.sendmaterialscosting) { if (!!bodyshop?.cdk_configuration?.sendmaterialscosting) {
//Manually send the percentage of the costing. //Manually send the percentage of the costing.
@@ -236,34 +263,22 @@ exports.default = async function (socket, jobid) {
const mapaAccountName = selectedDmsAllocationConfig.costs.MAPA; const mapaAccountName = selectedDmsAllocationConfig.costs.MAPA;
const mapaAccount = bodyshop.md_responsibility_centers.costs.find((c) => c.name === mapaAccountName); const mapaAccount = bodyshop.md_responsibility_centers.costs.find((c) => c.name === mapaAccountName);
if (mapaAccount) { if (mapaAccount) {
if (!costCenterHash[mapaAccountName]) if (!costCenterHash[mapaAccountName]) costCenterHash[mapaAccountName] = Dinero();
costCenterHash[mapaAccountName] = Dinero();
if (job.bodyshop.use_paint_scale_data === true) { if (job.bodyshop.use_paint_scale_data === true) {
if (job.mixdata.length > 0) { if (job.mixdata.length > 0) {
costCenterHash[mapaAccountName] = costCenterHash[ costCenterHash[mapaAccountName] = costCenterHash[mapaAccountName].add(
mapaAccountName
].add(
Dinero({ Dinero({
amount: Math.round( amount: Math.round(((job.mixdata[0] && job.mixdata[0].totalliquidcost) || 0) * 100)
((job.mixdata[0] && job.mixdata[0].totalliquidcost) || 0) *
100
),
}) })
); );
} else { } else {
costCenterHash[mapaAccountName] = costCenterHash[ costCenterHash[mapaAccountName] = costCenterHash[mapaAccountName].add(
mapaAccountName Dinero(job.job_totals.rates.mapa.total).percentage(bodyshop?.cdk_configuration?.sendmaterialscosting)
].add(
Dinero(job.job_totals.rates.mapa.total).percentage(
bodyshop?.cdk_configuration?.sendmaterialscosting
)
); );
} }
} else { } else {
costCenterHash[mapaAccountName] = costCenterHash[mapaAccountName].add( costCenterHash[mapaAccountName] = costCenterHash[mapaAccountName].add(
Dinero(job.job_totals.rates.mapa.total).percentage( Dinero(job.job_totals.rates.mapa.total).percentage(bodyshop?.cdk_configuration?.sendmaterialscosting)
bodyshop?.cdk_configuration?.sendmaterialscosting
)
); );
} }
} else { } else {
@@ -289,9 +304,7 @@ exports.default = async function (socket, jobid) {
// (c) => c.name === mashAccountName // (c) => c.name === mashAccountName
// ); // );
taxAllocations.state.sale = taxAllocations.state.sale.add( taxAllocations.state.sale = taxAllocations.state.sale.add(Dinero({ amount: Math.round((ca_bc_pvrt || 0) * 100) }));
Dinero({ amount: Math.round((ca_bc_pvrt || 0) * 100) })
);
} }
if (job.towing_payable && job.towing_payable !== 0) { if (job.towing_payable && job.towing_payable !== 0) {
@@ -361,7 +374,7 @@ exports.default = async function (socket, jobid) {
); );
} else { } else {
CdkBase.createLogEvent( CdkBase.createLogEvent(
socket, connectionData,
"ERROR", "ERROR",
`Error encountered in CdkCalculateAllocations. Unable to find adjustment account. ${error}` `Error encountered in CdkCalculateAllocations. Unable to find adjustment account. ${error}`
); );
@@ -401,18 +414,4 @@ exports.default = async function (socket, jobid) {
} }
}) })
]; ];
} catch (error) {
console.log(error);
CdkBase.createLogEvent(socket, "ERROR", `Error encountered in CdkCalculateAllocations. ${error}`);
}
};
async function QueryJobData(socket, jobid) {
CdkBase.createLogEvent(socket, "DEBUG", `Querying job data for id ${jobid}`);
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
const result = await client
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
.request(queries.GET_CDK_ALLOCATIONS, { id: jobid });
CdkBase.createLogEvent(socket, "TRACE", `Job data query result ${JSON.stringify(result, null, 2)}`);
return result.jobs_by_pk;
} }

View File

@@ -1,11 +1,13 @@
const express = require("express"); const express = require("express");
const router = express.Router(); const router = express.Router();
const cdkGetMake = require("../cdk/cdk-get-makes"); const cdkGetMake = require("../cdk/cdk-get-makes");
const cdkCalculateAllocations = require("../cdk/cdk-calculate-allocations");
const validateFirebaseIdTokenMiddleware = require("../middleware/validateFirebaseIdTokenMiddleware"); const validateFirebaseIdTokenMiddleware = require("../middleware/validateFirebaseIdTokenMiddleware");
const withUserGraphQLClientMiddleware = require("../middleware/withUserGraphQLClientMiddleware"); const withUserGraphQLClientMiddleware = require("../middleware/withUserGraphQLClientMiddleware");
router.use(validateFirebaseIdTokenMiddleware); router.use(validateFirebaseIdTokenMiddleware);
router.post("/getvehicles", withUserGraphQLClientMiddleware, cdkGetMake.default); router.post("/getvehicles", withUserGraphQLClientMiddleware, cdkGetMake.default);
router.post("/calculate-allocations", withUserGraphQLClientMiddleware, cdkCalculateAllocations.defaultRoute);
module.exports = router; module.exports = router;