SiteController.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. using DevExpress.Utils;
  2. using DevExpress.Web;
  3. using DevExpress.Web.ASPxThemes;
  4. using DevExpress.Web.Mvc;
  5. using DevExpress.XtraPrinting;
  6. using DevExpress.XtraReports.UI;
  7. using GreenTree.Nachtragsmanagement.Core.Authentication;
  8. using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
  9. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  10. using GreenTree.Nachtragsmanagement.Services.Appendix;
  11. using GreenTree.Nachtragsmanagement.Services.Configuration;
  12. using GreenTree.Nachtragsmanagement.Services.Deviation;
  13. using GreenTree.Nachtragsmanagement.Services.Logging;
  14. using GreenTree.Nachtragsmanagement.Services.Site;
  15. using GreenTree.Nachtragsmanagement.Services.User;
  16. using GreenTree.Nachtragsmanagement.Web.Extensions;
  17. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  18. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  19. using GreenTree.Nachtragsmanagement.Web.Models.Site;
  20. using Newtonsoft.Json;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.IO;
  24. using System.Linq;
  25. using System.Net.Mime;
  26. using System.Web;
  27. using System.Web.Mvc;
  28. using System.Web.UI;
  29. using System.Web.UI.WebControls;
  30. using static GreenTree.Nachtragsmanagement.Web.Extensions.MVCxGridViewGeneratorHelper;
  31. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  32. {
  33. public class SiteController : Controller
  34. {
  35. private readonly ISiteService _siteService;
  36. private readonly IDeviationService _deviationService;
  37. private readonly IAppendixService _appendixService;
  38. private readonly IUserService _userService;
  39. private readonly IUserHelper _userHelper;
  40. private readonly ILogger _logger;
  41. private readonly IConfigurationService _configurationService;
  42. public SiteController(
  43. ISiteService siteService,
  44. IDeviationService deviationService,
  45. IAppendixService appendixService,
  46. IUserService userService,
  47. IUserHelper userHelper,
  48. ILogger logger,
  49. IConfigurationService configurationService)
  50. {
  51. _siteService = siteService;
  52. _deviationService = deviationService;
  53. _appendixService = appendixService;
  54. _userService = userService;
  55. _userHelper = userHelper;
  56. _logger = logger;
  57. _configurationService = configurationService;
  58. ViewData["AllUsers"] = _userService.GetAllUsers();
  59. ViewData["AllUsersWithRole"] =
  60. _userService.GetAllUsers()
  61. .Select(u => new
  62. {
  63. Id = u.Id,
  64. Description = String.Format("{0} - {1}", u.Lastname,
  65. String.Join(", ", u.Roles
  66. .Select(r => r.Description)))
  67. })
  68. .ToList();
  69. ViewData["DefaultAppendixState"] = _appendixService.GetDefaultState();
  70. }
  71. #region Sites
  72. /// <summary>
  73. /// Basic site view function
  74. /// </summary>
  75. [FunctionAuthorize(true, "Site-Sites")]
  76. public ActionResult ViewSites()
  77. {
  78. var currentUser = _userHelper.FromCookiesOrSession();
  79. var sites = _siteService.GetAllUserAssignedSites(currentUser);
  80. var siteModels = sites
  81. .Select(u => SiteDataModel.FromSite(u, false, _configurationService))
  82. .ToList();
  83. return View("~/Views/Sites/View.cshtml", siteModels);
  84. }
  85. /// <summary>
  86. /// Get JSON data of specific site
  87. /// </summary>
  88. /// <param name="id">Site id.</param>
  89. public ActionResult GetSite(int id = -1)
  90. {
  91. var site = _siteService.GetSiteById(id);
  92. if (site == null)
  93. return new JsonResult
  94. {
  95. Data = "notFound",
  96. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  97. };
  98. var siteModel = SiteDataModel.FromSite(site, false, _configurationService);
  99. var siteTreeModel = SiteTreeDataModel.TreeFromSite(site);
  100. siteModel.SiteTreeData = siteTreeModel;
  101. return new JsonResult
  102. {
  103. Data = JsonConvert.SerializeObject(siteModel),
  104. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  105. };
  106. }
  107. /// <summary>
  108. /// Gets a full deviation description of a given site
  109. /// </summary>
  110. /// <param name="id">The site id.</param>
  111. public ActionResult GetDeviationDescription(int id)
  112. {
  113. var result = new JsonResult
  114. {
  115. JsonRequestBehavior = JsonRequestBehavior.AllowGet,
  116. Data = String.Empty
  117. };
  118. var site = _siteService.GetSiteById(id);
  119. var siteModel = SiteDataModel.FromSite(site, false, _configurationService);
  120. result.Data = siteModel.HtmlDeviationDescription;
  121. return result;
  122. }
  123. /// <summary>
  124. /// Callback result for site grid
  125. /// </summary>
  126. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  127. public ActionResult PartialSites(int scrollHeight = -1)
  128. {
  129. var currentUser = _userHelper.FromCookiesOrSession();
  130. var sites = _siteService.GetAllUserAssignedSites(currentUser);
  131. var siteModels = sites
  132. .Select(u => SiteDataModel.FromSite(u, false, _configurationService))
  133. .ToList();
  134. ViewData["ScrollHeight"] = scrollHeight;
  135. return PartialView("~/Views/Sites/_SiteGridPartial.cshtml", siteModels);
  136. }
  137. /// <summary>
  138. /// Export result for site grid
  139. /// </summary>
  140. [HttpPost]
  141. public ActionResult ExportPartialSites(string displayMode, string exportformat)
  142. {
  143. if (String.IsNullOrEmpty(displayMode))
  144. return new EmptyResult();
  145. var currentUser = _userHelper.FromCookiesOrSession();
  146. var sites = _siteService.GetAllUserAssignedSites(currentUser);
  147. var siteModels = sites
  148. .Select(u => SiteDataModel.FromSite(u, false, _configurationService))
  149. .ToList();
  150. var viewContext = new ViewContext();
  151. var viewPage = new ViewPage();
  152. var htmlHelper = new System.Web.Mvc.HtmlHelper(viewContext, viewPage);
  153. MVCxGridViewState gridViewState = (MVCxGridViewState)Session["SiteGridViewState"];
  154. var settings = GridViewSettingsHelper.SiteGridViewSettings(htmlHelper);
  155. if (gridViewState != null)
  156. {
  157. var generator = new MVCReportGeneratonHelper();
  158. generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
  159. generator.CustomizeGroupColumnSummary += new CustomizeColumnGroupSummaryEventHandler(generator_CustomizeGroupColumnSummary);
  160. generator.CustomizeTotalColumnSummary += new CustomizeColumnTotalSummaryEventHandler(generator_CustomizeTotalColumnSummary);
  161. generator.PageSummaryGetResult += new SummaryGetResultHandler(generator_PageSummaryGetResult);
  162. var report = generator.GenerateMVCReport(gridViewState, siteModels, "Baustellenliste");
  163. if (displayMode == "popup")
  164. {
  165. return PartialView("~/Views/Shared/_PrintPopupPartial.cshtml",
  166. new PrintGridModel(report, "devGridViewSite",
  167. new { Controller = "Site", Action = "ExportPartialSites",
  168. displayMode = "callback", exportformat = String.Empty },
  169. new { Controller = "Site", Action = "ExportPartialSites",
  170. displayMode = "export", exportformat = String.Empty }));
  171. }
  172. else if (displayMode == "callback")
  173. {
  174. return PartialView("~/Views/Shared/_PrintDocumentViewerPartial.cshtml",
  175. new PrintGridModel(report, "devGridViewSite",
  176. new { Controller = "Site", Action = "ExportPartialSites",
  177. displayMode = "callback", exportformat = String.Empty },
  178. new { Controller = "Site", Action = "ExportPartialSites",
  179. displayMode = "export", exportformat = String.Empty }));
  180. }
  181. else if (displayMode == "export")
  182. {
  183. switch (exportformat.ToLower())
  184. {
  185. case "xlsx":
  186. settings.TotalSummary["Description"].DisplayFormat = "Anzahl = {0:n0}";
  187. settings.TotalSummary["DeviationValue"].DisplayFormat = "{0:c2}";
  188. settings.TotalSummary["SiteDeviationValue"].DisplayFormat = "{0:c2}";
  189. settings.TotalSummary["AppendixValueRemaining"].DisplayFormat = "{0:c2}";
  190. settings.TotalSummary["AppendixValueNegotiated"].DisplayFormat = "{0:c2}";
  191. return GridViewExtension.ExportToXlsx(settings, siteModels);
  192. case "xls":
  193. settings.TotalSummary["Description"].DisplayFormat = "Anzahl = {0:n0}";
  194. settings.TotalSummary["DeviationValue"].DisplayFormat = "{0:c2}";
  195. settings.TotalSummary["SiteDeviationValue"].DisplayFormat = "{0:c2}";
  196. settings.TotalSummary["AppendixValueRemaining"].DisplayFormat = "{0:c2}";
  197. settings.TotalSummary["AppendixValueNegotiated"].DisplayFormat = "{0:c2}";
  198. return GridViewExtension.ExportToXls(settings, siteModels);
  199. case "pdf":
  200. report.Name = "Baustellenliste";
  201. return DocumentViewerExtension.ExportTo(report);
  202. }
  203. }
  204. return new EmptyResult();
  205. }
  206. else
  207. return new EmptyResult();
  208. }
  209. private decimal accumulatedCustomSummaryCount = 0;
  210. private decimal accumulatedCustomSummaryDeviationValue = 0;
  211. private decimal accumulatedCustomSummarySiteDeviationValue = 0;
  212. private decimal accumulatedCustomSummaryAppendixValueRemaining = 0;
  213. private decimal accumulatedCustomSummaryAppendixValueCalculated = 0;
  214. private decimal accumulatedCustomSummaryAppendixValueCleared = 0;
  215. private decimal accumulatedCustomSummaryAppendixValueNegotiated = 0;
  216. /// <summary>
  217. /// Set custom summary result for page
  218. /// </summary>
  219. /// <param name="source"></param>
  220. /// <param name="e"></param>
  221. private void generator_PageSummaryGetResult(object source, SummaryGetResultEventArgs e)
  222. {
  223. var label = (XRLabel)source;
  224. if (label.Tag.ToString() == "Description")
  225. {
  226. accumulatedCustomSummaryCount += e.CalculatedValues.Count;
  227. e.Result = accumulatedCustomSummaryCount;
  228. }
  229. if (label.Tag.ToString() == "DeviationValue")
  230. {
  231. accumulatedCustomSummaryDeviationValue += e.CalculatedValues.OfType<decimal>().Sum();
  232. e.Result = accumulatedCustomSummaryDeviationValue;
  233. }
  234. if (label.Tag.ToString() == "SiteDeviationValue")
  235. {
  236. accumulatedCustomSummarySiteDeviationValue += e.CalculatedValues.OfType<decimal>().Sum();
  237. e.Result = accumulatedCustomSummarySiteDeviationValue;
  238. }
  239. if (label.Tag.ToString() == "AppendixValueRemaining")
  240. {
  241. accumulatedCustomSummaryAppendixValueRemaining += e.CalculatedValues.OfType<decimal>().Sum();
  242. e.Result = accumulatedCustomSummaryAppendixValueRemaining;
  243. }
  244. if (label.Tag.ToString() == "AppendixValueCalculated")
  245. {
  246. accumulatedCustomSummaryAppendixValueCalculated += e.CalculatedValues.OfType<decimal>().Sum();
  247. e.Result = accumulatedCustomSummaryAppendixValueCalculated;
  248. }
  249. if (label.Tag.ToString() == "AppendixValueCleared")
  250. {
  251. accumulatedCustomSummaryAppendixValueCleared += e.CalculatedValues.OfType<decimal>().Sum();
  252. e.Result = accumulatedCustomSummaryAppendixValueCleared;
  253. }
  254. if (label.Tag.ToString() == "AppendixValueNegotiated")
  255. {
  256. accumulatedCustomSummaryAppendixValueNegotiated += e.CalculatedValues.OfType<decimal>().Sum();
  257. e.Result = accumulatedCustomSummaryAppendixValueNegotiated;
  258. }
  259. e.Handled = true;
  260. }
  261. /// <summary>
  262. /// Customize created columns
  263. /// </summary>
  264. private void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
  265. {
  266. foreach (var column in e.ColumnsInfo)
  267. {
  268. if (column.ColumnCaption == "#") { column.IsVisible = false; }
  269. if (column.FieldName == "CustomNumber") { column.ColumnWidth = 60; }
  270. if (column.FieldName == "SiteDescription") { column.ColumnWidth = 80; }
  271. if (column.FieldName == "Start") { column.ColumnWidth = 65; }
  272. if (column.FieldName == "End") { column.ColumnWidth = 65; }
  273. if (column.FieldName == "DeviationDescription") { column.IsVisible = false; column.IsDetail = true; }
  274. if (column.FieldName == "AppendixDescription") { column.ColumnWidth = 60; }
  275. if (column.FieldName == "DeviationValue") { column.ColumnWidth = 70; }
  276. if (column.FieldName == "UserDescription") { column.ColumnWidth = 120; }
  277. if (column.FieldName == "Comment") { column.IsVisible = false; column.IsDetail = true; }
  278. }
  279. }
  280. /// <summary>
  281. /// Customize column summary
  282. /// </summary>
  283. private void generator_CustomizeGroupColumnSummary(object source, ColumnSummaryCreationEventArgs e)
  284. {
  285. if (e.FieldName == "Description") { e.Summary.FormatString = "Alle = {0:n0}"; }
  286. if (e.FieldName == "DeviationValue") { e.Summary.FormatString = "VA-Wert ∑ = {0:c2}"; }
  287. if (e.FieldName == "SiteDeviationValue") { e.Summary.FormatString = "Off.VA ∑ = {0:c2}"; }
  288. if (e.FieldName == "AppendixValueRemaining") { e.Summary.FormatString = "Off.NT ∑ {0:c2}"; }
  289. if (e.FieldName == "AppendixValueCalculated") { e.Summary.FormatString = "Bew.NT ∑ {0:c2}"; }
  290. if (e.FieldName == "AppendixValueCleared") { e.Summary.FormatString = "Pot.NT ∑ {0:c2}"; }
  291. if (e.FieldName == "AppendixValueNegotiated") { e.Summary.FormatString = "Verh.NT ∑ {0:c2}"; }
  292. }
  293. /// <summary>
  294. /// Customize column summary
  295. /// </summary>
  296. private void generator_CustomizeTotalColumnSummary(object source, ColumnSummaryCreationEventArgs e)
  297. {
  298. if (e.FieldName == "Description") { e.Summary.FormatString = "Alle = {0:n0}"; }
  299. if (e.FieldName == "DeviationValue") { e.Summary.FormatString = "{0:c2}"; }
  300. if (e.FieldName == "SiteDeviationValue") { e.Summary.FormatString = "{0:c2}"; }
  301. if (e.FieldName == "AppendixValueRemaining") { e.Summary.FormatString = "{0:c2}"; }
  302. if (e.FieldName == "AppendixValueCalculated") { e.Summary.FormatString = "{0:c2}"; }
  303. if (e.FieldName == "AppendixValueCleared") { e.Summary.FormatString = "{0:c2}"; }
  304. if (e.FieldName == "AppendixValueNegotiated") { e.Summary.FormatString = "{0:c2}"; }
  305. }
  306. /// <summary>
  307. /// Callback result for site appendices
  308. /// </summary>
  309. /// <param name="siteId">Id of site.</param>
  310. public ActionResult PartialDeviationAppendices(int siteId)
  311. {
  312. var site = _siteService.GetSiteById(siteId);
  313. var siteModel = SiteDataModel.FromSite(site, false, _configurationService);
  314. var siteTreeModel = SiteTreeDataModel.TreeFromSite(site);
  315. siteModel.SiteTreeData = siteTreeModel;
  316. return PartialView("~/Views/Sites/_SiteEditTreePartial.cshtml", siteModel);
  317. }
  318. /// <summary>
  319. /// Partial edit for editing of existing or for new site
  320. /// </summary>
  321. /// <param name="id">Id for existing site, otherweise -1.</param>
  322. public ActionResult EditSite(int id = -1)
  323. {
  324. var site = _siteService.GetSiteById(id);
  325. var siteModel = SiteDataModel.FromSite(site, true, _configurationService);
  326. var siteTreeModel = SiteTreeDataModel.TreeFromSite(site);
  327. siteModel.SiteTreeData = siteTreeModel;
  328. return PartialView("~/Views/Sites/_SiteEditPartial.cshtml", siteModel);
  329. }
  330. /// <summary>
  331. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  332. /// </summary>
  333. /// <param name="siteModel">Site model to be saved.</param>
  334. [HttpPost, ValidateInput(false)]
  335. public ActionResult EditSite(SiteDataModel siteModel)
  336. {
  337. try
  338. {
  339. if (!ModelState.IsValid)
  340. {
  341. foreach (var role in siteModel.UserValues)
  342. siteModel.UserDescriptions.Add(
  343. ((IList<User>)ViewData["AllUsers"])
  344. .First(r => r.Id == role).Lastname);
  345. if (siteModel.Id != -1)
  346. {
  347. var site = _siteService.GetSiteById(siteModel.Id);
  348. var siteTreeModel = SiteTreeDataModel.TreeFromSite(site);
  349. siteModel.SiteTreeData = siteTreeModel;
  350. }
  351. return PartialView("~/Views/Sites/_SiteEditPartial.cshtml", siteModel);
  352. }
  353. var selectedUsers = _userService.GetUsersByIds(siteModel.UserValues.ToArray());
  354. if (siteModel.Id == -1)
  355. {
  356. var site = siteModel.ToSite();
  357. site.SetUsers(selectedUsers);
  358. _siteService.InsertSite(site);
  359. _logger.Entity(site, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookiesOrSession());
  360. }
  361. else
  362. {
  363. var site = _siteService.GetSiteById(siteModel.Id);
  364. site.CustomNumber = siteModel.CustomNumber;
  365. site.Description = siteModel.Description;
  366. site.Start = siteModel.Start;
  367. site.End = siteModel.End;
  368. site.Comment = siteModel.Comment;
  369. site.SetUsers(selectedUsers);
  370. _siteService.UpdateSite(site);
  371. _logger.Entity(site, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  372. }
  373. return new JsonResult
  374. {
  375. Data = "success"
  376. };
  377. }
  378. catch (Exception ex)
  379. {
  380. _logger.Error("Fehler bei Speicherung einer Baustelle.", ex, _userHelper.FromCookiesOrSession());
  381. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  382. }
  383. }
  384. /// <summary>
  385. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  386. /// </summary>
  387. /// <param name="siteModel">Site model to be saved.</param>
  388. [HttpPost, ValidateInput(false)]
  389. public ActionResult EditSiteForAppend(SiteDataModel siteModel)
  390. {
  391. try
  392. {
  393. if (!ModelState.IsValid)
  394. {
  395. foreach (var role in siteModel.UserValues)
  396. siteModel.UserDescriptions.Add(
  397. ((IList<User>)ViewData["AllUsers"])
  398. .First(r => r.Id == role).Lastname);
  399. return PartialView("~/Views/Sites/_SiteEditPartial.cshtml", siteModel);
  400. }
  401. var selectedUsers = _userService.GetUsersByIds(siteModel.UserValues.ToArray());
  402. var siteId = siteModel.Id;
  403. if (siteModel.Id == -1)
  404. {
  405. var site = siteModel.ToSite();
  406. site.SetUsers(selectedUsers);
  407. _siteService.InsertSite(site);
  408. siteId = site.Id;
  409. _logger.Entity(site, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookiesOrSession());
  410. }
  411. return EditSite(siteId);
  412. }
  413. catch (Exception ex)
  414. {
  415. _logger.Error("Fehler bei Speicherung einer Baustelle für einen Nachtrag.", ex, _userHelper.FromCookiesOrSession());
  416. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  417. }
  418. }
  419. /// <summary>
  420. /// Simple JSON result for deleting a specific site
  421. /// </summary>
  422. /// <param name="id">Site id.</param>
  423. [HttpPost]
  424. public ActionResult DeleteSite(int id)
  425. {
  426. try
  427. {
  428. var site = _siteService.GetSiteById(id);
  429. if (site != null)
  430. _siteService.DeleteSite(site);
  431. _logger.Entity(site, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookiesOrSession());
  432. return new JsonResult
  433. {
  434. Data = "success"
  435. };
  436. }
  437. catch (Exception ex)
  438. {
  439. _logger.Error("Fehler bei Löschung einer Baustelle.", ex, _userHelper.FromCookiesOrSession());
  440. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  441. }
  442. }
  443. #endregion
  444. #region Assigning
  445. /// <summary>
  446. /// Assigns a deviation to an existing entity of type site or appendix
  447. /// </summary>
  448. /// <param name="siteId">The siteId in which the context works.</param>
  449. /// <param name="sourceKey">The sourceKey of the deviation.</param>
  450. /// <param name="targetKey">The targetKey of the site or appendix.</param>
  451. public ActionResult AssignDeviationToEntity(int siteId, string sourceKey, string targetKey)
  452. {
  453. if (!sourceKey.StartsWith("d"))
  454. return new EmptyResult();
  455. if (sourceKey == targetKey)
  456. return new EmptyResult();
  457. var determinedTargetKey = targetKey;
  458. if (targetKey.StartsWith("d"))
  459. {
  460. return new EmptyResult();
  461. //var targetDeviationId = Convert.ToInt32(sourceKey.Replace("d_", String.Empty));
  462. //var targetDeviation = _deviationService.GetDeviationById(targetDeviationId);
  463. //if (targetDeviation == null)
  464. // return new EmptyResult();
  465. //if (targetDeviation.Site != null)
  466. // determinedTargetKey = "a_0";
  467. //if (targetDeviation.Appendix != null)
  468. // determinedTargetKey = String.Format("a_{0}", targetDeviation.AppendixId);
  469. }
  470. var deviationId = Convert.ToInt32(sourceKey.Replace("d_", String.Empty));
  471. var deviation = _deviationService.GetDeviationById(deviationId);
  472. if (determinedTargetKey == "a_0")
  473. {
  474. try
  475. {
  476. var site = _siteService.GetSiteById(siteId);
  477. if (deviation.Site != null)
  478. return new EmptyResult();
  479. deviation.Appendix = null;
  480. deviation.AppendixId = null;
  481. site.Deviations.Add(deviation);
  482. _siteService.UpdateSite(site);
  483. _deviationService.UpdateDeviation(deviation);
  484. _logger.Entity(deviation, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  485. }
  486. catch (Exception ex)
  487. {
  488. _logger.Error("Fehler bei Zuweisung einer VA zu einer Baustelle.", ex, _userHelper.FromCookiesOrSession());
  489. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  490. }
  491. }
  492. else
  493. {
  494. try
  495. {
  496. var appendixId = Convert.ToInt32(determinedTargetKey.Replace("a_", String.Empty));
  497. var appendix = _appendixService.GetAppendixById(appendixId);
  498. if (deviation.AppendixId == appendixId)
  499. return new EmptyResult();
  500. deviation.Site = null;
  501. deviation.SiteId = null;
  502. appendix.Deviations.Add(deviation);
  503. _appendixService.UpdateAppendix(appendix);
  504. _deviationService.UpdateDeviation(deviation);
  505. _logger.Entity(deviation, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookiesOrSession());
  506. }
  507. catch (Exception ex)
  508. {
  509. _logger.Error("Fehler bei Zuweisung einer VA zu einem Nachtrag.", ex, _userHelper.FromCookiesOrSession());
  510. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  511. }
  512. }
  513. return new JsonResult
  514. {
  515. Data = "success"
  516. };
  517. }
  518. #endregion
  519. }
  520. }