public ActionResult DeleteDisplinePeriod(int id)
        {
            List <Error> errors = new List <Error>();
            DataSource <DisplinPeriodViewModel> Source = new DataSource <DisplinPeriodViewModel>();
            DisplinPeriod Displine = _hrUnitOfWork.DisciplineRepository.GetDisplinPeriod(id);

            if (Displine != null)
            {
                AutoMapper(new Models.AutoMapperParm
                {
                    Source     = Displine,
                    ObjectName = "DisplinPeriods",
                    Transtype  = TransType.Delete
                });
                _hrUnitOfWork.DisciplineRepository.RemoveDisplinPeriod(Displine.Id);
            }
            var DeletedPeriods = _hrUnitOfWork.Repository <DisPeriodNo>().Where(a => a.PeriodId == id).ToList();

            if (DeletedPeriods != null)
            {
                _hrUnitOfWork.DisciplineRepository.RemoveRange(DeletedPeriods);
            }

            string message = "OK";

            Source.Errors = SaveChanges(Language);
            if (Source.Errors.Count > 0)
            {
                return(Json(Source));
            }
            else
            {
                return(Json(message));
            }
        }
        public ActionResult SaveDisplinPeriod(DisplinPeriodViewModel model, OptionsViewModel moreInfo, RequestDisplinRangeGrid grid1, int?periodval)
        {
            List <Error> errors = new List <Error>();

            if (ModelState.IsValid)
            {
                if (ServerValidationEnabled)
                {
                    errors = _hrUnitOfWork.CompanyRepository.CheckForm(new CheckParm
                    {
                        CompanyId  = CompanyId,
                        ObjectName = "DisplinPeriods",
                        TableName  = "DisplinPeriods",
                        Columns    = Models.Utils.GetColumnViews(ModelState.Where(a => !a.Key.Contains('.'))),
                        Culture    = Language
                    });

                    if (errors.Count() > 0)
                    {
                        foreach (var e in errors)
                        {
                            foreach (var errorMsg in e.errors)
                            {
                                ModelState.AddModelError(errorMsg.field, errorMsg.message);
                            }
                        }

                        return(Json(Models.Utils.ParseFormErrors(ModelState)));
                    }
                }
                var record = _hrUnitOfWork.Repository <DisplinPeriod>().FirstOrDefault(j => j.Id == model.Id);
                if (record == null) //Add
                {
                    record = new DisplinPeriod();
                    AutoMapper(new Models.AutoMapperParm
                    {
                        Destination = record,
                        Source      = model,
                        ObjectName  = "DisplinPeriods",
                        Options     = moreInfo,
                        Transtype   = TransType.Insert
                    });
                    record.CreatedTime = DateTime.Now;
                    record.CreatedUser = UserName;
                    _hrUnitOfWork.DisciplineRepository.Add(record);
                    if (model.submit == true)
                    {
                        // && model.Frequency != null && model.Times != null
                        if (model.TotalYear != null)
                        {
                            GetPeriodNO(model, record);
                        }
                        else
                        {
                            ModelState.AddModelError("TotalYear", MsgUtils.Instance.Trls("MustInsertTotalYear"));
                            return(Json(Models.Utils.ParseFormErrors(ModelState)));
                        }
                    }
                }
                else //update
                {
                    AutoMapper(new Models.AutoMapperParm
                    {
                        Destination = record,
                        Source      = model,
                        ObjectName  = "People",
                        Options     = moreInfo,
                        Transtype   = TransType.Update
                    });
                    record.ModifiedTime = DateTime.Now;
                    record.ModifiedUser = UserName;
                    _hrUnitOfWork.DisciplineRepository.Attach(record);
                    _hrUnitOfWork.DisciplineRepository.Entry(record).State = EntityState.Modified;
                    if (model.submit == true)
                    {
                        if (model.TotalYear != null)
                        {
                            GetPeriodNO(model, record);
                        }
                        else
                        {
                            ModelState.AddModelError("TotalYear", MsgUtils.Instance.Trls("MustInsertTotalYear"));
                            return(Json(Models.Utils.ParseFormErrors(ModelState)));
                        }
                    }
                }
                errors = SaveGrid1(grid1, ModelState.Where(a => a.Key.Contains("grid1")), record);
                if (errors.Count > 0)
                {
                    return(Json(errors.First().errors.First().message));
                }
                var    Errors  = SaveChanges(Language);
                string message = "OK," + record.Id;
                if (Errors.Count > 0)
                {
                    message = Errors.First().errors.First().message;
                }

                return(Json(message));
            }

            else
            {
                return(Json(Models.Utils.ParseFormErrors(ModelState)));
            }
        }
        private List <Error> SaveGrid1(RequestDisplinRangeGrid grid1, IEnumerable <KeyValuePair <string, ModelState> > state, DisplinPeriod Displinobj)
        {
            List <Error> errors = new List <Error>();

            // Deleted
            if (grid1.deleted != null)
            {
                foreach (DisplinRangeViewModel model in grid1.deleted)
                {
                    var RequestDisplinRange = new DisplinRange
                    {
                        Id = model.Id
                    };

                    _hrUnitOfWork.DisciplineRepository.Remove(RequestDisplinRange);
                }
            }
            // Exclude delete models from sever side validations
            if (ServerValidationEnabled)
            {
                var modified = Models.Utils.GetModifiedRows(state.Where(a => !a.Key.Contains("deleted")));
                if (modified.Count > 0)
                {
                    errors = _hrUnitOfWork.CompanyRepository.Check(new CheckParm
                    {
                        CompanyId  = CompanyId,
                        ObjectName = "DisplinRanges",
                        Columns    = Models.Utils.GetModifiedRows(state.Where(a => !a.Key.Contains("deleted"))),
                        Culture    = Language
                    });
                    if (errors.Count() > 0)
                    {
                        return(errors);
                    }
                }
            }

            // updated records
            if (grid1.updated != null)
            {
                foreach (DisplinRangeViewModel model in grid1.updated)
                {
                    var requestRange = new DisplinRange();
                    AutoMapper(new Models.AutoMapperParm {
                        Destination = requestRange, Source = model
                    });
                    requestRange.ModifiedTime = DateTime.Now;
                    requestRange.ModifiedUser = UserName;
                    _hrUnitOfWork.DisciplineRepository.Attach(requestRange);
                    _hrUnitOfWork.DisciplineRepository.Entry(requestRange).State = EntityState.Modified;
                }
            }

            // inserted records

            if (grid1.inserted != null)
            {
                foreach (DisplinRangeViewModel model in grid1.inserted)
                {
                    var requestRange = new DisplinRange();
                    AutoMapper(new Models.AutoMapperParm {
                        Destination = requestRange, Source = model
                    });
                    requestRange.DisplinPeriod = Displinobj;
                    requestRange.CreatedTime   = DateTime.Now;
                    requestRange.CreatedUser   = UserName;
                    _hrUnitOfWork.DisciplineRepository.Add(requestRange);
                }
            }

            return(errors);
        }
        private void GetPeriodNO(DisplinPeriodViewModel model, DisplinPeriod record)
        {
            var      period     = _hrUnitOfWork.Repository <DisPeriodNo>().Where(s => s.PeriodId == record.Id).Select(a => a.PeriodNo).LastOrDefault();
            int      day        = model.PeriodSDate.Day;
            int      modelYear  = model.PeriodSDate.Year;
            DateTime startTime  = model.PeriodSDate;
            DateTime endTime    = model.PeriodSDate;
            DateTime customName = model.PeriodSDate;
            //int year1 = 0;
            int year = 0;

            if (model.Frequency == 1)
            {
                year = 360;
            }
            else if (model.Frequency == 2)
            {
                year = 52;
            }
            else if (model.Frequency == 3)
            {
                year = 12;
            }
            else
            {
                year = 1;
            }
            int?Iteration = (model.TotalYear * year) / model.Times;

            for (int i = 0; i < Iteration; i++)
            {
                if (model.Frequency == 3)
                {
                    startTime = model.PeriodSDate.AddMonths(model.Times * i);
                    endTime   = model.PeriodSDate.AddMonths(model.Times * (i + 1)).AddDays(-1);
                    //int month = model.PeriodSDate.Month + i * model.Times;
                    //int Year = modelYear;
                    //if (month > 12)
                    //{

                    //    Year = modelYear + Convert.ToInt32(month/12);
                    //    year1 = Convert.ToInt32(month / 12);
                    //    month = month - (year1 * 12);
                    //}
                    //startTime = new DateTime(Year, month, day);
                    //endTime = startTime.AddMonths(model.Times).AddDays(-1);
                }
                else if (model.Frequency == 4)
                {
                    startTime = model.PeriodSDate.AddYears(model.Times * i);
                    endTime   = model.PeriodSDate.AddYears(model.Times * (i + 1)).AddDays(-1);
                }
                else if (model.Frequency == 1)
                {
                    startTime = model.PeriodSDate.AddDays(model.Times * i);
                    endTime   = model.PeriodSDate.AddDays(model.Times * (i + 1)).AddDays(-1);
                }
                else
                {
                    int Calcweak = model.Times * 7;
                    startTime = model.PeriodSDate.AddDays(Calcweak * i);
                    endTime   = model.PeriodSDate.AddDays(Calcweak * (i + 1)).AddDays(-1);
                }

                DisPeriodNo periodNo = new DisPeriodNo()
                {
                    PeriodNo    = ++period,
                    CreatedUser = UserName,
                    Posted      = false,
                    PostDate    = DateTime.Now,
                    CreatedTime = DateTime.Now,
                    Name        = startTime.ToString("MM/yyyy"),
                    PeriodEDate = endTime,
                    PeriodSDate = startTime,
                    PeriodId    = record.Id
                };

                _hrUnitOfWork.DisciplineRepository.Add(periodNo);
            }
        }