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