Exemplo n.º 1
0
        public virtual ActionResult Index(SPSearchByFilterDTO filters, string roleSelected = null)
        {
            if (Session["Display"] == null)
            {
                Session["Display"] = string.Empty;
            }

            ViewBag.Loader         = true;
            ViewBag.LoaderOptional = true;
            ViewBag.RoleSelected   = roleSelected;
            var model        = new List <SPAdminitrationDTO>();
            var responsePrev = _spAdministrationService.GetSPPreviousMarchPMRCLass();
            var responseCurr = _spAdministrationService.GetSPCurrentMarchPMRCLass();
            var responseExc  = _spAdministrationService.GetSPExcdFinExtCri();
            var responseMod  = _spAdministrationService.GetSPModality();
            var responseDis  = _spAdministrationService.GetSPDisplayed();
            var userName     = IDBContext.Current.UserName;

            ViewBag.ListRoles = _viewModelMapperHelper.GetUserRoleList();

            var firstLevelPermission  = IDBContext.Current.HasPermission(SpGlobalValues.FIRST_LEVEL);
            var secondLevelPermission = IDBContext.Current.HasPermission(SpGlobalValues.SECOND_LEVEL);
            var isRoleSelected        = roleSelected != null;

            if (isRoleSelected)
            {
                firstLevelPermission  = roleSelected == SpGlobalValues.ChiefofOperations;
                secondLevelPermission = roleSelected == SpGlobalValues.CountryRepresentative;
            }

            var roleResponse = _spAdministrationService.GetRole(firstLevelPermission, secondLevelPermission);

            if (roleResponse.IsValid)
            {
                if (Request.RequestType == "Get")
                {
                    Session.Remove(userName + roleSelected);
                    roleSelected = null;
                }

                if (roleSelected == null)
                {
                    if (roleResponse.Quantity == 2)
                    {
                        ViewBag.ShowModalSelectRole = true;
                        return(View());
                    }

                    roleSelected = roleResponse.Role;
                    Session[userName + roleSelected] = roleSelected;
                    ViewBag.RoleSelected             = roleResponse.Role;
                }
                else
                {
                    ViewBag.ShowModalSelectRole      = false;
                    Session[userName + roleSelected] = roleSelected;
                }
            }
            else
            {
                return(RedirectToAction("NotAccess"));
            }

            var aManager      = Globals.Resolve <IAuthorizationManager>();
            var securityGroup = aManager.GetAdGroups(userName);

            securityGroup =
                _spAdministrationService.LoadSecurityGroupsAccordingToPermissions(
                    securityGroup.ToList(), firstLevelPermission, secondLevelPermission);
            ViewBag.spModality = _viewModelMapperHelper.GetListMasterData(SpGlobalValues.ModalityType);
            var roleId       = _spAdministrationService.GetRoleIdByRole(Session[userName + roleSelected].ToString());
            var responseUnit = _spAdministrationService.GetSPUnit(string.Join(",", securityGroup.ToArray()), Session[userName + roleSelected].ToString());

            var responseSearch = _spAdministrationService.Filters(new SearchByFiltersRequest
            {
                OperationNumber      = filters.OperationNum,
                OperationName        = filters.OperationName,
                ApprovalYear         = filters.ApprovalYear,
                Unit                 = filters.Unit,
                PrevYearMarhPmrClass = filters.PrevYearMarhPmrClass,
                CurrYearMarPmrClass  = filters.CurrYearMarPmrClass,
                ExFinanciatExtCri    = filters.ExFinanciatExtCri,
                SpModality           = filters.SpModality,
                SpModalityAfter      = filters.SpModalityAfter,
                LastEditedBy         = filters.LastEditedBy,
                IsRevRequired        = filters.IsRevRequired == null ? false : filters.IsRevRequired,
                SecurityGroup        = string.Join(",", securityGroup.ToArray()),
                RoleUser             = _spAdministrationService.GetRoleIdByRole(Session[userName + roleSelected].ToString())
            });

            if (!responseUnit.IsValid)
            {
                ViewBag.ShowError = responseUnit.ErrorMessage;
                throw new Exception(responseUnit.ErrorMessage);
            }

            if (!responsePrev.IsValid)
            {
                ViewBag.ShowError = responsePrev.ErrorMessage;
                throw new Exception(responsePrev.ErrorMessage);
            }

            if (!responseCurr.IsValid)
            {
                ViewBag.ShowError = responseCurr.ErrorMessage;
                throw new Exception(responseCurr.ErrorMessage);
            }

            if (!responseExc.IsValid)
            {
                ViewBag.ShowError = responseExc.ErrorMessage;
                throw new Exception(responseExc.ErrorMessage);
            }

            if (!responseMod.IsValid)
            {
                ViewBag.ShowError = responseMod.ErrorMessage;
                throw new Exception(responseMod.ErrorMessage);
            }

            if (!responseDis.IsValid)
            {
                ViewBag.ShowError = responseDis.ErrorMessage;
                throw new Exception(responseDis.ErrorMessage);
            }

            var result = new SPAdministrationSearchViewModel
            {
                UnitSearch         = responseUnit.Units,
                PrevMarchPMRClass  = responsePrev.PrevMarchPMRClass,
                CurrMarchPMRClass  = responseCurr.CurrMarchPMRClass,
                ExceedFinExtCri    = responseExc.ExceedFinExtCri,
                SPModality         = responseMod.SPModality,
                Displayed          = responseDis.Displayed,
                ResultsSearch      = responseSearch.Results,
                SearchByFilters    = filters,
                ParentRelationship = _spAdministrationService.ResolveRelatedOperationsWithStatus(
                    responseSearch.Results.Select(r => r.OperationNumber).ToList()).Models
            };

            ViewBag.SerializedAdministration = PageSerializationHelper.SerializeObject(model);

            ViewBag.CountryName = _spAdministrationService.GetCountryNames(securityGroup.ToList());

            var canEdit = _spAdministrationService.GetPermissionEdit(Session[userName + roleSelected].ToString());

            if (!canEdit.IsValid)
            {
                ViewBag.ShowError = canEdit.ErrorMessage;
            }

            var lstOperationSpStatus = _viewModelMapperHelper.GetItemsCodeMasterData(SpGlobalValues.OperationSpStatusType);

            canEdit = _spAdministrationService.RevalidateEditPermissions(
                result.ResultsSearch,
                canEdit,
                Session[userName + roleSelected].ToString(),
                lstOperationSpStatus, string.Join(",",
                                                  securityGroup.ToArray()),
                _spAdministrationService.GetRoleIdByRole(Session[userName + roleSelected].ToString()));

            bool canSubmit = true;

            if (canEdit.CanEdit && Session[userName + roleSelected].ToString().Equals(SpGlobalValues.CountryRepresentative))
            {
                canSubmit = _spAdministrationService.GetSubmitPermission(canEdit.CurrentPeriod, securityGroup.ToList());
            }

            ViewBag.CooRole              = _spAdministrationService.GetRoleIdByRole(SpGlobalValues.ChiefofOperations);
            ViewBag.CrRole               = _spAdministrationService.GetRoleIdByRole(SpGlobalValues.CountryRepresentative);
            ViewBag.UserRole             = roleId;
            ViewBag.ActualCycle          = canEdit.CurrentPeriod;
            ViewBag.isPermissionForEdit  = canEdit.CanEdit;
            ViewBag.currentYear          = canEdit.CurrentYear;
            ViewBag.DetailedId           = _spAdministrationService.GetIdDetailed(SpGlobalValues.DetailedId);
            ViewBag.lstOperationSpStatus = lstOperationSpStatus;
            ViewBag.SortIndex            = filters.OrderIndex;
            ViewBag.SortCount            = filters.OrderCount;
            ViewBag.CanSubmit            = canSubmit;
            ViewBag.UserName             = userName;
            var textoModal = string.Empty;

            switch (canEdit.CurrentPeriod)
            {
            case 1:
                textoModal = Session[userName + roleSelected].ToString() == SpGlobalValues.ChiefofOperations ? Localization.GetText("SP.Administration.Warning.Coo1") : Localization.GetText("SP.Administration.Warning.Cr1");
                break;

            case 2:
                textoModal = Session[userName + roleSelected].ToString() == SpGlobalValues.ChiefofOperations ? Localization.GetText("SP.Administration.Warning.Coo2") : Localization.GetText("SP.Administration.Warning.Cr2");
                break;
            }

            ViewBag.LiteralModal = textoModal;

            return(View(result));
        }