forked from alaguraj/odoo-testing-addons
update order handling in EqualSplitScreen for improved split functionality
This commit is contained in:
parent
bff3db82de
commit
33dd4214ee
@ -3,6 +3,7 @@
|
|||||||
import { registry } from "@web/core/registry";
|
import { registry } from "@web/core/registry";
|
||||||
import { usePos } from "@point_of_sale/app/store/pos_hook";
|
import { usePos } from "@point_of_sale/app/store/pos_hook";
|
||||||
import { Component, useState } from "@odoo/owl";
|
import { Component, useState } from "@odoo/owl";
|
||||||
|
import { Order } from "@point_of_sale/app/store/models";
|
||||||
|
|
||||||
export class EqualSplitScreen extends Component {
|
export class EqualSplitScreen extends Component {
|
||||||
static template = "dine360_restaurant.EqualSplitScreen";
|
static template = "dine360_restaurant.EqualSplitScreen";
|
||||||
@ -18,13 +19,13 @@ export class EqualSplitScreen extends Component {
|
|||||||
|
|
||||||
get formattedTotal() {
|
get formattedTotal() {
|
||||||
const order = this.order;
|
const order = this.order;
|
||||||
return this.env.utils.formatCurrency(order ? order.getTotalWithTax() : 0);
|
return this.env.utils.formatCurrency(order ? order.get_total_with_tax() : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
get formattedEqualAmount() {
|
get formattedEqualAmount() {
|
||||||
const order = this.order;
|
const order = this.order;
|
||||||
if (!order || this.state.numPeople < 1) return this.env.utils.formatCurrency(0);
|
if (!order || this.state.numPeople < 1) return this.env.utils.formatCurrency(0);
|
||||||
return this.env.utils.formatCurrency(order.getTotalWithTax() / this.state.numPeople);
|
return this.env.utils.formatCurrency(order.get_total_with_tax() / this.state.numPeople);
|
||||||
}
|
}
|
||||||
|
|
||||||
decreasePeople() {
|
decreasePeople() {
|
||||||
@ -44,7 +45,7 @@ export class EqualSplitScreen extends Component {
|
|||||||
if (!order) return;
|
if (!order) return;
|
||||||
|
|
||||||
const N = this.state.numPeople;
|
const N = this.state.numPeople;
|
||||||
const total = order.getTotalWithTax();
|
const total = order.get_total_with_tax();
|
||||||
const target = Math.round((total / N) * 100) / 100;
|
const target = Math.round((total / N) * 100) / 100;
|
||||||
const lines = order.get_orderlines();
|
const lines = order.get_orderlines();
|
||||||
const splitlines = {};
|
const splitlines = {};
|
||||||
@ -54,32 +55,92 @@ export class EqualSplitScreen extends Component {
|
|||||||
if (accumulated >= target - 0.005) break;
|
if (accumulated >= target - 0.005) break;
|
||||||
|
|
||||||
const lineTotal = this._getLineTotal(line);
|
const lineTotal = this._getLineTotal(line);
|
||||||
const perUnit = line.qty > 0 ? lineTotal / line.qty : 0;
|
const qty = line.get_quantity();
|
||||||
|
const perUnit = qty > 0 ? lineTotal / qty : 0;
|
||||||
if (perUnit <= 0) continue;
|
if (perUnit <= 0) continue;
|
||||||
|
|
||||||
const needed = target - accumulated;
|
const needed = target - accumulated;
|
||||||
|
|
||||||
if (lineTotal <= needed + 0.005) {
|
if (lineTotal <= needed + 0.005) {
|
||||||
splitlines[line.uid] = line.qty;
|
splitlines[line.id] = qty;
|
||||||
accumulated += lineTotal;
|
accumulated += lineTotal;
|
||||||
} else {
|
} else {
|
||||||
const qtyToTake = Math.round((needed / perUnit) * 1000) / 1000;
|
const qtyToTake = Math.round((needed / perUnit) * 1000) / 1000;
|
||||||
if (qtyToTake > 0.001) {
|
if (qtyToTake > 0.001) {
|
||||||
splitlines[line.uid] = qtyToTake;
|
splitlines[line.id] = qtyToTake;
|
||||||
accumulated += qtyToTake * perUnit;
|
accumulated += qtyToTake * perUnit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof this.pos.splitOrder === "function") {
|
const splitKeys = Object.keys(splitlines);
|
||||||
await this.pos.splitOrder(order, splitlines);
|
if (splitKeys.length === 0) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newOrder = new Order(
|
||||||
|
{ env: this.env },
|
||||||
|
{
|
||||||
|
pos: this.pos,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const id of splitKeys) {
|
||||||
|
const splitQty = splitlines[id];
|
||||||
|
const line = order.get_orderline(parseInt(id));
|
||||||
|
if (!line) continue;
|
||||||
|
|
||||||
|
const orderline = line.clone();
|
||||||
|
newOrder.add_orderline(orderline);
|
||||||
|
orderline.set_quantity(splitQty, "do not recompute unit price");
|
||||||
|
|
||||||
|
if (!this.pos.disallowLineQuantityChange()) {
|
||||||
|
line.set_quantity(
|
||||||
|
line.get_quantity() - splitQty,
|
||||||
|
"do not recompute unit price"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const decreaseLine = line.clone();
|
||||||
|
decreaseLine.order = order;
|
||||||
|
decreaseLine.noDecrease = true;
|
||||||
|
decreaseLine.set_quantity(-splitQty);
|
||||||
|
order.add_orderline(decreaseLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.pos.disallowLineQuantityChange()) {
|
||||||
|
for (const id of splitKeys) {
|
||||||
|
const line = order.get_orderline(parseInt(id));
|
||||||
|
if (line && Math.abs(line.get_quantity()) < 0.00001) {
|
||||||
|
order.removeOrderline(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newOrder.set_screen_data({ name: "PaymentScreen" });
|
||||||
|
|
||||||
|
if (this.pos.orderPreparationCategories.size) {
|
||||||
|
order.updateLastOrderChange();
|
||||||
|
newOrder.updateLastOrderChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
newOrder.setCustomerCount(1);
|
||||||
|
newOrder.originalSplittedOrder = order;
|
||||||
|
|
||||||
|
const newCustomerCount = order.getCustomerCount() - 1;
|
||||||
|
order.setCustomerCount(newCustomerCount || 1);
|
||||||
|
order.set_screen_data({ name: "ProductScreen" });
|
||||||
|
|
||||||
|
const reactiveNewOrder = this.pos.makeOrderReactive(newOrder);
|
||||||
|
this.pos.orders.add(reactiveNewOrder);
|
||||||
|
this.pos.selectedOrder = reactiveNewOrder;
|
||||||
|
|
||||||
this.pos.showScreen("PaymentScreen");
|
this.pos.showScreen("PaymentScreen");
|
||||||
}
|
}
|
||||||
|
|
||||||
_getLineTotal(line) {
|
_getLineTotal(line) {
|
||||||
if (typeof line.get_price_with_tax === "function") return line.get_price_with_tax();
|
if (typeof line.get_price_with_tax === "function") return line.get_price_with_tax();
|
||||||
return (line.price || 0) * (line.qty || 1);
|
return (line.get_unit_price() || 0) * (line.get_quantity() || 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user