feature/IO-3205-Paint-Scale-Integrations: Fix output query

This commit is contained in:
Dave Richer
2025-04-30 12:37:22 -04:00
parent a2511685ac
commit 593f01f5dc

View File

@@ -52,6 +52,8 @@ interface GraphQLResponse {
shopname: string; shopname: string;
}; };
jobs?: Array<{ jobs?: Array<{
labhrs: any;
larhrs: any;
ro_number: string; ro_number: string;
ownr_ln: string; ownr_ln: string;
ownr_fn: string; ownr_fn: string;
@@ -232,80 +234,68 @@ const outputTypeHandlers: Record<
try { try {
log.info(`Generating PPG output for config ${config.id}: ${config.path}`); log.info(`Generating PPG output for config ${config.id}: ${config.path}`);
// Ensure output directory exists
await fs.mkdir(config.path!, { recursive: true }); await fs.mkdir(config.path!, { recursive: true });
// GraphQL query for jobs
const query = ` const query = `
query PpgData($today: date!, $todayplus5: date!, $shopid: uuid!) { query PpgData($today: timestamptz!, $todayplus5: timestamptz!, $shopid: uuid!) {
bodyshops_by_pk(id: $shopid) { bodyshops_by_pk(id:$shopid) {
imexshopid id
shopname shopname
} imexshopid
jobs(where: { }
jobs(where: {
_or: [
{
_and: [ _and: [
{ shop_id: { _eq: $shopid } }, { scheduled_in: { _lte: $todayplus5 } },
{ _or: [ { scheduled_in: { _gte: $today } }
{ status: { _eq: "Active" } },
{ scheduled_date: { _gte: $today, _lte: $todayplus5 } }
] }
] ]
}) { },
ro_number { inproduction: { _eq: true } }
ownr_ln ]
ownr_fn }) {
plate_no id
v_vin ro_number
v_model_yr status
v_make_desc ownr_fn
v_model_desc ownr_ln
vehicle { ownr_co_nm
v_paint_codes { v_vin
paint_cd1 v_model_yr
} v_make_desc
v_model_desc
v_color
plate_no
ins_co_nm
est_ct_fn
est_ct_ln
rate_mapa
rate_lab
job_totals
vehicle {
v_paint_codes
}
labhrs: joblines_aggregate(where: { mod_lbr_ty: { _neq: "LAR" }, removed: { _eq: false } }) {
aggregate {
sum {
mod_lb_hrs
} }
larhrs_aggregate: estimate_lines_aggregate(where: { line_type: { _eq: "Paint" } }) {
aggregate {
sum {
mod_lb_hrs
}
}
}
ins_co_nm
est_ct_ln
est_ct_fn
job_totals {
rates {
mapa {
total {
amount
}
}
}
totals {
subtotal {
amount
}
}
}
rate_mapa
labhrs_aggregate: estimate_lines_aggregate(where: { line_type: { _eq: "Body" } }) {
aggregate {
sum {
mod_lb_hrs
}
}
}
rate_lab
} }
} }
`; larhrs: joblines_aggregate(where: { mod_lbr_ty: { _eq: "LAR" }, removed: { _eq: false } }) {
aggregate {
sum {
mod_lb_hrs
}
}
}
}
}
`;
const variables = { const variables = {
today: new Date().toISOString().split("T")[0], today: new Date().toISOString(),
todayplus5: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000) todayplus5: new Date(Date.now() + 5 * 86400000).toISOString(),
.toISOString()
.split("T")[0],
shopid: (store.get("app.bodyshop") as BodyShop)?.id, shopid: (store.get("app.bodyshop") as BodyShop)?.id,
}; };
@@ -314,8 +304,9 @@ const outputTypeHandlers: Record<
variables, variables,
)) as GraphQLResponse; )) as GraphQLResponse;
// Build JSON structure const jobs = response.jobs ?? [];
const jsonData = {
const header = {
PPG: { PPG: {
Header: { Header: {
Protocol: { Protocol: {
@@ -342,52 +333,48 @@ const outputTypeHandlers: Record<
ShopName: response.bodyshops_by_pk?.shopname || "", ShopName: response.bodyshops_by_pk?.shopname || "",
}, },
RepairOrders: { RepairOrders: {
ROCount: (response.jobs?.length || 0).toString(), ROCount: jobs.length.toString(),
RO: RO: jobs.map((job) => ({
response.jobs?.map((job) => ({ RONumber: job.ro_number || "",
RONumber: job.ro_number || "", ROStatus: "Open",
ROStatus: "Open", Customer: `${job.ownr_ln || ""}, ${job.ownr_fn || ""}`,
Customer: `${job.ownr_ln || ""}, ${job.ownr_fn || ""}`, ROPainterNotes: "",
ROPainterNotes: "", LicensePlateNum: job.plate_no || "",
LicensePlateNum: job.plate_no || "", VIN: job.v_vin || "",
VIN: job.v_vin || "", ModelYear: job.v_model_yr || "",
ModelYear: job.v_model_yr || "", MakeDesc: job.v_make_desc || "",
MakeDesc: job.v_make_desc || "", ModelName: job.v_model_desc || "",
ModelName: job.v_model_desc || "", OEMColorCode: job.vehicle?.v_paint_codes?.paint_cd1 || "",
OEMColorCode: job.vehicle?.v_paint_codes?.paint_cd1 || "", RefinishLaborHours:
RefinishLaborHours: job.larhrs?.aggregate?.sum?.mod_lb_hrs || 0,
job.larhrs_aggregate?.aggregate?.sum?.mod_lb_hrs || 0, InsuranceCompanyName: job.ins_co_nm || "",
InsuranceCompanyName: job.ins_co_nm || "", EstimatorName: `${job.est_ct_ln || ""}, ${job.est_ct_fn || ""}`,
EstimatorName: `${job.est_ct_ln || ""}, ${job.est_ct_fn || ""}`, PaintMaterialsRevenue: (
PaintMaterialsRevenue: ( (job.job_totals?.rates?.mapa?.total?.amount || 0) / 100
(job.job_totals?.rates?.mapa?.total?.amount || 0) / 100 ).toFixed(2),
).toFixed(2), PaintMaterialsRate: job.rate_mapa || 0,
PaintMaterialsRate: job.rate_mapa || 0, BodyHours: job.labhrs?.aggregate?.sum?.mod_lb_hrs || 0,
BodyHours: BodyLaborRate: job.rate_lab || 0,
job.labhrs_aggregate?.aggregate?.sum?.mod_lb_hrs || 0, TotalCostOfRepairs: (
BodyLaborRate: job.rate_lab || 0, (job.job_totals?.totals?.subtotal?.amount || 0) / 100
TotalCostOfRepairs: ( ).toFixed(2),
(job.job_totals?.totals?.subtotal?.amount || 0) / 100 })),
).toFixed(2),
})) || [],
}, },
}, },
}, },
}, },
}; };
// Convert JSON to XML const xml = create({ version: "1.0" }, header).end({ prettyPrint: true });
const doc = create({ version: "1.0" }, jsonData);
const xmlString = doc.end({ prettyPrint: true });
// Save XML to file
const outputPath = path.join(config.path!, `PPGPaint.xml`); const outputPath = path.join(config.path!, `PPGPaint.xml`);
await fs.writeFile(outputPath, xmlString); await fs.writeFile(outputPath, xml);
log.info(`Saved PPG output XML to ${outputPath}`); log.info(`Saved PPG output XML to ${outputPath}`);
} catch (error) { } catch (error) {
log.error(`Error generating PPG output for config ${config.id}:`, error); log.error(`Error generating PPG output for config ${config.id}:`, error);
} }
}, // Add other output type handlers as needed },
}; };
// Default handler for unsupported types // Default handler for unsupported types