| 1 |
|
| 2 |
|
| 3 |
import { _t } from "@web/core/l10n/translation"; |
| 4 |
import { renderToElement } from "@web/core/utils/render"; |
| 5 |
|
| 6 |
export async function generateReceiptsToPrint(order, orderChange) { |
| 7 |
const { orderData, changes } = posmodel.generateOrderChange( |
| 8 |
order, |
| 9 |
orderChange, |
| 10 |
Array.from(posmodel.config.printerCategories), |
| 11 |
false |
| 12 |
); |
| 13 |
const receiptsData = await posmodel.generateReceiptsDataToPrint( |
| 14 |
orderData, |
| 15 |
changes, |
| 16 |
orderChange |
| 17 |
); |
| 18 |
const groupedReceiptsData = await posmodel.prepareReceiptGroupedData(receiptsData); |
| 19 |
return groupedReceiptsData.map((data) => |
| 20 |
renderToElement("point_of_sale.OrderChangeReceipt", { |
| 21 |
data: data, |
| 22 |
}) |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
export async function generatePreparationReceipts() { |
| 28 |
const order = posmodel.getOrder(); |
| 29 |
const orderChange = posmodel.changesToOrder(order, posmodel.config.printerCategories, false); |
| 30 |
return await generateReceiptsToPrint(order, orderChange); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
export async function generateFireCourseReceipts() { |
| 35 |
const order = posmodel.getOrder(); |
| 36 |
const course = order.getSelectedCourse(); |
| 37 |
const orderChange = { |
| 38 |
new: [], |
| 39 |
cancelled: [], |
| 40 |
noteUpdate: course.lines.map((line) => ({ product_id: line.getProduct().id })), |
| 41 |
noteUpdateTitle: _t("Course %s fired", "" + course.index), |
| 42 |
printNoteUpdateData: false, |
| 43 |
}; |
| 44 |
return await generateReceiptsToPrint(order, orderChange); |
| 45 |
} |
| 46 |
|
| 47 |
export function checkPreparationTicketData( |
| 48 |
data, |
| 49 |
opts = { |
| 50 |
visibleInDom: [], |
| 51 |
invisibleInDom: [], |
| 52 |
lineOrder: [], |
| 53 |
fireCourse: false, |
| 54 |
} |
| 55 |
) { |
| 56 |
const check = async () => { |
| 57 |
let tickets = []; |
| 58 |
|
| 59 |
if (opts.fireCourse) { |
| 60 |
tickets = await generateFireCourseReceipts(); |
| 61 |
} else { |
| 62 |
tickets = await generatePreparationReceipts(); |
| 63 |
} |
| 64 |
|
| 65 |
if ( |
| 66 |
!tickets[0] && |
| 67 |
!data.length && |
| 68 |
!opts.invisibleInDom?.length && |
| 69 |
!opts.visibleInDom?.length && |
| 70 |
!opts.lineOrder?.length && |
| 71 |
!opts.fireCourse |
| 72 |
) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
const lines = tickets[0].querySelectorAll(".orderline"); |
| 77 |
const lineNames = []; |
| 78 |
|
| 79 |
let idx = 0; |
| 80 |
for (const line of lines) { |
| 81 |
const name = line.firstChild.children[1].innerHTML; |
| 82 |
const qty = line.firstChild.children[0].innerHTML; |
| 83 |
const domAttrs = Object.values(line.children[1]?.children || []); |
| 84 |
const attrs = domAttrs.map((c) => c.innerHTML).filter(Boolean); |
| 85 |
const values = data[idx]; |
| 86 |
|
| 87 |
if (values.qty != qty) { |
| 88 |
throw new Error( |
| 89 |
`Ticket data mismatch for ${name}: expected ${values.qty}, got ${qty}` |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
if (values.name != name) { |
| 94 |
throw new Error( |
| 95 |
`Ticket data mismatch for ${name}: expected ${values.name}, got ${name}, maybe lines ordering ?` |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
if (values.attributes) { |
| 100 |
for (const attr of values.attributes) { |
| 101 |
const found = attrs.find((a) => a.includes(attr)); |
| 102 |
if (!found) { |
| 103 |
throw new Error( |
| 104 |
`Attribute ${attr} not found in printed receipt for ${name}` |
| 105 |
); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (!values) { |
| 111 |
throw new Error(`Received ${name} but no check data found`); |
| 112 |
} |
| 113 |
|
| 114 |
lineNames.push(name); |
| 115 |
idx++; |
| 116 |
} |
| 117 |
|
| 118 |
if (opts.visibleInDom) { |
| 119 |
for (const inDom of opts.visibleInDom) { |
| 120 |
let found = false; |
| 121 |
for (const ticket of tickets) { |
| 122 |
if (ticket.innerHTML.includes(inDom)) { |
| 123 |
found = true; |
| 124 |
} |
| 125 |
} |
| 126 |
if (!found) { |
| 127 |
throw new Error(`${inDom} not found in printed receipt`); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if (opts.invisibleInDom) { |
| 133 |
for (const notInDom of opts.invisibleInDom) { |
| 134 |
for (const ticket of tickets) { |
| 135 |
if (ticket.innerHTML.includes(notInDom)) { |
| 136 |
throw new Error(`${notInDom} should not be in printed receipt`); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
return [ |
| 144 |
{ |
| 145 |
trigger: "body", |
| 146 |
run: async () => await check(), |
| 147 |
}, |
| 148 |
]; |
| 149 |
} |