338 lines
13 KiB
C#
338 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using AutoMapper;
|
|
using gehGassi.Common.Data;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Core.Services;
|
|
using gehGassi.Permissions;
|
|
using gehGassi.Web.Auth;
|
|
using gehGassi.Web.Auth.Attributes;
|
|
using gehGassi.Web.Helper;
|
|
using gehGassi.Web.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
namespace gehGassi.Web.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller für die Verwaltung von Steuern
|
|
/// </summary>
|
|
[Authorize]
|
|
public class TaxRateController : BaseController
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IStringLocalizer<TaxRateController> _localizer;
|
|
private readonly ITaxRateService _taxRateService;
|
|
private readonly ILanguageService _languageService;
|
|
private readonly ICountryService _countryService;
|
|
private readonly IProductService _productService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="localizer">Instanz eines IStringLocalizer</param>
|
|
/// <param name="taxRateService">Instanz eines ITaxRateService</param>
|
|
/// <param name="languageService">Instanz eines ILanguageService</param>
|
|
/// <param name="countryService">Instanz eines ICountryService</param>
|
|
/// <param name="productService">Instanz eines IProductService</param>
|
|
public TaxRateController(IMapper mapper, IStringLocalizer<TaxRateController> localizer, ITaxRateService taxRateService, ILanguageService languageService, ICountryService countryService,
|
|
IProductService productService)
|
|
{
|
|
_mapper = mapper;
|
|
_localizer = localizer;
|
|
_taxRateService = taxRateService;
|
|
_languageService = languageService;
|
|
_countryService = countryService;
|
|
_productService = productService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Steuer-Verwaltung zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsTaxRates)]
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück
|
|
/// </summary>
|
|
/// <param name="dm">Abfragekriterien</param>
|
|
/// <returns>Liste von gefundenen Entitäten</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsTaxRates)]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult GetTaxRates([FromBody] DataManager dm)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
}
|
|
|
|
var resultList = _taxRateService.FilterWithNames(dm?.SearchValue ?? "", SelectedLanguage, FallbackLanguage);
|
|
|
|
//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<TaxRateListVm>(item)).ToList();
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Steuer
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsTaxRates)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Create()
|
|
{
|
|
var model = new TaxRateCrudVm()
|
|
{
|
|
TextVms = new List<TaxRateTextVm>(),
|
|
};
|
|
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new TaxRateTextVm() { Id = model.Id, Language = language, Name = string.Empty});
|
|
}
|
|
|
|
return PartialView("_Create", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Rubrik
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsTaxRates)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(TaxRateCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var exists = await _taxRateService.ExistsAsync(model.CountryIso, model.Value);
|
|
if (!exists)
|
|
{
|
|
var item = _taxRateService.Create();
|
|
_mapper.Map(model, item);
|
|
|
|
if (item.UseForShipment)
|
|
await _taxRateService.ResetShipmentAsync(-1);
|
|
|
|
// Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
}
|
|
|
|
_taxRateService.Add(item);
|
|
await _taxRateService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("", _localizer["Err_Tax_Exists"].Value);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.CountryIso);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Steuer
|
|
/// </summary>
|
|
/// <param name="id">Id der Branche</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsTaxRates)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Edit(int id)
|
|
{
|
|
var item = await _taxRateService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
var model = _mapper.Map<TaxRateCrudVm>(item);
|
|
|
|
model.TextVms = new List<TaxRateTextVm>();
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new TaxRateTextVm()
|
|
{
|
|
Id = item.Id,
|
|
Language = language,
|
|
Name = item.Get("Name", language, true)
|
|
});
|
|
}
|
|
|
|
var country = _countryService.GetCountry(item.CountryIso);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
return PartialView("_Edit", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten eines Kunden
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsTaxRates)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Edit(TaxRateCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _taxRateService.GetAsync(model.Id);
|
|
if (item != null)
|
|
{
|
|
_mapper.Map(model, item);
|
|
|
|
if(item.UseForShipment)
|
|
await _taxRateService.ResetShipmentAsync(item.Id);
|
|
|
|
//Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
}
|
|
|
|
await _taxRateService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(new { result.Success, result.Html, result.Data });
|
|
}
|
|
}
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.CountryIso);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Steuer
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsTaxRates)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Delete(List<int> ids)
|
|
{
|
|
var batchErrorHeader = $"<p><strong>{_localizer["Common_BatchDelete_Failed"].Value}</strong></p>";
|
|
var batchResult = new BatchResponseVm(batchErrorHeader) { Success = true };
|
|
|
|
foreach (var id in ids)
|
|
{
|
|
var item = await _taxRateService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
//await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig
|
|
var usedCount = 0;
|
|
|
|
var productCount = await _productService.CountByTaxRateAsync(item.Id);
|
|
usedCount += productCount;
|
|
|
|
if (usedCount == 0)
|
|
{
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Title, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
_taxRateService.Remove(item);
|
|
}
|
|
else
|
|
{
|
|
if (usedCount != 0)
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.Title, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.Title}</strong>", $"<strong>{usedCount}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = "", Success = false, NotFound = true, ErrorMessage = string.Format(_localizer["Common_BatchDelete_NotFound"], $"<strong>{id}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
if (batchResult.BatchResponseList.Any(c => c.Success))
|
|
await _taxRateService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
#region Lookup
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Steuerstufen für LookUp zurück
|
|
/// </summary>
|
|
/// <param name="dm">DataManager</param>
|
|
/// <param name="includeNone">Soll "Keine(r)" integriert werden</param>
|
|
/// <returns>Liste</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
public async Task<IActionResult> Lookup([FromBody] DataManager dm, bool includeNone = false)
|
|
{
|
|
var filter = string.Empty;
|
|
if (dm.Where?.FirstOrDefault() != null)
|
|
{
|
|
filter = dm.Where.First().value.ToString();
|
|
if (filter.IndexOf('(') > 0)
|
|
{
|
|
filter = filter.Substring(0, (filter.IndexOf('(') - 1));
|
|
filter = filter.TrimEnd();
|
|
}
|
|
}
|
|
|
|
var items = await _taxRateService.FilterWithNames(filter, SelectedLanguage, FallbackLanguage).ToListAsync();
|
|
var result = items.Select(item => new LookupItemVm() { Id = item.Id.ToString(), Name = $"{item.Name} ({item.Value:n2}%)" }).OrderBy(c => c.Name).ToList();
|
|
|
|
if (includeNone)
|
|
result.Insert(0, new LookupItemVm() { Id = "-1", Name = _localizer["Common_None"].ToString() });
|
|
|
|
return Json(result);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|