AppendixController.cs 17 KB

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