def update_item(item_id): id_cart, list_item = utils.list_item_of_user(current_user.id) data = request.json list_book = utils.get_list_book() for item in list_item: if (str(item.id) == item_id): if ('quantity' in data): for book in list_book: if str(book.id) == str(item.id_book): if book.import_number - book.sold - int(data['quantity']) <20: return jsonify(({ 'code': 300 })) item.quantity = int(data['quantity']) db.session.commit() total_quantity, total_amount = utils.cart_stats(current_user.id) print(total_amount, total_quantity) return jsonify(({ 'code': 200, 'total_quantity': total_quantity, 'total_amount': total_amount })) return jsonify(({ 'code': 500 }))
def pay(): try: id_cart, list_item = utils.list_item_of_user(current_user.id) cart = utils.get_item_by_id_cart(id_cart) total_quantity, total_amount = utils.cart_stats(current_user.id) data = request.json name_receiver = data.get('name_receiver') phone_number_receiver = data.get('phone_number_receiver') address_receiver = data.get('address_receiver') bill = Bill(id_user = current_user.id, address_delivery=address_receiver, phone=phone_number_receiver, name_delivery=name_receiver, total_price = total_amount) db.session.add(bill) print(bill.id) for p in cart: if(p.would_buy==1): bill_detail = BillDetail(Bill=bill, id_book=p.id_book, price=p.discount, quantity=p.quantity) db.session.add(bill_detail) db.session.delete(p) db.session.commit() return jsonify({ 'message':'Thành Công' }) except: return jsonify({ 'message': 'Thất bại' })
def delete_item(item_id): id_cart, list_item =utils.list_item_of_user(current_user.id) for item in list_item: if(str(item.id) == item_id): db.session.delete(item) db.session.commit() return jsonify({ 'message': 'Xóa thành công' }) return jsonify({ 'message': 'Xóa thất bại' })
def add_to_cart(): if not (current_user.is_authenticated): return jsonify({ "message": "Vui lòng đăng nhập" }) try: data = request.json id_book = str(data.get('id')) name = data.get('name') price = data.get('price') discount = data.get('discount') quantity = data.get('quantity') id_cart, list_item = utils.list_item_of_user(current_user.id) list_book = utils.get_list_book() for l in list_book: if str(l.id) == id_book: book = l break if book.import_number - book.sold - int(quantity) <20: return jsonify({ "message": "Số lượng sách vượt quá!!!" }) flag = 0 for item in list_item: if (str(item.id_book) == id_book): item.quantity += quantity flag = 1 db.session.commit() if (flag == 0): newitem = CartItem(id_cart=id_cart, id_book=id_book, quantity=quantity, price=price, discount=price * (1 - discount)) db.session.add(newitem) db.session.commit() return jsonify({ "message": "Thêm thành công" }) except: return jsonify({ "message": "Thêm thất bại" })
def confirm_pay(): id_cart, list_item = utils.list_item_of_user(current_user.id) total_quantity, total_amount = utils.cart_stats(current_user.id) dola = "{:.2f}".format(total_amount/23000) return render_template('confirm__pay.html', id_cart = id_cart, list_item = list_item, total_amount = total_amount, total_quantity=total_quantity, list_book = utils.load_Book(),list_book_category=utils.get_book_category(), list_image = utils.get_all_image(), dola = dola)
def payment(): id_cart, list_item = utils.list_item_of_user(current_user.id) total_quantity, total_amount = utils.cart_stats(current_user.id) return render_template('payment.html', id_cart = id_cart, list_item = list_item, total_amount = total_amount, total_quantity=total_quantity, list_book = utils.load_Book(),list_book_category=utils.get_book_category(), list_image = utils.get_all_image())