mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 11:59:23 +00:00
Allow transactions on counter when an user has recorded too many products as long as he doesn't record more
This commit is contained in:
@ -1,146 +1,141 @@
|
||||
import { exportToHtml } from "#core:utils/globals";
|
||||
import { BasketItem } from "#counter:counter/basket";
|
||||
import type { CounterConfig, ErrorMessage } from "#counter:counter/types";
|
||||
import type { CounterProductSelect } from "./components/counter-product-select-index";
|
||||
|
||||
exportToHtml("loadCounter", (config: CounterConfig) => {
|
||||
document.addEventListener("alpine:init", () => {
|
||||
Alpine.data("counter", () => ({
|
||||
basket: {} as Record<string, BasketItem>,
|
||||
errors: [],
|
||||
customerBalance: config.customerBalance,
|
||||
codeField: undefined,
|
||||
alertMessage: {
|
||||
content: "",
|
||||
show: false,
|
||||
timeout: null,
|
||||
},
|
||||
document.addEventListener("alpine:init", () => {
|
||||
Alpine.data("counter", (config: CounterConfig) => ({
|
||||
basket: {} as Record<string, BasketItem>,
|
||||
errors: [],
|
||||
customerBalance: config.customerBalance,
|
||||
codeField: null as CounterProductSelect | null,
|
||||
alertMessage: {
|
||||
content: "",
|
||||
show: false,
|
||||
timeout: null,
|
||||
},
|
||||
|
||||
init() {
|
||||
// Fill the basket with the initial data
|
||||
for (const entry of config.formInitial) {
|
||||
if (entry.id !== undefined && entry.quantity !== undefined) {
|
||||
this.addToBasket(entry.id, entry.quantity);
|
||||
this.basket[entry.id].errors = entry.errors ?? [];
|
||||
}
|
||||
init() {
|
||||
// Fill the basket with the initial data
|
||||
for (const entry of config.formInitial) {
|
||||
if (entry.id !== undefined && entry.quantity !== undefined) {
|
||||
this.addToBasket(entry.id, entry.quantity);
|
||||
this.basket[entry.id].errors = entry.errors ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
this.codeField = this.$refs.codeField;
|
||||
this.codeField.widget.focus();
|
||||
this.codeField = this.$refs.codeField;
|
||||
this.codeField.widget.focus();
|
||||
|
||||
// It's quite tricky to manually apply attributes to the management part
|
||||
// of a formset so we dynamically apply it here
|
||||
this.$refs.basketManagementForm
|
||||
.querySelector("#id_form-TOTAL_FORMS")
|
||||
.setAttribute(":value", "getBasketSize()");
|
||||
},
|
||||
// It's quite tricky to manually apply attributes to the management part
|
||||
// of a formset so we dynamically apply it here
|
||||
this.$refs.basketManagementForm
|
||||
.querySelector("#id_form-TOTAL_FORMS")
|
||||
.setAttribute(":value", "getBasketSize()");
|
||||
},
|
||||
|
||||
removeFromBasket(id: string) {
|
||||
removeFromBasket(id: string) {
|
||||
delete this.basket[id];
|
||||
},
|
||||
|
||||
addToBasket(id: string, quantity: number): ErrorMessage {
|
||||
const item: BasketItem =
|
||||
this.basket[id] || new BasketItem(config.products[id], 0);
|
||||
|
||||
const oldQty = item.quantity;
|
||||
item.quantity += quantity;
|
||||
|
||||
if (item.quantity <= 0) {
|
||||
delete this.basket[id];
|
||||
},
|
||||
|
||||
addToBasket(id: string, quantity: number): ErrorMessage {
|
||||
const item: BasketItem =
|
||||
this.basket[id] || new BasketItem(config.products[id], 0);
|
||||
|
||||
const oldQty = item.quantity;
|
||||
item.quantity += quantity;
|
||||
|
||||
if (item.quantity <= 0) {
|
||||
delete this.basket[id];
|
||||
return "";
|
||||
}
|
||||
|
||||
this.basket[id] = item;
|
||||
|
||||
if (this.sumBasket() > this.customerBalance) {
|
||||
item.quantity = oldQty;
|
||||
if (item.quantity === 0) {
|
||||
delete this.basket[id];
|
||||
}
|
||||
return gettext("Not enough money");
|
||||
}
|
||||
|
||||
return "";
|
||||
},
|
||||
}
|
||||
|
||||
getBasketSize() {
|
||||
return Object.keys(this.basket).length;
|
||||
},
|
||||
this.basket[id] = item;
|
||||
|
||||
sumBasket() {
|
||||
if (this.getBasketSize() === 0) {
|
||||
return 0;
|
||||
if (this.sumBasket() > this.customerBalance) {
|
||||
item.quantity = oldQty;
|
||||
if (item.quantity === 0) {
|
||||
delete this.basket[id];
|
||||
}
|
||||
const total = Object.values(this.basket).reduce(
|
||||
(acc: number, cur: BasketItem) => acc + cur.sum(),
|
||||
0,
|
||||
) as number;
|
||||
return total;
|
||||
},
|
||||
return gettext("Not enough money");
|
||||
}
|
||||
|
||||
showAlertMessage(message: string) {
|
||||
if (this.alertMessage.timeout !== null) {
|
||||
clearTimeout(this.alertMessage.timeout);
|
||||
return "";
|
||||
},
|
||||
|
||||
getBasketSize() {
|
||||
return Object.keys(this.basket).length;
|
||||
},
|
||||
|
||||
sumBasket() {
|
||||
if (this.getBasketSize() === 0) {
|
||||
return 0;
|
||||
}
|
||||
const total = Object.values(this.basket).reduce(
|
||||
(acc: number, cur: BasketItem) => acc + cur.sum(),
|
||||
0,
|
||||
) as number;
|
||||
return total;
|
||||
},
|
||||
|
||||
showAlertMessage(message: string) {
|
||||
if (this.alertMessage.timeout !== null) {
|
||||
clearTimeout(this.alertMessage.timeout);
|
||||
}
|
||||
this.alertMessage.content = message;
|
||||
this.alertMessage.show = true;
|
||||
this.alertMessage.timeout = setTimeout(() => {
|
||||
this.alertMessage.show = false;
|
||||
this.alertMessage.timeout = null;
|
||||
}, 2000);
|
||||
},
|
||||
|
||||
addToBasketWithMessage(id: string, quantity: number) {
|
||||
const message = this.addToBasket(id, quantity);
|
||||
if (message.length > 0) {
|
||||
this.showAlertMessage(message);
|
||||
}
|
||||
},
|
||||
|
||||
onRefillingSuccess(event: CustomEvent) {
|
||||
if (event.type !== "htmx:after-request" || event.detail.failed) {
|
||||
return;
|
||||
}
|
||||
this.customerBalance += Number.parseFloat(
|
||||
(event.detail.target.querySelector("#id_amount") as HTMLInputElement).value,
|
||||
);
|
||||
document.getElementById("selling-accordion").click();
|
||||
this.codeField.widget.focus();
|
||||
},
|
||||
|
||||
finish() {
|
||||
if (this.getBasketSize() === 0) {
|
||||
this.showAlertMessage(gettext("You can't send an empty basket."));
|
||||
return;
|
||||
}
|
||||
this.$refs.basketForm.submit();
|
||||
},
|
||||
|
||||
cancel() {
|
||||
location.href = config.cancelUrl;
|
||||
},
|
||||
|
||||
handleCode() {
|
||||
const [quantity, code] = this.codeField.getSelectedProduct() as [number, string];
|
||||
|
||||
if (this.codeField.getOperationCodes().includes(code.toUpperCase())) {
|
||||
if (code === "ANN") {
|
||||
this.cancel();
|
||||
}
|
||||
this.alertMessage.content = message;
|
||||
this.alertMessage.show = true;
|
||||
this.alertMessage.timeout = setTimeout(() => {
|
||||
this.alertMessage.show = false;
|
||||
this.alertMessage.timeout = null;
|
||||
}, 2000);
|
||||
},
|
||||
|
||||
addToBasketWithMessage(id: string, quantity: number) {
|
||||
const message = this.addToBasket(id, quantity);
|
||||
if (message.length > 0) {
|
||||
this.showAlertMessage(message);
|
||||
if (code === "FIN") {
|
||||
this.finish();
|
||||
}
|
||||
},
|
||||
|
||||
onRefillingSuccess(event: CustomEvent) {
|
||||
if (event.type !== "htmx:after-request" || event.detail.failed) {
|
||||
return;
|
||||
}
|
||||
this.customerBalance += Number.parseFloat(
|
||||
(event.detail.target.querySelector("#id_amount") as HTMLInputElement).value,
|
||||
);
|
||||
document.getElementById("selling-accordion").click();
|
||||
this.codeField.widget.focus();
|
||||
},
|
||||
|
||||
finish() {
|
||||
if (this.getBasketSize() === 0) {
|
||||
this.showAlertMessage(gettext("You can't send an empty basket."));
|
||||
return;
|
||||
}
|
||||
this.$refs.basketForm.submit();
|
||||
},
|
||||
|
||||
cancel() {
|
||||
location.href = config.cancelUrl;
|
||||
},
|
||||
|
||||
handleCode() {
|
||||
const [quantity, code] = this.codeField.getSelectedProduct() as [
|
||||
number,
|
||||
string,
|
||||
];
|
||||
|
||||
if (this.codeField.getOperationCodes().includes(code.toUpperCase())) {
|
||||
if (code === "ANN") {
|
||||
this.cancel();
|
||||
}
|
||||
if (code === "FIN") {
|
||||
this.finish();
|
||||
}
|
||||
} else {
|
||||
this.addToBasketWithMessage(code, quantity);
|
||||
}
|
||||
this.codeField.widget.clear();
|
||||
this.codeField.widget.focus();
|
||||
},
|
||||
}));
|
||||
});
|
||||
} else {
|
||||
this.addToBasketWithMessage(code, quantity);
|
||||
}
|
||||
this.codeField.widget.clear();
|
||||
this.codeField.widget.focus();
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
$(() => {
|
||||
|
Reference in New Issue
Block a user