From e1a43f1d29c62368a0e3b014df4cc7fe011b4234 Mon Sep 17 00:00:00 2001 From: Levi Date: Fri, 14 Oct 2016 00:51:39 -0500 Subject: [PATCH] Fixed free days amount when user should pay extra membership months --- digitalglarus/models.py | 38 ++++++++++++------- .../digitalglarus/booking_payment.html | 2 +- digitalglarus/views.py | 8 +++- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/digitalglarus/models.py b/digitalglarus/models.py index 9d423f3d..7ed0f53a 100644 --- a/digitalglarus/models.py +++ b/digitalglarus/models.py @@ -87,15 +87,6 @@ class Membership(models.Model): class MembershipOrder(Ordereable, models.Model): membership = models.ForeignKey(Membership) - def first_membership_range_date(self): - start_date = self.created_at - _, days_in_month = calendar.monthrange(start_date.year, - start_date.month) - pass_days = start_date.day - days_left = days_in_month - pass_days - end_date = start_date + timedelta(days=days_left) - return start_date, end_date - @classmethod def current_membership(cls, user): last_payment = cls.objects.\ @@ -107,6 +98,21 @@ class MembershipOrder(Ordereable, models.Model): end_date = start_date + timedelta(days=days_in_month) return start_date, end_date + def first_membership_range_date(self): + start_date = self.created_at + _, days_in_month = calendar.monthrange(start_date.year, + start_date.month) + pass_days = start_date.day + days_left = days_in_month - pass_days + end_date = start_date + timedelta(days=days_left) + return start_date, end_date + + def get_membership_order_cc_data(self): + return { + 'last4': self.last4, + 'cc_brand': self.cc_brand, + } + def get_membership_range_date(self): start_date = self.created_at _, days_in_month = calendar.monthrange(start_date.year, @@ -174,6 +180,7 @@ class Booking(models.Model): @classmethod def booking_price(cls, user, start_date, end_date): + TWO_DAYS = 2 MAX_MONTH_PRICE = BookingPrice.objects.last().special_month_price MAX_MONTH_DAYS_PROMOTION = 31 MIN_MONTH_DAYS_PROMOTION = 19 @@ -188,9 +195,6 @@ class Booking(models.Model): if remanent_days <= MIN_MONTH_DAYS_PROMOTION else MAX_MONTH_PRICE normal_price = months_prices + remanent_days_price - free_days = cls.get_ramaining_free_days(user, start_date, end_date) - final_booking_price = normal_price - (free_days * price_per_day) - # Calculating membership required months price for booking required_membership_months = 0 membership_booking_price = 0.0 @@ -200,6 +204,14 @@ class Booking(models.Model): required_membership_months = cls.membership_required_booking_months(today, end_date) membership_booking_price = membership_price * required_membership_months + # TO-DO Fix this, what happens when user has free days from his current membership month + if not required_membership_months: + free_days = cls.get_ramaining_free_days(user, start_date, end_date) + else: + free_days = required_membership_months * TWO_DAYS + # free_days += free_days_for_required_memberships + final_booking_price = normal_price - (free_days * price_per_day) + # Add required membership months to final prices final_booking_price += membership_booking_price @@ -222,7 +234,7 @@ class BookingOrder(Ordereable, models.Model): return { 'last4': self.last4, 'cc_brand': self.cc_brand, - } + } if self.last4 and self.cc_brand else None def booking_days(self): return (self.booking.end_date - self.booking.start_date).days + 1 diff --git a/digitalglarus/templates/digitalglarus/booking_payment.html b/digitalglarus/templates/digitalglarus/booking_payment.html index 8613dcab..43137b46 100644 --- a/digitalglarus/templates/digitalglarus/booking_payment.html +++ b/digitalglarus/templates/digitalglarus/booking_payment.html @@ -69,7 +69,7 @@ {% if credit_card_data %}

Credit Card

-

Last 4: {{credit_card_data.last4}}

+

Last 4: *****{{credit_card_data.last4}}

Type: {{credit_card_data.cc_brand}}

diff --git a/digitalglarus/views.py b/digitalglarus/views.py index 473b68a7..0fa81637 100644 --- a/digitalglarus/views.py +++ b/digitalglarus/views.py @@ -170,13 +170,16 @@ class BookingPaymentView(LoginRequiredMixin, MembershipRequiredMixin, FormView): for key in self.booking_needed_fields} user = self.request.user last_booking_order = BookingOrder.objects.filter(customer__user=user).last() + last_membership_order = MembershipOrder.objects.filter(customer__user=user).last() + credit_card_data = last_booking_order.get_booking_cc_data() if last_booking_order \ + and last_booking_order.get_booking_cc_data() \ + else last_membership_order.get_membership_order_cc_data() # booking_price_per_day = BookingPrice.objects.get().price_per_day # total_discount = booking_price_per_day * booking_data.get('free_days') booking_data.update({ # 'booking_price_per_day': booking_price_per_day, # 'current_billing_address': self.request.user.billing_addresses.first().to_dict(), - 'credit_card_data': last_booking_order.get_booking_cc_data() if last_booking_order - else None, + 'credit_card_data': credit_card_data if credit_card_data else None, 'stripe_key': settings.STRIPE_API_PUBLIC_KEY }) context.update(booking_data) @@ -559,6 +562,7 @@ class OrdersBookingDetailView(LoginRequiredMixin, DetailView): membership_required_months = bookig_order.membership_required_months membership_required_months_price = bookig_order.membership_required_months_price + original_price += membership_required_months_price context.update({ 'original_price': original_price,