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

View File

@ -79,11 +79,12 @@
</div>
{% endfor %}
<p>{% trans %}Basket: {% endtrans %}</p>
<form method="post" action="" x-ref="basketForm">
<form x-cloak method="post" action="" x-ref="basketForm">
{% csrf_token %}
<div x-ref="basketManagementForm">
{{ form.management_form }}
</div>
<ul x-show="getBasketSize() === 0">{% trans %}This basket is empty{% endtrans %}</ul>
<template x-for="(item, index) in Object.values(basket)">
<ul>
<template x-for="error in item.errors">
@ -97,7 +98,7 @@
<span x-text="item.product.name"></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>
<input type="hidden" :value="item.quantity" :id="`id_form-${index}-quantity`" :name="`form-${index}-quantity`" required readonly>
@ -112,7 +113,7 @@
</p>
<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 %}"/>
</div>
</form>
@ -188,6 +189,7 @@
name: "{{ product.name }}",
price: {{ product.price }},
hasTrayPrice: {{ product.tray | tojson }},
quantityForTrayPrice: {{ product.QUANTITY_FOR_TRAY_PRICE }},
},
{%- endfor -%}
};

View File

@ -106,9 +106,8 @@ class BaseBasketForm(BaseFormSet):
self._check_enough_money(self[0].counter, self[0].customer)
def _check_forms_have_errors(self):
for form in self:
if len(form.errors):
raise ValidationError(_("Submmited basket is invalid"))
if any(len(form.errors) > 0 for form in self):
raise ValidationError(_("Submmited basket is invalid"))
def _check_enough_money(self, counter: Counter, customer: Customer):
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()
# To get all forms errors to the javascript, we create a list of error list
kwargs["form_errors"] = []
for field_errors in kwargs["form"].errors:
kwargs["form_errors"].append(list(field_errors.values()))
kwargs["form_errors"] = [
list(field_error.values()) for field_error in kwargs["form"].errors
]
if self.object.type == "BAR":
kwargs["student_card_fragment"] = StudentCardFormView.get_template_data(
self.customer