public ActionResult SubmitProposal(SetProposalViewModel viewModel) { var existingEmployee = DB.StockHistories .Where(x => x.UserId == viewModel.EmployeeId && x.EndDate == null) .OrderByDescending(x => x.StockHistoryId) .FirstOrDefault(); if (existingEmployee != null) { // Update Stock History if material is already in existingEmployee.EndDate = DateTime.Today; existingEmployee.UpdatedDate = DateTime.Today; existingEmployee.UpdatedById = LoggedUser.EmployeeId; StockHistory stockHistory = new StockHistory(); stockHistory.StockId = existingEmployee.StockId; stockHistory.UserId = viewModel.EmployeeId; stockHistory.StartDate = DateTime.Today; stockHistory.EndDate = null; stockHistory.CreatedDate = DateTime.Today; stockHistory.CreatedById = LoggedUser.EmployeeId; stockHistory.UpdatedDate = null; stockHistory.UpdatedById = null; DB.StockHistories.Add(stockHistory); // Update GATE_ComponentRequest var componentRequest = DB.GATE_ComponentRequest.Find(viewModel.ComponentId); componentRequest.StockHistoryId = existingEmployee.StockHistoryId; DB.SaveChanges(); } else { if (viewModel.EmployeeId != 0) { // Add material into Stock Stock stock = new Stock(); StockHelper stockService = new StockHelper(DB, LoggedUser); string companyCodeName = stockService.FindCompanyCodeNameFromEmployee(viewModel.EmployeeId); string materialCode = stockService.FindMaterialCodeFromComponentId(viewModel.ComponentId); int companyId = DB.Companies.Where(x => x.CompanyCodeName == companyCodeName).Select(x => x.ID).FirstOrDefault(); int materialId = DB.GenericMaterials .Where(x => x.MaterialCode == materialCode) .Select(x => x.GenericMaterialId) .FirstOrDefault(); int seqnember = stockService.GetNextSeqNumber(companyId, materialId); string productCode = stockService.GenerateProductCode(companyCodeName, materialCode, seqnember); stock.ProductCode = productCode; stock.CompanyId = companyId; stock.GenericMaterialId = materialId; stock.SeqNumber = seqnember; stock.Note = null; stock.StockStatusId = StockStatus.InStock; DB.Stocks.Add(stock); DB.SaveChanges(); // Update Stock History for previous employee StockHistory previousEmployee = new StockHistory(); previousEmployee.StockId = stock.StockId; previousEmployee.UserId = viewModel.EmployeeId; previousEmployee.StartDate = DateTime.Today; previousEmployee.EndDate = DateTime.Today; previousEmployee.CreatedById = LoggedUser.EmployeeId; previousEmployee.CreatedDate = DateTime.Today; previousEmployee.UpdatedDate = DateTime.Today; previousEmployee.UpdatedById = LoggedUser.EmployeeId; DB.StockHistories.Add(previousEmployee); DB.SaveChanges(); // Update GATE_ComponentRequest var componentRequest = DB.GATE_ComponentRequest.Find(viewModel.ComponentId); componentRequest.StockHistoryId = previousEmployee.StockHistoryId; // Update Stock History for new employee StockHistory newEmployee = new StockHistory(); newEmployee.StockId = stock.StockId; newEmployee.UserId = DB.GATE_MaterialRequest .Where(x => x.MaterialRequestId == viewModel.MaterialRequestId) .Select(x => x.ConcernedEmployeeId) .FirstOrDefault(); newEmployee.StartDate = DateTime.Today; newEmployee.EndDate = null; newEmployee.CreatedDate = DateTime.Today; newEmployee.CreatedById = LoggedUser.EmployeeId; newEmployee.UpdatedDate = null; newEmployee.UpdatedById = null; DB.StockHistories.Add(newEmployee); DB.SaveChanges(); } // User select a material from Stock, not from an employee else { // Add material into Stock Stock stock = new Stock(); StockHelper stockService = new StockHelper(DB, LoggedUser); string companyCodeName = DB.GATE_MaterialRequest .Where(x => x.MaterialRequestId == viewModel.MaterialRequestId) .Select(x => x.ConcernedEmployee.Company.CompanyCodeName) .FirstOrDefault(); string materialCode = stockService.FindMaterialCodeFromComponentId(viewModel.ComponentId); int companyId = DB.Companies.Where(x => x.CompanyCodeName == companyCodeName).Select(x => x.ID).FirstOrDefault(); int materialId = DB.GenericMaterials .Where(x => x.MaterialCode == materialCode) .Select(x => x.GenericMaterialId) .FirstOrDefault(); int seqnember = stockService.GetNextSeqNumber(companyId, materialId); string productCode = stockService.GenerateProductCode(companyCodeName, materialCode, seqnember); stock.ProductCode = productCode; stock.CompanyId = companyId; stock.GenericMaterialId = materialId; stock.SeqNumber = seqnember; stock.Note = null; stock.StockStatusId = StockStatus.InStock; DB.Stocks.Add(stock); DB.SaveChanges(); // Update Stock History. It is same to indicate the material from Stock StockHistory previousEmployee = new StockHistory(); previousEmployee.StockId = stock.StockId; previousEmployee.UserId = DB.GATE_MaterialRequest .Where(x => x.MaterialRequestId == viewModel.MaterialRequestId) .Select(x => x.ConcernedEmployeeId) .FirstOrDefault(); previousEmployee.StartDate = DateTime.Today; previousEmployee.EndDate = DateTime.Today; previousEmployee.CreatedById = LoggedUser.EmployeeId; previousEmployee.CreatedDate = DateTime.Today; previousEmployee.UpdatedDate = DateTime.Today; previousEmployee.UpdatedById = LoggedUser.EmployeeId; DB.StockHistories.Add(previousEmployee); DB.SaveChanges(); // Update GATE_ComponentRequest var componentRequest = DB.GATE_ComponentRequest.Find(viewModel.ComponentId); componentRequest.StockHistoryId = previousEmployee.StockHistoryId; // Update Stock History for new employee StockHistory newEmployee = new StockHistory(); newEmployee.StockId = stock.StockId; newEmployee.UserId = DB.GATE_MaterialRequest .Where(x => x.MaterialRequestId == viewModel.MaterialRequestId) .Select(x => x.ConcernedEmployeeId) .FirstOrDefault(); newEmployee.StartDate = DateTime.Today; newEmployee.EndDate = null; newEmployee.CreatedDate = DateTime.Today; newEmployee.CreatedById = LoggedUser.EmployeeId; newEmployee.UpdatedDate = null; newEmployee.UpdatedById = null; DB.StockHistories.Add(newEmployee); DB.SaveChanges(); } } Session["SelectedEmployeeId"] = viewModel.EmployeeId; return ConfirmDelivery(viewModel.ComponentId); }