using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using EnVisage; using EnVisage.Code; using jQuery.DataTables.Mvc; using EnVisage.Models; using EnVisage.Code.BLL; using EnVisage.App_Start; namespace EnVisage.Controllers { public class ExpenditureCategoryController : BaseController { /// /// An UI representation of Expenditure Category to be displayed as list items /// public class ListExpenditureCategory { public Guid Id { get; set; } public string Name { get; set; } public int? Type { get; set; } public string TypeS { get; set; } public int? UseType { get; set; } public string UseTypeS { get; set; } public string CGEFX { get; set; } public string GLName { get; set; } public string GLNumber { get; set; } public string UOM { get; set; } public decimal? UOMValue { get; set; } public string CreditDepartment { get; set; } public int ScenarioDetailCount { get; set; } } /// /// GET: /ExpenditureCategory/ /// /// Empty view [HttpGet] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Read)] public ActionResult Index() { //if (!string.IsNullOrEmpty(Request.QueryString["UOM"])) //{ // return View(new { UOM = Request.QueryString["UOM"].ToString() }); //} return View(); } //[HttpGet] //[AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Read)] //public JsonResult Index(string UOM) //{ // JQueryDataTablesModel jQueryDataTablesModel = new JQueryDataTablesModel(); // jQueryDataTablesModel.sSearch_ = new ReadOnlyCollection(new List(){ string.Empty, string.Empty, UOM }); // return GetList(jQueryDataTablesModel); //} /// /// Returns JSON Expenditure Category list with filters and sort for jQuery DataTables /// [HttpPost] [AreaSecurityAttribute(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"].ToString(), 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); } } private IList GetExpenditureCategories(int startIndex, int pageSize, ReadOnlyCollection sortedColumns, out int totalRecordCount, out int searchRecordCount, string searchString, ReadOnlyCollection filters) { var query = from c in DbContext.ExpenditureCategory select new ListExpenditureCategory() { Id = c.Id, Name = c.Expenditure.Name, CGEFX = c.CGEFX, GLName = c.GLAccount.Name, GLNumber = c.GLAccount.GLNumber, Type = c.Type, UseType = c.UseType, CreditDepartment = c.CreditDepartment.Name, UOM = c.UOM.Name, UOMValue = c.UOM.UOMValue, ScenarioDetailCount = c.ScenarioDetail.Count }; //filter if (!string.IsNullOrWhiteSpace(searchString)) { query = query.Where(c => c.Name.ToLower().Contains(searchString.ToLower())); } if (!string.IsNullOrWhiteSpace(filters[0])) query = query.Where(c => c.Name.ToLower().Equals(filters[0].ToLower())); //if (!string.IsNullOrWhiteSpace(filters[1])) // query = query.Where(c => c.UOM.ToLower().Equals(filters[1].ToLower())); var search_uom = filters[2]; if (!string.IsNullOrWhiteSpace(search_uom) && !"All units".Equals(search_uom,StringComparison.CurrentCultureIgnoreCase)) { var q = new List(); var search_uom_arr = search_uom.Split(' '); var search_uom_name = search_uom_arr.FirstOrDefault(); var search_uom_val = search_uom_arr.Length > 1 ? search_uom_arr[1] : string.Empty; decimal search_uom_val_dec; if (decimal.TryParse(search_uom_val, out search_uom_val_dec)) { foreach (var c in query.Where(c => search_uom_name.Equals(c.UOM, StringComparison.CurrentCultureIgnoreCase) && c.UOMValue == search_uom_val_dec)) { q.Add(c); } } else { foreach (var c in query.Where(c => search_uom_name.Equals(c.UOM, StringComparison.CurrentCultureIgnoreCase) && c.UOMValue == null)) { q.Add(c); } } query = q.AsQueryable(); } //sort foreach (var sortedColumn in sortedColumns) { switch (sortedColumn.PropertyName) { case "Id": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.Id); else query = query.OrderByDescending(c => c.Id); break; case "GLName": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.GLName); else query = query.OrderByDescending(c => c.GLName); break; case "GLNumber": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.GLNumber); else query = query.OrderByDescending(c => c.GLNumber); break; case "TypeS": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.Type); else query = query.OrderByDescending(c => c.Type); break; case "UseTypeS": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.UseType); else query = query.OrderByDescending(c => c.UseType); break; case "UOM": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.UOM); else query = query.OrderByDescending(c => c.UOM); break; case "CreditDepartment": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.CreditDepartment); else query = query.OrderByDescending(c => c.CreditDepartment); break; default: if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.Name); else query = query.OrderByDescending(c => c.Name); break; } } totalRecordCount = DbContext.ExpenditureCategory.Count(); searchRecordCount = query.Count(); var list = query.Skip(startIndex).Take(pageSize).ToList(); foreach (var c in list) { c.TypeS = c.Type.HasValue ? ((ExpenditureCategoryModel.CategoryTypes)c.Type).ToDisplayValue() : ""; c.UseTypeS = c.UseType.HasValue ? ((ExpenditureCategoryModel.UseTypes)c.UseType).ToDisplayValue() : ""; c.UOM = c.UOM + (c.UOMValue.HasValue && c.UOMValue > 0 ? " " + c.UOMValue.Value.ToString("0.##") : ""); } return list; } // GET: /ExpenditureCategory/Details/5 [AreaSecurityAttribute(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 [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult Edit(Guid? id) { ViewBag.ExpenditureCategoryId = id.ToString(); var model = new ExpenditureCategoryModel(); try { var manager = new ExpenditureCategoryManager(DbContext); model = (ExpenditureCategoryModel)manager.Load(id) ?? new ExpenditureCategoryModel(); model.CalculatesCategories = new Expenditure2ExpendituresModel(model.Expenditure2Expenditure.OrderBy(x=>x.ProcessOrder).ToList()); model.Fees = new FeesModel(model.FeeCalculation ); } 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] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult Edit(ExpenditureCategoryModel model) { if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.Id.ToString(), User.Identity.Name)) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); model.TrimStringProperties(); if (ModelState.IsValid) { try { var manager = new ExpenditureCategoryManager(DbContext); manager.Save(model); DbContext.SaveChanges(); ContentLocker.RemoveLock("ExpenditureCategory", model.Id.ToString(), User.Identity.Name); 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); } [AreaSecurityAttribute(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)) { var lRates = (from c in DbContext.Rates where c.ExpenditureCategoryId == expenditureCategoryId.Value && c.Type == (int)RateModel.RateType.Global select c).ToList(); if (lRates.Count == 0 || (from d in lRates select d.EndDate).Max() < DateTime.MaxValue.AddYears(-4)) { model.StartDate = (lRates.Count == 0) ? DateTime.Today : ((from d in lRates select d.EndDate).Max()).AddDays(1); model.EndDate = model.StartDate.AddYears(5).AddDays(-1); } else { model.StartDate = (from d in lRates select d.EndDate).Max(); model.EndDate = model.StartDate; } } if ((model.ParentId == Guid.Empty || model.ParentId == null) && expenditureCategoryId.HasValue) model.ParentId = expenditureCategoryId.Value; if ((model.ExpenditureCategoryId == Guid.Empty || model.ExpenditureCategoryId == null) && expenditureCategoryId.HasValue) model.ExpenditureCategoryId = expenditureCategoryId.Value; return PartialView("_editRate", model); } [HttpPost] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult EditRate(RateModel model) { if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.Id.ToString(), User.Identity.Name)) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); model.TrimStringProperties(); try { if (ModelState.IsValid) { var manager = new RateManager(DbContext); manager.Save(model); DbContext.SaveChanges(); manager.Dispose(); //ContentLocker.RemoveLock("ExpenditureCategoryRate", model.Id.ToString(), User.Identity.Name); return LoadRateList(model.ExpenditureCategoryId, null); } else { 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); } HttpContext.Response.StatusCode = 500; HttpContext.Response.Clear(); return LoadRateList(model.ExpenditureCategoryId, model); //PartialView("_ratesContainer", model); } [AreaSecurityAttribute(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); } [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Read)] public ActionResult LoadRateList(Guid? ExpenditureCategoryId) { return LoadRateList(ExpenditureCategoryId, null); } private ActionResult LoadRateList(Guid? ExpenditureCategoryId, RateModel editRate) { 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 rates = expenditureCategory.Rates; rates.EditRate = editRate; //if(ModelState.IsValid) // ContentLocker.RemoveLock("ExpenditureCategoryRate", ExpenditureCategoryId.ToString(), User.Identity.Name); return PartialView("_ratesContainer", rates); } [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult DeleteRate(Guid? rateId) { if (rateId == null || rateId == Guid.Empty) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var model = new RateModel(); try { var manager = new RateManager(DbContext); model = (RateModel)manager.Load(rateId); 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("_deleteRate", model); } [HttpPost] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult DeleteRate(RateModel model) { if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.Name)) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var manager = new RateManager(DbContext); var dbObj = manager.Load(model.Id, false); if (dbObj == null) return HttpNotFound(); DbContext.Rates.Remove(dbObj); DbContext.SaveChanges(); //ContentLocker.RemoveLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.Name); return LoadRateList(model.ExpenditureCategoryId, null); //return PartialView("_deleteRate", model); } [AreaSecurityAttribute(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(); if (model.ExpenditureCategoryId == null) model.ExpenditureCategoryId = expenditureCategoryId; return PartialView("_editFeeCalculation", model); } [HttpPost] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult EditFeeCalculation(FeeCalculation model) { if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.Name)) 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.Name); return LoadFeeCalculationList(model.ExpenditureCategoryId, null); } else { 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); } HttpContext.Response.StatusCode = 500; HttpContext.Response.Clear(); return LoadFeeCalculationList(model.ExpenditureCategoryId, model); //PartialView("_ratesContainer", model); } [AreaSecurityAttribute(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 = (FeeCalculation)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 = (FeeCalculation)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); } 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); fees.EditFee = editFee; //if (ModelState.IsValid) // ContentLocker.RemoveLock("ExpenditureCategoryFee", ExpenditureCategoryId.ToString(), User.Identity.Name); return PartialView("_feesContainer", fees); } [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] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult DeleteFeeCalculation(FeeCalculation model) { if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ExpenditureCategoryId.ToString(), User.Identity.Name)) 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.Name); return LoadFeeCalculationList(model.ExpenditureCategoryId, null); //return PartialView("_deleteRate", model); } [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult EditCalculatesCategory(Guid? id, Guid? expenditureCategoryId) { var manager = new CalculatesCategoryManager(DbContext); var model = (Expenditure2Expenditure)manager.Load(id, false) ?? new Expenditure2Expenditure(); if (model.ParentId == Guid.Empty && expenditureCategoryId.HasValue) model.ParentId = expenditureCategoryId.Value; return PartialView("_editCalculatesCategory", model); } [HttpPost] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult EditCalculatesCategory(Expenditure2Expenditure model) { if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ParentId.ToString(), User.Identity.Name)) 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.Name); return LoadCalculatesCategoryList(model.ParentId, null); //return RedirectToAction("Edit", new { id = model.ParentId }); } else { 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); } HttpContext.Response.StatusCode = 500; HttpContext.Response.Clear(); return LoadCalculatesCategoryList(model.ParentId, model); //PartialView("_ratesContainer", model); } [AreaSecurityAttribute(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); 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 = (Expenditure2Expenditure)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)) { 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 = (Expenditure2Expenditure)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); } [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Read)] public ActionResult LoadCalculatesCategoryList(Guid? expenditureCategoryId) { return LoadCalculatesCategoryList(expenditureCategoryId, null); } 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; //if (ModelState.IsValid) // ContentLocker.RemoveLock("ExpenditureCategoryRate", expenditureCategoryId.ToString(), User.Identity.Name); return PartialView("_calculatesContainer", calaCat); } [AreaSecurityAttribute(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 = (Expenditure2Expenditure)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] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult DeleteCalculatesCategory(Expenditure2Expenditure model) { if (model == null || ContentLocker.IsLock("ExpenditureCategory", model.ParentId.ToString(), User.Identity.Name)) 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.Name); return LoadCalculatesCategoryList(model.ParentId, null); } // GET: /CreditDepartment/Delete/5 [HttpGet] [AreaSecurityAttribute(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); } // POST: /CreditDepartment/Delete/5 [HttpPost] [ValidateAntiForgeryToken] [AreaSecurityAttribute(area = Areas.ExpenditureCategories, level = AccessLevel.Write)] public ActionResult Delete(ExpenditureCategoryModel model) { if (ContentLocker.IsLock("ExpenditureCategory", model.Id.ToString(), User.Identity.Name)) 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, "Expenditure Category has assigned scenarios and could not be deleted"); while (dbObj.FeeCalculation.Count > 0) { var feeManager = new FeeCalculationManager(DbContext); var feeDbObj = feeManager.Load(dbObj.FeeCalculation.Last().Id, false); if (feeDbObj == null) return HttpNotFound(); DbContext.FeeCalculation.Remove(feeDbObj); } while ( dbObj.Rates.Count > 0) { var rateManager = new RateManager(DbContext); var rateDbObj = rateManager.Load(dbObj.Rates.Last().Id, false); if (rateDbObj == null) return HttpNotFound(); DbContext.Rates.Remove(rateDbObj); } while (dbObj.Expenditure2Expenditure.Count>0) { var categoryManager = new CalculatesCategoryManager(DbContext); var categoryDbObj = categoryManager.Load(dbObj.Expenditure2Expenditure.Last().Id, false); if (categoryDbObj == null) return HttpNotFound(); DbContext.Expenditure2Expenditure.Remove(categoryDbObj); } DbContext.SaveChanges(); DbContext.ExpenditureCategory.Remove(dbObj); DbContext.SaveChanges(); //ContentLocker.RemoveLock("ExpenditureCategory", dbObj.Id.ToString(), User.Identity.Name); return RedirectToAction("Index"); } } }