Esempio n. 1
0
def checkOutView(request, coupon_id):
    total = 0
    counter = 0
    cart_items = None
    discount_price = None
    new_total = None
    add_coupon = False
    coupon = None

    try:
        cart = Cart.objects.get(cart_id=_cart_id(request))  #ดึงตะกร้า
        cart_items = CartItem.objects.filter(
            cart=cart, active=True)  #ดึงข้อมูลสินค้าในตะกร้า
        for item in cart_items:
            total += (item.product.price * item.quantity)
            counter += (item.quantity)
    except Exception as e:
        pass

    form = CheckOutForm(request.POST)

    if request.method == 'POST':

        if 'confirm' in request.POST:
            print('Uppppppppppppppppppppppppppppppppp')
            if coupon_id == 0:
                new_total = total

            else:
                coupon = Coupon.objects.get(id=coupon_id)
                new_total = total - coupon.discount
                add_coupon = True

        else:
            print('Downnnnnnnnnnnnnnnnnnnnnnnnnnnnnn')
            if coupon_id == 0:
                new_total = total

            else:
                coupon = Coupon.objects.get(id=coupon_id)
                new_total = total - coupon.discount
                add_coupon = True
            form = CheckOutForm(request.POST)
            if form.is_valid():
                now = timezone.now()
                data = Order()

                data.first_name = form.cleaned_data['first_name']
                data.last_name = form.cleaned_data['last_name']
                data.phone = form.cleaned_data['phone']
                data.user_id = request.user.username
                data.address = form.cleaned_data.get('address')
                data.city = form.cleaned_data.get('city')
                data.district = form.cleaned_data.get('district')
                data.subdistrict = form.cleaned_data.get('subdistrict')
                data.postcode = form.cleaned_data.get('postcode')
                data.total = new_total
                data.status = 'รอชำระเงิน'
                if coupon_id != 0:
                    data.code = coupon.code
                data.save()

                for item in cart_items:
                    order_item = OrderItem.objects.create(
                        product=item.product.name,
                        quantity=item.quantity,
                        price=item.product.price,
                        order=data)
                    order_item.save()
                    #ลดจำนวนstock
                    product = Product.objects.get(id=item.product.id)
                    product.stock = int(item.product.stock -
                                        order_item.quantity)
                    product.save()
                    item.delete()
                order = Order.objects.get(id=data.id)
                return redirect(order.get_url())

    else:
        form = CheckOutForm()
        new_total = total

    return render(
        request, "checkout.html",
        dict(cart_items=cart_items,
             total=total,
             counter=counter,
             form=form,
             new_total=new_total,
             coupon=coupon,
             add_coupon=add_coupon,
             coupon_id=coupon_id))