Skip to content

Commit dcf77e8

Browse files
committed
[REF] pos_customer_wallet: Refactor to .esm.js
Signed-off-by: Carmen Bianca BAKKER <carmen@coopiteasy.be>
1 parent 0f8eeef commit dcf77e8

File tree

4 files changed

+224
-229
lines changed

4 files changed

+224
-229
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/** @odoo-module alias=pos_customer_wallet.PaymentScreen **/
2+
// SPDX-FileCopyrightText: 2022 Coop IT Easy SC
3+
//
4+
// SPDX-License-Identifier: AGPL-3.0-or-later
5+
6+
import PaymentScreen from "point_of_sale.PaymentScreen";
7+
8+
import Registries from "point_of_sale.Registries";
9+
10+
const WalletPaymentScreen = (PaymentScreen_) =>
11+
class extends PaymentScreen_ {
12+
/* eslint-disable no-unused-vars */
13+
/**
14+
* Overload function.
15+
*
16+
* - If wallet journal is selected, check if customer is selected.
17+
* - if wallet journal is selected, check if wallet amount is sufficient.
18+
*
19+
* @param {Boolean} isForceValidate - Passed to super.
20+
* @returns {Boolean} Whether the order is valid.
21+
*/
22+
async validateOrder(isForceValidate) {
23+
/* eslint-enable no-unused-vars */
24+
var partner = this.currentOrder.get_partner();
25+
var [payment_wallet_amount, payment_lines_qty] =
26+
this.get_amount_debit_with_customer_wallet_journal();
27+
var [product_wallet_amount, product_lines_qty] =
28+
this.get_amount_credit_with_customer_wallet_product();
29+
30+
var wallet_amount = payment_wallet_amount - product_wallet_amount;
31+
32+
if (!partner) {
33+
if (payment_lines_qty > 0) {
34+
this.showPopup("ErrorPopup", {
35+
title: this.env._t("No customer selected"),
36+
body: this.env._t(
37+
"Cannot use customer wallet payment method without selecting a customer.\n\n Please select a customer or use a different payment method."
38+
),
39+
});
40+
return;
41+
}
42+
if (product_lines_qty > 0) {
43+
var wallet_product_names = [];
44+
var wallet_products = this.find_customer_wallet_products();
45+
wallet_products.forEach(function (product) {
46+
wallet_product_names.push(product.display_name);
47+
});
48+
this.showPopup("ErrorPopup", {
49+
title: this.env._t("No customer selected"),
50+
body:
51+
this.env._t("Cannot sell the product '") +
52+
wallet_product_names.join(",") +
53+
this.env._t(
54+
"' without selecting a customer. Please select a customer or remove the order line(s)."
55+
),
56+
});
57+
return;
58+
}
59+
} else if (this.is_balance_above_minimum(partner, wallet_amount)) {
60+
this.showPopup("ErrorPopup", {
61+
title: this.env._t("Customer wallet balance not sufficient"),
62+
body: this.env._t(
63+
"There is not enough balance in the customer's wallet to perform this order."
64+
),
65+
});
66+
return;
67+
}
68+
69+
await super.validateOrder(...arguments);
70+
}
71+
72+
/**
73+
* Overload function.
74+
*
75+
* Once the order is validated, update the wallet amount
76+
* of the current customer, if defined.
77+
*/
78+
async _finalizeValidation() {
79+
var partner = this.currentOrder.get_partner();
80+
if (partner) {
81+
var payment_wallet_amount =
82+
this.get_amount_debit_with_customer_wallet_journal()[0];
83+
var product_wallet_amount =
84+
this.get_amount_credit_with_customer_wallet_product()[0];
85+
var wallet_amount = payment_wallet_amount - product_wallet_amount;
86+
partner.customer_wallet_balance -= wallet_amount;
87+
}
88+
89+
await super._finalizeValidation();
90+
}
91+
92+
is_balance_above_minimum(client, wallet_amount) {
93+
return (
94+
client.customer_wallet_balance - wallet_amount <=
95+
this.env.pos.config.minimum_wallet_amount - 0.00001
96+
);
97+
}
98+
99+
/**
100+
* Calculate the balance of the customer wallet after completing this
101+
* order.
102+
*
103+
* @returns {Number} New balance.
104+
*/
105+
get new_wallet_amount() {
106+
var partner = this.currentOrder.get_partner();
107+
if (partner) {
108+
var payment_wallet_amount =
109+
this.get_amount_debit_with_customer_wallet_journal()[0];
110+
var product_wallet_amount =
111+
this.get_amount_credit_with_customer_wallet_product()[0];
112+
return (
113+
partner.customer_wallet_balance -
114+
payment_wallet_amount +
115+
product_wallet_amount
116+
);
117+
}
118+
return false;
119+
}
120+
121+
/**
122+
* Return the payment method of the wallet journal, if exists.
123+
*
124+
* @returns A payment method which has a customer
125+
* wallet journal. The first match is returned.
126+
*/
127+
find_customer_wallet_payment_method() {
128+
// This is fairly naive.
129+
for (var i = 0; i < this.payment_methods_from_config.length; i++) {
130+
if (this.payment_methods_from_config[i].is_customer_wallet_method) {
131+
return this.payment_methods_from_config[i];
132+
}
133+
}
134+
return null;
135+
}
136+
137+
/**
138+
* Return the wallet products, if exist.
139+
*
140+
* @returns {list} A list of products which are marked as wallet
141+
* products.
142+
*/
143+
find_customer_wallet_products() {
144+
var wallet_products = [];
145+
for (const value of Object.values(this.env.pos.db.product_by_id)) {
146+
if (value.is_customer_wallet_product) {
147+
wallet_products.push(value);
148+
}
149+
}
150+
return wallet_products;
151+
}
152+
153+
/**
154+
* Return the payment amount with wallet payment method.
155+
*
156+
* @returns {list} A list of two elements. The first element is the
157+
* balance of payment done with wallet payment method. The second
158+
* element is the number of payment lines.
159+
*/
160+
get_amount_debit_with_customer_wallet_journal() {
161+
var order = this.currentOrder;
162+
var method = this.find_customer_wallet_payment_method();
163+
var wallet_amount = 0;
164+
var lines_qty = 0;
165+
order.paymentlines.forEach((item) => {
166+
if (item.payment_method === method) {
167+
wallet_amount += item.amount;
168+
lines_qty += 1;
169+
}
170+
});
171+
return [wallet_amount, lines_qty];
172+
}
173+
174+
/**
175+
* Return the amount credited by wallet products.
176+
*
177+
* @returns {list} A list of two elements. The first element is the
178+
* balance of order lines done with wallet product. The second element
179+
* is the number of order lines.
180+
*/
181+
get_amount_credit_with_customer_wallet_product() {
182+
var order = this.currentOrder;
183+
var wallet_product_ids = [];
184+
var wallet_products = this.find_customer_wallet_products();
185+
wallet_products.forEach(function (product) {
186+
wallet_product_ids.push(product.id);
187+
});
188+
var wallet_amount = 0;
189+
var lines_qty = 0;
190+
191+
order.orderlines.forEach((orderline) => {
192+
if (wallet_product_ids.includes(orderline.product.id)) {
193+
wallet_amount += orderline.get_price_without_tax();
194+
lines_qty += 1;
195+
}
196+
});
197+
198+
return [wallet_amount, lines_qty];
199+
}
200+
};
201+
202+
Registries.Component.extend(PaymentScreen, WalletPaymentScreen);
203+
export default WalletPaymentScreen;

0 commit comments

Comments
 (0)