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;
}
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;
}
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);
}
}
}
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;
}
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;
}
Aggregations