AppendixController.cs 17 KB

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