| 1 |
/* global posmodel */ |
| 2 |
|
| 3 |
const getData = ({ lineProductName, productName, partnerName } = {}) => { |
| 4 |
const order = posmodel.models["pos.order"].find((o) => o.pos_reference.includes("device_sync")); |
| 5 |
|
| 6 |
let partner = null; |
| 7 |
if (partnerName) { |
| 8 |
partner = posmodel.models["res.partner"].find((p) => p.name === partnerName); |
| 9 |
} |
| 10 |
|
| 11 |
let line = null; |
| 12 |
if (lineProductName) { |
| 13 |
line = order.lines.find((l) => l.product_id.display_name === lineProductName); |
| 14 |
} |
| 15 |
|
| 16 |
let product = null; |
| 17 |
if (productName) { |
| 18 |
product = posmodel.models["product.product"].find((p) => p.display_name === productName); |
| 19 |
} |
| 20 |
|
| 21 |
return { order, line, product, partner }; |
| 22 |
}; |
| 23 |
|
| 24 |
const notify = async () => { |
| 25 |
const orm = posmodel.env.services.orm; |
| 26 |
await orm.call("pos.config", "notify_synchronisation", [ |
| 27 |
posmodel.config.id, |
| 28 |
posmodel.session.id, |
| 29 |
999, |
| 30 |
]); |
| 31 |
}; |
| 32 |
|
| 33 |
const getLineData = (product, order, quantity) => ({ |
| 34 |
name: product.display_name, |
| 35 |
order_id: order.id, |
| 36 |
product_id: product.id, |
| 37 |
price_unit: product.lst_price, |
| 38 |
price_subtotal: product.lst_price * quantity, |
| 39 |
price_subtotal_incl: product.lst_price * quantity, |
| 40 |
discount: 0, |
| 41 |
qty: quantity, |
| 42 |
}); |
| 43 |
|
| 44 |
// In the point-of-sale code, we consider that synchronization is necessary |
| 45 |
// when the write_date of the local order is smaller than that of the server. |
| 46 |
// To prevent the PoS from ignoring our synchronization. |
| 47 |
const writeOnOrder = async (order, data) => { |
| 48 |
const sec = new Date(order.write_date).getMilliseconds() + 1010; |
| 49 |
const timeout = Math.ceil(sec - new Date().getMilliseconds(), 0); |
| 50 |
await new Promise((res) => setTimeout(res, timeout)); |
| 51 |
const orm = posmodel.env.services.orm; |
| 52 |
await orm.write("pos.order", [order.id], data); |
| 53 |
await notify(); |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* @param {string} productName |
| 58 |
* @param {number} quantity |
| 59 |
* @returns StepSchema[] |
| 60 |
*/ |
| 61 |
export function createNewLine(productName, quantity) { |
| 62 |
return [ |
| 63 |
{ |
| 64 |
trigger: "body", |
| 65 |
run: async () => { |
| 66 |
const { order, product } = getData({ productName }); |
| 67 |
await writeOnOrder(order, { |
| 68 |
lines: [[0, 0, getLineData(product, order, quantity)]], |
| 69 |
}); |
| 70 |
}, |
| 71 |
}, |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @param {string} productName |
| 77 |
* @param {number} quantity |
| 78 |
* @returns StepSchema[] |
| 79 |
*/ |
| 80 |
export function changeLineQuantity(productName, quantity) { |
| 81 |
return [ |
| 82 |
{ |
| 83 |
trigger: "body", |
| 84 |
run: async () => { |
| 85 |
const { order, line } = getData({ lineProductName: productName }); |
| 86 |
await writeOnOrder(order, { |
| 87 |
lines: [[1, line.id, getLineData(line.product_id, order, quantity)]], |
| 88 |
}); |
| 89 |
}, |
| 90 |
}, |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param {string} partnerName |
| 96 |
* @returns StepSchema[] |
| 97 |
*/ |
| 98 |
export function changePartner(partnerName) { |
| 99 |
return [ |
| 100 |
{ |
| 101 |
trigger: "body", |
| 102 |
run: async () => { |
| 103 |
const { order, partner } = getData({ partnerName }); |
| 104 |
await writeOnOrder(order, { |
| 105 |
partner_id: partner.id, |
| 106 |
}); |
| 107 |
}, |
| 108 |
}, |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
export function markOrderAsPaid() { |
| 113 |
return [ |
| 114 |
{ |
| 115 |
trigger: "body", |
| 116 |
run: async () => { |
| 117 |
const { order } = getData({}); |
| 118 |
await writeOnOrder(order, { |
| 119 |
state: "paid", |
| 120 |
amount_paid: order.amount_total, |
| 121 |
amount_return: 0, |
| 122 |
amount_tax: 0, |
| 123 |
amount_total: 0, |
| 124 |
}); |
| 125 |
}, |
| 126 |
}, |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
export function createNewOrderOnTable(tableName, productTuple) { |
| 131 |
return [ |
| 132 |
{ |
| 133 |
trigger: "body", |
| 134 |
run: async () => { |
| 135 |
const orm = posmodel.env.services.orm; |
| 136 |
const prices = { |
| 137 |
amount_paid: 0, |
| 138 |
amount_return: 0, |
| 139 |
amount_tax: 0, |
| 140 |
amount_total: 0, |
| 141 |
}; |
| 142 |
const lines = productTuple.map(([productName, quantity]) => { |
| 143 |
const product = posmodel.models["product.product"].find( |
| 144 |
(p) => p.display_name === productName |
| 145 |
); |
| 146 |
const lineData = getLineData(product, false, quantity); |
| 147 |
prices.amount_paid += lineData.price_subtotal; |
| 148 |
prices.amount_return += lineData.price_subtotal; |
| 149 |
return [ |
| 150 |
0, |
| 151 |
0, |
| 152 |
{ |
| 153 |
...lineData, |
| 154 |
price_subtotal: lineData.price_subtotal, |
| 155 |
price_subtotal_incl: lineData.price_subtotal_incl, |
| 156 |
}, |
| 157 |
]; |
| 158 |
}); |
| 159 |
const table = posmodel.models["restaurant.table"].find( |
| 160 |
(t) => t.table_number === parseInt(tableName) |
| 161 |
); |
| 162 |
await orm.create("pos.order", [ |
| 163 |
{ |
| 164 |
...prices, |
| 165 |
pos_reference: `device_sync_${Math.floor(Math.random() * 9999)}`, |
| 166 |
session_id: posmodel.session.id, |
| 167 |
table_id: table.id, |
| 168 |
lines, |
| 169 |
}, |
| 170 |
]); |
| 171 |
await notify(); |
| 172 |
}, |
| 173 |
}, |
| 174 |
]; |
| 175 |
} |