public ActionResult ChangeType(int target, int type) { var dlo = DB.DeliveryListOrders.FirstOrDefault(x => x.ID == target); if (dlo != null) { dlo.Type = null; dlo.CarID = null; dlo.WorkerID = null; dlo.Order.OrderDelivery.DeliveryType = type; DB.SubmitChanges(); if (dlo.Order.ShopID.HasValue) { var poly = new PolygonList(dlo.Order.ShopID.Value); if (type == (int)SectorTypes.CarSector) { var cars = poly.GetDefaultCars(dlo.Order); if (cars.Any()) { dlo.CarID = cars.First().ID; } } if (type == (int)SectorTypes.CourierSector) { var cars = poly.GetDefaultCouriers(dlo.Order); if (cars.Any()) { dlo.CarID = cars.First().ID; } } DB.SubmitChanges(); } } return(new ContentResult()); }
public ActionResult CheckList(string Date, int ShopID) { var d = Date.ToDate(); if (!d.HasValue) { return(new ContentResult()); } /* * var options = new DataLoadOptions(); * options.LoadWith((DeliveryListOrder x) => x.Order); * DB.LoadOptions = options; */ var list = DB.DeliveryLists.FirstOrDefault(x => x.Date.Date == d.Value.Date && x.ShopID == ShopID); /* * var dtl = (DateTime?)Session["CheckList_" + Date.Date.ToString("yy-mm-dd")]; * if (dtl.HasValue && DateTime.Now.Subtract(dtl.Value).TotalSeconds < 2) * { * return list; * } * else * { * Session["CheckList_" + Date.Date.ToString("yy-mm-dd")] = null; * } * Session["CheckList_" + Date.Date.ToString("yy-mm-dd")] = DateTime.Now; * */ var orders = DB.Orders.Where( x => x.DeliveryDate.HasValue && x.DeliveryDate.Value.Date == d.Value.Date && x.ShopID == ShopID) .ToList(); var poly = new PolygonList(ShopID); foreach (var order in orders) { if (order.OrderDelivery == null) { var delivery = new DeliveryAddress() { DeliveryType = (int)SectorTypes.CarSector }; DB.DeliveryAddresses.InsertOnSubmit(delivery); order.OrderDelivery = delivery; DB.SubmitChanges(); } if (order.OrderDelivery.DeliveryType == (int)SectorTypes.CarSector && !order.CarID.HasValue) { var cars = poly.GetDefaultCars(order); if (cars.Any()) { order.CarID = cars.First().ID; } } if (order.OrderDelivery.DeliveryType == (int)SectorTypes.CourierSector && !order.WorkerID.HasValue) { var couriers = poly.GetDefaultCouriers(order); if (couriers.Any()) { order.WorkerID = couriers.First().ID; } } } DB.SubmitChanges(); var stores = DB.Stores.Where(x => x.ShopStores.Any(z => z.ShopID == ShopID)).ToList(); if (list == null) { list = new DeliveryList() { Approved = false, Date = d.Value.Date, ShopID = ShopID }; DB.DeliveryLists.InsertOnSubmit(list); DB.SubmitChanges(); int add = 1; if (stores.Any() && orders.Any()) { var workers = orders.Where(x => x.WorkerID.HasValue).Select(x => x.WorkerID).Distinct().ToList(); foreach (var worker in workers) { DB.DeliveryListOrders.InsertAllOnSubmit( stores.Select( (x, index) => new DeliveryListOrder() { ListID = list.ID, StoreID = x.ID, OrderNum = index + add, Type = (int)SectorTypes.CourierSector, WorkerID = worker }) .ToList()); DB.SubmitChanges(); add += stores.Count; var workerOrders = orders.Where(x => x.WorkerID == worker).ToList(); DB.DeliveryListOrders.InsertAllOnSubmit( workerOrders.Select( (x, index) => new DeliveryListOrder() { ListID = list.ID, OrderID = x.ID, OrderNum = index + add }) .ToList()); DB.SubmitChanges(); add += workerOrders.Count; } var cars = orders.Where(x => x.CarID.HasValue).Select(x => x.CarID).Distinct().ToList(); foreach (var car in cars) { DB.DeliveryListOrders.InsertAllOnSubmit( stores.Select( (x, index) => new DeliveryListOrder() { ListID = list.ID, StoreID = x.ID, OrderNum = index + add, Type = (int)SectorTypes.CarSector, CarID = car }) .ToList()); add += stores.Count; var carOrders = orders.Where(x => x.CarID == car).ToList(); DB.DeliveryListOrders.InsertAllOnSubmit( carOrders.Select( (x, index) => new DeliveryListOrder() { ListID = list.ID, OrderID = x.ID, OrderNum = index + add }) .ToList()); DB.SubmitChanges(); add += carOrders.Count; } var another = orders.Where(x => !x.WorkerID.HasValue && !x.CarID.HasValue); DB.DeliveryListOrders.InsertAllOnSubmit( another.Select( (x, index) => new DeliveryListOrder() { ListID = list.ID, OrderID = x.ID, OrderNum = index + add }) .ToList()); DB.SubmitChanges(); } } else { var old = DB.DeliveryListOrders.Where(x => x.ListID == list.ID && x.Store == null && x.Order == null).ToList(); if (old.Any()) { DB.DeliveryListOrders.DeleteAllOnSubmit(old); DB.SubmitChanges(); } var newOrders = orders.Where(x => list.DeliveryListOrders.All(z => z.OrderID != x.ID)).ToList(); if (newOrders.Any()) { var min = list.DeliveryListOrders.Any() ? (list.DeliveryListOrders.Max(x => x.OrderNum) + 1) : 1; DB.DeliveryListOrders.InsertAllOnSubmit(newOrders.Select((x, index) => new DeliveryListOrder() { ListID = list.ID, OrderID = x.ID, OrderNum = min + index })); DB.SubmitChanges(); } var orderList = DB.DeliveryListOrders.Where(x => x.ListID == list.ID && (x.Order.CarID.HasValue || x.Order.WorkerID.HasValue)).ToList(); foreach (var listOrder in orderList) { if (listOrder.Order.OrderDelivery.DeliveryType == (int)SectorTypes.CarSector) { var storeCheck = DB.DeliveryListOrders.Where( x => x.StoreID.HasValue && x.ListID == list.ID && x.CarID.HasValue && !x.OrderID.HasValue && x.CarID == listOrder.Order.CarID); if (!storeCheck.Any()) { var stl = stores.Select( (x, index) => new DeliveryListOrder() { ListID = list.ID, StoreID = x.ID, OrderNum = listOrder.OrderNum - index - 1, Type = (int)SectorTypes.CarSector, CarID = listOrder.Order.CarID }) .ToList(); DB.DeliveryListOrders.InsertAllOnSubmit(stl); DB.SubmitChanges(); } } if (listOrder.Order.OrderDelivery.DeliveryType == (int)SectorTypes.CourierSector) { var storeCheck = DB.DeliveryListOrders.Where( x => x.StoreID.HasValue && x.ListID == list.ID && x.WorkerID.HasValue && !x.OrderID.HasValue && x.WorkerID == listOrder.Order.WorkerID); if (!storeCheck.Any()) { var stl = stores.Select( (x, index) => new DeliveryListOrder() { ListID = list.ID, StoreID = x.ID, OrderNum = listOrder.OrderNum - index - 1, Type = (int)SectorTypes.CourierSector, WorkerID = listOrder.Order.WorkerID }) .ToList(); DB.DeliveryListOrders.InsertAllOnSubmit(stl); DB.SubmitChanges(); } } } var emptyCheck = DB.DeliveryListOrders.Where( x => !x.OrderID.HasValue && x.ListID == list.ID && x.StoreID.HasValue).ToList(); foreach (var st in emptyCheck) { var suitable = DB.DeliveryListOrders.Any( x => x.OrderID.HasValue && x.ListID == list.ID && x.Order.OrderDelivery != null && x.Order.OrderDelivery.DeliveryType > 0 && (x.Order.OrderDelivery.DeliveryType == (int)SectorTypes.CarSector ? x.Order.CarID == st.CarID : x.Order.WorkerID == st.WorkerID)); if (!suitable) { DB.DeliveryListOrders.DeleteOnSubmit(st); } } DB.SubmitChanges(); } return(new ContentResult()); }