private int CreateRequestFromParent(int parentID) { //Родительская заявка CustomerRequest parentCR = _context.CustomerRequests .Include(c => c.Customer) .Include(c => c.Program) .Include(c => c.RequestElementTypes) .ThenInclude(c => c.ElementType) .FirstOrDefault(m => m.CustomerRequestID == parentID); //новая заявка CustomerRequest newCR = new CustomerRequest { Customer = parentCR.Customer, TestProgramID = ChildProgramID(parentCR.Program.TestProgramID), RequestDate = parentCR.RequestDate, Description = parentCR.Description + ". Создана из заявки " + parentID + ".", RequestNumber = parentCR.RequestNumber, ParentCustomerRequestID = parentCR.CustomerRequestID }; //сохранить _context.CustomerRequests.Add(newCR); int num = _context.SaveChanges(); int id = newCR.CustomerRequestID; newCR = _context.CustomerRequests .Include(c => c.Program) .ThenInclude(c => c.ElementntTypes) .ThenInclude(e => e.ChainItems) .ThenInclude(r => r.Operation) .Include(c => c.RequestElementTypes) .FirstOrDefault(m => m.CustomerRequestID == id); if (!newCR.IsProceed) { List <RequestElementType> retList = new List <RequestElementType>(); foreach (ElementType e in newCR.Program.ElementntTypes) { RequestElementType r = new RequestElementType { ElementType = e, Order = e.Order }; r.BatchCount = 0; r.ItemCount = 0; r.KitCount = 0; //перебираем элементы родительской заявки foreach (var parentRET in parentCR.RequestElementTypes) { if (parentRET.ElementType.ChildElementTypeID == e.ElementTypeID) { //копируем из родительского элемента r.KitCount += parentRET.KitCount; r.BatchCount += parentRET.BatchCount; r.ItemCount += parentRET.ItemCount; } } List <RequestOperation> roList = new List <RequestOperation>(); foreach (TestChainItem tci in e.ChainItems) { RequestOperation rO = new RequestOperation { TestChainItem = tci, RequestElementType = r, IsExecute = tci.Operation.IsExecuteDefault, SampleCount = tci.Operation.SampleCount, ExecuteCount = 1 }; roList.Add(rO); r.RequestOperations = roList; } retList.Add(r); } newCR.RequestElementTypes = retList; newCR.IsProceed = true; } num = _context.SaveChanges(); return(newCR.CustomerRequestID); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } CustomerRequest.CreateDate = System.DateTime.Now; if (UserID > 0) { CustomerRequest.CreateUserID = UserID; } _context.CustomerRequests.Add(CustomerRequest); // await _context.SaveChangesAsync(); int num = _context.SaveChanges(); int id = CustomerRequest.CustomerRequestID; CustomerRequest = await _context.CustomerRequests .Include(c => c.Customer) .Include(c => c.Program) .ThenInclude(c => c.ElementntTypes) .ThenInclude(e => e.ChainItems) .ThenInclude(r => r.Operation) .Include(c => c.RequestElementTypes) .FirstOrDefaultAsync(m => m.CustomerRequestID == id); if (!CustomerRequest.IsProceed) { List <RequestElementType> retList = new List <RequestElementType>(); foreach (ElementType e in CustomerRequest.Program.ElementntTypes) { RequestElementType r = new RequestElementType { ElementType = e, BatchCount = 0, ItemCount = 0, Order = e.Order }; List <RequestOperation> roList = new List <RequestOperation>(); foreach (TestChainItem tci in e.ChainItems) { RequestOperation rO = new RequestOperation { TestChainItem = tci, RequestElementType = r, IsExecute = tci.Operation.IsExecuteDefault, SampleCount = tci.Operation.SampleCount, ExecuteCount = 1 }; roList.Add(rO); r.RequestOperations = roList; } retList.Add(r); } CustomerRequest.RequestElementTypes = retList; CustomerRequest.IsProceed = true; } num = _context.SaveChanges(); return(RedirectToPage("./Edit", new { id = id })); }