public async Task <IActionResult> AddAbsens(string id, AddAbsens addAbsens)
        {
            var workers = await _userManager.FindByIdAsync(id);

            var lastAbsens = await _dbContext.WorkerAbsens.Where(x => x.WorkerId == workers.Id && x.AbsensId == 2 && x.Date.Year == DateTime.Now.Year && x.Date.Month == DateTime.Now.Month).LastOrDefaultAsync();

            int dateCount = 0;

            if (lastAbsens != null)
            {
                dateCount = DateTime.Now.Day - lastAbsens.Date.Day;
            }

            if (!ModelState.IsValid)
            {
                addAbsens.WorkerAccount = workers.Account;
                addAbsens.WorkerId      = workers.Id;
                addAbsens.Absens        = _dbContext.Absens.ToList();
                return(View(addAbsens));
            }

            WorkerAbsens workerAbsens = new WorkerAbsens();

            workerAbsens.WorkerId = workers.Id;
            workerAbsens.AbsensId = addAbsens.SelectedAbsens;
            workerAbsens.Date     = DateTime.Now;
            workerAbsens.Reason   = addAbsens.Reason;

            if (dateCount == 1)
            {
                workerAbsens.AbsensCount = lastAbsens.AbsensCount + 1;
            }
            else
            {
                workerAbsens.AbsensCount = 1;
            }

            await _dbContext.WorkerAbsens.AddAsync(workerAbsens);

            await _dbContext.SaveChangesAsync();

            if (lastAbsens != null)
            {
                if (lastAbsens.AbsensCount == 4 && dateCount == 1)
                {
                    workers.Working = false;
                    await _dbContext.SaveChangesAsync();
                }
            }

            return(RedirectToAction("WorkerList", "Worker"));
        }
        public async Task <IActionResult> Create(EmployeeViewModel create, Employee employee)
        {
            create.Genders    = _dbContext.Genders.ToList();
            create.Maritals   = _dbContext.MaritalStatuses.ToList();
            create.Educations = _dbContext.Educations.ToList();

            string imgName = $"{Path.GetRandomFileName().ToUpper()}_{Path.GetRandomFileName().ToLower()}_{DateTime.Now.ToString("dd_MM_yyyy_hh_mm")}.jpeg";

            if (!ModelState.IsValid)
            {
                return(View(create));
            }

            if (ModelState["Photo"].ValidationState == ModelValidationState.Invalid)
            {
                return(View(create));
            }

            if (!create.Photo.IsImage())
            {
                ModelState.AddModelError("Photo", "File type must be image");
                return(View(create));
            }

            employee.Image = await employee.Photo.SaveAsync(_env.WebRootPath);

            employee.Name                 = create.Name;
            employee.Surname              = create.Surname;
            employee.FatherName           = create.FatherName;
            employee.Born                 = create.Born;
            employee.Residence            = create.Residence;
            employee.IDCardNumber         = create.IDCardNumber;
            employee.IDCardFinCode        = create.IDCardFinCode;
            employee.DistrictRegistration = create.DistrictRegistration;
            employee.EducationId          = create.SelectedEducation;
            employee.GenderId             = create.SelectedGender;
            employee.MaritalStatusId      = create.SelectedMarital;
            employee.Number               = create.Number;

            await _dbContext.Employees.AddAsync(employee);

            await _dbContext.SaveChangesAsync();

            return(RedirectToAction(nameof(EmployeeList)));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddVacation(string id, VacationViewModel vacationView)
        {
            var worker = await _userManager.FindByIdAsync(id);

            if (!ModelState.IsValid)
            {
                vacationView.WorkerAccount = worker.Account;
                vacationView.WorkerId      = worker.Id;
                return(View(vacationView));
            }

            Vacation vacation = new Vacation();

            vacation.WorkerId  = worker.Id;
            vacation.StartDate = vacationView.StartDate;
            vacation.EndDate   = vacationView.EndDate;

            await _dbContext.Vacations.AddAsync(vacation);

            await _dbContext.SaveChangesAsync();

            return(RedirectToAction("WorkerList", "Worker"));
        }
        public async Task <IActionResult> CalculateSalary(SalaryModel salaryModel)
        {
            List <Salary>       salary        = new List <Salary>();
            List <SalaryForApi> salaryForApis = new List <SalaryForApi>();

            foreach (var item in salaryModel.AvialableWorkers)
            {
                if (item.IsChecked == true)
                {
                    salary.Add(new Salary()
                    {
                        EmployeeId = item.EmployeeId, TotalSalary = item.TotalSalary, CalculatedDate = item.OldCalculate
                    });
                    salaryForApis.Add(new SalaryForApi()
                    {
                        IDCardNumber = item.IDCardNumber, Balance = item.TotalSalary
                    });
                }
            }

            using (HttpClient httpClient = new HttpClient())
            {
                string jsonString = JsonConvert.SerializeObject(salaryForApis);
                var    requestUrl = new Uri("https://localhost:44399/api/salary?salary=" + jsonString);

                using (HttpContent httpContent = new StringContent(jsonString))
                {
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = httpClient.PutAsync(requestUrl, httpContent).Result;
                }
            }
            _dbContext.Salaries.AddRange(salary);
            await _dbContext.SaveChangesAsync();

            return(RedirectToAction(nameof(WorkerSalary)));
        }
        public async Task <IActionResult> Edit(string id, WorkersViewModel workersView)
        {
            var workers = await _userManager.FindByIdAsync(id);

            string companyWorkPlaceId       = $"{Path.GetRandomFileName()}_{DateTime.Now.ToString("dd_MM_yyyy_hh_mm")}";
            string companyWorkPlaceBonusId  = $"{Path.GetRandomFileName().ToLower()}_{DateTime.Now.ToString("dd_MM_yyyy_hh_mm")}";
            string companyWorkPlaceAbsensId = $"{Path.GetRandomFileName().ToUpper()}_{DateTime.Now.ToString("dd_MM_yyyy_hh_mm")}";

            workersView.Departments = _dbContext.Departments.ToList();
            workersView.Employees   = _dbContext.Employees.ToList();
            workersView.Name        = _dbContext.Employees.Where(x => x.ID == workers.EmployeeId).Select(x => x.Name).FirstOrDefault();
            workersView.Surname     = _dbContext.Employees.Where(x => x.ID == workers.EmployeeId).Select(x => x.Surname).FirstOrDefault();
            workersView.Position    = _dbContext.Positions.Where(x => x.ID == workers.PositionId).Select(x => x.Name).FirstOrDefault();
            workersView.Email       = workers.Email;
            workersView.Department  = _dbContext.Positions.Include(m => m.Department).Where(x => x.ID == workers.PositionId).Select(m => m.Department.Name).FirstOrDefault();
            workersView.Password    = workers.PassText;

            if (!ModelState.IsValid)
            {
                return(View(workersView));
            }

            if (workersView.SelectedPosition == workers.PositionId)
            {
                return(View(workersView));
            }

            CompanyWorkPlace companyWorkPlace = new CompanyWorkPlace
            {
                ID          = companyWorkPlaceId,
                EmployeeId  = workers.EmployeeId,
                ChangedDate = DateTime.Now,
                PositionId  = workersView.SelectedPosition
            };

            _dbContext.CompanyWorkPlaces.Add(companyWorkPlace);

            CompanyWorkPlaceBonus companyWorkPlaceBonus = new CompanyWorkPlaceBonus
            {
                ID = companyWorkPlaceBonusId,
                CompanyWorkPlaceId = companyWorkPlaceId,
                BonusSalary        = _dbContext.WorkerBonus.Where(x => x.WorkerId == workers.Id).Select(x => x.BonusSalary).Sum()
            };

            _dbContext.CompanyWorkPlaceBonus.Add(companyWorkPlaceBonus);

            CompanyWorkPlaceAbsens companyWorkPlaceAbsens = new CompanyWorkPlaceAbsens
            {
                ID = companyWorkPlaceAbsensId,
                CompanyWorkPlaceId = companyWorkPlaceId,
                ExcusableAbsens    = _dbContext.WorkerAbsens.Where(x => x.WorkerId == workers.Id && x.AbsensId == 1).Count(),
                UnExcusableAbsens  = _dbContext.WorkerAbsens.Where(x => x.WorkerId == workers.Id && x.AbsensId == 2).Count()
            };

            _dbContext.CompanyWorkPlaceAbsens.Add(companyWorkPlaceAbsens);

            Worker worker = new Worker
            {
                BeginDate  = DateTime.Now,
                Account    = workers.Account,
                Email      = workers.Email,
                EmployeeId = workers.EmployeeId,
                PassText   = workers.PassText,
                UserName   = workers.UserName,
                PositionId = workersView.SelectedPosition,
                Working    = true
            };

            _dbContext.WorkerBonus.RemoveRange(_dbContext.WorkerBonus.Where(x => x.WorkerId == workers.Id).ToList());
            _dbContext.WorkerAbsens.RemoveRange(_dbContext.WorkerAbsens.Where(x => x.WorkerId == workers.Id).ToList());

            await _userManager.DeleteAsync(workers);

            IdentityResult result = await _userManager.CreateAsync(worker, workers.PassText);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            await _userManager.AddToRoleAsync(worker, Roles.Worker.ToString());

            await _dbContext.SaveChangesAsync();

            return(RedirectToAction(nameof(WorkerList)));
        }