Esempio n. 1
0
 public ActionResult Index(EmployeeVM employeeVM)
 {
     try
     {
         using (IDbConnection cn = new SqlConnection(_connectionString))
         {
             cn.Open();
             employeeVM.Employee.DateTimeStampRecord(DbChangeType.Create);
             cn.Insert <Employee>(employeeVM.Employee);
             if (employeeVM.Employee.EmpId > 0)
             {
                 LeaveStatus leaveStatus = new LeaveStatus();
                 leaveStatus.EmpId = employeeVM.Employee.EmpId;
                 cn.Insert <LeaveStatus>(leaveStatus);
                 var user = new ApplicationUser()
                 {
                     UserName = employeeVM.Employee.Email, FirstName = employeeVM.Employee.EmpFirstName, LastName = employeeVM.Employee.EmpLastName, Email = employeeVM.Employee.Email
                 };
                 var result = UserManager.Create(user, employeeVM.Password.Trim());
                 if (result.Succeeded)
                 {
                     string defaultRole = UserRole.Employee.ToString();
                     if (employeeVM.Employee.IsSupervisor)
                     {
                         defaultRole = UserRole.Supervisor.ToString();
                     }
                     UserManager.AddToRole(user.Id, defaultRole);
                     var curUser = UserManager.FindByName(User.Identity.Name);
                     var task    = new Task(() => { MailUtility.SendAccountMail(employeeVM.Employee.EmpFirstName, employeeVM.Password.Trim(), employeeVM.Employee.Email, curUser.Email, curUser.FirstName); });
                     task.Start();
                     //MailUtility.SendAccountMail(employeeVM.Employee.EmpFirstName, employeeVM.Password, employeeVM.Employee.Email, curUser.Email, curUser.FirstName);
                 }
             }
             cn.Close();
         }
     }
     catch (Exception ex)
     {
         ConfigLogger.Instance.LogError(ex);
     }
     return(RedirectToAction("Index"));
 }
Esempio n. 2
0
 public JsonResult UpdateEmployeeData(EmployeeVM employeeVM, FormCollection frm)
 {
     try
     {
         if (!string.IsNullOrWhiteSpace(employeeVM.Employee.EmpCode))
         {
             if (IsExistEmpCode(employeeVM.Employee.EmpCode, employeeVM.Employee.EmpId))
             {
                 return(Json("Employee Code already exist!", JsonRequestBehavior.AllowGet));
             }
         }
         using (IDbConnection cn = new SqlConnection(_connectionString))
         {
             cn.Open();
             if (employeeVM.Employee.EmpId > 0)
             {
                 employeeVM.Employee.DateTimeStampRecord(DbChangeType.Update);
                 employeeVM.Employee.IsActive = true;
                 cn.Update <Employee>(employeeVM.Employee);
                 SaveEmpSupIds(cn, employeeVM.Employee.EmpId, employeeVM.SupIds);
                 string userName    = cn.GetList <AspNetUsers>().Where(x => x.Email == employeeVM.Employee.Email).FirstOrDefault().UserName;
                 var    user        = UserManager.FindByName(userName);
                 string role        = UserManager.GetRoles(user.Id)[0];
                 string defaultRole = UserRole.Employee.ToString();
                 if (employeeVM.Employee.IsSupervisor)
                 {
                     defaultRole = UserRole.Supervisor.ToString();
                 }
                 UserManager.RemoveFromRole(user.Id, role);
                 UserManager.AddToRole(user.Id, defaultRole);
                 user.FirstName = employeeVM.Employee.EmpFirstName;
                 user.LastName  = employeeVM.Employee.EmpLastName;
                 UserManager.Update(user);
             }
             else
             {
                 employeeVM.Employee.DateTimeStampRecord(DbChangeType.Create);
                 employeeVM.Employee.IsActive = true;
                 cn.Insert <Employee>(employeeVM.Employee);
                 if (employeeVM.Employee.EmpId > 0)
                 {
                     SaveEmpSupIds(cn, employeeVM.Employee.EmpId, employeeVM.SupIds);
                     LeaveStatus leaveStatus = new LeaveStatus();
                     leaveStatus.EmpId = employeeVM.Employee.EmpId;
                     cn.Insert <LeaveStatus>(leaveStatus);
                     var user = new ApplicationUser()
                     {
                         UserName = employeeVM.Employee.Email, FirstName = employeeVM.Employee.EmpFirstName, LastName = employeeVM.Employee.EmpLastName, Email = employeeVM.Employee.Email
                     };
                     var result = UserManager.Create(user, employeeVM.Password.Trim());
                     if (result.Succeeded)
                     {
                         string defaultRole = UserRole.Employee.ToString();
                         if (employeeVM.Employee.IsSupervisor)
                         {
                             defaultRole = UserRole.Supervisor.ToString();
                         }
                         UserManager.AddToRole(user.Id, defaultRole);
                         var curUser = UserManager.FindByName(User.Identity.Name);
                         var task    = new Task(() => { MailUtility.SendAccountMail(employeeVM.Employee.EmpFirstName, employeeVM.Password.Trim(), employeeVM.Employee.Email, curUser.Email, curUser.FirstName); });
                         task.Start();
                         //MailUtility.SendAccountMail(employeeVM.Employee.EmpFirstName, employeeVM.Password, employeeVM.Employee.Email, curUser.Email, curUser.FirstName);
                     }
                     else
                     {
                         cn.Delete <Employee>(employeeVM.Employee);
                         return(Json("Email already exist!", JsonRequestBehavior.AllowGet));
                     }
                 }
             }
             cn.Close();
         }
         ListDataService.RefreshLists();
     }
     catch (Exception ex)
     {
         ConfigLogger.Instance.LogError(ex);
         return(Json("Some error occurred.", JsonRequestBehavior.AllowGet));
     }
     return(Json("Success", JsonRequestBehavior.AllowGet));
 }