public IHttpActionResult AddEmpPayScale(int EmpId) { try { using (MaxMasterDbEntities db = new MaxMasterDbEntities()) { var form = HttpContext.Current.Request.Form; EmployeePayscale emp = db.EmployeePayscales.Where(x => x.Emp_Id == EmpId).FirstOrDefault(); //EmployeePayscale empPayScale = new EmployeePayscale(); emp.BankAccNo = form.Get("AccountNumber"); emp.BankAccName = form.Get("AccountName"); emp.BankBranch = form.Get("BranchName"); emp.BankName = form.Get("BankName"); emp.Gross = Convert.ToInt32(form.Get("CTC")); db.Entry(emp).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(Ok()); } } catch (Exception ex) { new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace); return(Content(HttpStatusCode.InternalServerError, "An error occureed, please try again later")); } }
public async Task <IHttpActionResult> AddEmployee() { try { /* get data from form */ var form = HttpContext.Current.Request.Form; var email = form.Get("Email"); var firstName = form.Get("FirstName"); var role = form.Get("RoleId"); var doj = form.Get("DOJ"); var provisionalPeriod = form.Get("ProvisionalPeriod"); var managerId = form.Get("Manager"); /* Register to AspNet Users */ UserManager <ApplicationUser> userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var user1 = userManager.FindByEmail(email); if (user1 != null) { return(Content(HttpStatusCode.InternalServerError, "User with email " + email + " already exists")); } using (MaxMasterDbEntities db = new MaxMasterDbEntities()) { var roleName = db.AspNetRoles.Where(x => x.Id == role).FirstOrDefault().Name; Employee emp = new Employee(); emp.FirstName = firstName; emp.MiddleName = form.Get("MiddleName"); emp.LastName = form.Get("LastName"); emp.Email = form.Get("Email"); emp.PrimaryPhone = form.Get("PrimaryNum"); emp.SecondaryPhone = form.Get("SecondaryNum"); emp.Gender = form.Get("Gender"); emp.DOB = DateTime.Parse(form.Get("DOB")); emp.BloodGroup = form.Get("BloodGroup"); emp.Aadhar = form.Get("Aadhar"); emp.PAN = form.Get("Pan"); emp.AddressLine1 = form.Get("AddressLine1"); emp.AddressLine2 = form.Get("AddressLine2"); emp.Country_Id = Convert.ToInt32(form.Get("CountryId")); emp.State_Id = Convert.ToInt32(form.Get("StateId")); emp.City_Id = Convert.ToInt32(form.Get("CityId")); emp.ZIP = form.Get("Zip"); emp.Role_Id = (form.Get("RoleId")); emp.Department_Id = Convert.ToInt32(form.Get("DepartmentId")); emp.Designation_Id = Convert.ToInt32(form.Get("DesignationId")); emp.OrgId = Convert.ToInt32(form.Get("OrgId")); emp.Shift_Id = Convert.ToInt32(form.Get("Shift")); if (provisionalPeriod != "") { emp.ProvisionalPeriod = Convert.ToByte(provisionalPeriod); } else { emp.ProvisionalPeriod = 0; } if (!string.IsNullOrEmpty(managerId)) { emp.Manager_Id = Convert.ToInt32(form.Get("Manager")); } emp.EmploymentType = form.Get("EmpType"); if (!string.IsNullOrEmpty(doj)) { emp.DOJ = DateTime.Parse(form.Get("DOJ")); } emp.UpdatedBy = User.Identity.GetUserId(); emp.LastUpdated = DateTime.Now; emp.Active = true; var prefix = db.Organisations.Where(x => x.Id == emp.OrgId).FirstOrDefault().EmpPrefix; if (emp.EmploymentType == "Consultant") { int Count = db.Employees.Where(x => x.EmploymentType == "Consultant" && x.OrgId == emp.OrgId).Count(); if (Count > 0) { var LastEmployeeNumber = db.Employees.Where(x => x.EmploymentType == "Consultant" && x.OrgId == emp.OrgId).OrderByDescending(x => x.LastUpdated).FirstOrDefault().EmployeeNumber; if (LastEmployeeNumber != null) { int count = Convert.ToInt32(LastEmployeeNumber.Split('V')[1]); GenerateNewNumber: var employeeNumber = prefix + "V" + (count + 1).ToString("00"); var empExists = db.Employees.Where(x => x.EmployeeNumber == employeeNumber).FirstOrDefault(); var userexists = await _userManager.FindByNameAsync(employeeNumber); if (empExists != null || userexists != null) { count++; goto GenerateNewNumber; } emp.EmployeeNumber = employeeNumber; } } else { emp.EmployeeNumber = prefix + "V01"; } } if (emp.EmploymentType == "Permanent") { int Count = db.Employees.Where(x => x.EmploymentType == "Permanent" && x.OrgId == emp.OrgId).Count(); if (Count > 0) { var LastEmployeeNumber = db.Employees.Where(x => x.EmploymentType == "Permanent" && x.OrgId == emp.OrgId).OrderByDescending(x => x.EmployeeNumber).FirstOrDefault().EmployeeNumber; if (LastEmployeeNumber != null) { int count = Convert.ToInt32(LastEmployeeNumber.Split('-')[1]); GenerateNewNumber: var employeeNumber = prefix + (count + 1).ToString("000"); if (db.Employees.Any(x => x.EmployeeNumber == employeeNumber)) { count++; goto GenerateNewNumber; } emp.EmployeeNumber = employeeNumber; } } else { emp.EmployeeNumber = prefix + "001"; } } var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; if (file != null && file.ContentLength > 0) { var fileDirectory = HttpContext.Current.Server.MapPath("~/Employee_Images"); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } var physicalPath = Path.Combine(ConfigurationManager.AppSettings["ApiUrl"], emp.EmployeeNumber + Path.GetExtension(file.FileName)); file.SaveAs(physicalPath); var path = Path.Combine(ConfigurationManager.AppSettings["ApiUrl"], "Employee_Images", emp.EmployeeNumber + Path.GetExtension(file.FileName)); emp.PhotoURL = path; } string organisation = db.Organisations.Where(x => x.Id == emp.OrgId).FirstOrDefault().OrgName; bool UserCreated = new UserController().CreateUser(emp.EmployeeNumber, email, roleName, organisation); var user = userManager.FindByEmail(email); if (user != null) { emp.AspNetUserId = user.Id; } db.Employees.Add(emp); EmployeePayscale empPayScale = new EmployeePayscale(); empPayScale.Gross = 0; empPayScale.Emp_Id = emp.Id; db.EmployeePayscales.Add(empPayScale); db.SaveChanges(); return(Ok()); } } catch (Exception ex) { new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace); return(Content(HttpStatusCode.InternalServerError, "An error occoured, please try again!")); } }