use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class OrderServiceImpl method caculateOrder.
private OrderTotalSummary caculateOrder(OrderSummary summary, Customer customer, final MerchantStore store, final Language language) throws Exception {
OrderTotalSummary totalSummary = new OrderTotalSummary();
List<OrderTotal> orderTotals = new ArrayList<OrderTotal>();
Map<String, OrderTotal> otherPricesTotals = new HashMap<String, OrderTotal>();
ShippingConfiguration shippingConfiguration = null;
BigDecimal grandTotal = new BigDecimal(0);
grandTotal.setScale(2, RoundingMode.HALF_UP);
// price by item
/**
* qty * price
* subtotal
*/
BigDecimal subTotal = new BigDecimal(0);
subTotal.setScale(2, RoundingMode.HALF_UP);
for (ShoppingCartItem item : summary.getProducts()) {
BigDecimal st = item.getItemPrice().multiply(new BigDecimal(item.getQuantity()));
item.setSubTotal(st);
subTotal = subTotal.add(st);
// Other prices
FinalPrice finalPrice = item.getFinalPrice();
if (finalPrice != null) {
List<FinalPrice> otherPrices = finalPrice.getAdditionalPrices();
if (otherPrices != null) {
for (FinalPrice price : otherPrices) {
if (!price.isDefaultPrice()) {
OrderTotal itemSubTotal = otherPricesTotals.get(price.getProductPrice().getCode());
if (itemSubTotal == null) {
itemSubTotal = new OrderTotal();
itemSubTotal.setModule(Constants.OT_ITEM_PRICE_MODULE_CODE);
itemSubTotal.setTitle(Constants.OT_ITEM_PRICE_MODULE_CODE);
itemSubTotal.setOrderTotalCode(price.getProductPrice().getCode());
itemSubTotal.setOrderTotalType(OrderTotalType.PRODUCT);
itemSubTotal.setSortOrder(0);
otherPricesTotals.put(price.getProductPrice().getCode(), itemSubTotal);
}
BigDecimal orderTotalValue = itemSubTotal.getValue();
if (orderTotalValue == null) {
orderTotalValue = new BigDecimal(0);
orderTotalValue.setScale(2, RoundingMode.HALF_UP);
}
orderTotalValue = orderTotalValue.add(price.getFinalPrice());
itemSubTotal.setValue(orderTotalValue);
if (price.getProductPrice().getProductPriceType().name().equals(OrderValueType.ONE_TIME)) {
subTotal = subTotal.add(price.getFinalPrice());
}
}
}
}
}
}
// only in order page, otherwise invokes too many processing
if (OrderSummaryType.ORDERTOTAL.name().equals(summary.getOrderSummaryType().name()) || OrderSummaryType.SHOPPINGCART.name().equals(summary.getOrderSummaryType().name())) {
// Post processing order total variation modules for sub total calculation - drools, custom modules
// may affect the sub total
OrderTotalVariation orderTotalVariation = orderTotalService.findOrderTotalVariation(summary, customer, store, language);
int currentCount = 10;
if (CollectionUtils.isNotEmpty(orderTotalVariation.getVariations())) {
for (OrderTotal variation : orderTotalVariation.getVariations()) {
variation.setSortOrder(currentCount++);
orderTotals.add(variation);
subTotal = subTotal.subtract(variation.getValue());
}
}
}
totalSummary.setSubTotal(subTotal);
grandTotal = grandTotal.add(subTotal);
OrderTotal orderTotalSubTotal = new OrderTotal();
orderTotalSubTotal.setModule(Constants.OT_SUBTOTAL_MODULE_CODE);
orderTotalSubTotal.setOrderTotalType(OrderTotalType.SUBTOTAL);
orderTotalSubTotal.setOrderTotalCode("order.total.subtotal");
orderTotalSubTotal.setTitle(Constants.OT_SUBTOTAL_MODULE_CODE);
orderTotalSubTotal.setSortOrder(5);
orderTotalSubTotal.setValue(subTotal);
orderTotals.add(orderTotalSubTotal);
// shipping
if (summary.getShippingSummary() != null) {
OrderTotal shippingSubTotal = new OrderTotal();
shippingSubTotal.setModule(Constants.OT_SHIPPING_MODULE_CODE);
shippingSubTotal.setOrderTotalType(OrderTotalType.SHIPPING);
shippingSubTotal.setOrderTotalCode("order.total.shipping");
shippingSubTotal.setTitle(Constants.OT_SHIPPING_MODULE_CODE);
shippingSubTotal.setSortOrder(100);
orderTotals.add(shippingSubTotal);
if (!summary.getShippingSummary().isFreeShipping()) {
shippingSubTotal.setValue(summary.getShippingSummary().getShipping());
grandTotal = grandTotal.add(summary.getShippingSummary().getShipping());
} else {
shippingSubTotal.setValue(new BigDecimal(0));
grandTotal = grandTotal.add(new BigDecimal(0));
}
// check handling fees
shippingConfiguration = shippingService.getShippingConfiguration(store);
if (summary.getShippingSummary().getHandling() != null && summary.getShippingSummary().getHandling().doubleValue() > 0) {
if (shippingConfiguration.getHandlingFees() != null && shippingConfiguration.getHandlingFees().doubleValue() > 0) {
OrderTotal handlingubTotal = new OrderTotal();
handlingubTotal.setModule(Constants.OT_HANDLING_MODULE_CODE);
handlingubTotal.setOrderTotalType(OrderTotalType.HANDLING);
handlingubTotal.setOrderTotalCode("order.total.handling");
handlingubTotal.setTitle(Constants.OT_HANDLING_MODULE_CODE);
// handlingubTotal.setText("order.total.handling");
handlingubTotal.setSortOrder(120);
handlingubTotal.setValue(summary.getShippingSummary().getHandling());
orderTotals.add(handlingubTotal);
grandTotal = grandTotal.add(summary.getShippingSummary().getHandling());
}
}
}
// tax
List<TaxItem> taxes = taxService.calculateTax(summary, customer, store, language);
if (taxes != null && taxes.size() > 0) {
BigDecimal totalTaxes = new BigDecimal(0);
totalTaxes.setScale(2, RoundingMode.HALF_UP);
int taxCount = 200;
for (TaxItem tax : taxes) {
OrderTotal taxLine = new OrderTotal();
taxLine.setModule(Constants.OT_TAX_MODULE_CODE);
taxLine.setOrderTotalType(OrderTotalType.TAX);
taxLine.setOrderTotalCode(tax.getLabel());
taxLine.setSortOrder(taxCount);
taxLine.setTitle(Constants.OT_TAX_MODULE_CODE);
taxLine.setText(tax.getLabel());
taxLine.setValue(tax.getItemPrice());
totalTaxes = totalTaxes.add(tax.getItemPrice());
orderTotals.add(taxLine);
// grandTotal=grandTotal.add(tax.getItemPrice());
taxCount++;
}
grandTotal = grandTotal.add(totalTaxes);
totalSummary.setTaxTotal(totalTaxes);
}
// grand total
OrderTotal orderTotal = new OrderTotal();
orderTotal.setModule(Constants.OT_TOTAL_MODULE_CODE);
orderTotal.setOrderTotalType(OrderTotalType.TOTAL);
orderTotal.setOrderTotalCode("order.total.total");
orderTotal.setTitle(Constants.OT_TOTAL_MODULE_CODE);
// orderTotal.setText("order.total.total");
orderTotal.setSortOrder(500);
orderTotal.setValue(grandTotal);
orderTotals.add(orderTotal);
totalSummary.setTotal(grandTotal);
totalSummary.setTotals(orderTotals);
return totalSummary;
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ShoppingCartServiceImpl method populateShoppingCartItem.
@Override
public ShoppingCartItem populateShoppingCartItem(final Product product) throws ServiceException {
Validate.notNull(product, "Product should not be null");
Validate.notNull(product.getMerchantStore(), "Product.merchantStore should not be null");
ShoppingCartItem item = new ShoppingCartItem(product);
// set item price
FinalPrice price = pricingService.calculateProductPrice(product);
item.setItemPrice(price.getFinalPrice());
return item;
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ProductFacadeV2Impl method getProductPrice.
@Override
public ReadableProductPrice getProductPrice(Long id, ProductPriceRequest priceRequest, MerchantStore store, Language language) {
Validate.notNull(id, "Product id cannot be null");
Validate.notNull(priceRequest, "Product price request cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
try {
Product model = productService.findOne(id, store);
List<ProductAttribute> attributes = null;
if (!CollectionUtils.isEmpty(priceRequest.getOptions())) {
List<Long> attrinutesIds = priceRequest.getOptions().stream().map(p -> p.getId()).collect(Collectors.toList());
attributes = productAttributeService.getByAttributeIds(store, model, attrinutesIds);
for (ProductAttribute attribute : attributes) {
if (attribute.getProduct().getId().longValue() != id.longValue()) {
// throw unauthorized
throw new OperationNotAllowedException("Attribute with id [" + attribute.getId() + "] is not attached to product id [" + id + "]");
}
}
}
if (!StringUtils.isBlank(priceRequest.getSku())) {
// change default availability with sku (instance availability)
List<ProductAvailability> availabilityList = productAvailabilityService.getBySku(priceRequest.getSku(), store);
if (CollectionUtils.isNotEmpty(availabilityList)) {
model.setAvailabilities(new HashSet(availabilityList));
}
}
FinalPrice price;
// attributes can be null;
price = pricingService.calculateProductPrice(model, attributes);
ReadableProductPrice readablePrice = new ReadableProductPrice();
ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
populator.setPricingService(pricingService);
return populator.populate(price, readablePrice, store, language);
} catch (Exception e) {
throw new ServiceRuntimeException("An error occured while getting product price", e);
}
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ProductFacadeImpl method getProductPrice.
@Override
public ReadableProductPrice getProductPrice(Long id, ProductPriceRequest priceRequest, MerchantStore store, Language language) {
Validate.notNull(id, "Product id cannot be null");
Validate.notNull(priceRequest, "Product price request cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
try {
Product model = productService.findOne(id, store);
// TODO check if null
List<Long> attrinutesIds = priceRequest.getOptions().stream().map(p -> p.getId()).collect(Collectors.toList());
List<ProductAttribute> attributes = productAttributeService.getByAttributeIds(store, model, attrinutesIds);
for (ProductAttribute attribute : attributes) {
if (attribute.getProduct().getId().longValue() != id.longValue()) {
// throw unauthorized
throw new OperationNotAllowedException("Attribute with id [" + attribute.getId() + "] is not attached to product id [" + id + "]");
}
}
FinalPrice price;
price = pricingService.calculateProductPrice(model, attributes);
ReadableProductPrice readablePrice = new ReadableProductPrice();
ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
populator.setPricingService(pricingService);
return populator.populate(price, readablePrice, store, language);
} catch (Exception e) {
throw new ServiceRuntimeException("An error occured while getting product price", e);
}
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ReadableProductPopulator method populate.
@Override
public ReadableProduct populate(Product source, ReadableProduct target, MerchantStore store, Language language) throws ConversionException {
Validate.notNull(pricingService, "Requires to set PricingService");
Validate.notNull(imageUtils, "Requires to set imageUtils");
try {
List<com.salesmanager.shop.model.catalog.product.ProductDescription> fulldescriptions = new ArrayList<com.salesmanager.shop.model.catalog.product.ProductDescription>();
if (language == null) {
target = new ReadableProductFull();
}
if (target == null) {
target = new ReadableProduct();
}
ProductDescription description = source.getProductDescription();
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 (target instanceof ReadableProductFull) {
((ReadableProductFull) target).setDescriptions(fulldescriptions);
}
if (language == null) {
language = store.getDefaultLanguage();
}
final Language lang = language;
target.setId(source.getId());
target.setAvailable(source.isAvailable());
target.setProductShipeable(source.isProductShipeable());
ProductSpecification specifications = new ProductSpecification();
specifications.setHeight(source.getProductHeight());
specifications.setLength(source.getProductLength());
specifications.setWeight(source.getProductWeight());
specifications.setWidth(source.getProductWidth());
target.setProductSpecifications(specifications);
target.setPreOrder(source.isPreOrder());
target.setRefSku(source.getRefSku());
target.setSortOrder(source.getSortOrder());
if (source.getType() != null) {
target.setType(this.type(source.getType(), language));
}
if (source.getOwner() != null) {
RentalOwner owner = new RentalOwner();
owner.setId(source.getOwner().getId());
owner.setEmailAddress(source.getOwner().getEmailAddress());
owner.setFirstName(source.getOwner().getBilling().getFirstName());
owner.setLastName(source.getOwner().getBilling().getLastName());
com.salesmanager.shop.model.customer.address.Address address = new com.salesmanager.shop.model.customer.address.Address();
address.setAddress(source.getOwner().getBilling().getAddress());
address.setBillingAddress(true);
address.setCity(source.getOwner().getBilling().getCity());
address.setCompany(source.getOwner().getBilling().getCompany());
address.setCountry(source.getOwner().getBilling().getCountry().getIsoCode());
address.setZone(source.getOwner().getBilling().getZone().getCode());
address.setLatitude(source.getOwner().getBilling().getLatitude());
address.setLongitude(source.getOwner().getBilling().getLongitude());
address.setPhone(source.getOwner().getBilling().getTelephone());
address.setPostalCode(source.getOwner().getBilling().getPostalCode());
owner.setAddress(address);
target.setOwner(owner);
}
if (source.getDateAvailable() != null) {
target.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
}
if (source.getAuditSection() != null) {
target.setCreationDate(DateUtil.formatDate(source.getAuditSection().getDateCreated()));
}
/* if(source.getProductReviewAvg()!=null) {
double avg = source.getProductReviewAvg().doubleValue();
double rating = Math.round(avg * 2) / 2.0f;
target.setRating(rating);
}*/
target.setProductVirtual(source.getProductVirtual());
/* if(source.getProductReviewCount()!=null) {
target.setRatingCount(source.getProductReviewCount().intValue());
}*/
if (description != null) {
com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
target.setDescription(tragetDescription);
}
if (source.getManufacturer() != null) {
ManufacturerDescription manufacturer = source.getManufacturer().getDescriptions().iterator().next();
ReadableManufacturer manufacturerEntity = new ReadableManufacturer();
com.salesmanager.shop.model.catalog.manufacturer.ManufacturerDescription d = new com.salesmanager.shop.model.catalog.manufacturer.ManufacturerDescription();
d.setName(manufacturer.getName());
manufacturerEntity.setDescription(d);
manufacturerEntity.setId(source.getManufacturer().getId());
manufacturerEntity.setOrder(source.getManufacturer().getOrder());
manufacturerEntity.setCode(source.getManufacturer().getCode());
target.setManufacturer(manufacturerEntity);
}
/* if(source.getType() != null) {
ReadableProductType type = new ReadableProductType();
type.setId(source.getType().getId());
type.setCode(source.getType().getCode());
type.setName(source.getType().getCode());//need name
target.setType(type);
}*/
/**
* TODO use ProductImageMapper
*/
Set<ProductImage> images = source.getImages();
if (images != null && images.size() > 0) {
List<ReadableImage> imageList = new ArrayList<ReadableImage>();
String contextPath = imageUtils.getContextPath();
for (ProductImage img : images) {
ReadableImage prdImage = new ReadableImage();
prdImage.setImageName(img.getProductImage());
prdImage.setDefaultImage(img.isDefaultImage());
prdImage.setOrder(img.getSortOrder() != null ? img.getSortOrder().intValue() : 0);
if (img.getImageType() == 1 && img.getProductImageUrl() != null) {
prdImage.setImageUrl(img.getProductImageUrl());
} else {
StringBuilder imgPath = new StringBuilder();
imgPath.append(contextPath).append(imageUtils.buildProductImageUtils(store, source.getSku(), img.getProductImage()));
prdImage.setImageUrl(imgPath.toString());
}
prdImage.setId(img.getId());
prdImage.setImageType(img.getImageType());
if (img.getProductImageUrl() != null) {
prdImage.setExternalUrl(img.getProductImageUrl());
}
if (img.getImageType() == 1 && img.getProductImageUrl() != null) {
// video
prdImage.setVideoUrl(img.getProductImageUrl());
}
if (prdImage.isDefaultImage()) {
target.setImage(prdImage);
}
imageList.add(prdImage);
}
imageList = imageList.stream().sorted(Comparator.comparingInt(ReadableImage::getOrder)).collect(Collectors.toList());
target.setImages(imageList);
}
if (!CollectionUtils.isEmpty(source.getCategories())) {
ReadableCategoryPopulator categoryPopulator = new ReadableCategoryPopulator();
List<ReadableCategory> categoryList = new ArrayList<ReadableCategory>();
for (Category category : source.getCategories()) {
ReadableCategory readableCategory = new ReadableCategory();
categoryPopulator.populate(category, readableCategory, store, language);
categoryList.add(readableCategory);
}
target.setCategories(categoryList);
}
if (!CollectionUtils.isEmpty(source.getAttributes())) {
Set<ProductAttribute> attributes = source.getAttributes();
// split read only and options
// Map<Long,ReadableProductAttribute> readOnlyAttributes = null;
Map<Long, ReadableProductProperty> properties = null;
Map<Long, ReadableProductOption> selectableOptions = null;
if (!CollectionUtils.isEmpty(attributes)) {
for (ProductAttribute attribute : attributes) {
ReadableProductOption opt = null;
ReadableProductAttribute attr = null;
ReadableProductProperty property = null;
ReadableProductPropertyValue propertyValue = null;
ReadableProductOptionValueEntity optValue = new ReadableProductOptionValueEntity();
ReadableProductAttributeValue attrValue = new ReadableProductAttributeValue();
ProductOptionValue optionValue = attribute.getProductOptionValue();
if (attribute.getAttributeDisplayOnly()) {
// read only attribute = property
/*
if(readOnlyAttributes==null) {
readOnlyAttributes = new TreeMap<Long,ReadableProductAttribute>();
}
attr = readOnlyAttributes.get(attribute.getProductOption().getId());
if(attr==null) {
attr = createAttribute(attribute, language);
}
if(attr!=null) {
readOnlyAttributes.put(attribute.getProductOption().getId(), attr);
}
attrValue.setDefaultValue(attribute.getAttributeDefault());
if(attribute.getProductOptionValue()!=null) {
attrValue.setId(attribute.getProductOptionValue().getId());//id of the option value
} else {
attrValue.setId(attribute.getId());
}
attrValue.setLang(language.getCode());
attrValue.setSortOrder(0);
if(attribute.getProductOptionSortOrder()!=null) {
attrValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
}
List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
ProductOptionValueDescription podescription = null;
if(podescriptions!=null && podescriptions.size()>0) {
podescription = podescriptions.get(0);
if(podescriptions.size()>1) {
for(ProductOptionValueDescription optionValueDescription : podescriptions) {
if(optionValueDescription.getLanguage().getId().intValue()==language.getId().intValue()) {
podescription = optionValueDescription;
break;
}
}
}
}
attrValue.setName(podescription.getName());
attrValue.setDescription(podescription.getDescription());
if(attr!=null) {
attr.getAttributeValues().add(attrValue);
}
*/
// if(properties==null) {
// properties = new TreeMap<Long,ReadableProductProperty>();
// }
// property = properties.get(attribute.getProductOption().getId());
// if(property==null) {
property = createProperty(attribute, language);
// that is the property
ReadableProductOption readableOption = new ReadableProductOption();
ReadableProductPropertyValue readableOptionValue = new ReadableProductPropertyValue();
readableOption.setCode(attribute.getProductOption().getCode());
readableOption.setId(attribute.getProductOption().getId());
Set<ProductOptionDescription> podescriptions = attribute.getProductOption().getDescriptions();
if (podescriptions != null && podescriptions.size() > 0) {
for (ProductOptionDescription optionDescription : podescriptions) {
if (optionDescription.getLanguage().getCode().equals(language.getCode())) {
readableOption.setName(optionDescription.getName());
}
}
}
property.setProperty(readableOption);
Set<ProductOptionValueDescription> povdescriptions = attribute.getProductOptionValue().getDescriptions();
readableOptionValue.setId(attribute.getProductOptionValue().getId());
if (povdescriptions != null && povdescriptions.size() > 0) {
for (ProductOptionValueDescription optionValueDescription : povdescriptions) {
if (optionValueDescription.getLanguage().getCode().equals(language.getCode())) {
readableOptionValue.setName(optionValueDescription.getName());
}
}
}
property.setPropertyValue(readableOptionValue);
// } else{
// properties.put(attribute.getProductOption().getId(), property);
// }
/* propertyValue.setCode(attribute.getProductOptionValue().getCode());
propertyValue.setId(attribute.getProductOptionValue().getId());
propertyValue.setSortOrder(0);
if(attribute.getProductOptionSortOrder()!=null) {
propertyValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
}
List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
if(podescriptions!=null && podescriptions.size()>0) {
for(ProductOptionValueDescription optionValueDescription : podescriptions) {
com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription desc = new com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription();
desc.setId(optionValueDescription.getId());
desc.setName(optionValueDescription.getName());
propertyValue.getValues().add(desc);
}
}
property.setPropertyValue(propertyValue);*/
// if(attr!=null) {
// attr.getAttributeValues().add(attrValue);
// }
target.getProperties().add(property);
} else {
if (selectableOptions == null) {
selectableOptions = new TreeMap<Long, ReadableProductOption>();
}
opt = selectableOptions.get(attribute.getProductOption().getId());
if (opt == null) {
opt = createOption(attribute, language);
}
if (opt != null) {
selectableOptions.put(attribute.getProductOption().getId(), opt);
}
optValue.setDefaultValue(attribute.getAttributeDefault());
// optValue.setId(attribute.getProductOptionValue().getId());
optValue.setId(attribute.getId());
optValue.setCode(attribute.getProductOptionValue().getCode());
com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription valueDescription = new com.salesmanager.shop.model.catalog.product.attribute.ProductOptionValueDescription();
valueDescription.setLanguage(language.getCode());
// optValue.setLang(language.getCode());
if (attribute.getProductAttributePrice() != null && attribute.getProductAttributePrice().doubleValue() > 0) {
String formatedPrice = pricingService.getDisplayAmount(attribute.getProductAttributePrice(), store);
optValue.setPrice(formatedPrice);
}
if (!StringUtils.isBlank(attribute.getProductOptionValue().getProductOptionValueImage())) {
optValue.setImage(imageUtils.buildProductPropertyImageUtils(store, attribute.getProductOptionValue().getProductOptionValueImage()));
}
optValue.setSortOrder(0);
if (attribute.getProductOptionSortOrder() != null) {
optValue.setSortOrder(attribute.getProductOptionSortOrder().intValue());
}
List<ProductOptionValueDescription> podescriptions = optionValue.getDescriptionsSettoList();
ProductOptionValueDescription podescription = null;
if (podescriptions != null && podescriptions.size() > 0) {
podescription = podescriptions.get(0);
if (podescriptions.size() > 1) {
for (ProductOptionValueDescription optionValueDescription : podescriptions) {
if (optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
podescription = optionValueDescription;
break;
}
}
}
}
valueDescription.setName(podescription.getName());
valueDescription.setDescription(podescription.getDescription());
optValue.setDescription(valueDescription);
if (opt != null) {
opt.getOptionValues().add(optValue);
}
}
}
}
if (selectableOptions != null) {
List<ReadableProductOption> options = new ArrayList<ReadableProductOption>(selectableOptions.values());
target.setOptions(options);
}
}
// remove products from invisible category -> set visible = false
/* Set<Category> categories = source.getCategories();
boolean isVisible = true;
if(!CollectionUtils.isEmpty(categories)) {
for(Category c : categories) {
if(c.isVisible()) {
isVisible = true;
break;
} else {
isVisible = false;
}
}
}*/
// target.setVisible(isVisible);
// availability
ProductAvailability availability = null;
for (ProductAvailability a : source.getAvailabilities()) {
// TODO validate region
// if(availability.getRegion().equals(Constants.ALL_REGIONS)) {//TODO REL 2.1 accept a region
availability = a;
target.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
target.setQuantityOrderMaximum(availability.getProductQuantityOrderMax() == null ? 1 : availability.getProductQuantityOrderMax());
target.setQuantityOrderMinimum(availability.getProductQuantityOrderMin() == null ? 1 : availability.getProductQuantityOrderMin());
if (availability.getProductQuantity().intValue() > 0 && target.isAvailable()) {
target.setCanBePurchased(true);
}
// }
}
target.setSku(source.getSku());
FinalPrice price = pricingService.calculateProductPrice(source);
if (price != null) {
target.setFinalPrice(pricingService.getDisplayAmount(price.getFinalPrice(), store));
target.setPrice(price.getFinalPrice());
target.setOriginalPrice(pricingService.getDisplayAmount(price.getOriginalPrice(), store));
if (price.isDiscounted()) {
target.setDiscounted(true);
}
// price appender
if (availability != null) {
Set<ProductPrice> prices = availability.getPrices();
if (!CollectionUtils.isEmpty(prices)) {
ReadableProductPrice readableProductPrice = new ReadableProductPrice();
readableProductPrice.setDiscounted(target.isDiscounted());
readableProductPrice.setFinalPrice(target.getFinalPrice());
readableProductPrice.setOriginalPrice(target.getOriginalPrice());
Optional<ProductPrice> pr = prices.stream().filter(p -> p.getCode().equals(ProductPrice.DEFAULT_PRICE_CODE)).findFirst();
target.setProductPrice(readableProductPrice);
if (pr.isPresent()) {
readableProductPrice.setId(pr.get().getId());
Optional<ProductPriceDescription> d = pr.get().getDescriptions().stream().filter(desc -> desc.getLanguage().getCode().equals(lang.getCode())).findFirst();
if (d.isPresent()) {
com.salesmanager.shop.model.catalog.product.ProductPriceDescription priceDescription = new com.salesmanager.shop.model.catalog.product.ProductPriceDescription();
priceDescription.setLanguage(language.getCode());
priceDescription.setId(d.get().getId());
priceDescription.setPriceAppender(d.get().getPriceAppender());
readableProductPrice.setDescription(priceDescription);
}
}
}
}
}
if (target instanceof ReadableProductFull) {
((ReadableProductFull) target).setDescriptions(fulldescriptions);
}
return target;
} catch (Exception e) {
throw new ConversionException(e);
}
}
Aggregations