public ActionResult GetCopmanyIdAndName()
 {
     var a = _company.GetModuleIdAndName();
     List<CompanyIdAndName> list = new List<CompanyIdAndName>();
     foreach (var item in a)
     {
         CompanyIdAndName company = new CompanyIdAndName { Id = item.Key, Name = item.Value };
         list.Add(company);
     }
     return Json(list, JsonRequestBehavior.AllowGet);
 }
        public ActionResult CompaniesForUser()
        {
            ISecUserService _su;
            _su = new SecUserService(new SecUserRepository(_databaseFactory), new UnitOfWork(_databaseFactory));
            if (Session["userId"] != null)
            {
                int userId = Convert.ToInt32(Session["userId"]);
                SecUser user = _su.GetById(userId);
                ISecCompanyUserService _compUserService = new SecCompanyUserService(new SecCompanyUserRepository(_databaseFactory), new UnitOfWork(_databaseFactory));
                var compUserList = _compUserService.GetAll();

                if (compUserList != null && compUserList.Count() > 0)
                {
                    compUserList = compUserList.Where(i => i.SecUserId == userId).ToList();
                    if (compUserList != null && compUserList.Count() > 0)
                    {
                        var companyIds = compUserList.Select(i => i.SecCompanyId).ToList();

                        var permittedCompanyList = _company.GetAll();
                        IList<SecCompany> result = permittedCompanyList.Where(b => companyIds.Contains(b.Id)).ToList();

                        List<CompanyIdAndName> list = GetCompanyList(result);
                        //Default compnay
                        if (user.HrmEmployeeId != null)
                        {
                            ERPOptima.Model.HRM.HrmEmployee emp = new ERPOptima.Model.HRM.HrmEmployee();
                            ERPOptima.Service.Hrm.HrmEmployeeService es = new ERPOptima.Service.Hrm.HrmEmployeeService(new ERPOptima.Data.Hrm.Repository.HrmEmployeeRepository(_databaseFactory), new UnitOfWork(_databaseFactory));
                            emp = es.GetById(user.HrmEmployeeId.Value);
                            if (emp != null)
                            {

                                _company = new SecCompanyService(new SecCompanyRepository(_databaseFactory), new UnitOfWork(_databaseFactory));
                                var defaultCompany = _company.GetById(emp.SecCompanyId.Value);
                                if (defaultCompany != null)
                                {
                                    CompanyIdAndName obj = new CompanyIdAndName();
                                    obj.Id = defaultCompany.Id;
                                    obj.Name = defaultCompany.Name;
                                    if (!list.Contains(obj))
                                    {
                                        list.Insert(0, obj);
                                    }
                                }
                            }
                        }
                        return Json(list, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        List<CompanyIdAndName> list = new List<CompanyIdAndName>();
                        //default company
                        if (user.HrmEmployeeId != null)
                        {
                            ERPOptima.Model.HRM.HrmEmployee emp = new ERPOptima.Model.HRM.HrmEmployee();
                            ERPOptima.Service.Hrm.HrmEmployeeService es = new ERPOptima.Service.Hrm.HrmEmployeeService(new ERPOptima.Data.Hrm.Repository.HrmEmployeeRepository(_databaseFactory), new UnitOfWork(_databaseFactory));
                            emp = es.GetById(user.HrmEmployeeId.Value);
                            if (emp != null)
                            {

                                _company = new SecCompanyService(new SecCompanyRepository(_databaseFactory), new UnitOfWork(_databaseFactory));
                                var defaultCompany = _company.GetById(emp.SecCompanyId.Value);
                                if (defaultCompany != null)
                                {
                                    CompanyIdAndName obj = new CompanyIdAndName();
                                    obj.Id = defaultCompany.Id;
                                    obj.Name = defaultCompany.Name;
                                    if (!list.Contains(obj))
                                    {
                                        list.Insert(0, obj);
                                    }
                                }
                            }
                        }

                        return Json(list, JsonRequestBehavior.AllowGet);
                    }
                }

                //var a = _company.GetModuleIdAndName();
                //List<CompanyIdAndName> list = new List<CompanyIdAndName>();
                //foreach (var item in a)
                //{
                //    CompanyIdAndName company = new CompanyIdAndName { Id = item.Key, Name = item.Value };
                //    list.Add(company);
                //}
                //return Json(list, JsonRequestBehavior.AllowGet);

                else
                {
                    //default company
                    //IList<SecCompany> defaultCompanyList = new List<SecCompany>();
                    //var defaultCompany = _company.GetById(1);
                    //defaultCompanyList.Add(defaultCompany);
                    //List<CompanyIdAndName> list = GetCompanyList(defaultCompanyList);
                    List<CompanyIdAndName> list = new List<CompanyIdAndName>();
                    //default company
                    if (user.HrmEmployeeId != null)
                    {
                        ERPOptima.Model.HRM.HrmEmployee emp = new ERPOptima.Model.HRM.HrmEmployee();
                        ERPOptima.Service.Hrm.HrmEmployeeService es = new ERPOptima.Service.Hrm.HrmEmployeeService(new ERPOptima.Data.Hrm.Repository.HrmEmployeeRepository(_databaseFactory), new UnitOfWork(_databaseFactory));
                        emp = es.GetById(user.HrmEmployeeId.Value);
                        if (emp != null)
                        {

                            _company = new SecCompanyService(new SecCompanyRepository(_databaseFactory), new UnitOfWork(_databaseFactory));
                            var defaultCompany = _company.GetById(emp.SecCompanyId.Value);
                            if (defaultCompany != null)
                            {
                                CompanyIdAndName obj = new CompanyIdAndName();
                                obj.Id = defaultCompany.Id;
                                obj.Name = defaultCompany.Name;
                                if (!list.Contains(obj))
                                {
                                    list.Insert(0, obj);
                                }
                            }
                        }
                    }
                    return Json(list, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                Operation ret = new Operation();
                ret.Success = false;
                ret.Message = "Unauthenticated user ";
                ret.OperationId = -1;
                return Json(ret, JsonRequestBehavior.AllowGet);
            }
        }
 public List<CompanyIdAndName> GetCompanyList(IList<SecCompany> result)
 {
     List<CompanyIdAndName> list = new List<CompanyIdAndName>();
     foreach (var item in result)
     {
         CompanyIdAndName company = new CompanyIdAndName { Id = item.Id, Name = item.Name };
         list.Add(company);
     }
     return list;
 }