use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute 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;
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.
the class PersistableProductDefinitionMapper method merge.
@Override
public Product merge(PersistableProductDefinition source, Product destination, MerchantStore store, Language language) {
Validate.notNull(destination, "Product must not be null");
try {
destination.setSku(source.getIdentifier());
destination.setAvailable(source.isVisible());
destination.setDateAvailable(new Date());
destination.setRefSku(source.getIdentifier());
if (source.getId() != null && source.getId().longValue() == 0) {
destination.setId(null);
} else {
destination.setId(source.getId());
}
// MANUFACTURER
if (!StringUtils.isBlank(source.getManufacturer())) {
Manufacturer manufacturer = manufacturerService.getByCode(store, source.getManufacturer());
if (manufacturer == null) {
throw new ConversionException("Manufacturer [" + source.getManufacturer() + "] does not exist");
}
destination.setManufacturer(manufacturer);
}
// PRODUCT TYPE
if (!StringUtils.isBlank(source.getType())) {
ProductType type = productTypeService.getByCode(source.getType(), store, language);
if (type == null) {
throw new ConversionException("Product type [" + source.getType() + "] does not exist");
}
destination.setType(type);
}
if (!StringUtils.isBlank(source.getDateAvailable())) {
destination.setDateAvailable(DateUtil.getDate(source.getDateAvailable()));
}
destination.setMerchantStore(store);
List<Language> languages = new ArrayList<Language>();
Set<ProductDescription> descriptions = new HashSet<ProductDescription>();
if (!CollectionUtils.isEmpty(source.getDescriptions())) {
for (com.salesmanager.shop.model.catalog.product.ProductDescription description : source.getDescriptions()) {
ProductDescription productDescription = new ProductDescription();
Language lang = languageService.getByCode(description.getLanguage());
if (lang == null) {
throw new ConversionException("Language code " + description.getLanguage() + " is invalid, use ISO code (en, fr ...)");
}
if (!CollectionUtils.isEmpty(destination.getDescriptions())) {
for (ProductDescription desc : destination.getDescriptions()) {
if (desc.getLanguage().getCode().equals(description.getLanguage())) {
productDescription = desc;
break;
}
}
}
productDescription.setProduct(destination);
productDescription.setDescription(description.getDescription());
productDescription.setProductHighlight(description.getHighlights());
productDescription.setName(description.getName());
productDescription.setSeUrl(description.getFriendlyUrl());
productDescription.setMetatagKeywords(description.getKeyWords());
productDescription.setMetatagDescription(description.getMetaDescription());
productDescription.setTitle(description.getTitle());
languages.add(lang);
productDescription.setLanguage(lang);
descriptions.add(productDescription);
}
}
if (descriptions.size() > 0) {
destination.setDescriptions(descriptions);
}
// if(source.getRating() != null) {
// destination.setProductReviewAvg(new BigDecimal(source.getRating()));
// }
// destination.setProductReviewCount(source.getRatingCount());
/**
* Product definition
*/
ProductAvailability productAvailability = null;
ProductPrice defaultPrice = null;
if (!CollectionUtils.isEmpty(destination.getAvailabilities())) {
for (ProductAvailability avail : destination.getAvailabilities()) {
Set<ProductPrice> prices = avail.getPrices();
for (ProductPrice p : prices) {
if (p.isDefaultPrice()) {
if (productAvailability == null) {
productAvailability = avail;
defaultPrice = p;
productAvailability.setProductQuantity(source.getQuantity());
productAvailability.setProductStatus(source.isCanBePurchased());
p.setProductPriceAmount(source.getPrice());
break;
}
}
}
}
}
if (productAvailability == null) {
// create with default values
productAvailability = new ProductAvailability(destination, store);
destination.getAvailabilities().add(productAvailability);
productAvailability.setProductQuantity(source.getQuantity());
productAvailability.setProductQuantityOrderMin(1);
productAvailability.setProductQuantityOrderMax(1);
productAvailability.setRegion(Constants.ALL_REGIONS);
productAvailability.setAvailable(Boolean.valueOf(destination.isAvailable()));
productAvailability.setProductStatus(source.isCanBePurchased());
}
if (defaultPrice == null) {
defaultPrice = new ProductPrice();
defaultPrice.setDefaultPrice(true);
defaultPrice.setProductPriceAmount(source.getPrice());
defaultPrice.setCode(ProductPriceEntity.DEFAULT_PRICE_CODE);
defaultPrice.setProductAvailability(productAvailability);
productAvailability.getPrices().add(defaultPrice);
for (Language lang : languages) {
ProductPriceDescription ppd = new ProductPriceDescription();
ppd.setProductPrice(defaultPrice);
ppd.setLanguage(lang);
ppd.setName(ProductPriceDescription.DEFAULT_PRICE_DESCRIPTION);
defaultPrice.getDescriptions().add(ppd);
}
}
if (source.getProductSpecifications() != null) {
destination.setProductHeight(source.getProductSpecifications().getHeight());
destination.setProductLength(source.getProductSpecifications().getLength());
destination.setProductWeight(source.getProductSpecifications().getWeight());
destination.setProductWidth(source.getProductSpecifications().getWidth());
if (source.getProductSpecifications().getManufacturer() != null) {
Manufacturer manuf = null;
if (!StringUtils.isBlank(source.getProductSpecifications().getManufacturer())) {
manuf = manufacturerService.getByCode(store, source.getProductSpecifications().getManufacturer());
}
if (manuf == null) {
throw new ConversionException("Invalid manufacturer id");
}
if (manuf != null) {
if (manuf.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid manufacturer id");
}
destination.setManufacturer(manuf);
}
}
}
destination.setSortOrder(source.getSortOrder());
destination.setProductVirtual(source.isVirtual());
destination.setProductShipeable(source.isShipeable());
// attributes
if (source.getProperties() != null) {
for (com.salesmanager.shop.model.catalog.product.attribute.PersistableProductAttribute attr : source.getProperties()) {
ProductAttribute attribute = persistableProductAttributeMapper.convert(attr, store, language);
attribute.setProduct(destination);
destination.getAttributes().add(attribute);
}
}
// categories
if (!CollectionUtils.isEmpty(source.getCategories())) {
for (com.salesmanager.shop.model.catalog.category.Category categ : source.getCategories()) {
Category c = null;
if (!StringUtils.isBlank(categ.getCode())) {
c = categoryService.getByCode(store, categ.getCode());
} else {
Validate.notNull(categ.getId(), "Category id nust not be null");
c = categoryService.getById(categ.getId(), store.getId());
}
if (c == null) {
throw new ConversionException("Category id " + categ.getId() + " does not exist");
}
if (c.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid category id");
}
destination.getCategories().add(c);
}
}
return destination;
} catch (Exception e) {
throw new ConversionRuntimeException("Error converting product mapper", e);
}
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.
the class PackingBox method getBoxPackagesDetails.
@Override
public List<PackageDetails> getBoxPackagesDetails(List<ShippingProduct> products, MerchantStore store) throws ServiceException {
if (products == null) {
throw new ServiceException("Product list cannot be null !!");
}
double width = 0;
double length = 0;
double height = 0;
double weight = 0;
double maxweight = 0;
// int treshold = 0;
ShippingConfiguration shippingConfiguration = shippingService.getShippingConfiguration(store);
if (shippingConfiguration == null) {
throw new ServiceException("ShippingConfiguration not found for merchant " + store.getCode());
}
width = (double) shippingConfiguration.getBoxWidth();
length = (double) shippingConfiguration.getBoxLength();
height = (double) shippingConfiguration.getBoxHeight();
weight = shippingConfiguration.getBoxWeight();
maxweight = shippingConfiguration.getMaxWeight();
List<PackageDetails> boxes = new ArrayList<PackageDetails>();
// maximum number of boxes
int maxBox = 100;
int iterCount = 0;
List<Product> individualProducts = new ArrayList<Product>();
// need to put items individually
for (ShippingProduct shippingProduct : products) {
Product product = shippingProduct.getProduct();
if (product.isProductVirtual()) {
continue;
}
int qty = shippingProduct.getQuantity();
Set<ProductAttribute> attrs = shippingProduct.getProduct().getAttributes();
// set attributes values
BigDecimal w = product.getProductWeight();
BigDecimal h = product.getProductHeight();
BigDecimal l = product.getProductLength();
BigDecimal wd = product.getProductWidth();
if (w == null) {
w = new BigDecimal(defaultWeight);
}
if (h == null) {
h = new BigDecimal(defaultHeight);
}
if (l == null) {
l = new BigDecimal(defaultLength);
}
if (wd == null) {
wd = new BigDecimal(defaultWidth);
}
if (attrs != null && attrs.size() > 0) {
for (ProductAttribute attribute : attrs) {
if (attribute.getProductAttributeWeight() != null) {
w = w.add(attribute.getProductAttributeWeight());
}
}
}
if (qty > 1) {
for (int i = 1; i <= qty; i++) {
Product temp = new Product();
temp.setProductHeight(h);
temp.setProductLength(l);
temp.setProductWidth(wd);
temp.setProductWeight(w);
temp.setAttributes(product.getAttributes());
temp.setDescriptions(product.getDescriptions());
individualProducts.add(temp);
}
} else {
Product temp = new Product();
temp.setProductHeight(h);
temp.setProductLength(l);
temp.setProductWidth(wd);
temp.setProductWeight(w);
temp.setAttributes(product.getAttributes());
temp.setDescriptions(product.getDescriptions());
individualProducts.add(temp);
}
iterCount++;
}
if (iterCount == 0) {
return null;
}
int productCount = individualProducts.size();
List<PackingBox> boxesList = new ArrayList<PackingBox>();
// start the creation of boxes
PackingBox box = new PackingBox();
// set box max volume
double maxVolume = width * length * height;
if (maxVolume == 0 || maxweight == 0) {
merchantLogService.save(new MerchantLog(store, "shipping", "Check shipping box configuration, it has a volume of " + maxVolume + " and a maximum weight of " + maxweight + ". Those values must be greater than 0."));
throw new ServiceException("Product configuration exceeds box configuraton");
}
box.setVolumeLeft(maxVolume);
box.setWeightLeft(maxweight);
// assign first box
boxesList.add(box);
// int boxCount = 1;
List<Product> assignedProducts = new ArrayList<Product>();
// calculate the volume for the next object
if (assignedProducts.size() > 0) {
individualProducts.removeAll(assignedProducts);
assignedProducts = new ArrayList<Product>();
}
boolean productAssigned = false;
for (Product p : individualProducts) {
// Set<ProductAttribute> attributes = p.getAttributes();
productAssigned = false;
double productWeight = p.getProductWeight().doubleValue();
// validate if product fits in the box
if (p.getProductWidth().doubleValue() > width || p.getProductHeight().doubleValue() > height || p.getProductLength().doubleValue() > length) {
// log message to customer
merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has a demension larger than the box size specified. Will use per item calculation."));
throw new ServiceException("Product configuration exceeds box configuraton");
}
if (productWeight > maxweight) {
merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has a weight larger than the box maximum weight specified. Will use per item calculation."));
throw new ServiceException("Product configuration exceeds box configuraton");
}
double productVolume = (p.getProductWidth().doubleValue() * p.getProductHeight().doubleValue() * p.getProductLength().doubleValue());
if (productVolume == 0) {
merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has one of the dimension set to 0 and therefore cannot calculate the volume"));
throw new ServiceException("Product configuration exceeds box configuraton");
}
if (productVolume > maxVolume) {
throw new ServiceException("Product configuration exceeds box configuraton");
}
// Iterator boxIter = boxesList.iterator();
for (PackingBox pbox : boxesList) {
double volumeLeft = pbox.getVolumeLeft();
double weightLeft = pbox.getWeightLeft();
if ((volumeLeft * .75) >= productVolume && pbox.getWeightLeft() >= productWeight) {
// fit the item
// in this
// box
// fit in the current box
volumeLeft = volumeLeft - productVolume;
pbox.setVolumeLeft(volumeLeft);
weightLeft = weightLeft - productWeight;
pbox.setWeightLeft(weightLeft);
assignedProducts.add(p);
productCount--;
double w = pbox.getWeight();
w = w + productWeight;
pbox.setWeight(w);
productAssigned = true;
maxBox--;
break;
}
}
if (!productAssigned) {
// create a new box
box = new PackingBox();
// set box max volume
box.setVolumeLeft(maxVolume);
box.setWeightLeft(maxweight);
boxesList.add(box);
double volumeLeft = box.getVolumeLeft() - productVolume;
box.setVolumeLeft(volumeLeft);
double weightLeft = box.getWeightLeft() - productWeight;
box.setWeightLeft(weightLeft);
assignedProducts.add(p);
productCount--;
double w = box.getWeight();
w = w + productWeight;
box.setWeight(w);
maxBox--;
}
}
// now prepare the shipping info
// number of boxes
// Iterator ubIt = usedBoxesList.iterator();
System.out.println("###################################");
System.out.println("Number of boxes " + boxesList.size());
System.out.println("###################################");
for (PackingBox pb : boxesList) {
PackageDetails details = new PackageDetails();
details.setShippingHeight(height);
details.setShippingLength(length);
details.setShippingWeight(weight + box.getWeight());
details.setShippingWidth(width);
details.setItemName(store.getCode());
boxes.add(details);
}
return boxes;
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute in project shopizer by shopizer-ecommerce.
the class ProductTest method testAttributes.
private void testAttributes(Product product) throws Exception {
/**
* An attribute can be created dynamicaly but the attached Option and Option value need to exist
*/
MerchantStore store = product.getMerchantStore();
Language en = languageService.getByCode("en");
/**
* Create size option
*/
ProductOption color = new ProductOption();
color.setMerchantStore(store);
color.setCode("COLOR");
color.setProductOptionType(ProductOptionType.Radio.name());
ProductOptionDescription optionDescription = new ProductOptionDescription();
optionDescription.setLanguage(en);
optionDescription.setName("Color");
optionDescription.setDescription("Color of an item");
optionDescription.setProductOption(color);
color.getDescriptions().add(optionDescription);
// create option
productOptionService.saveOrUpdate(color);
/**
* Create size option
*/
ProductOption size = new ProductOption();
size.setMerchantStore(store);
size.setCode("SIZE");
size.setProductOptionType(ProductOptionType.Radio.name());
ProductOptionDescription sizeDescription = new ProductOptionDescription();
sizeDescription.setLanguage(en);
sizeDescription.setName("Size");
sizeDescription.setDescription("Size of an item");
sizeDescription.setProductOption(size);
size.getDescriptions().add(sizeDescription);
// create option
productOptionService.saveOrUpdate(size);
// option value
ProductOptionValue red = new ProductOptionValue();
red.setMerchantStore(store);
red.setCode("red");
ProductOptionValueDescription redDescription = new ProductOptionValueDescription();
redDescription.setLanguage(en);
redDescription.setName("Red");
redDescription.setDescription("Red color");
redDescription.setProductOptionValue(red);
red.getDescriptions().add(redDescription);
// create an option value
productOptionValueService.saveOrUpdate(red);
// another option value
ProductOptionValue blue = new ProductOptionValue();
blue.setMerchantStore(store);
blue.setCode("blue");
ProductOptionValueDescription blueDescription = new ProductOptionValueDescription();
blueDescription.setLanguage(en);
blueDescription.setName("Blue");
blueDescription.setDescription("Color blue");
blueDescription.setProductOptionValue(blue);
blue.getDescriptions().add(blueDescription);
// create another option value
productOptionValueService.saveOrUpdate(blue);
// option value
ProductOptionValue small = new ProductOptionValue();
small.setMerchantStore(store);
small.setCode("small");
ProductOptionValueDescription smallDescription = new ProductOptionValueDescription();
smallDescription.setLanguage(en);
smallDescription.setName("Small");
smallDescription.setDescription("Small size");
smallDescription.setProductOptionValue(small);
small.getDescriptions().add(smallDescription);
// create an option value
productOptionValueService.saveOrUpdate(small);
// another option value
ProductOptionValue medium = new ProductOptionValue();
medium.setMerchantStore(store);
medium.setCode("medium");
ProductOptionValueDescription mediumDescription = new ProductOptionValueDescription();
mediumDescription.setLanguage(en);
mediumDescription.setName("Medium");
mediumDescription.setDescription("Medium size");
mediumDescription.setProductOptionValue(medium);
medium.getDescriptions().add(mediumDescription);
// create another option value
productOptionValueService.saveOrUpdate(medium);
ProductAttribute color_blue = new ProductAttribute();
color_blue.setProduct(product);
color_blue.setProductOption(color);
color_blue.setAttributeDefault(true);
// no price variation
color_blue.setProductAttributePrice(new BigDecimal(0));
// weight variation
color_blue.setProductAttributeWeight(new BigDecimal(1));
color_blue.setProductOptionValue(blue);
productAttributeService.create(color_blue);
product.getAttributes().add(color_blue);
/**
* create attributes *
*/
// attributes
ProductAttribute color_red = new ProductAttribute();
color_red.setProduct(product);
color_red.setProductOption(color);
color_red.setAttributeDefault(true);
// no price variation
color_red.setProductAttributePrice(new BigDecimal(0));
// weight variation
color_red.setProductAttributeWeight(new BigDecimal(1));
color_red.setProductOptionValue(red);
productAttributeService.create(color_red);
product.getAttributes().add(color_red);
ProductAttribute smallAttr = new ProductAttribute();
smallAttr.setProduct(product);
smallAttr.setProductOption(size);
smallAttr.setAttributeDefault(true);
// no price variation
smallAttr.setProductAttributePrice(new BigDecimal(0));
// weight variation
smallAttr.setProductAttributeWeight(new BigDecimal(1));
smallAttr.setProductOptionValue(small);
productAttributeService.create(smallAttr);
product.getAttributes().add(smallAttr);
productService.update(product);
/**
* get options facets
*/
List<ProductAttribute> attributes = productAttributeService.getProductAttributesByCategoryLineage(store, product.getCategories().iterator().next().getLineage(), en);
Assert.assertTrue((long) attributes.size() > 0);
}
use of com.salesmanager.core.model.catalog.product.attribute.ProductAttribute 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;
}
Aggregations