Don't use codes as a primary key in counter click

This commit is contained in:
Antoine Bartuccio 2024-12-20 18:34:27 +01:00
parent e464809865
commit eaf72ad0ed
3 changed files with 17 additions and 26 deletions

View File

@ -11,6 +11,7 @@ interface CounterConfig {
}
interface Product {
id: string;
code: string;
name: string;
price: number;
@ -56,18 +57,11 @@ exportToHtml("loadCounter", (config: CounterConfig) => {
.setAttribute(":value", "getBasketSize()");
},
getItemIdFromCode(code: string): string {
return Object.keys(config.products).find(
(key) => config.products[key].code === code,
);
removeFromBasket(id: string) {
delete this.basket[id];
},
removeFromBasket(code: string) {
delete this.basket[this.getItemIdFromCode(code)];
},
addToBasket(code: string, quantity: number): [boolean, string] {
const id = this.getItemIdFromCode(code);
addToBasket(id: string, quantity: number): [boolean, string] {
const item: BasketItem =
this.basket[id] || new BasketItem(config.products[id], 0);
@ -126,7 +120,7 @@ exportToHtml("loadCounter", (config: CounterConfig) => {
});
},
handleCode(event: SubmitEvent) {
handleCode() {
const [quantity, code] = this.codeField.getSelectedProduct() as [
number,
string,

View File

@ -63,7 +63,7 @@
{% for category in categories.keys() %}
<optgroup label="{{ category }}">
{% for product in categories[category] %}
<option value="{{ product.code }}">{{ product }}</option>
<option value="{{ product.id }}">{{ product }}</option>
{% endfor %}
</optgroup>
{% endfor %}
@ -86,17 +86,17 @@
</div>
<template x-for="(item, index) in Object.values(basket)">
<ul>
<button @click.prevent="addToBasket(item.product.code, -1)">-</button>
<button @click.prevent="addToBasket(item.product.id, -1)">-</button>
<span x-text="item.quantity"></span>
<button @click.prevent="addToBasket(item.product.code, 1)">+</button>
<button @click.prevent="addToBasket(item.product.id, 1)">+</button>
<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>
<button @click.prevent="removeFromBasket(item.product.code)"><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.product.code" :id="`id_form-${index}-code`" :name="`form-${index}-code`" required readonly>
<input type="hidden" :value="item.product.id" :id="`id_form-${index}-id`" :name="`form-${index}-id`" required readonly>
</ul>
</template>
@ -149,7 +149,7 @@
<div id="cat_{{ category|slugify }}">
<h5>{{ category }}</h5>
{% for product in categories[category] -%}
<button @click="addToBasket('{{ product.code }}', 1)">
<button @click="addToBasket('{{ product.id }}', 1)">
<strong>{{ product.name }}</strong>
{% if product.icon %}
<img src="{{ product.icon.url }}" alt="image de {{ product.name }}"/>
@ -171,7 +171,7 @@
const products = {
{%- for p in products -%}
{{ p.id }}: {
code: "{{ p.code }}",
id: "{{ p.id }}",
name: "{{ p.name }}",
price: {{ p.price }},
hasTrayPrice: {{ p.tray | tojson }},

View File

@ -19,8 +19,8 @@ from django.db import transaction
from django.db.models import F
from django.forms import (
BaseFormSet,
Form,
IntegerField,
ModelForm,
ValidationError,
formset_factory,
)
@ -47,12 +47,9 @@ def get_operator(counter: Counter, customer: Customer) -> User:
return counter.get_random_barman()
class ProductForm(ModelForm):
class ProductForm(Form):
quantity = IntegerField(min_value=1)
class Meta:
model = Product
fields = ["code"]
id = IntegerField(min_value=0)
def __init__(
self,
@ -75,13 +72,13 @@ class ProductForm(ModelForm):
user = self.customer.user
# We store self.product so we can use it later on the formset validation
self.product = self.counter.products.filter(code=cleaned_data["code"]).first()
self.product = self.counter.products.filter(id=cleaned_data["id"]).first()
if self.product is None:
raise ValidationError(
_(
"Product %(product)s doesn't exist or isn't available on this counter"
)
% {"product": cleaned_data["code"]}
% {"product": cleaned_data["id"]}
)
# Test alcohoolic products