public virtual IActionResult ContactsIdDeletePost([FromRoute] int id) { bool exists = _context.HetContact.Any(a => a.ContactId == id); // not found if (!exists) { return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); } HetContact item = _context.HetContact.First(a => a.ContactId == id); // check if this is a project - and if this is a "primary contact" if (item.ProjectId != null && item.ProjectId > 0) { int projectId = (int)item.ProjectId; HetProject project = _context.HetProject .FirstOrDefault(x => x.ProjectId == projectId); if (project != null && project.PrimaryContactId == id) { project.PrimaryContactId = null; _context.HetProject.Update(project); } } _context.HetContact.Remove(item); // save the changes _context.SaveChanges(); return(new ObjectResult(new HetsResponse(item))); }
public virtual IActionResult ProjectIdAttachmentsPost([FromRoute] int id, [FromForm] IList <IFormFile> files) { // validate the id bool exists = _context.HetProject.Any(a => a.ProjectId == id); if (!exists) { return(new StatusCodeResult(404)); } HetProject project = _context.HetProject .Include(x => x.HetDigitalFile) .First(a => a.ProjectId == id); foreach (IFormFile file in files) { if (file.Length > 0) { HetDigitalFile attachment = new HetDigitalFile(); // strip out extra info in the file name if (!string.IsNullOrEmpty(file.FileName)) { attachment.FileName = Path.GetFileName(file.FileName); } // allocate storage for the file and create a memory stream attachment.FileContents = new byte[file.Length]; using (MemoryStream fileStream = new MemoryStream(attachment.FileContents)) { file.CopyTo(fileStream); } attachment.Type = GetType(attachment.FileName); // set the mime type id int?mimeTypeId = StatusHelper.GetMimeTypeId(attachment.Type, _context); if (mimeTypeId == null) { throw new DataException("Mime Type Id cannot be null"); } attachment.MimeTypeId = (int)mimeTypeId; project.HetDigitalFile.Add(attachment); } } _context.SaveChanges(); return(new ObjectResult(project.HetDigitalFile)); }
/// <summary> /// Convert to Project Lite Model /// </summary> /// <param name="project"></param> public static ProjectLite ToLiteModel(HetProject project) { ProjectLite projectLite = new ProjectLite(); if (project != null) { projectLite.Id = project.ProjectId; projectLite.Status = project.ProjectStatusType?.Description; projectLite.Name = project.Name; projectLite.PrimaryContact = project.PrimaryContact; projectLite.District = project.District; projectLite.Requests = project.HetRentalRequest?.Count; projectLite.Hires = project.HetRentalAgreement?.Count; } return(projectLite); }
public static List <History> GetHistoryRecords(int id, int?offset, int?limit, DbAppContext context) { HetProject project = context.HetProject.AsNoTracking() .Include(x => x.HetHistory) .First(a => a.ProjectId == id); List <HetHistory> data = project.HetHistory .OrderByDescending(y => y.AppLastUpdateTimestamp) .ToList(); if (offset == null) { offset = 0; } if (limit == null) { limit = data.Count - offset; } List <History> result = new List <History>(); for (int i = (int)offset; i < data.Count && i < offset + limit; i++) { History temp = new History(); if (data[i] != null) { temp.HistoryText = data[i].HistoryText; temp.Id = data[i].HistoryId; temp.LastUpdateTimestamp = data[i].AppLastUpdateTimestamp; temp.LastUpdateUserid = data[i].AppLastUpdateUserid; temp.AffectedEntityId = data[i].ProjectId; } result.Add(temp); } return(result); }
/// <summary> /// Get a Project record /// </summary> /// <param name="id"></param> /// <param name="context"></param> /// <returns></returns> public static HetProject GetRecord(int id, DbAppContext context) { HetProject project = context.HetProject.AsNoTracking() .Include(x => x.ProjectStatusType) .Include(x => x.District) .ThenInclude(x => x.Region) .Include(x => x.HetContact) .Include(x => x.PrimaryContact) .Include(x => x.HetRentalRequest) .ThenInclude(y => y.DistrictEquipmentType) .Include(x => x.HetRentalRequest) .ThenInclude(y => y.RentalRequestStatusType) .Include(x => x.HetRentalRequest) .ThenInclude(y => y.HetRentalRequestRotationList) .Include(x => x.HetRentalAgreement) .ThenInclude(y => y.Equipment) .ThenInclude(z => z.DistrictEquipmentType) .Include(x => x.HetRentalAgreement) .ThenInclude(y => y.RentalAgreementStatusType) .FirstOrDefault(a => a.ProjectId == id); if (project != null) { project.Status = project.ProjectStatusType.ProjectStatusTypeCode; // calculate the number of hired (yes or forced hire) equipment // count active requests (In Progress) int countActiveRequests = 0; foreach (HetRentalRequest rentalRequest in project.HetRentalRequest) { rentalRequest.Status = rentalRequest.RentalRequestStatusType.RentalRequestStatusTypeCode; int temp = 0; foreach (HetRentalRequestRotationList equipment in rentalRequest.HetRentalRequestRotationList) { if (equipment.OfferResponse != null && equipment.OfferResponse.ToLower().Equals("yes")) { temp++; } if (equipment.IsForceHire != null && equipment.IsForceHire == true) { temp++; } } rentalRequest.YesCount = temp; rentalRequest.HetRentalRequestRotationList = null; if (rentalRequest.RentalRequestStatusType.RentalRequestStatusTypeCode == null || rentalRequest.RentalRequestStatusType.RentalRequestStatusTypeCode .Equals(HetRentalRequest.StatusInProgress)) { countActiveRequests++; } } // count active agreements (Active) int countActiveAgreements = 0; foreach (HetRentalAgreement rentalAgreement in project.HetRentalAgreement) { rentalAgreement.Status = rentalAgreement.RentalAgreementStatusType.RentalAgreementStatusTypeCode; if (rentalAgreement.RentalAgreementStatusType.RentalAgreementStatusTypeCode == null || rentalAgreement.RentalAgreementStatusType.RentalAgreementStatusTypeCode .Equals(HetRentalAgreement.StatusActive)) { countActiveAgreements++; } // workaround for converted records from Bc Bid if (rentalAgreement.Number.StartsWith("BCBid")) { rentalAgreement.RentalRequestId = -1; rentalAgreement.RentalRequestRotationListId = -1; } } // Only allow editing the "Status" field under the following conditions: // * If Project.status is currently "Active" AND // (All child RentalRequests.Status != "In Progress" AND All child RentalAgreement.status != "Active")) // * If Project.status is currently != "Active" if (project.ProjectStatusType.ProjectStatusTypeCode.Equals(HetProject.StatusActive) && (countActiveRequests > 0 || countActiveAgreements > 0)) { project.CanEditStatus = false; } else { project.CanEditStatus = true; } } return(project); }
/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="rotationDoc"></param> /// <param name="systemId"></param> /// <param name="maxIndex"></param> private static void CopyToInstance(DbAppContext dbContext, ImportModels.RotationDoc oldObject, ref BcbidRotationDoc rotationDoc, string systemId, ref int maxIndex) { try { if (rotationDoc != null) { return; } rotationDoc = new BcbidRotationDoc { NoteId = oldObject.Note_Id }; ++maxIndex; // *********************************************** // we only need records from the current fiscal // so ignore all others // *********************************************** DateTime fiscalStart; if (DateTime.UtcNow.Month == 1 || DateTime.UtcNow.Month == 2 || DateTime.UtcNow.Month == 3) { fiscalStart = new DateTime(DateTime.UtcNow.AddYears(-1).Year, 4, 1); } else { fiscalStart = new DateTime(DateTime.UtcNow.Year, 4, 1); } // *********************************************** // set rotation data // *********************************************** string noteType = oldObject.Note_Type; if (string.IsNullOrEmpty(noteType)) { return; } rotationDoc.NoteType = noteType.Trim(); // reason string reason = oldObject.Reason; if (!string.IsNullOrEmpty(reason)) { rotationDoc.Reason = reason; } // asked date DateTime?createdDate = ImportUtility.CleanDate(oldObject.Created_Dt); if (createdDate == null || createdDate < fiscalStart) { return; // move to next } rotationDoc.AskedDate = (DateTime)createdDate; // was asked -- ForceHire if (noteType.ToUpper() == "FORCEHIRE") { rotationDoc.WasAsked = false; rotationDoc.IsForceHire = true; } else { rotationDoc.WasAsked = true; rotationDoc.IsForceHire = false; } // setup the reason string tempResponse = ""; if (noteType.ToUpper() == "FORCEHIRE") { tempResponse = "Force Hire"; } else if (noteType.ToUpper() == "NOHIRE") { tempResponse = "No Hire"; } else { switch (noteType.ToUpper()) { case "0": tempResponse = "Owner didn't call back/no answer"; break; case "1": tempResponse = "Equipment not suitable"; break; case "2": tempResponse = "Working elsewhere"; break; case "3": tempResponse = "No agreement on rates"; break; case "4": tempResponse = "Equipment under repairs"; break; case "5": tempResponse = "Work limit reached"; break; case "6": tempResponse = "No WCB/WCB in arrears"; break; case "7": tempResponse = "No insurance/inadequate insurance"; break; case "8": tempResponse = "Not interested/turned job down"; break; case "9": tempResponse = "Equipment not available"; break; case "10": tempResponse = "Other"; break; } } if (string.IsNullOrEmpty(tempResponse)) { tempResponse = noteType; } rotationDoc.OfferRefusalReason = tempResponse; // ************************************************ // get the imported equipment record map // ************************************************ string tempId = oldObject.Equip_Id.ToString(); HetImportMap mapEquip = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempId && x.OldTable == ImportEquip.OldTable && x.NewTable == ImportEquip.NewTable); if (mapEquip == null) { return; // ignore and move to the next record } // *********************************************** // find the equipment record // *********************************************** HetEquipment equipment = dbContext.HetEquipment.AsNoTracking() .Include(x => x.LocalArea) .ThenInclude(y => y.ServiceArea) .ThenInclude(z => z.District) .FirstOrDefault(x => x.EquipmentId == mapEquip.NewKey); if (equipment == null) { return; // ignore and move to the next record } int tempNewEquipmentId = equipment.EquipmentId; rotationDoc.EquipmentId = tempNewEquipmentId; // ************************************************ // get the imported project record map // ************************************************ string tempProjectId = oldObject.Project_Id.ToString(); HetImportMap mapProject = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempProjectId && x.OldTable == ImportProject.OldTable && x.NewTable == ImportProject.NewTable); // *********************************************** // find the project record // *********************************************** HetProject project; if (mapProject != null) { project = dbContext.HetProject.AsNoTracking() .FirstOrDefault(x => x.ProjectId == mapProject.NewKey); if (project == null) { throw new ArgumentException(string.Format("Cannot locate Project record (Rotation Doc Id: {0}", tempId)); } int tempNewProjectId = project.ProjectId; rotationDoc.ProjectId = tempNewProjectId; } else { int districtId = equipment.LocalArea.ServiceArea.District.DistrictId; int?statusId = StatusHelper.GetStatusId(HetProject.StatusComplete, "projectStatus", dbContext); if (statusId == null) { throw new DataException(string.Format("Status Id cannot be null (Time Sheet Equip Id: {0}", tempId)); } // create new project project = new HetProject { DistrictId = districtId, Information = "Created to support Rotation Doc import from BCBid", ProjectStatusTypeId = (int)statusId, Name = "Legacy BCBid Project", AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; // save now so we can access it for other time records dbContext.HetProject.Add(project); dbContext.SaveChangesForImport(); // add mapping record ImportUtility.AddImportMapForProgress(dbContext, ImportProject.OldTable, tempProjectId, project.ProjectId, ImportProject.NewTable); dbContext.SaveChangesForImport(); } // *********************************************** // create rotationDoc // *********************************************** rotationDoc.AppCreateUserid = systemId; rotationDoc.AppCreateTimestamp = DateTime.UtcNow; rotationDoc.AppLastUpdateUserid = systemId; rotationDoc.AppLastUpdateTimestamp = DateTime.UtcNow; dbContext.BcbidRotationDoc.Add(rotationDoc); } catch (Exception ex) { Debug.WriteLine("***Error*** - Master Rotation Doc Index: " + maxIndex); Debug.WriteLine(ex.Message); throw; } }
/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="timeRecord"></param> /// <param name="systemId"></param> /// <param name="maxTimeSheetIndex"></param> private static void CopyToTimeRecorded(DbAppContext dbContext, ImportModels.EquipUsage oldObject, ref HetTimeRecord timeRecord, string systemId, ref int maxTimeSheetIndex) { try { if (oldObject.Equip_Id <= 0) { return; } if (oldObject.Project_Id <= 0) { return; } // *********************************************** // we only need records from the current fiscal // so ignore all others // *********************************************** DateTime fiscalStart; if (DateTime.UtcNow.Month == 1 || DateTime.UtcNow.Month == 2 || DateTime.UtcNow.Month == 3) { fiscalStart = new DateTime(DateTime.UtcNow.AddYears(-1).Year, 4, 1); } else { fiscalStart = new DateTime(DateTime.UtcNow.Year, 4, 1); } string tempRecordDate = oldObject.Worked_Dt; if (string.IsNullOrEmpty(tempRecordDate)) { return; // ignore if we don't have a created date } if (!string.IsNullOrEmpty(tempRecordDate)) { DateTime?recordDate = ImportUtility.CleanDate(tempRecordDate); if (recordDate == null || recordDate < fiscalStart) { return; // ignore this record - it is outside of the fiscal years } } // ************************************************ // get the imported equipment record map // ************************************************ string tempId = oldObject.Equip_Id.ToString(); HetImportMap mapEquip = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempId && x.OldTable == ImportEquip.OldTable && x.NewTable == ImportEquip.NewTable); if (mapEquip == null) { return; // ignore and move to the next record } // *********************************************** // find the equipment record // *********************************************** HetEquipment equipment = dbContext.HetEquipment.AsNoTracking() .Include(x => x.LocalArea) .ThenInclude(y => y.ServiceArea) .ThenInclude(z => z.District) .FirstOrDefault(x => x.EquipmentId == mapEquip.NewKey); if (equipment == null) { return; // ignore and move to the next record } // ************************************************ // get the imported project record map // ************************************************ string tempProjectId = oldObject.Project_Id.ToString(); HetImportMap mapProject = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldKey == tempProjectId && x.OldTable == ImportProject.OldTable && x.NewTable == ImportProject.NewTable); // *********************************************** // find the project record // (or create a project (inactive)) // *********************************************** HetProject project; if (mapProject != null) { project = dbContext.HetProject.AsNoTracking() .FirstOrDefault(x => x.ProjectId == mapProject.NewKey); if (project == null) { throw new ArgumentException(string.Format("Cannot locate Project record (Time Sheet Equip Id: {0}", tempId)); } } else { int districtId = equipment.LocalArea.ServiceArea.District.DistrictId; int?statusId = StatusHelper.GetStatusId(HetProject.StatusComplete, "projectStatus", dbContext); if (statusId == null) { throw new DataException(string.Format("Status Id cannot be null (Time Sheet Equip Id: {0}", tempId)); } // create new project project = new HetProject { DistrictId = districtId, Information = "Created to support Time Record import from BCBid", ProjectStatusTypeId = (int)statusId, Name = "Legacy BCBid Project", AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; // save now so we can access it for other time records dbContext.HetProject.Add(project); dbContext.SaveChangesForImport(); // add mapping record ImportUtility.AddImportMapForProgress(dbContext, ImportProject.OldTable, tempProjectId, project.ProjectId, ImportProject.NewTable); dbContext.SaveChangesForImport(); } // *********************************************** // find or create the rental agreement // *********************************************** DateTime?enteredDate = ImportUtility.CleanDate(oldObject.Entered_Dt); // use for the agreement HetRentalAgreement agreement = dbContext.HetRentalAgreement.AsNoTracking() .FirstOrDefault(x => x.EquipmentId == equipment.EquipmentId && x.ProjectId == project.ProjectId && x.DistrictId == equipment.LocalArea.ServiceArea.District.DistrictId); if (agreement == null) { int equipmentId = equipment.EquipmentId; int projectId = project.ProjectId; int districtId = equipment.LocalArea.ServiceArea.District.DistrictId; int?statusId = StatusHelper.GetStatusId(HetRentalAgreement.StatusComplete, "rentalAgreementStatus", dbContext); if (statusId == null) { throw new DataException(string.Format("Status Id cannot be null (Time Sheet Equip Id: {0}", tempId)); } int?agrRateTypeId = StatusHelper.GetRatePeriodId(HetRatePeriodType.PeriodDaily, dbContext); if (agrRateTypeId == null) { throw new DataException("Rate Period Id cannot be null"); } int?year = (ImportUtility.CleanDate(oldObject.Worked_Dt))?.Year; // create a new agreement record agreement = new HetRentalAgreement { EquipmentId = equipmentId, ProjectId = projectId, DistrictId = districtId, RentalAgreementStatusTypeId = (int)statusId, RatePeriodTypeId = (int)agrRateTypeId, Note = "Created to support Time Record import from BCBid", Number = string.Format("BCBid{0}-{1}-{2}", projectId, equipmentId, year), DatedOn = enteredDate, AppCreateUserid = systemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; // save now so we can access it for other time records dbContext.HetRentalAgreement.Add(agreement); dbContext.SaveChangesForImport(); } // *********************************************** // create time record // *********************************************** timeRecord = new HetTimeRecord { TimeRecordId = ++maxTimeSheetIndex }; // *********************************************** // set time period type // *********************************************** int?timePeriodTypeId = StatusHelper.GetTimePeriodId(HetTimePeriodType.PeriodDay, dbContext); if (timePeriodTypeId == null) { throw new DataException("Time Period Id cannot be null"); } timeRecord.TimePeriodTypeId = (int)timePeriodTypeId; // *********************************************** // set time record attributes // *********************************************** DateTime?workedDate = ImportUtility.CleanDate(oldObject.Worked_Dt); if (workedDate != null) { timeRecord.WorkedDate = (DateTime)workedDate; } else { throw new DataException(string.Format("Worked Date cannot be null (TimeSheet Index: {0}", maxTimeSheetIndex)); } // get hours worked float?tempHoursWorked = ImportUtility.GetFloatValue(oldObject.Hours); if (tempHoursWorked != null) { timeRecord.Hours = tempHoursWorked; } else { throw new DataException(string.Format("Hours cannot be null (TimeSheet Index: {0}", maxTimeSheetIndex)); } if (enteredDate != null) { timeRecord.EnteredDate = (DateTime)enteredDate; } else { throw new DataException(string.Format("Entered Date cannot be null (TimeSheet Index: {0}", maxTimeSheetIndex)); } // *********************************************** // create time record // *********************************************** int raId = agreement.RentalAgreementId; timeRecord.RentalAgreementId = raId; timeRecord.AppCreateUserid = systemId; timeRecord.AppCreateTimestamp = DateTime.UtcNow; timeRecord.AppLastUpdateUserid = systemId; timeRecord.AppLastUpdateTimestamp = DateTime.UtcNow; dbContext.HetTimeRecord.Add(timeRecord); } catch (Exception ex) { Debug.WriteLine("***Error*** - Worked Date: " + oldObject.Worked_Dt); Debug.WriteLine("***Error*** - Master Time Record Index: " + maxTimeSheetIndex); Debug.WriteLine(ex.Message); throw; } }
/// <summary> /// Import Projects /// </summary> /// <param name="performContext"></param> /// <param name="dbContext"></param> /// <param name="fileLocation"></param> /// <param name="systemId"></param> public static void Import(PerformContext performContext, DbAppContext dbContext, string fileLocation, string systemId) { // check the start point. If startPoint == sigId then it is already completed int startPoint = ImportUtility.CheckInterMapForStartPoint(dbContext, OldTableProgress, BcBidImport.SigId, NewTable); if (startPoint == BcBidImport.SigId) // this means the import job it has done today is complete for all the records in the xml file. { performContext.WriteLine("*** Importing " + XmlFileName + " is complete from the former process ***"); return; } int maxProjectIndex = 0; if (dbContext.HetProject.Any()) { maxProjectIndex = dbContext.HetProject.Max(x => x.ProjectId); } try { string rootAttr = "ArrayOf" + OldTable; // create progress indicator performContext.WriteLine("Processing " + OldTable); var progress = performContext.WriteProgressBar(); progress.SetValue(0); // create serializer and serialize xml file XmlSerializer ser = new XmlSerializer(typeof(Project[]), new XmlRootAttribute(rootAttr)); ser.UnknownAttribute += ImportUtility.UnknownAttribute; ser.UnknownElement += ImportUtility.UnknownElement; MemoryStream memoryStream = ImportUtility.MemoryStreamGenerator(XmlFileName, OldTable, fileLocation, rootAttr); Project[] legacyItems = (Project[])ser.Deserialize(memoryStream); int ii = startPoint; // skip the portion already processed if (startPoint > 0) { legacyItems = legacyItems.Skip(ii).ToArray(); } Debug.WriteLine("Importing Project Data. Total Records: " + legacyItems.Length); foreach (Project item in legacyItems.WithProgress(progress)) { // see if we have this one already HetImportMap importMap = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldTable == OldTable && x.OldKey == item.Project_Id.ToString()); // new entry if (importMap == null && item.Project_Id > 0) { HetProject instance = null; CopyToInstance(dbContext, item, ref instance, systemId, ref maxProjectIndex); ImportUtility.AddImportMap(dbContext, OldTable, item.Project_Id.ToString(), NewTable, instance.ProjectId); // save has to be done immediately because we need access to the records dbContext.SaveChangesForImport(); } // periodically save change to the progress record if (++ii % 1000 == 0) { try { ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, ii.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChangesForImport(); } catch (Exception e) { performContext.WriteLine("Error saving data " + e.Message); } } } try { performContext.WriteLine("*** Importing " + XmlFileName + " is Done ***"); ImportUtility.AddImportMapForProgress(dbContext, OldTableProgress, BcBidImport.SigId.ToString(), BcBidImport.SigId, NewTable); dbContext.SaveChangesForImport(); } catch (Exception e) { string temp = string.Format("Error saving data (ProjectIndex: {0}): {1}", maxProjectIndex, e.Message); performContext.WriteLine(temp); throw new DataException(temp); } } catch (Exception e) { performContext.WriteLine("*** ERROR ***"); performContext.WriteLine(e.ToString()); throw; } }
/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="project"></param> /// <param name="systemId"></param> /// <param name="maxProjectIndex"></param> private static void CopyToInstance(DbAppContext dbContext, Project oldObject, ref HetProject project, string systemId, ref int maxProjectIndex) { try { if (project != null) { return; } project = new HetProject { ProjectId = ++maxProjectIndex }; // *********************************************** // set project properties // *********************************************** string tempProjectNumber = ImportUtility.CleanString(oldObject.Project_Num).ToUpper(); if (!string.IsNullOrEmpty(tempProjectNumber)) { project.ProvincialProjectNumber = tempProjectNumber; } // project name string tempName = ImportUtility.CleanString(oldObject.Job_Desc1).ToUpper(); if (!string.IsNullOrEmpty(tempName)) { project.Name = ImportUtility.GetCapitalCase(tempName); } // project information string tempInformation = ImportUtility.CleanString(oldObject.Job_Desc2); if (!string.IsNullOrEmpty(tempInformation)) { tempInformation = ImportUtility.GetUppercaseFirst(tempInformation); project.Information = tempInformation; } // *********************************************** // set service area for the project // *********************************************** HetServiceArea serviceArea = dbContext.HetServiceArea.AsNoTracking() .Include(x => x.District) .FirstOrDefault(x => x.MinistryServiceAreaId == oldObject.Service_Area_Id); if (serviceArea == null) { throw new DataException(string.Format("Service Area cannot be null (ProjectIndex: {0}", maxProjectIndex)); } int tempDistrictId = serviceArea.District.DistrictId; project.DistrictId = tempDistrictId; // *********************************************** // check that we don't have this equipment type // already (from another service area - but same district) // *********************************************** HetProject existingProject = dbContext.HetProject.AsNoTracking() .Include(x => x.ProjectStatusType) .Include(x => x.District) .FirstOrDefault(x => x.Name == tempName && x.District.DistrictId == tempDistrictId); if (existingProject != null) { project.ProjectId = existingProject.ProjectId; return; // not adding a duplicate } // *********************************************** // default the project to Active // *********************************************** int?statusId = StatusHelper.GetStatusId(HetProject.StatusActive, "projectStatus", dbContext); if (statusId == null) { throw new DataException(string.Format("Status Id cannot be null (ProjectIndex: {0}", maxProjectIndex)); } project.ProjectStatusTypeId = (int)statusId; // *********************************************** // create project // *********************************************** project.AppCreateUserid = systemId; project.AppCreateTimestamp = DateTime.UtcNow; project.AppLastUpdateUserid = systemId; project.AppLastUpdateTimestamp = DateTime.UtcNow; dbContext.HetProject.Add(project); } catch (Exception ex) { Debug.WriteLine("***Error*** - Project Name: " + oldObject.Job_Desc1); Debug.WriteLine("***Error*** - Master project Index: " + maxProjectIndex); Debug.WriteLine(ex.Message); throw; } }