AppendixController.cs 43 KB

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