mirror of
https://github.com/ae-utbm/sith.git
synced 2025-11-22 20:56:59 +00:00
make Refilling.payment_method a SmallIntegerField
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"),
|
||||
]
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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}
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user