AppendixController.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. using DevExpress.Web.Mvc;
  2. using DevExpress.XtraPrinting;
  3. using DevExpress.XtraReports.UI;
  4. using GreenTree.Nachtragsmanagement.Core;
  5. using GreenTree.Nachtragsmanagement.Core.Authentication;
  6. using GreenTree.Nachtragsmanagement.Services.Appendix;
  7. using GreenTree.Nachtragsmanagement.Services.Deviation;
  8. using GreenTree.Nachtragsmanagement.Services.Logging;
  9. using GreenTree.Nachtragsmanagement.Services.Site;
  10. using GreenTree.Nachtragsmanagement.Web.Extensions;
  11. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  12. using GreenTree.Nachtragsmanagement.Web.Models.Appendix;
  13. using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
  14. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  15. using Newtonsoft.Json;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Drawing;
  19. using System.Linq;
  20. using System.Net.Mime;
  21. using System.Web;
  22. using System.Web.Mvc;
  23. using static GreenTree.Nachtragsmanagement.Web.Extensions.MVCxGridViewGeneratorHelper;
  24. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  25. {
  26. public class AppendixController : Controller
  27. {
  28. private readonly IDeviationService _deviationSerivce;
  29. private readonly IAppendixService _appendixService;
  30. private readonly ISiteService _siteService;
  31. private readonly IUserHelper _userHelper;
  32. private readonly ILogger _logger;
  33. public AppendixController(
  34. IDeviationService deviationService,
  35. IAppendixService appendixService,
  36. ISiteService siteService,
  37. IUserHelper userHelper,
  38. ILogger logger)
  39. {
  40. _deviationSerivce = deviationService;
  41. _appendixService = appendixService;
  42. _siteService = siteService;
  43. _userHelper = userHelper;
  44. _logger = logger;
  45. ViewData["AllCategories"] = _appendixService.GetAllCategories();
  46. ViewData["AllStates"] = _appendixService.GetAllStates();
  47. }
  48. #region Appendices
  49. /// <summary>
  50. /// Basic appendix view function
  51. /// </summary>
  52. [FunctionAuthorize(true, "Appendix-Appendices")]
  53. public ActionResult ViewAppendices()
  54. {
  55. var currentUser = _userHelper.FromCookies();
  56. var appendices = _appendixService.GetAllUserAssignedAppendices(currentUser);
  57. var appendixModels = appendices
  58. .Select(u => AppendixDataModel.FromAppendix(u, false))
  59. .ToList();
  60. return View("~/Views/Appendices/View.cshtml", appendixModels);
  61. }
  62. /// <summary>
  63. /// Get JSON data of specific appendix
  64. /// </summary>
  65. /// <param name="id">Appendix id.</param>
  66. public ActionResult GetAppendix(int id = -1)
  67. {
  68. var appendix = _appendixService.GetAppendixById(id);
  69. if (appendix == null)
  70. return new JsonResult
  71. {
  72. Data = "notFound",
  73. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  74. };
  75. var appendixModel = AppendixDataModel.FromAppendix(appendix, false);
  76. return new JsonResult
  77. {
  78. Data = JsonConvert.SerializeObject(appendixModel),
  79. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  80. };
  81. }
  82. /// <summary>
  83. /// Callback result for appendix grid
  84. /// </summary>
  85. public ActionResult PartialAppendices(int scrollHeight = -1)
  86. {
  87. var currentUser = _userHelper.FromCookies();
  88. var appendices = _appendixService.GetAllUserAssignedAppendices(currentUser);
  89. var appendixModels = appendices
  90. .Select(u => AppendixDataModel.FromAppendix(u, false))
  91. .ToList();
  92. ViewData["ScrollHeight"] = scrollHeight;
  93. return PartialView("~/Views/Appendices/_AppendixGridPartial.cshtml", appendixModels);
  94. }
  95. /// <summary>
  96. /// Export result for appendix grid
  97. /// </summary>
  98. [HttpPost]
  99. public ActionResult ExportPartialAppendices(string displayMode, string exportformat)
  100. {
  101. if (String.IsNullOrEmpty(displayMode))
  102. return new EmptyResult();
  103. var currentUser = _userHelper.FromCookies();
  104. var appendices = _appendixService.GetAllUserAssignedAppendices(currentUser);
  105. var appendixModels = appendices
  106. .Select(u => AppendixDataModel.FromAppendix(u, false))
  107. .ToList();
  108. var viewContext = new ViewContext();
  109. var viewPage = new ViewPage();
  110. var htmlHelper = new System.Web.Mvc.HtmlHelper(viewContext, viewPage);
  111. MVCxGridViewState gridViewState = (MVCxGridViewState)Session["AppendixGridViewState"];
  112. var settings = GridViewSettingsHelper.AppendixGridViewSettings(htmlHelper);
  113. if (gridViewState != null)
  114. {
  115. var generator = new MVCReportGeneratonHelper();
  116. generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
  117. generator.CustomizeGroupColumnSummary += new CustomizeColumnGroupSummaryEventHandler(generator_CustomizeGroupColumnSummary);
  118. generator.CustomizeTotalColumnSummary += new CustomizeColumnTotalSummaryEventHandler(generator_CustomizeTotalColumnSummary);
  119. var report = generator.GenerateMVCReport(gridViewState, appendixModels);
  120. if (displayMode == "popup")
  121. return PartialView("~/Views/Shared/_PrintPopupPartial.cshtml",
  122. new PrintGridModel(report, "Appendix", "ExportPartialAppendices", "devGridViewAppendix"));
  123. else if (displayMode == "callback")
  124. return PartialView("~/Views/Shared/_PrintDocumentViewerPartial.cshtml",
  125. new PrintGridModel(report, "Appendix", "ExportPartialAppendices", "devGridViewAppendix"));
  126. else if (displayMode == "export")
  127. {
  128. switch (exportformat.ToLower())
  129. {
  130. case "xlsx":
  131. settings.TotalSummary["OfferingValue"].DisplayFormat = "{0:c2}";
  132. settings.TotalSummary["NegotiationValue"].DisplayFormat = "{0:c2}";
  133. settings.TotalSummary["Description"].DisplayFormat = "Anzahl = {0:n0}";
  134. return GridViewExtension.ExportToXlsx(settings, appendixModels);
  135. case "xls":
  136. settings.TotalSummary["OfferingValue"].DisplayFormat = "{0:c2}";
  137. settings.TotalSummary["NegotiationValue"].DisplayFormat = "{0:c2}";
  138. settings.TotalSummary["Description"].DisplayFormat = "Anzahl = {0:n0}";
  139. return GridViewExtension.ExportToXls(settings, appendixModels);
  140. case "pdf":
  141. generator.WritePdfToResponse(Response, "Nachtragsliste.pdf", DispositionTypeNames.Attachment.ToString());
  142. break;
  143. }
  144. }
  145. return new EmptyResult();
  146. }
  147. else
  148. return new EmptyResult();
  149. }
  150. /// <summary>
  151. /// Customize created columns
  152. /// </summary>
  153. private void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
  154. {
  155. foreach (var column in e.ColumnsInfo)
  156. {
  157. if (column.FieldName == "CustomNumber") { column.ColumnWidth = 30; }
  158. if (column.FieldName == "SiteDescription") { column.ColumnWidth = 60; }
  159. if (column.FieldName == "DeviationDescription") { column.ColumnWidth = 50; }
  160. if (column.FieldName == "RelationOfferingToNegotiation") { column.ColumnWidth = 70; }
  161. if (column.FieldName == "RelationOfferingToDeviations") { column.ColumnWidth = 80; }
  162. if (column.FieldName == "StateDescription") { column.ColumnWidth = 60; }
  163. if (column.FieldName == "Comment") { column.IsVisible = false; column.IsDetail = true; }
  164. }
  165. }
  166. /// <summary>
  167. /// Customize column summary
  168. /// </summary>
  169. private void generator_CustomizeGroupColumnSummary(object source, ColumnSummaryCreationEventArgs e)
  170. {
  171. if (e.FieldName == "OfferingValue") { e.Summary.FormatString = "Angeb. ∑ = {0:c2}"; }
  172. if (e.FieldName == "NegotiationValue") { e.Summary.FormatString = "Verhand. ∑ = {0:c2}"; }
  173. if (e.FieldName == "Description") { e.Summary.FormatString = "Alle = {0:n0}"; }
  174. }
  175. /// <summary>
  176. /// Customize column summary
  177. /// </summary>
  178. private void generator_CustomizeTotalColumnSummary(object source, ColumnSummaryCreationEventArgs e)
  179. {
  180. if (e.FieldName == "OfferingValue") { e.Summary.FormatString = "{0:c2}"; }
  181. if (e.FieldName == "NegotiationValue") { e.Summary.FormatString = "{0:c2}"; }
  182. if (e.FieldName == "Description") { e.Summary.FormatString = "Alle = {0:n0}"; }
  183. }
  184. /// <summary>
  185. /// Partial edit for editing of existing or for new appendix
  186. /// </summary>
  187. /// <param name="id">Id for existing appendix, otherweise -1.</param>
  188. public ActionResult EditAppendix(int id = -1)
  189. {
  190. var appendix = _appendixService.GetAppendixById(id);
  191. var appendixModel = AppendixDataModel.FromAppendix(appendix, true);
  192. var defaultState = _appendixService.GetDefaultState();
  193. if (defaultState != null)
  194. ViewData["DefaultState"] = defaultState.Id;
  195. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  196. }
  197. /// <summary>
  198. /// Partial edit for creating a new deviation for a site
  199. /// </summary>
  200. /// <param name="siteId">Id of the site which the deviation should be appended to.</param>
  201. public ActionResult AppendAppendixToSite(int siteId)
  202. {
  203. var site = _siteService.GetSiteById(siteId);
  204. var lastCustomNumber = 0;
  205. if (site.Appendices.Any())
  206. lastCustomNumber = site.Appendices
  207. .Max(d => StaticHelper.TryParseInt(d.CustomNumber));
  208. var appendixModel = new AppendixDataModel
  209. {
  210. Id = -1,
  211. SiteId = siteId,
  212. CustomNumber = (lastCustomNumber + 1).ToString(),
  213. Percentage = (decimal)0.5
  214. };
  215. var defaultState = _appendixService.GetDefaultState();
  216. if (defaultState != null)
  217. ViewData["DefaultState"] = defaultState.Id;
  218. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  219. }
  220. /// <summary>
  221. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  222. /// </summary>
  223. /// <param name="appendixModel">Appendix model to be saved.</param>
  224. [HttpPost, ValidateInput(false)]
  225. public ActionResult EditAppendix(AppendixDataModel appendixModel)
  226. {
  227. try
  228. {
  229. appendixModel.CategoryValueEntities =
  230. appendixModel.CategoryEntities
  231. .Select(r => JsonConvert.DeserializeObject<CategoryValueDataModel>(r))
  232. .ToList();
  233. for (int i = 0; i < appendixModel.CategoryValueEntities.Count; i++)
  234. appendixModel.CategoryValueEntities.ElementAt(i).Json = appendixModel.CategoryEntities.ElementAt(i);
  235. appendixModel.PercentageValue = appendixModel.OfferingValue * appendixModel.Percentage;
  236. if (!ModelState.IsValid)
  237. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  238. var categoryValues = appendixModel.CategoryValueEntities
  239. .Select(r => r.ToCategoryValue())
  240. .ToList();
  241. if (appendixModel.Id == -1)
  242. {
  243. var appendix = appendixModel.ToAppendix();
  244. appendix.SetCategoryValues(categoryValues);
  245. _appendixService.InsertAppendix(appendix);
  246. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  247. }
  248. else
  249. {
  250. var appendix = _appendixService.GetAppendixById(appendixModel.Id);
  251. appendix.CustomNumber = appendixModel.CustomNumber;
  252. appendix.Description = appendixModel.Description;
  253. appendix.Percentage = appendixModel.Percentage;
  254. appendix.OfferingNumber = appendixModel.OfferingNumber;
  255. appendix.OfferingDate = appendixModel.OfferingDate;
  256. appendix.NegotiationDate = appendixModel.NegotiationDate;
  257. appendix.NegotiationValue = appendixModel.NegotiationValue;
  258. appendix.ProtocolExists = appendixModel.ProtocolExists;
  259. appendix.StateId = appendixModel.StateId;
  260. appendix.OrderNumber = appendixModel.OrderNumber;
  261. appendix.OrderDate = appendixModel.OrderDate;
  262. appendix.OrderInvoiceCreated = appendixModel.OrderInvoiceCreated;
  263. appendix.Comment = appendixModel.Comment;
  264. appendix.SiteId = appendixModel.SiteId;
  265. appendix.SetCategoryValues(categoryValues);
  266. _appendixService.UpdateAppendix(appendix);
  267. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  268. }
  269. return new JsonResult
  270. {
  271. Data = "success"
  272. };
  273. }
  274. catch (Exception ex)
  275. {
  276. _logger.Error("Fehler bei Speicherung eines Nachtrags.", ex, _userHelper.FromCookies());
  277. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  278. }
  279. }
  280. /// <summary>
  281. /// Simple JSON result for deleting a specific appendix
  282. /// </summary>
  283. /// <param name="id">Appendix id.</param>
  284. [HttpPost]
  285. public ActionResult DeleteAppendix(int id)
  286. {
  287. try
  288. {
  289. var appendix = _appendixService.GetAppendixById(id);
  290. if (appendix != null)
  291. _appendixService.DeleteAppendix(appendix);
  292. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  293. return new JsonResult
  294. {
  295. Data = "success"
  296. };
  297. }
  298. catch (Exception ex)
  299. {
  300. _logger.Error("Fehler bei Löschung eines Nachtrags.", ex, _userHelper.FromCookies());
  301. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  302. }
  303. }
  304. #endregion
  305. #region Invoices
  306. /// <summary>
  307. /// Get JSON data of specific invoice
  308. /// </summary>
  309. /// <param name="id">Invoice id.</param>
  310. public ActionResult GetInvoice(int id = -1)
  311. {
  312. var invoice = _appendixService.GetInvoiceById(id);
  313. if (invoice == null)
  314. return new JsonResult
  315. {
  316. Data = "notFound",
  317. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  318. };
  319. var invoiceModel = InvoiceDataModel.FromInvoice(invoice, false);
  320. return new JsonResult
  321. {
  322. Data = JsonConvert.SerializeObject(invoiceModel),
  323. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  324. };
  325. }
  326. /// <summary>
  327. /// Callback result for Invoice list
  328. /// </summary>
  329. public ActionResult PartialInvoices(int appendixId = -1)
  330. {
  331. if (appendixId == -1)
  332. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
  333. var appendix = _appendixService.GetAppendixById(appendixId);
  334. if (appendix == null)
  335. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
  336. var appendixDataModel = AppendixDataModel.FromAppendix(appendix, false);
  337. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", appendixDataModel);
  338. }
  339. /// <summary>
  340. /// Partial edit for editing of existing or for new invoice
  341. /// </summary>
  342. /// <param name="id">Id for existing invoice, otherweise -1.</param>
  343. /// <param name="appendixId">Id of corresponding Appendix</param>
  344. public ActionResult EditInvoice(int appendixId, int id = -1)
  345. {
  346. var invoice = _appendixService.GetInvoiceById(id);
  347. var invoiceModel = InvoiceDataModel.FromInvoice(invoice, true);
  348. if (id == -1)
  349. invoiceModel.AppendixId = appendixId;
  350. return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
  351. }
  352. /// <summary>
  353. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  354. /// </summary>
  355. /// <param name="invoiceModel">Invoice model to be saved.</param>
  356. [HttpPost, ValidateInput(false)]
  357. public ActionResult EditInvoice(InvoiceDataModel invoiceModel)
  358. {
  359. try
  360. {
  361. if (!ModelState.IsValid)
  362. return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
  363. if (invoiceModel.Id == -1)
  364. {
  365. var invoice = invoiceModel.ToInvoice();
  366. _appendixService.InsertInvoice(invoice);
  367. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  368. }
  369. else
  370. {
  371. var invoice = _appendixService.GetInvoiceById(invoiceModel.Id);
  372. invoice.CustomNumber = invoiceModel.CustomNumber.Value;
  373. invoice.Date = invoiceModel.DateTime.Value;
  374. invoice.Value = invoiceModel.Value.Value;
  375. _appendixService.UpdateInvoice(invoice);
  376. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  377. }
  378. return new JsonResult
  379. {
  380. Data = "success"
  381. };
  382. }
  383. catch (Exception ex)
  384. {
  385. _logger.Error("Fehler bei Speicherung einer Rechnung.", ex, _userHelper.FromCookies());
  386. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  387. }
  388. }
  389. /// <summary>
  390. /// Simple JSON result for deleting a specific invoice
  391. /// </summary>
  392. /// <param name="id">Invoice id.</param>
  393. [HttpPost]
  394. public ActionResult DeleteInvoice(int id)
  395. {
  396. try
  397. {
  398. var invoice = _appendixService.GetInvoiceById(id);
  399. if (invoice != null)
  400. _appendixService.DeleteInvoice(invoice);
  401. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  402. return new JsonResult
  403. {
  404. Data = "success"
  405. };
  406. }
  407. catch (Exception ex)
  408. {
  409. _logger.Error("Fehler bei Löschung einer Rechnung.", ex, _userHelper.FromCookies());
  410. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  411. }
  412. }
  413. #endregion
  414. #region Claims
  415. /// <summary>
  416. /// Basic claim view function
  417. /// </summary>
  418. [FunctionAuthorize(true, "Appendix-Claims")]
  419. public ActionResult ViewClaims()
  420. {
  421. return View("~/Views/Appendices/Claims.cshtml");
  422. }
  423. /// <summary>
  424. /// Get JSON data of specific claim
  425. /// </summary>
  426. /// <param name="claimType">Claim type.</param>
  427. /// <param name="id">Claim id.</param>
  428. public ActionResult GetClaim(string claimType, int id = -1)
  429. {
  430. switch (claimType.ToLower())
  431. {
  432. case "state":
  433. var state = _appendixService.GetStateById(id);
  434. if (state == null)
  435. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  436. else
  437. return new JsonResult
  438. {
  439. Data = JsonConvert.SerializeObject(state),
  440. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  441. };
  442. case "category":
  443. var category = _appendixService.GetCategoryById(id);
  444. if (category == null)
  445. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  446. else
  447. return new JsonResult
  448. {
  449. Data = JsonConvert.SerializeObject(category),
  450. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  451. };
  452. default:
  453. return new JsonResult { Data = "unknownClaimType", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  454. }
  455. }
  456. /// <summary>
  457. /// Callback result for claim grid
  458. /// </summary>
  459. /// <param name="claimType">Claim type.</param>
  460. public ActionResult PartialClaims(string claimType)
  461. {
  462. switch (claimType.ToLower())
  463. {
  464. case "state":
  465. return PartialView("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"]);
  466. case "category":
  467. return PartialView("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"]);
  468. default:
  469. return new EmptyResult();
  470. }
  471. }
  472. /// <summary>
  473. /// Partial edit for editing of existing or for new claim
  474. /// </summary>
  475. /// <param name="claimType">Claim type.</param>
  476. /// <param name="id">Id for existing claim, otherweise -1.</param>
  477. public ActionResult EditClaim(string claimType = "", int id = -1)
  478. {
  479. switch (claimType.ToLower())
  480. {
  481. case "state":
  482. var state = _appendixService.GetStateById(id);
  483. var stateModel = StateDataModel.FromState(state, true);
  484. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  485. case "category":
  486. var category = _appendixService.GetCategoryById(id);
  487. var categoryModel = CategoryDataModel.FromCategory(category, true);
  488. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  489. default:
  490. return new EmptyResult();
  491. }
  492. }
  493. /// <summary>
  494. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  495. /// </summary>
  496. /// <param name="stateModel">State model to be saved.</param>
  497. [HttpPost, ValidateInput(false)]
  498. public ActionResult EditState(StateDataModel stateModel)
  499. {
  500. try
  501. {
  502. if (!ModelState.IsValid)
  503. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  504. var allStates = _appendixService.GetAllStates();
  505. if (stateModel.IsDefault)
  506. {
  507. foreach (var state in allStates)
  508. {
  509. state.IsDefault = false;
  510. _appendixService.UpdateState(state);
  511. }
  512. }
  513. if (stateModel.IsFinish)
  514. {
  515. foreach (var state in allStates)
  516. {
  517. state.IsFinish = false;
  518. _appendixService.UpdateState(state);
  519. }
  520. }
  521. if (stateModel.Id == -1)
  522. {
  523. var claim = stateModel.ToState();
  524. _appendixService.InsertState(claim);
  525. _logger.Entity(claim, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  526. }
  527. else
  528. {
  529. var state = _appendixService.GetStateById(stateModel.Id);
  530. state.Description = stateModel.Description;
  531. state.HexColor = stateModel.HexColor;
  532. state.IsDefault = stateModel.IsDefault;
  533. state.IsFinish = stateModel.IsFinish;
  534. _appendixService.UpdateState(state);
  535. _logger.Entity(state, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  536. }
  537. return new JsonResult
  538. {
  539. Data = "success"
  540. };
  541. }
  542. catch (Exception ex)
  543. {
  544. _logger.Error("Fehler bei Speicherung eines NT-Status.", ex, _userHelper.FromCookies());
  545. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  546. }
  547. }
  548. /// <summary>
  549. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  550. /// </summary>
  551. /// <param name="categoryModel">Category model to be saved.</param>
  552. [HttpPost, ValidateInput(false)]
  553. public ActionResult EditCategory(CategoryDataModel categoryModel)
  554. {
  555. try
  556. {
  557. if (!ModelState.IsValid)
  558. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  559. if (categoryModel.Id == -1)
  560. {
  561. var claim = categoryModel.ToCategory();
  562. _appendixService.InsertCategory(claim);
  563. _logger.Entity(claim, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  564. }
  565. else
  566. {
  567. var category = _appendixService.GetCategoryById(categoryModel.Id);
  568. category.Description = categoryModel.Description;
  569. _appendixService.UpdateCategory(category);
  570. _logger.Entity(category, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  571. }
  572. return new JsonResult
  573. {
  574. Data = "success"
  575. };
  576. }
  577. catch (Exception ex)
  578. {
  579. _logger.Error("Fehler bei Löschung einer NT-Kategorie.", ex, _userHelper.FromCookies());
  580. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  581. }
  582. }
  583. /// <summary>
  584. /// Simple JSON result for deleting a specific claim
  585. /// </summary>
  586. /// <param name="claimType">Claim type.</param>
  587. /// <param name="id">Claim id.</param>
  588. /// <param name="replaceId">Id of claim which deviations get in place of deleting claim.</param>
  589. [HttpPost]
  590. public ActionResult DeleteClaim(string claimType, int id, int replaceId)
  591. {
  592. switch (claimType.ToLower())
  593. {
  594. case "state":
  595. try
  596. {
  597. var state = _appendixService.GetStateById(id);
  598. var replaceState = _appendixService.GetStateById(replaceId);
  599. var stateAppendices = _appendixService.GetAppendicesByState(id);
  600. foreach (var appendix in stateAppendices)
  601. {
  602. appendix.StateId = replaceId;
  603. appendix.State = replaceState;
  604. _appendixService.UpdateAppendix(appendix);
  605. }
  606. if (state != null)
  607. _appendixService.DeleteState(state);
  608. _logger.Entity(state, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  609. }
  610. catch (Exception ex)
  611. {
  612. _logger.Error("Fehler bei Löschung eines NT-Status.", ex, _userHelper.FromCookies());
  613. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  614. }
  615. break;
  616. case "category":
  617. try
  618. {
  619. var category = _appendixService.GetCategoryById(id);
  620. var replaceCategory = _appendixService.GetCategoryById(replaceId);
  621. var allAppendices = _appendixService.GetAllAppendices();
  622. foreach (var appendix in allAppendices)
  623. {
  624. foreach (var categoryValue in appendix.CategoryValues)
  625. {
  626. if (categoryValue.CategoryId == id)
  627. {
  628. categoryValue.Category = replaceCategory;
  629. categoryValue.CategoryId = replaceCategory.Id;
  630. }
  631. }
  632. _appendixService.UpdateAppendix(appendix);
  633. }
  634. if (category != null)
  635. _appendixService.DeleteCategory(category);
  636. _logger.Entity(category, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  637. }
  638. catch (Exception ex)
  639. {
  640. _logger.Error("Fehler bei Löschung einer NT-Kategorie.", ex, _userHelper.FromCookies());
  641. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  642. }
  643. break;
  644. default:
  645. return new EmptyResult();
  646. }
  647. return new JsonResult
  648. {
  649. Data = "success"
  650. };
  651. }
  652. #endregion
  653. }
  654. }