public ActionResult Duplicate(GrantPrimaryKey grantPrimaryKey, DuplicateGrantViewModel viewModel) { var originalGrant = grantPrimaryKey.EntityObject; Check.EnsureNotNull(originalGrant); var initialAwardGrantModificationForCopy = HttpRequestStorage.DatabaseEntities.GrantModifications.Single(gm => gm.GrantModificationID == viewModel.InitialAwardGrantModificationID); if (!ModelState.IsValid) { return(DuplicateGrantViewEdit(viewModel, originalGrant, initialAwardGrantModificationForCopy.GrantAllocations.ToList())); } var grantStatus = HttpRequestStorage.DatabaseEntities.GrantStatuses.Single(gs => gs.GrantStatusID == viewModel.GrantStatusID); var organization = originalGrant.Organization; var newGrant = Grant.CreateNewBlank(grantStatus, organization); viewModel.UpdateModel(newGrant); newGrant.CFDANumber = originalGrant.CFDANumber; newGrant.GrantTypeID = originalGrant.GrantTypeID; var newGrantModification = GrantModification.CreateNewBlank(newGrant, initialAwardGrantModificationForCopy.GrantModificationStatus); newGrantModification.GrantModificationAmount = viewModel.GrantModificationAmount ?? 0; newGrantModification.GrantModificationStartDate = viewModel.GrantStartDate ?? DateTime.Today; newGrantModification.GrantModificationEndDate = viewModel.GrantEndDate ?? DateTime.Today; newGrantModification.GrantModificationName = GrantModificationPurpose.InitialAward.GrantModificationPurposeName; var newGrantModificationPurpose = GrantModificationGrantModificationPurpose.CreateNewBlank(newGrantModification, GrantModificationPurpose.InitialAward); if (viewModel.GrantAllocationsToDuplicate != null && viewModel.GrantAllocationsToDuplicate.Any()) { foreach (var allocationID in viewModel.GrantAllocationsToDuplicate) { var allocationToCopy = HttpRequestStorage.DatabaseEntities.GrantAllocations.Single(ga => ga.GrantAllocationID == allocationID); var newAllocation = GrantAllocation.CreateNewBlank(newGrantModification); newAllocation.GrantAllocationName = allocationToCopy.GrantAllocationName; newAllocation.StartDate = allocationToCopy.StartDate; newAllocation.EndDate = allocationToCopy.EndDate; // 10/7/20 TK - not sure we wanna copy these but going for it anyways newAllocation.FederalFundCodeID = allocationToCopy.FederalFundCodeID; newAllocation.OrganizationID = allocationToCopy.OrganizationID; newAllocation.DNRUplandRegionID = allocationToCopy.DNRUplandRegionID; newAllocation.DivisionID = allocationToCopy.DivisionID; newAllocation.GrantManagerID = allocationToCopy.GrantManagerID; // 10/7/20 TK - make sure we setup the budgetLineItems for the new allocation newAllocation.CreateAllGrantAllocationBudgetLineItemsByCostType(); } } //need to save changes here, because otherwise the MessageForDisplay will link to an item with a negative ID, causing errors HttpRequestStorage.DatabaseEntities.SaveChanges(); SetMessageForDisplay($"{FieldDefinition.Grant.GetFieldDefinitionLabel()} \"{UrlTemplate.MakeHrefString(newGrant.GetDetailUrl(), newGrant.GrantName)}\" has been created."); return(new ModalDialogFormJsonResult()); //return RedirectToAction(new SitkaRoute<GrantController>(gc => gc.GrantDetail(newGrant.GrantID))); }
public void UpdateModel(Models.GrantModification grantModification) { grantModification.GrantModificationName = GrantModificationName; grantModification.GrantModificationAmount = GrantModificationAmount; //because this is only used for duplication we can just create the GM-GMP without merging foreach (var purpose in GrantModificationPurpose.Select(purposeID => Models.GrantModificationPurpose.All.Single(x => x.GrantModificationPurposeID == purposeID))) { GrantModificationGrantModificationPurpose.CreateNewBlank(grantModification, purpose); } }