256 lines
10 KiB
C#
256 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Web.Mvc;
|
|
using EnVisage.Code;
|
|
using EnVisage.Code.BLL;
|
|
using EnVisage.Models;
|
|
using jQuery.DataTables.Mvc;
|
|
using EnVisage.App_Start;
|
|
using EnVisage.Code.HtmlHelpers;
|
|
using Microsoft.AspNet.Identity;
|
|
using System.Text;
|
|
|
|
namespace EnVisage.Controllers
|
|
{
|
|
[Authorize]
|
|
public class UnitOfMeasureController : BaseController
|
|
{
|
|
/// <summary>
|
|
/// GET: /UnitOfMeasure/
|
|
/// </summary>
|
|
/// <returns>Empty view</returns>
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.UnitsOfMeasure, level = AccessLevel.Read)]
|
|
public ActionResult Index()
|
|
{
|
|
if (!SecurityManager.CheckSecurityObjectPermission(Areas.UnitsOfMeasure, AccessLevel.Read))
|
|
return Redirect("/");
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns JSON UnitOfMeasure list with filters and sort for jQuery DataTables
|
|
/// </summary>
|
|
[HttpPost]
|
|
[AreaSecurityAttribute(area = Areas.UnitsOfMeasure, level = AccessLevel.Read)]
|
|
public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel)
|
|
{
|
|
int totalRecordCount;
|
|
int searchRecordCount;
|
|
|
|
var units = GetUnits(startIndex: jQueryDataTablesModel.iDisplayStart,
|
|
pageSize: jQueryDataTablesModel.iDisplayLength, sortedColumns: jQueryDataTablesModel.GetSortedColumns(),
|
|
totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount, searchString: jQueryDataTablesModel.sSearch);
|
|
|
|
return this.DataTablesJson(items: units,
|
|
totalRecords: totalRecordCount,
|
|
totalDisplayRecords: searchRecordCount,
|
|
sEcho: jQueryDataTablesModel.sEcho);
|
|
|
|
}
|
|
|
|
private IEnumerable<UOMModel> GetUnits(int startIndex,
|
|
int pageSize,
|
|
IEnumerable<SortedColumn> sortedColumns,
|
|
out int totalRecordCount,
|
|
out int searchRecordCount,
|
|
string searchString)
|
|
{
|
|
var query = from c in DbContext.UOMs select new UOMModel { Id = c.Id, Name = c.Name, UOMValue = c.UOMValue, ExpenditureCategoryCount = c.ExpenditureCategory.Count() };
|
|
|
|
//filter
|
|
if (!string.IsNullOrWhiteSpace(searchString))
|
|
{
|
|
query = query.Where(c => c.Name.ToLower().Contains(searchString.ToLower()));
|
|
}
|
|
|
|
//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 "UOMValue":
|
|
if (sortedColumn.Direction == SortingDirection.Ascending)
|
|
query = query.OrderBy(c => c.UOMValue);
|
|
else
|
|
query = query.OrderByDescending(c => c.UOMValue);
|
|
break;
|
|
case "ExpenditureCategoryCount":
|
|
if (sortedColumn.Direction == SortingDirection.Ascending)
|
|
query = query.OrderBy(c => c.ExpenditureCategoryCount);
|
|
else
|
|
query = query.OrderByDescending(c => c.ExpenditureCategoryCount);
|
|
break;
|
|
default:
|
|
if (sortedColumn.Direction == SortingDirection.Ascending)
|
|
query = query.OrderBy(c => c.Name);
|
|
else
|
|
query = query.OrderByDescending(c => c.Name);
|
|
break;
|
|
}
|
|
}
|
|
|
|
totalRecordCount = DbContext.UOMs.Count();
|
|
searchRecordCount = query.Count();
|
|
return query.Skip(startIndex).Take(pageSize).ToList();
|
|
}
|
|
|
|
// GET: /UnitOfMeasure/Edit/5
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.UnitsOfMeasure, level = AccessLevel.Write)]
|
|
public ActionResult Edit(Guid? id)
|
|
{
|
|
var model = new UOMModel();
|
|
try
|
|
{
|
|
var manager = new UOMManager(DbContext);
|
|
model = (UOMModel)manager.Load(id) ?? new UOMModel();
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
SetErrorScript(message: blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
SetErrorScript();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
SetErrorScript();
|
|
}
|
|
return View(model);
|
|
}
|
|
|
|
// POST: /UnitOfMeasure/Edit/5
|
|
// Чтобы защититься от атак чрезмерной передачи данных, включите определенные свойства, для которых следует установить привязку. Дополнительные
|
|
// сведения см. в статье http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurityAttribute(area = Areas.UnitsOfMeasure, level = AccessLevel.Write)]
|
|
public ActionResult Edit(UOMModel model)
|
|
{
|
|
if (ContentLocker.IsLock("UnitOfMeasure", model.Id.ToString(), User.Identity.GetUserName()))
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
model.TrimStringProperties();
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var manager = new UOMManager(DbContext);
|
|
if (model.Id != null && model.Id != Guid.Empty)
|
|
{
|
|
var oldUOM = (UOMModel)DbContext.UOMs.Where(x => x.Id == model.Id).FirstOrDefault();
|
|
if (oldUOM != null && model.UOMValue != oldUOM.UOMValue) {
|
|
var scmanager = new ScenarioManager(DbContext);
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("UOMController.Edit method. model:");
|
|
model.DebugObjectProperties(sb);
|
|
sb.AppendLine("oldUOM:");
|
|
oldUOM.DebugObjectProperties(sb);
|
|
Logger.Debug(sb);
|
|
|
|
scmanager.RecalculateCapacityScenariosUOM(model, oldUOM);
|
|
}
|
|
}
|
|
manager.Save(model);
|
|
DbContext.SaveChanges();
|
|
ContentLocker.RemoveLock("UnitOfMeasure", 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);
|
|
}
|
|
|
|
// GET: /UnitOfMeasure/Delete/5
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.UnitsOfMeasure, level = AccessLevel.Write)]
|
|
public ActionResult Delete(Guid? id)
|
|
{
|
|
if (id == null || id == Guid.Empty)
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
var model = new UOMModel();
|
|
try
|
|
{
|
|
var manager = new UOMManager(DbContext);
|
|
model = (UOMModel)manager.Load(id);
|
|
if (model == null)
|
|
return HttpNotFound();
|
|
if (model.ExpenditureCategoryCount > 0)
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest,
|
|
"UOM has Expenditures assigned and could not be deleted");
|
|
}
|
|
catch (BLLException blEx)
|
|
{
|
|
if (blEx.DisplayError)
|
|
SetErrorScript(message: blEx.Message);
|
|
else
|
|
{
|
|
LogException(blEx);
|
|
SetErrorScript();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
LogException(exception);
|
|
SetErrorScript();
|
|
}
|
|
return View(model);
|
|
}
|
|
|
|
// POST: /UnitOfMeasure/Delete/5
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurityAttribute(area = Areas.UnitsOfMeasure, level = AccessLevel.Write)]
|
|
public ActionResult Delete(UOMModel model)
|
|
{
|
|
if (ContentLocker.IsLock("UnitOfMeasure", model.Id.ToString(), User.Identity.GetUserName()))
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
var manager = new UOMManager(DbContext);
|
|
var dbObj = manager.Load(model.Id, false);
|
|
if (dbObj == null)
|
|
return HttpNotFound();
|
|
if (dbObj.ExpenditureCategory.Count > 0)
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest,
|
|
"UOM has Ependitures assigned and could not be deleted");
|
|
DbContext.UOMs.Remove(dbObj);
|
|
DbContext.SaveChanges();
|
|
ContentLocker.RemoveLock("UnitOfMeasure", dbObj.Id.ToString(), User.Identity.GetUserName());
|
|
return RedirectToAction("Index");
|
|
}
|
|
}
|
|
}
|