Search in sources :

Example 1 with ReadableOrderSummaryPopulator

use of com.salesmanager.shop.populator.order.ReadableOrderSummaryPopulator in project shopizer by shopizer-ecommerce.

the class OrderTotalApi method calculateTotal.

/**
 * Public api
 * @param id
 * @param quote
 * @param merchantStore
 * @param language
 * @param response
 * @return
 */
@RequestMapping(value = { "/cart/{code}/total" }, method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableOrderTotalSummary calculateTotal(@PathVariable final String code, @RequestParam(value = "quote", required = false) Long quote, @ApiIgnore MerchantStore merchantStore, // possible postal code, province and country
@ApiIgnore Language language, HttpServletResponse response) {
    try {
        ShoppingCart shoppingCart = shoppingCartFacade.getShoppingCartModel(code, merchantStore);
        if (shoppingCart == null) {
            response.sendError(404, "Cart code " + code + " does not exist");
            return null;
        }
        ShippingSummary shippingSummary = null;
        // get shipping quote if asked for
        if (quote != null) {
            shippingSummary = shippingQuoteService.getShippingSummary(quote, merchantStore);
        }
        OrderTotalSummary orderTotalSummary = null;
        OrderSummary orderSummary = new OrderSummary();
        orderSummary.setShippingSummary(shippingSummary);
        List<ShoppingCartItem> itemsSet = new ArrayList<ShoppingCartItem>(shoppingCart.getLineItems());
        orderSummary.setProducts(itemsSet);
        orderTotalSummary = orderService.caculateOrderTotal(orderSummary, merchantStore, language);
        ReadableOrderTotalSummary returnSummary = new ReadableOrderTotalSummary();
        ReadableOrderSummaryPopulator populator = new ReadableOrderSummaryPopulator();
        populator.setMessages(messages);
        populator.setPricingService(pricingService);
        populator.populate(orderTotalSummary, returnSummary, merchantStore, language);
        return returnSummary;
    } catch (Exception e) {
        LOGGER.error("Error while calculating order summary", e);
        try {
            response.sendError(503, "Error while calculating order summary " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : ReadableOrderSummaryPopulator(com.salesmanager.shop.populator.order.ReadableOrderSummaryPopulator) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) OrderSummary(com.salesmanager.core.model.order.OrderSummary) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) ArrayList(java.util.ArrayList) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ReadableOrderSummaryPopulator

use of com.salesmanager.shop.populator.order.ReadableOrderSummaryPopulator in project shopizer by shopizer-ecommerce.

the class OrderTotalApi method payment.

/**
 * This service calculates order total for a given shopping cart This method takes in
 * consideration any applicable sales tax An optional request parameter accepts a quote id that
 * was received using shipping api
 *
 * @param quote
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/auth/cart/{id}/total" }, method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableOrderTotalSummary payment(@PathVariable final Long id, @RequestParam(value = "quote", required = false) Long quote, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) {
    try {
        Principal principal = request.getUserPrincipal();
        String userName = principal.getName();
        Customer customer = customerService.getByNick(userName);
        if (customer == null) {
            response.sendError(503, "Error while getting user details to calculate shipping quote");
        }
        ShoppingCart shoppingCart = shoppingCartFacade.getShoppingCartModel(id, merchantStore);
        if (shoppingCart == null) {
            response.sendError(404, "Cart id " + id + " does not exist");
            return null;
        }
        if (shoppingCart.getCustomerId() == null) {
            response.sendError(404, "Cart id " + id + " does not exist for exist for user " + userName);
            return null;
        }
        if (shoppingCart.getCustomerId().longValue() != customer.getId().longValue()) {
            response.sendError(404, "Cart id " + id + " does not exist for exist for user " + userName);
            return null;
        }
        ShippingSummary shippingSummary = null;
        // get shipping quote if asked for
        if (quote != null) {
            shippingSummary = shippingQuoteService.getShippingSummary(quote, merchantStore);
        }
        OrderTotalSummary orderTotalSummary = null;
        OrderSummary orderSummary = new OrderSummary();
        orderSummary.setShippingSummary(shippingSummary);
        List<ShoppingCartItem> itemsSet = new ArrayList<ShoppingCartItem>(shoppingCart.getLineItems());
        orderSummary.setProducts(itemsSet);
        orderTotalSummary = orderService.caculateOrderTotal(orderSummary, customer, merchantStore, language);
        ReadableOrderTotalSummary returnSummary = new ReadableOrderTotalSummary();
        ReadableOrderSummaryPopulator populator = new ReadableOrderSummaryPopulator();
        populator.setMessages(messages);
        populator.setPricingService(pricingService);
        populator.populate(orderTotalSummary, returnSummary, merchantStore, language);
        return returnSummary;
    } catch (Exception e) {
        LOGGER.error("Error while calculating order summary", e);
        try {
            response.sendError(503, "Error while calculating order summary " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : ReadableOrderSummaryPopulator(com.salesmanager.shop.populator.order.ReadableOrderSummaryPopulator) Customer(com.salesmanager.core.model.customer.Customer) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) OrderSummary(com.salesmanager.core.model.order.OrderSummary) ArrayList(java.util.ArrayList) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) Principal(java.security.Principal) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

OrderSummary (com.salesmanager.core.model.order.OrderSummary)2 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)2 ShippingSummary (com.salesmanager.core.model.shipping.ShippingSummary)2 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)2 ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)2 ReadableOrderTotalSummary (com.salesmanager.shop.model.order.ReadableOrderTotalSummary)2 ReadableOrderSummaryPopulator (com.salesmanager.shop.populator.order.ReadableOrderSummaryPopulator)2 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)2 ArrayList (java.util.ArrayList)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 Customer (com.salesmanager.core.model.customer.Customer)1 Principal (java.security.Principal)1