AppendixController.cs 29 KB

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