use of com.salesmanager.core.model.order.OrderSummary in project shopizer by shopizer-ecommerce.
the class ShoppingCartDataPopulator method populate.
@Override
public ShoppingCartData populate(final ShoppingCart shoppingCart, final ShoppingCartData cart, final MerchantStore store, final Language language) {
Validate.notNull(shoppingCart, "Requires ShoppingCart");
Validate.notNull(language, "Requires Language not null");
int cartQuantity = 0;
cart.setCode(shoppingCart.getShoppingCartCode());
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = shoppingCart.getLineItems();
List<ShoppingCartItem> shoppingCartItemsList = Collections.emptyList();
try {
if (items != null) {
shoppingCartItemsList = new ArrayList<ShoppingCartItem>();
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem item : items) {
ShoppingCartItem shoppingCartItem = new ShoppingCartItem();
shoppingCartItem.setCode(cart.getCode());
shoppingCartItem.setProductCode(item.getProduct().getSku());
shoppingCartItem.setProductVirtual(item.isProductVirtual());
shoppingCartItem.setProductId(item.getProductId());
shoppingCartItem.setId(item.getId());
String itemName = item.getProduct().getProductDescription().getName();
if (!CollectionUtils.isEmpty(item.getProduct().getDescriptions())) {
for (ProductDescription productDescription : item.getProduct().getDescriptions()) {
if (language != null && language.getId().intValue() == productDescription.getLanguage().getId().intValue()) {
itemName = productDescription.getName();
break;
}
}
}
shoppingCartItem.setName(itemName);
shoppingCartItem.setPrice(pricingService.getDisplayAmount(item.getItemPrice(), store));
shoppingCartItem.setQuantity(item.getQuantity());
cartQuantity = cartQuantity + item.getQuantity();
shoppingCartItem.setProductPrice(item.getItemPrice());
shoppingCartItem.setSubTotal(pricingService.getDisplayAmount(item.getSubTotal(), store));
ProductImage image = item.getProduct().getProductImage();
if (image != null && imageUtils != null) {
String imagePath = imageUtils.buildProductImageUtils(store, item.getProduct().getSku(), image.getProductImage());
shoppingCartItem.setImage(imagePath);
}
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = item.getAttributes();
if (attributes != null) {
List<ShoppingCartAttribute> cartAttributes = new ArrayList<ShoppingCartAttribute>();
for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attribute : attributes) {
ShoppingCartAttribute cartAttribute = new ShoppingCartAttribute();
cartAttribute.setId(attribute.getId());
cartAttribute.setAttributeId(attribute.getProductAttributeId());
cartAttribute.setOptionId(attribute.getProductAttribute().getProductOption().getId());
cartAttribute.setOptionValueId(attribute.getProductAttribute().getProductOptionValue().getId());
List<ProductOptionDescription> optionDescriptions = attribute.getProductAttribute().getProductOption().getDescriptionsSettoList();
List<ProductOptionValueDescription> optionValueDescriptions = attribute.getProductAttribute().getProductOptionValue().getDescriptionsSettoList();
if (!CollectionUtils.isEmpty(optionDescriptions) && !CollectionUtils.isEmpty(optionValueDescriptions)) {
String optionName = optionDescriptions.get(0).getName();
String optionValue = optionValueDescriptions.get(0).getName();
for (ProductOptionDescription optionDescription : optionDescriptions) {
if (optionDescription.getLanguage() != null && optionDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
optionName = optionDescription.getName();
break;
}
}
for (ProductOptionValueDescription optionValueDescription : optionValueDescriptions) {
if (optionValueDescription.getLanguage() != null && optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
optionValue = optionValueDescription.getName();
break;
}
}
cartAttribute.setOptionName(optionName);
cartAttribute.setOptionValue(optionValue);
cartAttributes.add(cartAttribute);
}
}
shoppingCartItem.setShoppingCartAttributes(cartAttributes);
}
shoppingCartItemsList.add(shoppingCartItem);
}
}
if (CollectionUtils.isNotEmpty(shoppingCartItemsList)) {
cart.setShoppingCartItems(shoppingCartItemsList);
}
if (shoppingCart.getOrderId() != null) {
cart.setOrderId(shoppingCart.getOrderId());
}
OrderSummary summary = new OrderSummary();
List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> productsList = new ArrayList<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
productsList.addAll(shoppingCart.getLineItems());
summary.setProducts(productsList.stream().filter(p -> p.getProduct().isAvailable()).collect(Collectors.toList()));
OrderTotalSummary orderSummary = shoppingCartCalculationService.calculate(shoppingCart, store, language);
if (CollectionUtils.isNotEmpty(orderSummary.getTotals())) {
List<OrderTotal> totals = new ArrayList<OrderTotal>();
for (com.salesmanager.core.model.order.OrderTotal t : orderSummary.getTotals()) {
OrderTotal total = new OrderTotal();
total.setCode(t.getOrderTotalCode());
total.setText(t.getText());
total.setValue(t.getValue());
totals.add(total);
}
cart.setTotals(totals);
}
cart.setSubTotal(pricingService.getDisplayAmount(orderSummary.getSubTotal(), store));
cart.setTotal(pricingService.getDisplayAmount(orderSummary.getTotal(), store));
cart.setQuantity(cartQuantity);
cart.setId(shoppingCart.getId());
} catch (ServiceException ex) {
LOG.error("Error while converting cart Model to cart Data.." + ex);
throw new ConversionException("Unable to create cart data", ex);
}
return cart;
}
use of com.salesmanager.core.model.order.OrderSummary in project shopizer by shopizer-ecommerce.
the class OrderTotalApi method calculateTotal.
/**
* Public api
* @param id
* @param quote
* @param merchantStore
* @param language
* @param response
* @return
*/
@RequestMapping(value = { "/cart/{code}/total" }, method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableOrderTotalSummary calculateTotal(@PathVariable final String code, @RequestParam(value = "quote", required = false) Long quote, @ApiIgnore MerchantStore merchantStore, // possible postal code, province and country
@ApiIgnore Language language, HttpServletResponse response) {
try {
ShoppingCart shoppingCart = shoppingCartFacade.getShoppingCartModel(code, merchantStore);
if (shoppingCart == null) {
response.sendError(404, "Cart code " + code + " does not exist");
return null;
}
ShippingSummary shippingSummary = null;
// get shipping quote if asked for
if (quote != null) {
shippingSummary = shippingQuoteService.getShippingSummary(quote, merchantStore);
}
OrderTotalSummary orderTotalSummary = null;
OrderSummary orderSummary = new OrderSummary();
orderSummary.setShippingSummary(shippingSummary);
List<ShoppingCartItem> itemsSet = new ArrayList<ShoppingCartItem>(shoppingCart.getLineItems());
orderSummary.setProducts(itemsSet);
orderTotalSummary = orderService.caculateOrderTotal(orderSummary, merchantStore, language);
ReadableOrderTotalSummary returnSummary = new ReadableOrderTotalSummary();
ReadableOrderSummaryPopulator populator = new ReadableOrderSummaryPopulator();
populator.setMessages(messages);
populator.setPricingService(pricingService);
populator.populate(orderTotalSummary, returnSummary, merchantStore, language);
return returnSummary;
} catch (Exception e) {
LOGGER.error("Error while calculating order summary", e);
try {
response.sendError(503, "Error while calculating order summary " + e.getMessage());
} catch (Exception ignore) {
}
return null;
}
}
use of com.salesmanager.core.model.order.OrderSummary in project shopizer by shopizer-ecommerce.
the class OrderFacadeImpl method processOrder.
/**
* Process order from api
*/
@Override
public Order processOrder(com.salesmanager.shop.model.order.v1.PersistableOrder order, Customer customer, MerchantStore store, Language language, Locale locale) throws ServiceException {
Validate.notNull(order, "Order cannot be null");
Validate.notNull(customer, "Customer cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
Validate.notNull(locale, "Locale cannot be null");
try {
Order modelOrder = new Order();
persistableOrderApiPopulator.populate(order, modelOrder, store, language);
Long shoppingCartId = order.getShoppingCartId();
ShoppingCart cart = shoppingCartService.getById(shoppingCartId, store);
if (cart == null) {
throw new ServiceException("Shopping cart with id " + shoppingCartId + " does not exist");
}
Set<ShoppingCartItem> shoppingCartItems = cart.getLineItems();
List<ShoppingCartItem> items = new ArrayList<ShoppingCartItem>(shoppingCartItems);
Set<OrderProduct> orderProducts = new LinkedHashSet<OrderProduct>();
OrderProductPopulator orderProductPopulator = new OrderProductPopulator();
orderProductPopulator.setDigitalProductService(digitalProductService);
orderProductPopulator.setProductAttributeService(productAttributeService);
orderProductPopulator.setProductService(productService);
for (ShoppingCartItem item : shoppingCartItems) {
OrderProduct orderProduct = new OrderProduct();
orderProduct = orderProductPopulator.populate(item, orderProduct, store, language);
orderProduct.setOrder(modelOrder);
orderProducts.add(orderProduct);
}
modelOrder.setOrderProducts(orderProducts);
if (order.getAttributes() != null && order.getAttributes().size() > 0) {
Set<OrderAttribute> attrs = new HashSet<OrderAttribute>();
for (com.salesmanager.shop.model.order.OrderAttribute attribute : order.getAttributes()) {
OrderAttribute attr = new OrderAttribute();
attr.setKey(attribute.getKey());
attr.setValue(attribute.getValue());
attr.setOrder(modelOrder);
attrs.add(attr);
}
modelOrder.setOrderAttributes(attrs);
}
// requires Shipping information (need a quote id calculated)
ShippingSummary shippingSummary = null;
// get shipping quote if asked for
if (order.getShippingQuote() != null && order.getShippingQuote().longValue() > 0) {
shippingSummary = shippingQuoteService.getShippingSummary(order.getShippingQuote(), store);
if (shippingSummary != null) {
modelOrder.setShippingModuleCode(shippingSummary.getShippingModule());
}
}
// requires Order Totals, this needs recalculation and then compare
// total with the amount sent as part
// of process order request. If totals does not match, an error
// should be thrown.
OrderTotalSummary orderTotalSummary = null;
OrderSummary orderSummary = new OrderSummary();
orderSummary.setShippingSummary(shippingSummary);
List<ShoppingCartItem> itemsSet = new ArrayList<ShoppingCartItem>(cart.getLineItems());
orderSummary.setProducts(itemsSet);
orderTotalSummary = orderService.caculateOrderTotal(orderSummary, customer, store, language);
if (order.getPayment().getAmount() == null) {
throw new ConversionException("Requires Payment.amount");
}
String submitedAmount = order.getPayment().getAmount();
BigDecimal calculatedAmount = orderTotalSummary.getTotal();
String strCalculatedTotal = calculatedAmount.toPlainString();
// compare both prices
if (!submitedAmount.equals(strCalculatedTotal)) {
throw new ConversionException("Payment.amount does not match what the system has calculated " + strCalculatedTotal + " (received " + submitedAmount + ") please recalculate the order and submit again");
}
modelOrder.setTotal(calculatedAmount);
List<com.salesmanager.core.model.order.OrderTotal> totals = orderTotalSummary.getTotals();
Set<com.salesmanager.core.model.order.OrderTotal> set = new HashSet<com.salesmanager.core.model.order.OrderTotal>();
if (!CollectionUtils.isEmpty(totals)) {
for (com.salesmanager.core.model.order.OrderTotal total : totals) {
total.setOrder(modelOrder);
set.add(total);
}
}
modelOrder.setOrderTotal(set);
PersistablePaymentPopulator paymentPopulator = new PersistablePaymentPopulator();
paymentPopulator.setPricingService(pricingService);
Payment paymentModel = new Payment();
paymentPopulator.populate(order.getPayment(), paymentModel, store, language);
modelOrder.setShoppingCartCode(cart.getShoppingCartCode());
/**
*/
if (!StringUtils.isBlank(customer.getNick()) && !customer.isAnonymous()) {
if (order.getCustomerId() == null && (customerFacade.checkIfUserExists(customer.getNick(), store))) {
customer.setAnonymous(true);
customer.setNick(null);
// send email instructions
}
}
// order service
modelOrder = orderService.processOrder(modelOrder, customer, items, orderTotalSummary, paymentModel, store);
// update cart
try {
cart.setOrderId(modelOrder.getId());
shoppingCartFacade.saveOrUpdateShoppingCart(cart);
} catch (Exception e) {
LOGGER.error("Cannot delete cart " + cart.getId(), e);
}
// email management
if ("true".equals(coreConfiguration.getProperty("ORDER_EMAIL_API"))) {
// send email
try {
notify(modelOrder, customer, store, language, locale);
} catch (Exception e) {
LOGGER.error("Cannot send order confirmation email", e);
}
}
return modelOrder;
} catch (Exception e) {
throw new ServiceException(e);
}
}
use of com.salesmanager.core.model.order.OrderSummary in project shopizer by shopizer-ecommerce.
the class OrderFacadeImpl method calculateOrderTotal.
private OrderTotalSummary calculateOrderTotal(MerchantStore store, Customer customer, com.salesmanager.shop.model.order.v0.PersistableOrder order, Language language) throws Exception {
OrderTotalSummary orderTotalSummary = null;
OrderSummary summary = new OrderSummary();
if (order instanceof ShopOrder) {
ShopOrder o = (ShopOrder) order;
summary.setProducts(o.getShoppingCartItems());
if (o.getShippingSummary() != null) {
summary.setShippingSummary(o.getShippingSummary());
}
if (!StringUtils.isBlank(o.getCartCode())) {
ShoppingCart shoppingCart = shoppingCartFacade.getShoppingCartModel(o.getCartCode(), store);
// promo code
if (!StringUtils.isBlank(shoppingCart.getPromoCode())) {
// promo
Date promoDateAdded = shoppingCart.getPromoAdded();
// valid
// 1 day
Instant instant = promoDateAdded.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate date = zdt.toLocalDate();
// date added < date + 1 day
LocalDate tomorrow = LocalDate.now().plusDays(1);
if (date.isBefore(tomorrow)) {
summary.setPromoCode(shoppingCart.getPromoCode());
} else {
// clear promo
shoppingCart.setPromoCode(null);
shoppingCartService.saveOrUpdate(shoppingCart);
}
}
}
orderTotalSummary = orderService.caculateOrderTotal(summary, customer, store, language);
} else {
// PersistableOrder not implemented
throw new Exception("calculateOrderTotal not yet implemented for PersistableOrder");
}
return orderTotalSummary;
}
use of com.salesmanager.core.model.order.OrderSummary in project shopizer by shopizer-ecommerce.
the class OrderServiceImpl method caculateShoppingCart.
private OrderTotalSummary caculateShoppingCart(ShoppingCart shoppingCart, final Customer customer, final MerchantStore store, final Language language) throws Exception {
OrderSummary orderSummary = new OrderSummary();
orderSummary.setOrderSummaryType(OrderSummaryType.SHOPPINGCART);
if (!StringUtils.isBlank(shoppingCart.getPromoCode())) {
// promo valid 1 day
Date promoDateAdded = shoppingCart.getPromoAdded();
if (promoDateAdded == null) {
promoDateAdded = new Date();
}
Instant instant = promoDateAdded.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate date = zdt.toLocalDate();
// date added < date + 1 day
LocalDate tomorrow = LocalDate.now().plusDays(1);
if (date.isBefore(tomorrow)) {
orderSummary.setPromoCode(shoppingCart.getPromoCode());
} else {
// clear promo
shoppingCart.setPromoCode(null);
shoppingCartService.saveOrUpdate(shoppingCart);
}
}
List<ShoppingCartItem> itemList = new ArrayList<ShoppingCartItem>(shoppingCart.getLineItems());
// filter out unavailable
itemList = itemList.stream().filter(p -> p.getProduct().isAvailable()).collect(Collectors.toList());
orderSummary.setProducts(itemList);
return caculateOrder(orderSummary, customer, store, language);
}
Aggregations