| 1 |
import { registry } from "@web/core/registry"; |
| 2 |
import { useService } from "@web/core/utils/hooks"; |
| 3 |
import { parseDateTime } from "@web/core/l10n/dates"; |
| 4 |
import { parseFloat } from "@web/views/fields/parsers"; |
| 5 |
import { _t } from "@web/core/l10n/translation"; |
| 6 |
import { AlertDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 7 |
import { ActionpadWidget } from "@point_of_sale/app/screens/product_screen/action_pad/action_pad"; |
| 8 |
import { BackButton } from "@point_of_sale/app/screens/product_screen/action_pad/back_button/back_button"; |
| 9 |
import { InvoiceButton } from "@point_of_sale/app/screens/ticket_screen/invoice_button/invoice_button"; |
| 10 |
import { Orderline } from "@point_of_sale/app/components/orderline/orderline"; |
| 11 |
import { CenteredIcon } from "@point_of_sale/app/components/centered_icon/centered_icon"; |
| 12 |
import { SearchBar } from "@point_of_sale/app/screens/ticket_screen/search_bar/search_bar"; |
| 13 |
import { usePos } from "@point_of_sale/app/hooks/pos_hook"; |
| 14 |
import { Component, onMounted, onWillStart, useState } from "@odoo/owl"; |
| 15 |
import { |
| 16 |
BACKSPACE, |
| 17 |
Numpad, |
| 18 |
getButtons, |
| 19 |
ZERO, |
| 20 |
DECIMAL, |
| 21 |
} from "@point_of_sale/app/components/numpad/numpad"; |
| 22 |
import { PosOrderLineRefund } from "@point_of_sale/app/models/pos_order_line_refund"; |
| 23 |
import { fuzzyLookup } from "@web/core/utils/search"; |
| 24 |
import { useTrackedAsync } from "@point_of_sale/app/hooks/hooks"; |
| 25 |
import { OrderDisplay } from "@point_of_sale/app/components/order_display/order_display"; |
| 26 |
import { BarcodeVideoScanner } from "@web/core/barcode/barcode_video_scanner"; |
| 27 |
import { makeAwaitable } from "@point_of_sale/app/utils/make_awaitable_dialog"; |
| 28 |
import { NumberPopup } from "@point_of_sale/app/components/popups/number_popup/number_popup"; |
| 29 |
import { ConnectionLostError } from "@web/core/network/rpc"; |
| 30 |
|
| 31 |
const { DateTime } = luxon; |
| 32 |
const NBR_BY_PAGE = 30; |
| 33 |
|
| 34 |
export class TicketScreen extends Component { |
| 35 |
static storeOnOrder = false; |
| 36 |
static template = "point_of_sale.TicketScreen"; |
| 37 |
static components = { |
| 38 |
ActionpadWidget, |
| 39 |
InvoiceButton, |
| 40 |
Orderline, |
| 41 |
OrderDisplay, |
| 42 |
CenteredIcon, |
| 43 |
SearchBar, |
| 44 |
Numpad, |
| 45 |
BackButton, |
| 46 |
BarcodeVideoScanner, |
| 47 |
}; |
| 48 |
static props = { |
| 49 |
reuseSavedUIState: { type: Boolean, optional: true }, |
| 50 |
stateOverride: { type: Object, optional: true }, |
| 51 |
}; |
| 52 |
static defaultProps = { |
| 53 |
// When passed as true, it will use the saved _state.ui as default |
| 54 |
// value when this component is reinstantiated. |
| 55 |
// After setting the default value, the _state.ui will be overridden |
| 56 |
// by the passed props.ui if there is any. |
| 57 |
reuseSavedUIState: false, |
| 58 |
ui: {}, |
| 59 |
}; |
| 60 |
|
| 61 |
setup() { |
| 62 |
this.pos = usePos(); |
| 63 |
this.ui = useService("ui"); |
| 64 |
this.dialog = useService("dialog"); |
| 65 |
this.numberBuffer = useService("number_buffer"); |
| 66 |
this.doPrint = useTrackedAsync((_selectedSyncedOrder) => this.print(_selectedSyncedOrder)); |
| 67 |
this.numberBuffer.use({ |
| 68 |
triggerAtInput: (event) => this._onUpdateSelectedOrderline(event), |
| 69 |
}); |
| 70 |
|
| 71 |
this.state = useState({ |
| 72 |
nbrByPage: NBR_BY_PAGE, |
| 73 |
page: 1, |
| 74 |
nbrPage: 1, |
| 75 |
filter: null, |
| 76 |
search: this.pos.getDefaultSearchDetails(), |
| 77 |
selectedOrderUuid: this.pos.getOrder()?.uuid || null, |
| 78 |
selectedOrderlineIds: {}, |
| 79 |
selectedPreset: null, |
| 80 |
}); |
| 81 |
Object.assign(this.state, this.props.stateOverride || {}); |
| 82 |
|
| 83 |
onMounted(this.onMounted); |
| 84 |
onWillStart(async () => { |
| 85 |
if (!this.pos.loadingOrderState) { |
| 86 |
try { |
| 87 |
this.pos.loadingOrderState = true; |
| 88 |
await this.pos.getServerOrders(); |
| 89 |
} catch (error) { |
| 90 |
if (error instanceof ConnectionLostError) { |
| 91 |
Promise.reject(error); |
| 92 |
return error; |
| 93 |
} |
| 94 |
throw error; |
| 95 |
} finally { |
| 96 |
this.pos.loadingOrderState = false; |
| 97 |
} |
| 98 |
} |
| 99 |
}); |
| 100 |
} |
| 101 |
onMounted() { |
| 102 |
setTimeout(() => { |
| 103 |
// Show updated list of synced orders when going back to the screen. |
| 104 |
this.onFilterSelected(this.state.filter); |
| 105 |
}); |
| 106 |
} |
| 107 |
async onClickPageNbr() { |
| 108 |
const nbr = await makeAwaitable(this.dialog, NumberPopup, { |
| 109 |
title: _t("Number by page"), |
| 110 |
isValid: (value) => value > 0 && value <= 100, |
| 111 |
}); |
| 112 |
|
| 113 |
if (nbr && !isNaN(nbr)) { |
| 114 |
this.state.nbrByPage = parseInt(nbr); |
| 115 |
this.state.page = 1; |
| 116 |
if (this.state.filter == "SYNCED") { |
| 117 |
await this._fetchSyncedOrders(); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
onPresetSelected(preset) { |
| 122 |
if (this.state.selectedPreset === preset) { |
| 123 |
this.state.selectedPreset = null; |
| 124 |
} else { |
| 125 |
this.state.selectedPreset = preset; |
| 126 |
const firstFilteredOrder = this.getFilteredOrderList()[0]; |
| 127 |
|
| 128 |
if (firstFilteredOrder) { |
| 129 |
this.onClickOrder(firstFilteredOrder); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
async print(order) { |
| 134 |
await this.pos.printReceipt({ order: order }); |
| 135 |
} |
| 136 |
async onFilterSelected(selectedFilter) { |
| 137 |
this.state.filter = selectedFilter; |
| 138 |
this.pos.screenState.ticketSCreen.totalCount = 0; |
| 139 |
this.pos.screenState.ticketSCreen.offsetByDomain = {}; |
| 140 |
|
| 141 |
if (this.state.filter == "SYNCED") { |
| 142 |
await this._fetchSyncedOrders(); |
| 143 |
} |
| 144 |
} |
| 145 |
getNumpadButtons() { |
| 146 |
return getButtons( |
| 147 |
[{ value: "-", text: "+/-", disabled: true }, ZERO, DECIMAL], |
| 148 |
[ |
| 149 |
{ value: "quantity", text: _t("Qty"), class: "active border-primary" }, |
| 150 |
{ value: "discount", text: _t("% Disc"), disabled: true }, |
| 151 |
{ value: "price", text: _t("Price"), disabled: true }, |
| 152 |
BACKSPACE, |
| 153 |
] |
| 154 |
); |
| 155 |
} |
| 156 |
async onClickScanOrder(qrcode) { |
| 157 |
if (qrcode) { |
| 158 |
const uuid = new URL(qrcode).searchParams.get("order_uuid"); |
| 159 |
const orders = await this.pos.data.loadServerOrders([["uuid", "=", uuid]]); |
| 160 |
const order = orders[0]; |
| 161 |
if (order) { |
| 162 |
this.state.filter = "SYNCED"; |
| 163 |
this.setSelectedOrder(order); |
| 164 |
this.pos.scanning = !this.pos.scanning; |
| 165 |
} else { |
| 166 |
this.env.services.notification.add(_t("Invalid QR Code! Please, Scan again!"), { |
| 167 |
type: "warning", |
| 168 |
}); |
| 169 |
} |
| 170 |
} else { |
| 171 |
this.env.services.notification.add(_t("Please, Scan again!"), { |
| 172 |
type: "warning", |
| 173 |
}); |
| 174 |
} |
| 175 |
} |
| 176 |
async onSearch(search) { |
| 177 |
this.state.search = search; |
| 178 |
this.state.page = 1; |
| 179 |
if (this.state.filter == "SYNCED") { |
| 180 |
await this._fetchSyncedOrders(); |
| 181 |
} |
| 182 |
} |
| 183 |
onClickOrder(clickedOrder) { |
| 184 |
this.setSelectedOrder(clickedOrder); |
| 185 |
this.numberBuffer.reset(); |
| 186 |
if ((!clickedOrder || clickedOrder.finalized) && !this.getSelectedOrderlineId()) { |
| 187 |
// Automatically select the first orderline of the selected order. |
| 188 |
const firstLine = this.getSelectedOrder().getOrderlines()[0]; |
| 189 |
if (firstLine) { |
| 190 |
this.state.selectedOrderlineIds[clickedOrder.id] = firstLine.id; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
async onClickReprintAll(order) { |
| 195 |
const printingChanges = order.uiState?.lastPrints; |
| 196 |
if (printingChanges) { |
| 197 |
await this.pos.printChanges(order, printingChanges, true); |
| 198 |
} |
| 199 |
} |
| 200 |
async onNextPage() { |
| 201 |
if (this.state.page < this.getNbrPages()) { |
| 202 |
this.state.page += 1; |
| 203 |
if (this.state.filter == "SYNCED") { |
| 204 |
await this._fetchSyncedOrders(); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
async onPrevPage() { |
| 209 |
if (this.state.page > 1) { |
| 210 |
this.state.page -= 1; |
| 211 |
if (this.state.filter == "SYNCED") { |
| 212 |
await this._fetchSyncedOrders(); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
async onInvoiceOrder(orderId) { |
| 217 |
const order = this.pos.models["pos.order"].get(orderId); |
| 218 |
this.setSelectedOrder(order); |
| 219 |
} |
| 220 |
onClickOrderline(orderline) { |
| 221 |
if (this.getSelectedOrder()?.finalized) { |
| 222 |
const order = this.getSelectedOrder(); |
| 223 |
this.state.selectedOrderlineIds[order.id] = orderline.id; |
| 224 |
this.numberBuffer.reset(); |
| 225 |
} |
| 226 |
} |
| 227 |
onClickRefundOrderUid(orderUuid) { |
| 228 |
// Open the refund order. |
| 229 |
const refundOrder = this.pos.models["pos.order"].find((order) => order.uuid == orderUuid); |
| 230 |
if (refundOrder) { |
| 231 |
this.setOrder(refundOrder); |
| 232 |
} |
| 233 |
} |
| 234 |
_onUpdateSelectedOrderline({ key, buffer }) { |
| 235 |
const order = this.getSelectedOrder(); |
| 236 |
if (!order) { |
| 237 |
return this.numberBuffer.reset(); |
| 238 |
} |
| 239 |
|
| 240 |
const selectedOrderlineId = this.getSelectedOrderlineId(); |
| 241 |
const orderline = order.lines.find((line) => line.id == selectedOrderlineId); |
| 242 |
if (!orderline) { |
| 243 |
return this.numberBuffer.reset(); |
| 244 |
} |
| 245 |
|
| 246 |
const toRefundDetails = orderline |
| 247 |
.getAllLinesInCombo() |
| 248 |
.map((line) => this.getToRefundDetail(line)); |
| 249 |
for (const toRefundDetail of toRefundDetails) { |
| 250 |
// When already linked to an order, do not modify the to refund quantity. |
| 251 |
if (toRefundDetail.destionation_order_id) { |
| 252 |
return this.numberBuffer.reset(); |
| 253 |
} |
| 254 |
|
| 255 |
const refundableQty = toRefundDetail.line.qty - toRefundDetail.line.refundedQty; |
| 256 |
if (refundableQty <= 0) { |
| 257 |
return this.numberBuffer.reset(); |
| 258 |
} |
| 259 |
|
| 260 |
if (buffer == null || buffer == "") { |
| 261 |
toRefundDetail.qty = 0; |
| 262 |
} else { |
| 263 |
const quantity = Math.abs(parseFloat(buffer)); |
| 264 |
if (quantity > refundableQty) { |
| 265 |
this.numberBuffer.reset(); |
| 266 |
if (!toRefundDetail.line.combo_parent_id) { |
| 267 |
this.dialog.add(AlertDialog, { |
| 268 |
title: _t("Maximum Exceeded"), |
| 269 |
body: _t( |
| 270 |
"The requested quantity to be refunded is higher than the ordered quantity. %s is requested while only %s can be refunded.", |
| 271 |
quantity, |
| 272 |
refundableQty |
| 273 |
), |
| 274 |
}); |
| 275 |
} |
| 276 |
} else { |
| 277 |
toRefundDetail.qty = quantity; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
async addAdditionalRefundInfo(order, destinationOrder) { |
| 283 |
// used by L10N, e.g: add a refund reason using a specific L10N field |
| 284 |
return Promise.resolve(); |
| 285 |
} |
| 286 |
// Used to override inside `pos_blackbox_be` and `pos_urban_piper` |
| 287 |
async _doneOrder(order) { |
| 288 |
return; |
| 289 |
} |
| 290 |
async onDoRefund() { |
| 291 |
const order = this.getSelectedOrder(); |
| 292 |
|
| 293 |
if (order && this._doesOrderHaveSoleItem(order)) { |
| 294 |
if (!this._prepareAutoRefundOnOrder(order)) { |
| 295 |
// Don't proceed on refund if preparation returned false. |
| 296 |
return; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if (!order || !this.getHasItemsToRefund()) { |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
const partner = order.getPartner(); |
| 305 |
// The order that will contain the refund orderlines. |
| 306 |
// We select the order if it is empty, else we create a new one. |
| 307 |
const destinationOrder = this._getEmptyOrder(partner); |
| 308 |
|
| 309 |
destinationOrder.is_refund = true; |
| 310 |
destinationOrder.pricelist_id = order.pricelist_id; |
| 311 |
// Add orderline for each toRefundDetail to the destinationOrder. |
| 312 |
const lines = []; |
| 313 |
for (const refundDetail of this._getRefundableDetails(partner, order)) { |
| 314 |
const refundLine = refundDetail.line; |
| 315 |
const alreadyRefundedLots = refundLine.refund_orderline_ids |
| 316 |
.filter((item) => !["cancel", "draft"].includes(item.order_id.state)) |
| 317 |
.flatMap((item) => item.pack_lot_ids) |
| 318 |
.map((pack_lot) => pack_lot.lot_name); |
| 319 |
const options = refundLine.pack_lot_ids |
| 320 |
.map((p) => p.lot_name) |
| 321 |
.filter((lotName) => !alreadyRefundedLots.includes(lotName)); |
| 322 |
const line = this.pos.models["pos.order.line"].create({ |
| 323 |
qty: -refundDetail.qty, |
| 324 |
price_unit: refundLine.price_unit, |
| 325 |
product_id: refundLine.product_id, |
| 326 |
order_id: destinationOrder, |
| 327 |
discount: refundLine.discount, |
| 328 |
tax_ids: refundLine.tax_ids.map((tax) => ["link", tax]), |
| 329 |
refunded_orderline_id: refundLine, |
| 330 |
// Only include as many pack_lot_ids as the refunded quantity requires. |
| 331 |
pack_lot_ids: options |
| 332 |
.slice(0, refundDetail.qty) |
| 333 |
.map((lotName) => ["create", { lot_name: lotName }]), |
| 334 |
price_type: "automatic", |
| 335 |
attribute_value_ids: refundLine.attribute_value_ids.map((attr) => ["link", attr]), |
| 336 |
}); |
| 337 |
lines.push(line); |
| 338 |
refundDetail.destination_order_uuid = destinationOrder.uuid; |
| 339 |
} |
| 340 |
// link the refund combo lines |
| 341 |
const refundComboParentLines = lines.filter( |
| 342 |
(l) => l.refunded_orderline_id.combo_line_ids.length > 0 |
| 343 |
); |
| 344 |
for (const refundComboParent of refundComboParentLines) { |
| 345 |
const children = refundComboParent.refunded_orderline_id.combo_line_ids |
| 346 |
.map((l) => l.refund_orderline_ids) |
| 347 |
.flat(); |
| 348 |
refundComboParent.combo_line_ids = [["link", ...children]]; |
| 349 |
} |
| 350 |
|
| 351 |
//Add a check too see if the fiscal position exist in the pos |
| 352 |
if (order.fiscal_position_not_found) { |
| 353 |
this.dialog.add(AlertDialog, { |
| 354 |
title: _t("Fiscal Position not found"), |
| 355 |
body: _t( |
| 356 |
"The fiscal position used in the original order is not loaded. Make sure it is loaded by adding it in the pos configuration." |
| 357 |
), |
| 358 |
}); |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
if (order.fiscal_position_id) { |
| 363 |
destinationOrder.fiscal_position_id = order.fiscal_position_id; |
| 364 |
} |
| 365 |
// Set the partner to the destinationOrder. |
| 366 |
this.setPartnerToRefundOrder(partner, destinationOrder); |
| 367 |
destinationOrder.refunded_order_id = order; |
| 368 |
this.pos.setOrder(destinationOrder); |
| 369 |
await this.addAdditionalRefundInfo(order, destinationOrder); |
| 370 |
|
| 371 |
this.postRefund(destinationOrder); |
| 372 |
this.pos.ticket_screen_mobile_pane = "left"; |
| 373 |
destinationOrder.setScreenData({ name: "PaymentScreen" }); |
| 374 |
this.pos.navigate("PaymentScreen", { orderUuid: destinationOrder.uuid }); |
| 375 |
} |
| 376 |
|
| 377 |
async onDeleteOrder(order) { |
| 378 |
await this.pos.onDeleteOrder(order); |
| 379 |
this.setSelectedOrder(this.pos.getOrder()); |
| 380 |
} |
| 381 |
|
| 382 |
postRefund(destinationOrder) {} |
| 383 |
|
| 384 |
setPartnerToRefundOrder(partner, destinationOrder) { |
| 385 |
if (partner && !destinationOrder.getPartner()) { |
| 386 |
destinationOrder.setPartner(partner); |
| 387 |
} |
| 388 |
} |
| 389 |
setSelectedOrder(order) { |
| 390 |
this.state.selectedOrderUuid = order?.uuid || null; |
| 391 |
} |
| 392 |
getSelectedOrder() { |
| 393 |
return this.pos.models["pos.order"].getBy("uuid", this.state.selectedOrderUuid) || null; |
| 394 |
} |
| 395 |
getSelectedOrderlineId() { |
| 396 |
if (this.getSelectedOrder()) { |
| 397 |
return this.state.selectedOrderlineIds[this.getSelectedOrder().id]; |
| 398 |
} |
| 399 |
} |
| 400 |
get isOrderSynced() { |
| 401 |
return ( |
| 402 |
this.getSelectedOrder()?.finalized && |
| 403 |
(this.getSelectedOrder().getScreenData().name === "" || this.state.filter === "SYNCED") |
| 404 |
); |
| 405 |
} |
| 406 |
activeOrderFilter(o) { |
| 407 |
const screen = ["ReceiptScreen", "TipScreen"]; |
| 408 |
const oScreen = o.getScreenData(); |
| 409 |
return (!o.finalized || screen.includes(oScreen.name)) && o.uiState.displayed; |
| 410 |
} |
| 411 |
getFilteredOrderList() { |
| 412 |
const orderModel = this.pos.models["pos.order"]; |
| 413 |
let orders = |
| 414 |
this.state.filter === "SYNCED" |
| 415 |
? orderModel.filter((o) => o.finalized && o.uiState.displayed) |
| 416 |
: orderModel.filter(this.activeOrderFilter); |
| 417 |
|
| 418 |
if (this.state.filter && !["ACTIVE_ORDERS", "SYNCED"].includes(this.state.filter)) { |
| 419 |
orders = orders.filter((order) => { |
| 420 |
const screen = order.getScreenData(); |
| 421 |
return this._getScreenToStatusMap()[screen.name] === this.state.filter; |
| 422 |
}); |
| 423 |
} |
| 424 |
|
| 425 |
if (this.state.search.searchTerm) { |
| 426 |
const repr = this._getSearchFields()[this.state.search.fieldName].repr; |
| 427 |
orders = fuzzyLookup(this.state.search.searchTerm, orders, repr); |
| 428 |
} |
| 429 |
if (this.state.search.partnerId && this.state.search.fieldName === "PARTNER") { |
| 430 |
orders = orders.filter((order) => order.partner_id?.id === this.state.search.partnerId); |
| 431 |
} |
| 432 |
|
| 433 |
if (this.state.selectedPreset) { |
| 434 |
orders = orders.filter((order) => order.preset_id?.id === this.state.selectedPreset.id); |
| 435 |
} |
| 436 |
|
| 437 |
const sortOrders = (orders, ascending = false) => |
| 438 |
orders.sort((a, b) => { |
| 439 |
const dateA = a.date_order; |
| 440 |
const dateB = b.date_order; |
| 441 |
|
| 442 |
if (!dateA.equals(dateB)) { |
| 443 |
return ascending ? dateA - dateB : dateB - dateA; |
| 444 |
} else { |
| 445 |
const nameA = parseInt(a.pos_reference.replace(/\D/g, "")) || 0; |
| 446 |
const nameB = parseInt(b.pos_reference.replace(/\D/g, "")) || 0; |
| 447 |
return ascending ? nameA - nameB : nameB - nameA; |
| 448 |
} |
| 449 |
}); |
| 450 |
|
| 451 |
if (this.state.filter === "SYNCED") { |
| 452 |
return sortOrders(orders).slice( |
| 453 |
(this.state.page - 1) * this.state.nbrByPage, |
| 454 |
this.state.page * this.state.nbrByPage |
| 455 |
); |
| 456 |
} else { |
| 457 |
this.pos.screenState.ticketSCreen.totalCount = orders.length; |
| 458 |
return sortOrders(orders, true).slice( |
| 459 |
(this.state.page - 1) * this.state.nbrByPage, |
| 460 |
this.state.page * this.state.nbrByPage |
| 461 |
); |
| 462 |
} |
| 463 |
} |
| 464 |
getDate(order) { |
| 465 |
return this.pos.getDate(order.date_order); |
| 466 |
} |
| 467 |
getTotal(order) { |
| 468 |
return this.env.utils.formatCurrency(order.priceIncl); |
| 469 |
} |
| 470 |
getPartner(order) { |
| 471 |
return order.getPartnerName(); |
| 472 |
} |
| 473 |
getCardholderName(order) { |
| 474 |
return order.getCardHolderName(); |
| 475 |
} |
| 476 |
getCashier(order) { |
| 477 |
return order.employee_id ? order.employee_id.name : ""; |
| 478 |
} |
| 479 |
getStatus(order) { |
| 480 |
if ( |
| 481 |
order.finalized && |
| 482 |
(order.getScreenData().name === "" || this.state.filter === "SYNCED") |
| 483 |
) { |
| 484 |
return _t("Paid"); |
| 485 |
} else { |
| 486 |
const screen = order.getScreenData(); |
| 487 |
return this._getOrderStates().get(this._getScreenToStatusMap()[screen.name])?.text; |
| 488 |
} |
| 489 |
} |
| 490 |
/** |
| 491 |
* If the order is the only order and is empty |
| 492 |
*/ |
| 493 |
isDefaultOrderEmpty(order) { |
| 494 |
const status = this._getScreenToStatusMap()[order.getScreenData().name]; |
| 495 |
const productScreenStatus = this._getScreenToStatusMap().ProductScreen; |
| 496 |
return ( |
| 497 |
order.getOrderlines().length === 0 && |
| 498 |
this.pos.getOpenOrders().length === 1 && |
| 499 |
status === productScreenStatus && |
| 500 |
order.payment_ids.length === 0 |
| 501 |
); |
| 502 |
} |
| 503 |
/** |
| 504 |
* Hide the delete button if one of the payments is a 'done' electronic payment. |
| 505 |
*/ |
| 506 |
shouldHideDeleteButton(order) { |
| 507 |
const orders = this.pos.models["pos.order"].filter((o) => !o.finalized); |
| 508 |
return ( |
| 509 |
(orders.length === 1 && orders[0].lines.length === 0) || |
| 510 |
(this.ui.isSmall && order != this.getSelectedOrder()) || |
| 511 |
this.isDefaultOrderEmpty(order) || |
| 512 |
order.finalized || |
| 513 |
order.payment_ids.some( |
| 514 |
(payment) => payment.isElectronic() && payment.getPaymentStatus() === "done" |
| 515 |
) || |
| 516 |
order.finalized |
| 517 |
); |
| 518 |
} |
| 519 |
isHighlighted(order) { |
| 520 |
const selectedOrder = this.getSelectedOrder(); |
| 521 |
return selectedOrder ? order.id && order.id == selectedOrder.id : false; |
| 522 |
} |
| 523 |
showCardholderName() { |
| 524 |
return this.pos.models["pos.payment.method"].some((method) => method.use_payment_terminal); |
| 525 |
} |
| 526 |
getSearchBarConfig() { |
| 527 |
return { |
| 528 |
searchFields: new Map( |
| 529 |
Object.entries(this._getSearchFields()).map(([key, val]) => [key, val.displayName]) |
| 530 |
), |
| 531 |
filter: { show: true, options: this._getFilterOptions() }, |
| 532 |
defaultSearchDetails: this.state.search, |
| 533 |
defaultFilter: this.state.filter, |
| 534 |
}; |
| 535 |
} |
| 536 |
getNbrPages() { |
| 537 |
return Math.ceil(this.pos.screenState.ticketSCreen.totalCount / this.state.nbrByPage); |
| 538 |
} |
| 539 |
getPageNumber() { |
| 540 |
if (!this.pos.screenState.ticketSCreen.totalCount) { |
| 541 |
return `0/0`; |
| 542 |
} else { |
| 543 |
return `${(this.state.page - 1) * this.state.nbrByPage + 1}-${Math.min( |
| 544 |
this.state.page * this.state.nbrByPage, |
| 545 |
this.pos.screenState.ticketSCreen.totalCount |
| 546 |
)} / ${this.pos.screenState.ticketSCreen.totalCount}`; |
| 547 |
} |
| 548 |
} |
| 549 |
getHasItemsToRefund() { |
| 550 |
const order = this.getSelectedOrder(); |
| 551 |
if (!order) { |
| 552 |
return false; |
| 553 |
} |
| 554 |
if (this._doesOrderHaveSoleItem(order)) { |
| 555 |
return true; |
| 556 |
} |
| 557 |
const total = Object.values(order.uiState.lineToRefund).reduce((acc, val) => { |
| 558 |
acc += val.qty; |
| 559 |
return acc; |
| 560 |
}, 0); |
| 561 |
|
| 562 |
return !this.pos.isProductQtyZero(total); |
| 563 |
} |
| 564 |
switchPane() { |
| 565 |
this.pos.switchPaneTicketScreen(); |
| 566 |
} |
| 567 |
/** |
| 568 |
* Find the empty order with the following priority: |
| 569 |
* - The empty order with the same parter as the provided. |
| 570 |
* - The first empty order without a partner. |
| 571 |
* - If no empty order, create a new one. |
| 572 |
* @param {Object | null} partner |
| 573 |
* @returns {boolean} |
| 574 |
*/ |
| 575 |
_getEmptyOrder(partner) { |
| 576 |
let emptyOrderForPartner = null; |
| 577 |
let emptyOrder = null; |
| 578 |
for (const order of this.pos.models["pos.order"].filter((order) => !order.finalized)) { |
| 579 |
if (order.getOrderlines().length === 0 && order.payment_ids.length === 0) { |
| 580 |
if (order.getPartner() === partner) { |
| 581 |
emptyOrderForPartner = order; |
| 582 |
break; |
| 583 |
} else if (!order.getPartner() && emptyOrder === null) { |
| 584 |
// If emptyOrderForPartner is not found, we will use the first empty order. |
| 585 |
emptyOrder = order; |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
return emptyOrderForPartner || emptyOrder || this.pos.addNewOrder({ partner_id: partner }); |
| 590 |
} |
| 591 |
_doesOrderHaveSoleItem(order) { |
| 592 |
const orderlines = order.getOrderlines(); |
| 593 |
if (orderlines.length !== 1) { |
| 594 |
return false; |
| 595 |
} |
| 596 |
const theOrderline = orderlines[0]; |
| 597 |
const refundableQty = theOrderline.getQuantity() - theOrderline.refundedQty; |
| 598 |
return this.pos.isProductQtyZero(refundableQty - 1); |
| 599 |
} |
| 600 |
_prepareAutoRefundOnOrder(order) { |
| 601 |
const selectedOrderlineId = this.getSelectedOrderlineId(); |
| 602 |
const orderline = order.lines.find((line) => line.id == selectedOrderlineId); |
| 603 |
if (!orderline) { |
| 604 |
return false; |
| 605 |
} |
| 606 |
|
| 607 |
const toRefundDetail = this.getToRefundDetail(orderline); |
| 608 |
if (this.pos.isProductQtyZero(toRefundDetail.maxQty - 1) && toRefundDetail.qty === 0) { |
| 609 |
toRefundDetail.qty = 1; |
| 610 |
} |
| 611 |
return true; |
| 612 |
} |
| 613 |
/** |
| 614 |
* Returns the corresponding toRefundDetail of the given orderline. |
| 615 |
* SIDE-EFFECT: Automatically creates a toRefundDetail object for |
| 616 |
* the given orderline if it doesn't exist and returns it. |
| 617 |
* @param {models.Orderline} orderline |
| 618 |
* @returns |
| 619 |
*/ |
| 620 |
getToRefundDetail(orderline) { |
| 621 |
const lineToRefund = orderline.order_id.uiState.lineToRefund; |
| 622 |
|
| 623 |
if (orderline.uuid in lineToRefund) { |
| 624 |
return lineToRefund[orderline.uuid]; |
| 625 |
} |
| 626 |
|
| 627 |
const newToRefundDetail = new PosOrderLineRefund( |
| 628 |
{ |
| 629 |
line_uuid: orderline.uuid, |
| 630 |
qty: 0, |
| 631 |
}, |
| 632 |
this.pos.models |
| 633 |
); |
| 634 |
|
| 635 |
lineToRefund[orderline.uuid] = newToRefundDetail; |
| 636 |
return newToRefundDetail; |
| 637 |
} |
| 638 |
/** |
| 639 |
* Select the lines from lineToRefund, as they can come from different orders. |
| 640 |
* Returns only details that: |
| 641 |
* - The quantity to refund is not zero |
| 642 |
* - Filtered by partner (optional) |
| 643 |
* - It's not yet linked to an active order (no destinationOrderUid) |
| 644 |
* |
| 645 |
* @param {Object} partner (optional) |
| 646 |
* @param {Order} order |
| 647 |
* @returns {Array} refundableDetails |
| 648 |
*/ |
| 649 |
_getRefundableDetails(partner, order) { |
| 650 |
return Object.values(this.pos.linesToRefund).filter( |
| 651 |
(refund) => |
| 652 |
!this.pos.isProductQtyZero(refund.qty) && |
| 653 |
refund.line.order_id.uuid === order.uuid && |
| 654 |
(partner ? refund.line.order_id.partner_id?.id === partner.id : true) && |
| 655 |
!refund.destinationOrder |
| 656 |
); |
| 657 |
} |
| 658 |
|
| 659 |
async setOrder(order) { |
| 660 |
if (this.pos.config.isShareable) { |
| 661 |
await this.pos.syncAllOrders(); |
| 662 |
} |
| 663 |
this.pos.setOrder(order); |
| 664 |
this.pos.navigateToOrderScreen(order); |
| 665 |
} |
| 666 |
|
| 667 |
onClickNewOrder() { |
| 668 |
const order = this.pos.createNewOrder({ |
| 669 |
preset_id: this.state.selectedPreset || null, |
| 670 |
}); |
| 671 |
this.pos.selectedOrderUuid = order.uuid; |
| 672 |
this.pos.addPendingOrder([order.id]); |
| 673 |
this.pos.navigateToOrderScreen(order); |
| 674 |
} |
| 675 |
|
| 676 |
_getFilterOptions() { |
| 677 |
const orderStates = this._getOrderStates(); |
| 678 |
orderStates.set("SYNCED", { text: _t("Paid") }); |
| 679 |
return orderStates; |
| 680 |
} |
| 681 |
/** |
| 682 |
* @returns {Record<string, { repr: (order: models.Order) => string, displayName: string, modelFields: Array }>} |
| 683 |
*/ |
| 684 |
_getSearchFields() { |
| 685 |
const fields = { |
| 686 |
REFERENCE: { |
| 687 |
repr: (order) => order.getName(), |
| 688 |
displayName: _t("Reference"), |
| 689 |
modelFields: ["tracking_number", "floating_order_name"], |
| 690 |
}, |
| 691 |
RECEIPT_NUMBER: { |
| 692 |
repr: (order) => order.pos_reference, |
| 693 |
displayName: _t("Receipt Number"), |
| 694 |
modelFields: ["pos_reference"], |
| 695 |
}, |
| 696 |
INVOICE_NUMBER: { |
| 697 |
repr: (order) => order.invoiceName, |
| 698 |
displayName: _t("Invoice Number"), |
| 699 |
modelFields: ["account_move.name"], |
| 700 |
}, |
| 701 |
DATE: { |
| 702 |
repr: (order) => this.getDate(order), |
| 703 |
displayName: _t("Date"), |
| 704 |
modelFields: ["date_order"], |
| 705 |
formatSearch: (searchTerm) => { |
| 706 |
const includesTime = searchTerm.includes(":"); |
| 707 |
let parsedDateTime; |
| 708 |
try { |
| 709 |
parsedDateTime = parseDateTime(searchTerm); |
| 710 |
} catch { |
| 711 |
return searchTerm; |
| 712 |
} |
| 713 |
if (includesTime) { |
| 714 |
return parsedDateTime.toUTC().toFormat("yyyy-MM-dd HH:mm:ss"); |
| 715 |
} else { |
| 716 |
return parsedDateTime.toFormat("yyyy-MM-dd"); |
| 717 |
} |
| 718 |
}, |
| 719 |
}, |
| 720 |
PARTNER: { |
| 721 |
repr: (order) => order.getPartnerName(), |
| 722 |
displayName: _t("Customer"), |
| 723 |
modelFields: ["partner_id.complete_name"], |
| 724 |
}, |
| 725 |
}; |
| 726 |
|
| 727 |
if (this.showCardholderName()) { |
| 728 |
fields.CARDHOLDER_NAME = { |
| 729 |
repr: (order) => order.getCardHolderName(), |
| 730 |
displayName: _t("Cardholder Name"), |
| 731 |
modelFields: ["payment_ids.cardholder_name"], |
| 732 |
}; |
| 733 |
} |
| 734 |
|
| 735 |
return fields; |
| 736 |
} |
| 737 |
/** |
| 738 |
* Maps the order screen params to order status. |
| 739 |
*/ |
| 740 |
_getScreenToStatusMap() { |
| 741 |
return { |
| 742 |
ProductScreen: "ONGOING", |
| 743 |
PaymentScreen: "PAYMENT", |
| 744 |
ReceiptScreen: "RECEIPT", |
| 745 |
}; |
| 746 |
} |
| 747 |
_getOrderStates() { |
| 748 |
// We need the items to be ordered, therefore, Map is used instead of normal object. |
| 749 |
const states = new Map(); |
| 750 |
states.set("ACTIVE_ORDERS", { |
| 751 |
text: _t("Active"), |
| 752 |
}); |
| 753 |
// The spaces are important to make sure the following states |
| 754 |
// are under the category of `Active`. |
| 755 |
states.set("ONGOING", { |
| 756 |
text: _t("Ongoing"), |
| 757 |
indented: true, |
| 758 |
}); |
| 759 |
states.set("PAYMENT", { |
| 760 |
text: _t("Payment"), |
| 761 |
indented: true, |
| 762 |
}); |
| 763 |
states.set("RECEIPT", { |
| 764 |
text: _t("Receipt"), |
| 765 |
indented: true, |
| 766 |
}); |
| 767 |
return states; |
| 768 |
} |
| 769 |
//#region SEARCH SYNCED ORDERS |
| 770 |
_computeSyncedOrdersDomain() { |
| 771 |
let { fieldName, searchTerm } = this.state.search; |
| 772 |
if (!searchTerm) { |
| 773 |
return []; |
| 774 |
} |
| 775 |
const searchField = this._getSearchFields()[fieldName]; |
| 776 |
if (searchField && searchField.modelFields && searchField.modelFields.length > 0) { |
| 777 |
if (searchField.formatSearch) { |
| 778 |
searchTerm = searchField.formatSearch(searchTerm); |
| 779 |
} |
| 780 |
const domain = []; |
| 781 |
for (const modelField of searchField.modelFields) { |
| 782 |
domain.unshift([modelField, "ilike", `%${searchTerm}%`]); |
| 783 |
if (domain.length > 1) { |
| 784 |
domain.unshift("|"); |
| 785 |
} |
| 786 |
} |
| 787 |
if (this.state.search.partnerId && this.state.search.fieldName === "PARTNER") { |
| 788 |
domain.unshift(["partner_id.id", "in", [this.state.search.partnerId]]); |
| 789 |
} |
| 790 |
return domain; |
| 791 |
} else { |
| 792 |
return []; |
| 793 |
} |
| 794 |
} |
| 795 |
/** |
| 796 |
* Fetches the done orders from the backend that needs to be shown. |
| 797 |
* If the order is already in cache, the full information about that |
| 798 |
* order is not fetched anymore, instead, we use info from cache. |
| 799 |
*/ |
| 800 |
async _fetchSyncedOrders() { |
| 801 |
const screenState = this.pos.screenState.ticketSCreen; |
| 802 |
const domain = this._computeSyncedOrdersDomain(); |
| 803 |
const offset = screenState.offsetByDomain[JSON.stringify(domain)] || 0; |
| 804 |
const config_id = this.pos.config.id; |
| 805 |
const { ordersInfo, totalCount } = await this.pos.data.call( |
| 806 |
"pos.order", |
| 807 |
"search_paid_order_ids", |
| 808 |
[], |
| 809 |
{ |
| 810 |
config_id, |
| 811 |
domain, |
| 812 |
limit: this.state.nbrByPage, |
| 813 |
offset, |
| 814 |
} |
| 815 |
); |
| 816 |
|
| 817 |
if (!screenState.offsetByDomain[JSON.stringify(domain)]) { |
| 818 |
screenState.offsetByDomain[JSON.stringify(domain)] = 0; |
| 819 |
} |
| 820 |
screenState.offsetByDomain[JSON.stringify(domain)] += ordersInfo.length; |
| 821 |
screenState.totalCount = totalCount; |
| 822 |
|
| 823 |
const idsNotInCacheOrOutdated = ordersInfo |
| 824 |
.filter((orderInfo) => { |
| 825 |
const order = this.pos.models["pos.order"].get(orderInfo[0]); |
| 826 |
|
| 827 |
if ( |
| 828 |
order && |
| 829 |
parseDateTime(orderInfo[1], { tz: "UTC" }).setZone("local").ts > |
| 830 |
order.date_order.ts |
| 831 |
) { |
| 832 |
return true; |
| 833 |
} |
| 834 |
|
| 835 |
return !order; |
| 836 |
}) |
| 837 |
.map((info) => info[0]); |
| 838 |
|
| 839 |
if (idsNotInCacheOrOutdated.length > 0) { |
| 840 |
await this.pos.data.loadServerOrders([ |
| 841 |
["id", "in", Array.from(new Set(idsNotInCacheOrOutdated))], |
| 842 |
]); |
| 843 |
} |
| 844 |
} |
| 845 |
//#endregion |
| 846 |
getPresetTimeColor(order) { |
| 847 |
const slot = order.preset_id.currentSlot; |
| 848 |
const presetTime = order.preset_time; |
| 849 |
if (!slot) { |
| 850 |
if (presetTime < DateTime.now()) { |
| 851 |
return "bg-danger text-white"; |
| 852 |
} else { |
| 853 |
return "bg-light text-emphasis"; |
| 854 |
} |
| 855 |
} |
| 856 |
if ( |
| 857 |
slot.datetime <= presetTime && |
| 858 |
presetTime < slot.datetime.plus({ minutes: order.preset_id.interval_time }) |
| 859 |
) { |
| 860 |
return "bg-warning text-white"; |
| 861 |
} else if (presetTime < slot.datetime) { |
| 862 |
return "bg-danger text-white"; |
| 863 |
} else { |
| 864 |
return "bg-light text-emphasis"; |
| 865 |
} |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
registry.category("pos_pages").add("TicketScreen", { |
| 870 |
name: "TicketScreen", |
| 871 |
component: TicketScreen, |
| 872 |
route: `/pos/ui/${odoo.pos_config_id}/ticket`, |
| 873 |
params: {}, |
| 874 |
}); |