mirror of
https://github.com/ae-utbm/sith.git
synced 2025-11-22 12:46:58 +00:00
make Refilling.payment_method a SmallIntegerField
This commit is contained in:
@@ -24,12 +24,6 @@
|
|||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
PAYMENT_METHOD = [
|
|
||||||
("CHECK", _("Check")),
|
|
||||||
("CASH", _("Cash")),
|
|
||||||
("CARD", _("Credit card")),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class CounterConfig(AppConfig):
|
class CounterConfig(AppConfig):
|
||||||
name = "counter"
|
name = "counter"
|
||||||
|
|||||||
@@ -136,7 +136,10 @@ class GetUserForm(forms.Form):
|
|||||||
|
|
||||||
|
|
||||||
class RefillForm(forms.ModelForm):
|
class RefillForm(forms.ModelForm):
|
||||||
allowed_refilling_methods = ["CASH", "CARD"]
|
allowed_refilling_methods = [
|
||||||
|
Refilling.PaymentMethod.CASH,
|
||||||
|
Refilling.PaymentMethod.CARD,
|
||||||
|
]
|
||||||
|
|
||||||
error_css_class = "error"
|
error_css_class = "error"
|
||||||
required_css_class = "required"
|
required_css_class = "required"
|
||||||
|
|||||||
@@ -2,18 +2,42 @@
|
|||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
from django.db.migrations.state import StateApps
|
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 = apps.get_model("counter", "Selling")
|
||||||
Selling.objects.filter(payment_method_str="CARD").update(payment_method=1)
|
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 = apps.get_model("counter", "Selling")
|
||||||
Selling.objects.filter(payment_method=1).update(payment_method_str="CARD")
|
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):
|
class Migration(migrations.Migration):
|
||||||
dependencies = [("counter", "0034_alter_selling_date_selling_date_month_idx")]
|
dependencies = [("counter", "0034_alter_selling_date_selling_date_month_idx")]
|
||||||
|
|
||||||
@@ -35,6 +59,26 @@ class Migration(migrations.Migration):
|
|||||||
verbose_name="payment method",
|
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.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.fields import ResizedImageField
|
||||||
from core.models import Group, Notification, User
|
from core.models import Group, Notification, User
|
||||||
from core.utils import get_start_of_semester
|
from core.utils import get_start_of_semester
|
||||||
from counter.apps import PAYMENT_METHOD
|
|
||||||
from counter.fields import CurrencyField
|
from counter.fields import CurrencyField
|
||||||
from subscription.models import Subscription
|
from subscription.models import Subscription
|
||||||
|
|
||||||
@@ -732,6 +731,11 @@ class RefillingQuerySet(models.QuerySet):
|
|||||||
class Refilling(models.Model):
|
class Refilling(models.Model):
|
||||||
"""Handle the refilling."""
|
"""Handle the refilling."""
|
||||||
|
|
||||||
|
class PaymentMethod(models.IntegerChoices):
|
||||||
|
CARD = 0, _("Credit card")
|
||||||
|
CASH = 1, _("Cash")
|
||||||
|
CHECK = 2, _("Check")
|
||||||
|
|
||||||
counter = models.ForeignKey(
|
counter = models.ForeignKey(
|
||||||
Counter, related_name="refillings", blank=False, on_delete=models.CASCADE
|
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
|
Customer, related_name="refillings", blank=False, on_delete=models.CASCADE
|
||||||
)
|
)
|
||||||
date = models.DateTimeField(_("date"))
|
date = models.DateTimeField(_("date"))
|
||||||
payment_method = models.CharField(
|
payment_method = models.PositiveSmallIntegerField(
|
||||||
_("payment method"), max_length=255, choices=PAYMENT_METHOD, default="CARD"
|
_("payment method"), choices=PaymentMethod, default=PaymentMethod.CARD
|
||||||
)
|
)
|
||||||
|
|
||||||
objects = RefillingQuerySet.as_manager()
|
objects = RefillingQuerySet.as_manager()
|
||||||
|
|||||||
@@ -115,14 +115,10 @@ class TestRefilling(TestFullClickBase):
|
|||||||
) -> HttpResponse:
|
) -> HttpResponse:
|
||||||
used_client = client if client is not None else self.client
|
used_client = client if client is not None else self.client
|
||||||
return used_client.post(
|
return used_client.post(
|
||||||
reverse(
|
reverse("counter:refilling_create", kwargs={"customer_id": user.pk}),
|
||||||
"counter:refilling_create",
|
{"amount": str(amount), "payment_method": Refilling.PaymentMethod.CASH},
|
||||||
kwargs={"customer_id": user.pk},
|
|
||||||
),
|
|
||||||
{"amount": str(amount), "payment_method": "CASH"},
|
|
||||||
HTTP_REFERER=reverse(
|
HTTP_REFERER=reverse(
|
||||||
"counter:click",
|
"counter:click", kwargs={"counter_id": counter.id, "user_id": user.pk}
|
||||||
kwargs={"counter_id": counter.id, "user_id": user.pk},
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,9 @@ class InvoiceCallView(
|
|||||||
end_date = start_date + relativedelta(months=1)
|
end_date = start_date + relativedelta(months=1)
|
||||||
|
|
||||||
kwargs["sum_cb"] = Refilling.objects.filter(
|
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"]
|
).aggregate(res=Sum("amount", default=0))["res"]
|
||||||
kwargs["sum_cb"] += (
|
kwargs["sum_cb"] += (
|
||||||
Selling.objects.filter(
|
Selling.objects.filter(
|
||||||
|
|||||||
@@ -253,7 +253,7 @@ class Invoice(models.Model):
|
|||||||
customer=customer,
|
customer=customer,
|
||||||
operator=self.user,
|
operator=self.user,
|
||||||
amount=i.product_unit_price * i.quantity,
|
amount=i.product_unit_price * i.quantity,
|
||||||
payment_method="CARD",
|
payment_method=Refilling.PaymentMethod.CARD,
|
||||||
date=self.date,
|
date=self.date,
|
||||||
)
|
)
|
||||||
new.save()
|
new.save()
|
||||||
|
|||||||
@@ -275,7 +275,9 @@ class EbouticPayWithSith(CanViewMixin, SingleObjectMixin, View):
|
|||||||
return redirect("eboutic:payment_result", "failure")
|
return redirect("eboutic:payment_result", "failure")
|
||||||
|
|
||||||
eboutic = get_eboutic()
|
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:
|
try:
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
# Selling.save has some important business logic in it.
|
# Selling.save has some important business logic in it.
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ from django.views.generic import CreateView, DetailView, TemplateView
|
|||||||
from django.views.generic.edit import FormView
|
from django.views.generic.edit import FormView
|
||||||
|
|
||||||
from core.views.group import PermissionGroupsUpdateView
|
from core.views.group import PermissionGroupsUpdateView
|
||||||
from counter.apps import PAYMENT_METHOD
|
|
||||||
from subscription.forms import (
|
from subscription.forms import (
|
||||||
SelectionDateForm,
|
SelectionDateForm,
|
||||||
SubscriptionExistingUserForm,
|
SubscriptionExistingUserForm,
|
||||||
@@ -129,6 +128,6 @@ class SubscriptionsStatsView(FormView):
|
|||||||
subscription_end__gte=self.end_date, subscription_start__lte=self.start_date
|
subscription_end__gte=self.end_date, subscription_start__lte=self.start_date
|
||||||
)
|
)
|
||||||
kwargs["subscriptions_types"] = settings.SITH_SUBSCRIPTIONS
|
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
|
kwargs["locations"] = settings.SITH_SUBSCRIPTION_LOCATIONS
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|||||||
Reference in New Issue
Block a user