| 1 |
import { AlertDialog, ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 2 |
import { serializeDateTime } from "@web/core/l10n/dates"; |
| 3 |
import { _t } from "@web/core/l10n/translation"; |
| 4 |
import { ConnectionLostError, RPCError } from "@web/core/network/rpc"; |
| 5 |
import { handleRPCError } from "./error_handlers"; |
| 6 |
import { ask } from "./make_awaitable_dialog"; |
| 7 |
|
| 8 |
/** |
| 9 |
* This class contains all methods related to order validation. Previously, |
| 10 |
* these methods were only used on the payment screen, but now that we have quick |
| 11 |
* order validation, they are used in different places. |
| 12 |
* |
| 13 |
* All behaviors related to order validation must be found in this class. |
| 14 |
* |
| 15 |
* @param {Object} params - The parameters for the order validation. |
| 16 |
* @param {Object} params.pos - The pos_store instance. |
| 17 |
* @param {Object} params.order - The order to validate. |
| 18 |
* @param {Object} [params.fastPaymentMethod=null] - The payment method to use for fast payment validation. |
| 19 |
*/ |
| 20 |
export default class OrderPaymentValidation { |
| 21 |
constructor({ pos, orderUuid, fastPaymentMethod = null }) { |
| 22 |
this.setup({ pos, orderUuid, fastPaymentMethod }); |
| 23 |
} |
| 24 |
|
| 25 |
setup(vals) { |
| 26 |
this.pos = vals.pos; |
| 27 |
this.orderUuid = vals.orderUuid; |
| 28 |
this.payment_methods_from_config = this.pos.config.payment_method_ids |
| 29 |
.slice() |
| 30 |
.sort((a, b) => a.sequence - b.sequence); |
| 31 |
if (vals.fastPaymentMethod) { |
| 32 |
this.order.addPaymentline(vals.fastPaymentMethod); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
get order() { |
| 37 |
return this.pos.models["pos.order"].getBy("uuid", this.orderUuid); |
| 38 |
} |
| 39 |
|
| 40 |
get nextPage() { |
| 41 |
if ( |
| 42 |
this.pos.config.iface_print_auto && |
| 43 |
this.pos.config.iface_print_skip_screen && |
| 44 |
this.order.payment_ids[0]?.payment_method_id |
| 45 |
) { |
| 46 |
return { |
| 47 |
page: "FeedbackScreen", |
| 48 |
params: { |
| 49 |
orderUuid: this.order.uuid, |
| 50 |
paymentMethodId: this.order.payment_ids[0]?.payment_method_id.id, |
| 51 |
}, |
| 52 |
}; |
| 53 |
} |
| 54 |
|
| 55 |
return !this.error |
| 56 |
? { |
| 57 |
page: "ReceiptScreen", |
| 58 |
params: { |
| 59 |
orderUuid: this.order.uuid, |
| 60 |
}, |
| 61 |
} |
| 62 |
: this.pos.defaultPage; |
| 63 |
} |
| 64 |
|
| 65 |
get paymentLines() { |
| 66 |
return this.order.payment_ids; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* This method can be overridden to perform checks before starting the order validation process. |
| 71 |
*/ |
| 72 |
async beforePostPushOrderResolve(order, order_server_ids) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* This method can be overridden to perform checks before starting the order validation process. |
| 78 |
*/ |
| 79 |
shouldDownloadInvoice() { |
| 80 |
if (!this.pos.config.canInvoice) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
async shouldHideValidationBehindFeedbackScreen() { |
| 87 |
const nextPage = this.nextPage; |
| 88 |
if (nextPage.page === "FeedbackScreen") { |
| 89 |
const waitForFn = async () => { |
| 90 |
await this.finalizeValidation(); |
| 91 |
}; |
| 92 |
nextPage.params.waitFor = waitForFn(); |
| 93 |
} else { |
| 94 |
try { |
| 95 |
this.pos.env.services.ui.block(); |
| 96 |
const response = await this.finalizeValidation(); |
| 97 |
if (response instanceof RPCError) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
} finally { |
| 101 |
this.pos.env.services.ui.unblock(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
this.pos.navigate(nextPage.page, nextPage.params); |
| 106 |
} |
| 107 |
|
| 108 |
async validateOrder(isForceValidate) { |
| 109 |
if ((await this.askBeforeValidation()) === false) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
if ((await this._askForCustomerIfRequired()) === false) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
this.pos.numberBuffer.capture(); |
| 116 |
if (!this.checkCashRoundingHasBeenWellApplied()) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
const linesToRemove = this.order.lines.filter((line) => line.canBeRemoved); |
| 120 |
for (const line of linesToRemove) { |
| 121 |
this.order.removeOrderline(line); |
| 122 |
} |
| 123 |
if (await this.isOrderValid(isForceValidate)) { |
| 124 |
// remove pending payments before finalizing the validation |
| 125 |
const toRemove = []; |
| 126 |
for (const line of this.paymentLines) { |
| 127 |
if (!line.isDone() || line.amount === 0) { |
| 128 |
toRemove.push(line); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
for (const line of toRemove) { |
| 133 |
this.order.removePaymentline(line); |
| 134 |
} |
| 135 |
|
| 136 |
await this.shouldHideValidationBehindFeedbackScreen(); |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
async finalizeValidation() { |
| 144 |
if (this.order.isPaidWithCash() || this.order.change) { |
| 145 |
this.pos.hardwareProxy.openCashbox(); |
| 146 |
} |
| 147 |
|
| 148 |
this.order.date_order = serializeDateTime(luxon.DateTime.now()); |
| 149 |
for (const line of this.paymentLines) { |
| 150 |
if (!line.amount === 0) { |
| 151 |
this.order.removePaymentline(line); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
this.pos.addPendingOrder([this.order.id]); |
| 156 |
this.order.state = "paid"; |
| 157 |
|
| 158 |
try { |
| 159 |
// 1. Save order to server. |
| 160 |
const syncOrderResult = await this.pos.syncAllOrders({ throw: true }); |
| 161 |
if (!syncOrderResult) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
// 2. Invoice, should not stop the validation process but a dialog is shown if an |
| 166 |
// error occured. |
| 167 |
if (this.shouldDownloadInvoice() && this.order.isToInvoice()) { |
| 168 |
if (this.order.raw.account_move) { |
| 169 |
await this.pos.env.services.account_move.downloadPdf( |
| 170 |
this.order.raw.account_move |
| 171 |
); |
| 172 |
} else { |
| 173 |
this.pos.dialog.add(AlertDialog, { |
| 174 |
title: _t("Backend Invoice"), |
| 175 |
body: _t( |
| 176 |
"An error occurred while generating an invoice. You can try again from the order list." |
| 177 |
), |
| 178 |
}); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// 3. Post process. |
| 183 |
const postPushOrders = syncOrderResult.filter((order) => order.waitForPushOrder()); |
| 184 |
if (postPushOrders.length > 0) { |
| 185 |
await this.postPushOrderResolve(postPushOrders.map((order) => order.id)); |
| 186 |
} |
| 187 |
|
| 188 |
return await this.afterOrderValidation(!!syncOrderResult && syncOrderResult.length > 0); |
| 189 |
} catch (error) { |
| 190 |
return this.handleValidationError(error); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
async postPushOrderResolve(ordersServerId) { |
| 195 |
const postPushResult = await this.beforePostPushOrderResolve(this.order, ordersServerId); |
| 196 |
if (!postPushResult) { |
| 197 |
this.pos.dialog.add(AlertDialog, { |
| 198 |
title: _t("Error: no internet connection."), |
| 199 |
body: _t("Some, if not all, post-processing after syncing order failed."), |
| 200 |
}); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
async afterOrderValidation() { |
| 205 |
// Always show the next screen regardless of error since pos has to |
| 206 |
// continue working even offline. |
| 207 |
if (!this.pos.config.module_pos_restaurant) { |
| 208 |
this.pos.checkPreparationStateAndSentOrderInPreparation(this.order, { |
| 209 |
orderDone: true, |
| 210 |
}); |
| 211 |
} |
| 212 |
|
| 213 |
if (this.order.nb_print === 0 && this.pos.config.iface_print_auto) { |
| 214 |
const invoiced_finalized = this.order.isToInvoice() ? this.order.finalized : true; |
| 215 |
if (invoiced_finalized) { |
| 216 |
await this.pos.printReceipt({ order: this.order }); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* This method can be overridden to perform checks before starting the order validation process. |
| 223 |
*/ |
| 224 |
async askBeforeValidation() { |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
handleValidationError(error) { |
| 229 |
if (error instanceof ConnectionLostError) { |
| 230 |
this.afterOrderValidation(); |
| 231 |
Promise.reject(error); |
| 232 |
} else if (error instanceof RPCError) { |
| 233 |
this.order.state = "draft"; |
| 234 |
handleRPCError(error, this.pos.dialog); |
| 235 |
} else { |
| 236 |
throw error; |
| 237 |
} |
| 238 |
return error; |
| 239 |
} |
| 240 |
|
| 241 |
checkCashRoundingHasBeenWellApplied() { |
| 242 |
const useRound = this.pos.config.hasCashRounding; |
| 243 |
if (!useRound) { |
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
const cashRounding = this.pos.config.rounding_method; |
| 248 |
const order = this.pos.getOrder(); |
| 249 |
const currency = this.pos.currency; |
| 250 |
for (const payment of order.payment_ids) { |
| 251 |
if (!payment.payment_method_id.is_cash_count) { |
| 252 |
continue; |
| 253 |
} |
| 254 |
|
| 255 |
const amountPaid = payment.getAmount(); |
| 256 |
const expectedAmountPaid = cashRounding.round(amountPaid); |
| 257 |
if (currency.isZero(expectedAmountPaid - amountPaid)) { |
| 258 |
continue; |
| 259 |
} |
| 260 |
|
| 261 |
this.pos.dialog.add(AlertDialog, { |
| 262 |
title: _t("Rounding error in payment lines"), |
| 263 |
body: _t( |
| 264 |
"The amount of your payment lines must be rounded to validate the transaction.\n" + |
| 265 |
"The rounding precision is %(rounding)s so you should set %(expectedAmount)s as payment amount instead of %(paidAmount)s.", |
| 266 |
{ |
| 267 |
rounding: cashRounding.rounding.toFixed(this.pos.currency.decimal_places), |
| 268 |
expectedAmount: expectedAmountPaid.toFixed( |
| 269 |
this.pos.currency.decimal_places |
| 270 |
), |
| 271 |
paidAmount: amountPaid.toFixed(this.pos.currency.decimal_places), |
| 272 |
} |
| 273 |
), |
| 274 |
}); |
| 275 |
return false; |
| 276 |
} |
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
async isOrderValid(isForceValidate) { |
| 281 |
if (this.order.isRefundInProcess()) { |
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
if (this.order.getOrderlines().length === 0 && this.order.isToInvoice()) { |
| 286 |
this.pos.dialog.add(AlertDialog, { |
| 287 |
title: _t("Empty Order"), |
| 288 |
body: _t( |
| 289 |
"There must be at least one product in your order before it can be validated and invoiced." |
| 290 |
), |
| 291 |
}); |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
if ( |
| 296 |
(this.order.isToInvoice() || this.order.getShippingDate()) && |
| 297 |
!this.order.getPartner() |
| 298 |
) { |
| 299 |
const confirmed = await ask(this.pos.dialog, { |
| 300 |
title: _t("Please select the Customer"), |
| 301 |
body: _t( |
| 302 |
"You need to select the customer before you can invoice or ship an order." |
| 303 |
), |
| 304 |
}); |
| 305 |
if (confirmed) { |
| 306 |
this.pos.selectPartner(); |
| 307 |
} |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
const partner = this.order.getPartner(); |
| 312 |
if ( |
| 313 |
this.order.getShippingDate() && |
| 314 |
!(partner.name && partner.street && partner.city && partner.country_id) |
| 315 |
) { |
| 316 |
this.pos.dialog.add(AlertDialog, { |
| 317 |
title: _t("Incorrect address for shipping"), |
| 318 |
body: _t("The selected customer needs an address."), |
| 319 |
}); |
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
if (!this.order.presetRequirementsFilled) { |
| 324 |
const { field, message } = this.order.uiState.requiredPartnerDetails || {}; |
| 325 |
this.pos.dialog.add(AlertDialog, { |
| 326 |
title: field ? _t("%s required", field) : _t("Missing required"), |
| 327 |
body: message || _t("Some required information is missing."), |
| 328 |
}); |
| 329 |
return false; |
| 330 |
} |
| 331 |
|
| 332 |
if ( |
| 333 |
!this.pos.currency.isZero(this.order.priceIncl) && |
| 334 |
this.order.payment_ids.length === 0 |
| 335 |
) { |
| 336 |
this.pos.notification.add(_t("Select a payment method to validate the order.")); |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
if (!this.order.isPaid() || this.invoicing) { |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
// The exact amount must be paid if there is no cash payment method defined. |
| 345 |
if ( |
| 346 |
Math.abs(this.order.priceIncl - this.order.amountPaid + this.order.appliedRounding) > |
| 347 |
0.00001 |
| 348 |
) { |
| 349 |
if (!this.pos.models["pos.payment.method"].some((pm) => pm.is_cash_count)) { |
| 350 |
this.pos.dialog.add(AlertDialog, { |
| 351 |
title: _t("Cannot return change without a cash payment method"), |
| 352 |
body: _t( |
| 353 |
"There is no cash payment method available in this point of sale to handle the change.\n\n Please pay the exact amount or add a cash payment method in the point of sale configuration" |
| 354 |
), |
| 355 |
}); |
| 356 |
return false; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// if the change is too large, it's probably an input error, make the user confirm. |
| 361 |
if ( |
| 362 |
!isForceValidate && |
| 363 |
this.order.priceIncl > 0 && |
| 364 |
this.order.priceIncl * 1000 < this.order.amountPaid |
| 365 |
) { |
| 366 |
this.pos.dialog.add(ConfirmationDialog, { |
| 367 |
title: _t("Please Confirm Large Amount"), |
| 368 |
body: |
| 369 |
_t("Are you sure that the customer wants to pay") + |
| 370 |
" " + |
| 371 |
this.pos.env.utils.formatCurrency(this.order.amountPaid) + |
| 372 |
" " + |
| 373 |
_t("for an order of") + |
| 374 |
" " + |
| 375 |
this.pos.env.utils.formatCurrency(this.order.priceIncl) + |
| 376 |
" " + |
| 377 |
_t('? Clicking "Confirm" will validate the payment.'), |
| 378 |
confirm: () => this.validateOrder(true), |
| 379 |
}); |
| 380 |
return false; |
| 381 |
} |
| 382 |
|
| 383 |
if (!this.order._isValidEmptyOrder()) { |
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
return true; |
| 388 |
} |
| 389 |
|
| 390 |
async _askForCustomerIfRequired() { |
| 391 |
const splitPayments = this.order.payment_ids.filter( |
| 392 |
(payment) => payment.payment_method_id.split_transactions |
| 393 |
); |
| 394 |
if (splitPayments.length && !this.order.getPartner()) { |
| 395 |
const paymentMethod = splitPayments[0].payment_method_id; |
| 396 |
const confirmed = await ask(this.pos.dialog, { |
| 397 |
title: _t("Customer Required"), |
| 398 |
body: _t("Customer is required for %s payment method.", paymentMethod.name), |
| 399 |
}); |
| 400 |
if (confirmed) { |
| 401 |
await this.pos.selectPartner(); |
| 402 |
} |
| 403 |
return false; |
| 404 |
} |
| 405 |
} |
| 406 |
} |