diff --git a/counter/apps.py b/counter/apps.py index 9348cd1c..54e7ad4c 100644 --- a/counter/apps.py +++ b/counter/apps.py @@ -24,12 +24,6 @@ from django.apps import AppConfig from django.utils.translation import gettext_lazy as _ -PAYMENT_METHOD = [ - ("CHECK", _("Check")), - ("CASH", _("Cash")), - ("CARD", _("Credit card")), -] - class CounterConfig(AppConfig): name = "counter" diff --git a/counter/forms.py b/counter/forms.py index 1b59855a..e2bf016e 100644 --- a/counter/forms.py +++ b/counter/forms.py @@ -136,7 +136,10 @@ class GetUserForm(forms.Form): class RefillForm(forms.ModelForm): - allowed_refilling_methods = ["CASH", "CARD"] + allowed_refilling_methods = [ + Refilling.PaymentMethod.CASH, + Refilling.PaymentMethod.CARD, + ] error_css_class = "error" required_css_class = "required" diff --git a/counter/migrations/0035_remove_selling_is_validated_and_more.py b/counter/migrations/0035_remove_selling_is_validated_and_more.py index 94513a43..a8a2e115 100644 --- a/counter/migrations/0035_remove_selling_is_validated_and_more.py +++ b/counter/migrations/0035_remove_selling_is_validated_and_more.py @@ -2,18 +2,42 @@ from django.db import migrations, models from django.db.migrations.state import StateApps +from django.db.models import Case, When -def migrate_payment_method(apps: StateApps, schema_editor): +def migrate_selling_payment_method(apps: StateApps, schema_editor): + # 0 <=> SITH_ACCOUNT is the default value, so no need to migrate it Selling = apps.get_model("counter", "Selling") Selling.objects.filter(payment_method_str="CARD").update(payment_method=1) -def migrate_payment_method_reverse(apps: StateApps, schema_editor): +def migrate_selling_payment_method_reverse(apps: StateApps, schema_editor): Selling = apps.get_model("counter", "Selling") Selling.objects.filter(payment_method=1).update(payment_method_str="CARD") +def migrate_refilling_payment_method(apps: StateApps, schema_editor): + Refilling = apps.get_model("counter", "Refilling") + Refilling.objects.update( + payment_method=Case( + When(payment_method_str="CARD", then=0), + When(payment_method_str="CASH", then=1), + When(payment_method_str="CHECK", then=2), + ) + ) + + +def migrate_refilling_payment_method_reverse(apps: StateApps, schema_editor): + Refilling = apps.get_model("counter", "Refilling") + Refilling.objects.update( + payment_method_str=Case( + When(payment_method=0, then="CARD"), + When(payment_method=1, then="CASH"), + When(payment_method=2, then="CHECK"), + ) + ) + + class Migration(migrations.Migration): dependencies = [("counter", "0034_alter_selling_date_selling_date_month_idx")] @@ -35,6 +59,26 @@ class Migration(migrations.Migration): verbose_name="payment method", ), ), - migrations.RunPython(migrate_payment_method, migrate_payment_method_reverse), + migrations.RunPython( + migrate_selling_payment_method, migrate_selling_payment_method_reverse + ), migrations.RemoveField(model_name="selling", name="payment_method_str"), + migrations.RenameField( + model_name="refilling", + old_name="payment_method", + new_name="payment_method_str", + ), + migrations.AddField( + model_name="refilling", + name="payment_method", + field=models.PositiveSmallIntegerField( + choices=[(0, "Credit card"), (1, "Cash"), (2, "Check")], + default=0, + verbose_name="payment method", + ), + ), + migrations.RunPython( + migrate_refilling_payment_method, migrate_refilling_payment_method_reverse + ), + migrations.RemoveField(model_name="refilling", name="payment_method_str"), ] diff --git a/counter/models.py b/counter/models.py index 4f0bee12..996f22e6 100644 --- a/counter/models.py +++ b/counter/models.py @@ -44,7 +44,6 @@ from club.models import Club from core.fields import ResizedImageField from core.models import Group, Notification, User from core.utils import get_start_of_semester -from counter.apps import PAYMENT_METHOD from counter.fields import CurrencyField from subscription.models import Subscription @@ -732,6 +731,11 @@ class RefillingQuerySet(models.QuerySet): class Refilling(models.Model): """Handle the refilling.""" + class PaymentMethod(models.IntegerChoices): + CARD = 0, _("Credit card") + CASH = 1, _("Cash") + CHECK = 2, _("Check") + counter = models.ForeignKey( Counter, related_name="refillings", blank=False, on_delete=models.CASCADE ) @@ -746,8 +750,8 @@ class Refilling(models.Model): Customer, related_name="refillings", blank=False, on_delete=models.CASCADE ) date = models.DateTimeField(_("date")) - payment_method = models.CharField( - _("payment method"), max_length=255, choices=PAYMENT_METHOD, default="CARD" + payment_method = models.PositiveSmallIntegerField( + _("payment method"), choices=PaymentMethod, default=PaymentMethod.CARD ) objects = RefillingQuerySet.as_manager() diff --git a/counter/tests/test_counter.py b/counter/tests/test_counter.py index e30a2b43..4f3e2df6 100644 --- a/counter/tests/test_counter.py +++ b/counter/tests/test_counter.py @@ -115,14 +115,10 @@ class TestRefilling(TestFullClickBase): ) -> HttpResponse: used_client = client if client is not None else self.client return used_client.post( - reverse( - "counter:refilling_create", - kwargs={"customer_id": user.pk}, - ), - {"amount": str(amount), "payment_method": "CASH"}, + reverse("counter:refilling_create", kwargs={"customer_id": user.pk}), + {"amount": str(amount), "payment_method": Refilling.PaymentMethod.CASH}, HTTP_REFERER=reverse( - "counter:click", - kwargs={"counter_id": counter.id, "user_id": user.pk}, + "counter:click", kwargs={"counter_id": counter.id, "user_id": user.pk} ), ) diff --git a/counter/views/invoice.py b/counter/views/invoice.py index d990b609..f6512f6c 100644 --- a/counter/views/invoice.py +++ b/counter/views/invoice.py @@ -67,7 +67,9 @@ class InvoiceCallView( end_date = start_date + relativedelta(months=1) kwargs["sum_cb"] = Refilling.objects.filter( - payment_method="CARD", date__gte=start_date, date__lte=end_date + payment_method=Refilling.PaymentMethod.CARD, + date__gte=start_date, + date__lte=end_date, ).aggregate(res=Sum("amount", default=0))["res"] kwargs["sum_cb"] += ( Selling.objects.filter( diff --git a/eboutic/models.py b/eboutic/models.py index 2a4e14b5..5a37f70f 100644 --- a/eboutic/models.py +++ b/eboutic/models.py @@ -253,7 +253,7 @@ class Invoice(models.Model): customer=customer, operator=self.user, amount=i.product_unit_price * i.quantity, - payment_method="CARD", + payment_method=Refilling.PaymentMethod.CARD, date=self.date, ) new.save() diff --git a/eboutic/views.py b/eboutic/views.py index 74cdc7ad..f48c86c1 100644 --- a/eboutic/views.py +++ b/eboutic/views.py @@ -275,7 +275,9 @@ class EbouticPayWithSith(CanViewMixin, SingleObjectMixin, View): return redirect("eboutic:payment_result", "failure") eboutic = get_eboutic() - sales = basket.generate_sales(eboutic, basket.user, Selling.PaymentMethod.SITH_ACCOUNT) + sales = basket.generate_sales( + eboutic, basket.user, Selling.PaymentMethod.SITH_ACCOUNT + ) try: with transaction.atomic(): # Selling.save has some important business logic in it. diff --git a/subscription/views.py b/subscription/views.py index 035f9035..fb4cde07 100644 --- a/subscription/views.py +++ b/subscription/views.py @@ -24,7 +24,6 @@ from django.views.generic import CreateView, DetailView, TemplateView from django.views.generic.edit import FormView from core.views.group import PermissionGroupsUpdateView -from counter.apps import PAYMENT_METHOD from subscription.forms import ( SelectionDateForm, SubscriptionExistingUserForm, @@ -129,6 +128,6 @@ class SubscriptionsStatsView(FormView): subscription_end__gte=self.end_date, subscription_start__lte=self.start_date ) kwargs["subscriptions_types"] = settings.SITH_SUBSCRIPTIONS - kwargs["payment_types"] = PAYMENT_METHOD + kwargs["payment_types"] = settings.SITH_SUBSCRIPTION_PAYMENT_METHOD kwargs["locations"] = settings.SITH_SUBSCRIPTION_LOCATIONS return kwargs