Search in sources :

Example 41 with Product

use of com.salesmanager.core.model.catalog.product.Product 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 42 with Product

use of com.salesmanager.core.model.catalog.product.Product 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 43 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ShoppingCartItemPopulator method populate.

@Override
public ShoppingCartItem populate(PersistableOrderProduct source, /**
 * TODO: Fix, target not used possible future bug ! *
 */
ShoppingCartItem target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "Requires to set productService");
    Validate.notNull(productAttributeService, "Requires to set productAttributeService");
    Validate.notNull(shoppingCartService, "Requires to set shoppingCartService");
    Product product = productService.getById(source.getProduct().getId());
    if (source.getAttributes() != null) {
        for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attr : source.getAttributes()) {
            ProductAttribute attribute = productAttributeService.getById(attr.getId());
            if (attribute == null) {
                throw new ConversionException("ProductAttribute with id " + attr.getId() + " is null");
            }
            if (attribute.getProduct().getId().longValue() != source.getProduct().getId().longValue()) {
                throw new ConversionException("ProductAttribute with id " + attr.getId() + " is not assigned to Product id " + source.getProduct().getId());
            }
            product.getAttributes().add(attribute);
        }
    }
    try {
        return shoppingCartService.populateShoppingCartItem(product);
    } catch (ServiceException e) {
        throw new ConversionException(e);
    }
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) Product(com.salesmanager.core.model.catalog.product.Product) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)

Example 44 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method createCartItem.

// used for api
private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(ShoppingCart cartModel, PersistableShoppingCartItem shoppingCartItem, MerchantStore store) throws Exception {
    Product product = productService.getById(shoppingCartItem.getProduct());
    if (product == null) {
        throw new ResourceNotFoundException("Item with id " + shoppingCartItem.getProduct() + " does not exist");
    }
    if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
        throw new ResourceNotFoundException("Item with id " + shoppingCartItem.getProduct() + " does not belong to merchant " + store.getId());
    }
    /**
     * Check if product quantity is 0
     * Check if product is available
     * Check if date available <= now
     */
    Set<ProductAvailability> availabilities = product.getAvailabilities();
    if (availabilities == null) {
        throw new Exception("Item with id " + product.getId() + " is not properly configured");
    }
    for (ProductAvailability availability : availabilities) {
        if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
            throw new Exception("Item with id " + product.getId() + " is not available");
        }
    }
    if (!product.isAvailable()) {
        throw new Exception("Item with id " + product.getId() + " is not available");
    }
    if (!DateUtil.dateBeforeEqualsDate(product.getDateAvailable(), new Date())) {
        throw new Exception("Item with id " + product.getId() + " is not available");
    }
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(product);
    item.setQuantity(shoppingCartItem.getQuantity());
    item.setShoppingCart(cartModel);
    // set attributes
    List<com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute> attributes = shoppingCartItem.getAttributes();
    if (!CollectionUtils.isEmpty(attributes)) {
        for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attribute : attributes) {
            ProductAttribute productAttribute = productAttributeService.getById(attribute.getId());
            if (productAttribute != null && productAttribute.getProduct().getId().longValue() == product.getId().longValue()) {
                com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
                item.addAttributes(attributeItem);
            }
        }
    }
    return item;
}
Also used : Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) NoResultException(javax.persistence.NoResultException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) LocalDate(java.time.LocalDate) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException)

Example 45 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method createCartItem.

private com.salesmanager.core.model.shoppingcart.ShoppingCartItem createCartItem(final ShoppingCart cartModel, final ShoppingCartItem shoppingCartItem, final MerchantStore store) throws Exception {
    Product product = productService.getById(shoppingCartItem.getProductId());
    if (product == null) {
        throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not exist");
    }
    if (product.getMerchantStore().getId().intValue() != store.getId().intValue()) {
        throw new Exception("Item with id " + shoppingCartItem.getProductId() + " does not belong to merchant " + store.getId());
    }
    /**
     * Check if product quantity is 0
     * Check if product is available
     * Check if date available <= now
     */
    Set<ProductAvailability> availabilities = product.getAvailabilities();
    if (availabilities == null) {
        throw new Exception("Item with id " + product.getId() + " is not properly configured");
    }
    for (ProductAvailability availability : availabilities) {
        if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
            throw new Exception("Item with id " + product.getId() + " is not available");
        }
    }
    if (!product.isAvailable()) {
        throw new Exception("Item with id " + product.getId() + " is not available");
    }
    if (!DateUtil.dateBeforeEqualsDate(product.getDateAvailable(), new Date())) {
        throw new Exception("Item with id " + product.getId() + " is not available");
    }
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(product);
    item.setQuantity(shoppingCartItem.getQuantity());
    item.setShoppingCart(cartModel);
    // attributes
    List<ShoppingCartAttribute> cartAttributes = shoppingCartItem.getShoppingCartAttributes();
    if (!CollectionUtils.isEmpty(cartAttributes)) {
        for (ShoppingCartAttribute attribute : cartAttributes) {
            ProductAttribute productAttribute = productAttributeService.getById(attribute.getAttributeId());
            if (productAttribute != null && productAttribute.getProduct().getId().longValue() == product.getId().longValue()) {
                com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
                item.addAttributes(attributeItem);
            }
        }
    }
    return item;
}
Also used : Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) NoResultException(javax.persistence.NoResultException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) LocalDate(java.time.LocalDate) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)120 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)53 ArrayList (java.util.ArrayList)45 Language (com.salesmanager.core.model.reference.language.Language)42 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)41 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)36 ServiceException (com.salesmanager.core.business.exception.ServiceException)35 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)35 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)33 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)33 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)29 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)28 List (java.util.List)25 Date (java.util.Date)23 LightPersistableProduct (com.salesmanager.shop.model.catalog.product.LightPersistableProduct)22 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 Category (com.salesmanager.core.model.catalog.category.Category)20 Collectors (java.util.stream.Collectors)20 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)19 ConversionException (com.salesmanager.core.business.exception.ConversionException)17