private void DoEquipmentAudit(List <SeniorityAudit> audits, EntityEntry entry, string smUserId) { Equipment changed = (Equipment)entry.Entity; int tempChangedId = changed.Id; // if this is an "empy" record - exit if (tempChangedId <= 0) { return; } Equipment original = Equipments.AsNoTracking() .Include(x => x.LocalArea) .Include(x => x.Owner) .First(a => a.Id == tempChangedId); // compare the old and new if (changed.IsSeniorityAuditRequired(original)) { DateTime currentTime = DateTime.UtcNow; // create the audit entry. SeniorityAudit seniorityAudit = new SeniorityAudit { BlockNumber = original.BlockNumber, EndDate = currentTime }; int tempLocalAreaId = original.LocalArea.Id; int tempOwnerId = original.Owner.Id; changed.SeniorityEffectiveDate = currentTime; seniorityAudit.AppCreateTimestamp = currentTime; seniorityAudit.AppLastUpdateTimestamp = currentTime; seniorityAudit.AppCreateUserid = smUserId; seniorityAudit.AppLastUpdateUserid = smUserId; seniorityAudit.EquipmentId = tempChangedId; seniorityAudit.LocalAreaId = tempLocalAreaId; seniorityAudit.OwnerId = tempOwnerId; if (seniorityAudit.Owner != null) { seniorityAudit.OwnerOrganizationName = seniorityAudit.Owner.OrganizationName; } if (original.SeniorityEffectiveDate != null) { seniorityAudit.StartDate = (DateTime)original.SeniorityEffectiveDate; } seniorityAudit.Seniority = original.Seniority; seniorityAudit.ServiceHoursLastYear = original.ServiceHoursLastYear; seniorityAudit.ServiceHoursTwoYearsAgo = original.ServiceHoursTwoYearsAgo; seniorityAudit.ServiceHoursThreeYearsAgo = original.ServiceHoursThreeYearsAgo; audits.Add(seniorityAudit); } }
private void DoEquipmentAudit(List <SeniorityAudit> audits, EntityEntry entry, string smUserId) { Equipment changed = (Equipment)entry.Entity; Equipment original = new Equipment { SeniorityEffectiveDate = (DateTime?)GetOriginalValue(entry, "SeniorityEffectiveDate"), Seniority = (float?)GetOriginalValue(entry, "Seniority"), LocalArea = (LocalArea)GetOriginalValue(entry, "LocalArea"), BlockNumber = (int?)GetOriginalValue(entry, "BlockNumber"), Owner = (Owner)GetOriginalValue(entry, "Owner"), ServiceHoursLastYear = (float?)GetOriginalValue(entry, "ServiceHoursLastYear"), ServiceHoursTwoYearsAgo = (float?)GetOriginalValue(entry, "ServiceHoursTwoYearsAgo"), ServiceHoursThreeYearsAgo = (float?)GetOriginalValue(entry, "ServiceHoursThreeYearsAgo") }; // Calculate Seniority. In the current UI design it is expected that this occurs after each change to the service hours. changed.CalculateSeniority(); // compare the old and new if (changed.IsSeniorityAuditRequired(original)) { DateTime currentTime = DateTime.UtcNow; // create the audit entry. SeniorityAudit seniorityAudit = new SeniorityAudit { BlockNumber = original.BlockNumber, EndDate = currentTime }; changed.SeniorityEffectiveDate = currentTime; seniorityAudit.Equipment = changed; seniorityAudit.CreateTimestamp = currentTime; seniorityAudit.LastUpdateTimestamp = currentTime; seniorityAudit.CreateUserid = smUserId; seniorityAudit.LastUpdateUserid = smUserId; seniorityAudit.LocalArea = original.LocalArea; seniorityAudit.Owner = original.Owner; if (seniorityAudit.Owner != null) { seniorityAudit.OwnerOrganizationName = seniorityAudit.Owner.OrganizationName; } if (original.SeniorityEffectiveDate != null) { seniorityAudit.StartDate = (DateTime)original.SeniorityEffectiveDate; } seniorityAudit.Seniority = original.Seniority; seniorityAudit.ServiceHoursLastYear = original.ServiceHoursLastYear; seniorityAudit.ServiceHoursTwoYearsAgo = original.ServiceHoursTwoYearsAgo; seniorityAudit.ServiceHoursThreeYearsAgo = original.ServiceHoursThreeYearsAgo; audits.Add(seniorityAudit); } }