diff --git a/server/accounting/pbs/pbs-job-export.js b/server/accounting/pbs/pbs-job-export.js index 4d25d0c3d..ead639de0 100644 --- a/server/accounting/pbs/pbs-job-export.js +++ b/server/accounting/pbs/pbs-job-export.js @@ -21,7 +21,7 @@ axios.interceptors.request.use((x) => { }; const printable = `${new Date()} | Request: ${x.method.toUpperCase()} | ${x.url } | ${JSON.stringify(x.data)} | ${JSON.stringify(headers)}`; - //console.log(printable); + //logRequestToFile(printable); CdkBase.createJsonEvent(socket, "DEBUG", `Raw Request: ${printable}`, x.data); @@ -31,13 +31,29 @@ axios.interceptors.request.use((x) => { axios.interceptors.response.use((x) => { const socket = x.config.socket; - const printable = `${new Date()} | Response: ${x.status} | ${JSON.stringify(x.data)}`; - //console.log(printable); + const printable = `${new Date()} | Response: ${x.status} ${x.statusText} |${JSON.stringify(x.data)}`; + //logRequestToFile(printable); CdkBase.createJsonEvent(socket, "DEBUG", `Raw Response: ${printable}`, x.data); return x; }); +const fs = require('fs'); +const path = require("path"); +function logRequestToFile(printable) { + try { + const logDir = path.join(process.cwd(), "logs"); + if (!fs.existsSync(logDir)) { + fs.mkdirSync(logDir, { recursive: true }); + } + const logFile = path.join(logDir, "pbs-http.log"); + fs.appendFileSync(logFile, `${printable}\n`); + } catch (err) { + console.error("Unexpected error in logRequestToFile:", err); + } +} + + exports.default = async function (socket, { txEnvelope, jobid }) { socket.logEvents = []; socket.recordid = jobid; @@ -90,16 +106,19 @@ exports.PbsSelectedCustomer = async function PbsSelectedCustomer(socket, selecte "INFO", `Contact and Vehicle updates disabled. Skipping to accounting data insert.` ); - const ownerRef = await QueryCustomerBycodeFromDms(socket, selectedCustomerId); - socket.ownerRef = ownerRef; + if (!socket.ownerRef) { + const ownerRef = (await QueryCustomerBycodeFromDms(socket, selectedCustomerId))?.[0]; + socket.ownerRef = ownerRef; + } CdkBase.createLogEvent(socket, "INFO", `Upserting vehicle information to DMS for ${socket.JobData.v_vin}`); - const vehicleRef = await GetVehicleData(socket, ownerRef.ReferenceId); + const vehicleRef = await GetVehicleData(socket, socket.ownerRef?.ReferenceId || socket.selectedCustomerId); socket.vehicleRef = vehicleRef; } CdkBase.createLogEvent(socket, "INFO", `Inserting accounting posting data..`); const insertResponse = await InsertAccountPostingData(socket); if (insertResponse.WasSuccessful) { + await CreateRepairOrderInPBS(socket, socket.ownerRef, socket.vehicleRef) CdkBase.createLogEvent(socket, "INFO", `Marking job as exported.`); await MarkJobExported(socket, socket.JobData.id); if (socket.jobData.bodyshop.pbs_configuration.ro_posting) { @@ -478,7 +497,7 @@ async function UpsertVehicleData(socket, ownerRef) { async function GetVehicleData(socket, ownerRef) { try { - const { data: Vehicles } = await axios.post( + const { data: { Vehicles } } = await axios.post( PBS_ENDPOINTS.VehicleGet, { SerialNumber: socket.JobData.bodyshop.pbs_serialnumber, @@ -714,7 +733,7 @@ async function RepairOrderGet(socket) { // "Tag": "String", //"ContactRef": socket.contactRef, // "ContactRefList": ["00000000000000000000000000000000"], - "VehicleRef": socket.vehicleRef?.ReferenceId, + "VehicleRef": socket.vehicleRef?.ReferenceId || socket.vehicleRef?.VehicleId, // "VehicleRefList": ["00000000000000000000000000000000"], // "Status": "String", // "CashieredSince": "0001-01-01T00:00:00.0000000Z", @@ -760,8 +779,8 @@ async function RepairOrderChange(socket) { // "CSRRef": "00000000000000000000000000000000", // "BookingUser": "String", // "BookingUserRef": "00000000000000000000000000000000", - "ContactRef": socket.ownerRef?.ReferenceId, - "VehicleRef": socket.vehicleRef?.ReferenceId, + "ContactRef": socket.ownerRef?.ReferenceId || socket.ownerRef?.ContactId, + "VehicleRef": socket.vehicleRef?.ReferenceId || socket.vehicleRef?.VehicleId, "MileageIn": socket.JobData.km_in, "Tag": "BODYSHOP", //"Status": "CLOSED", //Values here do not impact the status. Confirmed by PBS support. diff --git a/server/graphql-client/queries.js b/server/graphql-client/queries.js index 26bbd70fd..d889a7412 100644 --- a/server/graphql-client/queries.js +++ b/server/graphql-client/queries.js @@ -420,6 +420,8 @@ query QUERY_JOBS_FOR_PBS_EXPORT($id: uuid!) { v_make_desc v_color ca_customer_gst + scheduled_completion + actual_completion vehicle { v_trimcode v_makecode @@ -2155,18 +2157,16 @@ exports.UPDATE_OLD_TRANSITION = `mutation UPDATE_OLD_TRANSITION($jobid: uuid!, $ exports.INSERT_NEW_TRANSITION = ( includeOldTransition -) => `mutation INSERT_NEW_TRANSITION($newTransition: transitions_insert_input!, ${ - includeOldTransition ? `$oldTransitionId: uuid!, $duration: numeric` : "" -}) { +) => `mutation INSERT_NEW_TRANSITION($newTransition: transitions_insert_input!, ${includeOldTransition ? `$oldTransitionId: uuid!, $duration: numeric` : "" + }) { insert_transitions_one(object: $newTransition) { id } - ${ - includeOldTransition - ? `update_transitions(where: {id: {_eq: $oldTransitionId}}, _set: {duration: $duration}) { + ${includeOldTransition + ? `update_transitions(where: {id: {_eq: $oldTransitionId}}, _set: {duration: $duration}) { affected_rows }` - : "" + : "" } }`;