Search in sources :

Example 1 with OrderTotal

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

the class PersistableOrderPopulator method populate.

@Override
public Order populate(PersistableOrder source, Order target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "productService must be set");
    Validate.notNull(digitalProductService, "digitalProductService must be set");
    Validate.notNull(productAttributeService, "productAttributeService must be set");
    Validate.notNull(customerService, "customerService must be set");
    Validate.notNull(countryService, "countryService must be set");
    Validate.notNull(zoneService, "zoneService must be set");
    Validate.notNull(currencyService, "currencyService must be set");
    try {
        Map<String, Country> countriesMap = countryService.getCountriesMap(language);
        Map<String, Zone> zonesMap = zoneService.getZones(language);
        /**
         * customer *
         */
        PersistableCustomer customer = source.getCustomer();
        if (customer != null) {
            if (customer.getId() != null && customer.getId() > 0) {
                Customer modelCustomer = customerService.getById(customer.getId());
                if (modelCustomer == null) {
                    throw new ConversionException("Customer id " + customer.getId() + " does not exists");
                }
                if (modelCustomer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Customer id " + customer.getId() + " does not exists for store " + store.getCode());
                }
                target.setCustomerId(modelCustomer.getId());
                target.setBilling(modelCustomer.getBilling());
                target.setDelivery(modelCustomer.getDelivery());
                target.setCustomerEmailAddress(source.getCustomer().getEmailAddress());
            }
        }
        target.setLocale(LocaleUtils.getLocale(store));
        CreditCard creditCard = source.getCreditCard();
        if (creditCard != null) {
            String maskedNumber = CreditCardUtils.maskCardNumber(creditCard.getCcNumber());
            creditCard.setCcNumber(maskedNumber);
            target.setCreditCard(creditCard);
        }
        Currency currency = null;
        try {
            currency = currencyService.getByCode(source.getCurrency());
        } catch (Exception e) {
            throw new ConversionException("Currency not found for code " + source.getCurrency());
        }
        if (currency == null) {
            throw new ConversionException("Currency not found for code " + source.getCurrency());
        }
        target.setCurrency(currency);
        target.setDatePurchased(source.getDatePurchased());
        // target.setCurrency(store.getCurrency());
        target.setCurrencyValue(new BigDecimal(0));
        target.setMerchant(store);
        target.setStatus(source.getOrderStatus());
        target.setPaymentModuleCode(source.getPaymentModule());
        target.setPaymentType(source.getPaymentType());
        target.setShippingModuleCode(source.getShippingModule());
        target.setCustomerAgreement(source.isCustomerAgreed());
        target.setConfirmedAddress(source.isConfirmedAddress());
        if (source.getPreviousOrderStatus() != null) {
            List<OrderStatus> orderStatusList = source.getPreviousOrderStatus();
            for (OrderStatus status : orderStatusList) {
                OrderStatusHistory statusHistory = new OrderStatusHistory();
                statusHistory.setStatus(status);
                statusHistory.setOrder(target);
                target.getOrderHistory().add(statusHistory);
            }
        }
        if (!StringUtils.isBlank(source.getComments())) {
            OrderStatusHistory statusHistory = new OrderStatusHistory();
            statusHistory.setStatus(null);
            statusHistory.setOrder(target);
            statusHistory.setComments(source.getComments());
            target.getOrderHistory().add(statusHistory);
        }
        List<PersistableOrderProduct> products = source.getOrderProductItems();
        if (CollectionUtils.isEmpty(products)) {
            throw new ConversionException("Requires at least 1 PersistableOrderProduct");
        }
        com.salesmanager.shop.populator.order.PersistableOrderProductPopulator orderProductPopulator = new PersistableOrderProductPopulator();
        orderProductPopulator.setProductAttributeService(productAttributeService);
        orderProductPopulator.setProductService(productService);
        orderProductPopulator.setDigitalProductService(digitalProductService);
        for (PersistableOrderProduct orderProduct : products) {
            OrderProduct modelOrderProduct = new OrderProduct();
            orderProductPopulator.populate(orderProduct, modelOrderProduct, store, language);
            target.getOrderProducts().add(modelOrderProduct);
        }
        List<OrderTotal> orderTotals = source.getTotals();
        if (CollectionUtils.isNotEmpty(orderTotals)) {
            for (OrderTotal total : orderTotals) {
                com.salesmanager.core.model.order.OrderTotal totalModel = new com.salesmanager.core.model.order.OrderTotal();
                totalModel.setModule(total.getModule());
                totalModel.setOrder(target);
                totalModel.setOrderTotalCode(total.getCode());
                totalModel.setTitle(total.getTitle());
                totalModel.setValue(total.getValue());
                target.getOrderTotal().add(totalModel);
            }
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) Currency(com.salesmanager.core.model.reference.currency.Currency) ConversionException(com.salesmanager.core.business.exception.ConversionException) Zone(com.salesmanager.core.model.reference.zone.Zone) CreditCard(com.salesmanager.core.model.order.payment.CreditCard) ConversionException(com.salesmanager.core.business.exception.ConversionException) BigDecimal(java.math.BigDecimal) Country(com.salesmanager.core.model.reference.country.Country) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal)

Example 2 with OrderTotal

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

the class OrderFacadeImpl method setOrderTotals.

private void setOrderTotals(OrderEntity order, OrderTotalSummary summary) {
    List<OrderTotal> totals = new ArrayList<OrderTotal>();
    List<com.salesmanager.core.model.order.OrderTotal> orderTotals = summary.getTotals();
    for (com.salesmanager.core.model.order.OrderTotal t : orderTotals) {
        OrderTotal total = new OrderTotal();
        total.setCode(t.getOrderTotalCode());
        total.setTitle(t.getTitle());
        total.setValue(t.getValue());
        totals.add(total);
    }
    order.setTotals(totals);
}
Also used : ArrayList(java.util.ArrayList) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal)

Example 3 with OrderTotal

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

the class ShoppingCartDataPopulator method populate.

@Override
public ShoppingCartData populate(final ShoppingCart shoppingCart, final ShoppingCartData cart, final MerchantStore store, final Language language) {
    Validate.notNull(shoppingCart, "Requires ShoppingCart");
    Validate.notNull(language, "Requires Language not null");
    int cartQuantity = 0;
    cart.setCode(shoppingCart.getShoppingCartCode());
    Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = shoppingCart.getLineItems();
    List<ShoppingCartItem> shoppingCartItemsList = Collections.emptyList();
    try {
        if (items != null) {
            shoppingCartItemsList = new ArrayList<ShoppingCartItem>();
            for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem item : items) {
                ShoppingCartItem shoppingCartItem = new ShoppingCartItem();
                shoppingCartItem.setCode(cart.getCode());
                shoppingCartItem.setProductCode(item.getProduct().getSku());
                shoppingCartItem.setProductVirtual(item.isProductVirtual());
                shoppingCartItem.setProductId(item.getProductId());
                shoppingCartItem.setId(item.getId());
                String itemName = item.getProduct().getProductDescription().getName();
                if (!CollectionUtils.isEmpty(item.getProduct().getDescriptions())) {
                    for (ProductDescription productDescription : item.getProduct().getDescriptions()) {
                        if (language != null && language.getId().intValue() == productDescription.getLanguage().getId().intValue()) {
                            itemName = productDescription.getName();
                            break;
                        }
                    }
                }
                shoppingCartItem.setName(itemName);
                shoppingCartItem.setPrice(pricingService.getDisplayAmount(item.getItemPrice(), store));
                shoppingCartItem.setQuantity(item.getQuantity());
                cartQuantity = cartQuantity + item.getQuantity();
                shoppingCartItem.setProductPrice(item.getItemPrice());
                shoppingCartItem.setSubTotal(pricingService.getDisplayAmount(item.getSubTotal(), store));
                ProductImage image = item.getProduct().getProductImage();
                if (image != null && imageUtils != null) {
                    String imagePath = imageUtils.buildProductImageUtils(store, item.getProduct().getSku(), image.getProductImage());
                    shoppingCartItem.setImage(imagePath);
                }
                Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = item.getAttributes();
                if (attributes != null) {
                    List<ShoppingCartAttribute> cartAttributes = new ArrayList<ShoppingCartAttribute>();
                    for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attribute : attributes) {
                        ShoppingCartAttribute cartAttribute = new ShoppingCartAttribute();
                        cartAttribute.setId(attribute.getId());
                        cartAttribute.setAttributeId(attribute.getProductAttributeId());
                        cartAttribute.setOptionId(attribute.getProductAttribute().getProductOption().getId());
                        cartAttribute.setOptionValueId(attribute.getProductAttribute().getProductOptionValue().getId());
                        List<ProductOptionDescription> optionDescriptions = attribute.getProductAttribute().getProductOption().getDescriptionsSettoList();
                        List<ProductOptionValueDescription> optionValueDescriptions = attribute.getProductAttribute().getProductOptionValue().getDescriptionsSettoList();
                        if (!CollectionUtils.isEmpty(optionDescriptions) && !CollectionUtils.isEmpty(optionValueDescriptions)) {
                            String optionName = optionDescriptions.get(0).getName();
                            String optionValue = optionValueDescriptions.get(0).getName();
                            for (ProductOptionDescription optionDescription : optionDescriptions) {
                                if (optionDescription.getLanguage() != null && optionDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
                                    optionName = optionDescription.getName();
                                    break;
                                }
                            }
                            for (ProductOptionValueDescription optionValueDescription : optionValueDescriptions) {
                                if (optionValueDescription.getLanguage() != null && optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
                                    optionValue = optionValueDescription.getName();
                                    break;
                                }
                            }
                            cartAttribute.setOptionName(optionName);
                            cartAttribute.setOptionValue(optionValue);
                            cartAttributes.add(cartAttribute);
                        }
                    }
                    shoppingCartItem.setShoppingCartAttributes(cartAttributes);
                }
                shoppingCartItemsList.add(shoppingCartItem);
            }
        }
        if (CollectionUtils.isNotEmpty(shoppingCartItemsList)) {
            cart.setShoppingCartItems(shoppingCartItemsList);
        }
        if (shoppingCart.getOrderId() != null) {
            cart.setOrderId(shoppingCart.getOrderId());
        }
        OrderSummary summary = new OrderSummary();
        List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> productsList = new ArrayList<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
        productsList.addAll(shoppingCart.getLineItems());
        summary.setProducts(productsList.stream().filter(p -> p.getProduct().isAvailable()).collect(Collectors.toList()));
        OrderTotalSummary orderSummary = shoppingCartCalculationService.calculate(shoppingCart, store, language);
        if (CollectionUtils.isNotEmpty(orderSummary.getTotals())) {
            List<OrderTotal> totals = new ArrayList<OrderTotal>();
            for (com.salesmanager.core.model.order.OrderTotal t : orderSummary.getTotals()) {
                OrderTotal total = new OrderTotal();
                total.setCode(t.getOrderTotalCode());
                total.setText(t.getText());
                total.setValue(t.getValue());
                totals.add(total);
            }
            cart.setTotals(totals);
        }
        cart.setSubTotal(pricingService.getDisplayAmount(orderSummary.getSubTotal(), store));
        cart.setTotal(pricingService.getDisplayAmount(orderSummary.getTotal(), store));
        cart.setQuantity(cartQuantity);
        cart.setId(shoppingCart.getId());
    } catch (ServiceException ex) {
        LOG.error("Error while converting cart Model to cart Data.." + ex);
        throw new ConversionException("Unable to create cart data", ex);
    }
    return cart;
}
Also used : ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) OrderSummary(com.salesmanager.core.model.order.OrderSummary) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) ArrayList(java.util.ArrayList) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ConversionException(org.apache.commons.beanutils.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)

Aggregations

OrderTotal (com.salesmanager.shop.model.order.total.OrderTotal)3 ArrayList (java.util.ArrayList)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 ProductOptionDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription)1 ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)1 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)1 ProductImage (com.salesmanager.core.model.catalog.product.image.ProductImage)1 Customer (com.salesmanager.core.model.customer.Customer)1 OrderSummary (com.salesmanager.core.model.order.OrderSummary)1 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)1 OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)1 OrderStatus (com.salesmanager.core.model.order.orderstatus.OrderStatus)1 OrderStatusHistory (com.salesmanager.core.model.order.orderstatus.OrderStatusHistory)1 CreditCard (com.salesmanager.core.model.order.payment.CreditCard)1 Country (com.salesmanager.core.model.reference.country.Country)1 Currency (com.salesmanager.core.model.reference.currency.Currency)1 Zone (com.salesmanager.core.model.reference.zone.Zone)1 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)1 PersistableOrderProduct (com.salesmanager.shop.model.order.PersistableOrderProduct)1