377 lines
11 KiB
C#
377 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Data.Entity;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Web.Mvc;
|
|
using EnVisage.App_Start;
|
|
using EnVisage.Code;
|
|
using EnVisage.Code.BLL;
|
|
using EnVisage.Models;
|
|
using jQuery.DataTables.Mvc;
|
|
using EnVisage.Code.HtmlHelpers;
|
|
using Microsoft.AspNet.Identity;
|
|
|
|
namespace EnVisage.Controllers
|
|
{
|
|
[Authorize]
|
|
public class CalendarController : BaseController
|
|
{
|
|
#region General
|
|
|
|
/// <summary>
|
|
/// GET: /Calendar/
|
|
/// </summary>
|
|
/// <returns>Calendar view</returns>
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Read)]
|
|
public ActionResult Index()
|
|
{
|
|
try
|
|
{
|
|
if (!SecurityManager.CheckSecurityObjectPermission(Areas.FiscalCalendar, AccessLevel.Read))
|
|
return Redirect("/");
|
|
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
var model = manager.GetActiveCalendarSettingsItem(DateTime.Today);
|
|
|
|
if (model == null)
|
|
model = new FiscalCalendarModel();
|
|
|
|
return View(model);
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
SetErrorScript(message: blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
SetErrorScript();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
SetErrorScript();
|
|
}
|
|
|
|
return View(new FiscalCalendarModel());
|
|
}
|
|
|
|
#endregion
|
|
#region Fiscal Calendar
|
|
|
|
// GET: /Calendar/EditCalendar/5
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult EditCalendar(Guid? id)
|
|
{
|
|
var model = new FiscalCalendarModel();
|
|
try
|
|
{
|
|
if ((id != null) && (id != Guid.Empty))
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
model = manager.GetCalendarSettingsItem(id.Value);
|
|
|
|
if (model == null)
|
|
return HttpNotFound();
|
|
}
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
SetErrorScript(message: blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
SetErrorScript();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
SetErrorScript();
|
|
}
|
|
|
|
// Set defaults for new item
|
|
model.EffectiveChangeDate = DateTime.Today;
|
|
//model.StartingPoint = null; // SA. Uncomment to set First Calendar Date empty in Edit Calendar Form
|
|
return PartialView("_editCalendar", model);
|
|
}
|
|
|
|
// POST: /Calendar/EditCalendar/5
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult EditCalendar(FiscalCalendarModel model)
|
|
{
|
|
bool isNew = (model.Id == null) || (model.Id == Guid.Empty);
|
|
|
|
if (!isNew)
|
|
if (ContentLocker.IsLock("FiscalCalendarSettings", model.Id.ToString(), User.Identity.Name))
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
model.TrimStringProperties();
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
manager.SaveFiscalCalendarSettings(model, DateTime.Today);
|
|
DbContext.SaveChanges();
|
|
|
|
if (!isNew)
|
|
ContentLocker.RemoveLock("FiscalCalendarSettings", model.Id.ToString(), User.Identity.Name);
|
|
|
|
return PartialView("_editCalendar", model);
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
ModelState.AddModelError(string.Empty, blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
ModelState.AddModelError(string.Empty, "Cannot save calendar settings. Try again later.");
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
ModelState.AddModelError(string.Empty, "Cannot save calendar settings. Try again later.");
|
|
}
|
|
}
|
|
|
|
// return empty model with validation messages (if any)
|
|
HttpContext.Response.Clear();
|
|
HttpContext.Response.StatusCode = 500;
|
|
return PartialView("_editCalendar", model);
|
|
}
|
|
|
|
// GET: /Calendar/ViewCalendarHistory/
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Read)]
|
|
public ActionResult ViewCalendarHistory()
|
|
{
|
|
FiscalCalendarManager mngr = new FiscalCalendarManager(DbContext);
|
|
FiscalCalendarListModel model = mngr.GetCalendarSettingsItems(DateTime.Today);
|
|
return PartialView("_viewCalendarHistory", model);
|
|
}
|
|
|
|
#endregion
|
|
#region Holidays
|
|
|
|
/// <summary>
|
|
/// Returns JSON company list with filters and sort for jQuery DataTables
|
|
/// </summary>
|
|
[HttpPost]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Read)]
|
|
public JsonResult GetHolidayItems(JQueryDataTablesModel jQueryDataTablesModel)
|
|
{
|
|
int totalRecordCount;
|
|
int searchRecordCount;
|
|
|
|
var clients = GetHolidays(startIndex: jQueryDataTablesModel.iDisplayStart,
|
|
pageSize: jQueryDataTablesModel.iDisplayLength,
|
|
sortedColumns: jQueryDataTablesModel.GetSortedColumns(),
|
|
totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount,
|
|
searchString: jQueryDataTablesModel.sSearch);
|
|
|
|
return this.DataTablesJson(items: clients,
|
|
totalRecords: totalRecordCount,
|
|
totalDisplayRecords: searchRecordCount,
|
|
sEcho: jQueryDataTablesModel.sEcho);
|
|
}
|
|
|
|
|
|
private IEnumerable<object> GetHolidays(int startIndex,
|
|
int pageSize,
|
|
IEnumerable<SortedColumn> sortedColumns,
|
|
out int totalRecordCount,
|
|
out int searchRecordCount,
|
|
string searchString)
|
|
{
|
|
FiscalCalendarManager mngr = new FiscalCalendarManager(DbContext);
|
|
totalRecordCount = mngr.GetHolidaysCount();
|
|
var query = mngr.GetHolidays(DateTime.Today);
|
|
|
|
// Add filter
|
|
if (!string.IsNullOrWhiteSpace(searchString))
|
|
{
|
|
query = query.Where(c => c.Name.ToLower().Contains(searchString.ToLower()));
|
|
}
|
|
|
|
// Add sorting
|
|
foreach (var sortedColumn in sortedColumns)
|
|
{
|
|
switch (sortedColumn.PropertyName)
|
|
{
|
|
case "HolidayId":
|
|
query = sortedColumn.Direction == SortingDirection.Ascending
|
|
? query.OrderBy(c => c.HolidayId)
|
|
: query.OrderByDescending(c => c.HolidayId);
|
|
break;
|
|
default:
|
|
query = sortedColumn.Direction == SortingDirection.Ascending
|
|
? query.OrderBy(c => c.Name)
|
|
: query.OrderByDescending(c => c.Name);
|
|
break;
|
|
}
|
|
}
|
|
|
|
List<HolidayDisplayModel> list = query.Skip(startIndex).Take(pageSize).ToList();
|
|
searchRecordCount = query.Count();
|
|
return list;
|
|
}
|
|
|
|
|
|
// GET: /Calendar/EditCalendar/5
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult CreateHoliday()
|
|
{
|
|
var model = new HolidayModel();
|
|
model.EffectiveChangeDate = DateTime.Today;
|
|
return PartialView("_editHoliday", model);
|
|
}
|
|
|
|
// GET: /Calendar/EditHoliday/5
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult EditHoliday(Guid id)
|
|
{
|
|
var model = new HolidayModel();
|
|
try
|
|
{
|
|
if ((id != null) && (id != Guid.Empty))
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
model = manager.GetActiveHolidayItem(id, DateTime.Today);
|
|
|
|
if (model == null)
|
|
return HttpNotFound();
|
|
}
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
SetErrorScript(message: blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
SetErrorScript();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
SetErrorScript();
|
|
}
|
|
|
|
// Set defaults for new item
|
|
model.EffectiveChangeDate = DateTime.Today;
|
|
model.Id = Guid.Empty;
|
|
return PartialView("_editHoliday", model);
|
|
}
|
|
|
|
// POST: /Calendar/EditHoliday/5
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult EditHoliday(HolidayModel model)
|
|
{
|
|
bool isNew = model.HolidayId.Equals(Guid.Empty);
|
|
|
|
if (!isNew)
|
|
if (ContentLocker.IsLock("Holiday", model.HolidayId.ToString(), User.Identity.Name))
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
model.TrimStringProperties();
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
manager.SaveHoliday(model, DateTime.Today);
|
|
DbContext.SaveChanges();
|
|
|
|
if (!isNew)
|
|
ContentLocker.RemoveLock("Holiday", model.HolidayId.ToString(), User.Identity.Name);
|
|
|
|
return PartialView("_editHoliday", model);
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
ModelState.AddModelError(string.Empty, blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
ModelState.AddModelError(string.Empty, "Cannot save holiday. Try again later.");
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
ModelState.AddModelError(string.Empty, "Cannot save holiday. Try again later.");
|
|
}
|
|
}
|
|
|
|
// return empty model with validation messages (if any)
|
|
HttpContext.Response.Clear();
|
|
HttpContext.Response.StatusCode = 500;
|
|
return PartialView("_editHoliday", model);
|
|
}
|
|
|
|
// GET: /Calendar/ViewHolidayHistory/5
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Read)]
|
|
public ActionResult ViewHolidayHistory(Guid id)
|
|
{
|
|
FiscalCalendarManager mngr = new FiscalCalendarManager(DbContext);
|
|
HolidayListModel model = mngr.GetHolidayHistoryItems(id, DateTime.Today);
|
|
return PartialView("_viewHolidayHistory", model);
|
|
}
|
|
|
|
// POST: /Calendar/DeleteHoliday/5
|
|
[HttpPost]
|
|
[AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult DeleteHoliday(Guid id)
|
|
{
|
|
try
|
|
{
|
|
FiscalCalendarManager mngr = new FiscalCalendarManager(DbContext);
|
|
mngr.DeleteHoliday(id);
|
|
DbContext.SaveChanges();
|
|
return new HttpStatusCodeResult(HttpStatusCode.OK);
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
SetErrorScript(message: blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
SetErrorScript();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
SetErrorScript();
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|