use of com.salesmanager.shop.model.catalog.product.ReadableProductPrice 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.shop.model.catalog.product.ReadableProductPrice in project shopizer by shopizer-ecommerce.
the class ReadableInventoryMapper method prices.
private List<ReadableProductPrice> prices(ProductAvailability source, MerchantStore store, Language language) throws ConversionException {
ReadableProductPricePopulator populator = null;
List<ReadableProductPrice> prices = new ArrayList<ReadableProductPrice>();
for (ProductPrice price : source.getPrices()) {
populator = new ReadableProductPricePopulator();
populator.setPricingService(pricingService);
ReadableProductPrice p = populator.populate(price, new ReadableProductPrice(), store, language);
prices.add(p);
}
return prices;
}
use of com.salesmanager.shop.model.catalog.product.ReadableProductPrice in project shopizer by shopizer-ecommerce.
the class ReadableInventoryMapper method merge.
@Override
public ReadableInventory merge(ProductAvailability source, ReadableInventory destination, MerchantStore store, Language language) {
Validate.notNull(destination, "Destination Product availability cannot be null");
Validate.notNull(source, "Source Product availability cannot be null");
try {
destination.setQuantity(source.getProductQuantity() != null ? source.getProductQuantity().intValue() : 0);
destination.setProductQuantityOrderMax(source.getProductQuantityOrderMax() != null ? source.getProductQuantityOrderMax().intValue() : 0);
destination.setProductQuantityOrderMin(source.getProductQuantityOrderMin() != null ? source.getProductQuantityOrderMin().intValue() : 0);
destination.setOwner(source.getOwner());
destination.setId(source.getId());
destination.setRegion(source.getRegion());
destination.setRegionVariant(source.getRegionVariant());
destination.setStore(store(store, language));
if (source.getAvailable() != null) {
if (source.getProductDateAvailable() != null) {
boolean isAfter = LocalDate.parse(DateUtil.getPresentDate()).isAfter(LocalDate.parse(DateUtil.formatDate(source.getProductDateAvailable())));
if (isAfter && source.getAvailable().booleanValue()) {
destination.setAvailable(true);
}
destination.setDateAvailable(DateUtil.formatDate(source.getProductDateAvailable()));
} else {
destination.setAvailable(source.getAvailable().booleanValue());
}
}
if (source.getAuditSection() != null) {
if (source.getAuditSection().getDateCreated() != null) {
destination.setCreationDate(DateUtil.formatDate(source.getAuditSection().getDateCreated()));
}
}
List<ReadableProductPrice> prices = prices(source, store, language);
destination.setPrices(prices);
// not necessary when getting an inventory
// if(source.getProductInstance() != null) {
// destination.setInstance(readableProductInstanceMapper.convert(source.getProductInstance(), store, language));
// }
} catch (Exception e) {
throw new ConversionRuntimeException("Error while converting Inventory", e);
}
return destination;
}
use of com.salesmanager.shop.model.catalog.product.ReadableProductPrice 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.shop.model.catalog.product.ReadableProductPrice 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);
}
}
Aggregations