295 lines
12 KiB
C#
295 lines
12 KiB
C#
using System.Collections.Generic;
|
|
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.Extensions.Localization;
|
|
|
|
namespace gehGassi.Web.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller für die Verwaltung von Versandkosten
|
|
/// </summary>
|
|
[Authorize]
|
|
public class ShipmentCostController : BaseController
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IStringLocalizer<ShipmentCostController> _localizer;
|
|
private readonly IShipmentCostService _shipmentCostService;
|
|
private readonly ICountryService _countryService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="localizer">Instanz eines IStringLocalizer</param>
|
|
/// <param name="shipmentCostService">Instanz eines IShipmentCostService</param>
|
|
/// <param name="countryService">Instanz eines ICountryService</param>
|
|
public ShipmentCostController(IMapper mapper, IStringLocalizer<ShipmentCostController> localizer, IShipmentCostService shipmentCostService, ICountryService countryService)
|
|
{
|
|
_mapper = mapper;
|
|
_localizer = localizer;
|
|
_shipmentCostService = shipmentCostService;
|
|
_countryService = countryService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Verwaltung von Versandkosten zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsShipment)]
|
|
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.SettingsShipment)]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult GetShipmentCosts([FromBody] DataManager dm)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
}
|
|
|
|
var resultList = _shipmentCostService.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<ShipmentCostVm>(item)).ToList();
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Versandkostenregel
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsShipment)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Create()
|
|
{
|
|
var model = new ShipmentCostCrudVm()
|
|
{
|
|
TargetCountryIso = "--",
|
|
TargetCountryName = _localizer["Common_All"]
|
|
};
|
|
|
|
return PartialView("_Create", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Versandkostenregel
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsShipment)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(ShipmentCostCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = _shipmentCostService.Create();
|
|
_mapper.Map(model, item);
|
|
|
|
_shipmentCostService.Add(item);
|
|
await _shipmentCostService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
|
|
ModelState.Remove("TargetCountryName");
|
|
var country = _countryService.GetCountry(model.TargetCountryIso);
|
|
model.TargetCountryName = country != null ? country.Name : "";
|
|
if (model.TargetCountryIso == "--")
|
|
model.TargetCountryName = _localizer["Common_All"];
|
|
|
|
result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Versandkostenregel
|
|
/// </summary>
|
|
/// <param name="id">Id der Versandkostenregel</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsShipment)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Edit(int id)
|
|
{
|
|
var item = await _shipmentCostService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
var model = _mapper.Map<ShipmentCostCrudVm>(item);
|
|
|
|
var country = _countryService.GetCountry(item.TargetCountryIso);
|
|
model.TargetCountryName = country != null ? country.Name : "";
|
|
if (model.TargetCountryIso == "--")
|
|
model.TargetCountryName = _localizer["Common_All"];
|
|
|
|
return PartialView("_Edit", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Versandkostenregel
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.CustomersManage, Permission.SettingsShipment)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Edit(ShipmentCostCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _shipmentCostService.GetAsync(model.Id);
|
|
if (item != null)
|
|
{
|
|
_mapper.Map(model, item);
|
|
|
|
await _shipmentCostService.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("TargetCountryName");
|
|
var country = _countryService.GetCountry(model.TargetCountryIso);
|
|
model.TargetCountryName = country != null ? country.Name : "";
|
|
if (model.TargetCountryIso == "--")
|
|
model.TargetCountryName = _localizer["Common_All"];
|
|
|
|
result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Versandkostenregel
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsShipment)]
|
|
[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 _shipmentCostService.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.TargetCountryIso, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
_shipmentCostService.Remove(item);
|
|
}
|
|
else
|
|
{
|
|
if (usedCount != 0)
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.TargetCountryIso, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.TargetCountryIso}</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 _shipmentCostService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
#region Lookup
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Ländern für LookUp zurück - nur für Versandregeln
|
|
/// </summary>
|
|
/// <returns>Liste</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsShipment)]
|
|
public IActionResult LookupCountries([FromBody] DataManager dm)
|
|
{
|
|
var result = new List<LookupItemVm>();
|
|
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 countries = _countryService.GetCountries().OrderBy(c => c.Name);
|
|
|
|
var items = countries.Where(c => c.Name.ToLower().Contains(filter.ToLower()));
|
|
|
|
foreach (var item in items)
|
|
{
|
|
result.Add(new LookupItemVm() { Id = item.Iso2.ToString(), Name = $"{item.Name}" });
|
|
}
|
|
|
|
result.Insert(0, new LookupItemVm() { Id = "--", Name = _localizer["Common_All"].ToString() });
|
|
return Json(result);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|