Search in sources :

Example 1 with ReadableOrderTotalSummary

use of com.salesmanager.shop.model.order.ReadableOrderTotalSummary in project shopizer by shopizer-ecommerce.

the class ReadableOrderSummaryPopulator method populate.

@Override
public ReadableOrderTotalSummary populate(OrderTotalSummary source, ReadableOrderTotalSummary target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(pricingService, "PricingService must be set");
    Validate.notNull(messages, "LabelUtils must be set");
    if (target == null) {
        target = new ReadableOrderTotalSummary();
    }
    try {
        if (source.getSubTotal() != null) {
            target.setSubTotal(pricingService.getDisplayAmount(source.getSubTotal(), store));
        }
        if (source.getTaxTotal() != null) {
            target.setTaxTotal(pricingService.getDisplayAmount(source.getTaxTotal(), store));
        }
        if (source.getTotal() != null) {
            target.setTotal(pricingService.getDisplayAmount(source.getTotal(), store));
        }
        if (!CollectionUtils.isEmpty(source.getTotals())) {
            ReadableOrderTotalPopulator orderTotalPopulator = new ReadableOrderTotalPopulator();
            orderTotalPopulator.setMessages(messages);
            orderTotalPopulator.setPricingService(pricingService);
            for (OrderTotal orderTotal : source.getTotals()) {
                ReadableOrderTotal t = new ReadableOrderTotal();
                orderTotalPopulator.populate(orderTotal, t, store, language);
                target.getTotals().add(t);
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error during amount formatting " + e.getMessage());
        throw new ConversionException(e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) OrderTotal(com.salesmanager.core.model.order.OrderTotal) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) ConversionException(com.salesmanager.core.business.exception.ConversionException)

Example 2 with ReadableOrderTotalSummary

use of com.salesmanager.shop.model.order.ReadableOrderTotalSummary 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 3 with ReadableOrderTotalSummary

use of com.salesmanager.shop.model.order.ReadableOrderTotalSummary 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

ReadableOrderTotalSummary (com.salesmanager.shop.model.order.ReadableOrderTotalSummary)3 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 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 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 Customer (com.salesmanager.core.model.customer.Customer)1 OrderTotal (com.salesmanager.core.model.order.OrderTotal)1 ReadableOrderTotal (com.salesmanager.shop.model.order.total.ReadableOrderTotal)1 Principal (java.security.Principal)1