/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="equipment"></param> /// <param name="systemId"></param> /// <param name="maxEquipmentIndex"></param> private static void CopyToInstance(DbAppContext dbContext, ImportModels.Equip oldObject, ref HetEquipment equipment, string systemId, ref int maxEquipmentIndex) { try { int?statusIdApproved = StatusHelper.GetStatusId("Approved", "equipmentStatus", dbContext); int?statusIdUnapproved = StatusHelper.GetStatusId("Unapproved", "equipmentStatus", dbContext); int?statusIdArchived = StatusHelper.GetStatusId("Archived", "equipmentStatus", dbContext); if (oldObject.Equip_Id <= 0) { return; } equipment = new HetEquipment { EquipmentId = ++maxEquipmentIndex }; // there is a problem with 1 equipment record // Per BC Bid - the correct Owner Id should be: 8786195 if (oldObject.Equip_Id == 19165) { oldObject.Owner_Popt_Id = 8786195; } // *********************************************** // equipment code // *********************************************** string tempEquipmentCode = ImportUtility.CleanString(oldObject.Equip_Cd).ToUpper(); if (!string.IsNullOrEmpty(tempEquipmentCode)) { equipment.EquipmentCode = tempEquipmentCode; } else { // must have an equipment code: HETS-817 return; } // *********************************************** // set the equipment status // *********************************************** string tempArchive = oldObject.Archive_Cd; string tempStatus = oldObject.Status_Cd.Trim(); if (tempArchive == "Y") { if (statusIdArchived == null) { throw new DataException(string.Format("Status Id cannot be null (EquipmentIndex: {0})", maxEquipmentIndex)); } // archived! equipment.ArchiveCode = "Y"; equipment.ArchiveDate = DateTime.UtcNow; equipment.ArchiveReason = "Imported from BC Bid"; equipment.EquipmentStatusTypeId = (int)statusIdArchived; string tempArchiveReason = ImportUtility.CleanString(oldObject.Archive_Reason); if (!string.IsNullOrEmpty(tempArchiveReason)) { equipment.ArchiveReason = ImportUtility.GetUppercaseFirst(tempArchiveReason); } } else { if (statusIdApproved == null) { throw new DataException(string.Format("Status Id cannot be null (EquipmentIndex: {0})", maxEquipmentIndex)); } if (statusIdUnapproved == null) { throw new DataException(string.Format("Status Id cannot be null (EquipmentIndex: {0})", maxEquipmentIndex)); } equipment.ArchiveCode = "N"; equipment.ArchiveDate = null; equipment.ArchiveReason = null; equipment.EquipmentStatusTypeId = tempStatus == "A" ? (int)statusIdApproved : (int)statusIdUnapproved; equipment.StatusComment = string.Format("Imported from BC Bid ({0})", tempStatus); } // *********************************************** // set equipment attributes // *********************************************** string tempLicense = ImportUtility.CleanString(oldObject.Licence).ToUpper(); if (!string.IsNullOrEmpty(tempLicense)) { equipment.LicencePlate = tempLicense; } equipment.ApprovedDate = ImportUtility.CleanDate(oldObject.Approved_Dt); DateTime?tempReceivedDate = ImportUtility.CleanDate(oldObject.Received_Dt); if (tempReceivedDate != null) { equipment.ReceivedDate = (DateTime)tempReceivedDate; } else { if (equipment.ArchiveCode == "N" && equipment.EquipmentStatusTypeId == statusIdApproved) { throw new DataException(string.Format("Received Date cannot be null (EquipmentIndex: {0}", maxEquipmentIndex)); } } // get the created date and use the timestamp to fix the DateTime?tempCreatedDate = ImportUtility.CleanDateTime(oldObject.Created_Dt); if (tempCreatedDate != null) { int hours = Convert.ToInt32(tempCreatedDate?.ToString("HH")); int minutes = Convert.ToInt32(tempCreatedDate?.ToString("mm")); int secs = Convert.ToInt32(tempCreatedDate?.ToString("ss")); equipment.ReceivedDate = equipment.ReceivedDate.AddHours(hours); equipment.ReceivedDate = equipment.ReceivedDate.AddMinutes(minutes); equipment.ReceivedDate = equipment.ReceivedDate.AddSeconds(secs); } // pay rate float?tempPayRate = ImportUtility.GetFloatValue(oldObject.Pay_Rate); if (tempPayRate != null) { equipment.PayRate = tempPayRate; } // *********************************************** // make, model, year, etc. // *********************************************** string tempType = ImportUtility.CleanString(oldObject.Type); if (!string.IsNullOrEmpty(tempType)) { tempType = ImportUtility.GetCapitalCase(tempType); equipment.Type = tempType; } string tempMake = ImportUtility.CleanString(oldObject.Make); if (!string.IsNullOrEmpty(tempMake)) { tempMake = ImportUtility.GetCapitalCase(tempMake); equipment.Make = tempMake; } // model string tempModel = ImportUtility.CleanString(oldObject.Model).ToUpper(); if (!string.IsNullOrEmpty(tempModel)) { equipment.Model = tempModel; } // year string tempYear = ImportUtility.CleanString(oldObject.Year); if (!string.IsNullOrEmpty(tempYear)) { equipment.Year = tempYear; } // size string tempSize = ImportUtility.CleanString(oldObject.Size); if (!string.IsNullOrEmpty(tempSize)) { tempSize = ImportUtility.GetCapitalCase(tempSize); equipment.Size = tempSize; } // serial number string tempSerialNumber = ImportUtility.CleanString(oldObject.Serial_Num).ToUpper(); if (!string.IsNullOrEmpty(tempSerialNumber)) { equipment.SerialNumber = tempSerialNumber; } // operator string tempOperator = ImportUtility.CleanString(oldObject.Operator); if (!string.IsNullOrEmpty(tempOperator)) { equipment.Operator = tempOperator ?? null; } // *********************************************** // add comment into the notes field // *********************************************** string tempComment = ImportUtility.CleanString(oldObject.Comment); if (!string.IsNullOrEmpty(tempComment)) { tempComment = ImportUtility.GetUppercaseFirst(tempComment); HetNote note = new HetNote { Text = tempComment, IsNoLongerRelevant = false }; if (equipment.HetNote == null) { equipment.HetNote = new List <HetNote>(); } equipment.HetNote.Add(note); } // *********************************************** // add equipment to the correct area // *********************************************** if (oldObject.Area_Id != null) { HetLocalArea area = dbContext.HetLocalArea.AsNoTracking() .FirstOrDefault(x => x.LocalAreaNumber == oldObject.Area_Id); if (area != null) { int tempAreaId = area.LocalAreaId; equipment.LocalAreaId = tempAreaId; } } if (equipment.LocalAreaId == null && equipment.ArchiveCode == "N" && equipment.EquipmentStatusTypeId == statusIdApproved) { throw new DataException(string.Format("Local Area cannot be null (EquipmentIndex: {0}", maxEquipmentIndex)); } // *********************************************** // set the equipment type // *********************************************** if (oldObject.Equip_Type_Id != null) { // get the new id for the "District" Equipment Type string tempEquipmentTypeId = oldObject.Equip_Type_Id.ToString(); HetImportMap equipMap = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldTable == ImportDistrictEquipmentType.OldTable && x.OldKey == tempEquipmentTypeId && x.NewTable == ImportDistrictEquipmentType.NewTable); if (equipMap != null) { HetDistrictEquipmentType distEquipType = dbContext.HetDistrictEquipmentType .FirstOrDefault(x => x.DistrictEquipmentTypeId == equipMap.NewKey); if (distEquipType != null) { int tempEquipmentId = distEquipType.DistrictEquipmentTypeId; equipment.DistrictEquipmentTypeId = tempEquipmentId; } } } if (equipment.DistrictEquipmentTypeId == null && equipment.ArchiveCode == "N" && equipment.EquipmentStatusTypeId == statusIdApproved) { throw new DataException(string.Format("Equipment Type cannot be null (EquipmentIndex: {0}", maxEquipmentIndex)); } // *********************************************** // set the equipment owner // *********************************************** HetImportMap ownerMap = dbContext.HetImportMap.AsNoTracking() .FirstOrDefault(x => x.OldTable == ImportOwner.OldTable && x.OldKey == oldObject.Owner_Popt_Id.ToString()); if (ownerMap != null) { HetOwner owner = dbContext.HetOwner.FirstOrDefault(x => x.OwnerId == ownerMap.NewKey); if (owner != null) { int tempOwnerId = owner.OwnerId; equipment.OwnerId = tempOwnerId; // set address fields on the owner record if (string.IsNullOrEmpty(owner.Address1)) { string tempAddress1 = ImportUtility.CleanString(oldObject.Addr1); tempAddress1 = ImportUtility.GetCapitalCase(tempAddress1); string tempAddress2 = ImportUtility.CleanString(oldObject.Addr2); tempAddress2 = ImportUtility.GetCapitalCase(tempAddress2); string tempCity = ImportUtility.CleanString(oldObject.City); tempCity = ImportUtility.GetCapitalCase(tempCity); owner.Address1 = tempAddress1; owner.Address2 = tempAddress2; owner.City = tempCity; owner.PostalCode = ImportUtility.CleanString(oldObject.Postal).ToUpper(); owner.Province = "BC"; dbContext.HetOwner.Update(owner); } } } if (equipment.OwnerId == null && equipment.ArchiveCode != "Y") { throw new DataException(string.Format("Owner cannot be null (EquipmentIndex: {0}", maxEquipmentIndex)); } // *********************************************** // set seniority and hours // *********************************************** float?tempSeniority = ImportUtility.GetFloatValue(oldObject.Seniority); equipment.Seniority = tempSeniority ?? 0.0F; float?tempYearsOfService = ImportUtility.GetFloatValue(oldObject.Num_Years); equipment.YearsOfService = tempYearsOfService ?? 0.0F; int?tempBlockNumber = ImportUtility.GetIntValue(oldObject.Block_Num); equipment.BlockNumber = tempBlockNumber ?? null; float?tempServiceHoursLastYear = ImportUtility.GetFloatValue(oldObject.YTD1); equipment.ServiceHoursLastYear = tempServiceHoursLastYear ?? 0.0F; float?tempServiceHoursTwoYearsAgo = ImportUtility.GetFloatValue(oldObject.YTD2); equipment.ServiceHoursTwoYearsAgo = tempServiceHoursTwoYearsAgo ?? 0.0F; float?tempServiceHoursThreeYearsAgo = ImportUtility.GetFloatValue(oldObject.YTD3); equipment.ServiceHoursThreeYearsAgo = tempServiceHoursThreeYearsAgo ?? 0.0F; // *********************************************** // using the "to date" field to store the // equipment "Last_Dt" (hopefully the last time // this equipment was hired) // *********************************************** DateTime?tempLastDate = ImportUtility.CleanDate(oldObject.Last_Dt); if (tempLastDate != null) { equipment.ToDate = (DateTime)tempLastDate; } // *********************************************** // set last verified date (default to March 31, 2018) // *********************************************** equipment.LastVerifiedDate = DateTime.Parse("2018-03-31 0:00:01"); // *********************************************** // create equipment // *********************************************** equipment.AppCreateUserid = systemId; equipment.AppCreateTimestamp = DateTime.UtcNow; equipment.AppLastUpdateUserid = systemId; equipment.AppLastUpdateTimestamp = DateTime.UtcNow; dbContext.HetEquipment.Add(equipment); } catch (Exception ex) { Debug.WriteLine("***Error*** - Equipment Code: " + equipment.EquipmentCode); Debug.WriteLine("***Error*** - Master Equipment Index: " + maxEquipmentIndex); Debug.WriteLine(ex.Message); throw; } }