/// <summary>
        /// Determine if a Seniority Audit Record is required
        /// </summary>
        /// <param name="changed"></param>
        /// <returns>True if the changed record differs from this one</returns>
        public bool IsSeniorityAuditRequired(HetEquipment changed)
        {
            // change to seniority -> write audit
            if (Seniority != null && Seniority != changed.Seniority)
            {
                return(true);
            }

            // change to local area -> write audit
            if (LocalArea != null && LocalArea.LocalAreaId != changed.LocalArea.LocalAreaId)
            {
                return(true);
            }

            // change to block number -> write audit
            if (BlockNumber != null && BlockNumber != changed.BlockNumber)
            {
                return(true);
            }

            // change to owner -> write audit
            if (Owner != null && Owner.OwnerId != changed.Owner.OwnerId)
            {
                return(true);
            }

            // change to service hours
            if (ServiceHoursLastYear != null && ServiceHoursLastYear != changed.ServiceHoursLastYear)
            {
                return(true);
            }

            if (ServiceHoursTwoYearsAgo != null && ServiceHoursTwoYearsAgo != changed.ServiceHoursTwoYearsAgo)
            {
                return(true);
            }

            if (ServiceHoursThreeYearsAgo != null && ServiceHoursThreeYearsAgo != changed.ServiceHoursThreeYearsAgo)
            {
                return(true);
            }

            return(false);
        }
        private void DoEquipmentAudit(List <HetSeniorityAudit> audits, EntityEntry entry, string smUserId)
        {
            HetEquipment changed = (HetEquipment)entry.Entity;

            int tempChangedId = changed.EquipmentId;

            // if this is an "empty" record - exit
            if (tempChangedId <= 0)
            {
                return;
            }

            HetEquipment original = HetEquipment.AsNoTracking()
                                    .Include(x => x.LocalArea)
                                    .Include(x => x.Owner)
                                    .FirstOrDefault(a => a.EquipmentId == tempChangedId);

            // record doesn't exist
            if (original == null)
            {
                return;
            }

            // compare the old and new
            if (changed.IsSeniorityAuditRequired(original))
            {
                DateTime currentTime = DateTime.UtcNow;

                // create the audit entry.
                HetSeniorityAudit seniorityAudit = new HetSeniorityAudit
                {
                    BlockNumber = original.BlockNumber,
                    EndDate     = currentTime
                };

                int tempLocalAreaId = original.LocalArea.LocalAreaId;
                int tempOwnerId     = original.Owner.OwnerId;

                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);
            }
        }