@SuppressWarnings("unchecked")
 @ResponseBody
 @RequestMapping(value = "/admin/orders/acceptedOrders.ajax", method = RequestMethod.GET)
 public ResponseEntity<byte[]> listAcceptedOrders() throws Exception {
   List<Order> orders = orderRepository.findAllAcceptedOrders();
   Map<String, Object> model = new HashMap<String, Object>();
   model.put("success", true);
   model.put("orders", orders);
   model.put("count", orders.size());
   String[] excludes = new String[] {"orders.orderDiscounts"};
   return responseEntityUtils.buildResponse(model, excludes);
 }
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/order/deliveryEdit.ajax", method = RequestMethod.POST)
  public ResponseEntity<byte[]> buildDeliveryEdit(@RequestParam(value = "orderId") String orderId)
      throws Exception {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Building delivery options for orderId: " + orderId);
    }

    Map<String, Object> model = new HashMap<String, Object>();

    try {
      Order order = orderRepository.findByOrderId(orderId);
      Restaurant restaurant = order.getRestaurant();

      // Get opening times for today and the next three days
      DateTime currentTime = new DateTime();
      DateTime now =
          new DateTime(
              currentTime.getYear(),
              currentTime.getMonthOfYear(),
              currentTime.getDayOfMonth(),
              currentTime.getHourOfDay(),
              currentTime.getMinuteOfHour(),
              0,
              0);

      // Store if the restaurant is currently open
      boolean isOpen = restaurant.isOpen(currentTime);

      List<Integer> days = new ArrayList<Integer>();
      List<Set<LocalTime>> deliveryTimes = new ArrayList<Set<LocalTime>>();
      List<Set<LocalTime>> collectionTimes = new ArrayList<Set<LocalTime>>();

      // Get the remaining options for today first
      days.add(now.getDayOfWeek());
      DateTime[] openingAndClosingTimes = restaurant.getOpeningAndClosingTimes(now);
      DateTime earlyOpeningTime =
          openingAndClosingTimes[0] == null ? now : openingAndClosingTimes[0];
      DateTime lateOpeningTime =
          openingAndClosingTimes[2] == null ? now : openingAndClosingTimes[2];

      // For delivery times push the current time past the delivery time in minutes
      int deliveryTimeMinutes = restaurant.getDeliveryTimeMinutes();
      Pair<Set<LocalTime>, Set<LocalTime>> earlyDeliveryTimesPair =
          getTimeOptions(
              now.isBefore(earlyOpeningTime) ? earlyOpeningTime : now,
              openingAndClosingTimes[1],
              deliveryTimeMinutes);
      Pair<Set<LocalTime>, Set<LocalTime>> lateDeliveryTimesPair =
          getTimeOptions(
              now.isBefore(lateOpeningTime) ? lateOpeningTime : now,
              openingAndClosingTimes[3],
              deliveryTimeMinutes);
      earlyDeliveryTimesPair.first.addAll(lateDeliveryTimesPair.first);
      earlyDeliveryTimesPair.second.addAll(lateDeliveryTimesPair.second);

      // For delivery times push the current time past the collection time in minutes
      int collectionTimeMinutes = restaurant.getCollectionTimeMinutes();
      Pair<Set<LocalTime>, Set<LocalTime>> earlyCollectionTimesPair =
          getTimeOptions(
              now.isBefore(earlyOpeningTime) ? earlyOpeningTime : now,
              openingAndClosingTimes[1],
              collectionTimeMinutes);
      Pair<Set<LocalTime>, Set<LocalTime>> lateCollectionTimesPair =
          getTimeOptions(
              now.isBefore(lateOpeningTime) ? lateOpeningTime : now,
              openingAndClosingTimes[3],
              collectionTimeMinutes);
      earlyCollectionTimesPair.first.addAll(lateCollectionTimesPair.first);
      earlyCollectionTimesPair.second.addAll(lateCollectionTimesPair.second);

      // Add today's opening times
      deliveryTimes.add(earlyDeliveryTimesPair.first);
      collectionTimes.add(earlyCollectionTimesPair.first);

      // Now get the rest of the options for the remaining times
      for (int i = 0; i < 3; i++) {

        // Add any times after midnight from the previous list
        Set<LocalTime> deliveryTimesSet = new TreeSet<LocalTime>();
        Set<LocalTime> collectionTimesSet = new TreeSet<LocalTime>();
        deliveryTimesSet.addAll(earlyDeliveryTimesPair.second);
        collectionTimesSet.addAll(earlyCollectionTimesPair.second);

        // Now get the next set of opening and closing times
        now = now.plusDays(1);
        days.add(now.getDayOfWeek());

        openingAndClosingTimes = restaurant.getOpeningAndClosingTimes(now);
        earlyDeliveryTimesPair =
            getTimeOptions(
                openingAndClosingTimes[0], openingAndClosingTimes[1], deliveryTimeMinutes);
        lateDeliveryTimesPair =
            getTimeOptions(
                openingAndClosingTimes[2], openingAndClosingTimes[3], deliveryTimeMinutes);
        earlyDeliveryTimesPair.first.addAll(lateDeliveryTimesPair.first);
        earlyDeliveryTimesPair.second.addAll(lateDeliveryTimesPair.second);

        earlyCollectionTimesPair =
            getTimeOptions(
                openingAndClosingTimes[0], openingAndClosingTimes[1], collectionTimeMinutes);
        lateCollectionTimesPair =
            getTimeOptions(
                openingAndClosingTimes[2], openingAndClosingTimes[3], collectionTimeMinutes);
        earlyCollectionTimesPair.first.addAll(lateCollectionTimesPair.first);
        earlyCollectionTimesPair.second.addAll(lateCollectionTimesPair.second);

        // Add this day's time options to the list
        deliveryTimesSet.addAll(earlyDeliveryTimesPair.first);
        collectionTimesSet.addAll(earlyCollectionTimesPair.first);

        // Add these lists to the return
        deliveryTimes.add(deliveryTimesSet);
        collectionTimes.add(collectionTimesSet);
      }

      // Add the time options to the model
      model.put("days", days);
      model.put("deliveryTimes", deliveryTimes);
      model.put("collectionTimes", collectionTimes);
      model.put("collectionOnly", restaurant.getCollectionOnly());
      model.put("open", isOpen);
      model.put("success", true);

    } catch (Exception ex) {
      LOGGER.error("", ex);
      model.put("success", false);
      model.put("message", ex.getMessage());
    }
    return buildOrderResponse(model);
  }