AppendixController.cs 37 KB

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