AppendixController.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. accumulatedCustomSummaryNegotiationValue += e.CalculatedValues.OfType<decimal>().Sum();
  188. e.Result = accumulatedCustomSummaryNegotiationValue;
  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 == "UserDescription") { column.IsVisible = false; column.IsDetail = true; }
  228. if (column.FieldName == "OfferingValue") { column.ColumnWidth = 80; }
  229. if (column.FieldName == "NegotiationValue") { column.ColumnWidth = 80; }
  230. if (column.FieldName == "DeviationDescription") { column.ColumnWidth = 40; }
  231. if (column.FieldName == "RelationOfferingToNegotiation") { column.ColumnWidth = 50; }
  232. if (column.FieldName == "RelationOfferingToDeviations") { column.ColumnWidth = 50; }
  233. if (column.FieldName == "StateDescription") { column.ColumnWidth = 70; }
  234. if (column.FieldName == "CategoryValuesDescription") { column.IsVisible = false; column.IsDetail = true; }
  235. if (column.FieldName == "OrderInvoiceCreatedDescription") { column.ColumnWidth = 45; }
  236. if (column.FieldName == "Comment") { column.IsVisible = false; column.IsDetail = true; }
  237. }
  238. }
  239. /// <summary>
  240. /// Customize column summary
  241. /// </summary>
  242. private void generator_CustomizeGroupColumnSummary(object source, ColumnSummaryCreationEventArgs e)
  243. {
  244. if (e.FieldName == "OfferingValue") { e.Summary.FormatString = "Angeb. ∑ = {0:c2}"; }
  245. if (e.FieldName == "NegotiationValue") { e.Summary.FormatString = "Verhand. ∑ = {0:c2}"; }
  246. if (e.FieldName == "Description") { e.Summary.FormatString = "Alle = {0:n0}"; }
  247. }
  248. /// <summary>
  249. /// Customize column summary
  250. /// </summary>
  251. private void generator_CustomizeTotalColumnSummary(object source, ColumnSummaryCreationEventArgs e)
  252. {
  253. if (e.FieldName == "OfferingValue") { e.Summary.FormatString = "{0:c2}"; }
  254. if (e.FieldName == "NegotiationValue") { e.Summary.FormatString = "{0:c2}"; }
  255. if (e.FieldName == "Description") { e.Summary.FormatString = "Alle = {0:n0}"; }
  256. }
  257. /// <summary>
  258. /// Partial edit for editing of existing or for new appendix
  259. /// </summary>
  260. /// <param name="id">Id for existing appendix, otherweise -1.</param>
  261. public ActionResult EditAppendix(int id = -1)
  262. {
  263. var appendix = _appendixService.GetAppendixById(id);
  264. var appendixModel = AppendixDataModel.FromAppendix(appendix, true);
  265. var defaultState = _appendixService.GetDefaultState();
  266. if (defaultState != null)
  267. ViewData["DefaultState"] = defaultState.Id;
  268. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  269. }
  270. /// <summary>
  271. /// Partial edit for viewing an existing appendix
  272. /// </summary>
  273. /// <param name="id">Id for existing appendix, otherweise -1.</param>
  274. public ActionResult ViewAppendix(int id = -1)
  275. {
  276. if (id == -1)
  277. return new EmptyResult();
  278. var appendix = _appendixService.GetAppendixById(id);
  279. var appendixModel = AppendixDataModel.FromAppendix(appendix, true);
  280. var deviationModels = appendix.Deviations
  281. .Select(d => DeviationDataModel.FromDeviation(d, false, _configurationService));
  282. ViewData["AppendixDeviations"] = deviationModels;
  283. return PartialView("~/Views/Appendices/_AppendixViewPartial.cshtml", appendixModel);
  284. }
  285. /// <summary>
  286. /// Partial edit for creating a new deviation for a site
  287. /// </summary>
  288. /// <param name="siteId">Id of the site which the deviation should be appended to.</param>
  289. public ActionResult AppendAppendixToSite(int siteId)
  290. {
  291. var site = _siteService.GetSiteById(siteId);
  292. var lastCustomNumber = 0;
  293. if (site.Appendices.Any())
  294. lastCustomNumber = site.Appendices
  295. .Max(d => StaticHelper.TryParseInt(d.CustomNumber));
  296. var appendixModel = new AppendixDataModel
  297. {
  298. Id = -1,
  299. SiteId = siteId,
  300. CustomNumber = (lastCustomNumber + 1).ToString(),
  301. Percentage = (decimal)1.0
  302. };
  303. var defaultState = _appendixService.GetDefaultState();
  304. if (defaultState != null)
  305. ViewData["DefaultState"] = defaultState.Id;
  306. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  307. }
  308. /// <summary>
  309. /// Gets if an appendix OrderInvoiceCreated field and provides an edit form
  310. /// </summary>
  311. /// <param name="id">The entity id.</param>
  312. public ActionResult EditOrderInvoiceCreated(int id)
  313. {
  314. var appendix = _appendixService.GetAppendixById(id);
  315. if (appendix == null)
  316. return new EmptyResult();
  317. var model = new OrderInvoiceCreatedDataModel
  318. {
  319. AppendixId = appendix.Id,
  320. OrderInvoiceCreated = appendix.OrderInvoiceCreated
  321. };
  322. return PartialView("~/Views/Appendices/_EditOrderInvoiceCreatedPartial.cshtml", model);
  323. }
  324. /// <summary>
  325. /// Sets the OrderInvoiceCreated status for a given appendix
  326. /// </summary>
  327. [HttpPost, ValidateInput(false)]
  328. public ActionResult EditOrderInvoiceCreated(OrderInvoiceCreatedDataModel model)
  329. {
  330. if (model == null)
  331. return new EmptyResult();
  332. var appendix = _appendixService.GetAppendixById(model.AppendixId);
  333. if (appendix == null)
  334. return new EmptyResult();
  335. appendix.OrderInvoiceCreated = model.OrderInvoiceCreated;
  336. _appendixService.UpdateAppendix(appendix);
  337. return new JsonResult
  338. {
  339. Data = "success"
  340. };
  341. }
  342. /// <summary>
  343. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  344. /// </summary>
  345. /// <param name="appendixModel">Appendix model to be saved.</param>
  346. [HttpPost, ValidateInput(false)]
  347. public ActionResult EditAppendix(AppendixDataModel appendixModel)
  348. {
  349. try
  350. {
  351. appendixModel.CategoryValueEntities =
  352. appendixModel.CategoryEntities
  353. .Select(r => JsonConvert.DeserializeObject<CategoryValueDataModel>(r))
  354. .ToList();
  355. for (int i = 0; i < appendixModel.CategoryValueEntities.Count; i++)
  356. appendixModel.CategoryValueEntities.ElementAt(i).Json = appendixModel.CategoryEntities.ElementAt(i);
  357. appendixModel.PercentageValue = appendixModel.OfferingValue * appendixModel.Percentage;
  358. if (!ModelState.IsValid)
  359. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  360. var categoryValues = appendixModel.CategoryValueEntities
  361. .Select(r => r.ToCategoryValue())
  362. .ToList();
  363. if (appendixModel.Id == -1)
  364. {
  365. var appendix = appendixModel.ToAppendix();
  366. appendix.SetCategoryValues(categoryValues);
  367. _appendixService.InsertAppendix(appendix);
  368. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  369. }
  370. else
  371. {
  372. var appendix = _appendixService.GetAppendixById(appendixModel.Id);
  373. appendix.CustomNumber = appendixModel.CustomNumber;
  374. appendix.Description = appendixModel.Description;
  375. appendix.Percentage = appendixModel.Percentage;
  376. appendix.OfferingNumber = appendixModel.OfferingNumber;
  377. appendix.OfferingDate = appendixModel.OfferingDate;
  378. appendix.NegotiationDate = appendixModel.NegotiationDate;
  379. appendix.NegotiationValue = appendixModel.NegotiationValue;
  380. appendix.ProtocolExists = appendixModel.ProtocolExists;
  381. appendix.StateId = appendixModel.StateId;
  382. appendix.OrderNumber = appendixModel.OrderNumber;
  383. appendix.OrderDate = appendixModel.OrderDate;
  384. appendix.OrderInvoiceCreated = appendixModel.OrderInvoiceCreated;
  385. appendix.Comment = appendixModel.Comment;
  386. appendix.SiteId = appendixModel.SiteId;
  387. appendix.SetCategoryValues(categoryValues);
  388. _appendixService.UpdateAppendix(appendix);
  389. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  390. }
  391. return new JsonResult
  392. {
  393. Data = "success"
  394. };
  395. }
  396. catch (Exception ex)
  397. {
  398. _logger.Error("Fehler bei Speicherung eines Nachtrags.", ex, _userHelper.FromCookies());
  399. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  400. }
  401. }
  402. /// <summary>
  403. /// Simple JSON result for deleting a specific appendix
  404. /// </summary>
  405. /// <param name="id">Appendix id.</param>
  406. [HttpPost]
  407. public ActionResult DeleteAppendix(int id)
  408. {
  409. try
  410. {
  411. var appendix = _appendixService.GetAppendixById(id);
  412. if (appendix != null)
  413. _appendixService.DeleteAppendix(appendix);
  414. _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  415. return new JsonResult
  416. {
  417. Data = "success"
  418. };
  419. }
  420. catch (Exception ex)
  421. {
  422. _logger.Error("Fehler bei Löschung eines Nachtrags.", ex, _userHelper.FromCookies());
  423. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  424. }
  425. }
  426. #endregion
  427. #region Invoices
  428. /// <summary>
  429. /// Get JSON data of specific invoice
  430. /// </summary>
  431. /// <param name="id">Invoice id.</param>
  432. public ActionResult GetInvoice(int id = -1)
  433. {
  434. var invoice = _appendixService.GetInvoiceById(id);
  435. if (invoice == null)
  436. return new JsonResult
  437. {
  438. Data = "notFound",
  439. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  440. };
  441. var invoiceModel = InvoiceDataModel.FromInvoice(invoice, false);
  442. return new JsonResult
  443. {
  444. Data = JsonConvert.SerializeObject(invoiceModel),
  445. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  446. };
  447. }
  448. /// <summary>
  449. /// Callback result for Invoice list
  450. /// </summary>
  451. public ActionResult PartialInvoices(int appendixId = -1)
  452. {
  453. if (appendixId == -1)
  454. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
  455. var appendix = _appendixService.GetAppendixById(appendixId);
  456. if (appendix == null)
  457. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
  458. var appendixDataModel = AppendixDataModel.FromAppendix(appendix, false);
  459. return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", appendixDataModel);
  460. }
  461. /// <summary>
  462. /// Partial edit for editing of existing or for new invoice
  463. /// </summary>
  464. /// <param name="id">Id for existing invoice, otherweise -1.</param>
  465. /// <param name="appendixId">Id of corresponding Appendix</param>
  466. public ActionResult EditInvoice(int appendixId, int id = -1)
  467. {
  468. var invoice = _appendixService.GetInvoiceById(id);
  469. var invoiceModel = InvoiceDataModel.FromInvoice(invoice, true);
  470. if (id == -1)
  471. invoiceModel.AppendixId = appendixId;
  472. return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
  473. }
  474. /// <summary>
  475. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  476. /// </summary>
  477. /// <param name="invoiceModel">Invoice model to be saved.</param>
  478. [HttpPost, ValidateInput(false)]
  479. public ActionResult EditInvoice(InvoiceDataModel invoiceModel)
  480. {
  481. try
  482. {
  483. if (!ModelState.IsValid)
  484. return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
  485. if (invoiceModel.Id == -1)
  486. {
  487. var invoice = invoiceModel.ToInvoice();
  488. _appendixService.InsertInvoice(invoice);
  489. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  490. }
  491. else
  492. {
  493. var invoice = _appendixService.GetInvoiceById(invoiceModel.Id);
  494. invoice.CustomNumber = invoiceModel.CustomNumber.Value;
  495. invoice.Date = invoiceModel.DateTime.Value;
  496. invoice.Value = invoiceModel.Value.Value;
  497. _appendixService.UpdateInvoice(invoice);
  498. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  499. }
  500. return new JsonResult
  501. {
  502. Data = "success"
  503. };
  504. }
  505. catch (Exception ex)
  506. {
  507. _logger.Error("Fehler bei Speicherung einer Rechnung.", ex, _userHelper.FromCookies());
  508. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  509. }
  510. }
  511. /// <summary>
  512. /// Simple JSON result for deleting a specific invoice
  513. /// </summary>
  514. /// <param name="id">Invoice id.</param>
  515. [HttpPost]
  516. public ActionResult DeleteInvoice(int id)
  517. {
  518. try
  519. {
  520. var invoice = _appendixService.GetInvoiceById(id);
  521. if (invoice != null)
  522. _appendixService.DeleteInvoice(invoice);
  523. _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  524. return new JsonResult
  525. {
  526. Data = "success"
  527. };
  528. }
  529. catch (Exception ex)
  530. {
  531. _logger.Error("Fehler bei Löschung einer Rechnung.", ex, _userHelper.FromCookies());
  532. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  533. }
  534. }
  535. #endregion
  536. #region Claims
  537. /// <summary>
  538. /// Basic claim view function
  539. /// </summary>
  540. [FunctionAuthorize(true, "Appendix-Claims")]
  541. public ActionResult ViewClaims()
  542. {
  543. return View("~/Views/Appendices/Claims.cshtml");
  544. }
  545. /// <summary>
  546. /// Get JSON data of specific claim
  547. /// </summary>
  548. /// <param name="claimType">Claim type.</param>
  549. /// <param name="id">Claim id.</param>
  550. public ActionResult GetClaim(string claimType, int id = -1)
  551. {
  552. switch (claimType.ToLower())
  553. {
  554. case "state":
  555. var state = _appendixService.GetStateById(id);
  556. if (state == null)
  557. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  558. else
  559. return new JsonResult
  560. {
  561. Data = JsonConvert.SerializeObject(state),
  562. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  563. };
  564. case "category":
  565. var category = _appendixService.GetCategoryById(id);
  566. if (category == null)
  567. return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  568. else
  569. return new JsonResult
  570. {
  571. Data = JsonConvert.SerializeObject(category),
  572. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  573. };
  574. default:
  575. return new JsonResult { Data = "unknownClaimType", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  576. }
  577. }
  578. /// <summary>
  579. /// Callback result for claim grid
  580. /// </summary>
  581. /// <param name="claimType">Claim type.</param>
  582. public ActionResult PartialClaims(string claimType)
  583. {
  584. switch (claimType.ToLower())
  585. {
  586. case "state":
  587. return PartialView("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"]);
  588. case "category":
  589. return PartialView("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"]);
  590. default:
  591. return new EmptyResult();
  592. }
  593. }
  594. /// <summary>
  595. /// Partial edit for editing of existing or for new claim
  596. /// </summary>
  597. /// <param name="claimType">Claim type.</param>
  598. /// <param name="id">Id for existing claim, otherweise -1.</param>
  599. public ActionResult EditClaim(string claimType = "", int id = -1)
  600. {
  601. switch (claimType.ToLower())
  602. {
  603. case "state":
  604. var state = _appendixService.GetStateById(id);
  605. var stateModel = StateDataModel.FromState(state, true);
  606. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  607. case "category":
  608. var category = _appendixService.GetCategoryById(id);
  609. var categoryModel = CategoryDataModel.FromCategory(category, true);
  610. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  611. default:
  612. return new EmptyResult();
  613. }
  614. }
  615. /// <summary>
  616. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  617. /// </summary>
  618. /// <param name="stateModel">State model to be saved.</param>
  619. [HttpPost, ValidateInput(false)]
  620. public ActionResult EditState(StateDataModel stateModel)
  621. {
  622. try
  623. {
  624. if (!ModelState.IsValid)
  625. return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
  626. var allStates = _appendixService.GetAllStates();
  627. if (stateModel.IsDefault)
  628. {
  629. foreach (var state in allStates)
  630. {
  631. state.IsDefault = false;
  632. _appendixService.UpdateState(state);
  633. }
  634. }
  635. if (stateModel.IsFinish)
  636. {
  637. foreach (var state in allStates)
  638. {
  639. state.IsFinish = false;
  640. _appendixService.UpdateState(state);
  641. }
  642. }
  643. if (stateModel.Id == -1)
  644. {
  645. var claim = stateModel.ToState();
  646. _appendixService.InsertState(claim);
  647. _logger.Entity(claim, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  648. }
  649. else
  650. {
  651. var state = _appendixService.GetStateById(stateModel.Id);
  652. state.Description = stateModel.Description;
  653. state.HexColor = stateModel.HexColor;
  654. state.IsDefault = stateModel.IsDefault;
  655. state.IsFinish = stateModel.IsFinish;
  656. _appendixService.UpdateState(state);
  657. _logger.Entity(state, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  658. }
  659. return new JsonResult
  660. {
  661. Data = "success"
  662. };
  663. }
  664. catch (Exception ex)
  665. {
  666. _logger.Error("Fehler bei Speicherung eines NT-Status.", ex, _userHelper.FromCookies());
  667. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  668. }
  669. }
  670. /// <summary>
  671. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  672. /// </summary>
  673. /// <param name="categoryModel">Category model to be saved.</param>
  674. [HttpPost, ValidateInput(false)]
  675. public ActionResult EditCategory(CategoryDataModel categoryModel)
  676. {
  677. try
  678. {
  679. if (!ModelState.IsValid)
  680. return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
  681. if (categoryModel.Id == -1)
  682. {
  683. var claim = categoryModel.ToCategory();
  684. _appendixService.InsertCategory(claim);
  685. _logger.Entity(claim, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  686. }
  687. else
  688. {
  689. var category = _appendixService.GetCategoryById(categoryModel.Id);
  690. category.Description = categoryModel.Description;
  691. _appendixService.UpdateCategory(category);
  692. _logger.Entity(category, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  693. }
  694. return new JsonResult
  695. {
  696. Data = "success"
  697. };
  698. }
  699. catch (Exception ex)
  700. {
  701. _logger.Error("Fehler bei Löschung einer NT-Kategorie.", ex, _userHelper.FromCookies());
  702. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  703. }
  704. }
  705. /// <summary>
  706. /// Simple JSON result for deleting a specific claim
  707. /// </summary>
  708. /// <param name="claimType">Claim type.</param>
  709. /// <param name="id">Claim id.</param>
  710. /// <param name="replaceId">Id of claim which deviations get in place of deleting claim.</param>
  711. [HttpPost]
  712. public ActionResult DeleteClaim(string claimType, int id, int replaceId)
  713. {
  714. switch (claimType.ToLower())
  715. {
  716. case "state":
  717. try
  718. {
  719. var state = _appendixService.GetStateById(id);
  720. var replaceState = _appendixService.GetStateById(replaceId);
  721. var stateAppendices = _appendixService.GetAppendicesByState(id);
  722. foreach (var appendix in stateAppendices)
  723. {
  724. appendix.StateId = replaceId;
  725. appendix.State = replaceState;
  726. _appendixService.UpdateAppendix(appendix);
  727. }
  728. if (state != null)
  729. _appendixService.DeleteState(state);
  730. _logger.Entity(state, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  731. }
  732. catch (Exception ex)
  733. {
  734. _logger.Error("Fehler bei Löschung eines NT-Status.", ex, _userHelper.FromCookies());
  735. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  736. }
  737. break;
  738. case "category":
  739. try
  740. {
  741. var category = _appendixService.GetCategoryById(id);
  742. var replaceCategory = _appendixService.GetCategoryById(replaceId);
  743. var allAppendices = _appendixService.GetAllAppendices();
  744. foreach (var appendix in allAppendices)
  745. {
  746. foreach (var categoryValue in appendix.CategoryValues)
  747. {
  748. if (categoryValue.CategoryId == id)
  749. {
  750. categoryValue.Category = replaceCategory;
  751. categoryValue.CategoryId = replaceCategory.Id;
  752. }
  753. }
  754. _appendixService.UpdateAppendix(appendix);
  755. }
  756. if (category != null)
  757. _appendixService.DeleteCategory(category);
  758. _logger.Entity(category, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  759. }
  760. catch (Exception ex)
  761. {
  762. _logger.Error("Fehler bei Löschung einer NT-Kategorie.", ex, _userHelper.FromCookies());
  763. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  764. }
  765. break;
  766. default:
  767. return new EmptyResult();
  768. }
  769. return new JsonResult
  770. {
  771. Data = "success"
  772. };
  773. }
  774. #endregion
  775. }
  776. }