From 447ddc7c2eec77b7651e2d79779b773fcaa13e47 Mon Sep 17 00:00:00 2001 From: Levi Date: Thu, 29 Sep 2016 00:04:40 -0500 Subject: [PATCH] finishing digitalglarus booking --- .../migrations/0018_auto_20160928_0424.py | 27 +++++++++++ .../migrations/0019_auto_20160929_0324.py | 25 ++++++++++ digitalglarus/mixins.py | 4 +- digitalglarus/models.py | 36 ++++++++++++-- .../digitalglarus/booking_orders_detail.html | 10 +++- .../digitalglarus/booking_payment.html | 11 +++-- digitalglarus/views.py | 48 +++++++++++++------ 7 files changed, 137 insertions(+), 24 deletions(-) create mode 100644 digitalglarus/migrations/0018_auto_20160928_0424.py create mode 100644 digitalglarus/migrations/0019_auto_20160929_0324.py diff --git a/digitalglarus/migrations/0018_auto_20160928_0424.py b/digitalglarus/migrations/0018_auto_20160928_0424.py new file mode 100644 index 00000000..10a92271 --- /dev/null +++ b/digitalglarus/migrations/0018_auto_20160928_0424.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2016-09-28 04:24 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('digitalglarus', '0017_membership_active'), + ] + + operations = [ + migrations.AddField( + model_name='bookingorder', + name='membership_required_months', + field=models.IntegerField(default=0), + preserve_default=False, + ), + migrations.AddField( + model_name='bookingorder', + name='membership_required_months_price', + field=models.FloatField(default=0), + preserve_default=False, + ), + ] diff --git a/digitalglarus/migrations/0019_auto_20160929_0324.py b/digitalglarus/migrations/0019_auto_20160929_0324.py new file mode 100644 index 00000000..f78cf3e7 --- /dev/null +++ b/digitalglarus/migrations/0019_auto_20160929_0324.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2016-09-29 03:24 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('digitalglarus', '0018_auto_20160928_0424'), + ] + + operations = [ + migrations.AlterField( + model_name='bookingorder', + name='membership_required_months', + field=models.IntegerField(default=0), + ), + migrations.AlterField( + model_name='bookingorder', + name='membership_required_months_price', + field=models.FloatField(default=0), + ), + ] diff --git a/digitalglarus/mixins.py b/digitalglarus/mixins.py index 40777bf3..013cdd78 100644 --- a/digitalglarus/mixins.py +++ b/digitalglarus/mixins.py @@ -9,7 +9,7 @@ class MembershipRequiredMixin(object): def dispatch(self, request, *args, **kwargs): from .models import Membership - if not Membership.is_digitalglarus_member(request.user): + if not Membership.is_digitalglarus_active_member(request.user): return HttpResponseRedirect(self.membership_redirect_url) return super(MembershipRequiredMixin, self).dispatch(request, *args, **kwargs) @@ -20,7 +20,7 @@ class IsNotMemberMixin(object): def dispatch(self, request, *args, **kwargs): from .models import Membership - if Membership.is_digitalglarus_member(request.user): + if Membership.is_digitalglarus_active_member(request.user): return HttpResponseRedirect(self.already_member_redirect_url) return super(IsNotMemberMixin, self).dispatch(request, *args, **kwargs) diff --git a/digitalglarus/models.py b/digitalglarus/models.py index 5fdf6607..0147d53b 100644 --- a/digitalglarus/models.py +++ b/digitalglarus/models.py @@ -16,8 +16,9 @@ from .mixins import Ordereable class MembershipType(models.Model): + STANDARD = 'standard' MEMBERSHIP_TYPES = ( - ('standard', 'Standard'), + (STANDARD, 'Standard'), ) name = models.CharField(choices=MEMBERSHIP_TYPES, max_length=20) @@ -63,7 +64,7 @@ class Membership(models.Model): active = models.BooleanField(default=True) @classmethod - def is_digitalglarus_member(cls, user): + def is_digitalglarus_active_member(cls, user): past_month = (datetime.today() - relativedelta(months=1)).month has_booking_current_month = Q(membershiporder__customer__user=user, membershiporder__created_at__month=datetime.today().month) @@ -158,6 +159,14 @@ class Booking(models.Model): total_free_days = months * TWO_DAYS + free_days_this_month return total_free_days + @staticmethod + def membership_required_booking_months(start_date, end_date): + start_month = start_date.month + end_month = end_date.month + months = abs(start_month - (end_month + 12) if end_month < start_month + else end_month - start_month) + return months + @classmethod def booking_price(cls, user, start_date, end_date): @@ -178,13 +187,34 @@ class Booking(models.Model): free_days = cls.get_ramaining_free_days(user, start_date, end_date) final_booking_price = normal_price - (free_days * price_per_day) - return normal_price, final_booking_price, free_days + # Calculating membership required months price for booking + required_membership_months = 0 + membership_booking_price = 0.0 + if BookingOrder.user_has_not_bookings(user): + today = datetime.today().date() + membership_price = MembershipType.objects.get(name=MembershipType.STANDARD).price + required_membership_months = cls.membership_required_booking_months(today, end_date) + membership_booking_price = membership_price * required_membership_months + + # Add required membership months to final prices + final_booking_price += membership_booking_price + + return normal_price, final_booking_price, free_days,\ + required_membership_months, membership_booking_price class BookingOrder(Ordereable, models.Model): booking = models.OneToOneField(Booking) original_price = models.FloatField() special_month_price = models.FloatField() + membership_required_months = models.IntegerField(default=0) + membership_required_months_price = models.FloatField(default=0) + + + @classmethod + def user_has_not_bookings(cls, user): + return cls.objects.filter(customer__user=user).exists() + def booking_days(self): return (self.booking.end_date - self.booking.start_date).days + 1 diff --git a/digitalglarus/templates/digitalglarus/booking_orders_detail.html b/digitalglarus/templates/digitalglarus/booking_orders_detail.html index d4dac3b7..f3d7b049 100644 --- a/digitalglarus/templates/digitalglarus/booking_orders_detail.html +++ b/digitalglarus/templates/digitalglarus/booking_orders_detail.html @@ -44,6 +44,10 @@

Total days {{booking_days}}

{{original_price|floatformat}}CHF

+ {% if membership_required_months and membership_required_months_price %} +

Required Membership months: {{membership_required_months}}

+

{{membership_required_months_price|floatformat}}CHF

+ {% endif %} {% if free_days %}

Free days {{free_days}}

-{{total_discount|floatformat}}CHF

@@ -55,7 +59,7 @@ -
+

Order Summary

@@ -65,6 +69,10 @@

Total days {{booking_days}}

{{original_price|floatformat}}CHF

+ {% if membership_required_months and membership_required_months_price %} +

Required Membership months: {{membership_required_months}}

+

{{membership_required_months_price|floatformat}}CHF

+ {% endif %} {% if free_days %}

Free days {{free_days}}

-{{total_discount|floatformat}}CHF

diff --git a/digitalglarus/templates/digitalglarus/booking_payment.html b/digitalglarus/templates/digitalglarus/booking_payment.html index 336ebf70..fda2a0e2 100644 --- a/digitalglarus/templates/digitalglarus/booking_payment.html +++ b/digitalglarus/templates/digitalglarus/booking_payment.html @@ -121,15 +121,20 @@

-

Total days {{booking_days}}

+

Total days: {{booking_days}}

{{original_price|floatformat}}CHF

+ {% if membership_required_months and membership_required_months_price %} +

Required Membership months: {{membership_required_months}}

+

{{membership_required_months_price|floatformat}}CHF

+ {% endif %} +
{% if free_days %} -

Free days {{free_days}}

+

Free days: {{free_days}}

-{{total_discount|floatformat}}CHF

{% endif %}

Total

-

{{discount_price|floatformat}}CHF

+

{{final_price|floatformat}}CHF