public void CheckWorkDays(int OrderID) { int workDays = 0; var order = db.Orders.Find(OrderID); var paymentDate = new TimeZoneConvert(order.Payments.FirstOrDefault()?.AuditDate ?? order.TimeOfOrder.Value, EnumData.TimeZone.EST).Utc; var updateDate = new DateTime(2019, 4, 24, 8, 1, 0, DateTimeKind.Utc); var checkPoint = new DateTime(paymentDate.Year, paymentDate.Month, paymentDate.Day, 7, 0, 0, DateTimeKind.Utc); do { if (paymentDate.CompareTo(checkPoint) < 0) { if (!checkPoint.DayOfWeek.Equals(DayOfWeek.Saturday) && !checkPoint.DayOfWeek.Equals(DayOfWeek.Sunday)) { workDays++; } } checkPoint = checkPoint.AddDays(1); } while (checkPoint.CompareTo(updateDate) < 0); }
public string Update_Tracking(Packages package) { string Message = ""; try { MyHelp.Log("Packages", package.ID, string.Format("訂單【{0}】包裹SC更新", package.OrderID), Session); if (!SCWS.Is_login) { throw new Exception("SC is not logged in!"); } OrderData orderData = SCWS.Get_OrderData(package.OrderID.Value); Order SC_order = orderData.Order; Package SC_package = SC_order.Packages.FirstOrDefault(p => p.ID.Equals(package.ID)); if (SC_package == null) { MyHelp.Log("Packages", package.ID, "Not find package on SC website!", Session); package.IsEnable = false; SC_package = SC_order.Packages.First(p => p.ID.Equals(SC_order.Items.First(i => i.ID.Equals(package.Items.First(ii => ii.IsEnable.Value).ID)).PackageID)); var newPackage = db.Packages.AsNoTracking().First(p => p.ID.Equals(package.ID)); newPackage.ID = SC_package.ID; db.Packages.Add(newPackage); foreach (var item in package.Items) { item.PackageID = newPackage.ID; } foreach (var pick in db.PickProduct.Where(pick => pick.PackageID.Value.Equals(package.ID))) { pick.PackageID = newPackage.ID; } if (package.Label != null) { package.Label.PackageID = newPackage.ID; } db.SaveChanges(); MyHelp.Log("Packages", package.ID, string.Format("Change package {0} to {1}", package.ID, newPackage.ID), Session); package = newPackage; } string carrier = ""; try { if (package.Method == null) { throw new Exception("Not find method!"); } if (package.Method.Carriers == null) { throw new Exception("Not find carrir"); } carrier = package.Method.Carriers.Name; } catch (Exception e) { MyHelp.Log("Packages", package.ID, e.Message, Session); carrier = db.ShippingMethod.Find(package.ShippingMethod.Value).Carriers.Name; } SCWS.Update_PackageShippingStatus(SC_package, (package.UploadTracking ? package.TrackingNumber : ""), carrier); if (db.Packages.AsNoTracking().Where(p => p.IsEnable.Value && p.OrderID.Value.Equals(package.OrderID.Value)).All(p => p.ProcessStatus.Equals((byte)EnumData.ProcessStatus.已出貨))) { if (SCWS.Update_OrderShippingStatus(SC_order, carrier)) { var updatePackage = db.Packages.Find(package.ID); updatePackage.WorkDays = 0; var paymentDate = new TimeZoneConvert(package.Orders.Payments.FirstOrDefault()?.AuditDate ?? package.Orders.TimeOfOrder.Value, EnumData.TimeZone.EST).Utc; var updateDate = DateTime.UtcNow; var checkPoint = new DateTime(paymentDate.Year, paymentDate.Month, paymentDate.Day, 7, 0, 0, DateTimeKind.Utc); do { if (paymentDate.CompareTo(checkPoint) < 0) { if (!checkPoint.DayOfWeek.Equals(DayOfWeek.Saturday) && !checkPoint.DayOfWeek.Equals(DayOfWeek.Sunday)) { updatePackage.WorkDays++; } } checkPoint = checkPoint.AddDays(1); } while (checkPoint.CompareTo(updateDate) < 0); db.SaveChanges(); MyHelp.Log("Packages", package.ID, string.Format("訂單【{0}】SC完成出貨", package.OrderID), Session); } } foreach (Items item in package.Items.Where(i => i.IsEnable.Value).ToList()) { if (item.SerialNumbers.Any()) { SCWS.Update_ItemSerialNumber(item.ID, item.SerialNumbers.Select(s => s.SerialNumber).ToArray()); } } Message = Sync_Order(package.OrderID.Value); } catch (Exception e) { string[] serials = package.Items.Where(i => i.IsEnable.Value).SelectMany(i => i.SerialNumbers.Select(s => s.SerialNumber)).ToArray(); MyHelp.ErrorLog(e, string.Format("訂單包裹【{0}】更新失敗: #serials {1}", package.OrderID, string.Join(",", serials)), package.OrderID.ToString()); Message = e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message) ? e.InnerException.Message : e.Message; } return(Message); }