コード例 #1
0
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/admin/orders/list.ajax", method = RequestMethod.GET)
  public ResponseEntity<byte[]> list(
      @RequestParam(value = "page") int page,
      @RequestParam(value = "limit") int limit,
      @RequestParam(value = "sort", required = false) String sort,
      @RequestParam(value = "query", required = false) String query,
      HttpServletRequest req)
      throws Exception {

    PageRequest request;

    if (StringUtils.hasText(sort)) {
      List<Map<String, String>> sortParams =
          (List<Map<String, String>>) jsonUtils.deserialize(sort);
      Map<String, String> sortProperties = sortParams.get(0);
      String direction = sortProperties.get("direction");
      String property = sortProperties.get("property");
      request = new PageRequest(page - 1, limit, ("ASC".equals(direction) ? ASC : DESC), property);
    } else {
      request = new PageRequest(page - 1, limit);
    }

    List<Filter> filters = FilterUtils.extractFilters(req, filterDecoratorMap);
    List<Order> orders = orderRepository.pageByOrderId(request, query, filters);
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("orders", orders);
    model.put("count", orderRepository.count(query, filters));
    String[] excludes = new String[] {"orders.orderDiscounts"};
    return responseEntityUtils.buildResponse(model, excludes);
  }
コード例 #2
0
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/order/updateItemQuantity.ajax", method = RequestMethod.POST)
  public ResponseEntity<byte[]> updateItemQuantity(
      HttpServletRequest request, @RequestParam(value = "body") String body) throws Exception {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Updating order items: " + body);
    }

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

    try {
      // Extract request parameters
      Map<String, Object> params = (Map<String, Object>) jsonUtils.deserialize(body);
      String orderItemId = (String) params.get("orderItemId");
      Integer quantity = (Integer) params.get("quantity");

      HttpSession session = request.getSession(true);
      String orderId = (String) session.getAttribute("orderid");
      Order order = null;
      if (orderId != null) {
        order = orderRepository.findByOrderId(orderId);
        if (order != null) {
          order.updateItemQuantity(orderItemId, quantity);
          order = orderRepository.saveOrder(order);
          // Update can checkout status of order
          session.setAttribute("cancheckout", order.getCanCheckout());

          // Update order restaurant id session attribute if any items present
          if (order.getOrderItems().size() > 0) {
            session.setAttribute("orderrestaurantid", order.getRestaurantId());
            session.setAttribute("orderrestauranturl", order.getRestaurant().getUrl());
          } else {
            // If the restaurant session id does not match the order restaurant id, update the order
            String restaurantId = (String) session.getAttribute("restaurantid");
            if (!order.getRestaurantId().equals(restaurantId)) {
              Restaurant restaurant = restaurantRepository.findByRestaurantId(restaurantId);
              order.setRestaurant(restaurant);
              order = orderRepository.save(order);
            }
            session.removeAttribute("orderrestaurantid");
            session.removeAttribute("orderrestauranturl");
          }
        }
      }
      model.put("success", true);
      model.put("order", order);
    } catch (Exception ex) {
      LOGGER.error("", ex);
      model.put("success", false);
      model.put("message", ex.getMessage());
    }
    return buildOrderResponse(model);
  }
コード例 #3
0
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/order/updateFreeItem.ajax", method = RequestMethod.POST)
  public ResponseEntity<byte[]> updateFreeItem(
      HttpServletRequest request, @RequestParam(value = "body") String body) throws Exception {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Updating free item: " + body);
    }

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

    try {
      // Extract request parameters
      Map<String, Object> params = (Map<String, Object>) jsonUtils.deserialize(body);
      String discountId = (String) params.get("discountId");
      String freeItem = (String) params.get("freeItem");

      HttpSession session = request.getSession(true);
      String orderId = (String) session.getAttribute("orderid");
      Order order = null;
      if (orderId != null) {
        order = orderRepository.findByOrderId(orderId);
        if (order != null) {
          OrderDiscount orderDiscount = order.getOrderDiscount(discountId);
          if (orderDiscount != null) {
            orderDiscount.setSelectedFreeItem(freeItem);
            order = orderRepository.save(order);
          }

          // Update order restaurant id session attribute if any items present
          if (order.getOrderItems().size() > 0) {
            session.setAttribute("orderrestaurantid", order.getRestaurantId());
            session.setAttribute("orderrestauranturl", order.getRestaurant().getUrl());
          } else {
            session.removeAttribute("orderrestaurantid");
            session.removeAttribute("orderrestauranturl");
          }

          // Update can checkout status of order
          session.setAttribute("cancheckout", order.getCanCheckout());
        }
      }

      model.put("success", true);
      model.put("order", order);
    } catch (Exception ex) {
      LOGGER.error("", ex);
      model.put("success", false);
      model.put("message", ex.getMessage());
    }
    return buildOrderResponse(model);
  }
コード例 #4
0
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/order/checkSpecialOffer.ajax", method = RequestMethod.POST)
  public ResponseEntity<byte[]> checkSpecialOfferAvailability(
      HttpServletRequest request, @RequestParam(value = "body") String body) throws Exception {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Checking if special offer is applicable for order");
    }

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

    try {

      // Extract request parameters
      Map<String, Object> params = (Map<String, Object>) jsonUtils.deserialize(body);
      String restaurantId = (String) params.get("restaurantId");
      String orderId = (String) params.get("orderId");
      String specialOfferId = (String) params.get("specialOfferId");

      // Get the restaurant object
      Restaurant restaurant = restaurantRepository.findByRestaurantId(restaurantId);
      SpecialOffer specialOffer = restaurant.getSpecialOffer(specialOfferId);

      // Get the order object
      if (orderId != null) {
        Order order = orderRepository.findByOrderId(orderId);
        model.put("success", true);
        model.put("applicable", specialOffer.isApplicableTo(order));
      } else {
        model.put("success", true);
        model.put("applicable", specialOffer.isAvailableAt(new DateTime()));
      }
    } catch (Exception ex) {
      LOGGER.error("", ex);
      model.put("success", false);
      model.put("message", ex.getMessage());
    }
    return buildOrderResponse(model);
  }
コード例 #5
0
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/order/updateOrderDelivery.ajax", method = RequestMethod.POST)
  public ResponseEntity<byte[]> updateOrderDelivery(
      HttpServletRequest request, @RequestParam(value = "body") String body) throws Exception {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Updating order delivery type");
    }

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

    try {
      // Extract request parameters
      Map<String, Object> params = (Map<String, Object>) jsonUtils.deserialize(body);
      String orderId = (String) params.get("orderId");
      String deliveryType = (String) params.get("deliveryType");
      Integer dayOffset = Integer.valueOf((String) params.get("dayIndex"));
      String time = (String) params.get("time");

      // Get the order and update the delivery type
      Order order = orderRepository.findByOrderId(orderId);
      order.setDeliveryType(deliveryType);

      // Clear existing expected delivery/collection times
      order.setExpectedCollectionTime(null);
      order.setExpectedDeliveryTime(null);

      // If the selected time is 'ASAP' no need to do any processing
      if (!"ASAP".equals(time)) {
        DateTime now = new DateTime().plusDays(dayOffset);
        MutableDateTime expectedDate =
            new MutableDateTime(
                now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0, 0);
        Integer hour = Integer.valueOf(time.split(":")[0]);
        Integer minute = Integer.valueOf(time.split(":")[1]);
        expectedDate.setHourOfDay(hour);
        expectedDate.setMinuteOfHour(minute);
        if (Order.DELIVERY.equals(deliveryType)) {
          order.setExpectedDeliveryTime(expectedDate.toDateTime());
        } else {
          order.setExpectedCollectionTime(expectedDate.toDateTime());
        }
      }

      order = orderRepository.saveOrder(order);

      // Update order restaurant id session attribute if any items present
      HttpSession session = request.getSession(true);
      if (order.getOrderItems().size() > 0) {
        session.setAttribute("orderrestaurantid", order.getRestaurantId());
        session.setAttribute("orderrestauranturl", order.getRestaurant().getUrl());
      } else {
        session.removeAttribute("orderrestaurantId");
        session.removeAttribute("orderrestauranturl");
      }

      // Update can checkout status of order
      session.setAttribute("cancheckout", order.getCanCheckout());

      // All worked ok
      model.put("success", true);
      model.put("order", order);
    } catch (Exception ex) {
      LOGGER.error("", ex);
      model.put("success", false);
      model.put("message", ex.getMessage());
    }
    return buildOrderResponse(model);
  }
コード例 #6
0
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/order/addSpecialOffer.ajax", method = RequestMethod.POST)
  public ResponseEntity<byte[]> addSpecialOfferToOrder(
      HttpServletRequest request, @RequestParam(value = "body") String body) throws Exception {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Adding special offer to order: " + body);
    }

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

    try {

      // Extract request parameters
      Map<String, Object> params = (Map<String, Object>) jsonUtils.deserialize(body);
      String restaurantId = (String) params.get("restaurantId");
      String specialOfferId = (String) params.get("specialOfferId");
      List<String> itemChoices = (List<String>) params.get("itemChoices");
      List<String> itemChoiceCosts = (List<String>) params.get("itemChoiceCosts");
      Integer quantity = Integer.valueOf(params.get("quantity").toString());

      // Get the restaurant object
      Restaurant restaurant = restaurantRepository.findByRestaurantId(restaurantId);
      SpecialOffer specialOffer = restaurant.getSpecialOffer(specialOfferId);

      // Get the order out of the session
      HttpSession session = request.getSession(true);
      String orderId = (String) session.getAttribute("orderid");
      Order order;
      if (orderId == null) {
        order = buildAndRegister(session, restaurantId);
      } else {
        order = orderRepository.findByOrderId(orderId);
        if (order == null) {
          order = buildAndRegister(session, restaurantId);
        }
      }

      // Check if the special offer is applicable to this order
      if (!specialOffer.isApplicableTo(order)) {
        model.put("success", true);
        model.put("applicable", false);
      } else {
        // Wipe existing order if a new restaurant is selected
        if (!restaurantId.equals(order.getRestaurantId())) {
          order.setRestaurantId(restaurantId);
          order.setRestaurant(restaurant);
          order.getOrderItems().clear();
          order.getOrderDiscounts().clear();
        }

        // Build new order item
        OrderItem orderItem = new OrderItem();
        orderItem.setMenuItemNumber(specialOffer.getNumber());
        orderItem.setMenuItemId(specialOfferId);
        orderItem.setMenuItemTitle(specialOffer.getTitle());
        orderItem.setAdditionalItems(itemChoices);
        orderItem.setQuantity(quantity);
        double additionalCost = 0d;
        for (String itemChoiceCost : itemChoiceCosts) {
          additionalCost += Double.valueOf(itemChoiceCost);
        }
        orderItem.setCost(specialOffer.getCost() + additionalCost);

        // Add new order item to order and update
        order.addOrderItem(orderItem);
        order = orderRepository.saveOrder(order);

        // Update can checkout status of order
        session.setAttribute("cancheckout", order.getCanCheckout());

        // Update order restaurant id session attribute if any items present
        if (order.getOrderItems().size() > 0) {
          session.setAttribute("orderrestaurantid", order.getRestaurantId());
          session.setAttribute("orderrestauranturl", order.getRestaurant().getUrl());
        } else {
          session.removeAttribute("orderrestaurantId");
          session.removeAttribute("orderrestauranturl");
        }

        // Return success
        model.put("success", true);
        model.put("applicable", true);
        model.put("order", order);
      }
    } catch (Exception ex) {
      LOGGER.error("", ex);
      model.put("success", false);
      model.put("message", ex.getMessage());
    }
    return buildOrderResponse(model);
  }
コード例 #7
0
  @SuppressWarnings("unchecked")
  @ResponseBody
  @RequestMapping(value = "/order/addItem.ajax", method = RequestMethod.POST)
  public ResponseEntity<byte[]> addToOrder(
      HttpServletRequest request, @RequestParam(value = "body") String body) throws Exception {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Adding to order: " + body);
    }

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

    try {
      // Extract request parameters
      Map<String, Object> params = (Map<String, Object>) jsonUtils.deserialize(body);
      String restaurantId = (String) params.get("restaurantId");
      String itemId = (String) params.get("itemId");
      String itemType = (String) params.get("itemType");
      String itemSubType = (String) params.get("itemSubType");
      List<String> additionalItems = (List<String>) params.get("additionalItems");
      Integer quantity = Integer.valueOf(params.get("quantity").toString());

      // Get the restaurant object
      Restaurant restaurant = restaurantRepository.findByRestaurantId(restaurantId);
      MenuItem menuItem = restaurant.getMenuItem(itemId);

      // Build new order item
      OrderItem orderItem = new OrderItem();
      orderItem.setMenuItemNumber(menuItem.getNumber());
      orderItem.setMenuItemId(itemId);
      orderItem.setMenuItemTitle(menuItem.getTitle());
      orderItem.setMenuItemTypeName(itemType);
      orderItem.setMenuItemSubTypeName(itemSubType);
      orderItem.setAdditionalItems(additionalItems);
      orderItem.setQuantity(quantity);

      // Work out the cost of any additional Items
      double additionalItemCost = 0d;
      for (String additionalItemName : additionalItems) {
        if (StringUtils.hasText(itemType)) {
          MenuItemTypeCost menuItemTypeCost = menuItem.getMenuItemTypeCost(itemType);
          additionalItemCost +=
              menuItemTypeCost.getAdditionalItemCost() == null
                  ? 0d
                  : menuItemTypeCost.getAdditionalItemCost();
        } else if (menuItem.getAdditionalItemCost() != null) {
          additionalItemCost += menuItem.getAdditionalItemCost();
        } else {
          MenuItemAdditionalItemChoice additionalItemChoice =
              menuItem.getMenuItemAdditionalItemChoice(additionalItemName);
          additionalItemCost +=
              additionalItemChoice.getCost() == null ? 0d : additionalItemChoice.getCost();
        }
      }

      // Build the cost of the item
      if (StringUtils.hasText(itemType)) {
        MenuItemTypeCost menuItemTypeCost = menuItem.getMenuItemTypeCost(itemType);
        orderItem.setCost(menuItemTypeCost.getCost() + additionalItemCost);
      } else if (StringUtils.hasText(itemSubType)) {
        MenuItemSubType menuItemSubType = menuItem.getMenuItemSubType(itemSubType);
        orderItem.setCost(menuItemSubType.getCost() + additionalItemCost);
      } else {
        orderItem.setCost(menuItem.getCost() + additionalItemCost);
      }

      // Get the order out of the session
      HttpSession session = request.getSession(true);
      String orderId = (String) session.getAttribute("orderid");
      Order order;
      if (orderId == null) {
        order = buildAndRegister(session, restaurantId);
      } else {
        order = orderRepository.findByOrderId(orderId);
        if (order == null) {
          order = buildAndRegister(session, restaurantId);
        } else if (!restaurantId.equals(order.getRestaurantId())) {
          order.setRestaurant(restaurant);
          order.getOrderItems().clear();
          order.getOrderDiscounts().clear();
        }
      }

      // Add new order item to order and update
      order.addOrderItem(orderItem);
      order = orderRepository.saveOrder(order);

      // Update can checkout status of order
      session.setAttribute("cancheckout", order.getCanCheckout());

      // Update order restaurant id session attribute if any items present
      if (order.getOrderItems().size() > 0) {
        session.setAttribute("orderrestaurantid", order.getRestaurantId());
        session.setAttribute("orderrestauranturl", order.getRestaurant().getUrl());
      } else {
        session.removeAttribute("orderrestaurantid");
        session.removeAttribute("orderrestauranturl");
      }

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