AppendixController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. var defaultState = _appendixService.GetDefaultState();
  85. if (defaultState != null)
  86. ViewData["DefaultState"] = defaultState.Id;
  87. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  88. }
  89. /// <summary>
  90. /// Partial edit for creating a new deviation for a site
  91. /// </summary>
  92. /// <param name="siteId">Id of the site which the deviation should be appended to.</param>
  93. public ActionResult AppendAppendixToSite(int siteId)
  94. {
  95. var site = _siteService.GetSiteById(siteId);
  96. var lastCustomNumber = site.Appendices
  97. .Max(d => StaticHelper.TryParseInt(d.CustomNumber));
  98. var appendixModel = new AppendixDataModel
  99. {
  100. Id = -1,
  101. SiteId = siteId,
  102. CustomNumber = (lastCustomNumber + 1).ToString(),
  103. Percentage = (decimal)0.5
  104. };
  105. var defaultState = _appendixService.GetDefaultState();
  106. if (defaultState != null)
  107. ViewData["DefaultState"] = defaultState.Id;
  108. //ViewData["relationType"] = "site";
  109. //ViewData["relationId"] = siteId;
  110. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  111. }
  112. /// <summary>
  113. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  114. /// </summary>
  115. /// <param name="appendixModel">Appendix model to be saved.</param>
  116. [HttpPost, ValidateInput(false)]
  117. public ActionResult EditAppendix(AppendixDataModel appendixModel)
  118. {
  119. appendixModel.CategoryValueEntities =
  120. appendixModel.CategoryEntities
  121. .Select(r => JsonConvert.DeserializeObject<CategoryValueDataModel>(r))
  122. .ToList();
  123. for (int i = 0; i < appendixModel.CategoryValueEntities.Count; i++)
  124. appendixModel.CategoryValueEntities.ElementAt(i).Json = appendixModel.CategoryEntities.ElementAt(i);
  125. appendixModel.PercentageValue = appendixModel.OfferingValue * appendixModel.Percentage;
  126. if (!ModelState.IsValid)
  127. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  128. var categoryValues = appendixModel.CategoryValueEntities
  129. .Select(r => r.ToCategoryValue())
  130. .ToList();
  131. if (appendixModel.Id == -1)
  132. {
  133. var appendix = appendixModel.ToAppendix();
  134. appendix.SetCategoryValues(categoryValues);
  135. _appendixService.InsertAppendix(appendix);
  136. }
  137. else
  138. {
  139. var appendix = _appendixService.GetAppendixById(appendixModel.Id);
  140. appendix.CustomNumber = appendixModel.CustomNumber;
  141. appendix.Description = appendixModel.Description;
  142. appendix.Percentage = appendixModel.Percentage;
  143. appendix.OfferingNumber = appendixModel.OfferingNumber;
  144. appendix.OfferingDate = appendixModel.OfferingDate;
  145. appendix.NegotiationDate = appendixModel.NegotiationDate;
  146. appendix.NegotiationValue = appendixModel.NegotiationValue;
  147. appendix.ProtocolExists = appendixModel.ProtocolExists;
  148. appendix.StateId = appendixModel.StateId;
  149. appendix.OrderNumber = appendixModel.OrderNumber;
  150. appendix.OrderDate = appendixModel.OrderDate;
  151. appendix.OrderInvoiceCreated = appendixModel.OrderInvoiceCreated;
  152. appendix.Comment = appendixModel.Comment;
  153. appendix.SiteId = appendixModel.SiteId;
  154. appendix.SetCategoryValues(categoryValues);
  155. _appendixService.UpdateAppendix(appendix);
  156. }
  157. return new JsonResult
  158. {
  159. Data = "success"
  160. };
  161. }
  162. /// <summary>
  163. /// Simple JSON result for deleting a specific appendix
  164. /// </summary>
  165. /// <param name="id">Appendix id.</param>
  166. [HttpPost]
  167. public ActionResult DeleteAppendix(int id)
  168. {
  169. var appendix = _appendixService.GetAppendixById(id);
  170. if (appendix != null)
  171. _appendixService.DeleteAppendix(appendix);
  172. return new JsonResult
  173. {
  174. Data = "success"
  175. };
  176. }
  177. #endregion
  178. #region Claims
  179. /// <summary>
  180. /// Basic claim view function
  181. /// </summary>
  182. [FunctionAuthorize(true, "Appendix-Claims")]
  183. public ActionResult ViewClaims()
  184. {
  185. return View("~/Views/Appendices/Claims.cshtml");
  186. }
  187. /// <summary>
  188. /// Get JSON data of specific claim
  189. /// </summary>
  190. /// <param name="claimType">Claim type.</param>
  191. /// <param name="id">Claim id.</param>
  192. public ActionResult GetClaim(string claimType, int id = -1)
  193. {
  194. switch (claimType.ToLower())
  195. {
  196. case "state":
  197. var state = _appendixService.GetStateById(id);
  198. if (state == null)
  199. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  200. else
  201. return new JsonResult
  202. {
  203. Data = JsonConvert.SerializeObject(state),
  204. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  205. };
  206. case "category":
  207. var category = _appendixService.GetCategoryById(id);
  208. if (category == null)
  209. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  210. else
  211. return new JsonResult
  212. {
  213. Data = JsonConvert.SerializeObject(category),
  214. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  215. };
  216. default:
  217. return new JsonResult { Data = "unknownClaimType", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  218. }
  219. }
  220. /// <summary>
  221. /// Callback result for claim grid
  222. /// </summary>
  223. /// <param name="claimType">Claim type.</param>
  224. public ActionResult PartialClaims(string claimType)
  225. {
  226. switch (claimType.ToLower())
  227. {
  228. case "state":
  229. return PartialView("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"]);
  230. case "category":
  231. return PartialView("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"]);
  232. default:
  233. return new EmptyResult();
  234. }
  235. }
  236. /// <summary>
  237. /// Partial edit for editing of existing or for new claim
  238. /// </summary>
  239. /// <param name="claimType">Claim type.</param>
  240. /// <param name="id">Id for existing claim, otherweise -1.</param>
  241. public ActionResult EditClaim(string claimType = "", int id = -1)
  242. {
  243. switch (claimType.ToLower())
  244. {
  245. case "state":
  246. var state = _appendixService.GetStateById(id);
  247. var stateModel = StateDataModel.FromState(state, true);
  248. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  249. case "category":
  250. var category = _appendixService.GetCategoryById(id);
  251. var categoryModel = CategoryDataModel.FromCategory(category, true);
  252. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  253. default:
  254. return new EmptyResult();
  255. }
  256. }
  257. /// <summary>
  258. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  259. /// </summary>
  260. /// <param name="stateModel">State model to be saved.</param>
  261. [HttpPost, ValidateInput(false)]
  262. public ActionResult EditState(StateDataModel stateModel)
  263. {
  264. if (!ModelState.IsValid)
  265. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  266. if (stateModel.Id == -1)
  267. {
  268. var claim = stateModel.ToState();
  269. _appendixService.InsertState(claim);
  270. }
  271. else
  272. {
  273. var state = _appendixService.GetStateById(stateModel.Id);
  274. state.Description = stateModel.Description;
  275. state.HexColor = stateModel.HexColor;
  276. state.IsDefault = stateModel.IsDefault;
  277. _appendixService.UpdateState(state);
  278. }
  279. return new JsonResult
  280. {
  281. Data = "success"
  282. };
  283. }
  284. /// <summary>
  285. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  286. /// </summary>
  287. /// <param name="categoryModel">Category model to be saved.</param>
  288. [HttpPost, ValidateInput(false)]
  289. public ActionResult EditCategory(CategoryDataModel categoryModel)
  290. {
  291. if (!ModelState.IsValid)
  292. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  293. if (categoryModel.Id == -1)
  294. {
  295. var claim = categoryModel.ToCategory();
  296. _appendixService.InsertCategory(claim);
  297. }
  298. else
  299. {
  300. var category = _appendixService.GetCategoryById(categoryModel.Id);
  301. category.Description = categoryModel.Description;
  302. _appendixService.UpdateCategory(category);
  303. }
  304. return new JsonResult
  305. {
  306. Data = "success"
  307. };
  308. }
  309. /// <summary>
  310. /// Simple JSON result for deleting a specific claim
  311. /// </summary>
  312. /// <param name="claimType">Claim type.</param>
  313. /// <param name="id">Claim id.</param>
  314. /// <param name="replaceId">Id of claim which deviations get in place of deleting claim.</param>
  315. [HttpPost]
  316. public ActionResult DeleteClaim(string claimType, int id, int replaceId)
  317. {
  318. switch (claimType.ToLower())
  319. {
  320. case "state":
  321. var state = _appendixService.GetStateById(id);
  322. var replaceState = _appendixService.GetStateById(replaceId);
  323. var stateAppendices = _appendixService.GetAppendicesByState(id);
  324. foreach (var appendix in stateAppendices)
  325. {
  326. appendix.StateId = replaceId;
  327. appendix.State = replaceState;
  328. _appendixService.UpdateAppendix(appendix);
  329. }
  330. if (state != null)
  331. _appendixService.DeleteState(state);
  332. break;
  333. case "category":
  334. var category = _appendixService.GetCategoryById(id);
  335. var replaceCategory = _appendixService.GetCategoryById(replaceId);
  336. var allAppendices = _appendixService.GetAllAppendices();
  337. foreach (var appendix in allAppendices)
  338. {
  339. foreach (var categoryValue in appendix.CategoryValues)
  340. {
  341. if (categoryValue.CategoryId == id)
  342. {
  343. categoryValue.Category = replaceCategory;
  344. categoryValue.CategoryId = replaceCategory.Id;
  345. }
  346. }
  347. _appendixService.UpdateAppendix(appendix);
  348. }
  349. if (category != null)
  350. _appendixService.DeleteCategory(category);
  351. break;
  352. default:
  353. return new EmptyResult();
  354. }
  355. return new JsonResult
  356. {
  357. Data = "success"
  358. };
  359. }
  360. #endregion
  361. }
  362. }