Search in sources :

Example 6 with FinalPrice

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

the class ProductPriceUtils method calculateFinalPrice.

private FinalPrice calculateFinalPrice(Product product) {
    FinalPrice finalPrice = null;
    List<FinalPrice> otherPrices = null;
    Set<ProductAvailability> availabilities = product.getAvailabilities();
    for (ProductAvailability availability : availabilities) {
        if (!StringUtils.isEmpty(availability.getRegion()) && availability.getRegion().equals(Constants.ALL_REGIONS)) {
            // TODO REL 2.1 accept a region
            Set<ProductPrice> prices = availability.getPrices();
            for (ProductPrice price : prices) {
                FinalPrice p = finalPrice(price);
                if (price.isDefaultPrice()) {
                    finalPrice = p;
                } else {
                    if (otherPrices == null) {
                        otherPrices = new ArrayList<FinalPrice>();
                    }
                    otherPrices.add(p);
                }
            }
        }
    }
    if (finalPrice != null) {
        finalPrice.setAdditionalPrices(otherPrices);
    } else {
        if (otherPrices != null) {
            finalPrice = otherPrices.get(0);
        }
    }
    return finalPrice;
}
Also used : ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 7 with FinalPrice

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

the class ProductPriceUtils method getFinalProductPrice.

/**
 * This method calculates the final price taking into account
 * all attributes included having a specified default attribute with an attribute price gt 0
 * in the product object. The calculation is based
 * on the default price.
 * Attributes may be null
 * @param Product
 * @param List<ProductAttribute>
 * @return FinalPrice
 */
// Pricer
public FinalPrice getFinalProductPrice(Product product, List<ProductAttribute> attributes) {
    FinalPrice finalPrice = calculateFinalPrice(product);
    // attributes
    BigDecimal attributePrice = null;
    if (attributes != null && attributes.size() > 0) {
        for (ProductAttribute attribute : attributes) {
            if (attribute.getProductAttributePrice() != null && attribute.getProductAttributePrice().doubleValue() > 0) {
                if (attributePrice == null) {
                    attributePrice = new BigDecimal(0);
                }
                attributePrice = attributePrice.add(attribute.getProductAttributePrice());
            }
        }
        if (attributePrice != null && attributePrice.doubleValue() > 0) {
            BigDecimal fp = finalPrice.getFinalPrice();
            fp = fp.add(attributePrice);
            finalPrice.setFinalPrice(fp);
            BigDecimal op = finalPrice.getOriginalPrice();
            op = op.add(attributePrice);
            finalPrice.setOriginalPrice(op);
            BigDecimal dp = finalPrice.getDiscountedPrice();
            if (dp != null) {
                dp = dp.add(attributePrice);
                finalPrice.setDiscountedPrice(dp);
            }
        }
    }
    return finalPrice;
}
Also used : ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) BigDecimal(java.math.BigDecimal) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 8 with FinalPrice

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

the class SearchServiceImpl method index.

@Async
@SuppressWarnings("rawtypes")
public void index(MerchantStore store, Product product) throws ServiceException {
    if (configuration.getProperty(INDEX_PRODUCTS) == null || configuration.getProperty(INDEX_PRODUCTS).equals(Constants.FALSE)) {
        return;
    }
    FinalPrice price = pricingService.calculateProductPrice(product);
    Set<ProductDescription> descriptions = product.getDescriptions();
    for (ProductDescription description : descriptions) {
        StringBuilder collectionName = new StringBuilder();
        collectionName.append(PRODUCT_INDEX_NAME).append(UNDERSCORE).append(description.getLanguage().getCode()).append(UNDERSCORE).append(store.getCode().toLowerCase());
        IndexProduct index = new IndexProduct();
        index.setId(String.valueOf(product.getId()));
        index.setStore(store.getCode().toLowerCase());
        index.setLang(description.getLanguage().getCode());
        index.setAvailable(product.isAvailable());
        index.setDescription(description.getDescription());
        index.setName(description.getName());
        if (product.getManufacturer() != null) {
            index.setManufacturer(String.valueOf(product.getManufacturer().getId()));
        }
        if (price != null) {
            index.setPrice(price.getFinalPrice().doubleValue());
        }
        index.setHighlight(description.getProductHighlight());
        if (!StringUtils.isBlank(description.getMetatagKeywords())) {
            String[] tags = description.getMetatagKeywords().split(",");
            @SuppressWarnings("unchecked") List<String> tagsList = new ArrayList(Arrays.asList(tags));
            index.setTags(tagsList);
        }
        Set<Category> categories = product.getCategories();
        if (!CollectionUtils.isEmpty(categories)) {
            List<String> categoryList = new ArrayList<String>();
            for (Category category : categories) {
                categoryList.add(category.getCode());
            }
            index.setCategories(categoryList);
        }
        String jsonString = index.toJSONString();
        try {
            searchService.index(jsonString, collectionName.toString());
        } catch (Exception e) {
            throw new ServiceException("Cannot index product id [" + product.getId() + "], " + e.getMessage(), e);
        }
    }
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ArrayList(java.util.ArrayList) IndexProduct(com.salesmanager.core.model.search.IndexProduct) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) Async(org.springframework.scheduling.annotation.Async)

Example 9 with FinalPrice

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

the class ReadableProductPricePopulator method populate.

@Override
public ReadableProductPrice populate(ProductPrice source, ReadableProductPrice target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(pricingService, "pricingService must be set");
    Validate.notNull(source.getProductAvailability(), "productPrice.availability cannot be null");
    Validate.notNull(source.getProductAvailability().getProduct(), "productPrice.availability.product cannot be null");
    try {
        if (language == null) {
            target = new ReadableProductPriceFull();
        }
        if (source.getId() != null && source.getId() > 0) {
            target.setId(source.getId());
        }
        FinalPrice finalPrice = pricingService.calculateProductPrice(source.getProductAvailability().getProduct());
        target.setOriginalPrice(pricingService.getDisplayAmount(source.getProductPriceAmount(), store));
        if (finalPrice.isDiscounted()) {
            target.setDiscounted(true);
            target.setFinalPrice(pricingService.getDisplayAmount(source.getProductPriceSpecialAmount(), store));
        } else {
            target.setFinalPrice(pricingService.getDisplayAmount(finalPrice.getOriginalPrice(), store));
        }
        if (source.getDescriptions() != null && source.getDescriptions().size() > 0) {
            List<com.salesmanager.shop.model.catalog.product.ProductPriceDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductPriceDescription>();
            Set<ProductPriceDescription> descriptions = source.getDescriptions();
            ProductPriceDescription description = null;
            for (ProductPriceDescription desc : descriptions) {
                if (language != null && desc.getLanguage().getCode().equals(language.getCode())) {
                    description = desc;
                    break;
                } else {
                    fulldescriptions.add(populateDescription(desc));
                }
            }
            if (description != null) {
                com.salesmanager.shop.model.catalog.product.ProductPriceDescription d = populateDescription(description);
                target.setDescription(d);
            }
            if (target instanceof ReadableProductPriceFull) {
                ((ReadableProductPriceFull) target).setDescriptions(fulldescriptions);
            }
        }
    } catch (Exception e) {
        throw new ConversionException("Exception while converting to ReadableProductPrice", e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ReadableProductPriceFull(com.salesmanager.shop.model.catalog.product.ReadableProductPriceFull) ArrayList(java.util.ArrayList) ConversionException(com.salesmanager.core.business.exception.ConversionException) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 10 with FinalPrice

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

the class ReadableProductDefinitionMapper method merge.

@Override
public ReadableProductDefinition merge(Product source, ReadableProductDefinition destination, MerchantStore store, Language language) {
    Validate.notNull(source, "Product cannot be null");
    Validate.notNull(destination, "Product destination cannot be null");
    ReadableProductDefinition returnDestination = destination;
    if (language == null) {
        returnDestination = new ReadableProductDefinitionFull();
    }
    List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
    returnDestination.setIdentifier(source.getSku());
    returnDestination.setId(source.getId());
    returnDestination.setVisible(source.isAvailable());
    returnDestination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
    ProductDescription description = null;
    if (source.getDescriptions() != null && source.getDescriptions().size() > 0) {
        for (ProductDescription desc : source.getDescriptions()) {
            if (language != null && desc.getLanguage() != null && desc.getLanguage().getId().intValue() == language.getId().intValue()) {
                description = desc;
                break;
            } else {
                fulldescriptions.add(populateDescription(desc));
            }
        }
    }
    if (description != null) {
        com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
        returnDestination.setDescription(tragetDescription);
    }
    if (source.getManufacturer() != null) {
        ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
        returnDestination.setManufacturer(manufacturer);
    }
    if (!CollectionUtils.isEmpty(source.getCategories())) {
        List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
        for (Category category : source.getCategories()) {
            ReadableCategory readableCategory = readableCategoryMapper.convert(category, store, language);
            categoryList.add(readableCategory);
        }
        returnDestination.setCategories(categoryList);
    }
    ProductSpecification specifications = new ProductSpecification();
    specifications.setHeight(source.getProductHeight());
    specifications.setLength(source.getProductLength());
    specifications.setWeight(source.getProductWeight());
    specifications.setWidth(source.getProductWidth());
    if (!StringUtils.isBlank(store.getSeizeunitcode())) {
        specifications.setDimensionUnitOfMeasure(DimensionUnitOfMeasure.valueOf(store.getSeizeunitcode().toLowerCase()));
    }
    if (!StringUtils.isBlank(store.getWeightunitcode())) {
        specifications.setWeightUnitOfMeasure(WeightUnitOfMeasure.valueOf(store.getWeightunitcode().toLowerCase()));
    }
    returnDestination.setProductSpecifications(specifications);
    if (source.getType() != null) {
        ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
        returnDestination.setType(readableType);
    }
    returnDestination.setSortOrder(source.getSortOrder());
    // images
    Set<ProductImage> images = source.getImages();
    if (CollectionUtils.isNotEmpty(images)) {
        List<ReadableImage> imageList = images.stream().map(i -> this.convertImage(source, i, store)).collect(Collectors.toList());
        returnDestination.setImages(imageList);
    }
    // quantity
    ProductAvailability availability = null;
    for (ProductAvailability a : source.getAvailabilities()) {
        availability = a;
        returnDestination.setCanBePurchased(availability.getProductStatus());
        returnDestination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
    }
    FinalPrice price = null;
    try {
        price = pricingService.calculateProductPrice(source);
    } catch (ServiceException e) {
        throw new ConversionRuntimeException("Unable to get product price", e);
    }
    if (price != null) {
        returnDestination.setPrice(price.getStringPrice());
    }
    if (returnDestination instanceof ReadableProductDefinitionFull) {
        ((ReadableProductDefinitionFull) returnDestination).setDescriptions(fulldescriptions);
    }
    return returnDestination;
}
Also used : DateUtil(com.salesmanager.shop.utils.DateUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) ReadableCategoryMapper(com.salesmanager.shop.mapper.catalog.ReadableCategoryMapper) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) DimensionUnitOfMeasure(com.salesmanager.shop.model.references.DimensionUnitOfMeasure) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) Mapper(com.salesmanager.shop.mapper.Mapper) Product(com.salesmanager.core.model.catalog.product.Product) ReadableManufacturerMapper(com.salesmanager.shop.mapper.catalog.ReadableManufacturerMapper) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType) Set(java.util.Set) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) Collectors(java.util.stream.Collectors) Category(com.salesmanager.core.model.catalog.category.Category) ReadableProductTypeMapper(com.salesmanager.shop.mapper.catalog.ReadableProductTypeMapper) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) List(java.util.List) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) WeightUnitOfMeasure(com.salesmanager.shop.model.references.WeightUnitOfMeasure) Category(com.salesmanager.core.model.catalog.category.Category) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) ProductImage(com.salesmanager.core.model.catalog.product.image.ProductImage) ArrayList(java.util.ArrayList) ReadableImage(com.salesmanager.shop.model.catalog.product.ReadableImage) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ReadableCategory(com.salesmanager.shop.model.catalog.category.ReadableCategory) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ReadableManufacturer(com.salesmanager.shop.model.catalog.manufacturer.ReadableManufacturer) ReadableProductDefinition(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinition) ProductSpecification(com.salesmanager.shop.model.catalog.product.ProductSpecification) ReadableProductDefinitionFull(com.salesmanager.shop.model.catalog.product.product.definition.ReadableProductDefinitionFull) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) ReadableProductType(com.salesmanager.shop.model.catalog.product.type.ReadableProductType)

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