| 1 |
import { _t } from "@web/core/l10n/translation"; |
| 2 |
import { parseFloat } from "@web/views/fields/parsers"; |
| 3 |
import { useErrorHandlers, useAsyncLockedMethod } from "@point_of_sale/app/hooks/hooks"; |
| 4 |
import { registry } from "@web/core/registry"; |
| 5 |
import { useService } from "@web/core/utils/hooks"; |
| 6 |
|
| 7 |
import { AlertDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 8 |
import { NumberPopup } from "@point_of_sale/app/components/popups/number_popup/number_popup"; |
| 9 |
import { PriceFormatter } from "@point_of_sale/app/components/price_formatter/price_formatter"; |
| 10 |
import { DatePickerPopup } from "@point_of_sale/app/components/popups/date_picker_popup/date_picker_popup"; |
| 11 |
|
| 12 |
import { PaymentScreenPaymentLines } from "@point_of_sale/app/screens/payment_screen/payment_lines/payment_lines"; |
| 13 |
import { PaymentScreenStatus } from "@point_of_sale/app/screens/payment_screen/payment_status/payment_status"; |
| 14 |
import { usePos } from "@point_of_sale/app/hooks/pos_hook"; |
| 15 |
import { Component, onMounted } from "@odoo/owl"; |
| 16 |
import { Numpad, enhancedButtons } from "@point_of_sale/app/components/numpad/numpad"; |
| 17 |
import { makeAwaitable } from "@point_of_sale/app/utils/make_awaitable_dialog"; |
| 18 |
import { useRouterParamsChecker } from "@point_of_sale/app/hooks/pos_router_hook"; |
| 19 |
import OrderPaymentValidation from "@point_of_sale/app/utils/order_payment_validation"; |
| 20 |
|
| 21 |
export class PaymentScreen extends Component { |
| 22 |
static template = "point_of_sale.PaymentScreen"; |
| 23 |
static components = { |
| 24 |
Numpad, |
| 25 |
PaymentScreenPaymentLines, |
| 26 |
PaymentScreenStatus, |
| 27 |
PriceFormatter, |
| 28 |
}; |
| 29 |
static props = { |
| 30 |
orderUuid: String, |
| 31 |
}; |
| 32 |
|
| 33 |
setup() { |
| 34 |
this.pos = usePos(); |
| 35 |
this.ui = useService("ui"); |
| 36 |
this.dialog = useService("dialog"); |
| 37 |
this.invoiceService = useService("account_move"); |
| 38 |
this.notification = useService("notification"); |
| 39 |
this.hardwareProxy = useService("hardware_proxy"); |
| 40 |
this.printer = useService("printer"); |
| 41 |
this.payment_methods_from_config = this.pos.config.payment_method_ids |
| 42 |
.slice() |
| 43 |
.sort((a, b) => a.sequence - b.sequence); |
| 44 |
this.numberBuffer = useService("number_buffer"); |
| 45 |
this.numberBuffer.use(this._getNumberBufferConfig); |
| 46 |
useRouterParamsChecker(); |
| 47 |
useErrorHandlers(); |
| 48 |
this.payment_interface = null; |
| 49 |
this.error = false; |
| 50 |
this.validateOrder = useAsyncLockedMethod(this.validateOrder); |
| 51 |
onMounted(this.onMounted); |
| 52 |
} |
| 53 |
|
| 54 |
async validateOrder(isForceValidate = false) { |
| 55 |
const validation = new OrderPaymentValidation({ |
| 56 |
pos: this.pos, |
| 57 |
orderUuid: this.currentOrder.uuid, |
| 58 |
}); |
| 59 |
await validation.validateOrder(isForceValidate); |
| 60 |
} |
| 61 |
|
| 62 |
onMounted() { |
| 63 |
const order = this.pos.getOrder(); |
| 64 |
|
| 65 |
for (const payment of order.payment_ids) { |
| 66 |
const pmid = payment.payment_method_id.id; |
| 67 |
if (!this.pos.config.payment_method_ids.map((pm) => pm.id).includes(pmid)) { |
| 68 |
payment.delete({ backend: true }); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if (this.payment_methods_from_config.length == 1 && this.paymentLines.length == 0) { |
| 73 |
this.addNewPaymentLine(this.payment_methods_from_config[0]); |
| 74 |
} |
| 75 |
|
| 76 |
//Activate the invoice option for refund orders if the original order was invoiced. |
| 77 |
if ( |
| 78 |
this.currentOrder.isRefund && |
| 79 |
this.currentOrder.lines[0].refunded_orderline_id?.order_id?.isToInvoice() |
| 80 |
) { |
| 81 |
this.currentOrder.setToInvoice(true); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
getNumpadButtons() { |
| 86 |
const colorClassMap = { |
| 87 |
[this.env.services.localization.decimalPoint]: "o_colorlist_item_numpad_color_6", |
| 88 |
Backspace: "o_colorlist_item_numpad_color_1", |
| 89 |
"+10": "o_colorlist_item_numpad_color_10", |
| 90 |
"+20": "o_colorlist_item_numpad_color_10", |
| 91 |
"+50": "o_colorlist_item_numpad_color_10", |
| 92 |
"-": "o_colorlist_item_numpad_color_3", |
| 93 |
}; |
| 94 |
|
| 95 |
return enhancedButtons().map((button) => ({ |
| 96 |
...button, |
| 97 |
class: `${colorClassMap[button.value] || ""}`, |
| 98 |
})); |
| 99 |
} |
| 100 |
|
| 101 |
showMaxValueError() { |
| 102 |
this.dialog.add(AlertDialog, { |
| 103 |
title: _t("Maximum value reached"), |
| 104 |
body: _t( |
| 105 |
"The amount cannot be higher than the due amount if you don't have a cash payment method configured." |
| 106 |
), |
| 107 |
}); |
| 108 |
} |
| 109 |
get _getNumberBufferConfig() { |
| 110 |
const config = { |
| 111 |
// When the buffer is updated, trigger this event. |
| 112 |
// Note that the component listens to it. |
| 113 |
triggerAtInput: () => this.updateSelectedPaymentline(), |
| 114 |
useWithBarcode: true, |
| 115 |
}; |
| 116 |
|
| 117 |
return config; |
| 118 |
} |
| 119 |
get currentOrder() { |
| 120 |
return this.pos.models["pos.order"].getBy("uuid", this.props.orderUuid); |
| 121 |
} |
| 122 |
get isRefundOrder() { |
| 123 |
return this.currentOrder.isRefund; |
| 124 |
} |
| 125 |
get paymentLines() { |
| 126 |
return this.currentOrder.payment_ids; |
| 127 |
} |
| 128 |
get selectedPaymentLine() { |
| 129 |
return this.currentOrder.getSelectedPaymentline(); |
| 130 |
} |
| 131 |
makeAnimation() { |
| 132 |
this.pos.addAnimation = true; |
| 133 |
setTimeout(() => (this.pos.addAnimation = false), 1000); |
| 134 |
} |
| 135 |
async addNewPaymentLine(paymentMethod) { |
| 136 |
if (this.pos.paymentTerminalInProgress && paymentMethod.use_payment_terminal) { |
| 137 |
this.dialog.add(AlertDialog, { |
| 138 |
title: _t("Error"), |
| 139 |
body: _t("There is already an electronic payment in progress."), |
| 140 |
}); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if (this.paymentLines.length === 0) { |
| 145 |
this.makeAnimation(); |
| 146 |
} |
| 147 |
// original function: click_paymentmethods |
| 148 |
const result = this.currentOrder.addPaymentline(paymentMethod); |
| 149 |
if (result.status) { |
| 150 |
this.numberBuffer.set(result.data.amount.toString()); |
| 151 |
if ( |
| 152 |
paymentMethod.use_payment_terminal && |
| 153 |
!this.isRefundOrder && |
| 154 |
paymentMethod.payment_terminal.fastPayments |
| 155 |
) { |
| 156 |
const newPaymentLine = this.paymentLines.at(-1); |
| 157 |
this.sendPaymentRequest(newPaymentLine); |
| 158 |
} |
| 159 |
return true; |
| 160 |
} else { |
| 161 |
this.dialog.add(AlertDialog, { |
| 162 |
title: _t("Error"), |
| 163 |
body: result.data, |
| 164 |
}); |
| 165 |
return false; |
| 166 |
} |
| 167 |
} |
| 168 |
updateSelectedPaymentline(amount = false) { |
| 169 |
if (this.paymentLines.every((line) => line.paid)) { |
| 170 |
this.currentOrder.addPaymentline(this.payment_methods_from_config[0]); |
| 171 |
} |
| 172 |
if (!this.selectedPaymentLine) { |
| 173 |
return; |
| 174 |
} // do nothing if no selected payment line |
| 175 |
if (amount === false) { |
| 176 |
if (this.numberBuffer.get() === null) { |
| 177 |
amount = null; |
| 178 |
} else if (this.numberBuffer.get() === "") { |
| 179 |
amount = 0; |
| 180 |
} else { |
| 181 |
amount = this.numberBuffer.getFloat(); |
| 182 |
} |
| 183 |
} |
| 184 |
// disable changing amount on paymentlines with running or done payments on a payment terminal |
| 185 |
const payment_terminal = this.selectedPaymentLine.payment_method_id.payment_terminal; |
| 186 |
const hasCashPaymentMethod = this.payment_methods_from_config.some( |
| 187 |
(method) => method.type === "cash" |
| 188 |
); |
| 189 |
if ( |
| 190 |
!hasCashPaymentMethod && |
| 191 |
amount > this.currentOrder.remainingDue + this.selectedPaymentLine.amount |
| 192 |
) { |
| 193 |
this.selectedPaymentLine.setAmount(0); |
| 194 |
this.numberBuffer.set(this.currentOrder.remainingDue.toString()); |
| 195 |
amount = this.currentOrder.remainingDue; |
| 196 |
this.showMaxValueError(); |
| 197 |
} |
| 198 |
if ( |
| 199 |
payment_terminal && |
| 200 |
!["pending", "retry"].includes(this.selectedPaymentLine.getPaymentStatus()) |
| 201 |
) { |
| 202 |
return; |
| 203 |
} |
| 204 |
if (amount === null) { |
| 205 |
this.deletePaymentLine(this.selectedPaymentLine.uuid); |
| 206 |
} else { |
| 207 |
this.selectedPaymentLine.setAmount(amount); |
| 208 |
} |
| 209 |
} |
| 210 |
async toggleIsToInvoice() { |
| 211 |
if (!this.pos.config.canInvoice) { |
| 212 |
this.notification.add( |
| 213 |
_t("To enable invoice creation, please add a journal for it in the settings."), |
| 214 |
{ type: "warning" } |
| 215 |
); |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
this.currentOrder.setToInvoice(!this.currentOrder.isToInvoice()); |
| 220 |
} |
| 221 |
openCashbox() { |
| 222 |
this.hardwareProxy.openCashbox(); |
| 223 |
} |
| 224 |
async addTip() { |
| 225 |
const tip = this.currentOrder.getTip(); |
| 226 |
const change = Math.abs(this.currentOrder.change); |
| 227 |
const value = tip === 0 && change > 0 ? change : tip; |
| 228 |
const newTip = await makeAwaitable(this.dialog, NumberPopup, { |
| 229 |
title: tip ? _t("Change Tip") : _t("Add Tip"), |
| 230 |
startingValue: this.env.utils.formatCurrency(value, false), |
| 231 |
formatDisplayedValue: (x) => `${this.pos.currency.symbol} ${x}`, |
| 232 |
}); |
| 233 |
|
| 234 |
if (newTip === undefined) { |
| 235 |
return; |
| 236 |
} |
| 237 |
await this.pos.setTip(parseFloat(newTip ?? "")); |
| 238 |
const pLine = |
| 239 |
this.selectedPaymentLine && |
| 240 |
(!this.selectedPaymentLine.isElectronic() || |
| 241 |
this.selectedPaymentLine.getPaymentStatus() === "pending") |
| 242 |
? this.selectedPaymentLine |
| 243 |
: false; |
| 244 |
|
| 245 |
if (!pLine || newTip === tip) { |
| 246 |
this.notification.add( |
| 247 |
_t( |
| 248 |
"The tip has been added to the order. However,the selected payment line does not allow tips to be added." |
| 249 |
) |
| 250 |
); |
| 251 |
return; |
| 252 |
} |
| 253 |
const tipDifference = parseFloat(newTip) - (tip || 0); |
| 254 |
const tipToAdd = change <= 0 ? tipDifference : Math.max(0, tipDifference - change); |
| 255 |
pLine.setAmount(pLine.getAmount() + tipToAdd); |
| 256 |
} |
| 257 |
async toggleShippingDatePicker() { |
| 258 |
if (!this.currentOrder.getShippingDate()) { |
| 259 |
this.dialog.add(DatePickerPopup, { |
| 260 |
title: _t("Select the shipping date"), |
| 261 |
getPayload: (shippingDate) => { |
| 262 |
this.currentOrder.setShippingDate(shippingDate); |
| 263 |
}, |
| 264 |
}); |
| 265 |
} else { |
| 266 |
this.currentOrder.setShippingDate(false); |
| 267 |
} |
| 268 |
} |
| 269 |
deletePaymentLine(uuid) { |
| 270 |
const line = this.paymentLines.find((line) => line.uuid === uuid); |
| 271 |
if (line.payment_method_id.payment_method_type === "qr_code") { |
| 272 |
this.currentOrder.removePaymentline(line); |
| 273 |
this.numberBuffer.reset(); |
| 274 |
return; |
| 275 |
} |
| 276 |
// If a paymentline with a payment terminal linked to |
| 277 |
// it is removed, the terminal should get a cancel |
| 278 |
// request. |
| 279 |
if (["waiting", "waitingCard", "timeout"].includes(line.getPaymentStatus())) { |
| 280 |
line.setPaymentStatus("waitingCancel"); |
| 281 |
line.payment_method_id.payment_terminal |
| 282 |
.sendPaymentCancel(this.currentOrder, uuid) |
| 283 |
.then(() => { |
| 284 |
this.currentOrder.removePaymentline(line); |
| 285 |
this.numberBuffer.reset(); |
| 286 |
}); |
| 287 |
} else if (line.getPaymentStatus() !== "waitingCancel") { |
| 288 |
this.currentOrder.removePaymentline(line); |
| 289 |
this.numberBuffer.reset(); |
| 290 |
} |
| 291 |
} |
| 292 |
selectPaymentLine(uuid) { |
| 293 |
const line = this.paymentLines.find((line) => line.uuid === uuid); |
| 294 |
this.currentOrder.selectPaymentline(line); |
| 295 |
this.numberBuffer.reset(); |
| 296 |
} |
| 297 |
|
| 298 |
paymentMethodImage(id) { |
| 299 |
if (this.paymentMethod.image) { |
| 300 |
return `/web/image/pos.payment.method/${id}/image`; |
| 301 |
} else if (this.paymentMethod.type === "cash") { |
| 302 |
return "/point_of_sale/static/src/img/money.png"; |
| 303 |
} else if (this.paymentMethod.type === "pay_later") { |
| 304 |
return "/point_of_sale/static/src/img/pay-later.png"; |
| 305 |
} else { |
| 306 |
return "/point_of_sale/static/src/img/card-bank.png"; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
async sendPaymentRequest(line) { |
| 311 |
// Other payment lines can not be reversed anymore |
| 312 |
this.pos.paymentTerminalInProgress = true; |
| 313 |
this.numberBuffer.capture(); |
| 314 |
this.paymentLines.forEach(function (line) { |
| 315 |
line.can_be_reversed = false; |
| 316 |
}); |
| 317 |
|
| 318 |
let isPaymentSuccessful = false; |
| 319 |
if (line.payment_method_id.payment_method_type === "qr_code") { |
| 320 |
const resp = await this.pos.showQR(line); |
| 321 |
isPaymentSuccessful = line.handlePaymentResponse(resp); |
| 322 |
} else { |
| 323 |
isPaymentSuccessful = await line.pay(); |
| 324 |
} |
| 325 |
|
| 326 |
// Automatically validate the order when after an electronic payment, |
| 327 |
// the current order is fully paid and due is zero. |
| 328 |
this.pos.paymentTerminalInProgress = false; |
| 329 |
const config = this.pos.config; |
| 330 |
const currentOrder = line.pos_order_id; |
| 331 |
if ( |
| 332 |
isPaymentSuccessful && |
| 333 |
currentOrder.isPaid() && |
| 334 |
config.auto_validate_terminal_payment && |
| 335 |
!currentOrder.isRefundInProcess() |
| 336 |
) { |
| 337 |
this.validateOrder(false); |
| 338 |
} |
| 339 |
} |
| 340 |
async sendPaymentCancel(line) { |
| 341 |
const payment_terminal = line.payment_method_id.payment_terminal; |
| 342 |
line.setPaymentStatus("waitingCancel"); |
| 343 |
const isCancelSuccessful = await payment_terminal.sendPaymentCancel( |
| 344 |
this.currentOrder, |
| 345 |
line.uuid |
| 346 |
); |
| 347 |
if (isCancelSuccessful) { |
| 348 |
line.setPaymentStatus("retry"); |
| 349 |
this.pos.paymentTerminalInProgress = false; |
| 350 |
} else { |
| 351 |
line.setPaymentStatus("waitingCard"); |
| 352 |
} |
| 353 |
} |
| 354 |
async sendPaymentReverse(line) { |
| 355 |
const payment_terminal = line.payment_method_id.payment_terminal; |
| 356 |
line.setPaymentStatus("reversing"); |
| 357 |
|
| 358 |
const isReversalSuccessful = await payment_terminal.sendPaymentReversal(line.uuid); |
| 359 |
if (isReversalSuccessful) { |
| 360 |
line.setAmount(0); |
| 361 |
line.setPaymentStatus("reversed"); |
| 362 |
} else { |
| 363 |
line.can_be_reversed = false; |
| 364 |
line.setPaymentStatus("done"); |
| 365 |
} |
| 366 |
} |
| 367 |
async sendForceDone(line) { |
| 368 |
line.setPaymentStatus("done"); |
| 369 |
this.pos.paymentTerminalInProgress = false; |
| 370 |
const config = this.pos.config; |
| 371 |
const currentOrder = line.pos_order_id; |
| 372 |
if ( |
| 373 |
currentOrder.isPaid() && |
| 374 |
config.auto_validate_terminal_payment && |
| 375 |
!currentOrder.isRefundInProcess() |
| 376 |
) { |
| 377 |
this.validateOrder(false); |
| 378 |
} |
| 379 |
} |
| 380 |
async clickTableGuests() { |
| 381 |
this.pos.setCustomerCount(); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
registry.category("pos_pages").add("PaymentScreen", { |
| 386 |
name: "PaymentScreen", |
| 387 |
component: PaymentScreen, |
| 388 |
route: `/pos/ui/${odoo.pos_config_id}/payment/{string:orderUuid}`, |
| 389 |
params: { |
| 390 |
orderUuid: true, |
| 391 |
orderFinalized: false, |
| 392 |
}, |
| 393 |
}); |