Improve empty basket and tray price management

This commit is contained in:
Antoine Bartuccio 2024-12-22 12:00:42 +01:00
parent 7071553c3b
commit 372470b44b
3 changed files with 17 additions and 15 deletions

View File

@ -1,10 +1,8 @@
import { exportToHtml } from "#core:utils/globals"; import { exportToHtml } from "#core:utils/globals";
const quantityForTrayPrice = 6;
interface InitialFormData { interface InitialFormData {
/* Used to refill the form when the backend raises an error */ /* Used to refill the form when the backend raises an error */
id?: string; id?: Pick<Product, "id">;
quantity?: number; quantity?: number;
errors?: string[]; errors?: string[];
} }
@ -23,11 +21,13 @@ interface Product {
name: string; name: string;
price: number; price: number;
hasTrayPrice: boolean; hasTrayPrice: boolean;
quantityForTrayPrice: number;
} }
class BasketItem { class BasketItem {
quantity: number; quantity: number;
product: Product; product: Product;
quantityForTrayPrice: number;
errors: string[]; errors: string[];
constructor(product: Product, quantity: number) { constructor(product: Product, quantity: number) {
@ -40,7 +40,7 @@ class BasketItem {
if (!this.product.hasTrayPrice) { if (!this.product.hasTrayPrice) {
return 0; return 0;
} }
return Math.floor(this.quantity / quantityForTrayPrice); return Math.floor(this.quantity / this.product.quantityForTrayPrice);
} }
sum(): number { sum(): number {
@ -127,7 +127,9 @@ exportToHtml("loadCounter", (config: CounterConfig) => {
}, },
finish() { finish() {
this.$refs.basketForm.submit(); if (this.getBasketSize() > 0) {
this.$refs.basketForm.submit();
}
}, },
cancel() { cancel() {

View File

@ -79,11 +79,12 @@
</div> </div>
{% endfor %} {% endfor %}
<p>{% trans %}Basket: {% endtrans %}</p> <p>{% trans %}Basket: {% endtrans %}</p>
<form method="post" action="" x-ref="basketForm"> <form x-cloak method="post" action="" x-ref="basketForm">
{% csrf_token %} {% csrf_token %}
<div x-ref="basketManagementForm"> <div x-ref="basketManagementForm">
{{ form.management_form }} {{ form.management_form }}
</div> </div>
<ul x-show="getBasketSize() === 0">{% trans %}This basket is empty{% endtrans %}</ul>
<template x-for="(item, index) in Object.values(basket)"> <template x-for="(item, index) in Object.values(basket)">
<ul> <ul>
<template x-for="error in item.errors"> <template x-for="error in item.errors">
@ -97,7 +98,7 @@
<span x-text="item.product.name"></span> : <span x-text="item.product.name"></span> :
<span x-text="item.sum().toLocaleString(undefined, { minimumFractionDigits: 2 })">€</span> <span x-text="item.sum().toLocaleString(undefined, { minimumFractionDigits: 2 })">€</span>
<span x-cloak x-show="item.getBonusQuantity() > 0" x-text="`${item.getBonusQuantity()} x P`"></span> <span x-show="item.getBonusQuantity() > 0" x-text="`${item.getBonusQuantity()} x P`"></span>
<button @click.prevent="removeFromBasket(item.product.id)"><i class="fa fa-trash-can delete-action"></i></button> <button @click.prevent="removeFromBasket(item.product.id)"><i class="fa fa-trash-can delete-action"></i></button>
<input type="hidden" :value="item.quantity" :id="`id_form-${index}-quantity`" :name="`form-${index}-quantity`" required readonly> <input type="hidden" :value="item.quantity" :id="`id_form-${index}-quantity`" :name="`form-${index}-quantity`" required readonly>
@ -112,7 +113,7 @@
</p> </p>
<div class="row"> <div class="row">
<input class="btn btn-blue" type="submit" @click.prevent="finish" value="{% trans %}Finish{% endtrans %}"/> <input class="btn btn-blue" type="submit" @click.prevent="finish" :disabled="getBasketSize() == 0" value="{% trans %}Finish{% endtrans %}"/>
<input class="btn btn-grey" type="submit" @click.prevent="cancel" value="{% trans %}Cancel{% endtrans %}"/> <input class="btn btn-grey" type="submit" @click.prevent="cancel" value="{% trans %}Cancel{% endtrans %}"/>
</div> </div>
</form> </form>
@ -188,6 +189,7 @@
name: "{{ product.name }}", name: "{{ product.name }}",
price: {{ product.price }}, price: {{ product.price }},
hasTrayPrice: {{ product.tray | tojson }}, hasTrayPrice: {{ product.tray | tojson }},
quantityForTrayPrice: {{ product.QUANTITY_FOR_TRAY_PRICE }},
}, },
{%- endfor -%} {%- endfor -%}
}; };

View File

@ -106,9 +106,8 @@ class BaseBasketForm(BaseFormSet):
self._check_enough_money(self[0].counter, self[0].customer) self._check_enough_money(self[0].counter, self[0].customer)
def _check_forms_have_errors(self): def _check_forms_have_errors(self):
for form in self: if any(len(form.errors) > 0 for form in self):
if len(form.errors): raise ValidationError(_("Submmited basket is invalid"))
raise ValidationError(_("Submmited basket is invalid"))
def _check_enough_money(self, counter: Counter, customer: Customer): def _check_enough_money(self, counter: Counter, customer: Customer):
self.total_price = sum([data["total_price"] for data in self.cleaned_data]) self.total_price = sum([data["total_price"] for data in self.cleaned_data])
@ -272,10 +271,9 @@ class CounterClick(CounterTabsMixin, CanViewMixin, SingleObjectMixin, FormView):
kwargs["cancel_url"] = self.get_success_url() kwargs["cancel_url"] = self.get_success_url()
# To get all forms errors to the javascript, we create a list of error list # To get all forms errors to the javascript, we create a list of error list
kwargs["form_errors"] = [] kwargs["form_errors"] = [
for field_errors in kwargs["form"].errors: list(field_error.values()) for field_error in kwargs["form"].errors
kwargs["form_errors"].append(list(field_errors.values())) ]
if self.object.type == "BAR": if self.object.type == "BAR":
kwargs["student_card_fragment"] = StudentCardFormView.get_template_data( kwargs["student_card_fragment"] = StudentCardFormView.get_template_data(
self.customer self.customer