use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ReadableMinimalProductMapper method merge.
@Override
public ReadableMinimalProduct merge(Product source, ReadableMinimalProduct destination, MerchantStore store, Language language) {
Validate.notNull(source, "Product cannot be null");
Validate.notNull(destination, "ReadableMinimalProduct cannot be null");
for (ProductDescription desc : source.getDescriptions()) {
if (language != null && desc.getLanguage() != null && desc.getLanguage().getId().intValue() == language.getId().intValue()) {
destination.setDescription(this.description(desc));
break;
}
}
destination.setId(source.getId());
destination.setAvailable(source.isAvailable());
destination.setProductShipeable(source.isProductShipeable());
ProductSpecification specifications = new ProductSpecification();
specifications.setHeight(source.getProductHeight());
specifications.setLength(source.getProductLength());
specifications.setWeight(source.getProductWeight());
specifications.setWidth(source.getProductWidth());
destination.setProductSpecifications(specifications);
destination.setPreOrder(source.isPreOrder());
destination.setRefSku(source.getRefSku());
destination.setSortOrder(source.getSortOrder());
destination.setSku(source.getSku());
if (source.getDateAvailable() != null) {
destination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
}
if (source.getProductReviewAvg() != null) {
double avg = source.getProductReviewAvg().doubleValue();
double rating = Math.round(avg * 2) / 2.0f;
destination.setRating(rating);
}
destination.setProductVirtual(source.getProductVirtual());
if (source.getProductReviewCount() != null) {
destination.setRatingCount(source.getProductReviewCount().intValue());
}
try {
FinalPrice price = pricingService.calculateProductPrice(source);
if (price != null) {
destination.setFinalPrice(pricingService.getDisplayAmount(price.getFinalPrice(), store));
destination.setPrice(price.getFinalPrice());
destination.setOriginalPrice(pricingService.getDisplayAmount(price.getOriginalPrice(), store));
}
} catch (ServiceException e) {
throw new ConversionRuntimeException("An error occured during price calculation", e);
}
// image
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());
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()) {
destination.setImage(prdImage);
}
imageList.add(prdImage);
}
destination.setImages(imageList);
}
return null;
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ReadableProductMapper method merge.
@Override
public ReadableProduct merge(Product source, ReadableProduct destination, MerchantStore store, Language language) {
Validate.notNull(source, "Product cannot be null");
Validate.notNull(destination, "Product destination cannot be null");
destination.setSku(source.getSku());
destination.setRefSku(source.getRefSku());
destination.setId(source.getId());
destination.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;
}
}
destination.setId(source.getId());
destination.setAvailable(source.isAvailable());
destination.setProductShipeable(source.isProductShipeable());
ProductSpecification specifications = new ProductSpecification();
specifications.setHeight(source.getProductHeight());
specifications.setLength(source.getProductLength());
specifications.setWeight(source.getProductWeight());
specifications.setWidth(source.getProductWidth());
destination.setProductSpecifications(specifications);
destination.setPreOrder(source.isPreOrder());
destination.setRefSku(source.getRefSku());
destination.setSortOrder(source.getSortOrder());
if (source.getType() != null) {
ReadableProductType readableType = readableProductTypeMapper.convert(source.getType(), store, language);
destination.setType(readableType);
}
if (source.getDateAvailable() != null) {
destination.setDateAvailable(DateUtil.formatDate(source.getDateAvailable()));
}
if (source.getAuditSection() != null) {
destination.setCreationDate(DateUtil.formatDate(source.getAuditSection().getDateCreated()));
}
destination.setProductVirtual(source.getProductVirtual());
if (source.getProductReviewCount() != null) {
destination.setRatingCount(source.getProductReviewCount().intValue());
}
if (source.getManufacturer() != null) {
ReadableManufacturer manufacturer = readableManufacturerMapper.convert(source.getManufacturer(), store, language);
destination.setManufacturer(manufacturer);
}
// 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());
destination.setImages(imageList);
}
// read only product values
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();
// we need to set readonly attributes only
if (attribute.getAttributeDisplayOnly()) {
// read only attribute = property
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);
destination.getProperties().add(property);
}
if (selectableOptions != null) {
List<ReadableProductOption> options = new ArrayList<ReadableProductOption>(selectableOptions.values());
destination.setOptions(options);
}
}
}
}
}
// 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;
destination.setQuantity(availability.getProductQuantity() == null ? 1 : availability.getProductQuantity());
destination.setQuantityOrderMaximum(availability.getProductQuantityOrderMax() == null ? 1 : availability.getProductQuantityOrderMax());
destination.setQuantityOrderMinimum(availability.getProductQuantityOrderMin() == null ? 1 : availability.getProductQuantityOrderMin());
if (availability.getProductQuantity().intValue() > 0 && destination.isAvailable()) {
destination.setCanBePurchased(true);
}
// }
}
destination.setSku(source.getSku());
try {
FinalPrice price = pricingService.calculateProductPrice(source);
if (price != null) {
destination.setFinalPrice(pricingService.getDisplayAmount(price.getFinalPrice(), store));
destination.setPrice(price.getFinalPrice());
destination.setOriginalPrice(pricingService.getDisplayAmount(price.getOriginalPrice(), store));
if (price.isDiscounted()) {
destination.setDiscounted(true);
}
// price appender
if (availability != null) {
Set<ProductPrice> prices = availability.getPrices();
if (!CollectionUtils.isEmpty(prices)) {
ReadableProductPrice readableProductPrice = new ReadableProductPrice();
readableProductPrice.setDiscounted(destination.isDiscounted());
readableProductPrice.setFinalPrice(destination.getFinalPrice());
readableProductPrice.setOriginalPrice(destination.getOriginalPrice());
Optional<ProductPrice> pr = prices.stream().filter(p -> p.getCode().equals(ProductPrice.DEFAULT_PRICE_CODE)).findFirst();
destination.setProductPrice(readableProductPrice);
if (pr.isPresent()) {
readableProductPrice.setId(pr.get().getId());
Optional<ProductPriceDescription> d = pr.get().getDescriptions().stream().filter(desc -> desc.getLanguage().getCode().equals(language.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);
}
}
}
}
}
} catch (Exception e) {
throw new ConversionRuntimeException("An error while converting product price", e);
}
if (source.getProductReviewAvg() != null) {
double avg = source.getProductReviewAvg().doubleValue();
double rating = Math.round(avg * 2) / 2.0f;
destination.setRating(rating);
}
if (source.getProductReviewCount() != null) {
destination.setRatingCount(source.getProductReviewCount().intValue());
}
if (description != null) {
com.salesmanager.shop.model.catalog.product.ProductDescription tragetDescription = populateDescription(description);
destination.setDescription(tragetDescription);
}
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);
}
destination.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()));
}
destination.setProductSpecifications(specifications);
destination.setSortOrder(source.getSortOrder());
return destination;
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice 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);
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice 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);
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ProductPriceUtils method finalPrice.
private FinalPrice finalPrice(ProductPrice price) {
FinalPrice finalPrice = new FinalPrice();
BigDecimal fPrice = price.getProductPriceAmount();
BigDecimal oPrice = price.getProductPriceAmount();
Date today = new Date();
// calculate discount price
boolean hasDiscount = false;
if (price.getProductPriceSpecialStartDate() != null || price.getProductPriceSpecialEndDate() != null) {
if (price.getProductPriceSpecialStartDate() != null) {
if (price.getProductPriceSpecialStartDate().before(today)) {
if (price.getProductPriceSpecialEndDate() != null) {
if (price.getProductPriceSpecialEndDate().after(today)) {
hasDiscount = true;
fPrice = price.getProductPriceSpecialAmount();
finalPrice.setDiscountEndDate(price.getProductPriceSpecialEndDate());
}
}
}
}
if (!hasDiscount && price.getProductPriceSpecialStartDate() == null && price.getProductPriceSpecialEndDate() != null) {
if (price.getProductPriceSpecialEndDate().after(today)) {
hasDiscount = true;
fPrice = price.getProductPriceSpecialAmount();
finalPrice.setDiscountEndDate(price.getProductPriceSpecialEndDate());
}
}
} else {
if (price.getProductPriceSpecialAmount() != null && price.getProductPriceSpecialAmount().doubleValue() > 0) {
hasDiscount = true;
fPrice = price.getProductPriceSpecialAmount();
finalPrice.setDiscountEndDate(price.getProductPriceSpecialEndDate());
}
}
finalPrice.setProductPrice(price);
finalPrice.setFinalPrice(fPrice);
finalPrice.setOriginalPrice(oPrice);
if (price.isDefaultPrice()) {
finalPrice.setDefaultPrice(true);
}
if (hasDiscount) {
discountPrice(finalPrice);
}
return finalPrice;
}
Aggregations