private static Calendar getEntityByModel(CalendarViewModel model)
        {
            if (model == null) return null;

            Calendar entity = new Calendar();

            if (model.Id == 0)
            {
                entity.CreateBy = AuthenticationHelper.UserId;
                entity.CreateDate = DateTime.Now;
                entity.CompanyId = AuthenticationHelper.CompanyId.Value;
            }
            else
            {
                entity.CreateBy = model.CreateBy;
                entity.CreateDate = model.CreateDate;
                entity.CompanyId = model.CompanyId;
            }

            entity.Adjusting = model.Adjusting;
            entity.ClosingStatus = model.ClosingStatus;
            entity.EndDate = model.EndDate;
            entity.Id = model.Id;
            entity.PeriodName = model.PeriodName;
            entity.PeriodQuarter = model.PeriodQuarter;
            entity.PeriodYear = model.PeriodYear;
            entity.SeqNumber = model.SeqNumber;
            entity.SOBId = model.SOBId;
            entity.StartDate = model.StartDate;
            entity.UpdateDate = DateTime.Now;
            entity.UpdateBy = AuthenticationHelper.UserId;
            return entity;
        }
 public static string SaveCalendar(CalendarViewModel model)
 {
     if (model.Id > 0)
     {
         return service.Update(getEntityByModel(model));
     }
     else
     {
         return service.Insert(getEntityByModel(model));
     }
 }
        public ActionResult ChangeStatus(CalendarViewModel model)
        {
            if (ModelState.IsValid)
            {
                CalendarViewModel calendar = CalendarHelper.GetCalendar(model.Id.ToString());
                calendar.ClosingStatus = model.ClosingStatus;
                string result = CalendarHelper.SaveCalendar(calendar);
                return RedirectToAction("Index");
            }

            return View(model);
        }
        public ActionResult Edit(CalendarViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.SOBId = SessionHelper.SOBId;
                if (model.StartDate != null && model.EndDate < model.StartDate)
                {
                    ModelState.AddModelError("Error", "Start date is less than End Date!");
                }
                else
                {
                    string result = CalendarHelper.SaveCalendar(model);
                    return RedirectToAction("Index");
                }
            }

            return View(model);
        }
        public ActionResult Create(CalendarViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.SOBId = SessionHelper.SOBId;
                if (SessionHelper.Calendar != null && model.StartDate < SessionHelper.Calendar.EndDate)
                {
                    ModelState.AddModelError("Error", "Start date is overlaping with previous period!");
                }
                else if (model.StartDate != null && model.EndDate < model.StartDate)
                {
                    ModelState.AddModelError("Error", "Start date is less than End Date!");
                }
                else if (model.StartDate > model.EndDate)
                {
                    ModelState.AddModelError("Error", "Invalid Dates!");
                }
                else
                {
                    ////Not sure about the validity of this code,
                    ////To be checked later.
                    //////////////////////////////////////////////////
                    ////Calendar duplicateRecord = service.getCalendarByPeriod(AuthenticationHelper.CompanyId.Value, model.SOBId, model.StartDate, model.EndDate);
                    //////////////////////////////////////////////////
                    ////if (duplicateRecord == null)
                    ////{
                    model.ClosingStatus = "Open";
                    string result = CalendarHelper.SaveCalendar(model);    ////TODO: mapper should be in service
                    return RedirectToAction("Index");
                    ////}
                    ////else
                    ////{
                    ////    ModelState.AddModelError("Error", "Period Already Exists!");
                    ////}
                }
            }

            return View(model);
        }
        public ActionResult Create()
        {
            CalendarViewModel previousCalendar = CalendarHelper.GetPreviousCalendar(SessionHelper.SOBId, Const.CurrentDate.Year);

            CalendarViewModel model = new CalendarViewModel();

            if (previousCalendar != null)
            {
                model.StartDate = SessionHelper.Calendar.EndDate.AddDays(1);
                model.EndDate = model.StartDate.AddDays(1);
            }

            model.SOBId = SessionHelper.SOBId;            
            return View(model);
        }