using System; using AutoMapper; using gehGassi.Common.Data; using gehGassi.Permissions; using gehGassi.Web.Auth; using gehGassi.Web.Auth.Attributes; using gehGassi.Web.Helper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Localization; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using gehGassi.Core.Interfaces; using gehGassi.Domain.Common; using gehGassi.Web.Models; using gehGassi.Core.Services; namespace gehGassi.Web.Controllers { /// /// Controller für die Verwaltung von Transaktionsgebühren /// [Authorize] public class TransactionFeeController : BaseController { private readonly IMapper _mapper; private readonly IStringLocalizer _localizer; private readonly ITransactionFeeService _transactionFeeService; /// /// Erstellt eine Instanz /// /// Instanz eines IMapper /// Instanz eines IStringLocalizer /// Instanz eines ITransactionFeeService public TransactionFeeController(IMapper mapper, IStringLocalizer localizer, ITransactionFeeService transactionFeeService) { _mapper = mapper; _localizer = localizer; _transactionFeeService = transactionFeeService; } #region MangoPay Transaktionsgebühren /// /// Gibt einen View für die Verwaltung von Transaktionsgebühren zurück /// /// View [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] public IActionResult Index() { return View(); } /// /// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück /// /// Abfragekriterien /// Liste von gefundenen Entitäten [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [HttpPost] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult GetList([FromBody] DataManager dm) { if (dm != null) { var propList = new List(); dm.SetComplexProperties(propList); } var resultList = _transactionFeeService.Filter(dm?.SearchValue ?? ""); //Sortierung resultList = dm.ApplySorting(resultList); //Filter resultList = dm.ApplyFiltering(resultList, out var countFiltered); //Paging resultList = dm.ApplyPaging(resultList); var resultListVm = resultList.ToList().Select(item => _mapper.Map(item)).ToList(); foreach (var itemVm in resultListVm) { itemVm.PayInTypeText = itemVm.PayInType.GetDisplayName(AnnotationsLocalizer); } //FilterPreview? if (!dm.RequiresCounts) return Json(resultListVm); return Json(new { result = resultListVm, count = countFiltered }); } /// /// Anlegen einer Transaktionsgebühr /// /// PartialView [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Create() { var model = new TransactionFeeVm() { Id = Guid.NewGuid().ToString("N"), PayInType = PayInTypeVm.CB_VISA_MASTERCARD, Percent = 0, Fixed = 0 }; return PartialView("_Create", model); } /// /// Anlegen einer Transaktionsgebühr /// /// Model /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [ValidateAntiForgeryToken] [HttpPost] public async Task Create(TransactionFeeVm model) { var result = new ResponseVm { Success = false }; if (ModelState.IsValid) { var exists = await _transactionFeeService.ExistsAsync((PayInType)model.PayInType); if (!exists) { var item = _transactionFeeService.Create(); _mapper.Map(model, item); item.UpdatedAt = DateTimeOffset.UtcNow; _transactionFeeService.Add(item); await _transactionFeeService.CommitAsync(User.Identity.Name); result.Data = item.ToCamelCaseJson(); result.Success = true; result.Html = string.Empty; return Json(result); } else { ModelState.AddModelError("", _localizer["Err_TransactionFee_Exists"].Value); } } result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext); return Json(result); } /// /// Bearbeiten einer Transaktionsgebühr /// /// Id der Transaktionsgebühr /// PartialView [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public async Task Edit(string id) { var item = await _transactionFeeService.GetAsync(id); if (item != null) { var model = _mapper.Map(item); return PartialView("_Edit", model); } return PartialView("_Error"); } /// /// Bearbeiten einer Transaktionsgebühr /// /// Model /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [ValidateAntiForgeryToken] [HttpPost] public async Task Edit(TransactionFeeVm model) { var result = new ResponseVm { Success = false }; if (ModelState.IsValid) { var item = await _transactionFeeService.GetAsync(model.Id); if (item != null) { _mapper.Map(model, item); item.UpdatedAt = DateTimeOffset.UtcNow; await _transactionFeeService.CommitAsync(User.Identity.Name); result.Data = item.ToCamelCaseJson(); result.Success = true; result.Html = string.Empty; return Json(new { result.Success, result.Html, result.Data }); } } result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext); return Json(result); } /// /// Löschen einer Transaktionsgebühr /// /// Liste Id der Entität /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [HttpPost] public async Task Delete(List ids) { var batchErrorHeader = $"

{_localizer["Common_BatchDelete_Failed"].Value}

"; var batchResult = new BatchResponseVm(batchErrorHeader) { Success = true }; foreach (var id in ids) { var item = await _transactionFeeService.GetAsync(id); if (item != null) { //await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig var usedCount = 0; if (usedCount == 0) { batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.PayInType.ToString(), Success = true, NotFound = false, ErrorMessage = "" }); item.Deleted = true; item.UpdatedAt = DateTimeOffset.UtcNow; } else { if (usedCount != 0) { batchResult.Success = false; batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.PayInType.ToString(), Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"{item.PayInType.ToString()}", $"{usedCount}") + "
" }); } } } else { batchResult.Success = false; batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = "", Success = false, NotFound = true, ErrorMessage = string.Format(_localizer["Common_BatchDelete_NotFound"], $"{id}") + "
" }); } } if (batchResult.BatchResponseList.Any(c => c.Success)) await _transactionFeeService.CommitAsync(User.Identity.Name); return Json(batchResult); } #endregion #region GehGassi-Gebühren /// /// Gibt einen View für die Verwaltung von GehGassi-Gebühren zurück /// /// View [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] public IActionResult IndexGehGassi() { return View(); } /// /// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück /// /// Abfragekriterien /// Liste von gefundenen Entitäten [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [HttpPost] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult GetListGehGassi([FromBody] DataManager dm) { if (dm != null) { var propList = new List(); dm.SetComplexProperties(propList); } var resultList = _transactionFeeService.FilterGehGassiFees(dm?.SearchValue ?? ""); //Sortierung resultList = dm.ApplySorting(resultList); //Filter resultList = dm.ApplyFiltering(resultList, out var countFiltered); //Paging resultList = dm.ApplyPaging(resultList); var resultListVm = resultList.ToList().Select(item => _mapper.Map(item)).ToList(); //FilterPreview? if (!dm.RequiresCounts) return Json(resultListVm); return Json(new { result = resultListVm, count = countFiltered }); } /// /// Anlegen einer GehGassi-Gebühr /// /// PartialView [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult CreateGehGassi() { var model = new GehGassiFeeVm() { Id = Guid.NewGuid().ToString("N"), Percent = 0, Fixed = 0, StartAmmount = 0 }; return PartialView("_CreateGehGassi", model); } /// /// Anlegen einer GehGassi-Gebühr /// /// Model /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [ValidateAntiForgeryToken] [HttpPost] public async Task CreateGehGassi(GehGassiFeeVm model) { var result = new ResponseVm { Success = false }; if (ModelState.IsValid) { var exists = await _transactionFeeService.ExistsGehGassiFeeAsync(model.StartAmmount, model.Id); if (!exists) { var item = _transactionFeeService.CreateGehGassiFee(); _mapper.Map(model, item); item.UpdatedAt = DateTimeOffset.UtcNow; _transactionFeeService.AddGehGassiFee(item); await _transactionFeeService.CommitAsync(User.Identity.Name); result.Data = item.ToCamelCaseJson(); result.Success = true; result.Html = string.Empty; return Json(result); } else { ModelState.AddModelError("", _localizer["Err_GehGassiFee_Exists"].Value); } } result.Html = await PartialView("_CreateGehGassi", model).ToStringAsync(ControllerContext); return Json(result); } /// /// Bearbeiten einer GehGassi-Gebühr /// /// Id der GehGassi-Gebühr /// PartialView [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public async Task EditGehGassi(string id) { var item = await _transactionFeeService.GetGehGassiFeeAsync(id); if (item != null) { var model = _mapper.Map(item); return PartialView("_EditGehGassi", model); } return PartialView("_Error"); } /// /// Bearbeiten einer GehGassi-Gebühr /// /// Model /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [ValidateAntiForgeryToken] [HttpPost] public async Task EditGehGassi(GehGassiFeeVm model) { var result = new ResponseVm { Success = false }; if (ModelState.IsValid) { var exists = await _transactionFeeService.ExistsGehGassiFeeAsync(model.StartAmmount, model.Id); if (!exists) { var item = await _transactionFeeService.GetGehGassiFeeAsync(model.Id); if (item != null) { _mapper.Map(model, item); item.UpdatedAt = DateTimeOffset.UtcNow; await _transactionFeeService.CommitAsync(User.Identity.Name); result.Data = item.ToCamelCaseJson(); result.Success = true; result.Html = string.Empty; return Json(new { result.Success, result.Html, result.Data }); } } else { ModelState.AddModelError("", _localizer["Err_GehGassiFee_Exists"].Value); } } result.Html = await PartialView("_EditGehGassi", model).ToStringAsync(ControllerContext); return Json(result); } /// /// Löschen einer GehGassi-Gebühr /// /// Liste Id der Entität /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)] [HttpPost] public async Task DeleteGehGassi(List ids) { var batchErrorHeader = $"

{_localizer["Common_BatchDelete_Failed"].Value}

"; var batchResult = new BatchResponseVm(batchErrorHeader) { Success = true }; foreach (var id in ids) { var item = await _transactionFeeService.GetGehGassiFeeAsync(id); if (item != null) { //await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig var usedCount = 0; if (usedCount == 0) { batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.StartAmmount.ToString(), Success = true, NotFound = false, ErrorMessage = "" }); item.Deleted = true; item.UpdatedAt = DateTimeOffset.UtcNow; } else { if (usedCount != 0) { batchResult.Success = false; batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.StartAmmount.ToString(), Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"{item.StartAmmount.ToString()}", $"{usedCount}") + "
" }); } } } else { batchResult.Success = false; batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = "", Success = false, NotFound = true, ErrorMessage = string.Format(_localizer["Common_BatchDelete_NotFound"], $"{id}") + "
" }); } } if (batchResult.BatchResponseList.Any(c => c.Success)) await _transactionFeeService.CommitAsync(User.Identity.Name); return Json(batchResult); } #endregion } }