diff --git a/addons/dine360_restaurant/static/src/js/equal_split.js b/addons/dine360_restaurant/static/src/js/equal_split.js index 0bbc7cf..0222857 100644 --- a/addons/dine360_restaurant/static/src/js/equal_split.js +++ b/addons/dine360_restaurant/static/src/js/equal_split.js @@ -1,74 +1,55 @@ /** @odoo-module */ -import { SplitBillScreen } from "@pos_restaurant/overrides/components/split_bill_screen/split_bill_screen"; -import { patch } from "@web/core/utils/patch"; -import { useState } from "@odoo/owl"; +import { registry } from "@web/core/registry"; +import { usePos } from "@point_of_sale/app/store/pos_hook"; +import { Component, useState } from "@odoo/owl"; + +export class EqualSplitScreen extends Component { + static template = "dine360_restaurant.EqualSplitScreen"; -patch(SplitBillScreen.prototype, { setup() { - super.setup(...arguments); - this.equalSplit = useState({ - active: false, - numPeople: 2, - }); - }, + this.pos = usePos(); + this.state = useState({ numPeople: 2 }); + } - toggleEqualSplit() { - this.equalSplit.active = !this.equalSplit.active; - }, + get order() { + return this.pos.get_order(); + } - decreasePeople() { - if (this.equalSplit.numPeople > 2) { - this.equalSplit.numPeople--; - } - }, - - increasePeople() { - if (this.equalSplit.numPeople < 20) { - this.equalSplit.numPeople++; - } - }, - - get equalAmountPerPerson() { - const order = this.pos.get_order(); - if (!order || this.equalSplit.numPeople < 1) return 0; - return order.getTotalWithTax() / this.equalSplit.numPeople; - }, + get formattedTotal() { + const order = this.order; + return this.env.utils.formatCurrency(order ? order.getTotalWithTax() : 0); + } get formattedEqualAmount() { - return this.env.utils.formatCurrency(this.equalAmountPerPerson); - }, + const order = this.order; + if (!order || this.state.numPeople < 1) return this.env.utils.formatCurrency(0); + return this.env.utils.formatCurrency(order.getTotalWithTax() / this.state.numPeople); + } - get formattedOrderTotal() { - const order = this.pos.get_order(); - if (!order) return this.env.utils.formatCurrency(0); - return this.env.utils.formatCurrency(order.getTotalWithTax()); - }, + decreasePeople() { + if (this.state.numPeople > 2) this.state.numPeople--; + } - // Intercept pay() to auto-assign proportional lines when equal split is active - async pay() { - if (this.equalSplit.active) { - await this._payEqualSplit(); - } else { - await super.pay(...arguments); - } - }, + increasePeople() { + if (this.state.numPeople < 20) this.state.numPeople++; + } - async _payEqualSplit() { - const order = this.pos.get_order(); + back() { + this.pos.showScreen("SplitBillScreen"); + } + + async chargeCurrentPerson() { + const order = this.order; if (!order) return; - const lines = order.get_orderlines(); - if (lines.length === 0) return; - - const N = this.equalSplit.numPeople; + const N = this.state.numPeople; const total = order.getTotalWithTax(); const target = Math.round((total / N) * 100) / 100; - + const lines = order.get_orderlines(); + const splitlines = {}; let accumulated = 0; - const splitData = {}; - // Greedy fill: assign lines until we reach the per-person target amount for (const line of lines) { if (accumulated >= target - 0.005) break; @@ -79,53 +60,27 @@ patch(SplitBillScreen.prototype, { const needed = target - accumulated; if (lineTotal <= needed + 0.005) { - // Take the full line - splitData[line.uid] = line.qty; + splitlines[line.uid] = line.qty; accumulated += lineTotal; } else { - // Take a partial quantity to fill up to target const qtyToTake = Math.round((needed / perUnit) * 1000) / 1000; if (qtyToTake > 0.001) { - splitData[line.uid] = qtyToTake; + splitlines[line.uid] = qtyToTake; accumulated += qtyToTake * perUnit; } } } - // Write into whichever splitlines object the screen uses - this._setSplitlines(splitData); - - await super.pay(...arguments); - }, + if (typeof this.pos.splitOrder === "function") { + await this.pos.splitOrder(order, splitlines); + } + this.pos.showScreen("PaymentScreen"); + } _getLineTotal(line) { - // Try the standard Odoo 17 method, fall back to price * qty - if (typeof line.get_price_with_tax === "function") { - return line.get_price_with_tax(); - } - if (typeof line.getDisplayData === "function") { - const d = line.getDisplayData(); - return d.totalPrice || line.price * line.qty; - } + if (typeof line.get_price_with_tax === "function") return line.get_price_with_tax(); return (line.price || 0) * (line.qty || 1); - }, + } +} - _setSplitlines(newLines) { - // Odoo 17 may store splitlines on this.splitlines or this.state.splitlines - const target = - this.splitlines !== undefined - ? this.splitlines - : this.state && this.state.splitlines !== undefined - ? this.state.splitlines - : null; - - if (!target) return; - - for (const key of Object.keys(target)) { - delete target[key]; - } - for (const [uid, qty] of Object.entries(newLines)) { - target[uid] = qty; - } - }, -}); +registry.category("pos_screens").add("EqualSplitScreen", EqualSplitScreen); diff --git a/addons/dine360_restaurant/static/src/xml/equal_split.xml b/addons/dine360_restaurant/static/src/xml/equal_split.xml index 65ae798..91901cf 100644 --- a/addons/dine360_restaurant/static/src/xml/equal_split.xml +++ b/addons/dine360_restaurant/static/src/xml/equal_split.xml @@ -1,79 +1,85 @@ - - + + +
- - - -
- - By Item - - +

+ Equal Split +

- -
+ +
-
- Order Total: +
+ Order Total:
-
- -
-
+
+
people
-
-
-
Each person pays
+
+
Each person pays
-
+ + + +
- Press Charge to collect from the first person. - Repeat for each person. + After payment, return to the table and press Split again. + Decrease the count by 1 each time.
+
+ + + + +
+ + Split By Item + + +
-