public ActionResult DoCreate(FEAOrder model) { // Создаем model.DateCreated = DateTime.Now; model.Status = (short)FEAOrderStatus.Gathering; DataContext.FEAOrders.InsertOnSubmit(model); DataContext.SubmitChanges(); ShowSuccess(string.Format("Новая заявка №{0} успешно создана", model.Id)); return(Redirect(string.Format("/fea/{0}/edit#items", model.Id))); }
public ActionResult ChangeOrderStatus(long id, short newStatus, long newUser, long newWarehouse, string comments, bool createFeaOrder, string redirectUrl, bool extractFromWarehouse = false) { var order = DataContext.LeadOrders.FirstOrDefault(lo => lo.Id == id); if (order == null) { ShowError("Такой заказ не найден"); return(RedirectToAction("Index")); } if (!order.CanChangeStatus(CurrentUser)) { ShowError("Такой заказ не найден"); return(RedirectToAction("Index")); } // Изменяем статус var oldStatus = order.Status; var oldAssignedUserId = order.AssignedUserId; var oldWarehouse = order.AssignedWarehouseId; order.Status = newStatus; order.DateModified = DateTime.Now; if (newUser != oldAssignedUserId) { order.User.LeadOrders.Remove(order); DataContext.Users.First(u => u.Id == newUser).LeadOrders.Add(order); } if (newWarehouse != oldWarehouse) { if (order.Warehouse != null) { order.Warehouse.LeadOrders.Remove(order); } DataContext.Warehouses.First(w => w.Id == newWarehouse).LeadOrders.Add(order); } order.LeadOrderChangements.Add(new LeadOrderChangement() { Author = CurrentUser, Comments = comments, OldStatus = oldStatus, OldAssignedUserId = oldAssignedUserId, OldWarehouseId = oldWarehouse, NewStatus = newStatus, NewAssignedUserId = newUser, NewWarehouseId = newWarehouse, LeadOrder = order, DateCreated = DateTime.Now }); // Сохраняем DataContext.SubmitChanges(); ShowSuccess(string.Format("Статус заказа №{0} для {1} был успешно изменен", order.Id, order.Lead.ToString())); if (extractFromWarehouse && order.Warehouse != null) { // Извлекаем остатки со склада foreach (var orderItem in order.LeadOrderItems) { var warehouseProduct = order.Warehouse.WarehouseProducts.FirstOrDefault(wp => wp.ProductId == orderItem.ProductId); if (warehouseProduct == null) { warehouseProduct = new WarehouseProduct() { DateCreated = DateTime.Now, ProductId = orderItem.ProductId, Quantity = 0, Warehouse = order.Warehouse, Price = orderItem.Price }; order.Warehouse.WarehouseProducts.Add(warehouseProduct); } warehouseProduct.Quantity = warehouseProduct.Quantity - orderItem.Quantity; warehouseProduct.DateModified = DateTime.Now; warehouseProduct.WarehouseProductChangements.Add(new WarehouseProductChangement() { DateCreated = DateTime.Now, Amount = orderItem.Quantity, Direction = (short)WarehouseProductChangementDirection.Outcome, WarehouseProduct = warehouseProduct, Description = String.Format("Изъятие остатков в количестве {0} для выполнения заказа №{1} пользователем {2}", orderItem.Quantity, order.Id, CurrentUser.GetFio()) }); } order.LeadOrderChangements.Add(new LeadOrderChangement() { Author = CurrentUser, NewAssignedUserId = order.AssignedUserId, OldAssignedUserId = order.AssignedUserId, OldStatus = order.Status, NewStatus = order.Status, OldWarehouseId = order.AssignedWarehouseId, NewWarehouseId = order.AssignedWarehouseId, DateCreated = DateTime.Now, Comments = String.Format("Извлечение остатков со склада {0} ({1}) в общем количестве {2}", order.Warehouse.Title, order.Warehouse.City, order.LeadOrderItems.Sum(oi => oi.Quantity)) }); DataContext.SubmitChanges(); ShowSuccess(string.Format("Остатки со склада {0} ({1}) были успешно извлечены", order.Warehouse.Title, order.Warehouse.City)); } // Создаем заявку на дозакуп if (createFeaOrder) { // Ищем менеджера var targetManagers = order.Project.ProjectUsers.Where(u => u.User.HasPermission(Permission.FEA)) .Select(u => u.User) .ToList(); if (targetManagers.Count > 0) { User targetManager = null; targetManager = targetManagers.FirstOrDefault(u => u.RoleId != 1) ?? targetManagers.FirstOrDefault(); if (targetManager != null) { var newFeaOrder = new FEAOrder() { Description = string.Format("Заявка на дозакуп, сформированная автоматически из заказа №{0}", order.Id), DateCreated = DateTime.Now, Status = (short)FEAOrderStatus.Gathering, Manager = targetManager, Project = order.Project, TargetWarehouse = order.Warehouse }; newFeaOrder.FEAOrderItems.AddRange(order.LeadOrderItems.Select(oi => new FEAOrderItem() { DateCreated = DateTime.Now, FEAOrder = newFeaOrder, Price = oi.Price, ProductId = oi.ProductId, Quantity = oi.Quantity * 2 })); newFeaOrder.FEAOrdersStatusChangements.Add(new FEAOrdersStatusChangement() { User = CurrentUser, FEAOrder = newFeaOrder, DateCreated = DateTime.Now, Status = (short)FEAOrderStatus.Gathering, Comments = string.Format("Автоматическое создание заявку на дозакуп из заказа №{0}", order.Id) }); order.LeadOrderChangements.Add(new LeadOrderChangement() { Author = CurrentUser, NewAssignedUserId = order.AssignedUserId, OldAssignedUserId = order.AssignedUserId, OldStatus = order.Status, NewStatus = order.Status, OldWarehouseId = order.AssignedWarehouseId, NewWarehouseId = order.AssignedWarehouseId, DateCreated = DateTime.Now, Comments = String.Format("Автоматическое создание заявки на дозакуп №{0} на пользователя {1}", newFeaOrder.Id, targetManager.GetFio()) }); DataContext.SubmitChanges(); ShowSuccess(string.Format("Заявка на дозакуп №{0} успешно сформирована", newFeaOrder.Id)); } } } return(Redirect(redirectUrl + "#history")); }