Fixed free days amount when user should pay extra membership months
This commit is contained in:
parent
05a165095d
commit
e1a43f1d29
3 changed files with 32 additions and 16 deletions
|
@ -87,15 +87,6 @@ class Membership(models.Model):
|
||||||
class MembershipOrder(Ordereable, models.Model):
|
class MembershipOrder(Ordereable, models.Model):
|
||||||
membership = models.ForeignKey(Membership)
|
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
|
@classmethod
|
||||||
def current_membership(cls, user):
|
def current_membership(cls, user):
|
||||||
last_payment = cls.objects.\
|
last_payment = cls.objects.\
|
||||||
|
@ -107,6 +98,21 @@ class MembershipOrder(Ordereable, models.Model):
|
||||||
end_date = start_date + timedelta(days=days_in_month)
|
end_date = start_date + timedelta(days=days_in_month)
|
||||||
return start_date, end_date
|
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):
|
def get_membership_range_date(self):
|
||||||
start_date = self.created_at
|
start_date = self.created_at
|
||||||
_, days_in_month = calendar.monthrange(start_date.year,
|
_, days_in_month = calendar.monthrange(start_date.year,
|
||||||
|
@ -174,6 +180,7 @@ class Booking(models.Model):
|
||||||
@classmethod
|
@classmethod
|
||||||
def booking_price(cls, user, start_date, end_date):
|
def booking_price(cls, user, start_date, end_date):
|
||||||
|
|
||||||
|
TWO_DAYS = 2
|
||||||
MAX_MONTH_PRICE = BookingPrice.objects.last().special_month_price
|
MAX_MONTH_PRICE = BookingPrice.objects.last().special_month_price
|
||||||
MAX_MONTH_DAYS_PROMOTION = 31
|
MAX_MONTH_DAYS_PROMOTION = 31
|
||||||
MIN_MONTH_DAYS_PROMOTION = 19
|
MIN_MONTH_DAYS_PROMOTION = 19
|
||||||
|
@ -188,9 +195,6 @@ class Booking(models.Model):
|
||||||
if remanent_days <= MIN_MONTH_DAYS_PROMOTION else MAX_MONTH_PRICE
|
if remanent_days <= MIN_MONTH_DAYS_PROMOTION else MAX_MONTH_PRICE
|
||||||
normal_price = months_prices + remanent_days_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
|
# Calculating membership required months price for booking
|
||||||
required_membership_months = 0
|
required_membership_months = 0
|
||||||
membership_booking_price = 0.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)
|
required_membership_months = cls.membership_required_booking_months(today, end_date)
|
||||||
membership_booking_price = membership_price * required_membership_months
|
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
|
# Add required membership months to final prices
|
||||||
final_booking_price += membership_booking_price
|
final_booking_price += membership_booking_price
|
||||||
|
|
||||||
|
@ -222,7 +234,7 @@ class BookingOrder(Ordereable, models.Model):
|
||||||
return {
|
return {
|
||||||
'last4': self.last4,
|
'last4': self.last4,
|
||||||
'cc_brand': self.cc_brand,
|
'cc_brand': self.cc_brand,
|
||||||
}
|
} if self.last4 and self.cc_brand else None
|
||||||
|
|
||||||
def booking_days(self):
|
def booking_days(self):
|
||||||
return (self.booking.end_date - self.booking.start_date).days + 1
|
return (self.booking.end_date - self.booking.start_date).days + 1
|
||||||
|
|
|
@ -69,7 +69,7 @@
|
||||||
{% if credit_card_data %}
|
{% if credit_card_data %}
|
||||||
<form role="form" id="billing-form" method="post" action="{% url 'digitalglarus:booking_payment' %}" novalidate>
|
<form role="form" id="billing-form" method="post" action="{% url 'digitalglarus:booking_payment' %}" novalidate>
|
||||||
<h2 class="billing-head">Credit Card</h2>
|
<h2 class="billing-head">Credit Card</h2>
|
||||||
<h2 class="membership-lead">Last 4: {{credit_card_data.last4}}</h2>
|
<h2 class="membership-lead">Last 4: *****{{credit_card_data.last4}}</h2>
|
||||||
<h2 class="membership-lead">Type: {{credit_card_data.cc_brand}}</h2>
|
<h2 class="membership-lead">Type: {{credit_card_data.cc_brand}}</h2>
|
||||||
<input type="hidden" name="credit_card_needed" value="false"/>
|
<input type="hidden" name="credit_card_needed" value="false"/>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -170,13 +170,16 @@ class BookingPaymentView(LoginRequiredMixin, MembershipRequiredMixin, FormView):
|
||||||
for key in self.booking_needed_fields}
|
for key in self.booking_needed_fields}
|
||||||
user = self.request.user
|
user = self.request.user
|
||||||
last_booking_order = BookingOrder.objects.filter(customer__user=user).last()
|
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
|
# booking_price_per_day = BookingPrice.objects.get().price_per_day
|
||||||
# total_discount = booking_price_per_day * booking_data.get('free_days')
|
# total_discount = booking_price_per_day * booking_data.get('free_days')
|
||||||
booking_data.update({
|
booking_data.update({
|
||||||
# 'booking_price_per_day': booking_price_per_day,
|
# 'booking_price_per_day': booking_price_per_day,
|
||||||
# 'current_billing_address': self.request.user.billing_addresses.first().to_dict(),
|
# '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
|
'credit_card_data': credit_card_data if credit_card_data else None,
|
||||||
else None,
|
|
||||||
'stripe_key': settings.STRIPE_API_PUBLIC_KEY
|
'stripe_key': settings.STRIPE_API_PUBLIC_KEY
|
||||||
})
|
})
|
||||||
context.update(booking_data)
|
context.update(booking_data)
|
||||||
|
@ -559,6 +562,7 @@ class OrdersBookingDetailView(LoginRequiredMixin, DetailView):
|
||||||
|
|
||||||
membership_required_months = bookig_order.membership_required_months
|
membership_required_months = bookig_order.membership_required_months
|
||||||
membership_required_months_price = bookig_order.membership_required_months_price
|
membership_required_months_price = bookig_order.membership_required_months_price
|
||||||
|
original_price += membership_required_months_price
|
||||||
|
|
||||||
context.update({
|
context.update({
|
||||||
'original_price': original_price,
|
'original_price': original_price,
|
||||||
|
|
Loading…
Reference in a new issue