diff --git a/server/data/kaizen.js b/server/data/kaizen.js index 86068c1ea..8321ce138 100644 --- a/server/data/kaizen.js +++ b/server/data/kaizen.js @@ -219,8 +219,6 @@ const CreateRepairOrderTag = (job, errorCallback) => { } const repairCosts = CreateCosts(job); - const jobline = CreateJobLines(job.joblines); - const timeticket = CreateTimeTickets(job.timetickets); try { const ret = { @@ -290,8 +288,100 @@ const CreateRepairOrderTag = (job, errorCallback) => { (job.date_exported && moment(job.date_exported).tz(job.bodyshop.timezone).format(DateFormat)) || "", DateVoid: (job.date_void && moment(job.date_void).tz(job.bodyshop.timezone).format(DateFormat)) || "" }, - JobLineDetails: { jobline }, - TimeTicketDetails: { timeticket }, + JobLineDetails: (function () { + const joblineSource = Array.isArray(job.joblines) ? job.joblines : job.joblines ? [job.joblines] : []; + if (joblineSource.length === 0) return { jobline: [] }; + return { + jobline: joblineSource.map((jl = {}) => ({ + line_description: jl.line_desc || jl.line_description || "", + oem_part_no: jl.oem_partno || jl.oem_part_no || "", + alt_part_no: jl.alt_partno || jl.alt_part_no || "", + op_code_desc: jl.op_code_desc || "", + part_type: jl.part_type || "", + part_qty: jl.part_qty ?? jl.quantity ?? 0, + part_price: jl.act_price ?? jl.part_price ?? 0, + labor_type: jl.mod_lbr_ty || jl.labor_type || "", + labor_hours: jl.mod_lb_hrs ?? jl.labor_hours ?? 0, + labor_sale: jl.lbr_amt ?? jl.labor_sale ?? 0 + })) + }; + })(), + BillsDetails: (function () { + const billsSource = Array.isArray(job.bills) ? job.bills : job.bills ? [job.bills] : []; + if (billsSource.length === 0) return { BillDetails: [] }; + return { + BillDetails: billsSource.map( + ({ + billlines = [], + date = "", + is_credit_memo = false, + invoice_number = "", + isinhouse = false, + vendor = {} + } = {}) => ({ + BillLines: { + BillLine: billlines.map((bl = {}) => ({ + line_description: bl.line_desc || bl.line_description || "", + part_price: bl.actual_price ?? bl.part_price ?? bl.act_price ?? 0, + actual_cost: bl.actual_cost ?? 0, + cost_center: bl.cost_center || "", + deductedfromlbr: bl.deductedfromlbr || false, + part_qty: bl.quantity ?? bl.part_qty ?? 0, + oem_part_no: bl.oem_partno || bl.oem_part_no || "", + alt_part_no: bl.alt_partno || bl.alt_part_no || "" + })) + }, + date, + is_credit_memo, + invoice_number, + isinhouse, + vendorName: vendor.name || "" + }) + ) + }; + })(), + JobNotes: (function () { + const notesSource = Array.isArray(job.notes) ? job.notes : job.notes ? [job.notes] : []; + if (notesSource.length === 0) return { JobNote: [] }; + return { + JobNote: notesSource.map((note = {}) => ({ + created_at: note.created_at || "", + created_by: note.created_by || "", + critical: note.critical || false, + private: note.private || false, + text: note.text || "", + type: note.type || "" + })) + }; + })(), + TimeTicketDetails: (function () { + const ticketSource = Array.isArray(job.timetickets) + ? job.timetickets + : job.timetickets + ? [job.timetickets] + : []; + if (ticketSource.length === 0) return { timeticket: [] }; + return { + timeticket: ticketSource.map((ticket = {}) => ({ + date: ticket.date || "", + employee: + ticket.employee && ticket.employee.employee_number + ? ticket.employee.employee_number + .trim() + .concat(" - ", ticket.employee.first_name.trim(), " ", ticket.employee.last_name.trim()) + .trim() + : "", + productive_hrs: ticket.productivehrs ?? 0, + actual_hrs: ticket.actualhrs ?? 0, + cost_center: ticket.cost_center || "", + flat_rate: ticket.flat_rate || false, + rate: ticket.rate ?? 0, + ticket_cost: ticket.flat_rate + ? ticket.rate * (ticket.productivehrs || 0) + : ticket.rate * (ticket.actualhrs || 0) + })) + }; + })(), Sales: { Labour: { Aluminum: Dinero(job.job_totals.rates.laa.total).toFormat(DineroFormat), @@ -636,42 +726,3 @@ const CreateCosts = (job) => { }, 0) }; }; - -const CreateJobLines = (joblines) => { - const repairLines = []; - joblines.forEach((jobline) => { - repairLines.push({ - line_description: jobline.line_desc, - oem_part_no: jobline.oem_partno, - alt_part_no: jobline.alt_partno, - op_code_desc: jobline.op_code_desc, - part_type: jobline.part_type, - part_qty: jobline.part_qty, - part_price: jobline.act_price, - labor_type: jobline.mod_lbr_ty, - labor_hours: jobline.mod_lb_hrs, - labor_sale: jobline.lbr_amt - }); - }); - return repairLines; -}; - -const CreateTimeTickets = (timetickets) => { - const timeTickets = []; - timetickets.forEach((ticket) => { - timeTickets.push({ - date: ticket.date, - employee: ticket.employee.employee_number - .trim() - .concat(" - ", ticket.employee.first_name.trim(), " ", ticket.employee.last_name.trim()) - .trim(), - productive_hrs: ticket.productivehrs, - actual_hrs: ticket.actualhrs, - cost_center: ticket.cost_center, - flat_rate: ticket.flat_rate, - rate: ticket.rate, - ticket_cost: ticket.flat_rate ? ticket.rate * ticket.productive_hrs : ticket.rate * ticket.actual_hrs - }); - }); - return timeTickets; -}; diff --git a/server/graphql-client/queries.js b/server/graphql-client/queries.js index 60a37eebd..e3b3b0977 100644 --- a/server/graphql-client/queries.js +++ b/server/graphql-client/queries.js @@ -1219,7 +1219,7 @@ query ENTEGRAL_EXPORT($bodyshopid: uuid!) { }`; exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uuid!, $end: timestamptz) { - bodyshops_by_pk(id: $bodyshopid){ + bodyshops_by_pk(id: $bodyshopid) { id shopname address1 @@ -1246,15 +1246,24 @@ exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uu bills { billlines { actual_cost + actual_price cost_center + deductedfromlbr id + line_desc quantity } + date federal_tax_rate id is_credit_memo + invoice_number + isinhouse local_tax_rate state_tax_rate + vendor { + name + } } created_at clm_no @@ -1296,7 +1305,7 @@ exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uu joblines(where: {removed: {_eq: false}}) { act_price alt_partno - billlines(order_by: {bill: {date: desc_nulls_last}} limit: 1) { + billlines(order_by: {bill: {date: desc_nulls_last}}, limit: 1) { actual_cost actual_price quantity @@ -1319,8 +1328,8 @@ exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uu mod_lbr_ty oem_partno op_code_desc - parts_order_lines(order_by: {parts_order: {order_date: desc_nulls_last}} limit: 1){ - parts_order{ + parts_order_lines(order_by: {parts_order: {order_date: desc_nulls_last}}, limit: 1) { + parts_order { id order_date } @@ -1339,6 +1348,14 @@ exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uu jobid totalliquidcost } + notes { + created_at + created_by + critical + private + text + type + } ownr_addr1 ownr_addr2 ownr_city @@ -2204,16 +2221,18 @@ 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 }` - : "" + : "" } }`; @@ -3152,11 +3171,10 @@ exports.DELETE_PHONE_NUMBER_OPT_OUT = ` } `; - exports.INSERT_MEDIA_ANALYTICS = ` mutation INSERT_MEDIA_ANALYTICS($mediaObject: media_analytics_insert_input!) { insert_media_analytics_one(object: $mediaObject) { id } } -` \ No newline at end of file +`;