def buy_gift_view(request, offer, cart_item): context = RequestContext(request) context['offer'] = offer cart = ShoppingCart(request) if request.method == 'POST': additional_info_form = GiftOfferForm(request.POST) buy_form = BuyOfferForm(request.POST, user=request.user, offer=offer, initial={'quantity':1}, is_gift=True) if additional_info_form.is_valid() and buy_form.is_valid(): #TODO: Сделать здесь отдельный вид заказов GiftOrder. После использования кода GiftOrder и создается заказ соответствующий акции. Либо придумать что-то получше. gift_order = GiftOrder(offer=offer, user=request.user, visitor_info=request.session['visitor_info']) gift_order.gift_from_name = additional_info_form.cleaned_data['from_who'] gift_order.gift_to_name = additional_info_form.cleaned_data['to_who'] gift_order.gift_delivery_type = additional_info_form.cleaned_data['delivery_type'] if gift_order.gift_delivery_type == 'email': gift_order.gift_delivery_email = additional_info_form.cleaned_data['delivery_email'] gift_order.gift_message = additional_info_form.cleaned_data['message'] if buy_form.cleaned_data['payment_type'] == 1: #Если выбрали оплату с депозита, сразу списываем деньги и завершаем заказ gift_order.price = offer.coupon_price_money gift_order.price_type = 1 gift_order.save() profile = request.user.get_profile() transaction = profile.withdraw_money_deposit(gift_order.price, u"Оплата заказа подарка %s (%s)" % (gift_order.pk, offer.title,)) gift_order.complete(transaction) if cart_item is not None: cart.remove_item(cart_item['id']) return redirect('cabinet_gifts') elif buy_form.cleaned_data['payment_type'] == 2: #Аналогично с оплатой бонусами gift_order.price = offer.coupon_price_bonuses gift_order.price_type = 2 gift_order.save() profile = request.user.get_profile() transaction = profile.withdraw_bonuses(gift_order.price, u"Оплата заказа подарка %s (%s)" % (gift_order.pk, offer.title,)) gift_order.complete(transaction) if cart_item is not None: cart.remove_item(cart_item['id']) return redirect('cabinet_gifts') elif buy_form.cleaned_data['payment_type'] == 3: gift_order.price = offer.coupon_price_money gift_order.price_type = 1 gift_order.save() payment_request = PaymentRequest(inner_transaction=gift_order, amount=gift_order.price, comment=u"Bonus-House.ru. Оплата заказа #%s. Пользователь #%s" % (gift_order.pk, gift_order.user.pk,)) payment_request.save() context = RequestContext(request) context['amount'] = gift_order.price context['nickname'] = request.user.email context['order_id'] = payment_request.pk if cart_item is not None: cart.remove_item(cart_item['id']) return render_to_response('payments/dol/redirect_form.html', context) else: initial = None additional_info_form = GiftOfferForm(initial=initial) buy_form = BuyOfferForm(user=request.user, offer=offer, is_gift=True) if offer.is_additional_service(): messages.warning(request, '<span class="color_red">Внимание! Вы не можете подарить данную услугу тому, кто НЕ является клиентом сети Fitness House!</span> Для активации подарочного купона потребуется действующий номер карты (или договора)') context['additional_info_form'] = additional_info_form context['buy_form'] = buy_form return render_to_response('offers/buy_gift_additional_info.html', context)
def cart_remove(request, item_id): cart = ShoppingCart(request) cart.remove_item(int(item_id)) messages.info(request, 'Товар удален из корзины') return redirect('cart')
def buy_view(request, offer, cart_item=None): context = RequestContext(request) if not offer.can_buy(): messages.info(request, 'К сожалению, купоны по данной акции закончились') return redirect(offer.get_url()) context['offer'] = offer if cart_item is not None: cart = ShoppingCart(request) context['is_cart_item'] = True if cart_item is not None and cart_item['is_gift']: #Мы имеем дело с подарком return buy_gift_view(request, offer, cart_item) elif offer.is_abonement(): #Мы имеем дело с абонементом if request.method == 'POST': additional_info_form = AbonementsAdditionalInfoForm(request.POST, offer=offer) buy_form = BuyOfferForm(request.POST, user=request.user, offer=offer, initial={'quantity':1}) if additional_info_form.is_valid() and buy_form.is_valid(): additional_info_form.save() abonement_order = AbonementOrder(offer=offer, user=request.user, visitor_info=request.session['visitor_info'], additional_info=additional_info_form.instance) if buy_form.cleaned_data['payment_type'] == 1: #Если выбрали оплату с депозита, сразу списываем деньги и завершаем заказ abonement_order.price = offer.coupon_price_money abonement_order.price_type = 1 abonement_order.save() profile = request.user.get_profile() transaction = profile.withdraw_money_deposit(abonement_order.price, u"Оплата заказа %s (%s)" % (abonement_order.pk, offer.title,)) abonement_order.complete(transaction) if cart_item is not None: cart.remove_item(cart_item['id']) return redirect('cabinet_abonements') elif buy_form.cleaned_data['payment_type'] == 2: #Аналогично с оплатой бонусами abonement_order.price = offer.coupon_price_bonuses abonement_order.price_type = 2 abonement_order.save() profile = request.user.get_profile() transaction = profile.withdraw_bonuses(abonement_order.price, u"Оплата заказа %s (%s)" % (abonement_order.pk, offer.title,)) abonement_order.complete(transaction) if cart_item is not None: cart.remove_item(cart_item['id']) return redirect('cabinet_abonements') elif buy_form.cleaned_data['payment_type'] == 3: abonement_order.price = offer.coupon_price_money abonement_order.price_type = 1 abonement_order.save() payment_request = PaymentRequest(inner_transaction=abonement_order, amount=abonement_order.price, comment=u"Bonus-House.ru. Оплата заказа #%s. Пользователь #%s" % (abonement_order.pk, abonement_order.user.pk,)) payment_request.save() context = RequestContext(request) context['amount'] = abonement_order.price context['nickname'] = request.user.email context['order_id'] = payment_request.pk if cart_item is not None: cart.remove_item(cart_item['id']) return render_to_response('payments/dol/redirect_form.html', context) else: if cart_item is not None: initial = cart_item['additional_info'] else: initial = None initial = {'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email, 'birth_date': request.user.get_profile().birth_date, 'phone': request.user.get_profile().phone, 'gender': request.user.get_profile().gender} additional_info_form = AbonementsAdditionalInfoForm(offer=offer, initial=initial) buy_form = BuyOfferForm(user=request.user, offer=offer) context['additional_info_form'] = additional_info_form context['buy_form'] = buy_form return render_to_response('offers/buy_abonements_additional_info.html', context) elif offer.is_additional_service(): #Мы имеем дело с дополнительной услугой к договору if request.method == 'POST': additional_info_form = AbonementsClubCardForm(request.POST, offer=offer) buy_form = BuyOfferForm(request.POST, user=request.user, offer=offer, initial={'quantity':1}) if additional_info_form.is_valid() and buy_form.is_valid(): additional_info_form.save() order = AdditionalServicesOrder(offer=offer, user=request.user, visitor_info=request.session['visitor_info'], additional_info=additional_info_form.instance) if buy_form.cleaned_data['payment_type'] == 1: #Если выбрали оплату с депозита, сразу списываем деньги и завершаем заказ order.price = offer.coupon_price_money order.price_type = 1 order.save() profile = request.user.get_profile() transaction = profile.withdraw_money_deposit(order.price, u"Оплата заказа %s (%s)" % (order.pk, offer.title,)) order.complete(transaction) if cart_item is not None: cart.remove_item(cart_item['id']) return redirect('cabinet_abonements') elif buy_form.cleaned_data['payment_type'] == 2: #Аналогично с оплатой бонусами order.price = offer.coupon_price_bonuses order.price_type = 2 order.save() profile = request.user.get_profile() transaction = profile.withdraw_bonuses(order.price, u"Оплата заказа %s (%s)" % (order.pk, offer.title,)) order.complete(transaction) if cart_item is not None: cart.remove_item(cart_item['id']) return redirect('cabinet_additional_services') elif buy_form.cleaned_data['payment_type'] == 3: order.price = offer.coupon_price_money order.price_type = 1 order.save() payment_request = PaymentRequest(inner_transaction=order, amount=order.price, comment=u"Bonus-House.ru. Оплата заказа #%s. Пользователь #%s" % (order.pk, order.user.pk,)) payment_request.save() context = RequestContext(request) context['amount'] = order.price context['nickname'] = request.user.email context['order_id'] = payment_request.pk if cart_item is not None: cart.remove_item(cart_item['id']) return render_to_response('payments/dol/redirect_form.html', context) else: additional_info_form = AbonementsClubCardForm(offer=offer) buy_form = BuyOfferForm(user=request.user, offer=offer) context['additional_info_form'] = additional_info_form context['buy_form'] = buy_form return render_to_response('offers/buy_abonements_club_card.html', context) else: #Это не абонемент и не доп. услуги, а обычная акция if request.method == 'POST': buy_form = BuyOfferForm(request.POST, user=request.user, offer=offer) if buy_form.is_valid(): #@TODO: Протестировать определение реферера и узнать время, на которое его надо запоминать в сессии. order = Order(offer=offer, quantity=buy_form.cleaned_data['quantity'], user=request.user, visitor_info=request.session['visitor_info']) if buy_form.cleaned_data['payment_type'] == 1: #Если выбрали оплату с депозита, сразу списываем деньги и завершаем заказ order.price = offer.coupon_price_money * buy_form.cleaned_data['quantity'] order.price_type = 1 order.save() profile = request.user.get_profile() transaction = profile.withdraw_money_deposit(order.price, u"Оплата заказа %s (%s)" % (order.pk, offer.title,)) order.complete(transaction) if cart_item is not None: cart.remove_item(cart_item['id']) return redirect('cabinet') elif buy_form.cleaned_data['payment_type'] == 2: #Аналогично с оплатой бонусами order.price = offer.coupon_price_bonuses * buy_form.cleaned_data['quantity'] order.price_type = 2 order.save() profile = request.user.get_profile() transaction = profile.withdraw_bonuses(order.price, u"Оплата заказа %s (%s)" % (order.pk, offer.title,)) order.complete(transaction) if cart_item is not None: cart.remove_item(cart_item['id']) return redirect('cabinet') elif buy_form.cleaned_data['payment_type'] == 3: order.price = offer.coupon_price_money * buy_form.cleaned_data['quantity'] order.price_type = 1 order.save() payment_request = PaymentRequest(inner_transaction=order, amount=order.price, comment=u"Bonus-House.ru. Оплата заказа #%s. Пользователь #%s" % (order.pk, order.user.pk,)) payment_request.save() context = RequestContext(request) context['amount'] = order.price context['nickname'] = request.user.email context['order_id'] = payment_request.pk if cart_item is not None: cart.remove_item(cart_item['id']) return render_to_response('payments/dol/redirect_form.html', context) else: if cart_item is not None: initial = {'quantity':cart_item['quantity']} else: initial = None initial = {'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email, 'birth_date': request.user.get_profile().birth_date, 'phone': request.user.get_profile().phone, 'gender': request.user.get_profile().gender} additional_info_form = SimpleActionAdditionalInfoForm(offer=offer, initial=initial) context['additional_info_form'] = additional_info_form buy_form = BuyOfferForm(user=request.user, offer=offer, initial=initial) context['buy_form'] = buy_form return render_to_response('offers/buy_simple_actions_additional_info.html', context)