Merge pull request #130 from levivm/feature/new_digitalglarus

Created current user billing address model in membership payment
This commit is contained in:
Levi Velázquez 2016-10-14 00:53:06 -05:00 committed by GitHub
commit fdd57de8a5
5 changed files with 51 additions and 19 deletions

View file

@ -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

View file

@ -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>

View file

@ -49,7 +49,7 @@
<br> <br>
<div class="notice-box"> <div class="notice-box">
<p class="signup-text">Forgot password?<a href="{% url 'digitalglarus:reset_password' %}">Find ID/Password</a></p> <p class="signup-text">Forgot password?<a href="{% url 'digitalglarus:reset_password' %}">Find ID/Password</a></p>
<p class="signup-text">Not a member yet?<a href="{% url 'digitalglarus:signup' %}">Sign up </a>now.</p> <p class="signup-text">Not a member yet?<a href="{% url 'digitalglarus:signup' %}?{{request.GET.next}}">Sign up </a>now.</p>
</div> </div>
</div> </div>
</div> </div>

View file

@ -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)
@ -366,6 +369,15 @@ class MembershipPaymentView(LoginRequiredMixin, IsNotMemberMixin, FormView):
# Create Billing Address # Create Billing Address
billing_address = form.save() 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 # Create membership plan
membership_data = {'type': membership_type} membership_data = {'type': membership_type}
membership = Membership.create(membership_data) membership = Membership.create(membership_data)
@ -460,6 +472,10 @@ class UserBillingAddressView(LoginRequiredMixin, UpdateView):
def get_form_kwargs(self): def get_form_kwargs(self):
current_billing_address = self.request.user.billing_addresses.first() current_billing_address = self.request.user.billing_addresses.first()
form_kwargs = super(UserBillingAddressView, self).get_form_kwargs() form_kwargs = super(UserBillingAddressView, self).get_form_kwargs()
if not current_billing_address:
return form_kwargs
form_kwargs.update({ form_kwargs.update({
'initial': { 'initial': {
'street_address': current_billing_address.street_address, '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 = 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,

View file

@ -17,7 +17,10 @@ class SignupViewMixin(CreateView):
success_url = None success_url = None
def get_success_url(self): 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 return next_url
def form_valid(self, form): def form_valid(self, form):