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/templates/digitalglarus/login.html b/digitalglarus/templates/digitalglarus/login.html index e8aa40e1..f61a3f9b 100644 --- a/digitalglarus/templates/digitalglarus/login.html +++ b/digitalglarus/templates/digitalglarus/login.html @@ -49,7 +49,7 @@

Forgot password?Find ID/Password

-

Not a member yet?Sign up now.

+

Not a member yet?Sign up now.

diff --git a/digitalglarus/views.py b/digitalglarus/views.py index f2dfb4ab..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) @@ -366,6 +369,15 @@ class MembershipPaymentView(LoginRequiredMixin, IsNotMemberMixin, FormView): # Create Billing Address billing_address = form.save() + # Create Billing Address for User if he does not have one + if not customer.user.billing_addresses.count(): + data.update({ + 'user': customer.user.id + }) + billing_address_user_form = UserBillingAddressForm(data) + billing_address_user_form.is_valid() + billing_address_user_form.save() + # Create membership plan membership_data = {'type': membership_type} membership = Membership.create(membership_data) @@ -454,12 +466,16 @@ class MembershipDeactivateView(LoginRequiredMixin, UpdateView): class UserBillingAddressView(LoginRequiredMixin, UpdateView): model = UserBillingAddress form_class = UserBillingAddressForm - template_name = "digitalglarus/user_billing_address.html" + template_name = "digitalglarus/user_billing_address.html" success_url = reverse_lazy('digitalglarus:user_billing_address') def get_form_kwargs(self): current_billing_address = self.request.user.billing_addresses.first() form_kwargs = super(UserBillingAddressView, self).get_form_kwargs() + + if not current_billing_address: + return form_kwargs + form_kwargs.update({ 'initial': { 'street_address': current_billing_address.street_address, @@ -546,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, diff --git a/utils/views.py b/utils/views.py index 50f4aed4..e69bac1d 100644 --- a/utils/views.py +++ b/utils/views.py @@ -17,7 +17,10 @@ class SignupViewMixin(CreateView): success_url = None def get_success_url(self): - next_url = self.request.POST.get('next', self.success_url) + + next_url = self.request.POST.get('next') if self.request.POST.get('next')\ + else self.success_url + return next_url def form_valid(self, form):