Пример #1
0
 public static void Add(AbsenceDTO absence)
 {
     using (Entities db = new Entities())
     {
         db.Absences.Add(_CastDTO.DTOToAbsence(absence));
         db.SaveChanges();
     }
 }
Пример #2
0
 public static void Update(AbsenceDTO absence)
 {
     using (Entities db = new Entities())
     {
         var abs = db.Absences.FirstOrDefault(a => a.Id == absence.Id);
         abs.Name = absence.Name;
         db.SaveChanges();
     }
 }
Пример #3
0
 public IHttpActionResult Update([FromBody] AbsenceDTO absence)
 {
     try
     {
         Absence.Update(absence);
         return(Ok());
     }
     catch (Exception e)
     {
         LogManager.LogException(e);
         return(InternalServerError(e));
     }
 }
        public HttpResponseMessage AbsenceAddOrEdit([FromBody] AbsenceDTO uDto)
        {
            string key;
            var    ur        = new AppUserRepository();
            var    AbsenceId = 0;
            var    userId    = ur.ValidateUser(uDto.Key, out key, ref AbsenceId);

            AppUserRoleRepository aur = new AppUserRoleRepository();

            uDto.RegEmpLate          = uDto.RegEmpLate == "" ? null : uDto.RegEmpLate;
            uDto.RegEmpLeftEarly     = uDto.RegEmpLeftEarly == "" ? null : uDto.RegEmpLeftEarly;
            uDto.RegEmpOut           = uDto.RegEmpOut == "" ? null : uDto.RegEmpOut;
            uDto.TempEmpLate         = uDto.TempEmpLate == "" ? null : uDto.TempEmpLate;
            uDto.TempEmpLeftEarly    = uDto.TempEmpLeftEarly == "" ? null : uDto.TempEmpLeftEarly;
            uDto.TempEmpOut          = uDto.TempEmpOut == "" ? null : uDto.TempEmpOut;
            uDto.InmateLeftEarly     = uDto.InmateLeftEarly == "" ? null : uDto.InmateLeftEarly;
            uDto.InmateOut           = uDto.InmateOut == "" ? null : uDto.InmateOut;
            uDto.EmployeesOnVacation = uDto.EmployeesOnVacation == "" ? null : uDto.EmployeesOnVacation;

            if (userId > 0 && aur.IsInRole(userId, "Data Entry"))
            {
                var Absence = new Absence();
                var errors  = ValidateDtoData(uDto, Absence);
                if (errors.Any())
                {
                    return(ProcessValidationErrors(Request, errors, key));
                }
                var NEUserId = 0;


                if (int.TryParse(uDto.AbsenceID, out NEUserId))
                {
                    if (NEUserId == -1)
                    {
                        //  creating new User record
                        return(ProcessNewAbsenceRecord(Request, uDto, key, AbsenceId, userId));
                    }
                    else
                    {
                        //  editing existing User record
                        return(ProcessExistingAbsenceRecord(Request, uDto, NEUserId, key, AbsenceId, userId));
                    }
                }
                //  no idea what this is
                var msg = "invalid data structure submitted";
                return(Request.CreateResponse(HttpStatusCode.BadRequest, msg));
            }
            var message = "validation failed";

            return(Request.CreateResponse(HttpStatusCode.NotFound, message));
        }
        private void UpdateDepartmentTotalAbsence(AbsenceDTO uDto)
        {
            var pr         = new DepartmentTotalRepository();
            var prod       = new DepartmentTotal();
            var prodexists = pr.GetByDateAndDepartment(DateTime.Parse(uDto.AbsenceDate), int.Parse(uDto.DepartmentID));
            int wb         = 0;

            wb  = uDto.RegEmpLate == null ? 0 : int.Parse(uDto.RegEmpLate);
            wb += uDto.RegEmpLeftEarly == null ? 0 : int.Parse(uDto.RegEmpLeftEarly);
            wb += uDto.RegEmpOut == null ? 0 : int.Parse(uDto.RegEmpOut);
            wb += uDto.TempEmpLate == null ? 0 : int.Parse(uDto.TempEmpLate);
            wb += uDto.TempEmpLeftEarly == null ? 0 : int.Parse(uDto.TempEmpLeftEarly);
            wb += uDto.TempEmpOut == null ? 0 : int.Parse(uDto.TempEmpOut);
            wb += uDto.InmateLeftEarly == null ? 0 : int.Parse(uDto.InmateLeftEarly);
            wb += uDto.InmateOut == null ? 0 : int.Parse(uDto.InmateOut);
            wb += uDto.EmployeesOnVacation == null ? 0 : int.Parse(uDto.EmployeesOnVacation);
            if (prodexists == null)
            {
                prod.DepartmentID = int.Parse(uDto.DepartmentID);
                prod.DTDate       = DateTime.Parse(uDto.AbsenceDate);
                prod.Absences     = wb;
                pr.Save(prod);
            }
            else
            {
                prod = prodexists;
                var            wbr = new AbsenceRepository();
                List <Absence> wbl = wbr.GetByDateAndDepartment(DateTime.Parse(uDto.AbsenceDate), int.Parse(uDto.DepartmentID));
                wb  = wbl.Where(x => x.RegEmpLate != null).Sum(x => x.RegEmpLate).Value;
                wb += wbl.Where(x => x.RegEmpLeftEarly != null).Sum(x => x.RegEmpLeftEarly).Value;
                wb += wbl.Where(x => x.RegEmpOut != null).Sum(x => x.RegEmpOut).Value;
                wb += wbl.Where(x => x.TempEmpLate != null).Sum(x => x.TempEmpLate).Value;
                wb += wbl.Where(x => x.TempEmpLeftEarly != null).Sum(x => x.TempEmpLeftEarly).Value;
                wb += wbl.Where(x => x.TempEmpOut != null).Sum(x => x.TempEmpOut).Value;
                wb += wbl.Where(x => x.InmateLeftEarly != null).Sum(x => x.InmateLeftEarly).Value;
                wb += wbl.Where(x => x.InmateOut != null).Sum(x => x.InmateOut).Value;
                wb += wbl.Where(x => x.EmployeesOnVacation != null).Sum(x => x.EmployeesOnVacation).Value;

                if (wb != 0)
                {
                    prod.Absences = wb;
                }
                pr.Save(prod);
            }
        }
Пример #6
0
        public ActionResult Create(AbsenceDTO model, string sortType)
        {
            Absences        absence       = new Absences();
            List <Absences> absenceList   = new List <Absences>();
            List <DateTime> selectedDates = new List <DateTime>();

            try
            {
                if (ModelState.IsValid)
                {
                    if (model.StartDate != null && model.EndDate == null)
                    {
                        CultureInfo cul = CultureInfo.CurrentCulture;

                        int weekNum = cul.Calendar.GetWeekOfYear(
                            model.StartDate,
                            CalendarWeekRule.FirstDay,
                            DayOfWeek.Monday);


                        absence.Date        = model.StartDate;
                        absence.Hours       = model.Hours;
                        absence.AbsenceType = sortType;
                        absence.PersonID    = model.PersonID;
                        absence.week        = weekNum;


                        _context.Absences.Add(absence);
                        _context.SaveChanges();
                    }

                    else if (model.StartDate != null && model.EndDate != null)
                    {
                        for (var date = model.StartDate; date <= model.EndDate; date = date.AddDays(1))
                        {
                            selectedDates.Add(date);
                        }

                        foreach (var item in selectedDates)
                        {
                            CultureInfo cul = CultureInfo.CurrentCulture;

                            int weekNum = cul.Calendar.GetWeekOfYear(
                                item,
                                CalendarWeekRule.FirstDay,
                                DayOfWeek.Monday);

                            absenceList.Add(new Absences
                            {
                                Date        = item,
                                AbsenceType = sortType,
                                Hours       = model.Hours,
                                week        = weekNum,
                                PersonID    = model.PersonID
                            });
                        }

                        foreach (var item in absenceList)
                        {
                            _context.Absences.Add(item);
                            _context.SaveChanges();
                        }
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
        private HttpResponseMessage ProcessNewAbsenceRecord(HttpRequestMessage request, AbsenceDTO uDto, string key, int AbsenceId, int userId)
        {
            var ur   = new AbsenceRepository();
            var user = new Absence();


            var validationErrors = GetValidationErrors(ur, user, uDto, AbsenceId, userId);

            if (validationErrors.Any())
            {
                return(ProcessValidationErrors(request, validationErrors, key));
            }

            user = ur.Save(user);

            UpdateDepartmentTotalAbsence(uDto);
            uDto.Key       = key;
            uDto.AbsenceID = user.AbsenceID.ToString();
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);

            response.Headers.Location = new Uri(Url.Link("Default", new
            {
                id = user.AbsenceID
            }));
            return(response);
        }
        internal HttpResponseMessage AbsenceDates(HttpRequestMessage request, AbsenceDTO cqDTO)
        {
            string key;
            var    aur       = new AppUserRepository();
            var    companyId = 0;
            var    userId    = aur.ValidateUser(cqDTO.Key, out key, ref companyId);

            if (userId > 0)
            {
                var ur = new AbsenceRepository();
                var u  = new Absence();
                if (cqDTO.AbsenceDate != null)
                {
                    cqDTO.Start_AbsenceDate = DateTime.Parse(cqDTO.AbsenceDate).ToString();
                    cqDTO.End_AbsenceDate   = DateTime.Parse(cqDTO.AbsenceDate).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_AbsenceDate = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_AbsenceDate   = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_AbsenceDate = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_AbsenceDate   = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_AbsenceDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_AbsenceDate   = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) + 1).ToString() + "/14/" + cqDTO.StartDateYear).ToString();
                    }

                    cqDTO.StartDateMonth = null;
                    cqDTO.StartDateYear  = null;
                }
                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data      = ur.GetByPredicate(predicate);
                var col       = new Collection <Dictionary <string, string> >();
                data = data.GroupBy(x => x.AbsenceDate).Select(x => x.First()).OrderBy(x => x.AbsenceDate).ToList();
                foreach (var item in data)
                {
                    var dic = new Dictionary <string, string>();


                    dic.Add("AbsenceDate", item.AbsenceDate.ToShortDateString());

                    col.Add(dic);
                    var ufdic = new Dictionary <string, string>();
                }

                var retVal = new GenericDTO
                {
                    Key        = key,
                    ReturnData = col
                };
                return(Request.CreateResponse(HttpStatusCode.OK, retVal));
            }
            var message = "validation failed";

            return(request.CreateResponse(HttpStatusCode.NotFound, message));
        }
 public HttpResponseMessage AbsenceDates([FromBody] AbsenceDTO cqDTO)
 {
     return(AbsenceDates(Request, cqDTO));
 }
 private List <DbValidationError> GetValidationErrors(AbsenceRepository pr, Absence contact, AbsenceDTO cqDto, int YieldID, int userId)
 {
     contact.ProcessRecord(cqDto);
     return(pr.Validate(contact));
 }
        private HttpResponseMessage ProcessExistingAbsenceRecord(HttpRequestMessage request, AbsenceDTO cqDto, int contactId, string key, int AbsenceId, int userId)
        {
            var ur   = new AbsenceRepository();
            var user = new Absence();

            user = ur.GetById(contactId);


            var validationErrors = GetValidationErrors(ur, user, cqDto, AbsenceId, userId);

            if (validationErrors.Any())
            {
                return(ProcessValidationErrors(request, validationErrors, key));
            }

            //if (cqDto.Remove != null && int.Parse(cqDto.Remove) == 1)
            //{
            //    ur.Delete(user);
            //}
            else
            {
                ur.Save(user);
            }

            UpdateDepartmentTotalAbsence(cqDto);

            cqDto.Key = key;
            return(request.CreateResponse(HttpStatusCode.Accepted, cqDto));
        }