Search in sources :

Example 11 with FinalPrice

use of com.salesmanager.core.model.catalog.product.price.FinalPrice 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 12 with FinalPrice

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

the class ShoppingCartFacadeImpl method updateCartItems.

// TODO promoCode request parameter
@Override
public ShoppingCartData updateCartItems(Optional<String> promoCode, final List<ShoppingCartItem> shoppingCartItems, final MerchantStore store, final Language language) throws Exception {
    Validate.notEmpty(shoppingCartItems, "shoppingCartItems null or empty");
    ShoppingCart cartModel = null;
    Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
    for (ShoppingCartItem item : shoppingCartItems) {
        if (item.getQuantity() < 1) {
            throw new CartModificationException("Quantity must not be less than one");
        }
        if (cartModel == null) {
            cartModel = getCartModel(item.getCode(), store);
        }
        com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(item.getId(), cartModel);
        if (entryToUpdate == null) {
            throw new CartModificationException("Unknown entry number.");
        }
        entryToUpdate.getProduct();
        LOG.info("Updating cart entry quantity to" + item.getQuantity());
        entryToUpdate.setQuantity((int) item.getQuantity());
        List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
        productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
        final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
        entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
        cartItems.add(entryToUpdate);
    }
    cartModel.setPromoCode(null);
    if (promoCode.isPresent()) {
        cartModel.setPromoCode(promoCode.get());
        cartModel.setPromoAdded(new Date());
    }
    cartModel.setLineItems(cartItems);
    shoppingCartService.saveOrUpdate(cartModel);
    LOG.info("Cart entry updated with desired quantity");
    ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
    shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
    shoppingCartDataPopulator.setPricingService(pricingService);
    shoppingCartDataPopulator.setimageUtils(imageUtils);
    return shoppingCartDataPopulator.populate(cartModel, store, language);
}
Also used : CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) ArrayList(java.util.ArrayList) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Date(java.util.Date) LocalDate(java.time.LocalDate) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) HashSet(java.util.HashSet) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 13 with FinalPrice

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

the class ShoppingCartFacadeImpl method updateCartItem.

@Override
public ShoppingCartData updateCartItem(final Long itemID, final String cartId, final long newQuantity, final MerchantStore store, final Language language) throws Exception {
    if (newQuantity < 1) {
        throw new CartModificationException("Quantity must not be less than one");
    }
    if (StringUtils.isNotBlank(cartId)) {
        ShoppingCart cartModel = getCartModel(cartId, store);
        if (cartModel != null) {
            com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(itemID.longValue(), cartModel);
            if (entryToUpdate == null) {
                throw new CartModificationException("Unknown entry number.");
            }
            entryToUpdate.getProduct();
            LOG.info("Updating cart entry quantity to" + newQuantity);
            entryToUpdate.setQuantity((int) newQuantity);
            List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
            productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
            final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
            entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
            shoppingCartService.saveOrUpdate(cartModel);
            LOG.info("Cart entry updated with desired quantity");
            ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
            shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
            shoppingCartDataPopulator.setPricingService(pricingService);
            shoppingCartDataPopulator.setimageUtils(imageUtils);
            return shoppingCartDataPopulator.populate(cartModel, store, language);
        }
    }
    return null;
}
Also used : CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) ArrayList(java.util.ArrayList) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 14 with FinalPrice

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

the class PromoCodeCalculatorModule method caculateProductPiceVariation.

@Override
public OrderTotal caculateProductPiceVariation(OrderSummary summary, ShoppingCartItem shoppingCartItem, Product product, Customer customer, MerchantStore store) throws Exception {
    Validate.notNull(summary, "OrderTotalSummary must not be null");
    Validate.notNull(store, "MerchantStore must not be null");
    if (StringUtils.isBlank(summary.getPromoCode())) {
        return null;
    }
    KieSession kieSession = droolsBeanFactory.getKieSession(ResourceFactory.newClassPathResource("com/salesmanager/drools/rules/PromoCoupon.drl"));
    OrderTotalResponse resp = new OrderTotalResponse();
    OrderTotalInputParameters inputParameters = new OrderTotalInputParameters();
    inputParameters.setPromoCode(summary.getPromoCode());
    inputParameters.setDate(new Date());
    kieSession.insert(inputParameters);
    kieSession.setGlobal("total", resp);
    kieSession.fireAllRules();
    if (resp.getDiscount() != null) {
        OrderTotal orderTotal = null;
        if (resp.getDiscount() != null) {
            orderTotal = new OrderTotal();
            orderTotal.setOrderTotalCode(Constants.OT_DISCOUNT_TITLE);
            orderTotal.setOrderTotalType(OrderTotalType.SUBTOTAL);
            orderTotal.setTitle(Constants.OT_SUBTOTAL_MODULE_CODE);
            orderTotal.setText(summary.getPromoCode());
            // calculate discount that will be added as a negative value
            FinalPrice productPrice = pricingService.calculateProductPrice(product);
            Double discount = resp.getDiscount();
            BigDecimal reduction = productPrice.getFinalPrice().multiply(new BigDecimal(discount));
            reduction = reduction.multiply(new BigDecimal(shoppingCartItem.getQuantity()));
            // discount value
            orderTotal.setValue(reduction);
        // TODO check expiration
        }
        return orderTotal;
    }
    return null;
}
Also used : KieSession(org.kie.api.runtime.KieSession) OrderTotal(com.salesmanager.core.model.order.OrderTotal) Date(java.util.Date) BigDecimal(java.math.BigDecimal) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 15 with FinalPrice

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

the class ProductVariantApi method calculateVariant.

/**
 * Calculates the price based on selected options if any
 * @param id
 * @param options
 * @param merchantStore
 * @param language
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/products/{id}/variant", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ApiOperation(httpMethod = "POST", value = "Get product price variation based on selected product", notes = "", produces = "application/json", response = ReadableProductPrice.class)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableProductPrice calculateVariant(@PathVariable final Long id, @RequestBody ReadableSelectedProductVariant options, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletResponse response) throws Exception {
    Product product = productService.getById(id);
    if (product == null) {
        response.sendError(404, "Product not fount for id " + id);
        return null;
    }
    List<ReadableProductVariantValue> ids = options.getOptions();
    if (CollectionUtils.isEmpty(ids)) {
        return null;
    }
    List<ReadableProductVariantValue> variants = options.getOptions();
    List<ProductAttribute> attributes = new ArrayList<ProductAttribute>();
    Set<ProductAttribute> productAttributes = product.getAttributes();
    for (ProductAttribute attribute : productAttributes) {
        Long option = attribute.getProductOption().getId();
        Long optionValue = attribute.getProductOptionValue().getId();
        for (ReadableProductVariantValue v : variants) {
            if (v.getOption().longValue() == option.longValue() && v.getValue().longValue() == optionValue.longValue()) {
                attributes.add(attribute);
            }
        }
    }
    FinalPrice price = pricingService.calculateProductPrice(product, attributes);
    ReadableProductPrice readablePrice = new ReadableProductPrice();
    ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
    populator.setPricingService(pricingService);
    populator.populate(price, readablePrice, merchantStore, language);
    return readablePrice;
}
Also used : ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ReadableProductVariantValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariantValue) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)28 ArrayList (java.util.ArrayList)18 Product (com.salesmanager.core.model.catalog.product.Product)15 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)14 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)11 Language (com.salesmanager.core.model.reference.language.Language)11 BigDecimal (java.math.BigDecimal)9 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)8 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)8 ServiceException (com.salesmanager.core.business.exception.ServiceException)6 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)6 Category (com.salesmanager.core.model.catalog.category.Category)6 ReadableProductPrice (com.salesmanager.shop.model.catalog.product.ReadableProductPrice)6 ImageFilePath (com.salesmanager.shop.utils.ImageFilePath)6 ProductImage (com.salesmanager.core.model.catalog.product.image.ProductImage)5 ProductPrice (com.salesmanager.core.model.catalog.product.price.ProductPrice)5 ProductPriceDescription (com.salesmanager.core.model.catalog.product.price.ProductPriceDescription)5 ProductSpecification (com.salesmanager.shop.model.catalog.product.ProductSpecification)5 ReadableImage (com.salesmanager.shop.model.catalog.product.ReadableImage)5 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)5