Esempio n. 1
0
def confirm_takeout(request):
    # ************将XML解析为Json*******

    dict_data = xml_to_dict(request)
    order_dish = dict_data['dishes']
    ## 出现一单只有一个菜的情况
    if type(order_dish) == dict:
        order_dish = [order_dish]
    takeout_id = int(dict_data['takeout_id'])
    print(order_dish, takeout_id)
    takeout_ingredient = dishingredient(order_dish)
    all_consumption = takeout_ingredient.ingredient_all
    all_consumption_list = []
    for item in all_consumption.keys():
        consumption = {"ingredient_name":item, "ingredient_number":all_consumption[item]}
        all_consumption_list.append(consumption)
    #提取之前的all_order_log的外卖单号
    order_id_pre = all_order_log.objects.get(takeout = takeout_id).order_id

    # 更新向供应链发的订单
    scm_takeout = {"order_id": order_id_pre, "action":dict_data['action'], "raw_material":all_consumption_list}
    scm_takeout = dicttoxml.dicttoxml(scm_takeout, root = True, attr_type = False)
    url = base_url + 'g4/confirm_takeout_scm'
    r = requests.post(url, scm_takeout, headers = header)
    print("action",dict_data['action'])
    if int(dict_data['action']) == 0:
        print('外卖正式出库!')
        ## 添加到自己的订单细节数据集中
    # *************传入参数为order_id, order_dish和对应下单选项, 调用后台排程算法,直接修改waiting_list和station_id********
        KitchenUpdate = kitchen_update()
        KitchenUpdate.order_update(order_id_pre, order_dish)
    else:
        print('外卖预定的原材料释放!')    
    data = dicttoxml.dicttoxml({}, root = True, attr_type = False)
    return http.HttpResponse(data)
Esempio n. 2
0
def check_ingredient(ingredients):
    new_ingredient_name = [ig_detail["ingredient_name"] for ig_detail in ingredients]
    add_material = []
    # 查看现有的原材料
    supply_url = base_url + "g4/material"
    r = requests.get(supply_url)    
    all_material = xml_to_dict(r)['raw_material']
    all_remain_ingredient = dict()
    for ig_detail in all_material:
        all_remain_ingredient[ig_detail['ingredient_name']] = float(ig_detail['ingredient_number']) 
    # 仓库没有这道菜某些原材料
    for new_ig_name in new_ingredient_name:
        if new_ig_name not in list(all_remain_ingredient.keys()):
            add_material.append(new_ig_name) 
    if len(add_material)>0:
        url_add_material = base_url + 'g4/add_material'
        scm_request = {"add_material":add_material}
        data = dicttoxml.dicttoxml(scm_request, root = True, attr_type = False)
        #return http.HttpResponse(data)
        requests.post(url_add_material,data, headers = header)
        return 1

    ## 原材料不够
    else:
        success_status = 1
        for ig_detail in ingredients:
            ig_name = ig_detail['ingredient_name']
            ## 如果某个原材料严格不足,则标记success 均为失败.    
            if ig_name in all_remain_ingredient.keys():
                print(type(all_remain_ingredient[ig_name]), type(ig_detail['ingredient_number']))
                success_status = max(min(success_status, int(all_remain_ingredient[ig_name]/float(ig_detail['ingredient_number']))), 0)
        ## 这样也能够达到手动沽清的作用
        return 1 - success_status
Esempio n. 3
0
def add_order(request): 
    # ************将XML解析为Json*******
    dict_data = xml_to_dict(request)
    order_dish = dict_data['dishes']
    print(order_dish)
    table_id = int(dict_data['table_id'])
    serial_number = int(dict_data['serial'])
    
    dish_json = {"table_id": table_id, "success":1, "fail_dishes":None, "serial":serial_number}
    
    order_ingredient = dishingredient(order_dish)
    
    order_ingredient.left_order_ingredient()
    print(order_ingredient.ingredient_all)
    # 下单成功, 向仓库发出库单
    if len(order_ingredient.fail_dishes) == 0:
        print('向仓库发出库单!')
        ## 发给供应链的格式转化
        max_id = all_order_log.objects.all().aggregate(Max('order_id'))['order_id__max']
        if type(max_id) != int:
            max_id = 0
        all_consumption = order_ingredient.ingredient_all
        all_consumption_list = []
        for item in all_consumption.keys():
            consumption = {"ingredient_name":item, "ingredient_number":all_consumption[item]}
            all_consumption_list.append(consumption)
        print(all_consumption_list)
        scm_order = {"order_id": max_id + 1, "order_type":0, "raw_material":all_consumption_list}
        scm_order = dicttoxml.dicttoxml(scm_order, root = True, attr_type = False)
        # 向仓库发送POST请求 confirm_order_scm
        url = base_url + 'g4/confirm_order_scm'
        r = requests.post(url, scm_order, headers = header)
        # 向自己的数据库添加数据
        ## 在all_order_log中插入该条下单记录
        all_order_log.objects.create(order_id = max_id + 1, order_type = 0, table_id = table_id, serial = serial_number, takeout = -1)
# *************传入参数为order_dish, 调用后台排程算法,返回waiting_list和station_id********
        KitchenUpdate = kitchen_update()
        KitchenUpdate.order_update(max_id + 1, order_dish)
    # 下单失败
    else:
        dish_json['success'] = 0
        dish_json['fail_dishes'] = order_ingredient.fail_dishes

    data = dicttoxml.dicttoxml(dish_json, root = True, attr_type = False)
    return http.HttpResponse(data)
Esempio n. 4
0
 def post(self, request):
     from manager.models import dish
     #获取用户现在得到的current_dishes, 并将XML解析为json
     dish_json = xml_to_dict(request)
     current_dishes = dish_json['current_dishes']
     current_ingredient = dishingredient(current_dishes)
     dish_json = {"dishes":current_ingredient.left_current_ingredient()}
     #print(dish_json)
     data = dicttoxml.dicttoxml(dish_json, root = True, attr_type = False)
     return http.HttpResponse(data)
     return http.JsonResponse(dish_json)
Esempio n. 5
0
def add_takeout(request): 
    # ************将XML解析为Json*******

    dict_data = xml_to_dict(request)
    order_dish = dict_data['dishes']
    takeout_id = int(dict_data['takeout_id'])
    dish_json = {"takeout_id": takeout_id, "success":1, "fail_dishes":None}
    order_ingredient = dishingredient(order_dish)
    
    order_ingredient.left_order_ingredient()
    # 下单失败
    if len(order_ingredient.fail_dishes) > 0:
        dish_json['success'] = 0
        dish_json['fail_dishes'] = order_ingredient.fail_dishes
    
    else:
        print('外卖预出库!')
        # 外卖预出库
        ## 发送原材料消耗
        max_id = all_order_log.objects.all().aggregate(Max('order_id'))['order_id__max']
        all_consumption = order_ingredient.ingredient_all
        all_consumption_list = []
        for item in all_consumption.keys():
            consumption = {"ingredient_name":item, "ingredient_number":all_consumption[item]}
            all_consumption_list.append(consumption)
        print(all_consumption_list)
        # 向仓库发送POST请求 confirm_order_scm
        scm_order = {"order_id": max_id + 1, "order_type":1, "raw_material":all_consumption_list}
        scm_order = dicttoxml.dicttoxml(scm_order, root = True, attr_type = False)
        
        url = base_url + 'g4/confirm_order_scm'
        r = requests.post(url, scm_order, headers = header)
        # 向自己的数据库添加数据
        ## 在all_order_log中插入该条下单记录
        all_order_log.objects.create(order_id = max_id + 1, order_type = 1, table_id = -1, serial = -1, takeout = takeout_id)
    
    data = dicttoxml.dicttoxml(dish_json, root = True, attr_type = False)
    return http.HttpResponse(data)
Esempio n. 6
0
def nudge(request):
    dict_data = xml_to_dict(request)
    nudge_table_id = dict_data['table_id']
    nudge_serial = dict_data['serial']
    
    nudge_order_id = all_order_log.objects.get(table_id = nudge_table_id, serial = nudge_serial).order_id
    ## 已经催单,不需要再次调用了
    #for order_detail.objects.filter(order_id = 1)
    if order_detail.objects.filter(order_id = nudge_order_id).values('dish_status')[0]['dish_status'] == 1:
        print("您之前已经催单!")
        pass
    else:
        ##*************传入参数为order_id, 调用后台排程算法,直接修改waiting_list和station_id********
        ##向后厨更新数据库
        KitchenUpdate = kitchen_update()
        KitchenUpdate.nudge_update(nudge_order_id)
        print('后厨已接受您的催单!')
    data = dicttoxml.dicttoxml({}, root = True, attr_type = False)
    return http.HttpResponse(data)
Esempio n. 7
0
 def get(self, request):
     from manager.models import dish
    
     dish_list = list(dish.objects.all().values())
     dish_list = []
     for dish in dishes:
         dish = {
             "name":dish.name,
             "price":dish.price,
             "type":dish.dish_type,
             "id":dish.dish_id,
             "picture":dish.dish_pic
         }
         dish_list.append(dish)
     dish_json = {"dishes":dish_list}
     data = dicttoxml.dicttoxml(dish_json, root = True, attr_type = False)
     return http.HttpResponse(data)
     #print(data)
     return http.JsonResponse(dish_json)
Esempio n. 8
0
def remove_order(request):
    dict_data = xml_to_dict(request)
    remove_table_id = dict_data['table_id']
    remove_serial = dict_data['serial']
    remove_dishes = dict_data['dishes']
    if type(remove_dishes) == dict:
        remove_dishes = [remove_dishes]
    
    remove_order_id = all_order_log.objects.get(table_id = remove_table_id, serial = remove_serial).order_id
    
    # 标注相应的退菜记录
    for dish_detail in remove_dishes:
        print(dish_detail)
        print(remove_order_id, dish_detail['dish_id'])
        current_order_dish = order_detail.objects.get(order_id = remove_order_id, dish_id = int(dish_detail['dish_id']))
        current_dish_status = current_order_dish.dish_status
        
        # 如果这份菜还没有开始做, 之后的菜提前
        if current_dish_status == 0 or 1:
            current_station = current_order_dish.station_id
            current_wl = current_order_dish.waiting_list
            # 将这道菜标为废弃
            current_order_dish = order_detail.objects.filter(order_id = remove_order_id, dish_id = int(dish_detail['dish_id']))
            current_order_dish.update(dish_status = 3, waiting_list = -1)
            # 将之后的菜提前WL一位
            later_orders = order_detail.objects.filter(station_id = current_station, waiting_list__gt = current_wl)
            for later_order in later_orders:
                later_order.waiting_list = later_order.waiting_list - 1
                later_order.save()
        # 如果这份菜开始做了
        elif current_dish_status == 4:
        #提取开始做的时间,计算目前已经完成的菜数
        # 需要修改到时候是用秒还是分钟!!!
            time_speed = order_choice_log.objects.all().last().param
            have_seconds = (datetime.now() - current_order_dish.finish_time).total_seconds()/time_speed
            current_dish = dish.objects.get(dish_id = dish_detail['dish_id'])
            dish_time = current_dish.time_cost
            require_seconds = dish_time*(current_order_dish.count - dish_detail['count'])
            # 已经完全做完, 计算相应的已经花费的成本
            if require_seconds <= have_seconds:
                current_order_dish = order_detail.objects.filter(order_id = remove_order_id, dish_id = dish_detail['dish_id'])
                current_order_dish.update(dish_status = 3, waiting_list = -1, ingd_cost = have_seconds/dish_time*current_dish.ingd_cost)
                # 将之后的菜提前WL一位
                later_orders = order_detail.objects.filter(station_id = current_station, waiting_list__gt = 0)
                for later_order in later_orders:
                    later_order.waiting_list = later_order.waiting_list - 1
                    # 如果修改之前的WL值为1
                    if later_order.waiting_list == 0:
                        later_order.dish_status = 4
                        later_order.start_time = datetime.now()
                    later_order.save()

            # 剩余的并没有完全做完
            elif require_seconds > have_seconds:
                # 更新剩余的原材料份数
                ## 这种情况不算废弃, 因为剩余的仍然需要处理
                current_order_dish.count = current_order_dish.count - dish_detail['count']
                current_order_dish.save()            

        elif current_dish_status == 3:
            print('已经删了哥哥!')
        
        elif current_dish_status == 2:
            print("你退吧反正和我没啥关系了")
    data = dicttoxml.dicttoxml({}, root = True, attr_type = False)
    return http.HttpResponse(data)
Esempio n. 9
0
def kitchen_work():
    # 具体要执行的代码
    print(datetime.now().strftime('%Y%m%d %H:%M:%S'), '更新对应的排班记录!')
    ## 异常控制
    anomal_dishes = order_detail.objects.filter(dish_status=0, waiting_list=0)
    # 移到该队列最后
    for anomal_dish in anomal_dishes:
        wl_max = order_detail.objects.filter(
            dish_status=0, station_id=anomal_dish.station_id).aggregate(
                Max('waiting_list'))['waiting_list__max']
        if wl_max == 0 and len(
                order_detail.objects.filter(
                    dish_status=4, station_id=anomal_dish.station_id)) == 0:
            anomal_dish.start_time = datetime.now()
            anomal_dish.dish_status = 4
        else:
            anomal_dish.waiting_list = wl_max + 1
        anomal_dish.save()
    # try:
    current_dishes = order_detail.objects.filter(dish_status=4)
    for current_dish in current_dishes:

        consume_time = dish.objects.get(dish_id=current_dish.dish_id).time_cost
        time_speed = order_choice_log.objects.all().last().param
        #print(int(current_dish.count)*consume_time)
        if int(current_dish.count) * consume_time <= (
            (datetime.now() -
             current_dish.start_time).total_seconds()) / time_speed:
            print(current_dish.station_id, '有菜做完了!')
            # 修改相应的其他成本数据栏
            current_dish.dish_status = 2
            current_dish.ingd_cost = current_dish.count * dish.objects.get(
                dish_id=current_dish.dish_id).ingd_cost
            current_dish.save()

            # 此时需要给各种端口发更新
            current_dish_log = all_order_log.objects.get(
                order_id=current_dish.order_id.pk)
            ## ***************给自己的前端发消息***************
            dish_finish_time = current_dish.finish_time.strftime(
                '%Y%m%d %H:%M:%S')
            if current_dish_log.order_type == 0:
                ## 给机器人 (堂食,菜)
                url_robot = base_url + 'g5/finish_dish'
                dish_name = dish.objects.get(dish_id=current_dish.dish_id).name
                robot_info = {
                    "order_id": current_dish.order_id.pk,
                    "table_id": current_dish_log.table_id,
                    "name": dish_name,
                    "dish_count": current_dish.count
                }
                robot_info = dicttoxml.dicttoxml(robot_info,
                                                 root=True,
                                                 attr_type=False)
                #print('tosee why?')
                requests.post(url_robot, robot_info, headers=header)
                ## 给前台(堂食, 菜)
                url_order = base_url + 'g1/serve'
                table_info = {
                    "table_id":
                    current_dish_log.table_id,
                    "deliver_time":
                    dish_finish_time,
                    "dishes": [{
                        "dish_id": current_dish.dish_id,
                        "count": current_dish.count
                    }],
                    "serial":
                    current_dish_log.serial
                }
                table_info = dicttoxml.dicttoxml(table_info,
                                                 root=True,
                                                 attr_type=False)
                requests.post(url_order, table_info, headers=header)

            dish_order_id = current_dish.order_id.pk
            ## 判断这一单的菜是否全做完, 是否有正在等候等情况的菜
            order_finish_sign = order_detail.objects.filter(
                Q(order_id=dish_order_id),
                Q(dish_status=0) | Q(dish_status=1)
                | Q(dish_status=4)).exists()
            ## 不存在任何正在等的菜
            ## 给财务 (单)
            if order_finish_sign == False:
                url_account = base_url + 'g3/order_other_cost'
                order_total_cost = order_detail.objects.filter(
                    order_id=dish_order_id).aggregate(
                        Sum('ingd_cost'))['ingd_cost__sum']
                account_info = {
                    "order_id": dish_order_id,
                    "ingd_cost": order_total_cost,
                    "finish_time": dish_finish_time
                }
                account_info = dicttoxml.dicttoxml(account_info,
                                                   root=True,
                                                   attr_type=False)
                requests.post(url_account, account_info, headers=header)
                if current_dish_log.order_type == 1:
                    ## 给前台(外卖, 单)
                    url_takeout = base_url + 'g1/deliver_takeout'
                    takeout_id = all_order_log.objects.get(
                        order_id=dish_order_id).takeout
                    takeout_info = {
                        "takeout_id": takeout_id,
                        "deliver_time": dish_finish_time
                    }
                    takeout_info = dicttoxml.dicttoxml(takeout_info,
                                                       root=True,
                                                       attr_type=False)
                    requests.post(url_takeout, takeout_info, headers=header)
            # 将之后的菜提前WL一位
            later_orders = order_detail.objects.filter(
                station_id=current_dish.station_id, waiting_list__gt=0)
            for later_order in later_orders:
                later_order.waiting_list = later_order.waiting_list - 1
                # 如果修改之前的WL值为1
                if later_order.waiting_list == 0:
                    later_order.dish_status = 4
                    later_order.start_time = datetime.now()
                later_order.save()
def add_material_request(request):
    data = dicttoxml.dicttoxml({}, root=True, attr_type=False)
    return http.HttpResponse(data)
def takeout_request(request):
    data = dicttoxml.dicttoxml({}, root=True, attr_type=False)
    return http.HttpResponse(data)