| 1 |
export const changesToOrder = (order, orderPreparationCategories, cancelled = false) => { |
| 2 |
const toAdd = []; |
| 3 |
const toRemove = []; |
| 4 |
|
| 5 |
const orderChanges = getOrderChanges(order, orderPreparationCategories); |
| 6 |
const linesChanges = !cancelled |
| 7 |
? Object.values(orderChanges.orderlines) |
| 8 |
: Object.values(order.last_order_preparation_change.lines); |
| 9 |
|
| 10 |
for (const lineChange of linesChanges) { |
| 11 |
if (lineChange["quantity"] > 0 && !cancelled) { |
| 12 |
toAdd.push(lineChange); |
| 13 |
} else { |
| 14 |
lineChange["quantity"] = Math.abs(lineChange["quantity"]); // we need always positive values. |
| 15 |
toRemove.push(lineChange); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
return { |
| 20 |
new: toAdd, |
| 21 |
cancelled: toRemove, |
| 22 |
noteUpdate: Object.values(orderChanges.noteUpdate), |
| 23 |
general_customer_note: orderChanges.general_customer_note, |
| 24 |
internal_note: orderChanges.internal_note, |
| 25 |
}; |
| 26 |
}; |
| 27 |
|
| 28 |
/** |
| 29 |
* @returns {{ [lineKey: string]: { product_id: number, name: string, note: string, quantity: number } }} |
| 30 |
* This function recalculates the information to be sent to the preparation tools, |
| 31 |
* it uses the variable last_order_preparation_change which contains the last changes sent |
| 32 |
* to perform this calculation. |
| 33 |
*/ |
| 34 |
export const getOrderChanges = (order, orderPreparationCategories) => { |
| 35 |
const prepaCategoryIds = orderPreparationCategories; |
| 36 |
const oldChanges = order.last_order_preparation_change.lines; |
| 37 |
const changes = {}; |
| 38 |
const noteUpdate = {}; |
| 39 |
let changesCount = 0; |
| 40 |
let changeAbsCount = 0; |
| 41 |
|
| 42 |
const hasPreparationCategory = (product) => { |
| 43 |
if (!product) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
return product.parentPosCategIds.some((id) => prepaCategoryIds.has(id)); |
| 47 |
}; |
| 48 |
|
| 49 |
// Compares the orderlines of the order with the last ones sent. |
| 50 |
// When one of them has changed, we add the change. |
| 51 |
for (const orderline of order.getOrderlines()) { |
| 52 |
const product = orderline.getProduct(); |
| 53 |
const note = orderline.getNote(); |
| 54 |
const customerNote = orderline.getCustomerNote(); |
| 55 |
const lineKey = orderline.uuid; |
| 56 |
|
| 57 |
const hasPrepaCategory = |
| 58 |
hasPreparationCategory(product) || |
| 59 |
hasPreparationCategory(orderline.combo_parent_id?.product_id) || |
| 60 |
orderline.combo_line_ids?.some((line) => hasPreparationCategory(line.getProduct())) || |
| 61 |
false; |
| 62 |
|
| 63 |
if (hasPrepaCategory) { |
| 64 |
const key = Object.keys(order.last_order_preparation_change.lines).find((k) => |
| 65 |
k.startsWith(orderline.uuid) |
| 66 |
); // find old data but note changed |
| 67 |
const quantity = orderline.getQuantity(); |
| 68 |
|
| 69 |
const relatedKey = key !== lineKey ? key : lineKey; // if note update key would be different |
| 70 |
const quantityDiff = |
| 71 |
(oldChanges[relatedKey] ? quantity - oldChanges[relatedKey].quantity : quantity) || |
| 72 |
0; |
| 73 |
const noteChange = |
| 74 |
oldChanges[relatedKey] && |
| 75 |
(oldChanges[relatedKey].note !== note || |
| 76 |
oldChanges[relatedKey].customer_note !== customerNote); |
| 77 |
|
| 78 |
const lineDetails = { |
| 79 |
uuid: orderline.uuid, |
| 80 |
name: orderline.getFullProductName(), |
| 81 |
basic_name: orderline.product_id.name, |
| 82 |
isCombo: Boolean(orderline?.combo_line_ids?.length), |
| 83 |
combo_parent_uuid: orderline?.combo_parent_id?.uuid, |
| 84 |
product_id: product.id, |
| 85 |
attribute_value_names: orderline.attribute_value_ids.map((a) => a.name), |
| 86 |
quantity: quantityDiff, |
| 87 |
note: note, |
| 88 |
customer_note: customerNote, |
| 89 |
pos_categ_id: product.pos_categ_ids[0]?.id ?? 0, |
| 90 |
pos_categ_sequence: product.pos_categ_ids[0]?.sequence ?? 0, |
| 91 |
display_name: product.display_name, |
| 92 |
group: receiptLineGrouper.getGroup(orderline), |
| 93 |
}; |
| 94 |
|
| 95 |
if (quantityDiff) { |
| 96 |
// if note update with qty add |
| 97 |
changes[lineKey] = lineDetails; |
| 98 |
changesCount += quantityDiff; |
| 99 |
changeAbsCount += Math.abs(quantityDiff); |
| 100 |
if (noteChange) { |
| 101 |
lineDetails.quantity = oldChanges[relatedKey].quantity || 0; |
| 102 |
noteUpdate[lineKey] = lineDetails; |
| 103 |
} |
| 104 |
|
| 105 |
orderline.setHasChange(true); |
| 106 |
} else { |
| 107 |
if (quantityDiff) { |
| 108 |
orderline.setHasChange(false); |
| 109 |
} else { |
| 110 |
// If only note updated |
| 111 |
if (noteChange) { |
| 112 |
lineDetails.quantity = orderline.qty; |
| 113 |
noteUpdate[lineKey] = lineDetails; |
| 114 |
orderline.setHasChange(true); |
| 115 |
changesCount += 1; |
| 116 |
} else { |
| 117 |
orderline.setHasChange(false); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} else { |
| 122 |
orderline.setHasChange(false); |
| 123 |
} |
| 124 |
} |
| 125 |
// Checks whether an orderline has been deleted from the order since it |
| 126 |
// was last sent to the preparation tools. If so we add this to the changes. |
| 127 |
for (const [lineKey, lineResume] of Object.entries(order.last_order_preparation_change.lines)) { |
| 128 |
if (!order.models["pos.order.line"].getBy("uuid", lineResume["uuid"])) { |
| 129 |
const quantity = isNaN(lineResume["quantity"]) ? 0 : lineResume["quantity"]; |
| 130 |
if (!changes[lineKey]) { |
| 131 |
changes[lineKey] = { |
| 132 |
uuid: lineResume["uuid"], |
| 133 |
product_id: lineResume["product_id"], |
| 134 |
name: lineResume["name"], |
| 135 |
basic_name: lineResume["basic_name"], |
| 136 |
display_name: lineResume["display_name"], |
| 137 |
isCombo: Boolean(lineResume["isCombo"]), |
| 138 |
combo_parent_uuid: lineResume["combo_parent_uuid"], |
| 139 |
note: lineResume["note"], |
| 140 |
customer_note: lineResume["customer_note"], |
| 141 |
attribute_value_names: lineResume["attribute_value_names"], |
| 142 |
group: lineResume["group"], |
| 143 |
quantity: -quantity, |
| 144 |
}; |
| 145 |
changeAbsCount += Math.abs(quantity); |
| 146 |
changesCount += quantity; |
| 147 |
} else { |
| 148 |
changes[lineKey]["quantity"] -= quantity; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
const result = { |
| 154 |
nbrOfChanges: changeAbsCount, |
| 155 |
noteUpdate: noteUpdate, |
| 156 |
orderlines: changes, |
| 157 |
count: changesCount, |
| 158 |
}; |
| 159 |
|
| 160 |
// if `generalCustomerNote` key is present, then there is a change in the generalCustomerNote |
| 161 |
const lastGeneralCustomerNote = order.last_order_preparation_change.general_customer_note || ""; |
| 162 |
if (lastGeneralCustomerNote !== order.general_customer_note) { |
| 163 |
result.general_customer_note = order.general_customer_note; |
| 164 |
} |
| 165 |
const lastInternalNote = order.last_order_preparation_change.internal_note || ""; |
| 166 |
if (lastInternalNote !== order.internal_note) { |
| 167 |
result.internal_note = order.internal_note; |
| 168 |
} |
| 169 |
return result; |
| 170 |
}; |
| 171 |
|
| 172 |
export const receiptLineGrouper = { |
| 173 |
getGroup(orderLine) { |
| 174 |
// To be overridden |
| 175 |
}, |
| 176 |
}; |