AppendixController.cs 37 KB

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