AppendixController.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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, string exportType)
  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, "Nachtragsliste");
  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. if (exportType != "print")
  129. {
  130. switch (exportformat.ToLower())
  131. {
  132. case "xlsx":
  133. settings.TotalSummary["OfferingValue"].DisplayFormat = "{0:c2}";
  134. settings.TotalSummary["NegotiationValue"].DisplayFormat = "{0:c2}";
  135. settings.TotalSummary["Description"].DisplayFormat = "Anzahl = {0:n0}";
  136. return GridViewExtension.ExportToXlsx(settings, appendixModels);
  137. case "xls":
  138. settings.TotalSummary["OfferingValue"].DisplayFormat = "{0:c2}";
  139. settings.TotalSummary["NegotiationValue"].DisplayFormat = "{0:c2}";
  140. settings.TotalSummary["Description"].DisplayFormat = "Anzahl = {0:n0}";
  141. return GridViewExtension.ExportToXls(settings, appendixModels);
  142. case "pdf":
  143. generator.WritePdfToResponse(Response, "Nachtragsliste.pdf", DispositionTypeNames.Attachment.ToString());
  144. break;
  145. }
  146. }
  147. }
  148. return new EmptyResult();
  149. }
  150. else
  151. return new EmptyResult();
  152. }
  153. /// <summary>
  154. /// Customize created columns
  155. /// </summary>
  156. private void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
  157. {
  158. foreach (var column in e.ColumnsInfo)
  159. {
  160. if (column.FieldName == "CustomNumber") { column.ColumnWidth = 30; }
  161. if (column.FieldName == "SiteDescription") { column.ColumnWidth = 60; }
  162. if (column.FieldName == "DeviationDescription") { column.ColumnWidth = 50; }
  163. if (column.FieldName == "RelationOfferingToNegotiation") { column.ColumnWidth = 70; }
  164. if (column.FieldName == "RelationOfferingToDeviations") { column.ColumnWidth = 80; }
  165. if (column.FieldName == "StateDescription") { column.ColumnWidth = 60; }
  166. if (column.FieldName == "Comment") { column.IsVisible = false; column.IsDetail = true; }
  167. }
  168. }
  169. /// <summary>
  170. /// Customize column summary
  171. /// </summary>
  172. private void generator_CustomizeGroupColumnSummary(object source, ColumnSummaryCreationEventArgs e)
  173. {
  174. if (e.FieldName == "OfferingValue") { e.Summary.FormatString = "Angeb. ∑ = {0:c2}"; }
  175. if (e.FieldName == "NegotiationValue") { e.Summary.FormatString = "Verhand. ∑ = {0:c2}"; }
  176. if (e.FieldName == "Description") { e.Summary.FormatString = "Alle = {0:n0}"; }
  177. }
  178. /// <summary>
  179. /// Customize column summary
  180. /// </summary>
  181. private void generator_CustomizeTotalColumnSummary(object source, ColumnSummaryCreationEventArgs e)
  182. {
  183. if (e.FieldName == "OfferingValue") { e.Summary.FormatString = "{0:c2}"; }
  184. if (e.FieldName == "NegotiationValue") { e.Summary.FormatString = "{0:c2}"; }
  185. if (e.FieldName == "Description") { e.Summary.FormatString = "Alle = {0:n0}"; }
  186. }
  187. /// <summary>
  188. /// Partial edit for editing of existing or for new appendix
  189. /// </summary>
  190. /// <param name="id">Id for existing appendix, otherweise -1.</param>
  191. public ActionResult EditAppendix(int id = -1)
  192. {
  193. var appendix = _appendixService.GetAppendixById(id);
  194. var appendixModel = AppendixDataModel.FromAppendix(appendix, true);
  195. var defaultState = _appendixService.GetDefaultState();
  196. if (defaultState != null)
  197. ViewData["DefaultState"] = defaultState.Id;
  198. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  199. }
  200. /// <summary>
  201. /// Partial edit for creating a new deviation for a site
  202. /// </summary>
  203. /// <param name="siteId">Id of the site which the deviation should be appended to.</param>
  204. public ActionResult AppendAppendixToSite(int siteId)
  205. {
  206. var site = _siteService.GetSiteById(siteId);
  207. var lastCustomNumber = 0;
  208. if (site.Appendices.Any())
  209. lastCustomNumber = site.Appendices
  210. .Max(d => StaticHelper.TryParseInt(d.CustomNumber));
  211. var appendixModel = new AppendixDataModel
  212. {
  213. Id = -1,
  214. SiteId = siteId,
  215. CustomNumber = (lastCustomNumber + 1).ToString(),
  216. Percentage = (decimal)0.5
  217. };
  218. var defaultState = _appendixService.GetDefaultState();
  219. if (defaultState != null)
  220. ViewData["DefaultState"] = defaultState.Id;
  221. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  222. }
  223. /// <summary>
  224. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  225. /// </summary>
  226. /// <param name="appendixModel">Appendix model to be saved.</param>
  227. [HttpPost, ValidateInput(false)]
  228. public ActionResult EditAppendix(AppendixDataModel appendixModel)
  229. {
  230. try
  231. {
  232. appendixModel.CategoryValueEntities =
  233. appendixModel.CategoryEntities
  234. .Select(r => JsonConvert.DeserializeObject<CategoryValueDataModel>(r))
  235. .ToList();
  236. for (int i = 0; i < appendixModel.CategoryValueEntities.Count; i++)
  237. appendixModel.CategoryValueEntities.ElementAt(i).Json = appendixModel.CategoryEntities.ElementAt(i);
  238. appendixModel.PercentageValue = appendixModel.OfferingValue * appendixModel.Percentage;
  239. if (!ModelState.IsValid)
  240. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  241. var categoryValues = appendixModel.CategoryValueEntities
  242. .Select(r => r.ToCategoryValue())
  243. .ToList();
  244. if (appendixModel.Id == -1)
  245. {
  246. var appendix = appendixModel.ToAppendix();
  247. appendix.SetCategoryValues(categoryValues);
  248. _appendixService.InsertAppendix(appendix);
  249. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  250. }
  251. else
  252. {
  253. var appendix = _appendixService.GetAppendixById(appendixModel.Id);
  254. appendix.CustomNumber = appendixModel.CustomNumber;
  255. appendix.Description = appendixModel.Description;
  256. appendix.Percentage = appendixModel.Percentage;
  257. appendix.OfferingNumber = appendixModel.OfferingNumber;
  258. appendix.OfferingDate = appendixModel.OfferingDate;
  259. appendix.NegotiationDate = appendixModel.NegotiationDate;
  260. appendix.NegotiationValue = appendixModel.NegotiationValue;
  261. appendix.ProtocolExists = appendixModel.ProtocolExists;
  262. appendix.StateId = appendixModel.StateId;
  263. appendix.OrderNumber = appendixModel.OrderNumber;
  264. appendix.OrderDate = appendixModel.OrderDate;
  265. appendix.OrderInvoiceCreated = appendixModel.OrderInvoiceCreated;
  266. appendix.Comment = appendixModel.Comment;
  267. appendix.SiteId = appendixModel.SiteId;
  268. appendix.SetCategoryValues(categoryValues);
  269. _appendixService.UpdateAppendix(appendix);
  270. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  271. }
  272. return new JsonResult
  273. {
  274. Data = "success"
  275. };
  276. }
  277. catch (Exception ex)
  278. {
  279. _logger.Error("Fehler bei Speicherung eines Nachtrags.", ex, _userHelper.FromCookies());
  280. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  281. }
  282. }
  283. /// <summary>
  284. /// Simple JSON result for deleting a specific appendix
  285. /// </summary>
  286. /// <param name="id">Appendix id.</param>
  287. [HttpPost]
  288. public ActionResult DeleteAppendix(int id)
  289. {
  290. try
  291. {
  292. var appendix = _appendixService.GetAppendixById(id);
  293. if (appendix != null)
  294. _appendixService.DeleteAppendix(appendix);
  295. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  296. return new JsonResult
  297. {
  298. Data = "success"
  299. };
  300. }
  301. catch (Exception ex)
  302. {
  303. _logger.Error("Fehler bei Löschung eines Nachtrags.", ex, _userHelper.FromCookies());
  304. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  305. }
  306. }
  307. #endregion
  308. #region Invoices
  309. /// <summary>
  310. /// Get JSON data of specific invoice
  311. /// </summary>
  312. /// <param name="id">Invoice id.</param>
  313. public ActionResult GetInvoice(int id = -1)
  314. {
  315. var invoice = _appendixService.GetInvoiceById(id);
  316. if (invoice == null)
  317. return new JsonResult
  318. {
  319. Data = "notFound",
  320. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  321. };
  322. var invoiceModel = InvoiceDataModel.FromInvoice(invoice, false);
  323. return new JsonResult
  324. {
  325. Data = JsonConvert.SerializeObject(invoiceModel),
  326. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  327. };
  328. }
  329. /// <summary>
  330. /// Callback result for Invoice list
  331. /// </summary>
  332. public ActionResult PartialInvoices(int appendixId = -1)
  333. {
  334. if (appendixId == -1)
  335. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
  336. var appendix = _appendixService.GetAppendixById(appendixId);
  337. if (appendix == null)
  338. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
  339. var appendixDataModel = AppendixDataModel.FromAppendix(appendix, false);
  340. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", appendixDataModel);
  341. }
  342. /// <summary>
  343. /// Partial edit for editing of existing or for new invoice
  344. /// </summary>
  345. /// <param name="id">Id for existing invoice, otherweise -1.</param>
  346. /// <param name="appendixId">Id of corresponding Appendix</param>
  347. public ActionResult EditInvoice(int appendixId, int id = -1)
  348. {
  349. var invoice = _appendixService.GetInvoiceById(id);
  350. var invoiceModel = InvoiceDataModel.FromInvoice(invoice, true);
  351. if (id == -1)
  352. invoiceModel.AppendixId = appendixId;
  353. return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
  354. }
  355. /// <summary>
  356. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  357. /// </summary>
  358. /// <param name="invoiceModel">Invoice model to be saved.</param>
  359. [HttpPost, ValidateInput(false)]
  360. public ActionResult EditInvoice(InvoiceDataModel invoiceModel)
  361. {
  362. try
  363. {
  364. if (!ModelState.IsValid)
  365. return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
  366. if (invoiceModel.Id == -1)
  367. {
  368. var invoice = invoiceModel.ToInvoice();
  369. _appendixService.InsertInvoice(invoice);
  370. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  371. }
  372. else
  373. {
  374. var invoice = _appendixService.GetInvoiceById(invoiceModel.Id);
  375. invoice.CustomNumber = invoiceModel.CustomNumber.Value;
  376. invoice.Date = invoiceModel.DateTime.Value;
  377. invoice.Value = invoiceModel.Value.Value;
  378. _appendixService.UpdateInvoice(invoice);
  379. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  380. }
  381. return new JsonResult
  382. {
  383. Data = "success"
  384. };
  385. }
  386. catch (Exception ex)
  387. {
  388. _logger.Error("Fehler bei Speicherung einer Rechnung.", ex, _userHelper.FromCookies());
  389. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  390. }
  391. }
  392. /// <summary>
  393. /// Simple JSON result for deleting a specific invoice
  394. /// </summary>
  395. /// <param name="id">Invoice id.</param>
  396. [HttpPost]
  397. public ActionResult DeleteInvoice(int id)
  398. {
  399. try
  400. {
  401. var invoice = _appendixService.GetInvoiceById(id);
  402. if (invoice != null)
  403. _appendixService.DeleteInvoice(invoice);
  404. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  405. return new JsonResult
  406. {
  407. Data = "success"
  408. };
  409. }
  410. catch (Exception ex)
  411. {
  412. _logger.Error("Fehler bei Löschung einer Rechnung.", ex, _userHelper.FromCookies());
  413. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  414. }
  415. }
  416. #endregion
  417. #region Claims
  418. /// <summary>
  419. /// Basic claim view function
  420. /// </summary>
  421. [FunctionAuthorize(true, "Appendix-Claims")]
  422. public ActionResult ViewClaims()
  423. {
  424. return View("~/Views/Appendices/Claims.cshtml");
  425. }
  426. /// <summary>
  427. /// Get JSON data of specific claim
  428. /// </summary>
  429. /// <param name="claimType">Claim type.</param>
  430. /// <param name="id">Claim id.</param>
  431. public ActionResult GetClaim(string claimType, int id = -1)
  432. {
  433. switch (claimType.ToLower())
  434. {
  435. case "state":
  436. var state = _appendixService.GetStateById(id);
  437. if (state == null)
  438. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  439. else
  440. return new JsonResult
  441. {
  442. Data = JsonConvert.SerializeObject(state),
  443. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  444. };
  445. case "category":
  446. var category = _appendixService.GetCategoryById(id);
  447. if (category == null)
  448. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  449. else
  450. return new JsonResult
  451. {
  452. Data = JsonConvert.SerializeObject(category),
  453. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  454. };
  455. default:
  456. return new JsonResult { Data = "unknownClaimType", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  457. }
  458. }
  459. /// <summary>
  460. /// Callback result for claim grid
  461. /// </summary>
  462. /// <param name="claimType">Claim type.</param>
  463. public ActionResult PartialClaims(string claimType)
  464. {
  465. switch (claimType.ToLower())
  466. {
  467. case "state":
  468. return PartialView("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"]);
  469. case "category":
  470. return PartialView("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"]);
  471. default:
  472. return new EmptyResult();
  473. }
  474. }
  475. /// <summary>
  476. /// Partial edit for editing of existing or for new claim
  477. /// </summary>
  478. /// <param name="claimType">Claim type.</param>
  479. /// <param name="id">Id for existing claim, otherweise -1.</param>
  480. public ActionResult EditClaim(string claimType = "", int id = -1)
  481. {
  482. switch (claimType.ToLower())
  483. {
  484. case "state":
  485. var state = _appendixService.GetStateById(id);
  486. var stateModel = StateDataModel.FromState(state, true);
  487. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  488. case "category":
  489. var category = _appendixService.GetCategoryById(id);
  490. var categoryModel = CategoryDataModel.FromCategory(category, true);
  491. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  492. default:
  493. return new EmptyResult();
  494. }
  495. }
  496. /// <summary>
  497. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  498. /// </summary>
  499. /// <param name="stateModel">State model to be saved.</param>
  500. [HttpPost, ValidateInput(false)]
  501. public ActionResult EditState(StateDataModel stateModel)
  502. {
  503. try
  504. {
  505. if (!ModelState.IsValid)
  506. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  507. var allStates = _appendixService.GetAllStates();
  508. if (stateModel.IsDefault)
  509. {
  510. foreach (var state in allStates)
  511. {
  512. state.IsDefault = false;
  513. _appendixService.UpdateState(state);
  514. }
  515. }
  516. if (stateModel.IsFinish)
  517. {
  518. foreach (var state in allStates)
  519. {
  520. state.IsFinish = false;
  521. _appendixService.UpdateState(state);
  522. }
  523. }
  524. if (stateModel.Id == -1)
  525. {
  526. var claim = stateModel.ToState();
  527. _appendixService.InsertState(claim);
  528. _logger.Entity(claim, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  529. }
  530. else
  531. {
  532. var state = _appendixService.GetStateById(stateModel.Id);
  533. state.Description = stateModel.Description;
  534. state.HexColor = stateModel.HexColor;
  535. state.IsDefault = stateModel.IsDefault;
  536. state.IsFinish = stateModel.IsFinish;
  537. _appendixService.UpdateState(state);
  538. _logger.Entity(state, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  539. }
  540. return new JsonResult
  541. {
  542. Data = "success"
  543. };
  544. }
  545. catch (Exception ex)
  546. {
  547. _logger.Error("Fehler bei Speicherung eines NT-Status.", ex, _userHelper.FromCookies());
  548. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  549. }
  550. }
  551. /// <summary>
  552. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  553. /// </summary>
  554. /// <param name="categoryModel">Category model to be saved.</param>
  555. [HttpPost, ValidateInput(false)]
  556. public ActionResult EditCategory(CategoryDataModel categoryModel)
  557. {
  558. try
  559. {
  560. if (!ModelState.IsValid)
  561. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  562. if (categoryModel.Id == -1)
  563. {
  564. var claim = categoryModel.ToCategory();
  565. _appendixService.InsertCategory(claim);
  566. _logger.Entity(claim, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  567. }
  568. else
  569. {
  570. var category = _appendixService.GetCategoryById(categoryModel.Id);
  571. category.Description = categoryModel.Description;
  572. _appendixService.UpdateCategory(category);
  573. _logger.Entity(category, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  574. }
  575. return new JsonResult
  576. {
  577. Data = "success"
  578. };
  579. }
  580. catch (Exception ex)
  581. {
  582. _logger.Error("Fehler bei Löschung einer NT-Kategorie.", ex, _userHelper.FromCookies());
  583. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  584. }
  585. }
  586. /// <summary>
  587. /// Simple JSON result for deleting a specific claim
  588. /// </summary>
  589. /// <param name="claimType">Claim type.</param>
  590. /// <param name="id">Claim id.</param>
  591. /// <param name="replaceId">Id of claim which deviations get in place of deleting claim.</param>
  592. [HttpPost]
  593. public ActionResult DeleteClaim(string claimType, int id, int replaceId)
  594. {
  595. switch (claimType.ToLower())
  596. {
  597. case "state":
  598. try
  599. {
  600. var state = _appendixService.GetStateById(id);
  601. var replaceState = _appendixService.GetStateById(replaceId);
  602. var stateAppendices = _appendixService.GetAppendicesByState(id);
  603. foreach (var appendix in stateAppendices)
  604. {
  605. appendix.StateId = replaceId;
  606. appendix.State = replaceState;
  607. _appendixService.UpdateAppendix(appendix);
  608. }
  609. if (state != null)
  610. _appendixService.DeleteState(state);
  611. _logger.Entity(state, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  612. }
  613. catch (Exception ex)
  614. {
  615. _logger.Error("Fehler bei Löschung eines NT-Status.", ex, _userHelper.FromCookies());
  616. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  617. }
  618. break;
  619. case "category":
  620. try
  621. {
  622. var category = _appendixService.GetCategoryById(id);
  623. var replaceCategory = _appendixService.GetCategoryById(replaceId);
  624. var allAppendices = _appendixService.GetAllAppendices();
  625. foreach (var appendix in allAppendices)
  626. {
  627. foreach (var categoryValue in appendix.CategoryValues)
  628. {
  629. if (categoryValue.CategoryId == id)
  630. {
  631. categoryValue.Category = replaceCategory;
  632. categoryValue.CategoryId = replaceCategory.Id;
  633. }
  634. }
  635. _appendixService.UpdateAppendix(appendix);
  636. }
  637. if (category != null)
  638. _appendixService.DeleteCategory(category);
  639. _logger.Entity(category, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  640. }
  641. catch (Exception ex)
  642. {
  643. _logger.Error("Fehler bei Löschung einer NT-Kategorie.", ex, _userHelper.FromCookies());
  644. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  645. }
  646. break;
  647. default:
  648. return new EmptyResult();
  649. }
  650. return new JsonResult
  651. {
  652. Data = "success"
  653. };
  654. }
  655. #endregion
  656. }
  657. }