@Autowired
protected OrderCustomerService orderCustomerService;
public OrderCustomer checkout(final Customer customer, final Cart cart) throws Exception {
OrderCustomer orderCustomer = new OrderCustomer();
// ORDER NUMBER IS CREATE BY DAO
orderCustomer.setStatus(OrderStatus.ORDER_STATUS_PENDING.getPropertyKey());
orderCustomer.setCurrency(cart.getCurrency());
orderCustomer.setMarketAreaId(cart.getMarketAreaId());
orderCustomer.setRetailerId(cart.getRetailerId());
orderCustomer.setLocalizationId(cart.getLocalizationId());
orderCustomer.setCustomerId(customer.getId());
OrderAddress billingAddress = new OrderAddress();
BeanUtils.copyProperties(customer.getAddress(cart.getBillingAddressId()), billingAddress);
orderCustomer.setBillingAddress(billingAddress);
OrderAddress shippingAddress = new OrderAddress();
BeanUtils.copyProperties(customer.getAddress(cart.getShippingAddressId()), shippingAddress);
orderCustomer.setShippingAddress(shippingAddress);
// SHIPMENT
Set<OrderShipment> orderShipments = new HashSet<OrderShipment>();
Set<DeliveryMethod> deliveryMethods = cart.getDeliveryMethods();
if(deliveryMethods != null){
for (Iterator<DeliveryMethod> iteratorDeliveryMethod = deliveryMethods.iterator(); iteratorDeliveryMethod.hasNext();) {
DeliveryMethod deliveryMethod = (DeliveryMethod) iteratorDeliveryMethod.next();
OrderShipment orderShipment = new OrderShipment();
orderShipment.setName(deliveryMethod.getName());
orderShipment.setExpectedDeliveryDate(null);
orderShipment.setDeliveryMethodId(deliveryMethod.getId());
orderShipment.setPrice(deliveryMethod.getPrice(cart.getCurrency().getId()));
Set<CartItem> cartItems = cart.getCartItems();
Set<OrderItem> orderItems = new HashSet<OrderItem>();
for (Iterator<CartItem> iteratorCartItem = cartItems.iterator(); iteratorCartItem.hasNext();) {
CartItem cartItem = (CartItem) iteratorCartItem.next();
OrderItem orderItem = new OrderItem();
orderItem.setCurrency(cart.getCurrency());
orderItem.setProductSkuCode(cartItem.getProductSkuCode());
orderItem.setProductSku(cartItem.getProductSku());
orderItem.setPrice(cartItem.getPrice(cart.getMarketAreaId(), cart.getRetailerId()).getSalePrice());
orderItem.setQuantity(cartItem.getQuantity());
// TAXES
Set<CartItemTax> taxes = cartItem.getTaxes();
if(taxes != null){
for (Iterator<CartItemTax> iteratorCartItemTax = taxes.iterator(); iteratorCartItemTax.hasNext();) {
CartItemTax cartItemTax = (CartItemTax) iteratorCartItemTax.next();
OrderTax orderTax = new OrderTax();
orderTax.setName(cartItemTax.getTax().getName());
orderTax.setPercent(cartItemTax.getTax().getPercent());
orderTax.setAmount(cartItemTax.getTaxAmount());
orderItem.getOrderTaxes().add(orderTax);
}
}
orderItems.add(orderItem);
}
orderShipment.setOrderItems(orderItems);
orderShipments.add(orderShipment);
}
}
orderCustomer.setOrderShipments(orderShipments);
orderCustomer = orderCustomerService.createNewOrder(orderCustomer);
return orderCustomer;
}