AppendixController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 Invoices
  210. /// <summary>
  211. /// Get JSON data of specific invoice
  212. /// </summary>
  213. /// <param name="id">Invoice id.</param>
  214. public ActionResult GetInvoice(int id = -1)
  215. {
  216. var invoice = _appendixService.GetInvoiceById(id);
  217. if (invoice == null)
  218. return new JsonResult
  219. {
  220. Data = "notFound",
  221. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  222. };
  223. var invoiceModel = InvoiceDataModel.FromInvoice(invoice, false);
  224. return new JsonResult
  225. {
  226. Data = JsonConvert.SerializeObject(invoiceModel),
  227. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  228. };
  229. }
  230. /// <summary>
  231. /// Callback result for Invoice list
  232. /// </summary>
  233. public ActionResult PartialInvoices(int appendixId = -1)
  234. {
  235. if (appendixId == -1)
  236. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
  237. var appendix = _appendixService.GetAppendixById(appendixId);
  238. if (appendix == null)
  239. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
  240. var appendixDataModel = AppendixDataModel.FromAppendix(appendix, false);
  241. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", appendixDataModel);
  242. }
  243. /// <summary>
  244. /// Partial edit for editing of existing or for new invoice
  245. /// </summary>
  246. /// <param name="id">Id for existing invoice, otherweise -1.</param>
  247. /// <param name="appendixId">Id of corresponding Appendix</param>
  248. public ActionResult EditInvoice(int appendixId, int id = -1)
  249. {
  250. var invoice = _appendixService.GetInvoiceById(id);
  251. var invoiceModel = InvoiceDataModel.FromInvoice(invoice, true);
  252. if (id == -1)
  253. invoiceModel.AppendixId = appendixId;
  254. return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
  255. }
  256. /// <summary>
  257. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  258. /// </summary>
  259. /// <param name="invoiceModel">Invoice model to be saved.</param>
  260. [HttpPost, ValidateInput(false)]
  261. public ActionResult EditInvoice(InvoiceDataModel invoiceModel)
  262. {
  263. if (!ModelState.IsValid)
  264. return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
  265. if (invoiceModel.Id == -1)
  266. {
  267. var invoice = invoiceModel.ToInvoice();
  268. _appendixService.InsertInvoice(invoice);
  269. }
  270. else
  271. {
  272. var invoice = _appendixService.GetInvoiceById(invoiceModel.Id);
  273. invoice.CustomNumber = invoiceModel.CustomNumber.Value;
  274. invoice.Date = invoiceModel.DateTime.Value;
  275. invoice.Value = invoiceModel.Value.Value;
  276. _appendixService.UpdateInvoice(invoice);
  277. }
  278. return new JsonResult
  279. {
  280. Data = "success"
  281. };
  282. }
  283. /// <summary>
  284. /// Simple JSON result for deleting a specific invoice
  285. /// </summary>
  286. /// <param name="id">Invoice id.</param>
  287. [HttpPost]
  288. public ActionResult DeleteInvoice(int id)
  289. {
  290. var invoice = _appendixService.GetInvoiceById(id);
  291. if (invoice != null)
  292. _appendixService.DeleteInvoice(invoice);
  293. return new JsonResult
  294. {
  295. Data = "success"
  296. };
  297. }
  298. #endregion
  299. #region Claims
  300. /// <summary>
  301. /// Basic claim view function
  302. /// </summary>
  303. [FunctionAuthorize(true, "Appendix-Claims")]
  304. public ActionResult ViewClaims()
  305. {
  306. return View("~/Views/Appendices/Claims.cshtml");
  307. }
  308. /// <summary>
  309. /// Get JSON data of specific claim
  310. /// </summary>
  311. /// <param name="claimType">Claim type.</param>
  312. /// <param name="id">Claim id.</param>
  313. public ActionResult GetClaim(string claimType, int id = -1)
  314. {
  315. switch (claimType.ToLower())
  316. {
  317. case "state":
  318. var state = _appendixService.GetStateById(id);
  319. if (state == null)
  320. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  321. else
  322. return new JsonResult
  323. {
  324. Data = JsonConvert.SerializeObject(state),
  325. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  326. };
  327. case "category":
  328. var category = _appendixService.GetCategoryById(id);
  329. if (category == null)
  330. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  331. else
  332. return new JsonResult
  333. {
  334. Data = JsonConvert.SerializeObject(category),
  335. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  336. };
  337. default:
  338. return new JsonResult { Data = "unknownClaimType", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  339. }
  340. }
  341. /// <summary>
  342. /// Callback result for claim grid
  343. /// </summary>
  344. /// <param name="claimType">Claim type.</param>
  345. public ActionResult PartialClaims(string claimType)
  346. {
  347. switch (claimType.ToLower())
  348. {
  349. case "state":
  350. return PartialView("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"]);
  351. case "category":
  352. return PartialView("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"]);
  353. default:
  354. return new EmptyResult();
  355. }
  356. }
  357. /// <summary>
  358. /// Partial edit for editing of existing or for new claim
  359. /// </summary>
  360. /// <param name="claimType">Claim type.</param>
  361. /// <param name="id">Id for existing claim, otherweise -1.</param>
  362. public ActionResult EditClaim(string claimType = "", int id = -1)
  363. {
  364. switch (claimType.ToLower())
  365. {
  366. case "state":
  367. var state = _appendixService.GetStateById(id);
  368. var stateModel = StateDataModel.FromState(state, true);
  369. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  370. case "category":
  371. var category = _appendixService.GetCategoryById(id);
  372. var categoryModel = CategoryDataModel.FromCategory(category, true);
  373. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  374. default:
  375. return new EmptyResult();
  376. }
  377. }
  378. /// <summary>
  379. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  380. /// </summary>
  381. /// <param name="stateModel">State model to be saved.</param>
  382. [HttpPost, ValidateInput(false)]
  383. public ActionResult EditState(StateDataModel stateModel)
  384. {
  385. if (!ModelState.IsValid)
  386. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  387. var allStates = _appendixService.GetAllStates();
  388. if (stateModel.IsDefault)
  389. {
  390. foreach (var state in allStates)
  391. {
  392. state.IsDefault = false;
  393. _appendixService.UpdateState(state);
  394. }
  395. }
  396. if (stateModel.Id == -1)
  397. {
  398. var claim = stateModel.ToState();
  399. _appendixService.InsertState(claim);
  400. }
  401. else
  402. {
  403. var state = _appendixService.GetStateById(stateModel.Id);
  404. state.Description = stateModel.Description;
  405. state.HexColor = stateModel.HexColor;
  406. state.IsDefault = stateModel.IsDefault;
  407. _appendixService.UpdateState(state);
  408. }
  409. return new JsonResult
  410. {
  411. Data = "success"
  412. };
  413. }
  414. /// <summary>
  415. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  416. /// </summary>
  417. /// <param name="categoryModel">Category model to be saved.</param>
  418. [HttpPost, ValidateInput(false)]
  419. public ActionResult EditCategory(CategoryDataModel categoryModel)
  420. {
  421. if (!ModelState.IsValid)
  422. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  423. if (categoryModel.Id == -1)
  424. {
  425. var claim = categoryModel.ToCategory();
  426. _appendixService.InsertCategory(claim);
  427. }
  428. else
  429. {
  430. var category = _appendixService.GetCategoryById(categoryModel.Id);
  431. category.Description = categoryModel.Description;
  432. _appendixService.UpdateCategory(category);
  433. }
  434. return new JsonResult
  435. {
  436. Data = "success"
  437. };
  438. }
  439. /// <summary>
  440. /// Simple JSON result for deleting a specific claim
  441. /// </summary>
  442. /// <param name="claimType">Claim type.</param>
  443. /// <param name="id">Claim id.</param>
  444. /// <param name="replaceId">Id of claim which deviations get in place of deleting claim.</param>
  445. [HttpPost]
  446. public ActionResult DeleteClaim(string claimType, int id, int replaceId)
  447. {
  448. switch (claimType.ToLower())
  449. {
  450. case "state":
  451. var state = _appendixService.GetStateById(id);
  452. var replaceState = _appendixService.GetStateById(replaceId);
  453. var stateAppendices = _appendixService.GetAppendicesByState(id);
  454. foreach (var appendix in stateAppendices)
  455. {
  456. appendix.StateId = replaceId;
  457. appendix.State = replaceState;
  458. _appendixService.UpdateAppendix(appendix);
  459. }
  460. if (state != null)
  461. _appendixService.DeleteState(state);
  462. break;
  463. case "category":
  464. var category = _appendixService.GetCategoryById(id);
  465. var replaceCategory = _appendixService.GetCategoryById(replaceId);
  466. var allAppendices = _appendixService.GetAllAppendices();
  467. foreach (var appendix in allAppendices)
  468. {
  469. foreach (var categoryValue in appendix.CategoryValues)
  470. {
  471. if (categoryValue.CategoryId == id)
  472. {
  473. categoryValue.Category = replaceCategory;
  474. categoryValue.CategoryId = replaceCategory.Id;
  475. }
  476. }
  477. _appendixService.UpdateAppendix(appendix);
  478. }
  479. if (category != null)
  480. _appendixService.DeleteCategory(category);
  481. break;
  482. default:
  483. return new EmptyResult();
  484. }
  485. return new JsonResult
  486. {
  487. Data = "success"
  488. };
  489. }
  490. #endregion
  491. }
  492. }