예제 #1
0
def history(request):
    orders = PurchaseOrder.objects.filter(
        user=request.user).order_by('-created_date')
    """
    if not orders:
        return HttpResponseRedirect(reverse('cart:index'))
    """
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    wishlist = wishlists.get_wishlist(request)
    wishlist_object = wishlist['wishlist_object']
    paginator = Paginator(orders, 5)
    page = request.GET.get('page', 1)
    max_page = 4
    min_page = 0
    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        products = paginator.page(1)
    except EmptyPage:
        products = paginator.page(paginator.num_pages)

    max_page = products.number + 4
    min_page = products.number - 4
    token = get_token(request)
    return render(
        request, 'purchase_order/order_history.html', {
            'wishlist': wishlist_object,
            'cart': cart_object,
            'token': token,
            'products': products,
            'max_page': max_page,
            'min_page': min_page
        })
예제 #2
0
def checkout(request):
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    wishlist = wishlists.get_wishlist(request)
    wishlist_object = wishlist['wishlist_object']
    if not cart_object.get_total_items_in_cart():
        return HttpResponseRedirect(reverse('cart:index'))
    products = cart_object.get_items_in_cart()

    discount = 0
    discounted_price = 0

    if request.user.is_authenticated:
        discounted_price = cart_object.get_total_price()
        if not request.user.member.member_type == Member.GUEST and \
            not request.user.member.member_type == Member.NEW_MEMBER:
            benefit = request.user.member.get_level()['BENEFIT']
            discount = cart_object.get_total_price() * discount / 100
            discounted_price = cart_object.get_total_price() * (100 -
                                                                discount) / 100
            if discount <= 0:
                discount = int(discount)
                discounted_price = int(discounted_price)

    order = PurchaseOrder.objects.get_or_create(user=request.user,
                                                is_paid=False,
                                                is_checked_out=False)[0]

    for item in products:
        order_item = PurchaseOrderItem.objects.get_or_create(
            purchase_order=order, product=item.product)[0]
        order_item.quantity = item.quantity
        order_item.product_referal = item.product_referal
        order_item.save()

    order.total_price = discounted_price
    order.discount = discount
    order.shipping_cost = cart_object.shipping_cost
    order.total_payment = discounted_price + cart_object.shipping_cost
    if not cart_object.shipping_address and not cart_object.is_set_as_dropship:
        order.alamat_tujuan = request.user.member.get_home_address()
    else:
        order.is_set_as_dropship = cart_object.is_set_as_dropship
        order.customer = cart_object.customer
        order.alamat_tujuan = cart_object.shipping_address
    order.service = cart_object.shipping_service.upper()
    order.sub_service = cart_object.shipping_sub_service
    order.save()
    order_detail = {
        key: format_order(key, value)
        for (key, value) in model_to_dict(
            order,
            fields=[
                "order_number", "total_price", "total_payment",
                "shipping_cost", "alamat_tujuan", "service", "sub_service",
                "discount"
            ]).items()
    }
    return JsonResponse(order_detail, safe=False)
예제 #3
0
def home(request):
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    products = Product.objects.filter(is_archived=False).order_by('-pk')[:4]
    featured_products = Product.objects.filter(is_featured=True).order_by('-pk')[0]
    return render(request, 'keskei/index.html', 
        {'cart':cart_object, 
        'products':products,
        'featured_products':featured_products})
예제 #4
0
def pay(request):
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    if not cart_object.get_total_items_in_cart():
        return HttpResponseRedirect(reverse('cart:index'))
    cart_object.delete()
    order = PurchaseOrder.objects.filter(user=request.user,
                                         is_paid=False,
                                         is_checked_out=False)[0]
    order.is_checked_out = True
    order.is_paid = False
    order.is_verified = False
    order.payment_date = datetime.datetime.now()
    order.save()
    #if request.user.is_authenticated:
    #    if not request.user.member.member_type == Member.GUEST and \
    #        not request.user.member.member_type == Member.NEW_MEMBER:
    #            count_point(request,order)
    #            check_purchasing_bonus(request)
    del request.session['shopping_cart']
    cart = carts.get_cart(request)
    return HttpResponseRedirect(reverse('order:history'))
예제 #5
0
def detail(request, order_number):
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    wishlist = wishlists.get_wishlist(request)
    wishlist_object = wishlist['wishlist_object']
    order = PurchaseOrder.objects.get(order_number=order_number.upper())
    products = order.item_in_order.all()

    token = get_token(request)
    return render(
        request, 'purchase_order/order_detail.html', {
            'wishlist': wishlist_object,
            'products': products,
            'cart': cart_object,
            'token': token,
            'order': order
        })
예제 #6
0
def paginate_results(request, product_list,product_title):
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    wishlist = wishlists.get_wishlist(request)
    wishlist_object = wishlist['wishlist_object']
    max_page = 4
    min_page = 0
    products = ''
    if product_list:
        try:
            paginator = Paginator(product_list,12)
            page = request.GET.get('page', 1)
            try:
                products = paginator.page(page)
            except PageNotAnInteger:
                products = paginator.page(1)
            except EmptyPage:
                products = paginator.page(paginator.num_pages)

            max_page = products.number + 4
            min_page = products.number - 4
        except:
            pass

    try:
        categories = Category.objects.filter(is_archived=False)
    except:
        categories = ''

    response = render(request, 'st_storefront/product_all.html', 
        {'cart':cart_object,
         'wishlist':wishlist_object, 
         'products':products,
         'categories':categories,
         'product_title':product_title,
         'max_page':max_page,
         'min_page':min_page})
    return response
예제 #7
0
def wishlist_index(request):
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    wishlist = wishlists.get_wishlist(request)
    wishlist_object = wishlist['wishlist_object']
    item_list = wishlist_object.get_items_in_wishlist()
    paginator = Paginator(item_list, 6)
    page = request.GET.get('page', 1)
    max_page = 4
    min_page = 0
    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        products = paginator.page(1)
    except EmptyPage:
        products = paginator.page(paginator.num_pages)

    max_page = products.number + 4
    min_page = products.number - 4

    if request.method == 'POST':
        item = ""
        try:
            if request.POST['item']:
                item_pk = request.POST['item'].split('_')[1]
                item = WishListItem.objects.get(pk=item_pk)
                item.delete()
        except:
            pass

    return render(
        request, 'shopping_cart/wishlist_show_all.html', {
            'wishlist': wishlist_object,
            'cart': cart_object,
            'products': products,
            'max_page': max_page,
            'min_page': min_page
        })
예제 #8
0
def product_detail(request, product_pk, **kwargs):
    """
    referal_code = redirect_referal_code(request, kwargs=kwargs)
    if referal_code['code']:
        referer = Member.objects.get(referal_code = referal_code['code'])
    else:
        referer = ''

    if referal_code['message'] == "redirect_to_default":
        return  HttpResponseRedirect(request.scheme+"://"+settings.DEFAULT_HOST + 
                reverse('storefront:product_detail', kwargs={'product_pk':product_pk},
                    current_app=request.resolver_match.namespace))
    elif referal_code['message'] == 'redirect_to_referal':
        return HttpResponseRedirect(request.scheme+"://"+referer.user.username+'.'+settings.DEFAULT_HOST + 
                reverse('storefront:product_detail', kwargs={'product_pk':product_pk},
                    current_app=request.resolver_match.namespace))
    """
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    wishlist = wishlists.get_wishlist(request)
    wishlist_object = wishlist['wishlist_object']
    all_product = Product.objects.filter(is_archived=False).exclude(pk=product_pk)
    is_wishlist = False
    if len(all_product) >= 5:
        other_product = random.sample(list(all_product), 4)
    else:
        other_product = random.sample(list(all_product), len(all_product))

    product = Product.objects.get(pk=product_pk)
    is_in_wishlist = False

    if request.method == 'POST':
        method = ''
        try:
            method = request.POST['method']
        except:
            pass
        
        if method == 'wishlist':  
            is_wishlist = True 
            wishlist_item = WishListItem.objects.get_or_create(wishlist=wishlist_object, product=product)[0]
       
        elif method == 'cart':
            form = ProductCartForm(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                quantity = data.get('quantity')
                cart_item = False
                try :
                    cart_item = CartItem.objects.get_or_create(cart=cart_object, product=product)[0]
                except:
                    pass

                if not cart_item.quantity :
                    cart_item.quantity = quantity
                else :
                    cart_item.quantity += int(quantity)

                if request.user.is_authenticated:
                    if referer and not referer == request.user.member:
                        cart_item.product_referal = referer
                else: 
                    if referer:
                        cart_item.product_referal = referer
                    
                cart_item.save()
            
            return HttpResponseRedirect(reverse('cart:index', 
                current_app=request.resolver_match.namespace))

    form = ProductCartForm()

    product = Product.objects.get(pk=product_pk)

    discount = 0
    discounted_price = product.price
    
    if request.user.is_authenticated:
        if not request.user.member.member_type == Member.GUEST and \
            not request.user.member.member_type == Member.NEW_MEMBER:
            discount = request.user.member.get_level()['BENEFIT']
            discounted_price = product.price * (100 - discount) / 100

    products_in_wishlist = [x.product.id for x in wishlist_object.item_in_wishlist.all()]
    for pk in produreferal_cocts_in_wishlist:
        if product_pk == pk:
            is_in_wishlist = True

    if is_wishlist:
        response = HttpResponseRedirect(reverse("storefront:product_detail", kwargs=
            {'product_pk':product_pk})[:-1]+"#formQuantity")
    else:
        response = render(request, 'storefront/product_detail.html', 
            {'product':product, 
            'other_product':other_product, 
            'cart':cart_object, 
            'wishlist':wishlist_object, 
            'form':form, 
            'is_in_wishlist': is_in_wishlist,
            'discount': discount, 
            'discounted_price': int(discounted_price)})

    return response
예제 #9
0
def index(request):
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    services = ''
    selected_service = ''
    wishlist = wishlists.get_wishlist(request)
    wishlist_object = wishlist['wishlist_object']
    set_as_dropship = False
    chosen_customer = ''

    if not cart_object.get_total_items_in_cart():
        return HttpResponseRedirect(reverse('cart:index'))

    products = cart_object.get_items_in_cart()
    discount = 0
    discounted_price = 0
    shipping_origin = ShippingOrigin.objects.filter(is_default=True)[0]
    if 'old_cart_weight' in request.session:
        if request.session['old_cart_weight'] != cart_object.get_total_weight(
        ):
            cart_object.shipping_cost = 0
            cart_object.save()
            request.session['old_cart_weight'] = cart_object.get_total_weight()
    else:
        request.session['old_cart_weight'] = cart_object.get_total_weight()

    if request.method == 'POST':
        method = request.POST.get('method', 'remove')
        item = ""
        if method == 'check_shipping':
            costs_list = ''
            costs_list = get_cost(request.user,
                                  request.POST.get('courier', 'jne').lower())
            if costs_list:
                services = [x for x in costs_list]
                selected_service = request.POST['courier']
                cart_object.shipping_service = selected_service
                cart_object.save()
                request.session['services'] = services

            return JsonResponse(list(services), safe=False)
        elif method == 'set_shipping':
            if 'services' in request.session:
                services = request.session['services']
            user_selected_service = request.POST.get('service')
            for s in services:
                if s['service'] == user_selected_service:
                    cart_object.shipping_sub_service = user_selected_service
                    cart_object.shipping_cost = s['cost'][0]['int_value']
                    cart_object.save()
                    break
        elif method == 'add_dropship_address':
            form = CustomerAddForm(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                try:
                    customer = Customer()
                    customer.name = data.get('name')
                    customer.home_provinsi = data.get('provinsi_home').name
                    customer.kota_provinsi = Kota.objects.get(
                        pk=request.POST.get('kota_home')).name
                    customer.home_kecamatan = Kecamatan.objects.get(
                        pk=request.POST.get('kecamatan_home')).name
                    customer.home_kelurahan = Kelurahan.objects.get(
                        pk=request.POST.get('kelurahan_home')).name
                    customer.home_address = data.get('home_address')
                    customer.seller = request.user.member
                    customer.save()
                    cart_object.is_set_as_dropship = True
                    cart_object.customer = customer
                    cart_object.shipping_address = customer.get_home_address()
                    shipping_cost = cart_object.shipping_cost = 0
                    cart_object.save()
                except:
                    pass

        elif method == 'cancel_dropship':
            cart_object.is_set_as_dropship = False
            cart_object.customer = None
            cart_object.shipping_address = ''
            shipping_cost = cart_object.shipping_cost = 0
            cart_object.save()

        elif method == 'set_dropship_address':
            try:
                customer = Customer.objects.get(
                    id=request.POST.get('customer'))
                if customer:
                    cart_object.is_set_as_dropship = True
                    cart_object.customer = customer
                    cart_object.shipping_address = customer.get_home_address()
                    shipping_cost = cart_object.shipping_cost = 0
                    cart_object.save()
            except:
                pass

        if cart_object.shipping_cost != 0 and cart_object.get_total_items_in_cart(
        ) == 0:
            shipping_cost = 0
        else:
            shipping_cost = cart_object.shipping_cost

    elif request.method == 'GET':
        if cart_object.shipping_cost != 0 and cart_object.get_total_items_in_cart(
        ) == 0:
            cart_object.shipping_cost = 231
            cart_object.save()

        shipping_cost = cart_object.shipping_cost

    if request.user.is_authenticated:
        discounted_price = cart_object.get_total_price()
        if not request.user.member.member_type == Member.GUEST and \
            not request.user.member.member_type == Member.NEW_MEMBER:
            benefit = request.user.member.get_level()['BENEFIT']
            discount = cart_object.get_total_price() * discount / 100
            discounted_price = cart_object.get_total_price() * (100 -
                                                                discount) / 100
            if discount <= 0:
                discount = int(discount)
                discounted_price = int(discounted_price)

    if shipping_cost:
        discounted_price += shipping_cost

    couriers = get_courier()
    token = get_token(request)

    customers = Customer.objects.filter(seller=request.user.member)
    form = CustomerAddForm()
    return render(
        request, 'purchase_order/select_shipping.html', {
            'wishlist': wishlist_object,
            'customers': customers,
            'cart': cart_object,
            'products': products,
            'discount': discount,
            'discounted_price': discounted_price,
            'couriers': couriers,
            'shipping_cost': shipping_cost,
            'services': services,
            'shipping_origin': shipping_origin,
            'selected_service': selected_service,
            'token': token,
            'form': form
        })
예제 #10
0
def index(request):
    cart = carts.get_cart(request)
    cart_object = cart['cart_object']
    wishlist = wishlists.get_wishlist(request)
    wishlist_object = wishlist['wishlist_object']
    item_list = cart_object.get_items_in_cart()
    paginator = Paginator(item_list, 5)
    page = request.GET.get('page', 1)
    max_page = 4
    min_page = 0
    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        products = paginator.page(1)
    except EmptyPage:
        products = paginator.page(paginator.num_pages)

    max_page = products.number + 4
    min_page = products.number - 4
    discount = 0
    discounted_price = 0

    if request.method == 'POST':
        method = request.POST.get('method', 'remove')
        item = ""
        if method == 'remove':
            try:
                if request.POST['item']:
                    item_pk = request.POST['item'].split('_')[1]
                    item = CartItem.objects.get(pk=item_pk)
                    item.delete()
            except:
                pass
        elif method == 'update':
            try:
                if request.POST['item']:
                    item_pk = request.POST['item'].split('_')[1]
                    item = CartItem.objects.get(pk=item_pk)
                    quantity = request.POST.get('quantity', 1)
                    if int(quantity) > 0:
                        item.quantity = quantity
                        item.save()
            except:
                pass

    discounted_price = cart_object.get_total_price()

    if request.user.is_authenticated:
        if not request.user.member.member_type == Member.GUEST and \
            not request.user.member.member_type == Member.NEW_MEMBER:
            benefit = request.user.member.get_level()['BENEFIT']
            discount = cart_object.get_total_price() * discount / 100
            discounted_price = cart_object.get_total_price() * (100 -
                                                                discount) / 100
            if discount <= 0:
                discount = int(discount)
                discounted_price = int(discounted_price)

    return render(
        request, 'shopping_cart/cart_show_all.html', {
            'wishlist': wishlist_object,
            'cart': cart_object,
            'products': products,
            'discount': discount,
            'discounted_price': discounted_price,
            'max_page': max_page,
            'min_page': min_page
        })