diff --git a/client/package.json b/client/package.json index 29a7232f5..e8c21b18e 100644 --- a/client/package.json +++ b/client/package.json @@ -2,7 +2,7 @@ "name": "bodyshop", "version": "0.1.1", "private": true, - "proxy": "http://localhost:5000", + "proxy": "http://localhost:4000", "dependencies": { "@apollo/client": "^3.4.16", "@craco/craco": "^6.4.0", diff --git a/server/accounting/pbs/pbs-job-export.js b/server/accounting/pbs/pbs-job-export.js index ed666bcf4..260cf575c 100644 --- a/server/accounting/pbs/pbs-job-export.js +++ b/server/accounting/pbs/pbs-job-export.js @@ -11,13 +11,15 @@ const queries = require("../../graphql-client/queries"); const { PBS_ENDPOINTS, PBS_CREDENTIALS } = require("./pbs-constants"); //const { CDK_CREDENTIALS, CheckCdkResponseForError } = require("./cdk-wsdl"); -//const CalcualteAllocations = require("./cdk-calculate-allocations").default; +const CalculateAllocations = + require("../../cdk/cdk-calculate-allocations").default; const CdkBase = require("../../web-sockets/web-socket"); +const moment = require("moment"); exports.default = async function (socket, { txEnvelope, jobid }) { socket.logEvents = []; socket.recordid = jobid; - + socket.txEnvelope = txEnvelope; try { CdkBase.createLogEvent( socket, @@ -35,7 +37,18 @@ exports.default = async function (socket, { txEnvelope, jobid }) { `Upserting contact information to DMS for ${socket.JobData.ownr_fn} ${socket.JobData.ownr_ln} ${socket.JobData.ownr_co_nm}` ); const ownerRef = await UpsertContactData(socket); - await UpsertVehicleData(socket, ownerRef.ReferenceId); + CdkBase.createLogEvent( + socket, + "DEBUG", + `Upserting vehicle information to DMS for ${socket.JobData.v_vin}` + ); + // await UpsertVehicleData(socket, ownerRef.ReferenceId); + CdkBase.createLogEvent(socket, "DEBUG", `Inserting account data.`); + await InsertAccountPostingData(socket); + CdkBase.createLogEvent(socket, "DEBUG", `Marking job as exported.`); + // await MarkJobExported(socket, socket.JobData.id); + + socket.emit("export-success", socket.JobData.id); } catch (error) { CdkBase.createLogEvent( socket, @@ -318,3 +331,135 @@ async function UpsertVehicleData(socket, ownerRef) { throw new Error(error); } } +async function InsertAccountPostingData(socket) { + try { + const allocations = await CalculateAllocations(socket, socket.JobData.id); + + const wips = []; + allocations.forEach((alloc) => { + //Add the sale item from each allocation. + if (alloc.sale.getAmount() > 0 && !alloc.tax) { + const item = { + Account: alloc.profitCenter.dms_acctnumber, + ControlNumber: socket.JobData.ro_number, + Amount: alloc.sale.multiply(-1).toFormat("0.0"), + //Comment: "String", + //AdditionalInfo: "String", + InvoiceNumber: socket.JobData.ro_number, + InvoiceDate: moment(socket.JobData.date_invoiced).toISOString(), + }; + wips.push(item); + } + + //Add the cost Item. + if (alloc.cost.getAmount() > 0 && !alloc.tax) { + const item = { + Account: alloc.costCenter.dms_acctnumber, + ControlNumber: socket.JobData.ro_number, + Amount: alloc.cost.toFormat("0.0"), + //Comment: "String", + //AdditionalInfo: "String", + InvoiceNumber: socket.JobData.ro_number, + InvoiceDate: moment(socket.JobData.date_invoiced).toISOString(), + }; + wips.push(item); + + const itemWip = { + Account: alloc.costCenter.dms_wip_acctnumber, + ControlNumber: socket.JobData.ro_number, + Amount: alloc.cost.multiply(-1).toFormat("0.0"), + //Comment: "String", + //AdditionalInfo: "String", + InvoiceNumber: socket.JobData.ro_number, + InvoiceDate: moment(socket.JobData.date_invoiced).toISOString(), + }; + wips.push(itemWip); + //Add to the WIP account. + } + + if (alloc.tax) { + if (alloc.sale.getAmount() > 0) { + const item2 = { + Account: alloc.profitCenter.dms_acctnumber, + ControlNumber: socket.JobData.ro_number, + Amount: alloc.sale.multiply(-1).toFormat("0.0"), + //Comment: "String", + //AdditionalInfo: "String", + InvoiceNumber: socket.JobData.ro_number, + InvoiceDate: moment(socket.JobData.date_invoiced).toISOString(), + }; + wips.push(item2); + } + } + }); + + console.log( + JSON.stringify({ + SerialNumber: socket.JobData.bodyshop.pbs_serialnumber, + Posting: { + Reference: socket.JobData.ro_number, + JournalCode: socket.txEnvelope.journal, + TransactionDate: moment(socket.JobData.date_invoiced).toISOString(), //"0001-01-01T00:00:00.0000000Z", + Description: socket.txEnvelope.story, + //AdditionalInfo: "String", + Source: "ImEX Online", + Lines: wips, + }, + }) + ); + const { data: AccountPostingChange } = await axios.post( + PBS_ENDPOINTS.AccountingPostingChange, + { + SerialNumber: socket.JobData.bodyshop.pbs_serialnumber, + Posting: { + Reference: socket.JobData.ro_number, + JournalCode: socket.txEnvelope.journal, + TransactionDate: moment(socket.JobData.date_invoiced).toISOString(), //"0001-01-01T00:00:00.0000000Z", + Description: socket.txEnvelope.story, + //AdditionalInfo: "String", + Source: "ImEX Online", + Lines: wips, + }, + }, + { auth: PBS_CREDENTIALS } + ); + + CheckForErrors(socket, AccountPostingChange); + return AccountPostingChange; + } catch (error) { + CdkBase.createLogEvent( + socket, + "ERROR", + `Error in InsertAccountPostingData - ${error}` + ); + throw new Error(error); + } +} + +async function MarkJobExported(socket, jobid) { + CdkBase.createLogEvent( + socket, + "DEBUG", + `Marking job as exported for id ${jobid}` + ); + const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {}); + const result = await client + .setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` }) + .request(queries.MARK_JOB_EXPORTED, { + jobId: jobid, + job: { + status: + socket.JobData.bodyshop.md_ro_statuses.default_exported || + "Exported*", + date_exported: new Date(), + }, + log: { + bodyshopid: socket.JobData.bodyshop.id, + jobid: jobid, + successful: true, + useremail: socket.user.email, + }, + }); + + return result; +}