[FIX] point_of_sale: sync orders on pos closing

Step to reproduce:

- Open a pos session
- Turn off the connection between the server and your POS instance
- Create an order offline
- Turn back on the connection between the server and your POS instance
- Close the POS session

Issue:

The order created offline is not synced to the server

Cause:

The sync of the order has been implemented for the button to go back to
the backend without closing but not for the closing session button.

Solution:

Make the synch mechanism common to both buttons.

closes odoo/odoo#151016

X-original-commit: 4d57ce4c740d6a3350318b4dbcd49c9f2f0f104c
Signed-off-by: Joseph Caburnay (jcb) <jcb@odoo.com>
This commit is contained in:
fja-odoo
2024-01-30 15:15:10 +00:00
parent d2db535ec1
commit 99fa3ec267
3 changed files with 51 additions and 45 deletions
@@ -159,6 +159,11 @@ export class ClosePosPopup extends AbstractAwaitablePopup {
}
async closeSession() {
this.customerDisplay?.update({ closeUI: true });
// If there are orders in the db left unsynced, we try to sync.
const syncSuccess = await this.pos.push_orders_with_closing_popup();
if (!syncSuccess) {
return;
}
if (this.pos.config.cash_control) {
const response = await this.orm.call(
"pos.session",
@@ -270,19 +270,14 @@ export class PaymentScreen extends Component {
}
this.currentOrder.finalized = true;
// 1. Save order to server.
this.env.services.ui.block();
const syncOrderResult = await this.pos.push_single_order(this.currentOrder);
this.env.services.ui.unblock();
if (syncOrderResult instanceof ConnectionLostError) {
this.pos.showScreen(this.nextScreen);
return;
} else if (!syncOrderResult) {
return;
}
let syncOrderResult;
try {
// 1. Save order to server.
syncOrderResult = await this.pos.push_single_order(this.currentOrder);
if (!syncOrderResult) {
return;
}
// 2. Invoice.
if (this.shouldDownloadInvoice() && this.currentOrder.is_to_invoice()) {
if (syncOrderResult[0]?.account_move) {
@@ -299,11 +294,14 @@ export class PaymentScreen extends Component {
}
} catch (error) {
if (error instanceof ConnectionLostError) {
this.pos.showScreen(this.nextScreen);
Promise.reject(error);
return error;
} else {
throw error;
}
} finally {
this.env.services.ui.unblock()
}
// 3. Post process.
@@ -1098,6 +1098,38 @@ export class PosStore extends Reactive {
});
}
// To be used in the context of closing the POS
// Saves the order locally and try to send it to the backend.
// If there is an error show a popup
async push_orders_with_closing_popup (opts = {}) {
try {
await this.push_orders(opts);
return true;
} catch (error) {
console.warn(error);
const reason = this.failed
? _t(
'Some orders could not be submitted to ' +
'the server due to configuration errors. ' +
'You can exit the Point of Sale, but do ' +
'not close the session before the issue ' +
'has been resolved.'
)
: _t(
'Some orders could not be submitted to ' +
'the server due to internet connection issues. ' +
'You can exit the Point of Sale, but do ' +
'not close the session before the issue ' +
'has been resolved.'
);
await this.env.services.popup.add(ConfirmPopup, {
title: _t('Offline Orders'),
body: reason,
});
return false;
}
}
push_orders(opts = {}) {
return this.pushOrderMutex.exec(() => this._flush_orders(this.db.get_orders(), opts));
}
@@ -1118,10 +1150,7 @@ export class PosStore extends Reactive {
}
return server_ids;
} catch (error) {
if (error instanceof ConnectionLostError) {
Promise.reject(error);
return error;
} else {
if (!(error instanceof ConnectionLostError)) {
for (const order of orders) {
const reactiveOrder = this.orders.find((o) => o.uid === order.id);
reactiveOrder.finalized = false;
@@ -1129,8 +1158,8 @@ export class PosStore extends Reactive {
this.db.save_unpaid_order(reactiveOrder);
}
this.set_synch("connected");
throw error;
}
throw error;
} finally {
this._after_flush_orders(orders);
}
@@ -1801,35 +1830,9 @@ export class PosStore extends Reactive {
}
// If there are orders in the db left unsynced, we try to sync.
// If sync successful, close without asking.
// Otherwise, ask again saying that some orders are not yet synced.
try {
await this.push_orders();
window.location = "/web#action=point_of_sale.action_client_pos_menu";
} catch (error) {
console.warn(error);
const reason = this.failed
? _t(
"Some orders could not be submitted to " +
"the server due to configuration errors. " +
"You can exit the Point of Sale, but do " +
"not close the session before the issue " +
"has been resolved."
)
: _t(
"Some orders could not be submitted to " +
"the server due to internet connection issues. " +
"You can exit the Point of Sale, but do " +
"not close the session before the issue " +
"has been resolved."
);
const { confirmed } = await this.popup.add(ConfirmPopup, {
title: _t("Offline Orders"),
body: reason,
});
if (confirmed) {
window.location = "/web#action=point_of_sale.action_client_pos_menu";
}
const syncSuccess = await this.push_orders_with_closing_popup();
if (syncSuccess) {
window.location = '/web#action=point_of_sale.action_client_pos_menu';
}
}
async selectPartner() {