| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701 |
- using DevExpress.Web.Mvc;
- using GreenTree.Nachtragsmanagement.Core;
- using GreenTree.Nachtragsmanagement.Core.Authentication;
- using GreenTree.Nachtragsmanagement.Services.Appendix;
- using GreenTree.Nachtragsmanagement.Services.Deviation;
- using GreenTree.Nachtragsmanagement.Services.Logging;
- using GreenTree.Nachtragsmanagement.Services.Site;
- using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
- using GreenTree.Nachtragsmanagement.Web.Models.Appendix;
- using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
- using GreenTree.Nachtragsmanagement.Web.Models.Global;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace GreenTree.Nachtragsmanagement.Web.Controllers
- {
- public class AppendixController : Controller
- {
- private readonly IDeviationService _deviationSerivce;
- private readonly IAppendixService _appendixService;
- private readonly ISiteService _siteService;
- private readonly IUserHelper _userHelper;
- private readonly ILogger _logger;
- public AppendixController(
- IDeviationService deviationService,
- IAppendixService appendixService,
- ISiteService siteService,
- IUserHelper userHelper,
- ILogger logger)
- {
- _deviationSerivce = deviationService;
- _appendixService = appendixService;
- _siteService = siteService;
- _userHelper = userHelper;
- _logger = logger;
- ViewData["AllCategories"] = _appendixService.GetAllCategories();
- ViewData["AllStates"] = _appendixService.GetAllStates();
- }
- #region Appendices
- /// <summary>
- /// Basic appendix view function
- /// </summary>
- [FunctionAuthorize(true, "Appendix-Appendices")]
- public ActionResult ViewAppendices()
- {
- var currentUser = _userHelper.FromCookies();
- var appendices = _appendixService.GetAllUserAssignedAppendices(currentUser);
- var appendixModels = appendices
- .Select(u => AppendixDataModel.FromAppendix(u, false))
- .ToList();
- return View("~/Views/Appendices/View.cshtml", appendixModels);
- }
- /// <summary>
- /// Get JSON data of specific appendix
- /// </summary>
- /// <param name="id">Appendix id.</param>
- public ActionResult GetAppendix(int id = -1)
- {
- var appendix = _appendixService.GetAppendixById(id);
- if (appendix == null)
- return new JsonResult
- {
- Data = "notFound",
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- var appendixModel = AppendixDataModel.FromAppendix(appendix, false);
- return new JsonResult
- {
- Data = JsonConvert.SerializeObject(appendixModel),
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- }
- /// <summary>
- /// Callback result for appendix grid
- /// </summary>
- public ActionResult PartialAppendices(int scrollHeight = -1)
- {
- var currentUser = _userHelper.FromCookies();
- var appendices = _appendixService.GetAllUserAssignedAppendices(currentUser);
- var appendixModels = appendices
- .Select(u => AppendixDataModel.FromAppendix(u, false))
- .ToList();
- ViewData["ScrollHeight"] = scrollHeight;
- return PartialView("~/Views/Appendices/_AppendixGridPartial.cshtml", appendixModels);
- }
- /// <summary>
- /// Export result for appendix grid
- /// </summary>
- [HttpPost]
- public ActionResult ExportPartialAppendices(GridViewExportFormat exportformat)
- {
- if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
- return new EmptyResult();
- var currentUser = _userHelper.FromCookies();
- var appendices = _appendixService.GetAllUserAssignedAppendices(currentUser);
- var appendixModels = appendices
- .Select(u => AppendixDataModel.FromAppendix(u, false))
- .ToList();
- var viewContext = new ViewContext();
- var viewPage = new ViewPage();
- var htmlHelper = new HtmlHelper(viewContext, viewPage);
- var gridViewSettings = Extensions.GridViewSettingsHelper.AppendixGridViewSettings(htmlHelper);
- switch (exportformat.Format.ToLower())
- {
- case "xlsx":
- return GridViewExtension.ExportToXlsx(gridViewSettings, appendixModels);
- case "xls":
- return GridViewExtension.ExportToXls(gridViewSettings, appendixModels);
- case "pdf":
- return GridViewExtension.ExportToPdf(gridViewSettings, appendixModels);
- default:
- return new EmptyResult();
- }
- }
- /// <summary>
- /// Partial edit for editing of existing or for new appendix
- /// </summary>
- /// <param name="id">Id for existing appendix, otherweise -1.</param>
- public ActionResult EditAppendix(int id = -1)
- {
- var appendix = _appendixService.GetAppendixById(id);
- var appendixModel = AppendixDataModel.FromAppendix(appendix, true);
- var defaultState = _appendixService.GetDefaultState();
- if (defaultState != null)
- ViewData["DefaultState"] = defaultState.Id;
- return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
- }
- /// <summary>
- /// Partial edit for creating a new deviation for a site
- /// </summary>
- /// <param name="siteId">Id of the site which the deviation should be appended to.</param>
- public ActionResult AppendAppendixToSite(int siteId)
- {
- var site = _siteService.GetSiteById(siteId);
- var lastCustomNumber = 0;
- if (site.Appendices.Any())
- lastCustomNumber = site.Appendices
- .Max(d => StaticHelper.TryParseInt(d.CustomNumber));
- var appendixModel = new AppendixDataModel
- {
- Id = -1,
- SiteId = siteId,
- CustomNumber = (lastCustomNumber + 1).ToString(),
- Percentage = (decimal)0.5
- };
- var defaultState = _appendixService.GetDefaultState();
- if (defaultState != null)
- ViewData["DefaultState"] = defaultState.Id;
- return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
- }
- /// <summary>
- /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
- /// </summary>
- /// <param name="appendixModel">Appendix model to be saved.</param>
- [HttpPost, ValidateInput(false)]
- public ActionResult EditAppendix(AppendixDataModel appendixModel)
- {
- try
- {
- appendixModel.CategoryValueEntities =
- appendixModel.CategoryEntities
- .Select(r => JsonConvert.DeserializeObject<CategoryValueDataModel>(r))
- .ToList();
- for (int i = 0; i < appendixModel.CategoryValueEntities.Count; i++)
- appendixModel.CategoryValueEntities.ElementAt(i).Json = appendixModel.CategoryEntities.ElementAt(i);
- appendixModel.PercentageValue = appendixModel.OfferingValue * appendixModel.Percentage;
- if (!ModelState.IsValid)
- return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
- var categoryValues = appendixModel.CategoryValueEntities
- .Select(r => r.ToCategoryValue())
- .ToList();
- if (appendixModel.Id == -1)
- {
- var appendix = appendixModel.ToAppendix();
- appendix.SetCategoryValues(categoryValues);
- _appendixService.InsertAppendix(appendix);
- _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
- }
- else
- {
- var appendix = _appendixService.GetAppendixById(appendixModel.Id);
- appendix.CustomNumber = appendixModel.CustomNumber;
- appendix.Description = appendixModel.Description;
- appendix.Percentage = appendixModel.Percentage;
- appendix.OfferingNumber = appendixModel.OfferingNumber;
- appendix.OfferingDate = appendixModel.OfferingDate;
- appendix.NegotiationDate = appendixModel.NegotiationDate;
- appendix.NegotiationValue = appendixModel.NegotiationValue;
- appendix.ProtocolExists = appendixModel.ProtocolExists;
- appendix.StateId = appendixModel.StateId;
- appendix.OrderNumber = appendixModel.OrderNumber;
- appendix.OrderDate = appendixModel.OrderDate;
- appendix.OrderInvoiceCreated = appendixModel.OrderInvoiceCreated;
- appendix.Comment = appendixModel.Comment;
- appendix.SiteId = appendixModel.SiteId;
- appendix.SetCategoryValues(categoryValues);
- _appendixService.UpdateAppendix(appendix);
- _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
- }
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Speicherung eines Nachtrags.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- /// <summary>
- /// Simple JSON result for deleting a specific appendix
- /// </summary>
- /// <param name="id">Appendix id.</param>
- [HttpPost]
- public ActionResult DeleteAppendix(int id)
- {
- try
- {
- var appendix = _appendixService.GetAppendixById(id);
- if (appendix != null)
- _appendixService.DeleteAppendix(appendix);
- _logger.Entity(appendix, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Löschung eines Nachtrags.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- #endregion
- #region Invoices
- /// <summary>
- /// Get JSON data of specific invoice
- /// </summary>
- /// <param name="id">Invoice id.</param>
- public ActionResult GetInvoice(int id = -1)
- {
- var invoice = _appendixService.GetInvoiceById(id);
- if (invoice == null)
- return new JsonResult
- {
- Data = "notFound",
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- var invoiceModel = InvoiceDataModel.FromInvoice(invoice, false);
- return new JsonResult
- {
- Data = JsonConvert.SerializeObject(invoiceModel),
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- }
- /// <summary>
- /// Callback result for Invoice list
- /// </summary>
- public ActionResult PartialInvoices(int appendixId = -1)
- {
- if (appendixId == -1)
- return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
- var appendix = _appendixService.GetAppendixById(appendixId);
- if (appendix == null)
- return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", new AppendixDataModel());
- var appendixDataModel = AppendixDataModel.FromAppendix(appendix, false);
- return PartialView("~/Views/Appendices/_InvoiceListPartial.cshtml", appendixDataModel);
- }
- /// <summary>
- /// Partial edit for editing of existing or for new invoice
- /// </summary>
- /// <param name="id">Id for existing invoice, otherweise -1.</param>
- /// <param name="appendixId">Id of corresponding Appendix</param>
- public ActionResult EditInvoice(int appendixId, int id = -1)
- {
- var invoice = _appendixService.GetInvoiceById(id);
- var invoiceModel = InvoiceDataModel.FromInvoice(invoice, true);
- if (id == -1)
- invoiceModel.AppendixId = appendixId;
- return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
- }
- /// <summary>
- /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
- /// </summary>
- /// <param name="invoiceModel">Invoice model to be saved.</param>
- [HttpPost, ValidateInput(false)]
- public ActionResult EditInvoice(InvoiceDataModel invoiceModel)
- {
- try
- {
- if (!ModelState.IsValid)
- return PartialView("~/Views/Appendices/_InvoiceEditPartial.cshtml", invoiceModel);
- if (invoiceModel.Id == -1)
- {
- var invoice = invoiceModel.ToInvoice();
- _appendixService.InsertInvoice(invoice);
- _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
- }
- else
- {
- var invoice = _appendixService.GetInvoiceById(invoiceModel.Id);
- invoice.CustomNumber = invoiceModel.CustomNumber.Value;
- invoice.Date = invoiceModel.DateTime.Value;
- invoice.Value = invoiceModel.Value.Value;
- _appendixService.UpdateInvoice(invoice);
- _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
- }
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Speicherung einer Rechnung.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- /// <summary>
- /// Simple JSON result for deleting a specific invoice
- /// </summary>
- /// <param name="id">Invoice id.</param>
- [HttpPost]
- public ActionResult DeleteInvoice(int id)
- {
- try
- {
- var invoice = _appendixService.GetInvoiceById(id);
- if (invoice != null)
- _appendixService.DeleteInvoice(invoice);
- _logger.Entity(invoice, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Löschung einer Rechnung.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- #endregion
- #region Claims
- /// <summary>
- /// Basic claim view function
- /// </summary>
- [FunctionAuthorize(true, "Appendix-Claims")]
- public ActionResult ViewClaims()
- {
- return View("~/Views/Appendices/Claims.cshtml");
- }
- /// <summary>
- /// Get JSON data of specific claim
- /// </summary>
- /// <param name="claimType">Claim type.</param>
- /// <param name="id">Claim id.</param>
- public ActionResult GetClaim(string claimType, int id = -1)
- {
- switch (claimType.ToLower())
- {
- case "state":
- var state = _appendixService.GetStateById(id);
- if (state == null)
- return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
- else
- return new JsonResult
- {
- Data = JsonConvert.SerializeObject(state),
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- case "category":
- var category = _appendixService.GetCategoryById(id);
- if (category == null)
- return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
- else
- return new JsonResult
- {
- Data = JsonConvert.SerializeObject(category),
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- default:
- return new JsonResult { Data = "unknownClaimType", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
- }
- }
- /// <summary>
- /// Callback result for claim grid
- /// </summary>
- /// <param name="claimType">Claim type.</param>
- public ActionResult PartialClaims(string claimType)
- {
- switch (claimType.ToLower())
- {
- case "state":
- return PartialView("~/Views/Appendices/_StateListPartial.cshtml", ViewData["AllStates"]);
- case "category":
- return PartialView("~/Views/Appendices/_CategoryListPartial.cshtml", ViewData["AllCategories"]);
- default:
- return new EmptyResult();
- }
- }
- /// <summary>
- /// Partial edit for editing of existing or for new claim
- /// </summary>
- /// <param name="claimType">Claim type.</param>
- /// <param name="id">Id for existing claim, otherweise -1.</param>
- public ActionResult EditClaim(string claimType = "", int id = -1)
- {
- switch (claimType.ToLower())
- {
- case "state":
- var state = _appendixService.GetStateById(id);
- var stateModel = StateDataModel.FromState(state, true);
- return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
- case "category":
- var category = _appendixService.GetCategoryById(id);
- var categoryModel = CategoryDataModel.FromCategory(category, true);
- return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
- default:
- return new EmptyResult();
- }
- }
- /// <summary>
- /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
- /// </summary>
- /// <param name="stateModel">State model to be saved.</param>
- [HttpPost, ValidateInput(false)]
- public ActionResult EditState(StateDataModel stateModel)
- {
- try
- {
- if (!ModelState.IsValid)
- return PartialView("~/Views/Appendices/_StateEditPartial.cshtml", stateModel);
- var allStates = _appendixService.GetAllStates();
- if (stateModel.IsDefault)
- {
- foreach (var state in allStates)
- {
- state.IsDefault = false;
- _appendixService.UpdateState(state);
- }
- }
- if (stateModel.Id == -1)
- {
- var claim = stateModel.ToState();
- _appendixService.InsertState(claim);
- _logger.Entity(claim, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
- }
- else
- {
- var state = _appendixService.GetStateById(stateModel.Id);
- state.Description = stateModel.Description;
- state.HexColor = stateModel.HexColor;
- state.IsDefault = stateModel.IsDefault;
- _appendixService.UpdateState(state);
- _logger.Entity(state, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
- }
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Speicherung eines NT-Status.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- /// <summary>
- /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
- /// </summary>
- /// <param name="categoryModel">Category model to be saved.</param>
- [HttpPost, ValidateInput(false)]
- public ActionResult EditCategory(CategoryDataModel categoryModel)
- {
- try
- {
- if (!ModelState.IsValid)
- return PartialView("~/Views/Appendices/_CategoryEditPartial.cshtml", categoryModel);
- if (categoryModel.Id == -1)
- {
- var claim = categoryModel.ToCategory();
- _appendixService.InsertCategory(claim);
- _logger.Entity(claim, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
- }
- else
- {
- var category = _appendixService.GetCategoryById(categoryModel.Id);
- category.Description = categoryModel.Description;
- _appendixService.UpdateCategory(category);
- _logger.Entity(category, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
- }
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Löschung einer NT-Kategorie.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- /// <summary>
- /// Simple JSON result for deleting a specific claim
- /// </summary>
- /// <param name="claimType">Claim type.</param>
- /// <param name="id">Claim id.</param>
- /// <param name="replaceId">Id of claim which deviations get in place of deleting claim.</param>
- [HttpPost]
- public ActionResult DeleteClaim(string claimType, int id, int replaceId)
- {
- switch (claimType.ToLower())
- {
- case "state":
- try
- {
- var state = _appendixService.GetStateById(id);
- var replaceState = _appendixService.GetStateById(replaceId);
- var stateAppendices = _appendixService.GetAppendicesByState(id);
- foreach (var appendix in stateAppendices)
- {
- appendix.StateId = replaceId;
- appendix.State = replaceState;
- _appendixService.UpdateAppendix(appendix);
- }
- if (state != null)
- _appendixService.DeleteState(state);
- _logger.Entity(state, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Löschung eines NT-Status.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- break;
- case "category":
- try
- {
- var category = _appendixService.GetCategoryById(id);
- var replaceCategory = _appendixService.GetCategoryById(replaceId);
- var allAppendices = _appendixService.GetAllAppendices();
- foreach (var appendix in allAppendices)
- {
- foreach (var categoryValue in appendix.CategoryValues)
- {
- if (categoryValue.CategoryId == id)
- {
- categoryValue.Category = replaceCategory;
- categoryValue.CategoryId = replaceCategory.Id;
- }
- }
- _appendixService.UpdateAppendix(appendix);
- }
- if (category != null)
- _appendixService.DeleteCategory(category);
- _logger.Entity(category, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Löschung einer NT-Kategorie.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- break;
- default:
- return new EmptyResult();
- }
- return new JsonResult
- {
- Data = "success"
- };
- }
- #endregion
- }
- }
|