use of com.salesmanager.core.model.catalog.product.Product 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.Product in project shopizer by shopizer-ecommerce.
the class OrderServiceImpl method process.
private Order process(Order order, Customer customer, List<ShoppingCartItem> items, OrderTotalSummary summary, Payment payment, Transaction transaction, MerchantStore store) throws ServiceException {
Validate.notNull(order, "Order cannot be null");
Validate.notNull(customer, "Customer cannot be null (even if anonymous order)");
Validate.notEmpty(items, "ShoppingCart items cannot be null");
Validate.notNull(payment, "Payment cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(summary, "Order total Summary cannot be null");
UserContext context = UserContext.getCurrentInstance();
if (context != null) {
String ipAddress = context.getIpAddress();
if (!StringUtils.isBlank(ipAddress)) {
order.setIpAddress(ipAddress);
}
}
// first process payment
Transaction processTransaction = paymentService.processPayment(customer, store, payment, items, order);
if (order.getOrderHistory() == null || order.getOrderHistory().size() == 0 || order.getStatus() == null) {
OrderStatus status = order.getStatus();
if (status == null) {
status = OrderStatus.ORDERED;
order.setStatus(status);
}
Set<OrderStatusHistory> statusHistorySet = new HashSet<OrderStatusHistory>();
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(status);
statusHistory.setDateAdded(new Date());
statusHistory.setOrder(order);
statusHistorySet.add(statusHistory);
order.setOrderHistory(statusHistorySet);
}
if (customer.getId() == null || customer.getId() == 0) {
customerService.create(customer);
}
order.setCustomerId(customer.getId());
this.create(order);
if (transaction != null) {
transaction.setOrder(order);
if (transaction.getId() == null || transaction.getId() == 0) {
transactionService.create(transaction);
} else {
transactionService.update(transaction);
}
}
if (processTransaction != null) {
processTransaction.setOrder(order);
if (processTransaction.getId() == null || processTransaction.getId() == 0) {
transactionService.create(processTransaction);
} else {
transactionService.update(processTransaction);
}
}
/**
* decrement inventory
*/
LOGGER.debug("Update inventory");
Set<OrderProduct> products = order.getOrderProducts();
for (OrderProduct orderProduct : products) {
orderProduct.getProductQuantity();
Product p = productService.getById(orderProduct.getId());
if (p == null)
throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
for (ProductAvailability availability : p.getAvailabilities()) {
int qty = availability.getProductQuantity();
if (qty < orderProduct.getProductQuantity()) {
// throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
LOGGER.error("APP-BACKEND [" + ServiceException.EXCEPTION_INVENTORY_MISMATCH + "]");
}
qty = qty - orderProduct.getProductQuantity();
availability.setProductQuantity(qty);
}
productService.update(p);
}
return order;
}
use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.
the class OrderTotalServiceImpl method findOrderTotalVariation.
@Override
public OrderTotalVariation findOrderTotalVariation(OrderSummary summary, Customer customer, MerchantStore store, Language language) throws Exception {
RebatesOrderTotalVariation variation = new RebatesOrderTotalVariation();
List<OrderTotal> totals = null;
if (orderTotalPostProcessors != null) {
for (OrderTotalPostProcessorModule module : orderTotalPostProcessors) {
// TODO check if the module is enabled from the Admin
List<ShoppingCartItem> items = summary.getProducts();
for (ShoppingCartItem item : items) {
Long productId = item.getProductId();
Product product = productService.getProductForLocale(productId, language, languageService.toLocale(language, store));
OrderTotal orderTotal = module.caculateProductPiceVariation(summary, item, product, customer, store);
if (orderTotal == null) {
continue;
}
if (totals == null) {
totals = new ArrayList<OrderTotal>();
variation.setVariations(totals);
}
// if product is null it will be catched when invoking the module
orderTotal.setText(StringUtils.isNoneBlank(orderTotal.getText()) ? orderTotal.getText() : product.getProductDescription().getName());
variation.getVariations().add(orderTotal);
}
}
}
return variation;
}
use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.
the class ProductReviewServiceImpl method saveOrUpdate.
private void saveOrUpdate(ProductReview review) throws ServiceException {
Validate.notNull(review, "ProductReview cannot be null");
Validate.notNull(review.getProduct(), "ProductReview.product cannot be null");
Validate.notNull(review.getCustomer(), "ProductReview.customer cannot be null");
// refresh product
Product product = productService.getById(review.getProduct().getId());
// ajust product rating
Integer count = 0;
if (product.getProductReviewCount() != null) {
count = product.getProductReviewCount();
}
BigDecimal averageRating = product.getProductReviewAvg();
if (averageRating == null) {
averageRating = new BigDecimal(0);
}
// get reviews
BigDecimal totalRating = averageRating.multiply(new BigDecimal(count));
totalRating = totalRating.add(new BigDecimal(review.getReviewRating()));
count = count + 1;
double avg = totalRating.doubleValue() / count;
product.setProductReviewAvg(new BigDecimal(avg));
product.setProductReviewCount(count);
super.save(review);
productService.update(product);
review.setProduct(product);
}
use of com.salesmanager.core.model.catalog.product.Product 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