AppendixController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using GreenTree.Nachtragsmanagement.Core;
  2. using GreenTree.Nachtragsmanagement.Services.Appendix;
  3. using GreenTree.Nachtragsmanagement.Services.Deviation;
  4. using GreenTree.Nachtragsmanagement.Services.Site;
  5. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  6. using GreenTree.Nachtragsmanagement.Web.Models.Appendix;
  7. using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
  8. using Newtonsoft.Json;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. using System.Web.Mvc;
  14. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  15. {
  16. public class AppendixController : Controller
  17. {
  18. private readonly IDeviationService _deviationSerivce;
  19. private readonly IAppendixService _appendixService;
  20. private readonly ISiteService _siteService;
  21. public AppendixController(
  22. IDeviationService deviationService,
  23. IAppendixService appendixService,
  24. ISiteService siteService)
  25. {
  26. _deviationSerivce = deviationService;
  27. _appendixService = appendixService;
  28. _siteService = siteService;
  29. ViewData["AllCategories"] = _appendixService.GetAllCategories();
  30. ViewData["AllStates"] = _appendixService.GetAllStates();
  31. }
  32. #region Appendices
  33. /// <summary>
  34. /// Basic appendix view function
  35. /// </summary>
  36. [FunctionAuthorize(true, "Appendix-Appendices")]
  37. public ActionResult ViewAppendices()
  38. {
  39. var appendices = _appendixService.GetAllAppendices();
  40. var appendixModels = appendices
  41. .Select(u => AppendixDataModel.FromAppendix(u, false))
  42. .ToList();
  43. return View("~/Views/Appendices/View.cshtml", appendixModels);
  44. }
  45. /// <summary>
  46. /// Get JSON data of specific appendix
  47. /// </summary>
  48. /// <param name="id">Appendix id.</param>
  49. public ActionResult GetAppendix(int id = -1)
  50. {
  51. var appendix = _appendixService.GetAppendixById(id);
  52. if (appendix == null)
  53. return new JsonResult
  54. {
  55. Data = "notFound",
  56. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  57. };
  58. var appendixModel = AppendixDataModel.FromAppendix(appendix, false);
  59. return new JsonResult
  60. {
  61. Data = JsonConvert.SerializeObject(appendixModel),
  62. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  63. };
  64. }
  65. /// <summary>
  66. /// Callback result for appendix grid
  67. /// </summary>
  68. public ActionResult PartialAppendices()
  69. {
  70. var appendices = _appendixService.GetAllAppendices();
  71. var appendixModels = appendices
  72. .Select(u => AppendixDataModel.FromAppendix(u, false))
  73. .ToList();
  74. return PartialView("~/Views/Appendices/_AppendixGridPartial.cshtml", appendixModels);
  75. }
  76. /// <summary>
  77. /// Partial edit for editing of existing or for new appendix
  78. /// </summary>
  79. /// <param name="id">Id for existing appendix, otherweise -1.</param>
  80. public ActionResult EditAppendix(int id = -1)
  81. {
  82. var appendix = _appendixService.GetAppendixById(id);
  83. var appendixModel = AppendixDataModel.FromAppendix(appendix, true);
  84. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  85. }
  86. /// <summary>
  87. /// Partial edit for creating a new deviation for a site
  88. /// </summary>
  89. /// <param name="siteId">Id of the site which the deviation should be appended to.</param>
  90. public ActionResult AppendAppendixToSite(int siteId)
  91. {
  92. var site = _siteService.GetSiteById(siteId);
  93. var lastCustomNumber = site.Appendices
  94. .Max(d => StaticHelper.TryParseInt(d.CustomNumber));
  95. var appendixModel = new AppendixDataModel
  96. {
  97. Id = -1,
  98. CustomNumber = (lastCustomNumber + 1).ToString()
  99. };
  100. ViewData["relationType"] = "site";
  101. ViewData["relationId"] = siteId;
  102. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  103. }
  104. /// <summary>
  105. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  106. /// </summary>
  107. /// <param name="appendixModel">Appendix model to be saved.</param>
  108. [HttpPost, ValidateInput(false)]
  109. public ActionResult EditAppendix(AppendixDataModel appendixModel)
  110. {
  111. appendixModel.CategoryValueEntities =
  112. appendixModel.CategoryEntities
  113. .Select(r => JsonConvert.DeserializeObject<CategoryValueDataModel>(r))
  114. .ToList();
  115. for (int i = 0; i < appendixModel.CategoryValueEntities.Count; i++)
  116. appendixModel.CategoryValueEntities.ElementAt(i).Json = appendixModel.CategoryEntities.ElementAt(i);
  117. if (!ModelState.IsValid)
  118. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  119. var categoryValues = appendixModel.CategoryValueEntities
  120. .Select(r => r.ToCategoryValue())
  121. .ToList();
  122. if (appendixModel.Id == -1)
  123. {
  124. var appendix = appendixModel.ToAppendix();
  125. _appendixService.InsertAppendix(appendix);
  126. }
  127. else
  128. {
  129. var appendix = _appendixService.GetAppendixById(appendixModel.Id);
  130. appendix.CustomNumber = appendixModel.CustomNumber;
  131. appendix.Description = appendixModel.Description;
  132. appendix.Probability = appendixModel.Probability;
  133. appendix.OfferingNumber = appendixModel.OfferingNumber;
  134. appendix.OfferingDate = appendixModel.OfferingDate;
  135. appendix.NegotiationDate = appendixModel.NegotiationDate;
  136. appendix.NegotiationValue = appendixModel.NegotiationValue;
  137. appendix.ProtocolExists = appendixModel.ProtocolExists;
  138. appendix.OrderNumber = appendixModel.OrderNumber;
  139. appendix.OrderDate = appendixModel.OrderDate;
  140. appendix.OrderInvoiceCreated = appendixModel.OrderInvoiceCreated;
  141. appendix.Comment = appendixModel.Comment;
  142. appendix.SiteId = appendixModel.SiteId;
  143. _appendixService.UpdateAppendix(appendix);
  144. }
  145. return new JsonResult
  146. {
  147. Data = "success"
  148. };
  149. }
  150. /// <summary>
  151. /// Simple JSON result for deleting a specific appendix
  152. /// </summary>
  153. /// <param name="id">Appendix id.</param>
  154. [HttpPost]
  155. public ActionResult DeleteAppendix(int id)
  156. {
  157. var appendix = _appendixService.GetAppendixById(id);
  158. if (appendix != null)
  159. _appendixService.DeleteAppendix(appendix);
  160. return new JsonResult
  161. {
  162. Data = "success"
  163. };
  164. }
  165. #endregion
  166. #region Claims
  167. /// <summary>
  168. /// Basic claim view function
  169. /// </summary>
  170. [FunctionAuthorize(true, "Appendix-Claims")]
  171. public ActionResult ViewClaims()
  172. {
  173. return View("~/Views/Appendices/Claims.cshtml");
  174. }
  175. /// <summary>
  176. /// Get JSON data of specific claim
  177. /// </summary>
  178. /// <param name="claimType">Claim type.</param>
  179. /// <param name="id">Claim id.</param>
  180. public ActionResult GetClaim(string claimType, int id = -1)
  181. {
  182. switch (claimType.ToLower())
  183. {
  184. case "state":
  185. var state = _appendixService.GetStateById(id);
  186. if (state == null)
  187. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  188. else
  189. return new JsonResult
  190. {
  191. Data = JsonConvert.SerializeObject(state),
  192. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  193. };
  194. case "category":
  195. var category = _appendixService.GetCategoryById(id);
  196. if (category == null)
  197. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  198. else
  199. return new JsonResult
  200. {
  201. Data = JsonConvert.SerializeObject(category),
  202. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  203. };
  204. default:
  205. return new JsonResult { Data = "unknownClaimType", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  206. }
  207. }
  208. /// <summary>
  209. /// Callback result for claim grid
  210. /// </summary>
  211. /// <param name="claimType">Claim type.</param>
  212. public ActionResult PartialClaims(string claimType)
  213. {
  214. switch (claimType.ToLower())
  215. {
  216. case "state":
  217. return PartialView("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"]);
  218. case "category":
  219. return PartialView("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"]);
  220. default:
  221. return new EmptyResult();
  222. }
  223. }
  224. /// <summary>
  225. /// Partial edit for editing of existing or for new claim
  226. /// </summary>
  227. /// <param name="claimType">Claim type.</param>
  228. /// <param name="id">Id for existing claim, otherweise -1.</param>
  229. public ActionResult EditClaim(string claimType = "", int id = -1)
  230. {
  231. switch (claimType.ToLower())
  232. {
  233. case "state":
  234. var state = _appendixService.GetStateById(id);
  235. var stateModel = StateDataModel.FromState(state, true);
  236. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  237. case "category":
  238. var category = _appendixService.GetCategoryById(id);
  239. var categoryModel = CategoryDataModel.FromCategory(category, true);
  240. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  241. default:
  242. return new EmptyResult();
  243. }
  244. }
  245. /// <summary>
  246. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  247. /// </summary>
  248. /// <param name="stateModel">State model to be saved.</param>
  249. [HttpPost, ValidateInput(false)]
  250. public ActionResult EditState(StateDataModel stateModel)
  251. {
  252. if (!ModelState.IsValid)
  253. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  254. if (stateModel.Id == -1)
  255. {
  256. var claim = stateModel.ToState();
  257. _appendixService.InsertState(claim);
  258. }
  259. else
  260. {
  261. var state = _appendixService.GetStateById(stateModel.Id);
  262. state.Description = stateModel.Description;
  263. _appendixService.UpdateState(state);
  264. }
  265. return new JsonResult
  266. {
  267. Data = "success"
  268. };
  269. }
  270. /// <summary>
  271. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  272. /// </summary>
  273. /// <param name="categoryModel">Category model to be saved.</param>
  274. [HttpPost, ValidateInput(false)]
  275. public ActionResult EditCategory(CategoryDataModel categoryModel)
  276. {
  277. if (!ModelState.IsValid)
  278. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  279. if (categoryModel.Id == -1)
  280. {
  281. var claim = categoryModel.ToCategory();
  282. _appendixService.InsertCategory(claim);
  283. }
  284. else
  285. {
  286. var category = _appendixService.GetCategoryById(categoryModel.Id);
  287. category.Description = categoryModel.Description;
  288. _appendixService.UpdateCategory(category);
  289. }
  290. return new JsonResult
  291. {
  292. Data = "success"
  293. };
  294. }
  295. /// <summary>
  296. /// Simple JSON result for deleting a specific claim
  297. /// </summary>
  298. /// <param name="claimType">Claim type.</param>
  299. /// <param name="id">Claim id.</param>
  300. /// <param name="replaceId">Id of claim which deviations get in place of deleting claim.</param>
  301. [HttpPost]
  302. public ActionResult DeleteClaim(string claimType, int id, int replaceId)
  303. {
  304. switch (claimType.ToLower())
  305. {
  306. case "state":
  307. var state = _appendixService.GetStateById(id);
  308. var replaceState = _appendixService.GetStateById(replaceId);
  309. var stateAppendices = _appendixService.GetAppendicesByState(id);
  310. foreach (var appendix in stateAppendices)
  311. {
  312. appendix.StateId = replaceId;
  313. appendix.State = replaceState;
  314. _appendixService.UpdateAppendix(appendix);
  315. }
  316. if (state != null)
  317. _appendixService.DeleteState(state);
  318. break;
  319. default:
  320. return new EmptyResult();
  321. }
  322. return new JsonResult
  323. {
  324. Data = "success"
  325. };
  326. }
  327. #endregion
  328. }
  329. }