using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using EnVisage.Code;
using jQuery.DataTables.Mvc;
using EnVisage.Models;
using EnVisage.Code.BLL;
using EnVisage.App_Start;
using EnVisage.Code.Validation;
using Resources;
namespace EnVisage.Controllers
{
public class ExpenditureCategoryController : BaseController
{
#region Actions
///
/// GET: /ExpenditureCategory/
///
/// Empty view
[HttpGet]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Read)]
public ActionResult Index()
{
var model = new ExpenditureCategoriesListModel();
var uoms = this.GetUnitsOfMeasure();
model.UnitsOfMeasure = uoms;
return View(model);
}
///
/// Returns JSON Expenditure Category list with filters and sort for jQuery DataTables
///
[HttpPost]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Read)]
public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel)
{
int totalRecordCount;
int searchRecordCount;
if (string.IsNullOrEmpty(jQueryDataTablesModel.sSearch_[2]) && !string.IsNullOrEmpty(Request.QueryString["UOM"]))
{
var search_ = new ReadOnlyCollection(new List() { string.Empty, string.Empty,
Request.QueryString["UOM"], string.Empty, string.Empty, string.Empty, string.Empty,string.Empty});
var clients = GetExpenditureCategories(startIndex: jQueryDataTablesModel.iDisplayStart,
pageSize: jQueryDataTablesModel.iDisplayLength, sortedColumns: jQueryDataTablesModel.GetSortedColumns(),
totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount, searchString: jQueryDataTablesModel.sSearch,
filters: search_);
return this.DataTablesJson(items: clients,
totalRecords: totalRecordCount,
totalDisplayRecords: searchRecordCount,
sEcho: jQueryDataTablesModel.sEcho,
sSearch_: search_
);
}
else
{
var clients = GetExpenditureCategories(startIndex: jQueryDataTablesModel.iDisplayStart,
pageSize: jQueryDataTablesModel.iDisplayLength, sortedColumns: jQueryDataTablesModel.GetSortedColumns(),
totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount, searchString: jQueryDataTablesModel.sSearch,
filters: jQueryDataTablesModel.sSearch_);
return this.DataTablesJson(items: clients,
totalRecords: totalRecordCount,
totalDisplayRecords: searchRecordCount,
sEcho: jQueryDataTablesModel.sEcho);
}
}
// GET: /ExpenditureCategory/Details/5
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Read)]
public ActionResult Details(Guid? id)
{
var expenditureCategory = new ExpenditureCategoryModel();
try
{
var manager = new ExpenditureCategoryManager(DbContext);
expenditureCategory = (ExpenditureCategoryModel)manager.Load(id) ?? new ExpenditureCategoryModel();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
return PartialView("_details", expenditureCategory);
}
// GET: /ExpenditureCategory/Edit/5
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult Edit(Guid? id)
{
ViewBag.ExpenditureCategoryId = id.ToString();
var expenditureManager = new ExpenditureManager(DbContext);
ViewBag.Expenditures = expenditureManager.GetExpenditures();
var model = new ExpenditureCategoryModel();
try
{
var manager = new ExpenditureCategoryManager(DbContext);
ExpenditureCategory catItem = manager.Load(id);
model = (ExpenditureCategoryModel)catItem ?? new ExpenditureCategoryModel();
model.Name = catItem.Name; // SA. ENV-839. Restore the original EC name (the prev line sets EC name with Cost Center)
model.CalculatesCategories = new Expenditure2ExpendituresModel(model.Expenditure2Expenditure.OrderBy(x => x.ProcessOrder).ToList());
model.Fees = new FeesModel(model.FeeCalculation);
if (model.Id == Guid.Empty)
model.AllowResourceAssignment = true;
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult Edit(ExpenditureCategoryModel model)
{
if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.Id.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
model.TrimStringProperties();
var expenditureManager = new ExpenditureManager(DbContext);
ViewBag.Expenditures = expenditureManager.GetExpenditures();
if (ModelState.IsValid)
{
try
{
var manager = new ExpenditureCategoryManager(DbContext);
if (model.Id != Guid.Empty)
{
ExpenditureCategory expItem = manager.Load(model.Id, false);
var oldExp = (ExpenditureCategoryModel)expItem;
if (expItem != null)
oldExp.Name = expItem.Name; // SA. ENV-839. Restore the original EC name (prev line sets EC Name with COst Center)
if (model.ResourceCount > 0 && !model.AllowResourceAssignment)
model.AllowResourceAssignment = true;
if (model.UOMId != oldExp.UOMId)
{
var scmanager = new ScenarioManager(DbContext);
Logger.Debug("/ExpenditureCategory/Edit method (post).");
scmanager.RecalculateCapacityScenariosUOMChanged(model, oldExp);
}
}
manager.Save(model);
DbContext.SaveChanges();
ContentLocker.RemoveLock("ExpenditureCategory", model.Id.ToString(), User.Identity.GetUserName());
return RedirectToAction("Index");
}
catch (BLLException blEx) // handle any system specific error
{
// display error message if required
if (blEx.DisplayError)
ModelState.AddModelError(string.Empty, blEx.Message);
else // if display not requried then display modal form with general error message
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception) // handle any unexpected error
{
LogException(exception);
SetErrorScript();
}
}
// return empty model with validation messages (if any)
return View(model);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult EditRate(Guid? id, Guid? expenditureCategoryId)
{
var manager = new RateManager(DbContext);
var model = (RateModel)manager.Load(id, false) ?? new RateModel();
if (expenditureCategoryId.HasValue && (id == null || id == Guid.Empty))
{
//please check
var lRates = DbContext.Rates.Where(c =>
c.ExpenditureCategoryId == expenditureCategoryId.Value &&
c.Type == (int)RateModel.RateType.Global).ToList();
var maxEndDate = new DateTime();
var endDates = lRates.Select(d => d.EndDate).ToList();
if (endDates.Any())
maxEndDate = endDates.Max();
if (lRates.Count == 0 || maxEndDate < DateTime.MaxValue.AddYears(-4))
{
model.StartDate = lRates.Count == 0 ? DateTime.Today : maxEndDate.AddDays(1);
model.EndDate = model.StartDate.AddYears(5).AddDays(-1);
}
else
{
model.StartDate = maxEndDate;
model.EndDate = model.StartDate;
}
}
if ((model.ParentId == Guid.Empty || model.ParentId == null) && expenditureCategoryId.HasValue)
model.ParentId = expenditureCategoryId.Value;
if (model.ExpenditureCategoryId == Guid.Empty && expenditureCategoryId.HasValue)
model.ExpenditureCategoryId = expenditureCategoryId.Value;
return PartialView("_editRate", model);
}
[HttpPost]
[ValidateAjax]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult EditRate(RateModel model)
{
#if DEBUG
var watch1 = new System.Diagnostics.Stopwatch();
watch1.Start();
Logger.Debug("Start EditRate");
#endif
if (model != null && model.Id != Guid.Empty && ContentLocker.IsLock("Rate", model.Id.ToString(), User.Identity.GetUserName()))
{
ModelState.AddModelError(string.Empty, Messages.Rate_Edit_Being_UpdatedByAnotherUser);
return new FailedJsonResult(ModelState);
}
try
{
if (model == null)
throw new ArgumentNullException(nameof(model));
model.TrimStringProperties();
var userid = Guid.Parse(HttpContext.User.Identity.GetID());
var ecManager = new ExpenditureCategoryManager(DbContext);
using (var rateManager = new RateManager(DbContext))
{
rateManager.Save(model, userid);
DbContext.SaveChanges();
}
var expenditureCategory = (ExpenditureCategoryModel)ecManager.Load(model.ExpenditureCategoryId) ?? new ExpenditureCategoryModel();
ViewBag.ExpenditureCategoryId = model.ExpenditureCategoryId;
#if DEBUG
watch1.Stop();
System.Diagnostics.Debug.WriteLine($"EditRate All has taken {watch1.ElapsedMilliseconds} ms");
Logger.Debug($"EditRate All {watch1.ElapsedMilliseconds} ms");
#endif
return new PartialViewJsonResult(true, null, "_ratesContainer", expenditureCategory.Rates, ControllerContext, ViewData, TempData);
}
catch (BLLException blEx) // handle any system specific error
{
// display error message if required
if (blEx.DisplayError)
ModelState.AddModelError(string.Empty, blEx.Message);
else // if display not requried then display modal form with general error message
{
LogException(blEx);
ModelState.AddModelError(string.Empty, Messages.Rate_Save_Error);
}
}
catch (Exception exception) // handle any unexpected error
{
LogException(exception);
ModelState.AddModelError(string.Empty, Messages.Rate_Save_Error);
}
return new FailedJsonResult(ModelState);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult LoadRate(Guid? editRateId, Guid expenditureCategoryId, Guid? deleteRateId)
{
ViewBag.ExpenditureCategoryId = expenditureCategoryId.ToString();
var expenditureCategory = new ExpenditureCategoryModel();
try
{
var manager = new ExpenditureCategoryManager(DbContext);
expenditureCategory = (ExpenditureCategoryModel)manager.Load(expenditureCategoryId) ?? new ExpenditureCategoryModel();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
var model = new RatesModel { GlobalRates = expenditureCategory.Rates.GlobalRates };
if (deleteRateId.HasValue)
{
try
{
var manager = new RateManager(DbContext);
model.EditRate = (RateModel)manager.Load(deleteRateId) ?? new RateModel();
model.DeleteRateId = deleteRateId;
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
}
else
{
model.EditRate = new RateModel();
if (!editRateId.HasValue || Guid.Empty.Equals(editRateId.Value))
{
model.EditRate = new RateModel
{
ExpenditureCategoryId = expenditureCategoryId,
Type = 0,
Rate1 = 1,
StartDate = DateTime.Now,
EndDate = DateTime.Now,
DerivedObjectId = Guid.Empty,
FreezeRate = 0,
Id = Guid.Empty
};
return PartialView("_ratesContainer", model);
}
try
{
var manager = new RateManager(DbContext);
model.EditRate = (RateModel)manager.Load(editRateId) ?? new RateModel();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
}
return PartialView("_ratesContainer", model);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult DeleteRate(Guid? rateId)
{
if (rateId == null || rateId == Guid.Empty)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
try
{
var manager = new RateManager(DbContext);
var model = (RateModel)manager.Load(rateId);
if (model == null)
return HttpNotFound();
return PartialView("_deleteRate", model);
}
catch (Exception exception)
{
LogException(exception);
}
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}
[HttpPost]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult DeleteRate(RateModel model)
{
if (model != null && model.ExpenditureCategoryId != Guid.Empty && ContentLocker.IsLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
try
{
if (model != null)
{
var manager = new RateManager(DbContext);
var ecManager = new ExpenditureCategoryManager(DbContext);
var dbObj = manager.Load(model.Id, false);
if (dbObj == null)
throw new InvalidOperationException(
string.Format("Cannot delete rate {0} because it does not exist", model.Id));
var userid = Guid.Parse(HttpContext.User.Identity.GetID());
manager.Delete(dbObj, userid);
var expenditureCategory = (ExpenditureCategoryModel)ecManager.Load(model.ExpenditureCategoryId) ??
new ExpenditureCategoryModel();
ViewBag.ExpenditureCategoryId = model.ExpenditureCategoryId;
return new PartialViewJsonResult(true, null, "_ratesContainer", expenditureCategory.Rates,
ControllerContext, ViewData, TempData);
}
}
catch (Exception exception)
{
LogException(exception);
}
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult EditFeeCalculation(Guid? id, Guid expenditureCategoryId)
{
var manager = new FeeCalculationManager(DbContext);
var model = manager.Load(id, false) ?? new FeeCalculation();
return PartialView("_editFeeCalculation", model);
}
[HttpPost]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult EditFeeCalculation(FeeCalculation model)
{
if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
model.TrimStringProperties();
try
{
if (ModelState.IsValid)
{
var manager = new FeeCalculationManager(DbContext);
manager.Save(
new FeeCalculationModel()
{
MaxShot = model.MaxShot,
MinShot = model.MinShot,
Quantity = model.Quantity,
ExpenditureCategoryId = model.ExpenditureCategoryId,
Id = model.Id
});
DbContext.SaveChanges();
//ContentLocker.RemoveLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.GetUserName());
return LoadFeeCalculationList(model.ExpenditureCategoryId, null);
}
ModelState.AddModelError(string.Empty, Constants.ERROR_GENERAL_MESSAGE_TEMPLATE);
}
catch (BLLException blEx) // handle any system specific error
{
// display error message if required
if (blEx.DisplayError)
ModelState.AddModelError(string.Empty, blEx.Message);
else // if display not requried then display modal form with general error message
{
LogException(blEx);
SetErrorScript();
ModelState.AddModelError(string.Empty, blEx.Message);
}
}
catch (Exception exception) // handle any unexpected error
{
LogException(exception);
SetErrorScript();
ModelState.AddModelError(string.Empty, exception.Message);
}
// TODO: check and fix client side code to have support of this kind of response when current functionality is enabled again
return new FailedJsonResult(ModelState);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult LoadFeeCalculation(Guid? editFeeId, Guid expenditureCategoryId, Guid? deleteFeeId)
{
ViewBag.ExpenditureCategoryId = expenditureCategoryId.ToString();
var expenditureCategory = new ExpenditureCategoryModel();
try
{
var manager = new ExpenditureCategoryManager(DbContext);
expenditureCategory = (ExpenditureCategoryModel)manager.Load(expenditureCategoryId) ?? new ExpenditureCategoryModel();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
var model = new FeesModel(expenditureCategory.FeeCalculation.ToList());
if (deleteFeeId.HasValue)
{
try
{
var manager = new FeeCalculationManager(DbContext);
model.EditFee = manager.Load(deleteFeeId) ?? new FeeCalculation();
model.DeleteFeeId = deleteFeeId;
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
}
else
{
model.EditFee = new FeeCalculation();
if (!editFeeId.HasValue || Guid.Empty.Equals(editFeeId.Value))
{
model.EditFee = new FeeCalculation
{
ExpenditureCategoryId = expenditureCategoryId,
MinShot = 0,
MaxShot = 0,
Quantity = 0,
Id = Guid.Empty
};
return PartialView("_feesContainer", model);
}
try
{
var manager = new FeeCalculationManager(DbContext);
model.EditFee = manager.Load(editFeeId) ?? new FeeCalculation();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
}
return PartialView("_feesContainer", model);
}
[AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Read)]
public ActionResult LoadFeeCalculationList(Guid? expenditureCategoryId)
{
return LoadFeeCalculationList(expenditureCategoryId, null);
}
[HttpGet]
[AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult DeleteFeeCalculation(Guid? feeCalculationId)
{
if (feeCalculationId == null || feeCalculationId == Guid.Empty)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var model = new FeeCalculation();
try
{
var manager = new FeeCalculationManager(DbContext);
model = manager.Load(feeCalculationId);
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();
}
return PartialView("_deleteFeeCalculation", model);
}
[HttpPost]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult DeleteFeeCalculation(FeeCalculation model)
{
if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var manager = new FeeCalculationManager(DbContext);
var dbObj = manager.Load(model.Id, false);
if (dbObj == null)
return HttpNotFound();
DbContext.FeeCalculation.Remove(dbObj);
DbContext.SaveChanges();
//ContentLocker.RemoveLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.GetUserName());
return LoadFeeCalculationList(model.ExpenditureCategoryId, null); //return PartialView("_deleteRate", model);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult EditCalculatesCategory(Guid? id, Guid? expenditureCategoryId)
{
var manager = new CalculatesCategoryManager(DbContext);
var model = manager.Load(id, false) ?? new Expenditure2Expenditure();
if (model.ParentId == Guid.Empty && expenditureCategoryId.HasValue)
model.ParentId = expenditureCategoryId.Value;
return PartialView("_editCalculatesCategory", model);
}
[HttpPost]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult EditCalculatesCategory(Expenditure2Expenditure model)
{
if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ParentId.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
model.TrimStringProperties();
try
{
if (ModelState.IsValid)
{
var manager = new CalculatesCategoryManager(DbContext);
manager.Save(model);
DbContext.SaveChanges();
//ContentLocker.RemoveLock("ExpenditureCalcCategory", model.ParentId.ToString(), User.Identity.GetUserName());
return LoadCalculatesCategoryList(model.ParentId, null);
//return RedirectToAction("Edit", new { id = model.ParentId });
}
ModelState.AddModelError(string.Empty, Constants.ERROR_GENERAL_MESSAGE_TEMPLATE);
}
catch (BLLException blEx) // handle any system specific error
{
// display error message if required
if (blEx.DisplayError)
ModelState.AddModelError(string.Empty, blEx.Message);
else // if display not requried then display modal form with general error message
{
LogException(blEx);
SetErrorScript();
ModelState.AddModelError(string.Empty, blEx.Message);
}
}
catch (Exception exception) // handle any unexpected error
{
LogException(exception);
SetErrorScript();
ModelState.AddModelError(string.Empty, exception.Message);
}
// TODO: check and fix client side code to have support of this kind of response when current functionality is enabled again
return new FailedJsonResult(ModelState);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult LoadCalculatesCategory(Guid? editExpenditure2ExpenditureId, Guid? expenditureCategoryId, Guid? deleteExpenditure2ExpenditureId)
{
ViewBag.ExpenditureCategoryId = expenditureCategoryId.ToString();
var expenditureCategory = new ExpenditureCategoryModel();
try
{
var manager = new ExpenditureCategoryManager(DbContext);
if (expenditureCategoryId != null)
expenditureCategory = (ExpenditureCategoryModel)manager.Load(expenditureCategoryId.Value) ??
new ExpenditureCategoryModel();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
var model = new Expenditure2ExpendituresModel(expenditureCategory.Expenditure2Expenditure.ToList());
if (deleteExpenditure2ExpenditureId.HasValue)
{
try
{
var manager = new CalculatesCategoryManager(DbContext);
model.EditExpenditure2Expenditure = manager.Load(deleteExpenditure2ExpenditureId, false) ?? new Expenditure2Expenditure();
model.DeleteExpenditure2ExpenditureId = deleteExpenditure2ExpenditureId;
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
}
else
{
model.EditExpenditure2Expenditure = new Expenditure2Expenditure();
if (!editExpenditure2ExpenditureId.HasValue || Guid.Empty.Equals(editExpenditure2ExpenditureId.Value))
{
if (expenditureCategoryId != null)
model.EditExpenditure2Expenditure = new Expenditure2Expenditure()
{
ParentId = expenditureCategoryId.Value,
ChildId = Guid.Empty,
FactorType = null,
FactorInt = null,
ProcessOrder = null,
Id = Guid.Empty
};
return PartialView("_calculatesContainer", model);
}
try
{
var manager = new CalculatesCategoryManager(DbContext);
model.EditExpenditure2Expenditure = manager.Load(editExpenditure2ExpenditureId, false) ?? new Expenditure2Expenditure();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
}
return PartialView("_calculatesContainer", model);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Read)]
public ActionResult LoadCalculatesCategoryList(Guid? expenditureCategoryId)
{
return LoadCalculatesCategoryList(expenditureCategoryId, null);
}
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult DeleteCalculatesCategory(Guid? calculatesCategoryId)
{
if (calculatesCategoryId == null || calculatesCategoryId == Guid.Empty)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var model = new Expenditure2Expenditure();
try
{
var manager = new CalculatesCategoryManager(DbContext);
model = manager.Load(calculatesCategoryId);
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();
}
return PartialView("_deleteCalculatesCategory", model);
}
[HttpPost]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult DeleteCalculatesCategory(Expenditure2Expenditure model)
{
if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ParentId.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var manager = new CalculatesCategoryManager(DbContext);
var dbObj = manager.Load(model.Id, false);
if (dbObj == null)
return HttpNotFound();
DbContext.Expenditure2Expenditure.Remove(dbObj);
DbContext.SaveChanges();
//ContentLocker.RemoveLock("ExpenditureCategory", model.ParentId.ToString(), User.Identity.GetUserName());
return LoadCalculatesCategoryList(model.ParentId, null);
}
[HttpGet]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult Delete(Guid? id)
{
if (id == null || id == Guid.Empty)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var model = new ExpenditureCategoryModel();
try
{
var manager = new ExpenditureCategoryManager(DbContext);
model = (ExpenditureCategoryModel)manager.Load(id);
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();
}
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
[AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Write)]
public ActionResult Delete(ExpenditureCategoryModel model)
{
if (ContentLocker.IsLock("ExpenditureCategory", model.Id.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var manager = new ExpenditureCategoryManager(DbContext);
var dbObj = manager.Load(model.Id, false);
if (dbObj == null)
return HttpNotFound();
if (dbObj.ScenarioDetail.Count > 0)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest,
Messages.ExpenditureCategory_Delete_HasAssignedScenarios_Error);
if (dbObj.PeopleResources.Count > 0)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest,
Messages.ExpenditureCategory_Delete_UsedByResources_Error);
var feeCalculationToDeleteCollection = new List();
while (dbObj.FeeCalculation.Count > 0)
{
var feeManager = new FeeCalculationManager(DbContext);
var feeDbObj = feeManager.Load(dbObj.FeeCalculation.Last().Id, false);
if (feeDbObj == null)
return HttpNotFound();
feeCalculationToDeleteCollection.Add(feeDbObj);
}
DbContext.FeeCalculation.RemoveRange(feeCalculationToDeleteCollection);
var rateToDeleteCollection = new List();
while (dbObj.Rates.Count > 0)
{
var rateManager = new RateManager(DbContext);
var rateDbObj = rateManager.Load(dbObj.Rates.Last().Id, false);
if (rateDbObj == null)
return HttpNotFound();
rateToDeleteCollection.Add(rateDbObj);
}
DbContext.Rates.RemoveRange(rateToDeleteCollection);
var expenditure2ExpenditureToDeleteCollection = new List();
while (dbObj.Expenditure2Expenditure.Count > 0)
{
var categoryManager = new CalculatesCategoryManager(DbContext);
var categoryDbObj = categoryManager.Load(dbObj.Expenditure2Expenditure.Last().Id, false);
if (categoryDbObj == null)
return HttpNotFound();
expenditure2ExpenditureToDeleteCollection.Add(categoryDbObj);
}
DbContext.Expenditure2Expenditure.RemoveRange(expenditure2ExpenditureToDeleteCollection);
DbContext.Holiday2ExpenditureCategory.RemoveRange(dbObj.Holiday2ExpenditureCategory);
// Remove EC change records for resources, because for table PeopleResourceExpCatChange has no cascade delete constraint for NewExpenditureCategoryId field
var resourceExpCatChangeRecords = DbContext.PeopleResourceExpCatChanges.Where(x => x.NewExpenditureCategoryId.Equals(model.Id)).ToList();
if (resourceExpCatChangeRecords.Count > 0)
{
DbContext.PeopleResourceExpCatChanges.RemoveRange(resourceExpCatChangeRecords);
}
DbContext.ExpenditureCategory.Remove(dbObj);
DbContext.SaveChanges();
//ContentLocker.RemoveLock("ExpenditureCategory", dbObj.Id.ToString(), User.Identity.GetUserName());
return RedirectToAction("Index");
}
[HttpPost]
// [AreaSecurity(area = Areas.ExpenditureCategories, level = AccessLevel.Read)]
public JsonResult GetAllCategories()
{
var categories = (new ExpenditureCategoryManager(DbContext)).GetExpenditureDetails(true);
// javascript serializer does not support Guid as a type for dictionary key
var model = categories.ToDictionary(x => x.Key.ToString(), g => g.Value);
return Json(model);
}
#endregion
#region Private Methods
private ActionResult LoadCalculatesCategoryList(Guid? expenditureCategoryId, Expenditure2Expenditure editExpenditure2Expenditure)
{
ViewBag.ExpenditureCategoryId = expenditureCategoryId.ToString();
var expenditureCategory = new ExpenditureCategoryModel();
try
{
var manager = new ExpenditureCategoryManager(DbContext);
expenditureCategory = (ExpenditureCategoryModel)manager.Load(expenditureCategoryId) ?? new ExpenditureCategoryModel();
expenditureCategory.CalculatesCategories = new Expenditure2ExpendituresModel(expenditureCategory.Expenditure2Expenditure.OrderBy(x => x.ProcessOrder).ToList());
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
var calaCat = expenditureCategory.CalculatesCategories;
calaCat.EditExpenditure2Expenditure = editExpenditure2Expenditure;
return PartialView("_calculatesContainer", calaCat);
}
private ActionResult LoadFeeCalculationList(Guid? expenditureCategoryId, FeeCalculation editFee)
{
ViewBag.ExpenditureCategoryId = expenditureCategoryId.ToString();
var expenditureCategory = new ExpenditureCategoryModel();
try
{
var manager = new ExpenditureCategoryManager(DbContext);
expenditureCategory = (ExpenditureCategoryModel)manager.Load(expenditureCategoryId) ?? new ExpenditureCategoryModel();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
var fees = new FeesModel(expenditureCategory.FeeCalculation) { EditFee = editFee };
return PartialView("_feesContainer", fees);
}
private List