Exemplo n.º 1
0
 public RoutineAuditForm(SmartCore.CAA.RoutineAudits.RoutineAudit audit) : this()
 {
     _audit = audit;
     UpdateInformation();
 }
Exemplo n.º 2
0
        protected override void AnimatedThreadWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            _initialDocumentArray.Clear();
            _resultDocumentArray.Clear();

            AnimatedThreadWorker.ReportProgress(0, "load directives");

            _audit = GlobalObjects.CaaEnvironment.NewLoader.GetObjectById <CAAAuditDTO, CAAAudit>(_parentId);
            var records = GlobalObjects.CaaEnvironment.NewLoader
                          .GetObjectListAll <CAAAuditRecordDTO, CAAAuditRecord>(new Filter("AuditId", _parentId), loadChild: true).ToList();

            _currentRoutineId = records.Select(i => i.RoutineAuditId).FirstOrDefault();
            _routineAudit     = GlobalObjects.CaaEnvironment.NewLoader.GetObjectById <RoutineAuditDTO, SmartCore.CAA.RoutineAudits.RoutineAudit>(_currentRoutineId.Value);
            var manuals = GlobalObjects.CaaEnvironment.NewLoader.GetObjectList <StandartManualDTO, SmartCore.CAA.StandartManual.StandartManual>(new []
            {
                new Filter("OperatorId", new [] { _operatorId, -1 }.Distinct()),
                new Filter("ProgramTypeId", _routineAudit.Settings.TypeId),
            });

            _manual = manuals.FirstOrDefault();

            var routines = GlobalObjects.CaaEnvironment.NewLoader
                           .GetObjectListAll <RoutineAuditRecordDTO, RoutineAuditRecord>(new Filter("RoutineAuditId", _currentRoutineId), loadChild: true).ToList();

            var ids = new List <int>();

            if (_type == CheckListAuditType.Admin)
            {
                if (_audit.Settings.Status == RoutineStatus.Published)
                {
                    var ds = GlobalObjects.CaaEnvironment.NewLoader.Execute($@"select CheckListId from dbo.AuditChecks 
where AuditId in ({_parentId}) and IsDeleted = 0 and WorkflowStageId = {_stage.Value}");

                    var dt = ds.Tables[0];
                    ids.AddRange(from DataRow dr in dt.Rows select(int) dr[0]);
                }
                else
                {
                    ids = routines.Select(i => i.CheckListId).Distinct().ToList();
                }
            }
            else
            {
                var condition = _stage.HasValue ? $" and ac.WorkflowStageId = {_stage}" : "";

                var ds = GlobalObjects.CaaEnvironment.NewLoader.Execute($@"SELECT rec.CheckListId
   FROM [AuditPelRecords] rec
   cross apply
   (
		select SpecialistId as Auditor from  [dbo].[PelSpecialist] where ItemId = rec.AuditorId
   ) as auditor
   cross apply
   (
		select SpecialistId as Auditee from  [dbo].[PelSpecialist] where ItemId = rec.AuditeeId
   ) as auditee
   cross apply
   (
		select JSON_VALUE(SettingsJSON, '$.WorkflowStageId') as WorkflowStageId   from  [dbo].AuditChecks 
		where AuditId = rec.AuditId and CheckListId = rec.CheckListId
   ) as ac
   where rec.AuditId in ({_parentId}) and rec.IsDeleted = 0  {condition} 
   and (auditor.Auditor = {GlobalObjects.CaaEnvironment.IdentityUser.PersonnelId} or auditee.Auditee = {GlobalObjects.CaaEnvironment.IdentityUser.PersonnelId})");

                var dt = ds.Tables[0];

                ids.AddRange(from DataRow dr in dt.Rows select(int) dr[0]);
            }


            if (ids.Any())
            {
                var filter = new List <Filter>()
                {
                    new Filter("CheckListId", ids),
                    new Filter("AuditId", _audit.ItemId)
                };

                if (_stage.HasValue)
                {
                    filter.Add(new Filter("WorkflowStageId", _stage.Value));
                }

                var auditChecks = GlobalObjects.CaaEnvironment.NewLoader
                                  .GetObjectListAll <AuditCheckDTO, AuditCheck>(filter, loadChild: true).ToList();

                _initialDocumentArray.AddRange(GlobalObjects.CaaEnvironment.NewLoader.GetObjectListAll <CheckListDTO, CheckLists>(new Filter("ItemId", ids), loadChild: true, true));

                var revisions = new List <CheckListRevision>();
                var revedIds  = _initialDocumentArray.Where(i => i.RevisionId.HasValue).Select(i => i.RevisionId.Value).Distinct().ToList();
                revedIds.AddRange(_initialDocumentArray.Select(i => i.EditionId).Distinct());
                if (revedIds.Any())
                {
                    revisions.AddRange(GlobalObjects.CaaEnvironment.NewLoader.GetObjectList <CheckListRevisionDTO, CheckListRevision>(new List <Filter>()
                    {
                        new Filter("ItemId", values: revedIds),
                    }));
                }

                foreach (var check in _initialDocumentArray)
                {
                    check.AuditCheck     = auditChecks.FirstOrDefault(i => i.CheckListId == check.ItemId);
                    check.RevisionNumber = revisions.FirstOrDefault(i => i.ItemId == check.RevisionId)?.Number.ToString() ?? "";
                    check.EditionNumber  = revisions.FirstOrDefault(i => i.ItemId == check.RevisionId)?.Number.ToString() ?? "";
                }
            }


            var pelRecords = GlobalObjects.CaaEnvironment.NewLoader
                             .GetObjectListAll <AuditPelRecordDTO, AuditPelRecord>(new Filter("AuditId", _parentId));

            if (records.Any())
            {
                var pelSpec = GlobalObjects.CaaEnvironment.NewLoader.GetObjectList <PelSpecialistDTO, PelSpecialist>(new Filter("AuditId", _parentId));

                var specIds     = pelSpec.Select(i => i.SpecialistId);
                var specialists = GlobalObjects.CaaEnvironment.NewLoader
                                  .GetObjectListAll <CAASpecialistDTO, Specialist>(new Filter("ItemId", specIds),
                                                                                   loadChild: true);

                foreach (var specialist in specialists)
                {
                    specialist.Operator = GlobalObjects.CaaEnvironment.AllOperators.FirstOrDefault(i => i.ItemId == specialist.OperatorId) ?? AllOperators.Unknown;
                }

                foreach (var pel in pelSpec)
                {
                    pel.Specialist = specialists.FirstOrDefault(i => i.ItemId == pel.SpecialistId);
                }


                var dsTransfer = GlobalObjects.CaaEnvironment.NewLoader.Execute($@";WITH cte AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY CheckListId ORDER BY Created DESC) AS rn
   FROM [CheckListTransfer] where AuditId = {_parentId}
)
SELECT CheckListId, [From], [To]
FROM cte
WHERE rn = 1 and  IsDeleted = 0");

                var transfers = dsTransfer.Tables[0].Rows.OfType <DataRow>().Select(i => new
                {
                    CheckListId = (int)i[0],
                    From        = (int)i[1],
                    To          = (int)i[2],
                });

                foreach (var rec in pelRecords)
                {
                    rec.CheckList = _initialDocumentArray.FirstOrDefault(i => i.ItemId == rec.CheckListId);
                    rec.Auditor   = pelSpec.FirstOrDefault(i => i.ItemId == rec.AuditorId)?.Specialist ?? Specialist.Unknown;
                    rec.Auditee   = pelSpec.FirstOrDefault(i => i.ItemId == rec.AuditeeId)?.Specialist ?? Specialist.Unknown;

                    var tr = transfers.FirstOrDefault(i => i.CheckListId == rec.CheckListId);
                    if (tr != null)
                    {
                        rec.CurrentAuditor = pelSpec.FirstOrDefault(i => i.SpecialistId == tr.To)?.Specialist ?? Specialist.Unknown;
                    }
                }
            }


            var levels = GlobalObjects.CaaEnvironment.NewLoader.GetObjectList <FindingLevelsDTO, FindingLevels>(new []
            {
                new Filter("OperatorId", _operatorId),
                new Filter("ProgramTypeId", _manual.ProgramTypeId),
            });

            foreach (var check in _initialDocumentArray)
            {
                check.PelRecord = pelRecords.FirstOrDefault(i => i.CheckListId == check.ItemId);

                if (check.PelRecord?.CurrentAuditor?.ItemId == GlobalObjects.CaaEnvironment.IdentityUser.PersonnelId)
                {
                    check.IsEditable = true;
                }


                if (check.CheckUIType == CheckUIType.Iosa)
                {
                    check.Level = levels.FirstOrDefault(i => i.ItemId == check.Settings.LevelId) ??
                                  FindingLevels.Unknown;
                }
                else if (check.CheckUIType == CheckUIType.Safa)
                {
                    check.Level = levels.FirstOrDefault(i => i.ItemId == check.SettingsSafa.LevelId) ??
                                  FindingLevels.Unknown;
                }
                else if (check.CheckUIType == CheckUIType.Icao)
                {
                    check.Level = levels.FirstOrDefault(i => i.ItemId == check.SettingsIcao.LevelId) ??
                                  FindingLevels.Unknown;
                }
                check.Remains   = Lifelength.Null;
                check.Condition = ConditionState.Satisfactory;
            }


            AnimatedThreadWorker.ReportProgress(40, "filter directives");

            AnimatedThreadWorker.ReportProgress(70, "filter directives");

            FilterItems(_initialDocumentArray, _resultDocumentArray);

            AnimatedThreadWorker.ReportProgress(100, "Complete");
        }