예제 #1
0
def create_order():

    begin_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d')
    end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d')
    house_id = request.form.get('house_id')
    user_id = session['user_id']

    if not all([begin_date, end_date]):
        return jsonify(status_code.ORDER_BEGIN_END_DATA_NOT_NULL)

    if begin_date > end_date:
        return jsonify(status_code.ORDER_BEGIN_DATA_GT_END_DATE_ERROR)

    house = House.query.get(house_id)

    order = Order()
    order.user_id = user_id
    order.house_id = house_id
    order.begin_date = begin_date
    order.end_date = end_date
    order.days = (end_date - begin_date).days + 1
    order.house_price = house.price
    order.amount = order.days * order.house_price

    order.add_update()

    return jsonify(status_code.SUCCESS)
예제 #2
0
def order():

    order_dict = request.form

    house_id = order_dict.get('house_id')
    start_time = datetime.strptime(order_dict.get('start_time'), '%Y-%m-%d')
    end_time = datetime.strptime(order_dict.get('end_time'), '%Y-%m-%d')

    if not all([house_id, start_time, end_time]):
        return jsonify(status_code.PARAMS_ERROR)

    if start_time > end_time:
        return jsonify(status_code.ORDER_START_TIME_GT_END_TIME)

    house = House.query.get(house_id)

    order = Order()
    order.user_id = session['user_id']
    order.house_id = house_id
    order.begin_date = start_time
    order.end_date = end_time
    order.house_price = house.price
    order.days = (end_time - start_time).days + 1
    order.amount = order.days * order.house_price

    try:
        order.add_update()
        return jsonify(code=status_code.OK)
    except:
        return jsonify(status_code.DATABASE_ERROR)
예제 #3
0
def cart():
    form = OrderForm()

    my_cart = session.get('cart', [])
    title = f"Блюд в корзине: {len(my_cart)}" if my_cart else "Ваша корзина пуста"
    products = [Meal.query.get(product_id) for product_id in my_cart]
    total_amount = sum(product.price for product in products)
    total_amount_msg = f"Всего товаров на {total_amount} рублей"
    if request.method == 'GET' and current_user.is_authenticated:
        form.username.data = current_user.username
        form.email.data = current_user.email
        if current_user.address:
            form.address.data = current_user.address

    if form.validate_on_submit():
        new_order = Order()
        new_order.amount = total_amount
        new_order.state = OrderState.query.filter(
            OrderState.title == 'новый').first()
        new_order.user = current_user
        db.session.add(new_order)
        for item in my_cart:
            meal = Meal.query.get(item)
            new_order.meals.append(meal)
        db.session.commit()
        session['cart'] = []
        return redirect('/ordered/')
    return render_template('cart.html',
                           form=form,
                           products=products,
                           title=title,
                           total_amount=total_amount_msg)
예제 #4
0
def my_orders():

    if session:
        start = request.form.get('start')
        end = request.form.get('end')
        # span = request.form.get('span')
        house_id = request.form.get('house_id')

        starttime = datetime.strptime(start, '%Y-%m-%d')
        endtime = datetime.strptime(end, '%Y-%m-%d')

        house = House.query.get(house_id)
        order = Order()
        order.user_id = session['user_id']
        order.house_id = house_id
        order.begin_date = starttime
        order.end_date = endtime
        order.days = (endtime - starttime).days + 1
        order.house_price = house.price
        order.amount = order.days * order.house_price
        order.add_update()

        return jsonify({'code':200, 'msg':'ok'})
    else:
        return jsonify({ 'code':1122, 'msg':'未登录' })
예제 #5
0
def order():
    # 接收参数
    house_id = int(request.form.get('house_id'))
    start_date = datetime.strptime(request.form.get('start_date'), '%Y-%m-%d')
    end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d')
    # 验证有效性
    if not all([house_id, start_date, end_date]):
        return jsonify(status_code.PARAMS_ERROR)
    if start_date > end_date:
        return jsonify(status_code.ORDER_START_END_TIME_ERROR)
    # 查询房屋对象
    try:
        house = House.query.get(house_id)
    except:
        return jsonify(status_code.DATABASE_ERROR)
    # 创建订单对象
    order = Order()
    order.user_id = session['user_id']
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = (end_date - start_date).days + 1
    order.house_price = house.price
    order.amount = order.days * order.house_price

    try:
        order.add_update()
    except:
        return jsonify(status_code.DATABASE_ERROR)

    # 返回信息
    return jsonify(code=status_code.OK)
예제 #6
0
def orders():
    if request.method == 'GET':
        return render_template('orders.html')

    if request.method == 'POST':
        order = Order()
        house_id = request.form.get('id')
        house = House.query.filter(House.id == house_id).first()
        user_id = session['user_id']
        start = request.form.get('start')
        end = request.form.get('end')
        start_date = datetime.datetime.strptime(start, '%Y-%m-%d')
        end_date = datetime.datetime.strptime(end, '%Y-%m-%d')
        days = (end_date - start_date).days
        totle_days = days + 1
        house_price = house.price
        money = totle_days * house_price
        order.user_id = user_id
        order.house_id = house_id
        order.begin_date = start_date
        order.end_date = end_date
        order.days = totle_days
        order.house_price = house_price
        order.amount = money
        db.session.add(order)
        db.session.commit()
        return jsonify({'code': 200, 'msg': '上传订单成功'})
예제 #7
0
def booking():
    if request.method == 'GET':
        return render_template('booking.html')
    if request.method == 'POST':
        house_id = int(request.form.get('house_id'))
        house = House.query.filter(House.id == house_id).first()
        days = request.form.get('days')
        max_days = house.max_days
        begin_date = request.form.get('begin_date')
        end_date = request.form.get('end_date')
        begin_date = datetime.strptime(begin_date, '%Y-%m-%d')
        end_date = datetime.strptime(end_date, '%Y-%m-%d')
        amount = request.form.get('amount')
        user_id = session.get('user_id')
        order = Order()
        if house.max_days == 0 or days <= max_days:
            order.days = days
            order.house_id = house_id
            order.user_id = user_id
            order.begin_date = begin_date
            order.end_date = end_date
            order.house_price = house.price
            order.amount = amount
            order.add_update()
            return jsonify({'code': 200, 'msg': '请求成功'})
        return jsonify({'code': 10000, 'msg': '请求成功'})
예제 #8
0
def create_order():
    #获取参数
    dict = request.form
    house_id = int(dict.get('hosue_id'))

    start_date = datetime.strptime(dict.get('begin_date'), '%Y-%m-%d')
    end_date = datetime.strptime(dict.get('end_date'), '%Y-%m-%d')
    # 验证有效性
    if not all([house_id, start_date, end_date]):
        return jsonify(status_code.PARAMS_ERROR)
    if start_date > end_date:
        return jsonify(status_code.ORDER_START_END_TIME_ERROR)
    #获取房屋对象
    house = House.query.get(house_id)

    #创建订单对象
    order = Order()
    order.user_id = session['user_id']
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = (end_date - start_date).days + 1
    order.house_price = house.price
    order.amount = order.house_price * order.days

    try:
        order.add_update()
    except:
        return jsonify(status_code.DATABASE_ERROR)
    return jsonify(code=200, msg='请求成功')
예제 #9
0
def order():

    # 创建订单模型
    # 1.获取开始和结束时间
    begin_date = request.form.get('begin_date')
    end_date = request.form.get('end_date')

    begin_date = datetime.strptime(begin_date, '%Y-%m-%d')
    end_date = datetime.strptime(end_date, '%Y-%m-%d')
    # 2.获取当前用户和房屋id
    user_id = session['user_id']
    house_id = request.form.get('house_id')
    house = House.query.get(house_id)

    order = Order()
    order.user_id = user_id
    order.house_id = house_id
    order.begin_date = begin_date
    order.end_date = end_date
    order.days = (end_date - begin_date).days + 1
    order.house_price = house.price
    order.amount = order.days * house.price
    order.add_update()

    return jsonify(code=200)
예제 #10
0
def create_order():
    try:
        user_id = session['user_id']
    except:
        user_id = None
    if not user_id:
        return jsonify(status_code.LOGIN_STATUS_NOT_LOGIN)
    house_id = request.form.get('house_id')
    house = House.query.filter(House.id == house_id).first()
    price = house.price
    max_days = house.max_days
    sd = request.form.get('sd')
    ed = request.form.get('ed')
    sd = datetime.datetime.strptime(sd, '%Y-%m-%d')
    ed = datetime.datetime.strptime(ed, '%Y-%m-%d')
    if ed <= sd:
        return jsonify(status_code.BOOKING_STATUS_DATE_ERROR)
    days = (ed - sd).days
    if max_days != 0 and days > max_days:
        return jsonify(status_code.BOOKING_STATUS_MAX_DAYS_ERROR)
    amount = price * days
    order = Order()
    order.house_id = house_id
    order.user_id = user_id
    order.begin_date = sd
    order.end_date = ed
    order.days = days
    order.house_price = price
    order.amount = amount
    order.status = 'WAIT_ACCEPT'
    order.add_update()
    return jsonify(status_code.SUCCESS)
예제 #11
0
def check_in():
    uid = session.get('user_id')
    hid = request.form.get('hid')
    begin = request.form.get('begin')
    end = request.form.get('end')
    days = request.form.get('days')
    price = request.form.get('price')
    amount = request.form.get('amount')

    if not all([hid, begin, end, days, price, amount]):
        return jsonify(status_code.ORDER_PARAMS_NOT_COMPLETE)

    if begin > end:
        return jsonify(status_code.ORDER_TIME_PARAMS_ERROR)

    try:
        order = Order()
        order.user_id = uid
        order.house_id = hid
        order.begin_date = begin
        order.end_date = end
        order.days = days
        order.house_price = price
        order.amount = amount

        order.add_update()
        return jsonify(status_code.SUCCESS)
    except BaseException as e:
        print(e)
        db.session.rollback()
        return jsonify(status_code.DATABASE_ERROR)
예제 #12
0
def booking_post():
    house_id = request.form.get('house_id')
    house = House.query.get(house_id)
    if session['user_id'] == house.user_id:
        return jsonify(status_code.ORDER_HOUSE_USER_ID_IS_SESSION_ID)

    start_date = request.form.get('start_date')
    start_time = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_date = request.form.get('end_date')
    end_time = datetime.datetime.strptime(end_date, '%Y-%m-%d')
    days = (end_time-start_time).days + 1
    price = House.query.get(house_id).price
    amount = days * price

    order = Order()
    order.user_id = session['user_id']
    order.house_id = request.form.get('house_id')
    order.begin_date = start_time
    order.end_date = end_time
    order.days = days
    order.house_price = price
    order.amount = amount
    order.add_update()

    return jsonify(code=status_code.OK)
예제 #13
0
def add_orders():
    """
    添加订单
    :return:
    """
    s_date = request.form.get('start_date')
    e_date = request.form.get('end_date')
    h_id = request.form.get('house_id')
    days = request.form.get('days')

    # 不在选择区间的已完成或者待接单的订单
    order1 = Order.query.filter(Order.end_date > s_date,
                                Order.begin_date <= s_date,
                                Order.status != 'REJECTED').all()
    order2 = Order.query.filter(Order.end_date >= e_date,
                                Order.begin_date <= e_date,
                                Order.status != 'REJECTED').all()

    # 在选择区间内的待接单和已完成的订单
    # 所有的订单 和 房屋
    house1 = House.query.all()

    # 不能预订的房源id
    f_house_id = []
    for order in order1:
        f_house_id.append(order.house_id)
    for order in order2:
        f_house_id.append(order.house_id)

    house_id = []
    for house in house1:
        if house.id not in f_house_id:
            house_id.append(house.id)

    # 获取到有效的房屋id值 并去重
    houses_id = list(set(house_id))
    # 获取
    #  =======================================================================
    if int(h_id) in houses_id:
        order = Order()

        order.begin_date = s_date
        order.end_date = e_date
        order.house_id = h_id
        order.days = days

        house = House.query.get(order.house_id)
        house.order_count += 1
        order.house_price = house.price
        order.amount = request.form.get('amount')
        order.user_id = session['user_id']
        order.add_update()
        house.add_update()
        return jsonify({'code': 200, 'msg': '请求成功!'})

    already_order = Order.query.filter(Order.house_id == h_id,
                                       Order.status != 'REJECTED').first()
    data = already_order.to_dict()
    return jsonify({'code': 1001, 'msg': '该时间段已被预订', 'data': data})
예제 #14
0
def submit_order(id):
    order = Order()
    order.user_id = session.get('user_id')
    order.house_id = id
    order.begin_date = request.form.get('startDate')
    order.end_date = request.form.get('endDate')
    order.days = request.form.get('days')
    order.house_price = request.form.get('price')
    order.amount = request.form.get('amount')
    order.add_update()
    return jsonify({'code': 200, 'msg': '请求成功'})
예제 #15
0
def order_booking():
    order = Order()
    order.house_id = session['house_id']
    order.user_id = session['id']
    order.begin_date = request.form.get('sd')
    order.end_date = request.form.get('ed')
    order.house_price = request.form.get('price')
    order.amount = request.form.get('amount')
    order.days = request.form.get('days')
    order.save()
    return jsonify({'code': 200, 'msg': '请求成功'})
예제 #16
0
def stay():
    if request.method == 'POST':
        order = Order()
        order.amount = request.form.get('t')[1:].split('.')[0]
        order.days = request.form.get('d')
        order.begin_date = request.form.get('st')
        order.end_date = request.form.get('et')
        order.house_price = int(order.amount) / int(order.days)
        order.user_id = session['user_id']
        order.house_id = request.form.get('h_id')

        order.add_update()

        return jsonify({'code': 200, 'msg': '请求成功'})
예제 #17
0
def orders_post():
    house_id = request.form.get('house_id')
    begin_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d')
    end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d')
    house = House.query.get(house_id)
    order = Order()
    order.house_id = house_id
    order.user_id = session['user_id']
    order.begin_date = begin_date
    order.end_date = end_date
    order.days = (end_date - begin_date).days + 1
    order.house_price = house.price
    order.amount = order.house_price * order.days
    order.add_update()
    return jsonify(status_code.SUCCESS)
예제 #18
0
def order():
    house_id = request.form.get('house_id')
    start_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d')
    end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d')
    house = House.query.filter(House.id == house_id).first()

    order = Order()
    order.user_id = session['user_id']
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = (end_date - start_date).days
    order.house_price = house.price
    order.amount = order.days * order.house_price
    order.add_update()
    return jsonify(code=200, msg='ok', data=order.to_dict())
예제 #19
0
def orders_info():
    if request.method == 'POST':
        start_date = request.form.get('start_date')
        end_date = request.form.get('end_date')
        # order_amount = request.form.get('order_amount')
        days_amount = request.form.get('days_amount')

        house_id = session.get('house_id')
        user_id = session.get('user_id')
        house = House.query.get(house_id)

        order = Order()
        order.user_id = user_id
        order.house_id = house_id
        order.begin_date = start_date
        order.end_date = end_date
        days = int(float(days_amount) / house.price)
        # 判断days是否大于规定的
        if days > house.max_days or days < house.min_days:
            return jsonify({'code': 1111, 'msg': '日期有误,请重新选择!'})

        order.days = days
        order.house_price = house.price
        order.amount = days_amount
        order.add_update()
        session['order_id'] = order.id
        return jsonify({'code': 200, 'msg': '接收数据成功'})

    if request.method == 'GET':
        # 生成订单号
        seed = "1234567890abcdefghijklmnopqrstuvwxyz"
        sa = []
        for i in range(11):
            sa.append(random.choice(seed))
        order_num = ''.join(sa)
        order_id = session.get('order_id')
        order = Order.query.get(order_id)
        return jsonify({
            'code': 200,
            'msg': '接收数据成功',
            'order': order.to_dict(),
            'order_num': order_num
        })
예제 #20
0
def my_orders():
    order_dict = request.form
    house_id = order_dict.get('house_id')
    start_date = datetime.strptime(order_dict.get('start_date'), '%Y-%m-%d')
    end_date = datetime.strptime(order_dict.get('end_date'), '%Y-%m-%d')
    if not all([start_date,end_date]):
        return jsonify({'code':1001,'msg':'请选择日期'})

    house = House.query.get(house_id)
    order = Order()
    order.user_id = current_user.id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = (end_date - start_date).days + 1
    order.house_price = house.price
    order.amount = order.days * order.house_price
    order.add_update()
    return jsonify({'code':200})
예제 #21
0
def my_order():
    house_id = request.form.get('house_id')
    begin_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d')
    end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d')

    house = House.query.get(house_id)

    order = Order()
    order.user_id = session['user_id']
    order.house_id = house_id
    order.begin_date = begin_date
    order.end_date = end_date
    order.days = (end_date - begin_date).days + 1
    order.house_price = house.price
    order.amount = order.days * order.house_price

    order.add_update()

    return jsonify({'code': 200, 'msg': '请求成功'})
예제 #22
0
def submit_order():
    """提交订单"""
    # 获取数据
    user_id = session.get('user_id')
    if not user_id:
        return jsonify(status.NOT_LOGIN)
    house_id = session.get('house_id')
    sd = request.form.get('sd')
    ed = request.form.get('ed')

    house = House.query.filter(House.id == house_id).first()
    price = house.price
    days = int(request.form.get('days'))
    if not days >= house.min_days:
        return jsonify(status.SORT_TIME)
    max_days = house.max_days if house.max_days else 99999
    if not days <= max_days:
        return jsonify(status.LONG_TIME)
    sd = datetime.strptime(sd, '%Y-%m-%d')
    ed = datetime.strptime(ed, '%Y-%m-%d')

    # 拿到所有订单判断时间段
    orders = Order.query.filter(
        and_(Order.house_id == house_id,
             Order.status.in_(['WAIT_PAYMENT', 'PAID', 'WAIT_COMMENT'])))
    for order in orders:
        if ((order.begin_date <= sd) and (sd < order.end_date)) or \
                ((order.begin_date < ed) and (ed < order.end_date)):
            return jsonify({'code': 990, 'msg': '此时间段已被预订'})

    amount = price * days

    order = Order()
    order.user_id = user_id
    order.house_id = house_id
    order.begin_date = sd
    order.end_date = ed
    order.days = days
    order.house_price = price
    order.amount = amount
    order.add_update()
    return jsonify(status.SUCCESS)
예제 #23
0
def my_order():
    # 获取用户id, 开始时间, 结束时间
    order_dict = request.form
    house_id = order_dict.get('house_id')
    begin_date = datetime.strptime(order_dict.get('begin_date'), '%Y-%m-%d')
    end_date = datetime.strptime(order_dict.get('end_date'), '%Y-%m-%d')

    house = House.query.get(house_id)

    order = Order()
    order.user_id = session['user_id']
    order.house_id = house_id
    order.begin_date = begin_date
    order.end_date = end_date
    order.days = (end_date - begin_date).days + 1
    order.house_price = house.price
    order.amount = order.days * order.house_price
    order.add_update()

    return jsonify(status_code.SUCCESS)
예제 #24
0
def booking_data():
    house_id = int(request.form.get('house_id'))
    start_time = request.form.get('start_time')
    end_time = request.form.get('end_time')
    days = request.form.get('days')
    house_price = request.form.get('house_price')
    amount = request.form.get('amount')
    user_id = session.get('user_id')
    user_order = Order()
    user_order.user_id = user_id
    user_order.house_id = house_id
    user_order.begin_date = start_time
    user_order.end_date = end_time
    user_order.days = days
    user_order.house_price = house_price
    user_order.amount = amount
    user_order.add_update()
    house = House.query.filter_by(id=house_id).first()
    house.order_count += 1
    house.add_update()
    return jsonify({'code': 200, 'msg': '请求成功'})
예제 #25
0
def orders_post():
    house_id = request.form.get('house_id')
    start_date = request.form.get('start_date')
    start_time = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_date = request.form.get('end_date')
    end_time = datetime.datetime.strptime(end_date, '%Y-%m-%d')
    days = (end_time-start_time).days + 1
    price = House.query.get(house_id).price
    amount = days * price

    order = Order()
    order.user_id = session['user_id']
    order.house_id = request.form.get('house_id')
    order.begin_date = start_time
    order.end_date = end_time
    order.days = days
    order.house_price = price
    order.amount = amount
    order.add_update()

    return jsonify(status_code.OK)
예제 #26
0
def my_booking():
    data = request.form
    user_id = session['user_id']

    if 'house_id' in data:
        order = Order()
        order.house_id = data.get('house_id')
        order.user_id = user_id
        order.begin_date = data.get('sd')
        order.end_date = data.get('ed')
        order.days = data.get('days')
        order.house_price = data.get('price')
        order.amount = data.get('amount')
        order.add_update()

        return jsonify({
            'code': 200,
            'msg': '订单信息提交成功',
        })

    return jsonify({'code': 1000})
예제 #27
0
def add_order():
    if request.method == 'POST':
        startdate = request.form.get('startdate')
        enddate = request.form.get('enddate')
        house_id = request.form.get('house_id')
        days = int(request.form.get('days'))
        house = House.query.filter(House.id == house_id).first()
        if int(house.max_days) == 0:
            max_day = 100000
        else:
            max_day = int(house.max_days)
        # 判断输入入住时间是否合法
        if all([startdate, enddate, house_id]):
            # 判断入住时间是否在范围内
            if (int(house.min_days) <= days <= max_day):
                # 判断下单房源是不是自己发布的房源
                user_id = session.get('user_id')
                if house.user_id == user_id:
                    return jsonify({'code': 702, 'msg': '不能对自己的房源下单!'})
                else:
                    # 确定不是自己房源之后,执行下单操作
                    house_price = house.price
                    amount = house_price * days
                    status = "WAIT_ACCEPT"
                    # 创建订单
                    order = Order()
                    order.user_id = user_id
                    order.house_id = house_id
                    order.begin_date = startdate
                    order.end_date = enddate
                    order.days = days
                    order.house_price = house_price
                    order.amount = amount
                    order.status = status
                    order.add_update()
                    return jsonify({'code': 200, 'msg': '提交订单成功!'})
            else:
                return jsonify({'code': 701, 'msg': '请输入在可入住范围内选择入住时间!'})
        else:
            return jsonify({'code': 700, 'msg': '请选择正确的入住时间!'})
예제 #28
0
def my_booking():
    # 获取数据
    house_id = request.form.get('house_id')
    start_date = request.form.get('start_date')
    end_date = request.form.get('end_date')
    order_price = request.form.get('order_price')
    one_price = request.form.get('one_price')
    all_price = order_price.split('(')[0]
    day = order_price.split('共')[1].split('晚')[0]

    # 存入数据库
    order = Order()
    order.user_id = session['user_id']
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = day
    order.house_price = one_price
    order.amount = all_price
    order.add_update()

    return jsonify({'code': 200, 'msg': '请求成功'})
예제 #29
0
def my_order():
    begin_date = datetime.strptime(request.form.get('begin_date'), '%Y-%m-%d')
    end_date = datetime.strptime(request.form.get('end_date'), '%Y-%m-%d')

    # 获取当前用户和房屋id
    user_id = current_user.id
    house_id = request.form.get('house_id')
    # 获取房屋对象
    house = House.query.get(house_id)

    if house.user_id == user_id:
        return jsonify(status_code.USER_HOUSE_IS_NOT_SAME)
    order = Order()
    order.user_id = user_id
    order.house_id = house_id
    order.begin_date = begin_date
    order.end_date = end_date
    order.days = (end_date - begin_date).days + 1
    order.amount = order.days * house.price
    order.house_price = house.price
    order.add_update()
    return jsonify(status_code.SUCCESS)
예제 #30
0
def create_order():
    id = request.form.get('id')
    sd = request.form.get('sd')
    ed = request.form.get('ed')
    user_id = session.get('user_id')
    sd = datetime.datetime.strptime(sd, '%Y-%m-%d')
    ed = datetime.datetime.strptime(ed, '%Y-%m-%d')
    days = (ed - sd).days
    house = House.query.filter(House.id == id).first()
    price = house.price
    amount = days * price
    order = Order()
    order.user_id = user_id
    order.house_id = id
    order.begin_date = sd
    order.end_date = ed
    order.days = days
    order.house_price = price
    order.amount = amount
    order.status = 'WAIT_ACCEPT'
    order.add_update()
    return jsonify({'code': 200})
예제 #31
0
    def post(self, request, *args, **kwargs):
        complete_json_order = request.body

        complete_order_dict = json.loads(complete_json_order)

        customer_username = complete_order_dict["customer"]
        order_type = complete_order_dict["type"]
        product_items = complete_order_dict["productItems"]
        products_id_quantity = complete_order_dict["products"]
        date_fulfillment_scheduled = complete_order_dict["dateFulfilmentScheduled"]
        #dateFulfilmentScheduled

        order = Order()
        order.customer = User.objects.get(username=customer_username)
        order.date_initiated = datetime.now()
        order.date_fulfillment_scheduled = datetime.strptime(date_fulfillment_scheduled, '%d/%m/%Y')
        order.attendant_staff = request.user
        order.type = order_type

        if order.type == 'B':
            order.bulk_plan = BulkPlan.objects.get(owner__username=customer_username)

        if order.type == 'N':
            #calculate order price
            total_price = Decimal(0.0)
            for product in products_id_quantity:
                price = Product.objects.get(id=product["id"]).price
                total_this_product = price * product["quantity"]
                total_price += total_this_product

            order.amount = total_price
            order.amount_discount = 0
            order.amount_payable = order.amount - order.amount_discount

        order.order_number = OrderOperations.generate_order_number(order)
        order.save()

        action = OrderAction(order=order, action='C', actor=request.user,)
        action.save()

        # create order products
        for product in product_items:
            product_id = product["id"]
            product_serial = product["serialNumber"]
            product_items = product["items"]

            order_product = OrderProduct(order=order)
            order_product.product = Product.objects.get(id=product_id)
            order_product.serial_number = product_serial
            order_product.product_name = order_product.product.name
            order_product.product_price = order_product.product.price

            order_product.save()

            # create and add orderitems to order product
            for item in product_items:
                item_serial = item["serialNumber"]
                item_id = item["id"]
                item_colour_id = item["colourId"]
                item_alteration_id = item["alterationId"]

                item_model = ProductItem.objects.get(id=item_id).item

                order_item = OrderItem(order_product=order_product, serial_number=item_serial, item=item_model,)

                if item_colour_id:
                    order_item.colour = Colour.objects.get(id=item_colour_id)

                if item_alteration_id:
                    order_item.alteration = Alteration.objects.get(id=item_alteration_id)
                
                order_item.item_name = item_model.name
                order_item.item_tag = OrderOperations.generate_item_tag(order.order_number, product_serial, item_serial)

                order_item.save()
        
        # send order email
        to_email = order.customer.email
        logging.error(to_email,)
        if to_email:
            context_dictionary = dict()

            context_dictionary['company'] = General.get_company_information()
            context_dictionary['order'] = order

            invoice_email = Notifications.generate_email('invoice.htm', context_dictionary)
            response = Notifications.send_email(invoice_email, "New Fullers Haven Order", to_email,) 

        # send order SMS
        to_phone = order.customer.profile.phone if order.customer.profile else None
        logging.error(to_phone,)

        if to_phone:
            context_data = {'order_number': order.order_number, 
                            'number_of_items': order.number_of_items, 
                            'collection_date': GlobalOperations.get_date_as_text(order.date_fulfillment_scheduled, False)
                            }
            response = Notifications.send_smssolutions_sms('sms/new_order.txt', context_data, "234{0}".format(to_phone[1:]),)

        logging.debug("Good boy",)
        return Response({'order_id' : order.id}, status=status.HTTP_201_CREATED)