def ordercreate(request):
    totail = 0  # 商品总价
    totail_num = 0
    for i in request.session['order_add']:
        totail += i['num'] * i['price']
        totail_num += i['num']

    data = request.POST
    print(data)
    if data.get('consignee') and data.get('address') and data.get(
            'phone') and data.get('code'):
        try:
            # 1. 添加订单数据信息
            oi = Order()  # 添加订单数据
            oi.total_price = totail
            oi.total_number = totail_num
            oi.addcode = data.get('code')  # 收获邮箱
            oi.addname = data.get('consignee')  # 收获人
            oi.addphone = data.get('consignee')  # 收获手机号
            oi.address = data.get('address')  # 收获地址
            oi.uid = Users.objects.get(
                id=request.session['login_users']['user_id'])  # 订单用户
            oi.status = 1  # 订单状态
            oi.save()  # 保存数据

            # 2, 添加订单详情
            # print(request.session['order_add'])
            for i in request.session['order_add']:
                order_info_add = OrderInfo()
                order_info_add.num = i.get('num')  # 商品数量
                order_info_add.price = i.get('price')  # 商品单价
                order_info_add.orderid = oi
                order_info_add.gid = Goods.objects.get(id=i.get('id'))
                order_info_add.save()
                del request.session['cart'][str(i.get('id'))]
            if request.session.get('order_add'):
                del request.session['order_add']

            return HttpResponse(
                '<script>alert("下单成功, 请支付");location.href="/buy/%s"</script>' %
                oi.id)
        except:
            return HttpResponse(
                '<script>alert("订单信息错误, 请重新添加");location.href="/orderadd/"</script>'
            )

    return HttpResponse(
        '<script>alert("没有填写收获信息,请重新填写");location.href="/orderadd/"</script>')
Exemple #2
0
def new_order(request, customer_id):
    c_id = int(customer_id)
    customer_obj = Customer.objects.filter(id=c_id)
    cats = Category.objects.filter(is_active=True).values_list('name',
                                                               flat=True)
    if len(customer_obj) == 1:
        customer_obj = customer_obj[0]

        if request.method == 'POST':
            kg = float(request.POST.get('kg'))
            price_json = OrderedDict()
            all_price = Price.objects.all().order_by('kg')
            for each_price in all_price:
                price_json[str(each_price.kg)] = each_price.cost
            price_json = OrderedDict(sorted(price_json.items()))
            if kg < 0:
                return render(
                    request, 'new_order.html', {
                        'customer_obj': customer_obj,
                        'price_json': json.dumps(price_json),
                        'message_title': 'Error',
                        'message_type': 'error',
                        'message': 'Kg should be greater than 0',
                        'category':
                        [cats[i:i + 2] for i in range(0, len(cats), 2)]
                    })
            # current_price = Price.objects.order_by('-kg').filter(kg__lte=kg)[:1][0]
            current_price = float(request.POST.get('price'))
            print(current_price)
            order_obj = Order(customer=customer_obj,
                              kg=kg,
                              received_date=datetime.datetime.now(),
                              price=current_price,
                              status=1)

            order_obj.save()

            if request.POST.get('selected') == 'ind_kg':
                for each in request.POST:
                    if each.__contains__('cloth_'):
                        each_id = each.split('cloth_')[1]
                        cat_obj = Category.objects.get_or_create(
                            name=request.POST.get(each), is_active=True)[0]
                        order_details = OrderDetail(
                            order=order_obj,
                            category=cat_obj,
                            count=int(request.POST.get('qty_' + each_id)))
                        order_details.save()

                order_obj.status = 2
                order_obj.save()
            return render(
                request, 'new_order.html', {
                    'customer_obj': customer_obj,
                    'price_json': json.dumps(price_json),
                    'message_type': 'success',
                    'message_title': 'Success',
                    'swal_redirect': '/',
                    'message':
                    'Order registered for customer ' + customer_obj.name,
                    'category':
                    [cats[i:i + 2] for i in range(0, len(cats), 2)]
                })
        else:
            all_price = Price.objects.all().order_by('kg')
            price_json = {}
            for each_price in all_price:
                price_json[str(each_price.kg)] = each_price.cost

            return render(
                request, 'new_order.html', {
                    'customer_obj': customer_obj,
                    'price_json': json.dumps(price_json),
                    'category':
                    [cats[i:i + 2] for i in range(0, len(cats), 2)]
                })
    else:
        return HttpResponse('No objects found with that id')