private void StarTrack_Test(int OrderID) { Packages package = db.Packages.First(p => p.IsEnable.Value && p.OrderID.Value.Equals(OrderID)); SC_WebService SCWS = new SC_WebService(Session["ApiUserName"].ToString(), Session["ApiPassword"].ToString()); ShipProcess ship = new ShipProcess(SCWS); ship.Init(package); var result = ship.Dispatch(); }
private void Dispatch(Packages package) { if (Order.StatusCode.Value.Equals((int)OrderStatusCode.InProcess) && Order.PaymentStatus.Equals((int)OrderPaymentStatus.Charged)) { ThreadTask threadTask = new ThreadTask(string.Format("訂單下載 - 自動提交訂單【{0}】至待出貨區", Order.OrderID), Session); MyHelp.Log("Orders", Order.OrderID, string.Format("訂單下載 - 自動提交訂單【{0}】至待出貨區", Order.OrderID), Session); package.ProcessStatus = (int)EnumData.ProcessStatus.鎖定中; Packages.Update(package, package.ID); lock (Factory) { threadTask.AddWork(Factory.StartNew(Session => { threadTask.Start(); string message = ""; using (QDLogisticsEntities db = new QDLogisticsEntities()) { IRepository <Packages> Packages = new GenericRepository <Packages>(db); IRepository <PickProduct> PickProduct = new GenericRepository <PickProduct>(db); Packages packageData = Packages.Get(package.ID); try { HttpSessionStateBase session = (HttpSessionStateBase)Session; SC_WebService SCWS = new SC_WebService("*****@*****.**", "timfromweypro"); if (!SCWS.Is_login) { throw new Exception("SC is not login"); } OrderStateInfo order = SCWS.Get_OrderStatus(packageData.OrderID.Value); if ((int)order.PaymentStatus == package.Orders.PaymentStatus) { ShipProcess shipProcess = new ShipProcess(SCWS); MyHelp.Log("Orders", packageData.OrderID, "提交至待出貨區", session); /***** 上傳Item出貨倉 *****/ var SC_order = SCWS.Get_OrderData(packageData.OrderID.Value).Order; var SC_items = SC_order.Items.Where(i => i.PackageID.Equals(packageData.ID)).ToArray(); foreach (var item in SC_items) { if (!db.Skus.AsNoTracking().Any(s => s.Sku.Equals(item.ProductID))) { throw new Exception(string.Format("系統尚未有品號 {0} 資料!", item.ProductID)); } item.ShipFromWareHouseID = packageData.Items.First(i => i.IsEnable == true && i.ID == item.ID).ShipFromWarehouseID.Value; SCWS.Update_OrderItem(SC_items.First(i => i.ID.Equals(item.ID))); } MyHelp.Log("Orders", packageData.OrderID, "更新訂單包裹的出貨倉", session); /***** 更新客戶地址 *****/ var address = SC_order.ShippingAddress; DataProcess.SetAddressData(packageData.Orders.Addresses, address, SC_order.BillingAddress); /***** 檢查運送國家 *****/ if (!string.IsNullOrEmpty(packageData.Method.CountryData)) { var countryData = JsonConvert.DeserializeObject <Dictionary <string, bool> >(packageData.Method.CountryData); if (!countryData.ContainsKey(packageData.Orders.Addresses.CountryCode.ToUpper())) { throw new Exception(string.Format("訂單【{0}】國家名稱不合,請重新確認", packageData.OrderID)); } if (!countryData[packageData.Orders.Addresses.CountryCode.ToUpper()]) { throw new Exception(string.Format("訂單【{0}】不可寄送至國家{1}", packageData.OrderID, packageData.Orders.Addresses.CountryName)); } } shipProcess.Init(packageData); var result = shipProcess.Dispatch(); if (result.Status) { MyHelp.Log("Orders", packageData.OrderID, "訂單提交完成", session); if (packageData.Items.First(i => i.IsEnable.Value).ShipWarehouses.Name.Equals("TWN")) { int[] itemIDs = packageData.Items.Where(i => i.IsEnable.Value).Select(i => i.ID).ToArray(); List <PickProduct> pickList = PickProduct.GetAll(true).Where(p => itemIDs.Contains(p.ItemID.Value)).ToList(); foreach (Items item in packageData.Items.Where(i => i.IsEnable == true)) { PickProduct pick = pickList.FirstOrDefault(pk => pk.ItemID == item.ID); if (pick != null) { pick.IsEnable = true; pick.IsPicked = false; pick.IsMail = false; pick.QtyPicked = 0; DataProcess.setPickProductData(pick, item); PickProduct.Update(pick, pick.ID); } else { pick = new PickProduct() { IsEnable = true }; DataProcess.setPickProductData(pick, item); PickProduct.Create(pick); } } PickProduct.SaveChanges(); } packageData.ProcessStatus = (int)EnumData.ProcessStatus.待出貨; } else { message = result.Message; packageData.ProcessStatus = (int)EnumData.ProcessStatus.訂單管理; } } else { message = "Payment status is different"; packageData.Orders.StatusCode = (int)OrderStatusCode.OnHold; packageData.ProcessStatus = (int)EnumData.ProcessStatus.訂單管理; } Packages.Update(packageData, packageData.ID); Packages.SaveChanges(); } catch (Exception e) { message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; packageData.ProcessStatus = (int)EnumData.ProcessStatus.訂單管理; if (!string.IsNullOrEmpty(package.WinitNo)) { Winit_API winit = new Winit_API(); winit.CancelOutboundOrder(package.WinitNo); package.WinitNo = null; } Packages.Update(packageData, packageData.ID); Packages.SaveChanges(); } } return(message); }, Session)); } } }