/// <summary> /// /// </summary> public void TrimLookupRecords() { var numDeleted = 0; Console.WriteLine("Beginning trimming of lookup records"); using (var db = new Order_Entities()) { var lookupRecords = db.DSLToolComplemarTranslations.ToList(); var cometOrderIDs = db.Orders.Select(o => o.VendorOrderId).ToList(); foreach (var lookupRecord in lookupRecords) { if (lookupRecord.TimeCollected.AddDays(60) <= DateTime.Now || cometOrderIDs.Contains(lookupRecord.COMETOrderId.ToString(CultureInfo.InvariantCulture))) { db.DSLToolComplemarTranslations.DeleteObject(lookupRecord); numDeleted++; } } db.SaveChanges(); } Console.WriteLine("Completed trimming lookup records. {0} were removed.", numDeleted); }
/// <summary> /// Loops through the orders in the SIMPL db that are not closed, cancelled, or NA /// </summary> public void UpdateOrderStatusFromComet() { Console.Write("Begin updating individual orders..."); using (var db = new Order_Entities()) { // Get al orders where the vendor ID is known AND either the status is not a "final" status, or the number of update attempts is 0 var orders = db.Orders.Where(o => !string.IsNullOrEmpty(o.VendorOrderId) && ((o.OrderStatus.OrderStatusId != (int)SIMPLDbEnums.OrderStatusEnum.Canceled && o.OrderStatus.OrderStatusId != (int)SIMPLDbEnums.OrderStatusEnum.NA && o.OrderStatus.OrderStatusId != (int)SIMPLDbEnums.OrderStatusEnum.Closed) || o.UpdateAttempts == null)) .ToList(); using (var comet = new Dropship.CometProxy()) { foreach (var o in orders) { UpdateSingleOrder(o, comet, db); } } } Console.WriteLine("Done updating individual orders"); }
public void UpdateSingleOrder(Order o, Dropship.CometProxy comet, Order_Entities db) { Console.WriteLine("Processing order {0}", o.DSLToolOrderId); // increment the number of update attempts o.UpdateAttempts = o.UpdateAttempts == null ? 1 : o.UpdateAttempts + 1; // make sure we have a valid integer for the order id int orderNum; if (!int.TryParse(o.VendorOrderId, out orderNum)) { return; } // Get the order status back from Comet comet.Timeout = 60 * 1000 * 3; OrderStatus status = comet.GetOrderStatus( orderNum, 0, ConfigurationManager.AppSettings["COMETUsername"], ConfigurationManager.AppSettings["COMETPassword"] ); // Set the tracking numbers, if they exist if (status.ShippingStatus != null && status.ShippingStatus.TrackingNumbers != null && status.ShippingStatus.TrackingNumbers.Length > 0) { foreach (var s in status.ShippingStatus.TrackingNumbers) { var trackingNumber = o.TrackingNumbers.FirstOrDefault(t => t.TrackingNumber1 == s); if (trackingNumber == null) { var num = new TrackingNumber {TrackingNumber1 = s, TrackingNumberTypeId = 1}; o.TrackingNumbers.Add(num); } } } // Map the Comet order status to the SIMPL order status switch (status.Status1) { case com.complemar.cometerpws.OrderStatuses.Approved: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.Approved; break; case com.complemar.cometerpws.OrderStatuses.Cancelled: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.Canceled; break; case com.complemar.cometerpws.OrderStatuses.Closed: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.Closed; break; case com.complemar.cometerpws.OrderStatuses.InProcess: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.Confirmed; break; case com.complemar.cometerpws.OrderStatuses.Open: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.Confirmed; break; case com.complemar.cometerpws.OrderStatuses.Received: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.Closed; break; case com.complemar.cometerpws.OrderStatuses.Returned: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.RefusedOrder; break; case com.complemar.cometerpws.OrderStatuses.Shipped: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.Shipped; break; case com.complemar.cometerpws.OrderStatuses.Submitted: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.Ordered; break; case com.complemar.cometerpws.OrderStatuses.Unknown: o.Status = (int)SIMPLDbEnums.OrderStatusEnum.NA; break; } // apply the changes to the db after each order db.SaveChanges(); }
private void ProcessCOMETOrderStatuses(IEnumerable<OrderStatus> orderStatusList, out int numOrders) { Console.WriteLine("Begin processing COMET Order status list..."); numOrders = 0; using (var db = new Order_Entities()) { // check for any FI orders that don't yet have VendorOrderIDs but which have DSL Tool order IDs var ordersLackingVendorIDs = db.Orders.Where(o => !string.IsNullOrEmpty(o.DSLToolOrderId) && string.IsNullOrEmpty(o.VendorOrderId)).ToList(); // get the list of current lookup records it the DSLToolComplemar lookup table var existingCometOrderIds = db.DSLToolComplemarTranslations.Select(d => d.COMETOrderId).ToList(); foreach (var status in orderStatusList.Where(status => status.CustomerID.Substring(0, 2) == "FI")) { Console.WriteLine("Processing order: " + status.CustomerID); var dataChanged = false; // see if the COMET order ID already exists in the lookup table var existsInLookupTable = existingCometOrderIds.Contains(status.Transaction); // see if the DSL Order Tool order number already exists in the order record // (the DSL Order Tool order number is represented as the CustomerID in Complemar's database) var myOrder = ordersLackingVendorIDs.FirstOrDefault(o => o.DSLToolOrderId == status.CustomerID); if (myOrder != null) { // if it does, insert the COMET order id value myOrder.VendorOrderId = status.Transaction.ToString(CultureInfo.InvariantCulture); dataChanged = true; } // if dataChanged, we don't need a lookup record anymore...all the needed data is in the order record // so, if !existsInLookupTable and dataChanged, do nothing // if existsInLookupTable and dataChanged), remove the existing entry (there is no more updating needing to be done, ever) // if !existsInLookupTable and !dataChanged, add a new entry (we have no record of the DSL Tool order ID, so save the lookup record) // if existsInLookupTable and !dataChanged, do nothing if (!existsInLookupTable && !dataChanged) { var newEntry = new DSLToolComplemarTranslation(); newEntry.COMETOrderId = status.Transaction; newEntry.DSLToolOrderId = status.CustomerID; newEntry.TimeCollected = DateTime.Now; db.DSLToolComplemarTranslations.AddObject(newEntry); dataChanged = true; } else if (existsInLookupTable && dataChanged) { db.DSLToolComplemarTranslations.DeleteObject(db.DSLToolComplemarTranslations.First(d => d.COMETOrderId == status.Transaction)); } // save changes to the db if (dataChanged) { db.SaveChanges(); } numOrders++; } } Console.WriteLine("Done processing."); }