using GreenTree.Nachtragsmanagement.Core;
using GreenTree.Nachtragsmanagement.Services.Appendix;
using GreenTree.Nachtragsmanagement.Services.Deviation;
using GreenTree.Nachtragsmanagement.Services.Site;
using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
using GreenTree.Nachtragsmanagement.Web.Models.Appendix;
using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace GreenTree.Nachtragsmanagement.Web.Controllers
{
public class AppendixController : Controller
{
private readonly IDeviationService _deviationSerivce;
private readonly IAppendixService _appendixService;
private readonly ISiteService _siteService;
public AppendixController(
IDeviationService deviationService,
IAppendixService appendixService,
ISiteService siteService)
{
_deviationSerivce = deviationService;
_appendixService = appendixService;
_siteService = siteService;
ViewData["AllCategories"] = _appendixService.GetAllCategories();
ViewData["AllStates"] = _appendixService.GetAllStates();
}
#region Appendices
///
/// Basic appendix view function
///
[FunctionAuthorize(true, "Appendix-Appendices")]
public ActionResult ViewAppendices()
{
var appendices = _appendixService.GetAllAppendices();
var appendixModels = appendices
.Select(u => AppendixDataModel.FromAppendix(u, false))
.ToList();
return View("~/Views/Appendices/View.cshtml", appendixModels);
}
///
/// Get JSON data of specific appendix
///
/// Appendix id.
public ActionResult GetAppendix(int id = -1)
{
var appendix = _appendixService.GetAppendixById(id);
if (appendix == null)
return new JsonResult
{
Data = "notFound",
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
var appendixModel = AppendixDataModel.FromAppendix(appendix, false);
return new JsonResult
{
Data = JsonConvert.SerializeObject(appendixModel),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
///
/// Callback result for appendix grid
///
public ActionResult PartialAppendices()
{
var appendices = _appendixService.GetAllAppendices();
var appendixModels = appendices
.Select(u => AppendixDataModel.FromAppendix(u, false))
.ToList();
return PartialView("~/Views/Appendices/_AppendixGridPartial.cshtml", appendixModels);
}
///
/// Partial edit for editing of existing or for new appendix
///
/// Id for existing appendix, otherweise -1.
public ActionResult EditAppendix(int id = -1)
{
var appendix = _appendixService.GetAppendixById(id);
var appendixModel = AppendixDataModel.FromAppendix(appendix, true);
var defaultState = _appendixService.GetDefaultState();
if (defaultState != null)
ViewData["DefaultState"] = defaultState.Id;
return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
}
///
/// Partial edit for creating a new deviation for a site
///
/// Id of the site which the deviation should be appended to.
public ActionResult AppendAppendixToSite(int siteId)
{
var site = _siteService.GetSiteById(siteId);
var lastCustomNumber = site.Appendices
.Max(d => StaticHelper.TryParseInt(d.CustomNumber));
var appendixModel = new AppendixDataModel
{
Id = -1,
SiteId = siteId,
CustomNumber = (lastCustomNumber + 1).ToString(),
Percentage = (decimal)0.5
};
var defaultState = _appendixService.GetDefaultState();
if (defaultState != null)
ViewData["DefaultState"] = defaultState.Id;
//ViewData["relationType"] = "site";
//ViewData["relationId"] = siteId;
return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
}
///
/// Partial edit result if ModelState is valid, otherwise simple JSON result for success
///
/// Appendix model to be saved.
[HttpPost, ValidateInput(false)]
public ActionResult EditAppendix(AppendixDataModel appendixModel)
{
appendixModel.CategoryValueEntities =
appendixModel.CategoryEntities
.Select(r => JsonConvert.DeserializeObject(r))
.ToList();
for (int i = 0; i < appendixModel.CategoryValueEntities.Count; i++)
appendixModel.CategoryValueEntities.ElementAt(i).Json = appendixModel.CategoryEntities.ElementAt(i);
appendixModel.PercentageValue = appendixModel.OfferingValue * appendixModel.Percentage;
if (!ModelState.IsValid)
return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
var categoryValues = appendixModel.CategoryValueEntities
.Select(r => r.ToCategoryValue())
.ToList();
if (appendixModel.Id == -1)
{
var appendix = appendixModel.ToAppendix();
appendix.SetCategoryValues(categoryValues);
_appendixService.InsertAppendix(appendix);
}
else
{
var appendix = _appendixService.GetAppendixById(appendixModel.Id);
appendix.CustomNumber = appendixModel.CustomNumber;
appendix.Description = appendixModel.Description;
appendix.Percentage = appendixModel.Percentage;
appendix.OfferingNumber = appendixModel.OfferingNumber;
appendix.OfferingDate = appendixModel.OfferingDate;
appendix.NegotiationDate = appendixModel.NegotiationDate;
appendix.NegotiationValue = appendixModel.NegotiationValue;
appendix.ProtocolExists = appendixModel.ProtocolExists;
appendix.StateId = appendixModel.StateId;
appendix.OrderNumber = appendixModel.OrderNumber;
appendix.OrderDate = appendixModel.OrderDate;
appendix.OrderInvoiceCreated = appendixModel.OrderInvoiceCreated;
appendix.Comment = appendixModel.Comment;
appendix.SiteId = appendixModel.SiteId;
appendix.SetCategoryValues(categoryValues);
_appendixService.UpdateAppendix(appendix);
}
return new JsonResult
{
Data = "success"
};
}
///
/// Simple JSON result for deleting a specific appendix
///
/// Appendix id.
[HttpPost]
public ActionResult DeleteAppendix(int id)
{
var appendix = _appendixService.GetAppendixById(id);
if (appendix != null)
_appendixService.DeleteAppendix(appendix);
return new JsonResult
{
Data = "success"
};
}
#endregion
#region Claims
///
/// Basic claim view function
///
[FunctionAuthorize(true, "Appendix-Claims")]
public ActionResult ViewClaims()
{
return View("~/Views/Appendices/Claims.cshtml");
}
///
/// Get JSON data of specific claim
///
/// Claim type.
/// Claim id.
public ActionResult GetClaim(string claimType, int id = -1)
{
switch (claimType.ToLower())
{
case "state":
var state = _appendixService.GetStateById(id);
if (state == null)
return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
else
return new JsonResult
{
Data = JsonConvert.SerializeObject(state),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
case "category":
var category = _appendixService.GetCategoryById(id);
if (category == null)
return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
else
return new JsonResult
{
Data = JsonConvert.SerializeObject(category),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
default:
return new JsonResult { Data = "unknownClaimType", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
///
/// Callback result for claim grid
///
/// Claim type.
public ActionResult PartialClaims(string claimType)
{
switch (claimType.ToLower())
{
case "state":
return PartialView("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"]);
case "category":
return PartialView("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"]);
default:
return new EmptyResult();
}
}
///
/// Partial edit for editing of existing or for new claim
///
/// Claim type.
/// Id for existing claim, otherweise -1.
public ActionResult EditClaim(string claimType = "", int id = -1)
{
switch (claimType.ToLower())
{
case "state":
var state = _appendixService.GetStateById(id);
var stateModel = StateDataModel.FromState(state, true);
return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
case "category":
var category = _appendixService.GetCategoryById(id);
var categoryModel = CategoryDataModel.FromCategory(category, true);
return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
default:
return new EmptyResult();
}
}
///
/// Partial edit result if ModelState is valid, otherwise simple JSON result for success
///
/// State model to be saved.
[HttpPost, ValidateInput(false)]
public ActionResult EditState(StateDataModel stateModel)
{
if (!ModelState.IsValid)
return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
if (stateModel.Id == -1)
{
var claim = stateModel.ToState();
_appendixService.InsertState(claim);
}
else
{
var state = _appendixService.GetStateById(stateModel.Id);
state.Description = stateModel.Description;
state.HexColor = stateModel.HexColor;
state.IsDefault = stateModel.IsDefault;
_appendixService.UpdateState(state);
}
return new JsonResult
{
Data = "success"
};
}
///
/// Partial edit result if ModelState is valid, otherwise simple JSON result for success
///
/// Category model to be saved.
[HttpPost, ValidateInput(false)]
public ActionResult EditCategory(CategoryDataModel categoryModel)
{
if (!ModelState.IsValid)
return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
if (categoryModel.Id == -1)
{
var claim = categoryModel.ToCategory();
_appendixService.InsertCategory(claim);
}
else
{
var category = _appendixService.GetCategoryById(categoryModel.Id);
category.Description = categoryModel.Description;
_appendixService.UpdateCategory(category);
}
return new JsonResult
{
Data = "success"
};
}
///
/// Simple JSON result for deleting a specific claim
///
/// Claim type.
/// Claim id.
/// Id of claim which deviations get in place of deleting claim.
[HttpPost]
public ActionResult DeleteClaim(string claimType, int id, int replaceId)
{
switch (claimType.ToLower())
{
case "state":
var state = _appendixService.GetStateById(id);
var replaceState = _appendixService.GetStateById(replaceId);
var stateAppendices = _appendixService.GetAppendicesByState(id);
foreach (var appendix in stateAppendices)
{
appendix.StateId = replaceId;
appendix.State = replaceState;
_appendixService.UpdateAppendix(appendix);
}
if (state != null)
_appendixService.DeleteState(state);
break;
case "category":
var category = _appendixService.GetCategoryById(id);
var replaceCategory = _appendixService.GetCategoryById(replaceId);
var allAppendices = _appendixService.GetAllAppendices();
foreach (var appendix in allAppendices)
{
foreach (var categoryValue in appendix.CategoryValues)
{
if (categoryValue.CategoryId == id)
{
categoryValue.Category = replaceCategory;
categoryValue.CategoryId = replaceCategory.Id;
}
}
_appendixService.UpdateAppendix(appendix);
}
if (category != null)
_appendixService.DeleteCategory(category);
break;
default:
return new EmptyResult();
}
return new JsonResult
{
Data = "success"
};
}
#endregion
}
}