Esempio n. 1
0
def create_order():
    json_dict = request.json
    item = Order()
    item.user_id = json_dict['user_id']

    order_items_dict = json_dict['order_items']
    for order_item_dict in order_items_dict:
        order_item = OrderItem()
        if order_item.update_from_dict(order_item_dict):
            item.order_items.append(order_item)

    item.update_from_dict(json_dict)

    user = User.query.get(item.user_id)

    #TODO renable when voucher functionality is done
    # if user.subscribed:
    #     if UserSubscription.check_subscription_active(user.id):
    #         return Responses.SUBSCRIPTION_INACTIVE()

    # if 'voucher_codes' in json_dict.keys():
    #     voucher_codes = json_dict['voucher_codes']
    #     number_vouchers_allowed = int(ConfigValues.get_config_value('max_no_of_vouchers'))
    #     if len(voucher_codes) >  number_vouchers_allowed:
    #         return Responses.NO_VOUCHERS_EXCEEDED()
    #     vouchers = Voucher.get_vouchers(voucher_codes)
    #     if not vouchers[0]:
    #         return Responses.INVALID_VOUCHER()
    #     valid = Voucher.validate_voucher(vouchers)
    #     if valid:
    #         item.vouchers = vouchers
    #         item.calculate_discounted_cost()
    # else:
    details = item.calculate_cost()

    if details != -1:
        user.update({
            'number_of_items_ordered_this_month':
            int(details['no_items_this_month']),
            'month_first_order':
            details['month_first_order']
        })

    # convert date
    if len(item.update(json_dict, force_insert=True)) > 0:
        return Responses.OPERATION_FAILED()

    #send email confirmation to user
    Order.send_order_confirmation_email(order_number=item.id,
                                        user_email=user.email)
    # for item in item.order_items:
    #     item.m date_first_month_order.strftime('%Y-%m-%d %H:%M:%S')
    return res(item.as_dict())
Esempio n. 2
0
def add_order():
    json_dict = request.json
    item = Order()
    item.user_id = json_dict['user_id']
    item.status_id = json_dict['status_id']
    order_items_dict = json_dict['order_items']

    for order_item_dict in order_items_dict:
        order_item = OrderItem()       
        order_item.update_from_dict(order_item_dict)
        item.order_items.append(order_item)
    
    item.calculate_cost()
    
    if len(item.update(force_insert=True)) > 0:
        return Responses.OPERATION_FAILED()
    return res(item.as_dict())
Esempio n. 3
0
def calculate_order_discount():
    json_dict = request.json
    item = Order()
    order_items_dict = json_dict['order_items']

    for order_item_dict in order_items_dict:
        order_item = OrderItem()
        if order_item.update_from_dict(order_item_dict):
            item.order_items.append(order_item)

    voucher_codes = json_dict['voucher_codes']
    number_vouchers_allowed = int(
        ConfigValues.get_config_value('max_no_of_vouchers'))
    if len(voucher_codes) > number_vouchers_allowed:
        return Responses.NO_VOUCHERS_EXCEEDED()
    vouchers = Voucher.get_vouchers(voucher_codes)
    if not vouchers[0]:
        return Responses.INVALID_VOUCHER()
    valid = Voucher.validate_voucher(vouchers)
    if valid:
        item.vouchers = vouchers
        item.calculate_discounted_cost()
    return res(item.as_dict())