adapt formulas to new price system

This commit is contained in:
imperosol
2026-03-06 15:27:22 +01:00
parent 680dc44486
commit 0f1660ad79
11 changed files with 39 additions and 97 deletions

View File

@@ -1,11 +1,11 @@
import type { Product } from "#counter:counter/types";
import type { CounterItem } from "#counter:counter/types";
export class BasketItem {
quantity: number;
product: Product;
product: CounterItem;
errors: string[];
constructor(product: Product, quantity: number) {
constructor(product: CounterItem, quantity: number) {
this.quantity = quantity;
this.product = product;
this.errors = [];

View File

@@ -2,6 +2,7 @@ import { AlertMessage } from "#core:utils/alert-message";
import { BasketItem } from "#counter:counter/basket";
import type {
CounterConfig,
CounterItem,
ErrorMessage,
ProductFormula,
} from "#counter:counter/types";
@@ -63,8 +64,10 @@ document.addEventListener("alpine:init", () => {
},
checkFormulas() {
// Try to find a formula.
// A formula is found if all its elements are already in the basket
const products = new Set(
Object.keys(this.basket).map((i: string) => Number.parseInt(i)),
Object.values(this.basket).map((item: BasketItem) => item.product.productId),
);
const formula: ProductFormula = config.formulas.find((f: ProductFormula) => {
return f.products.every((p: number) => products.has(p));
@@ -72,22 +75,29 @@ document.addEventListener("alpine:init", () => {
if (formula === undefined) {
return;
}
// Now that the formula is found, remove the items composing it from the basket
for (const product of formula.products) {
const key = product.toString();
const key = Object.entries(this.basket).find(
([_, i]: [string, BasketItem]) => i.product.productId === product,
)[0];
this.basket[key].quantity -= 1;
if (this.basket[key].quantity <= 0) {
this.removeFromBasket(key);
}
}
// Then add the result product of the formula to the basket
const result = Object.values(config.products)
.filter((item: CounterItem) => item.productId === formula.result)
.reduce((acc, curr) => (acc.price.amount < curr.price.amount ? acc : curr));
this.addToBasket(result.price.id, 1);
this.alertMessage.display(
interpolate(
gettext("Formula %(formula)s applied"),
{ formula: config.products[formula.result.toString()].name },
{ formula: result.name },
true,
),
{ success: true },
);
this.addToBasket(formula.result.toString(), 1);
},
getBasketSize() {

View File

@@ -2,7 +2,7 @@ export type ErrorMessage = string;
export interface InitialFormData {
/* Used to refill the form when the backend raises an error */
id?: keyof Record<string, Product>;
id?: keyof Record<string, CounterItem>;
quantity?: number;
errors?: string[];
}
@@ -15,7 +15,7 @@ export interface ProductFormula {
export interface CounterConfig {
customerBalance: number;
customerId: number;
products: Record<string, Product>;
products: Record<string, CounterItem>;
formulas: ProductFormula[];
formInitial: InitialFormData[];
cancelUrl: string;
@@ -26,10 +26,11 @@ interface Price {
amount: number;
}
export interface Product {
export interface CounterItem {
productId: number;
price: Price;
code: string;
name: string;
price: Price;
hasTrayPrice: boolean;
quantityForTrayPrice: number;
}