public virtual Result UpdateWorkingWithDigitalDecisionInECM()
        {
            try
            {
                Result result = new Result();
                OrganizationsStructures OrganizationsStructures = new OrganizationsStructures()
                {
                    OrganizationID = this.OrganizationID,
                    WorkingWithDigitalDecisionInECM = this.WorkingWithDigitalDecisionInECM,
                    LastUpdatedDate = DateTime.Now,
                    LastUpdatedBy   = this.LoginIdentity.EmployeeCodeID
                };

                new OrganizationsStructuresDAL().UpdateWorkingWithDigitalDecisionInECM(OrganizationsStructures);
                result.Entity     = this;
                result.EnumType   = typeof(OrganizationStructureValidationEnum);
                result.EnumMember = OrganizationStructureValidationEnum.Done.ToString();

                return(result);
            }
            catch
            {
                throw;
            }
        }
 internal OrganizationsStructuresBLL MapOrganizationWithoutManager(OrganizationsStructures OrganizationStructure)
 {
     try
     {
         OrganizationsStructuresBLL OrganizationStructuresBLL = null;
         if (OrganizationStructure != null)
         {
             OrganizationStructuresBLL = new OrganizationsStructuresBLL()
             {
                 CreatedDate        = OrganizationStructure.CreatedDate,
                 LastUpdatedDate    = OrganizationStructure.LastUpdatedDate,
                 OrganizationID     = OrganizationStructure.OrganizationID,
                 OrganizationCode   = OrganizationStructure.OrganizationCode,
                 OrganizationName   = OrganizationStructure.OrganizationName, //+ " - " + new BranchesBLL().MapBranch(OrganizationStructure.Branches).BranchName,
                 ShowInECM          = OrganizationStructure.ShowInECM.HasValue ? OrganizationStructure.ShowInECM.Value : false,
                 ParentOrganization = OrganizationStructure.ParentOrganization != null ? new OrganizationsStructuresBLL()
                 {
                     OrganizationID   = OrganizationStructure.ParentOrganization.OrganizationID,
                     OrganizationCode = OrganizationStructure.ParentOrganization.OrganizationCode,
                     OrganizationName = OrganizationStructure.ParentOrganization.OrganizationName,
                     Branch           = new BranchesBLL()
                     {
                         BranchName = OrganizationStructure.ParentOrganization.Branches.BranchName
                     }
                 } : null,
                 Branch = new BranchesBLL().MapBranch(OrganizationStructure.Branches),
             };
         }
         return(OrganizationStructuresBLL);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        internal string GetOrganizationNameTillLastParentExceptPresident(List <OrganizationsStructures> AllOrgs, OrganizationsStructures org)
        {
            OrganizationsStructures OrganizationStructure = org;

            if (OrganizationStructure != null && !OrganizationStructure.OrganizationParentID.HasValue)
            {
                return(OrganizationStructure.OrganizationName);
            }
            else if (OrganizationStructure != null && OrganizationStructure.OrganizationParentID.HasValue && OrganizationStructure.OrganizationParentID == 1)
            {
                return(OrganizationStructure.OrganizationName);
            }
            else
            {
                OrganizationsStructures k = AllOrgs.FirstOrDefault(x => x.OrganizationID == org.OrganizationParentID);
                return(GetOrganizationNameTillLastParentExceptPresident(AllOrgs, k) + " / " + OrganizationStructure.OrganizationName);
            }
        }
        public virtual List <int> GetAllChildernByOrganizationID(int OrganizationID)
        {
            List <int> OrganizationIDsList = new List <int>();
            //List<OrganizationsStructuresBLL> OrganizationsStructuresBLLList = this.GetByOrganizationIDWithhAllChilds(OrganizationID).ToList();

            List <OrganizationsStructures> AllOrgs = new OrganizationsStructuresDAL().GetOrganizationStructure();
            List <OrganizationsStructures> orgs    = new List <OrganizationsStructures>();
            OrganizationsStructures        MainOrg = AllOrgs.FirstOrDefault(x => x.OrganizationID == OrganizationID);

            orgs.Add(MainOrg);
            AddChildrenID(AllOrgs, orgs, MainOrg.OrganizationID);

            foreach (var item in orgs)
            {
                OrganizationIDsList.Add(item.OrganizationID);
            }

            return(OrganizationIDsList);
        }
        public virtual Result Update()
        {
            try
            {
                Result result = new Result();
                OrganizationsStructures OrganizationsStructures = new OrganizationsStructures()
                {
                    OrganizationID       = this.OrganizationID,
                    OrganizationName     = this.OrganizationName,
                    OrganizationCode     = this.OrganizationCode,
                    BranchID             = this.Branch.BranchID,
                    OrganizationParentID = this.ParentOrganization != null ? (int)this.ParentOrganization.OrganizationID : (int?)null,
                    ManagerCodeID        = this.OrganizationManager != null && this.OrganizationManager.EmployeeCodeID != 0 ? (int)this.OrganizationManager.EmployeeCodeID : (int?)null,
                    LastUpdatedDate      = DateTime.Now,
                    LastUpdatedBy        = this.LoginIdentity.EmployeeCodeID
                };

                new OrganizationsStructuresDAL().Update(OrganizationsStructures);

                // if the manager of organization has been changed, the manager of the same organization in Assigning module must be changed (for all unfinished assignings)
                if (this.OrganizationManager != null)
                {
                    new AssigningsDAL().UpdateOrganizationManager(new Assignings()
                    {
                        OrganizationID  = this.OrganizationID,
                        ManagerCodeID   = this.OrganizationManager.EmployeeCodeID,
                        LastUpdatedDate = DateTime.Now,
                        LastUpdatedBy   = this.LoginIdentity.EmployeeCodeID
                    });
                }

                result.Entity     = this;
                result.EnumType   = typeof(OrganizationStructureValidationEnum);
                result.EnumMember = OrganizationStructureValidationEnum.Done.ToString();
                return(result);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// Task 256: Adding records in e service authorization after adding new organization node in organizations structure
        /// Set Manager as Parent_Manager_Code_ID if manager is null
        /// </summary>
        /// <returns></returns>
        public virtual Result Add()
        {
            try
            {
                Result result = new Result();

                // TASK 256 (related) if manager is null, we set "parent manage" is also manager of newly created organization
                if (this.OrganizationManager == null)
                {
                    OrganizationsStructures ParentOrg = new OrganizationsStructuresDAL().GetByOrganizationID(this.ParentOrganization.OrganizationID);
                    if (ParentOrg != null && ParentOrg.ManagerCodeID.HasValue && ParentOrg.ManagerCodeID.Value > 0)
                    {
                        this.OrganizationManager = new EmployeesCodesBLL()
                        {
                            EmployeeCodeID = ParentOrg.ManagerCodeID.Value
                        };
                    }
                }

                OrganizationsStructures OrganizationsStructures = new OrganizationsStructures()
                {
                    BranchID             = this.Branch.BranchID,
                    OrganizationParentID = this.ParentOrganization.OrganizationID,
                    OrganizationName     = this.OrganizationName,
                    OrganizationCode     = this.OrganizationCode,
                    ManagerCodeID        = this.OrganizationManager != null && this.OrganizationManager.EmployeeCodeID != 0 ? (int)this.OrganizationManager.EmployeeCodeID : (int?)null,
                    CreatedDate          = DateTime.Now,
                    CreatedBy            = this.LoginIdentity.EmployeeCodeID
                };
                this.OrganizationID = new OrganizationsStructuresDAL().Insert(OrganizationsStructures);

                #region EService Authorization: Create EServiceAuthorization records of the Organization based on EServiceTypes
                if (this.OrganizationManager != null && this.OrganizationManager.EmployeeCodeID > 0)
                {
                    EServicesAuthorizationsBLL EServiceAuthorizationBLL;
                    foreach (var item in new EServicesTypesBLL().GetEServicesTypes())
                    {
                        EServiceAuthorizationBLL = new EServicesAuthorizationsBLL();
                        EServiceAuthorizationBLL.Organization = new OrganizationsStructuresBLL()
                        {
                            OrganizationID = this.OrganizationID
                        };
                        EServiceAuthorizationBLL.AuthorizedPerson = new EmployeesCodesBLL()
                        {
                            EmployeeCodeID = this.OrganizationManager.EmployeeCodeID
                        };
                        EServiceAuthorizationBLL.EServiceType  = item;
                        EServiceAuthorizationBLL.CreatedDate   = DateTime.Now;
                        EServiceAuthorizationBLL.LoginIdentity = this.LoginIdentity;
                        EServiceAuthorizationBLL.Add();
                    }
                }
                #endregion

                result.Entity     = this;
                result.EnumType   = typeof(OrganizationStructureValidationEnum);
                result.EnumMember = OrganizationStructureValidationEnum.Done.ToString();
                return(result);
            }
            catch
            {
                throw;
            }
        }
        internal OrganizationsStructuresBLL MapOrganization(OrganizationsStructures OrganizationStructure)
        {
            try
            {
                OrganizationsStructuresBLL OrganizationStructuresBLL = null;
                if (OrganizationStructure != null)
                {
                    OrganizationStructuresBLL = new OrganizationsStructuresBLL()
                    {
                        CreatedDate      = OrganizationStructure.CreatedDate,
                        LastUpdatedDate  = OrganizationStructure.LastUpdatedDate,
                        OrganizationID   = OrganizationStructure.OrganizationID,
                        OrganizationCode = OrganizationStructure.OrganizationCode,
                        OrganizationName = OrganizationStructure.OrganizationName, //+ " - " + new BranchesBLL().MapBranch(OrganizationStructure.Branches).BranchName,
                        ShowInECM        = OrganizationStructure.ShowInECM.HasValue ? OrganizationStructure.ShowInECM.Value : false,
                        WorkingWithDigitalDecisionInECM = OrganizationStructure.WorkingWithDigitalDecisionInECM.HasValue ? OrganizationStructure.WorkingWithDigitalDecisionInECM.Value : false,
                        ParentOrganization = OrganizationStructure.ParentOrganization != null ? new OrganizationsStructuresBLL()
                        {
                            OrganizationID   = OrganizationStructure.ParentOrganization.OrganizationID,
                            OrganizationCode = OrganizationStructure.ParentOrganization.OrganizationCode,
                            OrganizationName = OrganizationStructure.ParentOrganization.OrganizationName,
                            Branch           = new BranchesBLL()
                            {
                                BranchName = OrganizationStructure.ParentOrganization.Branches.BranchName
                            },
                            OrganizationManager = new EmployeesCodesBLL()
                            {
                                EmployeeCodeID = OrganizationStructure.ParentOrganization.ManagerCodeID.HasValue ? OrganizationStructure.ParentOrganization.ManagerCodeID.Value : 0
                            }
                        } : null,
                        Branch = new BranchesBLL().MapBranch(OrganizationStructure.Branches),
                        OrganizationManager = OrganizationStructure.EmployeesCodes != null ? new EmployeesCodesBLL()
                        {
                            EmployeeCodeID = OrganizationStructure.EmployeesCodes.EmployeeCodeID,
                            EmployeeCodeNo = OrganizationStructure.EmployeesCodes.EmployeeCodeNo,
                            Employee       = new EmployeesBLL().MapEmployee(OrganizationStructure.EmployeesCodes.Employees),
                            HiringRecord   = new EmployeesCareersHistoryBLL()
                            {
                                JoinDate = new EmployeesCareersHistoryBLL().GetHiringDate(OrganizationStructure.EmployeesCodes.EmployeeCodeID)
                            },
                        } : null,
                    };

                    // comment out because there is json error on Organization structure page. Dated : 19-07-2020
                    //if (OrganizationStructure.EmployeesCodes != null)
                    //{
                    //    if (OrganizationStructure.EmployeesCodes.EmployeesCareersHistory != null)
                    //    {
                    //        if (OrganizationStructure.EmployeesCodes.EmployeesCareersHistory.Count > 0)
                    //        {
                    //            EmployeesCareersHistory CurrentEmplyeeJob = OrganizationStructure.EmployeesCodes.EmployeesCareersHistory.FirstOrDefault(x => x.IsActive == true); // to get current organization of his job
                    //            OrganizationStructuresBLL.OrganizationManager.EmployeeCurrentJob = new EmployeesCareersHistoryBLL()
                    //            {
                    //                OrganizationJob = new OrganizationsJobsBLL()
                    //                {
                    //                    OrganizationStructure = new OrganizationsStructuresBLL()
                    //                    {
                    //                        OrganizationName = CurrentEmplyeeJob != null ? CurrentEmplyeeJob.OrganizationsJobs.OrganizationsStructures.OrganizationName : string.Empty
                    //                        //OrganizationName =  OrganizationStructure.EmployeesCodes.EmployeesCareersHistory.FirstOrDefault(x => x.IsActive == true).OrganizationsJobs.OrganizationsStructures.OrganizationName
                    //                    }
                    //                }
                    //            };
                    //        }

                    //    }
                    //}
                }
                return(OrganizationStructuresBLL);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Task # 255: Changing the manager of any organization must affect on e services authorization module
        ///  - removed validation of Is Placement period finished or not?
        ///
        /// </summary>
        /// <param name="Organization"></param>
        /// <returns></returns>
        public Result ChangeOrganizationManager(OrganizationsStructuresBLL Organization)
        {
            try
            {
                Result result = null;

                // Now there is no need for this condition, placement is open for all times.

                //#region Validate if the Placement period finished or not
                //result = CommonHelper.IsValidToCompleteEmployeesPlacement();
                //if (result != null)
                //    return result;
                //#endregion

                // TASK 255 (related) Changing the manager of any organization must affect on e services authorization module
                OrganizationsStructures OldOrganization = new OrganizationsStructuresDAL().GetByOrganizationID(Organization.OrganizationID);

                // TASK 330 (related) Checking the manager organization is going to be changed or not .. if the manager is not going to be changed, no need to do any action
                if (OldOrganization.ManagerCodeID.HasValue)
                {
                    if (Organization.OrganizationManager.EmployeeCodeID == OldOrganization.ManagerCodeID.Value) // thats mean the manager is not going to be changed
                    {
                        return(result);
                    }
                }

                OrganizationsStructures OrganizationStructure = new OrganizationsStructures()
                {
                    OrganizationID  = Organization.OrganizationID,
                    ManagerCodeID   = Organization.OrganizationManager != null ? Organization.OrganizationManager.EmployeeCodeID : (int?)null,
                    LastUpdatedDate = DateTime.Now,
                    LastUpdatedBy   = Organization.LoginIdentity.EmployeeCodeID
                };

                Assignings Assigning = new Assignings()
                {
                    OrganizationID  = Organization.OrganizationID,
                    ManagerCodeID   = Organization.OrganizationManager != null ? Organization.OrganizationManager.EmployeeCodeID : (int?)null,
                    LastUpdatedDate = DateTime.Now,
                    LastUpdatedBy   = Organization.LoginIdentity.EmployeeCodeID
                };

                new OrganizationsStructuresDAL().UpdateManager(OrganizationStructure);
                new AssigningsDAL().UpdateOrganizationManager(Assigning); // if the manager of organization has been changed, the manager of the same organization in Assigning must be changed (for all unfinished assignings)

                // Old and New Organization must have ManagerCodeID
                // Update AuthorizedPerson in EServices Authorization table (related to task 255)
                if (OldOrganization.ManagerCodeID.HasValue && OldOrganization.ManagerCodeID.Value > 0 &&
                    Organization.OrganizationManager != null && Organization.OrganizationManager.EmployeeCodeID > 0)
                {
                    new EServicesAuthorizationsBLL()
                    {
                        LoginIdentity = Organization.LoginIdentity
                    }.ChangeAuthorizedPersonForAllChildByOrganizationID(Organization.OrganizationID, OldOrganization.ManagerCodeID.Value, Organization.OrganizationManager.EmployeeCodeID);
                }

                #region Task 318: Service to Cancel EServicesProxies By System
                try
                {
                    result = new EServicesProxiesBLL()
                    {
                        LoginIdentity = Organization.LoginIdentity
                    }
                    .RevokeEServiceProxyByEmployeeCodeID(Organization.OrganizationManager.EmployeeCodeID, EServicesProxiesStatusEnum.CancelledBySystem, Globalization.EServiceProxyCancelledBySystemBecauseOfChangeManagerText);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                #endregion


                // this to get mobile no of manager
                EmployeesCodesBLL ManagerCodeBLL = new EmployeesCodesBLL().GetByEmployeeCodeID(Organization.OrganizationManager != null ? Organization.OrganizationManager.EmployeeCodeID : 0);
                if (ManagerCodeBLL != null)
                {
                    SMSLogsBLL SMSLogBLL = new SMSLogsBLL()
                    {
                        BusinssSubCategory = BusinessSubCategoriesEnum.OrganizationStructure,
                        MobileNo           = ManagerCodeBLL.Employee.EmployeeMobileNo,
                        DetailID           = 0,
                        Message            = string.Format(Globalization.SMSManagerAlreadyAssignedToOrganizationMessageText, ManagerCodeBLL.Employee.FirstNameAr + " " + ManagerCodeBLL.Employee.LastNameAr, Organization.GetOrganizationNameTillLastParentExceptPresident(Organization.OrganizationID)),
                        CreatedBy          = Organization.LoginIdentity,
                        CreatedDate        = DateTime.Now,
                    };
                    new SMSBLL().SendSMS(SMSLogBLL);
                }

                result            = new Result();
                result.Entity     = this;
                result.EnumType   = typeof(OrganizationStructureValidationEnum);
                result.EnumMember = OrganizationStructureValidationEnum.Done.ToString();
                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }