public OrderEngine(ShoppingCart shoppingCart, User user) throws Exception { EntityManager em = JpaConnection.getInstance().getCurrentEntityManager(); this.shoppingCart = shoppingCart; this.user = user; isNew = true; String siteId = shoppingCart.getContentSessionKey().getSiteId(); ContentSessionKey contentSessionKey = shoppingCart.getContentSessionKey(); if (shoppingCart.getOrderNum() != null) { orderHeader = OrderHeaderDAO.load(siteId, shoppingCart.getOrderNum()); isNew = false; } else { orderHeader = new OrderHeader(); orderHeader.setOrderStatus(Constants.ORDERSTATUS_OPEN); } Customer customer = shoppingCart.getCustomer(); if (customer != null) { OrderAddress custAddress = null; if (orderHeader.getCustAddress() != null) { custAddress = orderHeader.getCustAddress(); } else { custAddress = new OrderAddress(); custAddress.setRecCreateBy(Constants.USERNAME_SYSTEM); custAddress.setRecCreateDatetime(new Date()); } CustomerAddress adddress = customer.getCustAddress(); custAddress.setCustUseAddress(Constants.CUST_ADDRESS_USE_OWN); custAddress.setCustPrefix(adddress.getCustPrefix()); custAddress.setCustFirstName(adddress.getCustFirstName()); custAddress.setCustMiddleName(adddress.getCustMiddleName()); custAddress.setCustLastName(adddress.getCustLastName()); custAddress.setCustSuffix(adddress.getCustSuffix()); custAddress.setCustAddressLine1(adddress.getCustAddressLine1()); custAddress.setCustAddressLine2(adddress.getCustAddressLine2()); custAddress.setCustCityName(adddress.getCustCityName()); custAddress.setCustStateName(adddress.getCustStateName()); custAddress.setCustStateCode(adddress.getCustStateCode()); custAddress.setCustCountryName(adddress.getCustCountryName()); custAddress.setCustCountryCode(adddress.getCustCountryCode()); custAddress.setCustStateName(adddress.getCustStateName()); custAddress.setCustZipCode(adddress.getCustZipCode()); custAddress.setCustPhoneNum(adddress.getCustPhoneNum()); custAddress.setCustFaxNum(adddress.getCustFaxNum()); custAddress.setRecUpdateBy(Constants.USERNAME_SYSTEM); custAddress.setRecUpdateDatetime(new Date()); Country country = CountryDAO.loadByCountryCode(siteId, adddress.getCustCountryCode()); custAddress.setCountry(country); if (adddress.getCustStateCode() != null) { State state = StateDAO.loadByStateCode(siteId, adddress.getCustStateCode()); custAddress.setState(state); } em.persist(custAddress); orderHeader.setCustAddress(custAddress); } CustomerAddress billingAddress = shoppingCart.getBillingAddress(); if (billingAddress != null) { OrderAddress address = null; if (orderHeader.getBillingAddress() != null) { address = orderHeader.getBillingAddress(); } else { address = new OrderAddress(); address.setRecCreateBy(Constants.USERNAME_SYSTEM); address.setRecCreateDatetime(new Date()); } address.setCustUseAddress(billingAddress.getCustUseAddress()); address.setCustPrefix(billingAddress.getCustPrefix()); address.setCustFirstName(billingAddress.getCustFirstName()); address.setCustMiddleName(billingAddress.getCustMiddleName()); address.setCustLastName(billingAddress.getCustLastName()); address.setCustSuffix(billingAddress.getCustSuffix()); address.setCustAddressLine1(billingAddress.getCustAddressLine1()); address.setCustAddressLine2(billingAddress.getCustAddressLine2()); address.setCustCityName(billingAddress.getCustCityName()); address.setCustStateName(billingAddress.getCustStateName()); address.setCustStateCode(billingAddress.getCustStateCode()); address.setCustCountryName(billingAddress.getCustCountryName()); address.setCustCountryCode(billingAddress.getCustCountryCode()); address.setCustStateName(billingAddress.getCustStateName()); address.setCustZipCode(billingAddress.getCustZipCode()); address.setCustPhoneNum(billingAddress.getCustPhoneNum()); address.setCustFaxNum(billingAddress.getCustFaxNum()); address.setRecUpdateBy(Constants.USERNAME_SYSTEM); address.setRecUpdateDatetime(new Date()); Country country = CountryDAO.loadByCountryCode(siteId, billingAddress.getCustCountryCode()); address.setCountry(country); if (billingAddress.getCustStateCode() != null) { State state = StateDAO.loadByStateCode(siteId, billingAddress.getCustStateCode()); address.setState(state); } em.persist(address); orderHeader.setBillingAddress(address); } CustomerAddress shippingAddress = shoppingCart.getShippingAddress(); if (shippingAddress != null) { OrderAddress address = null; if (orderHeader.getShippingAddress() != null) { address = orderHeader.getShippingAddress(); } else { address = new OrderAddress(); address.setRecCreateBy(Constants.USERNAME_SYSTEM); address.setRecCreateDatetime(new Date()); } address.setCustUseAddress(shippingAddress.getCustUseAddress()); address.setCustPrefix(shippingAddress.getCustPrefix()); address.setCustFirstName(shippingAddress.getCustFirstName()); address.setCustMiddleName(shippingAddress.getCustMiddleName()); address.setCustLastName(shippingAddress.getCustLastName()); address.setCustSuffix(shippingAddress.getCustSuffix()); address.setCustAddressLine1(shippingAddress.getCustAddressLine1()); address.setCustAddressLine2(shippingAddress.getCustAddressLine2()); address.setCustCityName(shippingAddress.getCustCityName()); address.setCustStateName(shippingAddress.getCustStateName()); address.setCustStateCode(shippingAddress.getCustStateCode()); address.setCustCountryName(shippingAddress.getCustCountryName()); address.setCustCountryCode(shippingAddress.getCustCountryCode()); address.setCustStateName(shippingAddress.getCustStateName()); address.setCustZipCode(shippingAddress.getCustZipCode()); address.setCustPhoneNum(shippingAddress.getCustPhoneNum()); address.setCustFaxNum(shippingAddress.getCustFaxNum()); address.setRecUpdateBy(Constants.USERNAME_SYSTEM); address.setRecUpdateDatetime(new Date()); Country country = CountryDAO.loadByCountryCode(siteId, shippingAddress.getCustCountryCode()); address.setCountry(country); if (shippingAddress.getCustStateCode() != null) { State state = StateDAO.loadByStateCode(siteId, shippingAddress.getCustStateCode()); address.setState(state); } em.persist(address); orderHeader.setShippingAddress(address); } if (customer != null) { orderHeader.setCustEmail(customer.getCustEmail()); } orderHeader.setShippingTotal(Float.valueOf(shoppingCart.getShippingOrderTotal())); orderHeader.setShippingDiscountTotal(shoppingCart.getShippingDiscountTotal()); orderHeader.setOrderTotal(Float.valueOf(shoppingCart.getOrderTotal())); orderHeader.setOrderDate(new Date()); orderHeader.setPaymentGatewayProvider(null); orderHeader.setCreditCardDesc(null); orderHeader.setCustCreditCardNum(null); if (!shoppingCart.isCashPaymentOrder()) { if (shoppingCart.getPaymentEngine() != null) { orderHeader.setPaymentGatewayProvider( shoppingCart.getPaymentEngine().getClass().getSimpleName()); } } CustomerCreditCard custCreditCard = shoppingCart.getCustCreditCard(); if (!shoppingCart.isCashPaymentOrder() && custCreditCard != null) { orderHeader.setCreditCardDesc(custCreditCard.getCreditCard().getCreditCardDesc()); orderHeader.setCustCreditCardNum(custCreditCard.getCustCreditCardNum()); creditCardInfo = new CreditCardInfo(); creditCardInfo.setCreditCardNum( AESEncoder.getInstance().decode(custCreditCard.getCustCreditCardNum())); creditCardInfo.setCreditCardFullName(custCreditCard.getCustCreditCardFullName()); creditCardInfo.setCreditCardVerNum(custCreditCard.getCustCreditCardVerNum()); creditCardInfo.setCreditCardExpiryMonth(custCreditCard.getCustCreditCardExpiryMonth()); creditCardInfo.setCreditCardExpiryYear(custCreditCard.getCustCreditCardExpiryYear()); } if (shoppingCart.getShippingMethod() != null) { orderHeader.setShippingMethodName( shoppingCart.getShippingMethod().getShippingMethodLanguage().getShippingMethodName()); if (!contentSessionKey.isSiteProfileClassDefault()) { for (ShippingMethodLanguage language : shoppingCart.getShippingMethod().getShippingMethodLanguages()) { if (language .getSiteProfileClass() .getSiteProfileClassId() .equals(contentSessionKey.getSiteProfileClassId())) { if (language.getShippingMethodName() != null) { orderHeader.setShippingMethodName(language.getShippingMethodName()); } break; } } } } orderHeader.setShippingPickUp(Constants.VALUE_NO); if (shoppingCart.isShippingPickUp()) { orderHeader.setShippingPickUp(Constants.VALUE_YES); String shippingMethodName = Languages.getLangTranValue(contentSessionKey.getLangId(), "content.text.shipping.pickUp"); orderHeader.setShippingMethodName(shippingMethodName); } // orderHeader.setOrderStatus(Constants.ORDERSTATUS_OPEN); orderHeader.setSiteDomain(shoppingCart.getSiteDomain()); orderHeader.setCustomer(customer); orderHeader.setSiteCurrency(shoppingCart.getSiteCurrency()); orderHeader.setSiteProfile(shoppingCart.getSiteProfile()); orderHeader.setShppingMethod(shoppingCart.getShippingMethod()); for (OrderItemDetail orderItemDetail : orderHeader.getOrderItemDetails()) { for (OrderDetailTax orderDetailTax : orderItemDetail.getOrderDetailTaxes()) { em.remove(orderDetailTax); } orderItemDetail.getOrderDetailTaxes().clear(); for (OrderAttributeDetail orderAttributeDetail : orderItemDetail.getOrderAttributeDetails()) { em.remove(orderAttributeDetail); } orderItemDetail.getOrderAttributeDetails().clear(); em.remove(orderItemDetail); } orderHeader.getOrderItemDetails().clear(); for (OrderOtherDetail orderOtherDetail : orderHeader.getOrderOtherDetails()) { em.remove(orderOtherDetail); } orderHeader.getOrderOtherDetails().clear(); for (OrderDetailTax orderTax : orderHeader.getOrderTaxes()) { em.remove(orderTax); } orderHeader.getOrderTaxes().clear(); Iterator<?> shoppingCartItems = shoppingCart.getShoppingCartItems().iterator(); int seqNum = 0; while (shoppingCartItems.hasNext()) { ShoppingCartItem shoppingCartItem = (ShoppingCartItem) shoppingCartItems.next(); OrderItemDetail orderItemDetail = new OrderItemDetail(); Item item = shoppingCartItem.getItem(); Item master = item; if (item.getItemTypeCd().equals(Constants.ITEM_TYPE_SKU)) { master = item.getItemSkuParent(); } orderItemDetail.setSeqNum(Integer.valueOf(seqNum++)); orderItemDetail.setItemNum(item.getItemNum()); orderItemDetail.setItemUpcCd(item.getItemUpcCd()); orderItemDetail.setItemSkuCd(item.getItemSkuCd()); orderItemDetail.setItemShortDesc(master.getItemLanguage().getItemShortDesc()); if (master.getItemLanguage().getItemShortDesc().length() > 128) { orderItemDetail.setItemShortDesc( master.getItemLanguage().getItemShortDesc().substring(0, 127)); } if (!contentSessionKey.isSiteProfileClassDefault()) { for (ItemLanguage language : master.getItemLanguages()) { if (language .getSiteProfileClass() .getSiteProfileClassId() .equals(contentSessionKey.getSiteProfileClassId())) { if (language.getItemShortDesc() != null) { orderItemDetail.setItemShortDesc(language.getItemShortDesc()); } break; } } } orderItemDetail.setItemTierQty(shoppingCartItem.getTierPrice().getItemTierQty()); orderItemDetail.setItemTierPrice(shoppingCartItem.getTierPrice().getItemTierPrice()); orderItemDetail.setItemOrderQty(shoppingCartItem.getItemQty()); orderItemDetail.setItemDetailAmount(shoppingCartItem.getItemPriceTotal()); orderItemDetail.setItemDetailDiscountAmount(shoppingCartItem.getItemDiscountAmount()); setDirty(shoppingCartItem.getItem().getItemSkuCd()); if (shoppingCartItem.getItem().getItemId() != -1L) { orderItemDetail.setItem(item); } orderItemDetail.setOrderHeader(orderHeader); Iterator<?> iterator = shoppingCartItem.getItemAttributeInfos().iterator(); while (iterator.hasNext()) { ItemAttributeInfo itemAttributeInfo = (ItemAttributeInfo) iterator.next(); ItemAttributeDetail itemAttributeDetail = (ItemAttributeDetail) em.find(ItemAttributeDetail.class, itemAttributeInfo.getItemAttribDetailId()); char customAttribTypeCode = itemAttributeDetail .getCustomAttributeDetail() .getCustomAttribute() .getCustomAttribTypeCode(); if (customAttribTypeCode == Constants.CUSTOM_ATTRIBUTE_TYPE_SKU_MAKEUP) { continue; } if (customAttribTypeCode == Constants.CUSTOM_ATTRIBUTE_TYPE_USER_INPUT) { continue; } if (customAttribTypeCode == Constants.CUSTOM_ATTRIBUTE_TYPE_USER_SELECT_DROPDOWN) { continue; } OrderAttributeDetail orderAttributeDetail = new OrderAttributeDetail(); orderAttributeDetail.setOrderItemDetail(orderItemDetail); orderAttributeDetail.setCustomAttributeDetail( itemAttributeDetail.getCustomAttributeDetail()); if (customAttribTypeCode == Constants.CUSTOM_ATTRIBUTE_TYPE_CUST_SELECT_DROPDOWN) { CustomAttributeOption customAttributeOption = (CustomAttributeOption) em.find(CustomAttributeOption.class, itemAttributeInfo.getCustomAttribOptionId()); orderAttributeDetail.setCustomAttributeOption(customAttributeOption); } if (customAttribTypeCode == Constants.CUSTOM_ATTRIBUTE_TYPE_CUST_INPUT) { orderAttributeDetail.setOrderAttribValue(itemAttributeInfo.getItemAttribDetailValue()); } orderItemDetail.getOrderAttributeDetails().add(orderAttributeDetail); } orderHeader.getOrderItemDetails().add(orderItemDetail); ItemTax taxes[] = shoppingCartItem.getTaxes(); for (int i = 0; i < taxes.length; i++) { OrderDetailTax orderDetailTax = new OrderDetailTax(); orderDetailTax.setTaxName(taxes[i].getTax().getTaxLanguage().getTaxName()); if (!contentSessionKey.isSiteProfileClassDefault()) { for (TaxLanguage language : taxes[i].getTax().getTaxLanguages()) { if (language .getSiteProfileClass() .getSiteProfileClassId() .equals(contentSessionKey.getSiteProfileClassId())) { if (language.getTaxName() != null) { orderDetailTax.setTaxName(language.getTaxName()); } break; } } } orderDetailTax.setTaxAmount(taxes[i].getTaxAmount()); orderDetailTax.setTax(taxes[i].getTax()); orderDetailTax.setOrderItemDetail(orderItemDetail); orderDetailTax.setOrderHeader(orderHeader); orderItemDetail.getOrderDetailTaxes().add(orderDetailTax); orderHeader.getOrderTaxes().add(orderDetailTax); } } Iterator<?> shoppingCartCoupons = shoppingCart.getShoppingCartCoupons().iterator(); seqNum = 0; while (shoppingCartCoupons.hasNext()) { ShoppingCartCoupon shoppingCartCoupon = (ShoppingCartCoupon) shoppingCartCoupons.next(); OrderOtherDetail orderOtherDetail = new OrderOtherDetail(); Coupon coupon = shoppingCartCoupon.getCoupon(); orderOtherDetail.setCoupon(coupon); orderOtherDetail.setSeqNum(seqNum++); orderOtherDetail.setOrderOtherDetailNum(coupon.getCouponCode()); orderOtherDetail.setOrderOtherDetailDesc(coupon.getCouponLanguage().getCouponName()); if (!contentSessionKey.isSiteProfileClassDefault()) { for (CouponLanguage language : coupon.getCouponLanguages()) { if (language .getSiteProfileClass() .getSiteProfileClassId() .equals(contentSessionKey.getSiteProfileClassId())) { if (language.getCouponName() != null) { orderOtherDetail.setOrderOtherDetailDesc(language.getCouponName()); } break; } } } orderOtherDetail.setOrderOtherDetailAmount(shoppingCartCoupon.getCouponAmount()); setDirty(coupon); orderHeader.getOrderOtherDetails().add(orderOtherDetail); } ItemTax shippingTaxes[] = shoppingCart.getShippingTaxes(); if (shippingTaxes != null) { for (int i = 0; i < shippingTaxes.length; i++) { OrderDetailTax orderDetailTax = new OrderDetailTax(); orderDetailTax.setTaxName(shippingTaxes[i].getTax().getTaxLanguage().getTaxName()); if (!contentSessionKey.isSiteProfileClassDefault()) { for (TaxLanguage language : shippingTaxes[i].getTax().getTaxLanguages()) { if (language .getSiteProfileClass() .getSiteProfileClassId() .equals(contentSessionKey.getSiteProfileClassId())) { if (language.getTaxName() != null) { orderDetailTax.setTaxName(language.getTaxName()); } break; } } } orderDetailTax.setTaxAmount(shippingTaxes[i].getTaxAmount()); orderDetailTax.setTax(shippingTaxes[i].getTax()); orderDetailTax.setOrderHeader(orderHeader); orderHeader.getOrderTaxes().add(orderDetailTax); } } // orderHeader.setOrderStatus(calcStatus(orderHeader)); }
public void saveOrder() throws Exception { EntityManager em = JpaConnection.getInstance().getCurrentEntityManager(); String userId = user == null ? Constants.USERNAME_SYSTEM : user.getUserId(); Date current = new Date(); saveHeader(); orderHeader.setOrderAbundantLoc(""); Iterator<?> iterator = null; iterator = orderHeader.getOrderItemDetails().iterator(); while (iterator.hasNext()) { OrderItemDetail orderItemDetail = (OrderItemDetail) iterator.next(); if (!isDirty(orderItemDetail.getItemSkuCd())) { continue; } if (orderItemDetail.getItem() != null) { InventoryEngine engine = new InventoryEngine(orderItemDetail.getItem()); engine.adjustBookedQty(orderItemDetail.getItemOrderQty()); } if (orderItemDetail.getRecCreateDatetime() == null) { orderItemDetail.setRecCreateBy(userId); orderItemDetail.setRecCreateDatetime(current); } orderItemDetail.setRecUpdateBy(userId); orderItemDetail.setRecUpdateDatetime(current); if (orderItemDetail.getOrderItemDetailId() == null) { em.persist(orderItemDetail); } Iterator<?> attributes = orderItemDetail.getOrderAttributeDetails().iterator(); while (attributes.hasNext()) { OrderAttributeDetail orderAttributeDetail = (OrderAttributeDetail) attributes.next(); if (orderAttributeDetail.getRecCreateDatetime() == null) { orderAttributeDetail.setRecCreateBy(userId); orderAttributeDetail.setRecCreateDatetime(current); } orderAttributeDetail.setRecUpdateBy(userId); orderAttributeDetail.setRecUpdateDatetime(current); if (orderAttributeDetail.getOrderAttribDetailId() == null) { em.persist(orderAttributeDetail); } } Iterator<?> taxes = orderItemDetail.getOrderDetailTaxes().iterator(); while (taxes.hasNext()) { OrderDetailTax orderDetailTax = (OrderDetailTax) taxes.next(); if (orderDetailTax.getRecCreateDatetime() == null) { orderDetailTax.setRecCreateBy(userId); orderDetailTax.setRecCreateDatetime(current); } orderDetailTax.setRecUpdateBy(userId); orderDetailTax.setRecUpdateDatetime(current); if (orderDetailTax.getOrderDetailTaxId() == null) { em.persist(orderDetailTax); } } } iterator = orderHeader.getOrderOtherDetails().iterator(); while (iterator.hasNext()) { OrderOtherDetail orderOtherDetail = (OrderOtherDetail) iterator.next(); if (isDirty(orderOtherDetail.getCoupon())) { if (orderOtherDetail.getRecCreateDatetime() == null) { orderOtherDetail.setRecCreateBy(userId); orderOtherDetail.setRecCreateDatetime(current); orderOtherDetail.setOrderHeader(orderHeader); } orderOtherDetail.setRecUpdateBy(userId); orderOtherDetail.setRecUpdateDatetime(current); if (orderOtherDetail.getOrderOtherDetailId() == null) { em.persist(orderOtherDetail); } } } PaymentTran paymentTran = orderHeader.getPaymentTran(); if (paymentTran != null) { if (paymentTran.getRecUpdateDatetime() == null) { paymentTran.setRecCreateBy(userId); paymentTran.setRecCreateDatetime(current); paymentTran.setRecUpdateBy(userId); paymentTran.setRecUpdateDatetime(current); if (paymentTran.getPaymentTranId() == null) { em.persist(paymentTran); } } } paymentTran = orderHeader.getVoidPaymentTran(); if (paymentTran != null) { if (paymentTran.getRecUpdateDatetime() == null) { paymentTran.setRecCreateBy(userId); paymentTran.setRecCreateDatetime(current); paymentTran.setRecUpdateBy(userId); paymentTran.setRecUpdateDatetime(current); if (paymentTran.getPaymentTranId() == null) { em.persist(paymentTran); } } } if (orderHeader.getCustAddress() != null) { OrderAddress custAddress = orderHeader.getCustAddress(); if (custAddress.getRecCreateBy() == null) { custAddress.setRecCreateBy(userId); custAddress.setRecCreateDatetime(current); } custAddress.setRecUpdateBy(userId); custAddress.setRecUpdateDatetime(current); if (orderHeader.getCustAddress().getOrderAddressId() == null) { em.persist(orderHeader.getCustAddress()); } } if (orderHeader.getShippingAddress() != null) { OrderAddress shippingAddress = orderHeader.getShippingAddress(); if (shippingAddress.getRecCreateBy() == null) { shippingAddress.setRecCreateBy(userId); shippingAddress.setRecCreateDatetime(current); } shippingAddress.setRecUpdateBy(userId); shippingAddress.setRecUpdateDatetime(current); if (orderHeader.getShippingAddress().getOrderAddressId() == null) { em.persist(orderHeader.getShippingAddress()); } } if (orderHeader.getBillingAddress() != null) { OrderAddress billingAddress = orderHeader.getBillingAddress(); if (billingAddress.getRecCreateBy() == null) { billingAddress.setRecCreateBy(userId); billingAddress.setRecCreateDatetime(current); } billingAddress.setRecUpdateBy(userId); billingAddress.setRecUpdateDatetime(current); if (orderHeader.getBillingAddress().getOrderAddressId() == null) { em.persist(orderHeader.getBillingAddress()); } } Iterator<?> taxes = orderHeader.getOrderTaxes().iterator(); while (taxes.hasNext()) { OrderDetailTax orderDetailTax = (OrderDetailTax) taxes.next(); if (orderDetailTax.getRecCreateDatetime() == null) { orderDetailTax.setRecCreateBy(userId); orderDetailTax.setRecCreateDatetime(current); } orderDetailTax.setRecUpdateBy(userId); orderDetailTax.setRecUpdateDatetime(current); if (orderDetailTax.getOrderDetailTaxId() == null) { em.persist(orderDetailTax); } } // orderHeader.setOrderStatus(calcStatus(orderHeader)); this.clean(); isNew = false; }