Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String productStoreId = (String) context.get("productStoreId");
String orderId = (String) context.get("orderId");
GetOrdersRequestType req = new GetOrdersRequestType();
GetOrdersResponseType resp = null;
try {
GenericValue orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
if (UtilValidate.isNotEmpty(orderHeader)) {
String externalId = orderHeader.getString("externalId").toString();
List<GenericValue> orderShipment = orderHeader.getRelated("OrderShipment");
if (orderShipment.size() > 0) {
List<GenericValue> trackingOrders = orderHeader.getRelated("TrackingCodeOrder");
ApiContext apiContext = EbayStoreHelper.getApiContext(productStoreId, locale, delegator);
GetOrdersCall ordersCall = new GetOrdersCall(apiContext);
OrderIDArrayType orderIdArr = new OrderIDArrayType();
String[] orderIdStr = {""+externalId};
orderIdArr.setOrderID(orderIdStr);
req.setOrderIDArray(orderIdArr);
Calendar orderFrom = Calendar.getInstance();
orderFrom.setTime(UtilDateTime.toDate("01/01/2001 00:00:00"));
req.setCreateTimeFrom(orderFrom);
Calendar orderTo = Calendar.getInstance();
orderTo.setTime(UtilDateTime.nowDate());
req.setCreateTimeTo(orderTo);
req.setOrderStatus(OrderStatusCodeType.SHIPPED);
req.setOrderRole(TradingRoleCodeType.SELLER);
resp = (GetOrdersResponseType) ordersCall.execute(req);
if (resp != null && "SUCCESS".equals(resp.getAck().toString())) {
OrderArrayType orderArr = resp.getOrderArray();
OrderType[] orderTypeList = orderArr.getOrder();
for (OrderType order : orderTypeList) {
String orderID = order.getOrderID();
if (orderID.equals(externalId)) {
AddOrderCall addOrderCall = new AddOrderCall(apiContext);
AddOrderRequestType addReq = new AddOrderRequestType();
AddOrderResponseType addResp = null;
OrderType newOrder = new OrderType();
ShippingDetailsType shippingDetail = (ShippingDetailsType) order.getShippingDetails();
if (trackingOrders.size() > 0) {
ShipmentTrackingDetailsType[] trackDetails = new ShipmentTrackingDetailsType[trackingOrders.size()];
for (int i = 0; i < trackDetails.length; i++) {
ShipmentTrackingDetailsType track = new ShipmentTrackingDetailsType();
track.setShipmentTrackingNumber(trackingOrders.get(i).get("trackingCodeId").toString());
trackDetails[i] = track;
}
shippingDetail.setShipmentTrackingDetails(trackDetails);
newOrder.setShippingDetails(shippingDetail);
}
newOrder.setOrderID(order.getOrderID());
newOrder.setOrderStatus(order.getOrderStatus());
newOrder.setAdjustmentAmount(order.getAdjustmentAmount());
newOrder.setAmountSaved(order.getAmountSaved());
newOrder.setCheckoutStatus(order.getCheckoutStatus());
newOrder.setShippingDetails(order.getShippingDetails());
newOrder.setCreatingUserRole(order.getCreatingUserRole());
newOrder.setCreatedTime(order.getCreatedTime());
newOrder.setPaymentMethods(order.getPaymentMethods());
newOrder.setShippingAddress(order.getShippingAddress());
newOrder.setSubtotal(order.getSubtotal());
newOrder.setTotal(order.getTotal());
newOrder.setTransactionArray(order.getTransactionArray());
newOrder.setBuyerUserID(order.getBuyerUserID());
newOrder.setPaidTime(order.getPaidTime());
newOrder.setShippedTime(order.getShippedTime());
newOrder.setIntegratedMerchantCreditCardEnabled(order.isIntegratedMerchantCreditCardEnabled());
addReq.setOrder(newOrder);
addResp = (AddOrderResponseType) addOrderCall.execute(addReq);
if (addResp != null && "SUCCESS".equals(addResp.getAck().toString())) {
Debug.logInfo("Upload tracking code to eBay success...", module);
} else {
createErrorLogMessage(userLogin, dctx.getDispatcher(), productStoreId, addResp.getAck().toString(), "Update order : uploadTrackingInfoBackToEbay", addResp.getErrors(0).getLongMessage());
}
}
}
} else {
createErrorLogMessage(userLogin, dctx.getDispatcher(), productStoreId, resp.getAck().toString(), "Get order : uploadTrackingInfoBackToEbay", resp.getErrors(0).getLongMessage());
}
}
}
} catch (Exception e) {
return ServiceUtil.returnError(e.getMessage());