@RequestMapping(value = "/summary/createUpdateDeliveryAddress.json", method = RequestMethod.POST)
  @RequireHardLogIn
  public String createUpdateDeliveryAddress(
      final Model model, @Valid final AddressForm form, final BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
      model.addAttribute("edit", Boolean.valueOf(StringUtils.isNotBlank(form.getAddressId())));
      // Work out if the address form should be displayed based on the payment type
      final B2BPaymentTypeData paymentType = getCheckoutFacade().getCheckoutCart().getPaymentType();
      final boolean payOnAccount =
          paymentType != null
              && CheckoutPaymentType.ACCOUNT.getCode().equals(paymentType.getCode());
      model.addAttribute("showAddressForm", Boolean.valueOf(!payOnAccount));

      return ControllerConstants.Views.Fragments.SingleStepCheckout.DeliveryAddressFormPopup;
    }

    // create delivery address and set it on cart
    final AddressData addressData = new AddressData();
    addressData.setId(form.getAddressId());
    addressData.setTitleCode(form.getTitleCode());
    addressData.setFirstName(form.getFirstName());
    addressData.setLastName(form.getLastName());
    addressData.setLine1(form.getLine1());
    addressData.setLine2(form.getLine2());
    addressData.setTown(form.getTownCity());
    addressData.setPostalCode(form.getPostcode());
    addressData.setCountry(getI18NFacade().getCountryForIsocode(form.getCountryIso()));
    addressData.setShippingAddress(
        Boolean.TRUE.equals(form.getShippingAddress())
            || Boolean.TRUE.equals(form.getSaveInAddressBook()));

    addressData.setVisibleInAddressBook(
        Boolean.TRUE.equals(form.getSaveInAddressBook())
            || StringUtils.isNotBlank(form.getAddressId()));
    addressData.setDefaultAddress(Boolean.TRUE.equals(form.getDefaultAddress()));

    if (StringUtils.isBlank(form.getAddressId())) {
      getUserFacade().addAddress(addressData);
    } else {
      getUserFacade().editAddress(addressData);
    }

    getCheckoutFacade().setDeliveryAddress(addressData);

    if (getCheckoutFacade().getCheckoutCart().getDeliveryMode() == null) {
      getCheckoutFacade().setDeliveryModeIfAvailable();
    }

    model.addAttribute("createUpdateStatus", "Success");
    model.addAttribute("addressId", addressData.getId());

    return REDIRECT_PREFIX
        + "/checkout/single/summary/getDeliveryAddressForm.json?addressId="
        + addressData.getId()
        + "&createUpdateStatus=Success";
  }
  @ResponseBody
  @RequestMapping(value = "/summary/setPaymentType.json", method = RequestMethod.POST)
  @RequireHardLogIn
  public CartData setPaymentType(@RequestParam(value = "paymentType") final String paymentType) {
    final B2BPaymentTypeData paymentTypeData = new B2BPaymentTypeData();
    paymentTypeData.setCode(paymentType);

    CartData cartData = new CartData();
    cartData.setPaymentType(paymentTypeData);

    cartData = getB2BCheckoutFacade().updateCheckoutCart(cartData);
    return cartData;
  }
  @RequestMapping(
      value = "/summary/getDeliveryAddressForm.json",
      method = {RequestMethod.GET, RequestMethod.POST})
  @RequireHardLogIn
  public String getDeliveryAddressForm(
      final Model model,
      @RequestParam(value = "addressId") final String addressId,
      @RequestParam(value = "createUpdateStatus") final String createUpdateStatus) {
    AddressData addressData = null;
    if (addressId != null && !addressId.isEmpty()) {
      addressData = getCheckoutFacade().getDeliveryAddressForCode(addressId);
    }

    final AddressForm addressForm = new AddressForm();

    final boolean hasAddressData = addressData != null;
    if (hasAddressData) {
      addressForm.setAddressId(addressData.getId());
      addressForm.setTitleCode(addressData.getTitleCode());
      addressForm.setFirstName(addressData.getFirstName());
      addressForm.setLastName(addressData.getLastName());
      addressForm.setLine1(addressData.getLine1());
      addressForm.setLine2(addressData.getLine2());
      addressForm.setTownCity(addressData.getTown());
      addressForm.setPostcode(addressData.getPostalCode());
      addressForm.setCountryIso(addressData.getCountry().getIsocode());
      addressForm.setShippingAddress(Boolean.valueOf(addressData.isShippingAddress()));
      addressForm.setBillingAddress(Boolean.valueOf(addressData.isBillingAddress()));
    }

    model.addAttribute("edit", Boolean.valueOf(hasAddressData));
    model.addAttribute("noAddresses", Boolean.valueOf(getUserFacade().isAddressBookEmpty()));

    model.addAttribute(addressForm);
    model.addAttribute("createUpdateStatus", createUpdateStatus);

    // Work out if the address form should be displayed based on the payment type
    final B2BPaymentTypeData paymentType = getCheckoutFacade().getCheckoutCart().getPaymentType();
    final boolean payOnAccount =
        paymentType != null && CheckoutPaymentType.ACCOUNT.getCode().equals(paymentType.getCode());
    model.addAttribute("showAddressForm", Boolean.valueOf(!payOnAccount));

    return ControllerConstants.Views.Fragments.SingleStepCheckout.DeliveryAddressFormPopup;
  }
  @RequestMapping(
      value = "/summary",
      method = {RequestMethod.GET, RequestMethod.POST})
  @RequireHardLogIn
  public String checkoutSummary(final Model model) throws CMSItemNotFoundException {

    if (!b2bUserGroupProvider.isCurrentUserAuthorizedToCheckOut()) {
      GlobalMessages.addErrorMessage(model, "checkout.error.invalid.accountType");
      return FORWARD_PREFIX + "/cart";
    }

    if (!hasItemsInCart()) {
      // no items in the cart
      return FORWARD_PREFIX + "/cart";
    }

    getCheckoutFacade().setDeliveryAddressIfAvailable();
    getCheckoutFacade().setDeliveryModeIfAvailable();
    getCheckoutFacade().setPaymentInfoIfAvailable();

    // Set to default payment type
    final B2BPaymentTypeData paymentTypeData = new B2BPaymentTypeData();
    paymentTypeData.setCode(CheckoutPaymentType.ACCOUNT.getCode());

    final CartData tempCartData = new CartData();
    tempCartData.setPaymentType(paymentTypeData);

    final CartData cartData = getB2BCheckoutFacade().updateCheckoutCart(tempCartData);

    // final CartData cartData = getCheckoutFacade().getCheckoutCart();
    if (cartData.getEntries() != null && !cartData.getEntries().isEmpty()) {
      for (final OrderEntryData entry : cartData.getEntries()) {
        final String productCode = entry.getProduct().getCode();
        final ProductData product =
            productFacade.getProductForCodeAndOptions(
                productCode,
                Arrays.asList(
                    ProductOption.BASIC,
                    ProductOption.PRICE,
                    ProductOption.PRICE_RANGE,
                    ProductOption.VARIANT_MATRIX));
        entry.setProduct(product);
      }
    }

    model.addAttribute("cartData", cartData);
    model.addAttribute("allItems", cartData.getEntries());
    model.addAttribute("deliveryAddress", cartData.getDeliveryAddress());
    model.addAttribute("deliveryMode", cartData.getDeliveryMode());
    model.addAttribute("paymentInfo", cartData.getPaymentInfo());
    model.addAttribute("costCenter", cartData.getCostCenter());
    model.addAttribute("quoteText", new B2BCommentData());
    // TODO:Make configuration hmc driven than hardcoding in controllers
    model.addAttribute("nDays", getNumberRange(1, 30));
    model.addAttribute("nthDayOfMonth", getNumberRange(1, 31));
    model.addAttribute("nthWeek", getNumberRange(1, 12));

    model.addAttribute(new AddressForm());
    model.addAttribute(new PaymentDetailsForm());
    if (!model.containsAttribute("placeOrderForm")) {
      final PlaceOrderForm placeOrderForm = new PlaceOrderForm();
      // TODO: Make setting of default recurrence enum value hmc driven rather hard coding in
      // controller
      placeOrderForm.setReplenishmentRecurrence(B2BReplenishmentRecurrenceEnum.MONTHLY);
      placeOrderForm.setnDays("14");
      final List<DayOfWeek> daysOfWeek = new ArrayList<DayOfWeek>();
      daysOfWeek.add(DayOfWeek.MONDAY);
      placeOrderForm.setnDaysOfWeek(daysOfWeek);
      model.addAttribute("placeOrderForm", placeOrderForm);
    }
    storeCmsPageInModel(model, getContentPageForLabelOrId(SINGLE_STEP_CHECKOUT_SUMMARY_CMS_PAGE));
    setUpMetaDataForContentPage(
        model, getContentPageForLabelOrId(SINGLE_STEP_CHECKOUT_SUMMARY_CMS_PAGE));
    model.addAttribute("metaRobots", "no-index,no-follow");
    return ControllerConstants.Views.Pages.SingleStepCheckout.CheckoutSummaryPage;
  }