Пример #1
0
def createBill(request):
    data = json.loads(request.body)
    cartID = data['cartID']
    action = data['action']
    cart = Shopping_cart.objects.get(cart_id=cartID)
    customer = Customer.objects.get(customer_id=cart.customer_id_id)
    tradesman = Tradesman.objects.get(employer_id= randrange(6))
    cart_pro = Cart_product.objects.all()
    total = cart.total
    bill = Bill.objects.all()
    day = date.today()
    dt = day.strftime("%Y-%m-%d")
    m=0
    if len(bill) != 0:
        for q in bill:
            if m < q.bill_number:
                m = q.bill_number
        m=m+1
        print('Bill_number',m)
        print('Date',dt)
        print('Customer',customer)
        print('Cart',cart)
        print('Total',total)
        if action == 'create':
            bill1 = Bill(bill_number = m, bill_date = dt, customer_id = customer, cart_id = cart, total = total)
            delivery1 = Delivery(delivery_id = m,bill_number = bill1,customer_id = customer,employer_id = tradesman,delivery_date = dt)
            for cp in cart_pro:
                if cp.cart_id_id == cart.cart_id:
                    bk = Backup(cart_product_id = cp.cart_product_id ,amount = cp.amount , price_total = cp.price_total,cart= cp.cart_id, product=cp.product_id, total = total)
                    bk.save()
                    cp.delete()
            cart.total=0
            cart.save()

        bill1.save()
        delivery1.save()
    else:
        m=1
        if action == 'create':
            bill1 = Bill(bill_number = m, bill_date = dt, customer_id = customer, cart_id = cart, total = total)
            delivery1 = Delivery(delivery_id = m,bill_number = bill1,customer_id = customer,employer_id = tradesman,delivery_date = dt)
            for cp in cart_pro:
                if cp.cart_id_id == cart.cart_id:
                    bk = Backup(cart_product_id = cp.cart_product_id ,amount = cp.amount , price_total = cp.price_total,cart= cp.cart_id, product=cp.product_id, total = total)
                    bk.save()
                    cp.delete()
            cart.total=0
            cart.save()

        bill1.save()
        delivery1.save()

    return JsonResponse('The bill was added', safe= False)
Пример #2
0
def cart_order_complete(request, total_price, username, phonenumber, address,
                        uid, delivery_message):
    delivery_lis = []
    cart_ref = db.collection("users").document(uid).collection('cart')
    cart_alldoc = cart_ref.stream()

    for doc in cart_alldoc:
        cart = Cart.from_dict(doc.to_dict())
        doc_ref = db.collection("delivery")
        doc_ref_random = db.collection("delivery").document()
        doc_ref_random.set(
            Delivery(cart.brand, cart.name, cart.sizedic, total_price,
                     username, phonenumber, address,
                     firestore.SERVER_TIMESTAMP, "배송전", "", "", uid,
                     delivery_message, cart.downloadurl,
                     cart.documentId).to_dict())

        user_doc = db.collection("users").document(uid).collection(
            "delivery").document(doc_ref_random.id)
        user_doc.set(
            Delivery(cart.brand, cart.name, cart.sizedic, total_price,
                     username, phonenumber, address,
                     firestore.SERVER_TIMESTAMP, "배송전", "", "", uid,
                     delivery_message, cart.downloadurl,
                     cart.documentId).to_dict())
        delievery_par = Delivery.from_dict(
            Delivery(cart.brand, cart.name, cart.sizedic, cart.price, username,
                     phonenumber, address, firestore.SERVER_TIMESTAMP, "배송전",
                     "", "", uid, delivery_message, cart.downloadurl,
                     cart.documentId).to_dict())
        # delivery=Delivery.from_dict(delivery_docs.to_dict())
        delivery_lis.append(delievery_par)
        #print(delivery_lis)
    #for문끝

    def delete_collection(coll_ref, batch_size):
        deldocs = coll_ref.limit(batch_size).stream()
        deleted = 0

        for doc in deldocs:
            print(f'Deleting doc {doc.id} => {doc.to_dict()}')
            doc.reference.delete()
            deleted = deleted + 1

        if deleted >= batch_size:
            return delete_collection(coll_ref, batch_size)

    delete_collection(cart_ref, 3)

    return render(request, 'getdata.html', {'delivery_lis': delivery_lis})
Пример #3
0
def create_order_delivery(delivery_info: dict):
    """
    创建一个订单配送记录
    :param delivery_info:
    :return:
    """
    delivery = Delivery(**delivery_info)
    delivery.save()
    return delivery
Пример #4
0
def getdata(request, option, price, username, phonenum, address, uid,
            delivery_message, product_id):

    delivery_lis = []

    prd_ref = db.collection(u'product').document(product_id)

    prd_docs = prd_ref.get()

    dictionary = literal_eval(option)

    if prd_docs.exists:
        products = Product.from_dict(prd_docs.to_dict())
        img = products.downloadurl
        brandname = products.brand
        product_name = products.name

        doc_ref = db.collection("delivery")
        doc_ref_random = db.collection("delivery").document()
        doc_ref_random.set(
            Delivery(brandname, product_name, dictionary, price, username,
                     phonenum, address, firestore.SERVER_TIMESTAMP, "배송전", "",
                     "", uid, delivery_message, img, product_id).to_dict())
        user_doc = db.collection("users").document(uid).collection(
            "delivery").document(doc_ref_random.id)
        user_doc.set(
            Delivery(brandname, product_name, dictionary, price, username,
                     phonenum, address, firestore.SERVER_TIMESTAMP, "배송전", "",
                     "", uid, delivery_message, img, product_id).to_dict())

        delievery_par = Delivery.from_dict(
            Delivery(brandname, product_name, dictionary, price, username,
                     phonenum, address, firestore.SERVER_TIMESTAMP, "배송전", "",
                     "", uid, delivery_message, img, product_id).to_dict())
        # delivery=Delivery.from_dict(delivery_docs.to_dict())
        delivery_lis.append(delievery_par)
        #print(delivery_lis)

        #유저에서 딜리버리 가져오기

    else:
        print(u'No such document!')

    return render(request, 'getdata.html', {'delivery_lis': delivery_lis})
Пример #5
0
 def post(self, request, format=None):
     agents = Delivery_Agent.objects.all().filter(on_delivery=False)
     node = Point(float(request.POST['x']),
                  float(request.POST['y']),
                  srid=4326)
     location_list = list()
     agent_list = list()
     selected_agent = Delivery_Agent.objects.annotate(
         distance=Distance('location', node)).order_by('distance').first()
     selected_agent.on_delivery = True
     selected_agent.save()
     delivery = Delivery()
     delivery.customer = Customer.objects.get(user=request.user)
     delivery.delivery_agent = selected_agent
     delivery.pickup_location = node
     delivery.location = selected_agent.location
     delivery.start_time = datetime.datetime.now()
     delivery.total_distance = selected_agent.location.distance(node) * 100
     delivery.save()
     content = {'agent': selected_agent.user.username}
     return Response(content)