Search in sources :

Example 6 with ShippingSummary

use of com.salesmanager.core.model.shipping.ShippingSummary in project shopizer by shopizer-ecommerce.

the class OrderShippingApi method shipping.

/**
 * Get shipping quote based on postal code
 * @param code
 * @param address
 * @param merchantStore
 * @param language
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/cart/{code}/shipping" }, method = RequestMethod.POST)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableShippingSummary shipping(@PathVariable final String code, @RequestBody AddressLocation address, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        Locale locale = request.getLocale();
        ShoppingCart cart = shoppingCartFacade.getShoppingCartModel(code, merchantStore);
        if (cart == null) {
            response.sendError(404, "Cart id " + code + " does not exist");
        }
        Delivery addr = new Delivery();
        addr.setPostalCode(address.getPostalCode());
        Country c = countryService.getByCode(address.getCountryCode());
        if (c == null) {
            c = merchantStore.getCountry();
        }
        addr.setCountry(c);
        Customer temp = new Customer();
        temp.setAnonymous(true);
        temp.setDelivery(addr);
        ShippingQuote quote = orderFacade.getShippingQuote(temp, cart, merchantStore, language);
        ShippingSummary summary = orderFacade.getShippingSummary(quote, merchantStore, language);
        ReadableShippingSummary shippingSummary = new ReadableShippingSummary();
        ReadableShippingSummaryPopulator populator = new ReadableShippingSummaryPopulator();
        populator.setPricingService(pricingService);
        populator.populate(summary, shippingSummary, merchantStore, language);
        List<ShippingOption> options = quote.getShippingOptions();
        if (!CollectionUtils.isEmpty(options)) {
            for (ShippingOption shipOption : options) {
                StringBuilder moduleName = new StringBuilder();
                moduleName.append("module.shipping.").append(shipOption.getShippingModuleCode());
                String carrier = messages.getMessage(moduleName.toString(), new String[] { merchantStore.getStorename() }, locale);
                String note = messages.getMessage(moduleName.append(".note").toString(), locale, "");
                shipOption.setDescription(carrier);
                shipOption.setNote(note);
                // option name
                if (!StringUtils.isBlank(shipOption.getOptionCode())) {
                    // try to get the translate
                    StringBuilder optionCodeBuilder = new StringBuilder();
                    try {
                        optionCodeBuilder.append("module.shipping.").append(shipOption.getShippingModuleCode());
                        String optionName = messages.getMessage(optionCodeBuilder.toString(), new String[] { merchantStore.getStorename() }, locale);
                        shipOption.setOptionName(optionName);
                    } catch (Exception e) {
                        // label not found
                        LOGGER.warn("No shipping code found for " + optionCodeBuilder.toString());
                    }
                }
            }
            shippingSummary.setShippingOptions(options);
        }
        return shippingSummary;
    } catch (Exception e) {
        LOGGER.error("Error while getting shipping quote", e);
        try {
            response.sendError(503, "Error while getting shipping quote" + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : Locale(java.util.Locale) Customer(com.salesmanager.core.model.customer.Customer) ReadableShippingSummaryPopulator(com.salesmanager.shop.populator.order.ReadableShippingSummaryPopulator) ShippingOption(com.salesmanager.core.model.shipping.ShippingOption) ShippingQuote(com.salesmanager.core.model.shipping.ShippingQuote) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) ReadableShippingSummary(com.salesmanager.shop.model.order.shipping.ReadableShippingSummary) Country(com.salesmanager.core.model.reference.country.Country) ReadableShippingSummary(com.salesmanager.shop.model.order.shipping.ReadableShippingSummary) Delivery(com.salesmanager.core.model.common.Delivery) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with ShippingSummary

use of com.salesmanager.core.model.shipping.ShippingSummary 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 8 with ShippingSummary

use of com.salesmanager.core.model.shipping.ShippingSummary in project shopizer by shopizer-ecommerce.

the class OrderFacadeImpl method processOrder.

/**
 * Process order from api
 */
@Override
public Order processOrder(com.salesmanager.shop.model.order.v1.PersistableOrder order, Customer customer, MerchantStore store, Language language, Locale locale) throws ServiceException {
    Validate.notNull(order, "Order cannot be null");
    Validate.notNull(customer, "Customer cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(language, "Language cannot be null");
    Validate.notNull(locale, "Locale cannot be null");
    try {
        Order modelOrder = new Order();
        persistableOrderApiPopulator.populate(order, modelOrder, store, language);
        Long shoppingCartId = order.getShoppingCartId();
        ShoppingCart cart = shoppingCartService.getById(shoppingCartId, store);
        if (cart == null) {
            throw new ServiceException("Shopping cart with id " + shoppingCartId + " does not exist");
        }
        Set<ShoppingCartItem> shoppingCartItems = cart.getLineItems();
        List<ShoppingCartItem> items = new ArrayList<ShoppingCartItem>(shoppingCartItems);
        Set<OrderProduct> orderProducts = new LinkedHashSet<OrderProduct>();
        OrderProductPopulator orderProductPopulator = new OrderProductPopulator();
        orderProductPopulator.setDigitalProductService(digitalProductService);
        orderProductPopulator.setProductAttributeService(productAttributeService);
        orderProductPopulator.setProductService(productService);
        for (ShoppingCartItem item : shoppingCartItems) {
            OrderProduct orderProduct = new OrderProduct();
            orderProduct = orderProductPopulator.populate(item, orderProduct, store, language);
            orderProduct.setOrder(modelOrder);
            orderProducts.add(orderProduct);
        }
        modelOrder.setOrderProducts(orderProducts);
        if (order.getAttributes() != null && order.getAttributes().size() > 0) {
            Set<OrderAttribute> attrs = new HashSet<OrderAttribute>();
            for (com.salesmanager.shop.model.order.OrderAttribute attribute : order.getAttributes()) {
                OrderAttribute attr = new OrderAttribute();
                attr.setKey(attribute.getKey());
                attr.setValue(attribute.getValue());
                attr.setOrder(modelOrder);
                attrs.add(attr);
            }
            modelOrder.setOrderAttributes(attrs);
        }
        // requires Shipping information (need a quote id calculated)
        ShippingSummary shippingSummary = null;
        // get shipping quote if asked for
        if (order.getShippingQuote() != null && order.getShippingQuote().longValue() > 0) {
            shippingSummary = shippingQuoteService.getShippingSummary(order.getShippingQuote(), store);
            if (shippingSummary != null) {
                modelOrder.setShippingModuleCode(shippingSummary.getShippingModule());
            }
        }
        // requires Order Totals, this needs recalculation and then compare
        // total with the amount sent as part
        // of process order request. If totals does not match, an error
        // should be thrown.
        OrderTotalSummary orderTotalSummary = null;
        OrderSummary orderSummary = new OrderSummary();
        orderSummary.setShippingSummary(shippingSummary);
        List<ShoppingCartItem> itemsSet = new ArrayList<ShoppingCartItem>(cart.getLineItems());
        orderSummary.setProducts(itemsSet);
        orderTotalSummary = orderService.caculateOrderTotal(orderSummary, customer, store, language);
        if (order.getPayment().getAmount() == null) {
            throw new ConversionException("Requires Payment.amount");
        }
        String submitedAmount = order.getPayment().getAmount();
        BigDecimal calculatedAmount = orderTotalSummary.getTotal();
        String strCalculatedTotal = calculatedAmount.toPlainString();
        // compare both prices
        if (!submitedAmount.equals(strCalculatedTotal)) {
            throw new ConversionException("Payment.amount does not match what the system has calculated " + strCalculatedTotal + " (received " + submitedAmount + ") please recalculate the order and submit again");
        }
        modelOrder.setTotal(calculatedAmount);
        List<com.salesmanager.core.model.order.OrderTotal> totals = orderTotalSummary.getTotals();
        Set<com.salesmanager.core.model.order.OrderTotal> set = new HashSet<com.salesmanager.core.model.order.OrderTotal>();
        if (!CollectionUtils.isEmpty(totals)) {
            for (com.salesmanager.core.model.order.OrderTotal total : totals) {
                total.setOrder(modelOrder);
                set.add(total);
            }
        }
        modelOrder.setOrderTotal(set);
        PersistablePaymentPopulator paymentPopulator = new PersistablePaymentPopulator();
        paymentPopulator.setPricingService(pricingService);
        Payment paymentModel = new Payment();
        paymentPopulator.populate(order.getPayment(), paymentModel, store, language);
        modelOrder.setShoppingCartCode(cart.getShoppingCartCode());
        /**
         */
        if (!StringUtils.isBlank(customer.getNick()) && !customer.isAnonymous()) {
            if (order.getCustomerId() == null && (customerFacade.checkIfUserExists(customer.getNick(), store))) {
                customer.setAnonymous(true);
                customer.setNick(null);
            // send email instructions
            }
        }
        // order service
        modelOrder = orderService.processOrder(modelOrder, customer, items, orderTotalSummary, paymentModel, store);
        // update cart
        try {
            cart.setOrderId(modelOrder.getId());
            shoppingCartFacade.saveOrUpdateShoppingCart(cart);
        } catch (Exception e) {
            LOGGER.error("Cannot delete cart " + cart.getId(), e);
        }
        // email management
        if ("true".equals(coreConfiguration.getProperty("ORDER_EMAIL_API"))) {
            // send email
            try {
                notify(modelOrder, customer, store, language, locale);
            } catch (Exception e) {
                LOGGER.error("Cannot send order confirmation email", e);
            }
        }
        return modelOrder;
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) OrderProductPopulator(com.salesmanager.shop.populator.order.OrderProductPopulator) ReadableOrderProductPopulator(com.salesmanager.shop.populator.order.ReadableOrderProductPopulator) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) OrderSummary(com.salesmanager.core.model.order.OrderSummary) ArrayList(java.util.ArrayList) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) OrderAttribute(com.salesmanager.core.model.order.attributes.OrderAttribute) PersistablePaymentPopulator(com.salesmanager.shop.populator.order.transaction.PersistablePaymentPopulator) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) ShopOrder(com.salesmanager.shop.model.order.ShopOrder) Order(com.salesmanager.core.model.order.Order) ConversionException(com.salesmanager.core.business.exception.ConversionException) BigDecimal(java.math.BigDecimal) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ConversionException(com.salesmanager.core.business.exception.ConversionException) CreditCardPayment(com.salesmanager.core.model.payments.CreditCardPayment) Payment(com.salesmanager.core.model.payments.Payment) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal)

Example 9 with ShippingSummary

use of com.salesmanager.core.model.shipping.ShippingSummary in project shopizer by shopizer-ecommerce.

the class OrderFacadeImpl method getShippingSummary.

/**
 * ShippingSummary contains the subset of information of a ShippingQuote
 */
@Override
public ShippingSummary getShippingSummary(ShippingQuote quote, MerchantStore store, Language language) {
    ShippingSummary summary = new ShippingSummary();
    if (quote.getSelectedShippingOption() != null) {
        summary.setShippingQuote(true);
        summary.setFreeShipping(quote.isFreeShipping());
        summary.setTaxOnShipping(quote.isApplyTaxOnShipping());
        summary.setHandling(quote.getHandlingFees());
        summary.setShipping(quote.getSelectedShippingOption().getOptionPrice());
        summary.setShippingOption(quote.getSelectedShippingOption().getOptionName());
        summary.setShippingModule(quote.getShippingModuleCode());
        summary.setShippingOptionCode(quote.getSelectedShippingOption().getOptionCode());
        if (quote.getDeliveryAddress() != null) {
            summary.setDeliveryAddress(quote.getDeliveryAddress());
        }
    }
    return summary;
}
Also used : ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary)

Example 10 with ShippingSummary

use of com.salesmanager.core.model.shipping.ShippingSummary in project shopizer by shopizer-ecommerce.

the class ShippingServiceImpl method getShippingSummary.

@Override
public ShippingSummary getShippingSummary(MerchantStore store, ShippingQuote shippingQuote, ShippingOption selectedShippingOption) throws ServiceException {
    ShippingSummary shippingSummary = new ShippingSummary();
    shippingSummary.setFreeShipping(shippingQuote.isFreeShipping());
    shippingSummary.setHandling(shippingQuote.getHandlingFees());
    shippingSummary.setShipping(selectedShippingOption.getOptionPrice());
    shippingSummary.setShippingModule(shippingQuote.getShippingModuleCode());
    shippingSummary.setShippingOption(selectedShippingOption.getDescription());
    return shippingSummary;
}
Also used : ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary)

Aggregations

ShippingSummary (com.salesmanager.core.model.shipping.ShippingSummary)14 ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)8 ArrayList (java.util.ArrayList)8 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 ShippingOption (com.salesmanager.core.model.shipping.ShippingOption)6 ReadableShippingSummary (com.salesmanager.shop.model.order.shipping.ReadableShippingSummary)6 ServiceException (com.salesmanager.core.business.exception.ServiceException)5 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)5 Language (com.salesmanager.core.model.reference.language.Language)5 ShippingQuote (com.salesmanager.core.model.shipping.ShippingQuote)5 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)5 ReadableShippingSummaryPopulator (com.salesmanager.shop.populator.order.ReadableShippingSummaryPopulator)5 Customer (com.salesmanager.core.model.customer.Customer)4 Country (com.salesmanager.core.model.reference.country.Country)4 ReadableDelivery (com.salesmanager.shop.model.customer.ReadableDelivery)4 ReadableShopOrder (com.salesmanager.shop.model.order.ReadableShopOrder)4 OrderSummary (com.salesmanager.core.model.order.OrderSummary)3 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)3