finishing digitalglarus booking

This commit is contained in:
Levi 2016-09-29 00:04:40 -05:00
parent 80095ed1aa
commit 447ddc7c2e
7 changed files with 137 additions and 24 deletions

View file

@ -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,
),
]

View file

@ -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),
),
]

View file

@ -9,7 +9,7 @@ class MembershipRequiredMixin(object):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
from .models import Membership 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 HttpResponseRedirect(self.membership_redirect_url)
return super(MembershipRequiredMixin, self).dispatch(request, *args, **kwargs) return super(MembershipRequiredMixin, self).dispatch(request, *args, **kwargs)
@ -20,7 +20,7 @@ class IsNotMemberMixin(object):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
from .models import Membership 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 HttpResponseRedirect(self.already_member_redirect_url)
return super(IsNotMemberMixin, self).dispatch(request, *args, **kwargs) return super(IsNotMemberMixin, self).dispatch(request, *args, **kwargs)

View file

@ -16,8 +16,9 @@ from .mixins import Ordereable
class MembershipType(models.Model): class MembershipType(models.Model):
STANDARD = 'standard'
MEMBERSHIP_TYPES = ( MEMBERSHIP_TYPES = (
('standard', 'Standard'), (STANDARD, 'Standard'),
) )
name = models.CharField(choices=MEMBERSHIP_TYPES, max_length=20) name = models.CharField(choices=MEMBERSHIP_TYPES, max_length=20)
@ -63,7 +64,7 @@ class Membership(models.Model):
active = models.BooleanField(default=True) active = models.BooleanField(default=True)
@classmethod @classmethod
def is_digitalglarus_member(cls, user): def is_digitalglarus_active_member(cls, user):
past_month = (datetime.today() - relativedelta(months=1)).month past_month = (datetime.today() - relativedelta(months=1)).month
has_booking_current_month = Q(membershiporder__customer__user=user, has_booking_current_month = Q(membershiporder__customer__user=user,
membershiporder__created_at__month=datetime.today().month) 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 total_free_days = months * TWO_DAYS + free_days_this_month
return total_free_days 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 @classmethod
def booking_price(cls, user, start_date, end_date): 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) free_days = cls.get_ramaining_free_days(user, start_date, end_date)
final_booking_price = normal_price - (free_days * price_per_day) 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): class BookingOrder(Ordereable, models.Model):
booking = models.OneToOneField(Booking) booking = models.OneToOneField(Booking)
original_price = models.FloatField() original_price = models.FloatField()
special_month_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): 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

View file

@ -44,6 +44,10 @@
</h2> </h2>
<h2 class="col-xs-6 payment-total text-left">Total days {{booking_days}}</h2> <h2 class="col-xs-6 payment-total text-left">Total days {{booking_days}}</h2>
<h2 class="order-sum">{{original_price|floatformat}}CHF</h2> <h2 class="order-sum">{{original_price|floatformat}}CHF</h2>
{% if membership_required_months and membership_required_months_price %}
<h2 class="col-xs-6 payment-total text-left">Required Membership months: {{membership_required_months}}</h2>
<h2 class="order-sum"><span>{{membership_required_months_price|floatformat}}CHF</span></h2>
{% endif %}
{% if free_days %} {% if free_days %}
<h2 class="col-xs-6 payment-total text-left">Free days {{free_days}} </h2> <h2 class="col-xs-6 payment-total text-left">Free days {{free_days}} </h2>
<h2 class="order-sum">-{{total_discount|floatformat}}CHF</h2> <h2 class="order-sum">-{{total_discount|floatformat}}CHF</h2>
@ -55,7 +59,7 @@
</div> </div>
</div> </div>
<div class="col-xs-12 col-sm-4 col-lg-4 wow fadeInDown"> <div class="col-xs-12 col-sm-4 col-lg-4 wow fadeInDown hidden-print">
<div class="order-summary"> <div class="order-summary">
<div class="header text-center"> <div class="header text-center">
<h2 class="order-name">Order Summary</h2> <h2 class="order-name">Order Summary</h2>
@ -65,6 +69,10 @@
<h2 class="col-xs-6 payment-total">Total days {{booking_days}}</h2> <h2 class="col-xs-6 payment-total">Total days {{booking_days}}</h2>
<h2 class="order-sum">{{original_price|floatformat}}CHF</h2> <h2 class="order-sum">{{original_price|floatformat}}CHF</h2>
{% if membership_required_months and membership_required_months_price %}
<h2 class="col-xs-6 payment-total">Required Membership months: {{membership_required_months}}</h2>
<h2 class="order-sum"><span>{{membership_required_months_price|floatformat}}CHF</span></h2>
{% endif %}
{% if free_days %} {% if free_days %}
<h2 class="col-xs-6 payment-total">Free days {{free_days}}</h2> <h2 class="col-xs-6 payment-total">Free days {{free_days}}</h2>
<h2 class="order-sum">-{{total_discount|floatformat}}CHF</h2> <h2 class="order-sum">-{{total_discount|floatformat}}CHF</h2>

View file

@ -121,15 +121,20 @@
<br/> <br/>
<hr class="greyline"> <hr class="greyline">
<h2 class="col-xs-6 payment-total">Total days {{booking_days}} </h2> <h2 class="col-xs-6 payment-total">Total days: {{booking_days}} </h2>
<h2 class="order-sum">{{original_price|floatformat}}CHF</h2> <h2 class="order-sum">{{original_price|floatformat}}CHF</h2>
{% if membership_required_months and membership_required_months_price %}
<h2 class="col-xs-6 payment-total">Required Membership months: {{membership_required_months}}</h2>
<h2 class="order-sum"><span>{{membership_required_months_price|floatformat}}CHF</span></h2>
{% endif %}
<br/>
{% if free_days %} {% if free_days %}
<h2 class="col-xs-6 payment-total">Free days {{free_days}}</h2> <h2 class="col-xs-6 payment-total">Free days: {{free_days}}</h2>
<h2 class="order-sum"><span class="text-danger">-{{total_discount|floatformat}}CHF</span></h2> <h2 class="order-sum"><span class="text-danger">-{{total_discount|floatformat}}CHF</span></h2>
{% endif %} {% endif %}
<hr class="greyline"> <hr class="greyline">
<h2 class="col-xs-6 payment-total">Total</h2> <h2 class="col-xs-6 payment-total">Total</h2>
<h2 class="order-result">{{discount_price|floatformat}}CHF</h2> <h2 class="order-result">{{final_price|floatformat}}CHF</h2>
<div class="text-center"> <div class="text-center">
<label class="custom-control custom-checkbox"> <label class="custom-control custom-checkbox">
<br/> <br/>

View file

@ -100,11 +100,22 @@ class BookingSelectDatesView(LoginRequiredMixin, MembershipRequiredMixin, FormVi
start_date = form.cleaned_data.get('start_date') start_date = form.cleaned_data.get('start_date')
end_date = form.cleaned_data.get('end_date') end_date = form.cleaned_data.get('end_date')
booking_days = (end_date - start_date).days + 1 booking_days = (end_date - start_date).days + 1
original_price, discount_price, free_days = Booking.\
price_per_day = BookingPrice.objects.get().price_per_day
original_price, final_price, free_days,\
membership_required_months, membership_required_months_price = Booking.\
booking_price(user, start_date, end_date) booking_price(user, start_date, end_date)
total_discount = price_per_day * free_days
self.request.session.update({ self.request.session.update({
'original_price': original_price, 'original_price': original_price,
'discount_price': discount_price, 'final_price': final_price,
'total_discount': total_discount,
'membership_required_months_price': membership_required_months_price,
'membership_required_months': membership_required_months,
'booking_price_per_day': price_per_day,
'booking_days': booking_days, 'booking_days': booking_days,
'free_days': free_days, 'free_days': free_days,
'start_date': start_date.strftime('%m/%d/%Y'), 'start_date': start_date.strftime('%m/%d/%Y'),
@ -118,8 +129,10 @@ class BookingPaymentView(LoginRequiredMixin, MembershipRequiredMixin, FormView):
form_class = BookingBillingForm form_class = BookingBillingForm
membership_redirect_url = reverse_lazy('digitalglarus:membership_pricing') membership_redirect_url = reverse_lazy('digitalglarus:membership_pricing')
# success_url = reverse_lazy('digitalglarus:booking_payment') # success_url = reverse_lazy('digitalglarus:booking_payment')
booking_needed_fields = ['original_price', 'discount_price', 'booking_days', 'free_days', booking_needed_fields = ['original_price', 'final_price', 'booking_days', 'free_days',
'start_date', 'end_date'] 'start_date', 'end_date', 'membership_required_months_price',
'membership_required_months', 'booking_price_per_day',
'total_discount']
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
from_booking = all(field in request.session.keys() from_booking = all(field in request.session.keys()
@ -138,7 +151,7 @@ class BookingPaymentView(LoginRequiredMixin, MembershipRequiredMixin, FormView):
'initial': { 'initial': {
'start_date': self.request.session.get('start_date'), 'start_date': self.request.session.get('start_date'),
'end_date': self.request.session.get('end_date'), 'end_date': self.request.session.get('end_date'),
'price': self.request.session.get('discount_price'), 'price': self.request.session.get('final_price'),
} }
}) })
return form_kwargs return form_kwargs
@ -148,11 +161,11 @@ class BookingPaymentView(LoginRequiredMixin, MembershipRequiredMixin, FormView):
booking_data = {key: self.request.session.get(key) booking_data = {key: self.request.session.get(key)
for key in self.booking_needed_fields} for key in self.booking_needed_fields}
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,
'total_discount': total_discount, # 'total_discount': total_discount,
'stripe_key': settings.STRIPE_API_PUBLIC_KEY 'stripe_key': settings.STRIPE_API_PUBLIC_KEY
}) })
context.update(booking_data) context.update(booking_data)
@ -165,7 +178,8 @@ class BookingPaymentView(LoginRequiredMixin, MembershipRequiredMixin, FormView):
start_date = data.get('start_date') start_date = data.get('start_date')
end_date = data.get('end_date') end_date = data.get('end_date')
normal_price, final_price, free_days = Booking.\ normal_price, final_price, free_days, membership_required_months,\
membership_required_months_price = Booking.\
booking_price(self.request.user, start_date, end_date) booking_price(self.request.user, start_date, end_date)
# Get or create stripe customer # Get or create stripe customer
@ -213,14 +227,12 @@ class BookingPaymentView(LoginRequiredMixin, MembershipRequiredMixin, FormView):
'stripe_charge': charge, 'stripe_charge': charge,
'amount': final_price, 'amount': final_price,
'original_price': normal_price, 'original_price': normal_price,
'special_month_price': BookingPrice.objects.last().special_month_price 'special_month_price': BookingPrice.objects.last().special_month_price,
'membership_required_months': membership_required_months,
'membership_required_months_price': membership_required_months_price,
} }
order = BookingOrder.create(order_data) order = BookingOrder.create(order_data)
# request.session.update({
# 'membership_price': membership.type.first_month_price,
# 'membership_dates': membership.type.first_month_formated_range
# })
return HttpResponseRedirect(self.get_success_url(order.id)) return HttpResponseRedirect(self.get_success_url(order.id))
@ -439,9 +451,15 @@ class OrdersBookingDetailView(LoginRequiredMixin, DetailView):
booking_days = (end_date - start_date).days + 1 booking_days = (end_date - start_date).days + 1
original_price = booking.price original_price = booking.price
final_price = booking.final_price final_price = booking.final_price
membership_required_months = bookig_order.membership_required_months
membership_required_months_price = bookig_order.membership_required_months_price
context.update({ context.update({
'original_price': original_price, 'original_price': original_price,
'total_discount': original_price - final_price, 'total_discount': original_price - final_price,
'membership_required_months': membership_required_months,
'membership_required_months_price': membership_required_months_price,
'final_price': final_price, 'final_price': final_price,
'booking_days': booking_days, 'booking_days': booking_days,
'free_days': free_days, 'free_days': free_days,