Search in sources :

Example 1 with ReadableOrderProduct

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

the class ReadableOrderProductMapper method merge.

@Override
public ReadableOrderProduct merge(OrderProduct source, ReadableOrderProduct target, MerchantStore store, Language language) {
    Validate.notNull(source, "OrderProduct cannot be null");
    Validate.notNull(target, "ReadableOrderProduct cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(language, "Language cannot be null");
    target.setId(source.getId());
    target.setOrderedQuantity(source.getProductQuantity());
    try {
        target.setPrice(pricingService.getDisplayAmount(source.getOneTimeCharge(), store));
    } catch (Exception e) {
        throw new ConversionRuntimeException("Cannot convert price", e);
    }
    target.setProductName(source.getProductName());
    target.setSku(source.getSku());
    // subtotal = price * quantity
    BigDecimal subTotal = source.getOneTimeCharge();
    subTotal = subTotal.multiply(new BigDecimal(source.getProductQuantity()));
    try {
        String subTotalPrice = pricingService.getDisplayAmount(subTotal, store);
        target.setSubTotal(subTotalPrice);
    } catch (Exception e) {
        throw new ConversionRuntimeException("Cannot format price", e);
    }
    if (source.getOrderAttributes() != null) {
        List<ReadableOrderProductAttribute> attributes = new ArrayList<ReadableOrderProductAttribute>();
        for (OrderProductAttribute attr : source.getOrderAttributes()) {
            ReadableOrderProductAttribute readableAttribute = new ReadableOrderProductAttribute();
            try {
                String price = pricingService.getDisplayAmount(attr.getProductAttributePrice(), store);
                readableAttribute.setAttributePrice(price);
            } catch (ServiceException e) {
                throw new ConversionRuntimeException("Cannot format price", e);
            }
            readableAttribute.setAttributeName(attr.getProductAttributeName());
            readableAttribute.setAttributeValue(attr.getProductAttributeValueName());
            attributes.add(readableAttribute);
        }
        target.setAttributes(attributes);
    }
    String productSku = source.getSku();
    if (!StringUtils.isBlank(productSku)) {
        Product product = productService.getByCode(productSku, language);
        if (product != null) {
            // TODO autowired
            ReadableProductPopulator populator = new ReadableProductPopulator();
            populator.setPricingService(pricingService);
            populator.setimageUtils(imageUtils);
            ReadableProduct productProxy;
            try {
                productProxy = populator.populate(product, new ReadableProduct(), store, language);
                target.setProduct(productProxy);
            } catch (ConversionException e) {
                throw new ConversionRuntimeException("Cannot convert product", e);
            }
            Set<ProductImage> images = product.getImages();
            ProductImage defaultImage = null;
            if (images != null) {
                for (ProductImage image : images) {
                    if (defaultImage == null) {
                        defaultImage = image;
                    }
                    if (image.isDefaultImage()) {
                        defaultImage = image;
                    }
                }
            }
            if (defaultImage != null) {
                target.setImage(defaultImage.getProductImage());
            }
        }
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ReadableOrderProductAttribute(com.salesmanager.shop.model.order.ReadableOrderProductAttribute) ArrayList(java.util.ArrayList) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ServiceException(com.salesmanager.core.business.exception.ServiceException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) BigDecimal(java.math.BigDecimal) ServiceException(com.salesmanager.core.business.exception.ServiceException) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) ReadableOrderProductAttribute(com.salesmanager.shop.model.order.ReadableOrderProductAttribute) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException)

Example 2 with ReadableOrderProduct

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

the class ReadableOrderProductPopulator method populate.

@Override
public ReadableOrderProduct populate(OrderProduct source, ReadableOrderProduct target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "Requires ProductService");
    Validate.notNull(pricingService, "Requires PricingService");
    Validate.notNull(imageUtils, "Requires imageUtils");
    target.setId(source.getId());
    target.setOrderedQuantity(source.getProductQuantity());
    try {
        target.setPrice(pricingService.getDisplayAmount(source.getOneTimeCharge(), store));
    } catch (Exception e) {
        throw new ConversionException("Cannot convert price", e);
    }
    target.setProductName(source.getProductName());
    target.setSku(source.getSku());
    // subtotal = price * quantity
    BigDecimal subTotal = source.getOneTimeCharge();
    subTotal = subTotal.multiply(new BigDecimal(source.getProductQuantity()));
    try {
        String subTotalPrice = pricingService.getDisplayAmount(subTotal, store);
        target.setSubTotal(subTotalPrice);
    } catch (Exception e) {
        throw new ConversionException("Cannot format price", e);
    }
    if (source.getOrderAttributes() != null) {
        List<ReadableOrderProductAttribute> attributes = new ArrayList<ReadableOrderProductAttribute>();
        for (OrderProductAttribute attr : source.getOrderAttributes()) {
            ReadableOrderProductAttribute readableAttribute = new ReadableOrderProductAttribute();
            try {
                String price = pricingService.getDisplayAmount(attr.getProductAttributePrice(), store);
                readableAttribute.setAttributePrice(price);
            } catch (ServiceException e) {
                throw new ConversionException("Cannot format price", e);
            }
            readableAttribute.setAttributeName(attr.getProductAttributeName());
            readableAttribute.setAttributeValue(attr.getProductAttributeValueName());
            attributes.add(readableAttribute);
        }
        target.setAttributes(attributes);
    }
    String productSku = source.getSku();
    if (!StringUtils.isBlank(productSku)) {
        Product product = productService.getByCode(productSku, language);
        if (product != null) {
            ReadableProductPopulator populator = new ReadableProductPopulator();
            populator.setPricingService(pricingService);
            populator.setimageUtils(imageUtils);
            ReadableProduct productProxy = populator.populate(product, new ReadableProduct(), store, language);
            target.setProduct(productProxy);
            Set<ProductImage> images = product.getImages();
            ProductImage defaultImage = null;
            if (images != null) {
                for (ProductImage image : images) {
                    if (defaultImage == null) {
                        defaultImage = image;
                    }
                    if (image.isDefaultImage()) {
                        defaultImage = image;
                    }
                }
            }
            if (defaultImage != null) {
                target.setImage(defaultImage.getProductImage());
            }
        }
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ReadableOrderProductAttribute(com.salesmanager.shop.model.order.ReadableOrderProductAttribute) ArrayList(java.util.ArrayList) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) ServiceException(com.salesmanager.core.business.exception.ServiceException) ConversionException(com.salesmanager.core.business.exception.ConversionException) BigDecimal(java.math.BigDecimal) ServiceException(com.salesmanager.core.business.exception.ServiceException) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) ReadableOrderProductAttribute(com.salesmanager.shop.model.order.ReadableOrderProductAttribute)

Example 3 with ReadableOrderProduct

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

the class OrderFacadeImpl method setOrderProductList.

private void setOrderProductList(final Order order, final Locale locale, final MerchantStore store, final Language language, final com.salesmanager.shop.model.order.v0.ReadableOrder readableOrder) throws ConversionException {
    List<ReadableOrderProduct> orderProducts = new ArrayList<ReadableOrderProduct>();
    for (OrderProduct p : order.getOrderProducts()) {
        ReadableOrderProductPopulator orderProductPopulator = new ReadableOrderProductPopulator();
        orderProductPopulator.setLocale(locale);
        orderProductPopulator.setProductService(productService);
        orderProductPopulator.setPricingService(pricingService);
        orderProductPopulator.setimageUtils(imageUtils);
        ReadableOrderProduct orderProduct = new ReadableOrderProduct();
        orderProductPopulator.populate(p, orderProduct, store, language);
        // image
        // attributes
        orderProducts.add(orderProduct);
    }
    readableOrder.setProducts(orderProducts);
}
Also used : OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) ReadableOrderProductPopulator(com.salesmanager.shop.populator.order.ReadableOrderProductPopulator) ArrayList(java.util.ArrayList) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct)

Example 4 with ReadableOrderProduct

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

the class OrderFacadeImpl method getReadableOrder.

@Override
public com.salesmanager.shop.model.order.v0.ReadableOrder getReadableOrder(Long orderId, MerchantStore store, Language language) {
    Validate.notNull(store, "MerchantStore cannot be null");
    Order modelOrder = orderService.getOrder(orderId, store);
    if (modelOrder == null) {
        throw new ResourceNotFoundException("Order not found with id " + orderId);
    }
    com.salesmanager.shop.model.order.v0.ReadableOrder readableOrder = new com.salesmanager.shop.model.order.v0.ReadableOrder();
    Long customerId = modelOrder.getCustomerId();
    if (customerId != null) {
        ReadableCustomer readableCustomer = customerFacade.getCustomerById(customerId, store, language);
        if (readableCustomer == null) {
            LOGGER.warn("Customer id " + customerId + " not found in order " + orderId);
        } else {
            readableOrder.setCustomer(readableCustomer);
        }
    }
    try {
        readableOrderPopulator.populate(modelOrder, readableOrder, store, language);
        // order products
        List<ReadableOrderProduct> orderProducts = new ArrayList<ReadableOrderProduct>();
        for (OrderProduct p : modelOrder.getOrderProducts()) {
            ReadableOrderProductPopulator orderProductPopulator = new ReadableOrderProductPopulator();
            orderProductPopulator.setProductService(productService);
            orderProductPopulator.setPricingService(pricingService);
            orderProductPopulator.setimageUtils(imageUtils);
            ReadableOrderProduct orderProduct = new ReadableOrderProduct();
            orderProductPopulator.populate(p, orderProduct, store, language);
            orderProducts.add(orderProduct);
        }
        readableOrder.setProducts(orderProducts);
    } catch (Exception e) {
        throw new ServiceRuntimeException("Error while getting order [" + orderId + "]");
    }
    return readableOrder;
}
Also used : ShopOrder(com.salesmanager.shop.model.order.ShopOrder) Order(com.salesmanager.core.model.order.Order) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) ArrayList(java.util.ArrayList) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) 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) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ReadableOrderProductPopulator(com.salesmanager.shop.populator.order.ReadableOrderProductPopulator) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException)

Example 5 with ReadableOrderProduct

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

the class OrderFacadeImpl method orderConfirmation.

@Override
public ReadableOrderConfirmation orderConfirmation(Order order, Customer customer, MerchantStore store, Language language) {
    Validate.notNull(order, "Order cannot be null");
    Validate.notNull(customer, "Customer cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    ReadableOrderConfirmation orderConfirmation = new ReadableOrderConfirmation();
    ReadableCustomer readableCustomer = readableCustomerMapper.convert(customer, store, language);
    orderConfirmation.setBilling(readableCustomer.getBilling());
    orderConfirmation.setDelivery(readableCustomer.getDelivery());
    ReadableTotal readableTotal = new ReadableTotal();
    Set<OrderTotal> totals = order.getOrderTotal();
    List<ReadableOrderTotal> readableTotals = totals.stream().sorted(Comparator.comparingInt(OrderTotal::getSortOrder)).map(tot -> convertOrderTotal(tot, store, language)).collect(Collectors.toList());
    readableTotal.setTotals(readableTotals);
    Optional<ReadableOrderTotal> grandTotal = readableTotals.stream().filter(tot -> tot.getCode().equals("order.total.total")).findFirst();
    if (grandTotal.isPresent()) {
        readableTotal.setGrandTotal(grandTotal.get().getText());
    }
    orderConfirmation.setTotal(readableTotal);
    List<ReadableOrderProduct> products = order.getOrderProducts().stream().map(pr -> convertOrderProduct(pr, store, language)).collect(Collectors.toList());
    orderConfirmation.setProducts(products);
    if (!StringUtils.isBlank(order.getShippingModuleCode())) {
        StringBuilder optionCodeBuilder = new StringBuilder();
        try {
            optionCodeBuilder.append("module.shipping.").append(order.getShippingModuleCode());
            String shippingName = messages.getMessage(optionCodeBuilder.toString(), new String[] { store.getStorename() }, languageService.toLocale(language, store));
            orderConfirmation.setShipping(shippingName);
        } catch (Exception e) {
            // label not found
            LOGGER.warn("No shipping code found for " + optionCodeBuilder.toString());
        }
    }
    if (order.getPaymentType() != null) {
        orderConfirmation.setPayment(order.getPaymentType().name());
    }
    /**
     * Confirmation may be formatted
     */
    orderConfirmation.setId(order.getId());
    return orderConfirmation;
}
Also used : ReadableOrderTotalMapper(com.salesmanager.shop.mapper.order.ReadableOrderTotalMapper) Order(com.salesmanager.core.model.order.Order) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) ReadableCustomerMapper(com.salesmanager.shop.mapper.customer.ReadableCustomerMapper) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) LanguageService(com.salesmanager.core.business.services.reference.language.LanguageService) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) LabelUtils(com.salesmanager.shop.utils.LabelUtils) Service(org.springframework.stereotype.Service) Logger(org.slf4j.Logger) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) Customer(com.salesmanager.core.model.customer.Customer) ReadableOrderConfirmation(com.salesmanager.shop.model.order.v1.ReadableOrderConfirmation) Set(java.util.Set) Collectors(java.util.stream.Collectors) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) List(java.util.List) Validate(org.apache.commons.lang3.Validate) OrderFacade(com.salesmanager.shop.store.controller.order.facade.v1.OrderFacade) Optional(java.util.Optional) Comparator(java.util.Comparator) ReadableOrderProductMapper(com.salesmanager.shop.mapper.order.ReadableOrderProductMapper) ReadableTotal(com.salesmanager.shop.model.order.total.ReadableTotal) OrderTotal(com.salesmanager.core.model.order.OrderTotal) ReadableTotal(com.salesmanager.shop.model.order.total.ReadableTotal) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) ReadableOrderConfirmation(com.salesmanager.shop.model.order.v1.ReadableOrderConfirmation) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) OrderTotal(com.salesmanager.core.model.order.OrderTotal)

Aggregations

OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)5 ReadableOrderProduct (com.salesmanager.shop.model.order.ReadableOrderProduct)5 ArrayList (java.util.ArrayList)4 ConversionException (com.salesmanager.core.business.exception.ConversionException)3 ServiceException (com.salesmanager.core.business.exception.ServiceException)3 Product (com.salesmanager.core.model.catalog.product.Product)2 ProductImage (com.salesmanager.core.model.catalog.product.image.ProductImage)2 Order (com.salesmanager.core.model.order.Order)2 OrderProductAttribute (com.salesmanager.core.model.order.orderproduct.OrderProductAttribute)2 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)2 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)2 PersistableOrderProduct (com.salesmanager.shop.model.order.PersistableOrderProduct)2 ReadableOrderProductAttribute (com.salesmanager.shop.model.order.ReadableOrderProductAttribute)2 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)2 ReadableOrderProductPopulator (com.salesmanager.shop.populator.order.ReadableOrderProductPopulator)2 BigDecimal (java.math.BigDecimal)2 LanguageService (com.salesmanager.core.business.services.reference.language.LanguageService)1 Customer (com.salesmanager.core.model.customer.Customer)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 OrderTotal (com.salesmanager.core.model.order.OrderTotal)1