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:
Antoine Bartuccio 2025-02-21 14:50:07 +01:00
parent a96b374ad7
commit 1978658b9c
4 changed files with 171 additions and 135 deletions

View File

@ -1,14 +1,13 @@
import { exportToHtml } from "#core:utils/globals";
import { BasketItem } from "#counter:counter/basket"; import { BasketItem } from "#counter:counter/basket";
import type { CounterConfig, ErrorMessage } from "#counter:counter/types"; 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", () => { document.addEventListener("alpine:init", () => {
Alpine.data("counter", () => ({ Alpine.data("counter", (config: CounterConfig) => ({
basket: {} as Record<string, BasketItem>, basket: {} as Record<string, BasketItem>,
errors: [], errors: [],
customerBalance: config.customerBalance, customerBalance: config.customerBalance,
codeField: undefined, codeField: null as CounterProductSelect | null,
alertMessage: { alertMessage: {
content: "", content: "",
show: false, show: false,
@ -121,10 +120,7 @@ exportToHtml("loadCounter", (config: CounterConfig) => {
}, },
handleCode() { handleCode() {
const [quantity, code] = this.codeField.getSelectedProduct() as [ const [quantity, code] = this.codeField.getSelectedProduct() as [number, string];
number,
string,
];
if (this.codeField.getOperationCodes().includes(code.toUpperCase())) { if (this.codeField.getOperationCodes().includes(code.toUpperCase())) {
if (code === "ANN") { if (code === "ANN") {
@ -141,7 +137,6 @@ exportToHtml("loadCounter", (config: CounterConfig) => {
}, },
})); }));
}); });
});
$(() => { $(() => {
/* Accordion UI between basket and refills */ /* Accordion UI between basket and refills */

View File

@ -27,7 +27,13 @@
{% block content %} {% block content %}
<h4>{{ counter }}</h4> <h4>{{ counter }}</h4>
<div id="bar-ui" x-data="counter"> <div id="bar-ui" x-data="counter({
customerBalance: {{ customer.amount }},
products: products,
customerId: {{ customer.pk }},
formInitial: formInitial,
cancelUrl: '{{ cancel_url }}',
})">
<noscript> <noscript>
<p class="important">Javascript is required for the counter UI.</p> <p class="important">Javascript is required for the counter UI.</p>
</noscript> </noscript>
@ -256,13 +262,7 @@
{%- endfor -%} {%- endfor -%}
]; ];
window.addEventListener("DOMContentLoaded", () => { window.addEventListener("DOMContentLoaded", () => {
loadCounter({ loadCounter();
customerBalance: {{ customer.amount }},
products: products,
customerId: {{ customer.pk }},
formInitial: formInitial,
cancelUrl: "{{ cancel_url }}",
});
}); });
</script> </script>
{% endblock script %} {% endblock script %}

View File

@ -681,6 +681,42 @@ class TestCounterClick(TestFullClickBase):
-3 - settings.SITH_ECOCUP_LIMIT -3 - settings.SITH_ECOCUP_LIMIT
) )
def test_recordings_when_negative(self):
self.refill_user(
self.customer,
self.cons.selling_price * 3 + Decimal(self.beer.selling_price),
)
self.customer.customer.recorded_products = settings.SITH_ECOCUP_LIMIT * -10
self.customer.customer.save()
self.login_in_bar(self.barmen)
assert (
self.submit_basket(
self.customer,
[BasketItem(self.dcons.id, 1)],
).status_code
== 200
)
assert self.updated_amount(
self.customer
) == self.cons.selling_price * 3 + Decimal(self.beer.selling_price)
assert (
self.submit_basket(
self.customer,
[BasketItem(self.cons.id, 3)],
).status_code
== 302
)
assert self.updated_amount(self.customer) == Decimal(self.beer.selling_price)
assert (
self.submit_basket(
self.customer,
[BasketItem(self.beer.id, 1)],
).status_code
== 302
)
assert self.updated_amount(self.customer) == 0
class TestCounterStats(TestCase): class TestCounterStats(TestCase):
@classmethod @classmethod

View File

@ -126,6 +126,11 @@ class BaseBasketForm(BaseFormSet):
if form.product.is_unrecord_product: if form.product.is_unrecord_product:
self.total_recordings += form.cleaned_data["quantity"] self.total_recordings += form.cleaned_data["quantity"]
# We don't want to block an user that have negative recordings
# if he isn't recording anything or reducing it's recording count
if self.total_recordings <= 0:
return
if not customer.can_record_more(self.total_recordings): if not customer.can_record_more(self.total_recordings):
raise ValidationError(_("This user have reached his recording limit")) raise ValidationError(_("This user have reached his recording limit"))