AppendixController.cs 42 KB

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