public async Task <IActionResult> EditEmployee(MstEmployeeAC mstEmployeeAc)
        {
            string userId   = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "user_id").Value;
            string fullname = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "fullname").Value;

            return(Ok(await _iEmployeeRepository.EditEmployee(mstEmployeeAc, Convert.ToInt64(userId), fullname)));
        }
        public async Task <IActionResult> CheckEmployee(MstEmployeeAC mstEmployeeAc)
        {
            bool       isValid   = true;
            ResponseAC responeAC = new ResponseAC();

            if (mstEmployeeAc.EmailId != null && mstEmployeeAc.EmailId.Length > 0)
            {
                isValid = await _iEmployeeRepository.checkEmailUnique(mstEmployeeAc.EmailId, mstEmployeeAc.UserId);

                if (!isValid)
                {
                    responeAC.Message    = "Email is already exists!";
                    responeAC.StatusCode = Convert.ToInt16(TeleBillingUtility.Helpers.Enums.EnumList.ResponseType.Error);
                    return(Ok(responeAC));
                }
            }

            if (mstEmployeeAc.EmpPFNumber != null && mstEmployeeAc.EmpPFNumber.Length > 0)
            {
                isValid = await _iEmployeeRepository.checkPFNumberUnique(mstEmployeeAc.EmpPFNumber, mstEmployeeAc.UserId);

                if (isValid)
                {
                    responeAC.Message    = "PFNumber is already exists!";
                    responeAC.StatusCode = Convert.ToInt16(TeleBillingUtility.Helpers.Enums.EnumList.ResponseType.Error);
                    return(Ok(responeAC));
                }
            }
            else
            {
                responeAC.Message    = "Employee details not found.";
                responeAC.StatusCode = Convert.ToInt16(TeleBillingUtility.Helpers.Enums.EnumList.ResponseType.Error);
                return(Ok(responeAC));
            }


            responeAC.Message    = "Employee is Valid";
            responeAC.StatusCode = Convert.ToInt16(TeleBillingUtility.Helpers.Enums.EnumList.ResponseType.Success);
            return(Ok(responeAC));
        }
        public async Task <ResponseAC> EditEmployee(MstEmployeeAC employee, long userId, string loginUserName)
        {
            ResponseAC responeAC = new ResponseAC();

            try
            {
                if (employee != null)
                {
                    if (employee.UserId > 0)
                    {
                        MstEmployee mstEmployee = new MstEmployee();
                        mstEmployee = await _dbTeleBilling_V01Context.MstEmployee.FindAsync(employee.UserId);

                        if (mstEmployee != null)
                        {
                            if (mstEmployee.IsSystemUser)
                            {
                                string pfNumber = employee.EmpPFNumber;
                                if (!await checkPFNumberUnique(pfNumber, employee.UserId))
                                {
                                    mstEmployee.FullName        = employee.FullName;
                                    mstEmployee.ExtensionNumber = employee.ExtensionNumber;
                                    mstEmployee.EmpPfnumber     = employee.EmpPFNumber;
                                    mstEmployee.EmailId         = employee.EmailId;

                                    mstEmployee.DepartmentId      = employee.DepartmentId;
                                    mstEmployee.Designation       = employee.Designation;
                                    mstEmployee.Description       = employee.Description;
                                    mstEmployee.BusinessUnitId    = employee.BusinessUnitId;
                                    mstEmployee.CostCenterId      = employee.CostCenterId;
                                    mstEmployee.CountryId         = employee.CountryId;
                                    mstEmployee.IsPresidentOffice = employee.IsPresidentOffice;
                                    mstEmployee.RoleId            = employee.RoleId;

                                    if (employee.ManagerEmployee != null)
                                    {
                                        if (employee.ManagerEmployee.UserId > 0)
                                        {
                                            mstEmployee.LineManagerId = employee.ManagerEmployee.UserId;
                                        }
                                    }
                                    else
                                    {
                                        mstEmployee.LineManagerId = employee.LineManagerId;
                                    }

                                    if (mstEmployee.LineManagerId == 0)
                                    {
                                        responeAC.Message    = "Line Manager is not valid !";
                                        responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                                        return(responeAC);
                                    }


                                    mstEmployee.UpdatedBy     = userId;
                                    mstEmployee.UpdatedDate   = DateTime.Now;
                                    mstEmployee.TransactionId = _iLogManagement.GenerateTeleBillingTransctionID();
                                    _dbTeleBilling_V01Context.Update(mstEmployee);
                                    await _dbTeleBilling_V01Context.SaveChangesAsync();

                                    responeAC.Message    = "Employee Updated Successfully !";
                                    responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Success);
                                    await _iLogManagement.SaveAuditActionLog((int)EnumList.AuditLogActionType.EditEmployee, loginUserName, userId, "Employee(" + mstEmployee.FullName + ")", (int)EnumList.ActionTemplateTypes.Edit, mstEmployee.UserId);

                                    return(responeAC);
                                }
                                else
                                {
                                    responeAC.Message    = "PFNumber is already exists!";
                                    responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                                    return(responeAC);
                                }
                            }
                            else
                            {
                                responeAC.Message    = "Employee is not system user.";
                                responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                                return(responeAC);
                            }
                        }
                    }
                }
                responeAC.Message    = _iStringConstant.DataNotFound;
                responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                return(responeAC);
            }
            catch (Exception e)
            {
                responeAC.Message    = "Error : " + e.Message;
                responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                return(responeAC);
            }
        }
        public async Task <MstEmployeeAC> GetEmployeeById(long userId)
        {
            MstEmployeeAC responseAc = new MstEmployeeAC();

            try
            {
                MstEmployee employee = await _dbTeleBilling_V01Context.MstEmployee.FindAsync(userId);


                MstEmployeeAC mstEmployee     = new MstEmployeeAC();
                MstEmployeeSP mstEmployeeData = new MstEmployeeSP();

                SortedList sl = new SortedList();
                sl.Add("userId", userId);
                DataSet ds = _objDalmysql.GetDataSet("uspGetEmployeeById", sl);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0] != null)
                    {
                        mstEmployeeData = _objDal.ConvertDataTableToGenericList <MstEmployeeSP>(ds.Tables[0]).FirstOrDefault();
                    }
                }

                if (mstEmployeeData != null)
                {
                    if (mstEmployeeData.UserId > 0)
                    {
                        mstEmployee = _mapper.Map <MstEmployeeAC>(mstEmployeeData);
                    }
                }

                if (employee != null)
                {
                    mstEmployee.FullName        = employee.FullName;
                    mstEmployee.ExtensionNumber = employee.ExtensionNumber;
                    mstEmployee.EmpPFNumber     = employee.EmpPfnumber;
                    mstEmployee.EmailId         = employee.EmailId;
                    mstEmployee.Password        = employee.Password;
                    mstEmployee.RoleId          = employee.RoleId;

                    mstEmployee.IsSystemUser = employee.IsSystemUser;
                    mstEmployee.Designation  = employee.Designation;

                    mstEmployee.DepartmentId      = employee.DepartmentId;
                    mstEmployee.BusinessUnitId    = employee.BusinessUnitId;
                    mstEmployee.CostCenterId      = employee.CostCenterId;
                    mstEmployee.CountryId         = employee.CountryId;
                    mstEmployee.IsPresidentOffice = employee.IsPresidentOffice;
                    mstEmployee.Description       = employee.Description;
                    mstEmployee.TransactionId     = employee.TransactionId;
                    mstEmployee.IsActive          = employee.IsActive;
                    mstEmployee.IsDelete          = employee.IsDelete;

                    //mstEmployee.BusinessUnit = mstEmployeeData.BusinessUnit;
                    //mstEmployee.CostCenter = mstEmployeeData.CostCenter;
                    //mstEmployee.Department = mstEmployeeData.Department;
                    //mstEmployee.Country = mstEmployeeData.Country;



                    if (employee.LineManagerId > 0)
                    {
                        MstEmployee lineManagerEmpDetail = new MstEmployee();
                        lineManagerEmpDetail = await _dbTeleBilling_V01Context.MstEmployee.Where(x => x.UserId == employee.LineManagerId).FirstOrDefaultAsync();

                        mstEmployee.LineManagerId   = employee.LineManagerId;
                        mstEmployee.ManagerEmployee = _mapper.Map <EmployeeAC>(lineManagerEmpDetail);
                    }
                }
                if (mstEmployee != null)
                {
                    if (mstEmployee.UserId > 0)
                    {
                        responseAc = mstEmployee;
                        return(responseAc);
                    }
                }

                return(new MstEmployeeAC());
            }
            catch (Exception e)
            {
                return(new MstEmployeeAC());
            }
        }
        public async Task <ResponseAC> AddEmployee(MstEmployeeAC employee, long userId, string loginUserName)
        {
            ResponseAC responeAC = new ResponseAC();

            try
            {
                if (employee != null)
                {
                    string pfNumber = employee.EmpPFNumber;
                    if (!await checkPFNumberUnique(pfNumber, 0))
                    {
                        MstEmployee mstEmployee = new MstEmployee();
                        mstEmployee.FullName        = employee.FullName;
                        mstEmployee.ExtensionNumber = employee.ExtensionNumber;
                        mstEmployee.EmpPfnumber     = employee.EmpPFNumber;
                        mstEmployee.EmailId         = employee.EmailId;

                        string randomPassword = "******";
                        string randomPadding  = CreatePassword(4);
                        mstEmployee.Password     = randomPassword + randomPadding;
                        mstEmployee.RoleId       = employee.RoleId;
                        mstEmployee.IsActive     = true;
                        mstEmployee.IsSystemUser = true;

                        mstEmployee.DepartmentId      = employee.DepartmentId;
                        mstEmployee.Designation       = employee.Designation;
                        mstEmployee.Description       = employee.Description;
                        mstEmployee.BusinessUnitId    = employee.BusinessUnitId;
                        mstEmployee.CostCenterId      = employee.CostCenterId;
                        mstEmployee.CountryId         = employee.CountryId;
                        mstEmployee.IsPresidentOffice = employee.IsPresidentOffice;
                        if (employee.ManagerEmployee != null)
                        {
                            if (employee.ManagerEmployee.UserId > 0)
                            {
                                mstEmployee.LineManagerId = employee.ManagerEmployee.UserId;
                            }
                        }

                        if (mstEmployee.LineManagerId == 0)
                        {
                            responeAC.Message    = "Line Manager is not valid !";
                            responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                            return(responeAC);
                        }



                        mstEmployee.CreatedBy     = userId;
                        mstEmployee.CreatedDate   = DateTime.Now;
                        mstEmployee.TransactionId = _iLogManagement.GenerateTeleBillingTransctionID();

                        await _dbTeleBilling_V01Context.AddAsync(mstEmployee);

                        await _dbTeleBilling_V01Context.SaveChangesAsync();

                        responeAC.Message    = "Employee Added Successfully !";
                        responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Success);
                        await _iLogManagement.SaveAuditActionLog((int)EnumList.AuditLogActionType.AddEmployee, loginUserName, userId, "Employee(" + mstEmployee.FullName + ")", (int)EnumList.ActionTemplateTypes.Add, mstEmployee.UserId);

                        #region Send Email for Registration Confirmation
                        EmployeeProfileAC empDetail = new EmployeeProfileAC();
                        try
                        {
                            empDetail = await GetUserProfile(mstEmployee.UserId);

                            if (empDetail != null)
                            {
                                if (empDetail.UserProfileData.UserId > 0)
                                {
                                    Dictionary <string, string> replacement = new Dictionary <string, string>();
                                    replacement.Add("{newEmpName}", empDetail.UserProfileData.FullName);
                                    replacement.Add("{PFNumber}", empDetail.UserProfileData.EmpPFNumber);
                                    replacement.Add("{Email}", empDetail.UserProfileData.EmailId);
                                    replacement.Add("{Password}", empDetail.UserProfileData.Password);

                                    replacement.Add("{EmpDesignation}", empDetail.UserProfileData.Designation);
                                    replacement.Add("{Emplocation}", empDetail.UserProfileData.Country);
                                    replacement.Add("{lineManagerDepartment}", empDetail.UserProfileData.LineManager);
                                    replacement.Add("{EmpDepartment}", empDetail.UserProfileData.Department);
                                    replacement.Add("{EmpCostCenter}", empDetail.UserProfileData.CostCenter);
                                    replacement.Add("{EmpBusinessUnit}", empDetail.UserProfileData.BusinessUnit);
                                    bool   issent  = false;
                                    string EmailId = empDetail.UserProfileData.EmailId;

                                    if (!(string.IsNullOrEmpty(empDetail.UserProfileData.EmailId) || empDetail.UserProfileData.EmailId == "n/a"))
                                    {
                                        issent = await _iEmailSender.SendEmail(Convert.ToInt64(EnumList.EmailTemplateType.NewRegistrationConfirmation), replacement, employee.EmailId);
                                    }
                                    else
                                    {// get line manager email
                                        string linemanagerEmail = await _dbTeleBilling_V01Context.MstEmployee.Where(x => x.UserId == mstEmployee.LineManagerId).Select(x => x.EmailId).FirstOrDefaultAsync();

                                        issent = await _iEmailSender.SendEmail(Convert.ToInt64(EnumList.EmailTemplateType.NewRegistrationInYourTeam), replacement, linemanagerEmail);
                                    }

                                    if (!issent)
                                    {
                                        responeAC.StatusCode = Convert.ToInt32(EnumList.ResponseType.Success);
                                        responeAC.Message    = "Employee Added Successfully! We Could Not Sent Mail Confirmation.";
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            responeAC.StatusCode = Convert.ToInt32(EnumList.ResponseType.Success);
                            responeAC.Message    = "Employee Added Successfully! Error :" + e.Message + " We Could Not Sent Mail Confirmation.";
                        }

                        #endregion

                        return(responeAC);
                    }
                    else
                    {
                        responeAC.Message    = "PFNumber is already exists!";
                        responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                        return(responeAC);
                    }
                }
                else
                {
                    responeAC.Message    = _iStringConstant.DataNotFound;
                    responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                    return(responeAC);
                }
            }
            catch (Exception e)
            {
                responeAC.Message    = "Error : " + e.Message;
                responeAC.StatusCode = Convert.ToInt16(EnumList.ResponseType.Error);
                return(responeAC);
            }
        }