use of com.salesmanager.core.model.order.OrderSummary in project shopizer by shopizer-ecommerce.
the class ReadableShoppingCartMapper method merge.
@Override
public ReadableShoppingCart merge(ShoppingCart source, ReadableShoppingCart destination, MerchantStore store, Language language) {
Validate.notNull(source, "ShoppingCart cannot be null");
Validate.notNull(destination, "ReadableShoppingCart cannot be null");
Validate.notNull(store, "MerchantStore cannot be null");
Validate.notNull(language, "Language cannot be null");
destination.setCode(source.getShoppingCartCode());
int cartQuantity = 0;
destination.setCustomer(source.getCustomerId());
try {
if (!StringUtils.isBlank(source.getPromoCode())) {
// promo valid 1 day
Date promoDateAdded = source.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)) {
destination.setPromoCode(source.getPromoCode());
}
}
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = source.getLineItems();
if (items != null) {
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem item : items) {
ReadableShoppingCartItem shoppingCartItem = new ReadableShoppingCartItem();
readableMinimalProductMapper.merge(item.getProduct(), shoppingCartItem, store, language);
// ReadableProductPopulator readableProductPopulator = new
// ReadableProductPopulator();
// readableProductPopulator.setPricingService(pricingService);
// readableProductPopulator.setimageUtils(imageUtils);
// readableProductPopulator.populate(item.getProduct(), shoppingCartItem, store,
// language);
shoppingCartItem.setPrice(item.getItemPrice());
shoppingCartItem.setFinalPrice(pricingService.getDisplayAmount(item.getItemPrice(), store));
shoppingCartItem.setQuantity(item.getQuantity());
cartQuantity = cartQuantity + item.getQuantity();
BigDecimal subTotal = pricingService.calculatePriceQuantity(item.getItemPrice(), item.getQuantity());
// calculate sub total (price * quantity)
shoppingCartItem.setSubTotal(subTotal);
shoppingCartItem.setDisplaySubTotal(pricingService.getDisplayAmount(subTotal, store));
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = item.getAttributes();
if (attributes != null) {
for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attribute : attributes) {
ProductAttribute productAttribute = productAttributeService.getById(attribute.getProductAttributeId());
if (productAttribute == null) {
LOG.warn("Product attribute with ID " + attribute.getId() + " not found, skipping cart attribute " + attribute.getId());
continue;
}
ReadableShoppingCartAttribute cartAttribute = new ReadableShoppingCartAttribute();
cartAttribute.setId(attribute.getId());
ProductOption option = productAttribute.getProductOption();
ProductOptionValue optionValue = productAttribute.getProductOptionValue();
List<ProductOptionDescription> optionDescriptions = option.getDescriptionsSettoList();
List<ProductOptionValueDescription> optionValueDescriptions = optionValue.getDescriptionsSettoList();
String optName = null;
String optValue = null;
if (!CollectionUtils.isEmpty(optionDescriptions) && !CollectionUtils.isEmpty(optionValueDescriptions)) {
optName = optionDescriptions.get(0).getName();
optValue = optionValueDescriptions.get(0).getName();
for (ProductOptionDescription optionDescription : optionDescriptions) {
if (optionDescription.getLanguage() != null && optionDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
optName = optionDescription.getName();
break;
}
}
for (ProductOptionValueDescription optionValueDescription : optionValueDescriptions) {
if (optionValueDescription.getLanguage() != null && optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
optValue = optionValueDescription.getName();
break;
}
}
}
if (optName != null) {
ReadableShoppingCartAttributeOption attributeOption = new ReadableShoppingCartAttributeOption();
attributeOption.setCode(option.getCode());
attributeOption.setId(option.getId());
attributeOption.setName(optName);
cartAttribute.setOption(attributeOption);
}
if (optValue != null) {
ReadableShoppingCartAttributeOptionValue attributeOptionValue = new ReadableShoppingCartAttributeOptionValue();
attributeOptionValue.setCode(optionValue.getCode());
attributeOptionValue.setId(optionValue.getId());
attributeOptionValue.setName(optValue);
cartAttribute.setOptionValue(attributeOptionValue);
}
shoppingCartItem.getCartItemattributes().add(cartAttribute);
}
}
destination.getProducts().add(shoppingCartItem);
}
}
// Calculate totals using shoppingCartService
// OrderSummary contains ShoppingCart items
OrderSummary summary = new OrderSummary();
List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> productsList = new ArrayList<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
productsList.addAll(source.getLineItems());
summary.setProducts(productsList);
// OrdetTotalSummary contains all calculations
OrderTotalSummary orderSummary = shoppingCartCalculationService.calculate(source, store, language);
if (CollectionUtils.isNotEmpty(orderSummary.getTotals())) {
if (orderSummary.getTotals().stream().filter(t -> Constants.OT_DISCOUNT_TITLE.equals(t.getOrderTotalCode())).count() == 0) {
// no promo coupon applied
destination.setPromoCode(null);
}
List<ReadableOrderTotal> totals = new ArrayList<ReadableOrderTotal>();
for (com.salesmanager.core.model.order.OrderTotal t : orderSummary.getTotals()) {
ReadableOrderTotal total = new ReadableOrderTotal();
total.setCode(t.getOrderTotalCode());
total.setValue(t.getValue());
total.setText(t.getText());
totals.add(total);
}
destination.setTotals(totals);
}
destination.setSubtotal(orderSummary.getSubTotal());
destination.setDisplaySubTotal(pricingService.getDisplayAmount(orderSummary.getSubTotal(), store));
destination.setTotal(orderSummary.getTotal());
destination.setDisplayTotal(pricingService.getDisplayAmount(orderSummary.getTotal(), store));
destination.setQuantity(cartQuantity);
destination.setId(source.getId());
if (source.getOrderId() != null) {
destination.setOrder(source.getOrderId());
}
} catch (Exception e) {
throw new ConversionRuntimeException("An error occured while converting ReadableShoppingCart", e);
}
return destination;
}
use of com.salesmanager.core.model.order.OrderSummary in project shopizer by shopizer-ecommerce.
the class OrderTotalApi method payment.
/**
* This service calculates order total for a given shopping cart This method takes in
* consideration any applicable sales tax An optional request parameter accepts a quote id that
* was received using shipping api
*
* @param quote
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = { "/auth/cart/{id}/total" }, method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableOrderTotalSummary payment(@PathVariable final Long id, @RequestParam(value = "quote", required = false) Long quote, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) {
try {
Principal principal = request.getUserPrincipal();
String userName = principal.getName();
Customer customer = customerService.getByNick(userName);
if (customer == null) {
response.sendError(503, "Error while getting user details to calculate shipping quote");
}
ShoppingCart shoppingCart = shoppingCartFacade.getShoppingCartModel(id, merchantStore);
if (shoppingCart == null) {
response.sendError(404, "Cart id " + id + " does not exist");
return null;
}
if (shoppingCart.getCustomerId() == null) {
response.sendError(404, "Cart id " + id + " does not exist for exist for user " + userName);
return null;
}
if (shoppingCart.getCustomerId().longValue() != customer.getId().longValue()) {
response.sendError(404, "Cart id " + id + " does not exist for exist for user " + userName);
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, customer, 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;
}
}
Aggregations