AppendixController.cs 32 KB

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