433 lines
15 KiB
C#
433 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
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.Validation;
|
|
using Resources;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EnVisage.Controllers
|
|
{
|
|
[Authorize]
|
|
public class CalendarController : BaseController
|
|
{
|
|
#region General
|
|
|
|
/// <summary>
|
|
/// GET: /Calendar/
|
|
/// </summary>
|
|
/// <returns>Calendar view</returns>
|
|
[HttpGet]
|
|
[AreaSecurity(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) ?? 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]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult EditCalendar(Guid? id)
|
|
{
|
|
try
|
|
{
|
|
var isNew = !(id.HasValue && id.Value != Guid.Empty);
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
var model = isNew ? new FiscalCalendarModel() : manager.GetCalendarSettingsItem(id);
|
|
|
|
if (model == null)
|
|
return HttpNotFound();
|
|
|
|
model.EffectiveChangeDate = DateTime.Today;
|
|
return PartialView("_editCalendar", model);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
// POST: /Calendar/EditCalendar/5
|
|
[HttpPost]
|
|
[ValidateAjax]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult EditCalendar(FiscalCalendarModel model)
|
|
{
|
|
bool isNew = model.Id == Guid.Empty;
|
|
|
|
if (!isNew && ContentLocker.IsLock("FiscalCalendarSettings", model.Id.ToString(), User.Identity.GetUserName()))
|
|
{
|
|
ModelState.AddModelError(string.Empty, Messages.Calendar_Edit_BeingUpdatedByAnotherUser);
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
|
|
model.TrimStringProperties();
|
|
|
|
try
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
manager.SaveFiscalCalendarSettings(model, DateTime.Today);
|
|
DbContext.SaveChanges();
|
|
|
|
if (!isNew)
|
|
ContentLocker.RemoveLock("FiscalCalendarSettings", model.Id.ToString(), User.Identity.GetUserName());
|
|
|
|
return new SuccessJsonResult();
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
ModelState.AddModelError(string.Empty, blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
ModelState.AddModelError(string.Empty, Messages.Calendar_Edit_Error);
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
ModelState.AddModelError(string.Empty, Messages.Calendar_Edit_Error);
|
|
}
|
|
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
|
|
// GET: /Calendar/ViewCalendarHistory/
|
|
[HttpGet]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Read)]
|
|
public ActionResult ViewCalendarHistory()
|
|
{
|
|
try
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
var model = manager.GetCalendarSettingsItems(DateTime.Today);
|
|
|
|
return PartialView("_viewCalendarHistory", model);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateJsonAntiForgeryToken]
|
|
public ActionResult GetAvailableDateRange()
|
|
{
|
|
try
|
|
{
|
|
return Json(new
|
|
{
|
|
MinDate = Constants.MIN_SELECTABLE_DATE,
|
|
MaxDate = Constants.MAX_SELECTABLE_DATE,
|
|
}, JsonRequestBehavior.AllowGet);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Holidays
|
|
|
|
/// <summary>
|
|
/// Returns JSON company list with filters and sort for jQuery DataTables
|
|
/// </summary>
|
|
[HttpPost]
|
|
[AreaSecurity(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);
|
|
}
|
|
|
|
[HttpPost]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Read)]
|
|
public JsonResult GetResources()
|
|
{
|
|
return Json(Newtonsoft.Json.JsonConvert.SerializeObject(Utils.GetTeamResorces(User.Identity.GetID())));
|
|
}
|
|
|
|
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 "HolidayGroupId":
|
|
query = sortedColumn.Direction == SortingDirection.Ascending
|
|
? query.OrderBy(c => c.HolidayGroupId)
|
|
: query.OrderByDescending(c => c.HolidayGroupId);
|
|
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]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult CreateHoliday()
|
|
{
|
|
try
|
|
{
|
|
var model = new HolidayModel
|
|
{
|
|
EffectiveChangeDate = DateTime.Today
|
|
};
|
|
return PartialView("_editHoliday", model);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
// GET: /Calendar/EditHoliday/5
|
|
[HttpGet]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult EditHoliday(Guid id)
|
|
{
|
|
try
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
var model = manager.GetActiveHolidayItem(id, DateTime.Today);
|
|
|
|
if (model == null)
|
|
return HttpNotFound();
|
|
|
|
model.EffectiveChangeDate = DateTime.Today;
|
|
return PartialView("_editHoliday", model);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
// POST: /Calendar/EditHoliday/5
|
|
[HttpPost]
|
|
[ValidateAjax]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult EditHoliday(HolidayModel model)
|
|
{
|
|
#if DEBUG
|
|
var watch1 = new System.Diagnostics.Stopwatch();
|
|
watch1.Start();
|
|
Logger.Debug("Start EditHoliday");
|
|
#endif
|
|
var isNew = model.HolidayGroupId.Equals(Guid.Empty);
|
|
|
|
if (!isNew && ContentLocker.IsLock("Holiday", model.HolidayGroupId.ToString(), User.Identity.GetUserName()))
|
|
{
|
|
ModelState.AddModelError(string.Empty, Messages.Holiday_Edit_BeingUpdatedByAnotherUser);
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
|
|
model.TrimStringProperties();
|
|
|
|
if (!model.MultipleDaysHoliday)
|
|
model.EndDate = null;
|
|
|
|
using (var transaction = DbContext.Database.BeginTransaction())
|
|
{
|
|
var transactionId = DbContext.GetClientConnectionId();
|
|
var userId = User.Identity.GetID();
|
|
|
|
try
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
|
|
#if DEBUG
|
|
watch1.Stop();
|
|
System.Diagnostics.Debug.WriteLine($"EditHoliday SaveHoliday has taken {watch1.ElapsedMilliseconds} ms");
|
|
Logger.Debug($"EditHoliday SaveHoliday {watch1.ElapsedMilliseconds} ms");
|
|
watch1 = new System.Diagnostics.Stopwatch();
|
|
watch1.Start();
|
|
#endif
|
|
|
|
manager.SaveHoliday(model, DateTime.Today);
|
|
|
|
#if DEBUG
|
|
watch1.Stop();
|
|
System.Diagnostics.Debug.WriteLine($"EditHoliday DbContext.SaveChanges(); has taken {watch1.ElapsedMilliseconds} ms");
|
|
Logger.Debug($"EditHoliday DbContext.SaveChanges(); {watch1.ElapsedMilliseconds} ms");
|
|
watch1 = new System.Diagnostics.Stopwatch();
|
|
watch1.Start();
|
|
#endif
|
|
|
|
DbContext.SaveChanges();
|
|
|
|
transaction.Commit();
|
|
|
|
Task.Run(() => AuditProxy.CommitHistoryChanges(transactionId, userId));
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
ModelState.AddModelError(string.Empty, blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
ModelState.AddModelError(string.Empty, Messages.Holiday_Edit_Error);
|
|
}
|
|
|
|
transaction.Rollback();
|
|
AuditProxy.ClearHistoryChanges(transactionId);
|
|
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
ModelState.AddModelError(string.Empty, Messages.Holiday_Edit_Error);
|
|
transaction.Rollback();
|
|
AuditProxy.ClearHistoryChanges(transactionId);
|
|
|
|
return new FailedJsonResult(ModelState);
|
|
}
|
|
}
|
|
|
|
if (!isNew)
|
|
ContentLocker.RemoveLock("Holiday", model.HolidayGroupId.ToString(), User.Identity.GetUserName());
|
|
|
|
#if DEBUG
|
|
watch1.Stop();
|
|
System.Diagnostics.Debug.WriteLine($"EditHoliday All has taken {watch1.ElapsedMilliseconds} ms");
|
|
Logger.Debug($"EditHoliday All {watch1.ElapsedMilliseconds} ms");
|
|
#endif
|
|
|
|
return new SuccessJsonResult();
|
|
}
|
|
|
|
// GET: /Calendar/ViewHolidayHistory/5
|
|
[HttpGet]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Read)]
|
|
public ActionResult ViewHolidayHistory(Guid id)
|
|
{
|
|
try
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
var model = manager.GetHolidayHistoryItems(id, DateTime.Today);
|
|
|
|
return PartialView("_viewHolidayHistory", model);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
// POST: /Calendar/DeleteHoliday/5
|
|
[HttpPost]
|
|
[AreaSecurity(area = Areas.FiscalCalendar, level = AccessLevel.Write)]
|
|
public ActionResult DeleteHoliday(Guid id)
|
|
{
|
|
try
|
|
{
|
|
var manager = new FiscalCalendarManager(DbContext);
|
|
manager.DeleteHoliday(id);
|
|
DbContext.SaveChanges();
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.OK);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |