/** * divide from parentOrder to multi sub order by wareHouseID * @input parentOrder : dw.order.Order */ importPackage( dw.system ); importPackage( dw.order ); importPackage( dw.util ); importPackage( dw.value ); importPackage( dw.web ); importScript( 'ext_ads:ServiceUtils.ds' ); importScript("app_lacoste_core:ATPCall/ATPUtils.ds"); function execute( args : PipelineDictionary ) : Number { var parentOrder = args.parentOrder; var isSuccess = splitOrders(parentOrder); if(isSuccess!=true){ return PIPELET_ERROR; } return PIPELET_NEXT; } /** * This function will create sub orders from parent order base on warehouseID * Parameter: parentOrder : dw.order.Order **/ function splitOrders(parentOrder : Order) : Boolean { var productsInOrderIterator : Iterator = parentOrder.getProductLineItems().iterator(); var productLineMap: HashMap = new HashMap(); while(!empty(productsInOrderIterator) && productsInOrderIterator.hasNext()) { var productLineItem : ProductLineItem = productsInOrderIterator.next(); if (productLineItem.bundledProductLineItems.length > 1) // product line item is bundle { for(var i = 0 ; i < productLineItem.bundledProductLineItems.length ; i++){ var bundleProductLineItem = productLineItem.bundledProductLineItems[i]; var wareHouseID : String = bundleProductLineItem.product.custom.wareHouseID; if (!productLineMap.containsKey(wareHouseID)) // if the warehouseID have not existed in hashmap { var arrayList = new ArrayList(); arrayList.push(bundleProductLineItem); productLineMap.put(wareHouseID, arrayList); } else //the warehouseID existed in hashmap { var arrayList = productLineMap.get(wareHouseID); arrayList.push(bundleProductLineItem); productLineMap.put(wareHouseID, arrayList); } } } else // normal product { var wareHouseID : String = productLineItem.product.custom.wareHouseID; if (!productLineMap.containsKey(wareHouseID)) { var arrayList = new ArrayList(); arrayList.push(productLineItem); productLineMap.put(wareHouseID, arrayList); } else{ var arrayList = productLineMap.get(wareHouseID); arrayList.push(productLineItem); productLineMap.put(wareHouseID, arrayList); } } }// end loop // will split order if have more one wareHouseID in hash map if(productLineMap.size() > 1){ var iter = productLineMap.keySet().iterator(); while (iter.hasNext()){ var warehouseID = iter.next(); var arrayList : ArrayList = productLineMap.get(warehouseID); var isSuccess = createSubOrderByBasket(arrayList, parentOrder, warehouseID); if(isSuccess!=true){ return false; } } adjustmentSubOrder(parentOrder); } // return true; } /** * This function will create order from basket * Parameter: arrayList : dw.util.ArrayList - List of product line item need to add to basket parentOrder : dw.order.Order - Parent order **/ function createSubOrderByBasket (arrayList : ArrayList , parentOrder : Order, warehouseID : String) : Boolean { var basket : Basket = dw.order.BasketMgr.getCurrentOrNewBasket(); // remove curent basket if exist; for each(var productLineItem in basket.allProductLineItems ) { basket.removeProductLineItem(productLineItem); } var priceAdjustment : Number = 0 ; var shippingMethodID = parentOrder.shipments[0].shippingMethodID; // single shipment for(var i = 0 ; i < arrayList.length ; i ++){ var pli = arrayList[i]; try{ var newPli : ProductLineItem; if (pli.isBonusProductLineItem()){ newPli = basket.createBonusProductLineItem(pli.getBonusDiscountLineItem(), pli.product, pli.getOptionModel(), basket.getDefaultShipment()); } else { newPli = basket.createProductLineItem(pli.product.ID, basket.getDefaultShipment()); newPli.setQuantityValue(pli.quantityValue); newPli.replaceProduct(pli.product); newPli.priceValue=pli.basePrice; } }catch(e){ var log = e; } } try{ var shipment = basket.defaultShipment; var shippingMethods = ShippingMgr.getShipmentShippingModel(shipment).getApplicableShippingMethods(); var shippingMethodsIter : Iterator = shippingMethods.iterator(); while( shippingMethodsIter.hasNext() ) { var method : ShippingMethod = shippingMethodsIter.next(); if( method.ID.equals( shippingMethodID ) ){ // set this shipping method shipment.setShippingMethod( method ); break; } } var parentAddress = parentOrder.billingAddress; var billingAddress = basket.createBillingAddress() ; billingAddress.setFirstName( parentAddress.firstName ); billingAddress.setLastName( parentAddress.lastName ); billingAddress.setAddress1( parentAddress.address1 ); billingAddress.setAddress2( parentAddress.address2 ); billingAddress.setCity( parentAddress.city ); billingAddress.setPostalCode( parentAddress.postalCode ); billingAddress.setStateCode( parentAddress.stateCode ); billingAddress.setCountryCode( parentAddress.countryCode.value ); billingAddress.setPhone( parentAddress.phone ); basket.customerName = parentOrder.customerName; basket.customerEmail = parentOrder.customerEmail; dw.system.HookMgr.callHook( "dw.ocapi.shop.basket.calculate", "calculate", basket ); //setUp for ATP var customeAddress = new Object(); customeAddress.FirstName = parentAddress.firstName; customeAddress.LastName = parentAddress.lastName; customeAddress.Address1 = parentAddress.address1; customeAddress.Address2 = parentAddress.address2; customeAddress.ZipCode = parentAddress.postalCode; customeAddress.City = parentAddress.postalCode; customeAddress.StateCode = parentAddress.stateCode ; customeAddress.CountryCode = parentAddress.countryCode.value ; customeAddress.Phone = parentAddress.phone; var tempOrderNo : String = OrderMgr.createOrderNo(); var isSuccess = ATPCallAllocate(basket,customeAddress, tempOrderNo); if(!isSuccess){ //have error when send ATP Alocate -> stop create SubOrder and cancel Parent Order OrderMgr.cancelOrder(parentOrder); return false; }else{ var subOrder = OrderMgr.createOrder(basket, tempOrderNo); if(subOrder != null){ //OrderMgr.placeOrder(subOrder); //syncCustomAttr(subOrder,parentOrder ); subOrder.custom.parentOrderID = parentOrder.orderNo; subOrder.custom.warehouseID = warehouseID; //subOrder.setStatus(parentOrder.getStatus().value) ; //subOrder.setPaymentStatus(parentOrder.getPaymentStatus().value); } } }catch(e){ var err = e; } return true; } /** * This function use to adjust amount for all sub orders base on parent order * Parameter: parentOrder : dw.order.Order - Parent order **/ function adjustmentSubOrder(parentOrder : Order) { var subOrderArray = getSubOrderByOrderID(parentOrder.orderNo); if(subOrderArray.length > 1 ){ var totalParentOrder = parentOrder.totalGrossPrice.value; var totalSubOrders = 0; for(var i = 0 ; i < subOrderArray.length ; i++){ var subOrder = subOrderArray[i]; totalSubOrders += subOrder.totalGrossPrice.value; } if(totalSubOrders > totalParentOrder) { var differentTotalAmount = (totalSubOrders - totalParentOrder).toFixed(2); var ratio = parseFloat((differentTotalAmount / totalSubOrders)).toFixed(2); var currentSubOrderTotalAmount = 0; for (var i = 0 ; i < subOrderArray.length ; i++ ){ var subOrder = subOrderArray[i]; var totalSubOrder = subOrder.totalGrossPrice.value; if(i == (subOrderArray.length - 1)){ subOrder.custom.adjustAmount = (differentTotalAmount - currentSubOrderTotalAmount) * -1; } else { var adjustAmount = parseFloat((ratio * totalSubOrder).toFixed(2)); subOrder.custom.adjustAmount = -1 * adjustAmount; currentSubOrderTotalAmount += adjustAmount; } } } } } /** * This function use to get total discount amount of one order * Parameter: parentOrder : Order - Parent order **/ function getOrderDiscount(parentOrder : Order) : Number { var merchTotalExclOrderDiscounts = parentOrder.getAdjustedMerchandizeTotalPrice(false); var merchTotalInclOrderDiscounts = parentOrder.getAdjustedMerchandizeTotalPrice(true); return orderDiscount = merchTotalExclOrderDiscounts.subtract( merchTotalInclOrderDiscounts ).value; } function syncCustomAttr(subOrder : Order , parentOrder : Order) : Object{ var p = parentOrder.custom; var excludeSync = ('lc_atpResponse','lc_atpResponseConfirm','lc_appeasementJson','parentOrderID','adjustAmount','warehouseID'); for (var key in p) { if (p.hasOwnProperty(key) && excludeSync.indexOf(key) == -1) { subOrder.custom[key] = p[key]; } } cashOnDelivery(subOrder); ServiceUtils.recalculateOrder(subOrder); return p; } function cashOnDelivery(order : Order ){ for each(var paymentInstrument : PaymentInstrument in order.paymentInstruments) { if(paymentInstrument.paymentMethod == "CASH_ON_DELIVERY") { order.custom.lc_orderToPayOnDelivery = true; order.custom.lc_orderAddedAmountToPayOnDelivery = Site.getCurrent().getCustomPreferenceValue('lc_cashOnDeliveryFees'); var taxRate = TaxMgr.getTaxRate(TaxMgr.defaultTaxClassID, TaxMgr.defaultTaxJurisdictionID); order.custom.lc_orderAddedTaxAmountToPayOnDelivery = new Number(order.custom.lc_orderAddedAmountToPayOnDelivery - (order.custom.lc_orderAddedAmountToPayOnDelivery / (1 + taxRate)).toFixed(2)); break; } else { order.custom.lc_orderToPayOnDelivery = false; order.custom.lc_orderAddedAmountToPayOnDelivery = 0; order.custom.lc_orderAddedTaxAmountToPayOnDelivery = 0; } } }