AppendixController.cs 22 KB

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