use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class OrderProductPopulator method populate.
/**
* Converts a ShoppingCartItem carried in the ShoppingCart to an OrderProduct
* that will be saved in the system
*/
@Override
public OrderProduct populate(ShoppingCartItem source, OrderProduct target, MerchantStore store, Language language) throws ConversionException {
Validate.notNull(productService, "productService must be set");
Validate.notNull(digitalProductService, "digitalProductService must be set");
Validate.notNull(productAttributeService, "productAttributeService must be set");
try {
Product modelProduct = productService.getById(source.getProductId());
if (modelProduct == null) {
throw new ConversionException("Cannot get product with id (productId) " + source.getProductId());
}
if (modelProduct.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Invalid product id " + source.getProductId());
}
DigitalProduct digitalProduct = digitalProductService.getByProduct(store, modelProduct);
if (digitalProduct != null) {
OrderProductDownload orderProductDownload = new OrderProductDownload();
orderProductDownload.setOrderProductFilename(digitalProduct.getProductFileName());
orderProductDownload.setOrderProduct(target);
orderProductDownload.setDownloadCount(0);
orderProductDownload.setMaxdays(ApplicationConstants.MAX_DOWNLOAD_DAYS);
target.getDownloads().add(orderProductDownload);
}
target.setOneTimeCharge(source.getItemPrice());
target.setProductName(source.getProduct().getDescriptions().iterator().next().getName());
target.setProductQuantity(source.getQuantity());
target.setSku(source.getProduct().getSku());
FinalPrice finalPrice = source.getFinalPrice();
if (finalPrice == null) {
throw new ConversionException("Object final price not populated in shoppingCartItem (source)");
}
// Default price
OrderProductPrice orderProductPrice = orderProductPrice(finalPrice);
orderProductPrice.setOrderProduct(target);
Set<OrderProductPrice> prices = new HashSet<OrderProductPrice>();
prices.add(orderProductPrice);
// Other prices
List<FinalPrice> otherPrices = finalPrice.getAdditionalPrices();
if (otherPrices != null) {
for (FinalPrice otherPrice : otherPrices) {
OrderProductPrice other = orderProductPrice(otherPrice);
other.setOrderProduct(target);
prices.add(other);
}
}
target.setPrices(prices);
// OrderProductAttribute
Set<ShoppingCartAttributeItem> attributeItems = source.getAttributes();
if (!CollectionUtils.isEmpty(attributeItems)) {
Set<OrderProductAttribute> attributes = new HashSet<OrderProductAttribute>();
for (ShoppingCartAttributeItem attribute : attributeItems) {
OrderProductAttribute orderProductAttribute = new OrderProductAttribute();
orderProductAttribute.setOrderProduct(target);
Long id = attribute.getProductAttributeId();
ProductAttribute attr = productAttributeService.getById(id);
if (attr == null) {
throw new ConversionException("Attribute id " + id + " does not exists");
}
if (attr.getProduct().getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Attribute id " + id + " invalid for this store");
}
orderProductAttribute.setProductAttributeIsFree(attr.getProductAttributeIsFree());
orderProductAttribute.setProductAttributeName(attr.getProductOption().getDescriptionsSettoList().get(0).getName());
orderProductAttribute.setProductAttributeValueName(attr.getProductOptionValue().getDescriptionsSettoList().get(0).getName());
orderProductAttribute.setProductAttributePrice(attr.getProductAttributePrice());
orderProductAttribute.setProductAttributeWeight(attr.getProductAttributeWeight());
orderProductAttribute.setProductOptionId(attr.getProductOption().getId());
orderProductAttribute.setProductOptionValueId(attr.getProductOptionValue().getId());
attributes.add(orderProductAttribute);
}
target.setOrderAttributes(attributes);
}
} catch (Exception e) {
throw new ConversionException(e);
}
return target;
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method updateCartItems.
// TODO promoCode request parameter
@Override
public ShoppingCartData updateCartItems(Optional<String> promoCode, final List<ShoppingCartItem> shoppingCartItems, final MerchantStore store, final Language language) throws Exception {
Validate.notEmpty(shoppingCartItems, "shoppingCartItems null or empty");
ShoppingCart cartModel = null;
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
for (ShoppingCartItem item : shoppingCartItems) {
if (item.getQuantity() < 1) {
throw new CartModificationException("Quantity must not be less than one");
}
if (cartModel == null) {
cartModel = getCartModel(item.getCode(), store);
}
com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(item.getId(), cartModel);
if (entryToUpdate == null) {
throw new CartModificationException("Unknown entry number.");
}
entryToUpdate.getProduct();
LOG.info("Updating cart entry quantity to" + item.getQuantity());
entryToUpdate.setQuantity((int) item.getQuantity());
List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
cartItems.add(entryToUpdate);
}
cartModel.setPromoCode(null);
if (promoCode.isPresent()) {
cartModel.setPromoCode(promoCode.get());
cartModel.setPromoAdded(new Date());
}
cartModel.setLineItems(cartItems);
shoppingCartService.saveOrUpdate(cartModel);
LOG.info("Cart entry updated with desired quantity");
ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
shoppingCartDataPopulator.setPricingService(pricingService);
shoppingCartDataPopulator.setimageUtils(imageUtils);
return shoppingCartDataPopulator.populate(cartModel, store, language);
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method updateCartItem.
@Override
public ShoppingCartData updateCartItem(final Long itemID, final String cartId, final long newQuantity, final MerchantStore store, final Language language) throws Exception {
if (newQuantity < 1) {
throw new CartModificationException("Quantity must not be less than one");
}
if (StringUtils.isNotBlank(cartId)) {
ShoppingCart cartModel = getCartModel(cartId, store);
if (cartModel != null) {
com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(itemID.longValue(), cartModel);
if (entryToUpdate == null) {
throw new CartModificationException("Unknown entry number.");
}
entryToUpdate.getProduct();
LOG.info("Updating cart entry quantity to" + newQuantity);
entryToUpdate.setQuantity((int) newQuantity);
List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
shoppingCartService.saveOrUpdate(cartModel);
LOG.info("Cart entry updated with desired quantity");
ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
shoppingCartDataPopulator.setPricingService(pricingService);
shoppingCartDataPopulator.setimageUtils(imageUtils);
return shoppingCartDataPopulator.populate(cartModel, store, language);
}
}
return null;
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice in project shopizer by shopizer-ecommerce.
the class PromoCodeCalculatorModule method caculateProductPiceVariation.
@Override
public OrderTotal caculateProductPiceVariation(OrderSummary summary, ShoppingCartItem shoppingCartItem, Product product, Customer customer, MerchantStore store) throws Exception {
Validate.notNull(summary, "OrderTotalSummary must not be null");
Validate.notNull(store, "MerchantStore must not be null");
if (StringUtils.isBlank(summary.getPromoCode())) {
return null;
}
KieSession kieSession = droolsBeanFactory.getKieSession(ResourceFactory.newClassPathResource("com/salesmanager/drools/rules/PromoCoupon.drl"));
OrderTotalResponse resp = new OrderTotalResponse();
OrderTotalInputParameters inputParameters = new OrderTotalInputParameters();
inputParameters.setPromoCode(summary.getPromoCode());
inputParameters.setDate(new Date());
kieSession.insert(inputParameters);
kieSession.setGlobal("total", resp);
kieSession.fireAllRules();
if (resp.getDiscount() != null) {
OrderTotal orderTotal = null;
if (resp.getDiscount() != null) {
orderTotal = new OrderTotal();
orderTotal.setOrderTotalCode(Constants.OT_DISCOUNT_TITLE);
orderTotal.setOrderTotalType(OrderTotalType.SUBTOTAL);
orderTotal.setTitle(Constants.OT_SUBTOTAL_MODULE_CODE);
orderTotal.setText(summary.getPromoCode());
// calculate discount that will be added as a negative value
FinalPrice productPrice = pricingService.calculateProductPrice(product);
Double discount = resp.getDiscount();
BigDecimal reduction = productPrice.getFinalPrice().multiply(new BigDecimal(discount));
reduction = reduction.multiply(new BigDecimal(shoppingCartItem.getQuantity()));
// discount value
orderTotal.setValue(reduction);
// TODO check expiration
}
return orderTotal;
}
return null;
}
use of com.salesmanager.core.model.catalog.product.price.FinalPrice 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;
}
Aggregations