Search in sources :

Example 1 with ShoppingCartAttributeItem

use of com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem in project shopizer by shopizer-ecommerce.

the class OrderProductPopulator method populate.

/**
 * Converts a ShoppingCartItem carried in the ShoppingCart to an OrderProduct
 * that will be saved in the system
 */
@Override
public OrderProduct populate(ShoppingCartItem source, OrderProduct 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");
    try {
        Product modelProduct = productService.getById(source.getProductId());
        if (modelProduct == null) {
            throw new ConversionException("Cannot get product with id (productId) " + source.getProductId());
        }
        if (modelProduct.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ConversionException("Invalid product id " + source.getProductId());
        }
        DigitalProduct digitalProduct = digitalProductService.getByProduct(store, modelProduct);
        if (digitalProduct != null) {
            OrderProductDownload orderProductDownload = new OrderProductDownload();
            orderProductDownload.setOrderProductFilename(digitalProduct.getProductFileName());
            orderProductDownload.setOrderProduct(target);
            orderProductDownload.setDownloadCount(0);
            orderProductDownload.setMaxdays(ApplicationConstants.MAX_DOWNLOAD_DAYS);
            target.getDownloads().add(orderProductDownload);
        }
        target.setOneTimeCharge(source.getItemPrice());
        target.setProductName(source.getProduct().getDescriptions().iterator().next().getName());
        target.setProductQuantity(source.getQuantity());
        target.setSku(source.getProduct().getSku());
        FinalPrice finalPrice = source.getFinalPrice();
        if (finalPrice == null) {
            throw new ConversionException("Object final price not populated in shoppingCartItem (source)");
        }
        // Default price
        OrderProductPrice orderProductPrice = orderProductPrice(finalPrice);
        orderProductPrice.setOrderProduct(target);
        Set<OrderProductPrice> prices = new HashSet<OrderProductPrice>();
        prices.add(orderProductPrice);
        // Other prices
        List<FinalPrice> otherPrices = finalPrice.getAdditionalPrices();
        if (otherPrices != null) {
            for (FinalPrice otherPrice : otherPrices) {
                OrderProductPrice other = orderProductPrice(otherPrice);
                other.setOrderProduct(target);
                prices.add(other);
            }
        }
        target.setPrices(prices);
        // OrderProductAttribute
        Set<ShoppingCartAttributeItem> attributeItems = source.getAttributes();
        if (!CollectionUtils.isEmpty(attributeItems)) {
            Set<OrderProductAttribute> attributes = new HashSet<OrderProductAttribute>();
            for (ShoppingCartAttributeItem attribute : attributeItems) {
                OrderProductAttribute orderProductAttribute = new OrderProductAttribute();
                orderProductAttribute.setOrderProduct(target);
                Long id = attribute.getProductAttributeId();
                ProductAttribute attr = productAttributeService.getById(id);
                if (attr == null) {
                    throw new ConversionException("Attribute id " + id + " does not exists");
                }
                if (attr.getProduct().getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Attribute id " + id + " invalid for this store");
                }
                orderProductAttribute.setProductAttributeIsFree(attr.getProductAttributeIsFree());
                orderProductAttribute.setProductAttributeName(attr.getProductOption().getDescriptionsSettoList().get(0).getName());
                orderProductAttribute.setProductAttributeValueName(attr.getProductOptionValue().getDescriptionsSettoList().get(0).getName());
                orderProductAttribute.setProductAttributePrice(attr.getProductAttributePrice());
                orderProductAttribute.setProductAttributeWeight(attr.getProductAttributeWeight());
                orderProductAttribute.setProductOptionId(attr.getProductOption().getId());
                orderProductAttribute.setProductOptionValueId(attr.getProductOptionValue().getId());
                attributes.add(orderProductAttribute);
            }
            target.setOrderAttributes(attributes);
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Product(com.salesmanager.core.model.catalog.product.Product) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) DigitalProduct(com.salesmanager.core.model.catalog.product.file.DigitalProduct) ShoppingCartAttributeItem(com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) ConversionException(com.salesmanager.core.business.exception.ConversionException) OrderProductPrice(com.salesmanager.core.model.order.orderproduct.OrderProductPrice) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) OrderProductDownload(com.salesmanager.core.model.order.orderproduct.OrderProductDownload) DigitalProduct(com.salesmanager.core.model.catalog.product.file.DigitalProduct) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) HashSet(java.util.HashSet)

Example 2 with ShoppingCartAttributeItem

use of com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem in project shopizer by shopizer-ecommerce.

the class ShoppingCartServiceImpl method getPopulatedItem.

@Transactional
private void getPopulatedItem(final ShoppingCartItem item) throws Exception {
    Product product = null;
    Long productId = item.getProductId();
    product = productService.getById(productId);
    if (product == null) {
        item.setObsolete(true);
        return;
    }
    item.setProduct(product);
    if (product.isProductVirtual()) {
        item.setProductVirtual(true);
    }
    Set<ShoppingCartAttributeItem> cartAttributes = item.getAttributes();
    Set<ProductAttribute> productAttributes = product.getAttributes();
    // attributes maintained
    List<ProductAttribute> attributesList = new ArrayList<ProductAttribute>();
    // attributes to remove
    List<ShoppingCartAttributeItem> removeAttributesList = new ArrayList<ShoppingCartAttributeItem>();
    // DELETE ORPHEANS MANUALLY
    if ((productAttributes != null && productAttributes.size() > 0) || (cartAttributes != null && cartAttributes.size() > 0)) {
        if (cartAttributes != null) {
            for (ShoppingCartAttributeItem attribute : cartAttributes) {
                long attributeId = attribute.getProductAttributeId();
                boolean existingAttribute = false;
                for (ProductAttribute productAttribute : productAttributes) {
                    if (productAttribute.getId().equals(attributeId)) {
                        attribute.setProductAttribute(productAttribute);
                        attributesList.add(productAttribute);
                        existingAttribute = true;
                        break;
                    }
                }
                if (!existingAttribute) {
                    removeAttributesList.add(attribute);
                }
            }
        }
    }
    // cleanup orphean item
    if (CollectionUtils.isNotEmpty(removeAttributesList)) {
        for (ShoppingCartAttributeItem attr : removeAttributesList) {
            shoppingCartAttributeItemRepository.delete(attr);
        }
    }
    // cleanup detached attributes
    if (CollectionUtils.isEmpty(attributesList)) {
        item.setAttributes(null);
    }
    // set item price
    FinalPrice price = pricingService.calculateProductPrice(product, attributesList);
    item.setItemPrice(price.getFinalPrice());
    item.setFinalPrice(price);
    BigDecimal subTotal = item.getItemPrice().multiply(new BigDecimal(item.getQuantity()));
    item.setSubTotal(subTotal);
}
Also used : ArrayList(java.util.ArrayList) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) Product(com.salesmanager.core.model.catalog.product.Product) ShoppingCartAttributeItem(com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) BigDecimal(java.math.BigDecimal) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ShoppingCartAttributeItem

use of com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem in project shopizer by shopizer-ecommerce.

the class ShoppingCartServiceImpl method getShoppingCartItems.

private Set<ShoppingCartItem> getShoppingCartItems(final ShoppingCart sessionCart, final MerchantStore store, final ShoppingCart cartModel) throws Exception {
    Set<ShoppingCartItem> shoppingCartItemsSet = null;
    if (CollectionUtils.isNotEmpty(sessionCart.getLineItems())) {
        shoppingCartItemsSet = new HashSet<ShoppingCartItem>();
        for (ShoppingCartItem shoppingCartItem : sessionCart.getLineItems()) {
            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());
            }
            ShoppingCartItem item = populateShoppingCartItem(product);
            item.setQuantity(shoppingCartItem.getQuantity());
            item.setShoppingCart(cartModel);
            List<ShoppingCartAttributeItem> cartAttributes = new ArrayList<ShoppingCartAttributeItem>();
            if (shoppingCartItem != null && !CollectionUtils.isEmpty(shoppingCartItem.getAttributes())) {
                cartAttributes.addAll(shoppingCartItem.getAttributes());
                if (CollectionUtils.isNotEmpty(cartAttributes)) {
                    for (ShoppingCartAttributeItem shoppingCartAttributeItem : cartAttributes) {
                        ProductAttribute productAttribute = productAttributeService.getById(shoppingCartAttributeItem.getId());
                        if (productAttribute != null && productAttribute.getProduct().getId().longValue() == product.getId().longValue()) {
                            ShoppingCartAttributeItem attributeItem = new ShoppingCartAttributeItem(item, productAttribute);
                            if (shoppingCartAttributeItem.getId() > 0) {
                                attributeItem.setId(shoppingCartAttributeItem.getId());
                            }
                            item.addAttributes(attributeItem);
                        }
                    }
                }
            }
            shoppingCartItemsSet.add(item);
        }
    }
    return shoppingCartItemsSet;
}
Also used : ArrayList(java.util.ArrayList) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) Product(com.salesmanager.core.model.catalog.product.Product) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) ShoppingCartAttributeItem(com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 4 with ShoppingCartAttributeItem

use of com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem in project shopizer by shopizer-ecommerce.

the class ShoppingCartTest method createShoppingCart.

@Test
public void createShoppingCart() throws Exception {
    MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    Language en = languageService.getByCode("en");
    /**
     * CATALOG CREATION *
     */
    ProductType generalType = productTypeService.getProductType(ProductType.GENERAL_TYPE);
    /**
     * Create the category
     */
    Category shirts = new Category();
    shirts.setMerchantStore(store);
    shirts.setCode("shirts");
    CategoryDescription shirtsEnglishDescription = new CategoryDescription();
    shirtsEnglishDescription.setName("Shirts");
    shirtsEnglishDescription.setCategory(shirts);
    shirtsEnglishDescription.setLanguage(en);
    Set<CategoryDescription> descriptions = new HashSet<CategoryDescription>();
    descriptions.add(shirtsEnglishDescription);
    shirts.setDescriptions(descriptions);
    categoryService.create(shirts);
    /**
     * Create a manufacturer
     */
    Manufacturer addidas = new Manufacturer();
    addidas.setMerchantStore(store);
    addidas.setCode("addidas");
    ManufacturerDescription addidasDesc = new ManufacturerDescription();
    addidasDesc.setLanguage(en);
    addidasDesc.setManufacturer(addidas);
    addidasDesc.setName("Addidas");
    addidas.getDescriptions().add(addidasDesc);
    manufacturerService.create(addidas);
    /**
     * Create an option
     */
    ProductOption option = new ProductOption();
    option.setMerchantStore(store);
    option.setCode("color");
    option.setProductOptionType(ProductOptionType.Radio.name());
    ProductOptionDescription optionDescription = new ProductOptionDescription();
    optionDescription.setLanguage(en);
    optionDescription.setName("Color");
    optionDescription.setDescription("Item color");
    optionDescription.setProductOption(option);
    option.getDescriptions().add(optionDescription);
    productOptionService.saveOrUpdate(option);
    /**
     * first option value *
     */
    ProductOptionValue white = new ProductOptionValue();
    white.setMerchantStore(store);
    white.setCode("white");
    ProductOptionValueDescription whiteDescription = new ProductOptionValueDescription();
    whiteDescription.setLanguage(en);
    whiteDescription.setName("White");
    whiteDescription.setDescription("White color");
    whiteDescription.setProductOptionValue(white);
    white.getDescriptions().add(whiteDescription);
    productOptionValueService.saveOrUpdate(white);
    ProductOptionValue black = new ProductOptionValue();
    black.setMerchantStore(store);
    black.setCode("black");
    /**
     * second option value *
     */
    ProductOptionValueDescription blackDesc = new ProductOptionValueDescription();
    blackDesc.setLanguage(en);
    blackDesc.setName("Black");
    blackDesc.setDescription("Black color");
    blackDesc.setProductOptionValue(black);
    black.getDescriptions().add(blackDesc);
    productOptionValueService.saveOrUpdate(black);
    /**
     * Create a complex product
     */
    Product product = new Product();
    product.setProductHeight(new BigDecimal(4));
    product.setProductLength(new BigDecimal(3));
    product.setProductWidth(new BigDecimal(1));
    product.setSku("XABC12");
    product.setManufacturer(addidas);
    product.setType(generalType);
    product.setMerchantStore(store);
    // Product description
    ProductDescription description = new ProductDescription();
    description.setName("Short sleeves shirt");
    description.setLanguage(en);
    description.setProduct(product);
    product.getDescriptions().add(description);
    product.getCategories().add(shirts);
    // availability
    ProductAvailability availability = new ProductAvailability();
    availability.setProductDateAvailable(new Date());
    availability.setProductQuantity(100);
    availability.setRegion("*");
    // associate with product
    availability.setProduct(product);
    // price
    ProductPrice dprice = new ProductPrice();
    dprice.setDefaultPrice(true);
    dprice.setProductPriceAmount(new BigDecimal(29.99));
    dprice.setProductAvailability(availability);
    ProductPriceDescription dpd = new ProductPriceDescription();
    dpd.setName("Base price");
    dpd.setProductPrice(dprice);
    dpd.setLanguage(en);
    dprice.getDescriptions().add(dpd);
    availability.getPrices().add(dprice);
    product.getAvailabilities().add(availability);
    // attributes
    // white
    ProductAttribute whiteAttribute = new ProductAttribute();
    whiteAttribute.setProduct(product);
    whiteAttribute.setProductOption(option);
    whiteAttribute.setAttributeDefault(true);
    // no price variation
    whiteAttribute.setProductAttributePrice(new BigDecimal(0));
    // no weight variation
    whiteAttribute.setProductAttributeWeight(new BigDecimal(0));
    whiteAttribute.setProductOption(option);
    whiteAttribute.setProductOptionValue(white);
    product.getAttributes().add(whiteAttribute);
    // black
    ProductAttribute blackAttribute = new ProductAttribute();
    blackAttribute.setProduct(product);
    blackAttribute.setProductOption(option);
    // 5 + dollars
    blackAttribute.setProductAttributePrice(new BigDecimal(5));
    // no weight variation
    blackAttribute.setProductAttributeWeight(new BigDecimal(0));
    blackAttribute.setProductOption(option);
    blackAttribute.setProductOptionValue(black);
    product.getAttributes().add(blackAttribute);
    productService.create(product);
    /**
     * Create Shopping cart *
     */
    ShoppingCart shoppingCart = new ShoppingCart();
    shoppingCart.setMerchantStore(store);
    UUID cartCode = UUID.randomUUID();
    shoppingCart.setShoppingCartCode(cartCode.toString());
    ShoppingCartItem item = new ShoppingCartItem(shoppingCart, product);
    item.setShoppingCart(shoppingCart);
    FinalPrice price = pricingService.calculateProductPrice(product);
    item.setItemPrice(price.getFinalPrice());
    item.setQuantity(1);
    /**
     * user selects black *
     */
    ShoppingCartAttributeItem attributeItem = new ShoppingCartAttributeItem(item, blackAttribute);
    item.getAttributes().add(attributeItem);
    shoppingCart.getLineItems().add(item);
    // create cart
    shoppingCartService.create(shoppingCart);
    /**
     * Retrieve cart *
     */
    ShoppingCart retrievedCart = shoppingCartService.getByCode(cartCode.toString(), store);
    Assert.assertNotNull(retrievedCart);
    /**
     * Delete cart *
     */
    shoppingCartService.delete(retrievedCart);
    /**
     * Check if cart has been deleted *
     */
    retrievedCart = shoppingCartService.getByCode(cartCode.toString(), store);
    Assert.assertNull(retrievedCart);
    // Clean up for other tests
    categoryService.delete(shirts);
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) Product(com.salesmanager.core.model.catalog.product.Product) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ShoppingCartAttributeItem(com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) Language(com.salesmanager.core.model.reference.language.Language) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) UUID(java.util.UUID) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) HashSet(java.util.HashSet) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) BigDecimal(java.math.BigDecimal) Date(java.util.Date) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ManufacturerDescription(com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription) CategoryDescription(com.salesmanager.core.model.catalog.category.CategoryDescription) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription) Test(org.junit.Test)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)4 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)4 ShoppingCartAttributeItem (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem)4 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)3 ShippingProduct (com.salesmanager.core.model.shipping.ShippingProduct)2 ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 Category (com.salesmanager.core.model.catalog.category.Category)1 CategoryDescription (com.salesmanager.core.model.catalog.category.CategoryDescription)1 ProductOption (com.salesmanager.core.model.catalog.product.attribute.ProductOption)1 ProductOptionDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription)1 ProductOptionValue (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue)1 ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)1 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)1 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)1 DigitalProduct (com.salesmanager.core.model.catalog.product.file.DigitalProduct)1