1229 lines
58 KiB
C#
1229 lines
58 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.Domain.Common;
|
|
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 Produkten und Kategorien
|
|
/// </summary>
|
|
[Authorize]
|
|
public class ProductController : BaseController
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IStringLocalizer<ProductController> _localizer;
|
|
private readonly ILanguageService _languageService;
|
|
private readonly IProductCategoryService _productCategoryService;
|
|
private readonly IProductService _productService;
|
|
private readonly ITaxRateService _taxRateService;
|
|
private readonly IAdvertisementCategoryService _advertisementCategoryService;
|
|
private readonly IBranchService _branchService;
|
|
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="languageService">Instanz eines ILanguageService</param>
|
|
/// <param name="productCategoryService">Instanz eines IProductCategoryService</param>
|
|
/// <param name="productService">Instanz eines IProductService</param>
|
|
/// <param name="taxRateService">Instanz eines ITaxRateService</param>
|
|
/// <param name="advertisementCategoryService">Instanz eines IAdvertisementCategoryService</param>
|
|
/// <param name="branchService">Instanz eines IBranchService</param>
|
|
/// <param name="countryService">Instanz eines ICountryService</param>
|
|
public ProductController(IMapper mapper, IStringLocalizer<ProductController> localizer, ILanguageService languageService, IProductCategoryService productCategoryService, IProductService productService,
|
|
ITaxRateService taxRateService, IAdvertisementCategoryService advertisementCategoryService, IBranchService branchService, ICountryService countryService)
|
|
{
|
|
_mapper = mapper;
|
|
_localizer = localizer;
|
|
_languageService = languageService;
|
|
_productCategoryService = productCategoryService;
|
|
_productService = productService;
|
|
_taxRateService = taxRateService;
|
|
_advertisementCategoryService = advertisementCategoryService;
|
|
_branchService = branchService;
|
|
_countryService = countryService;
|
|
}
|
|
|
|
#region Kategorien
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Verwaltung von Produktkategorien
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProductCategories)]
|
|
public IActionResult IndexCategories()
|
|
{
|
|
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.SettingsProductCategories)]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> GetCategories([FromBody] DataManager dm)
|
|
{
|
|
int? parentId = null;
|
|
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
|
|
if (dm.Where != null)
|
|
{
|
|
foreach (var whereFilter in dm.Where)
|
|
{
|
|
if (whereFilter.Field == "parentId" && whereFilter.value != null)
|
|
parentId = int.Parse(whereFilter.value.ToString());
|
|
if (whereFilter.predicates == null)
|
|
continue;
|
|
foreach (var whereFilterPredicate in whereFilter.predicates)
|
|
{
|
|
if (whereFilterPredicate.Field == "visibility")
|
|
whereFilterPredicate.value = (ShopVisibility)((int)((long)whereFilterPredicate.value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var resultList = _productCategoryService.FilterWithNames(parentId, dm?.SearchValue ?? "", SelectedLanguage, FallbackLanguage, false);
|
|
|
|
//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<ProductCategoryListVm>(item)).OrderBy(c => c.DisplayOrder).ThenBy(c => c.Title).ToList();
|
|
|
|
foreach (var itemVm in resultListVm)
|
|
{
|
|
itemVm.ShopVisibilityText = itemVm.Visibility.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.HasChildren = await _productCategoryService.HasChildrenAsync(itemVm.Id);
|
|
}
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Kategorie
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProductCategories)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult CreateCategory()
|
|
{
|
|
var model = new ProductCategoryCrudVm()
|
|
{
|
|
TextVms = new List<ProductCategoryTextVm>(),
|
|
Visibility = ShopVisibilityVm.None,
|
|
ParentId = -1,
|
|
ParentTitle = _localizer["Common_None"],
|
|
DisplayOrder = 100
|
|
};
|
|
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ProductCategoryTextVm() { Id = model.Id, Language = language, Name = string.Empty, Description = string.Empty, ImageLanguage = string.Empty });
|
|
}
|
|
|
|
return PartialView("_CreateCategory", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Kategorie
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProductCategories)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateCategory(ProductCategoryCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = _productCategoryService.Create();
|
|
_mapper.Map(model, item);
|
|
if (model.ParentId == -1)
|
|
item.ParentId = null;
|
|
item.Created = DateTimeOffset.UtcNow;
|
|
|
|
|
|
// Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
item.Set("Description", textVm.Language, textVm.Description);
|
|
item.Set("ImageLanguage", textVm.Language, textVm.ImageLanguage);
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.ImageLanguage))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.ImageLanguage);
|
|
var fileName = Path.GetFileName(textVm.ImageLanguage);
|
|
var filenameToUse = FileServiceHelper.GetProductCategoryPath(item.Id) + $"cat_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("ImageLanguage", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.ImageLanguage);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
|
|
_productCategoryService.Add(item);
|
|
await _productCategoryService.CommitAsync(User.Identity.Name);
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.Image))
|
|
{
|
|
//Nun das Bild umbenennen und dann kopieren. Ebenso Thumbnails erstellen
|
|
var extension = Path.GetExtension(model.Image);
|
|
var fileName = Path.GetFileName(model.Image);
|
|
|
|
var filenameToUse = FileServiceHelper.GetProductCategoryPath(item.Id) + $"cat--{Guid.NewGuid():N}{extension}";
|
|
item.Image = filenameToUse;
|
|
await _productCategoryService.CommitAsync(User.Identity.Name);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
|
|
//Altes Löschen
|
|
await FileService.DeleteAsync(FileServiceHelper.TempContainer, model.Image);
|
|
}
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
|
|
ModelState.Remove("ParentTitle");
|
|
var parent = await _productCategoryService.GetAsync(model.ParentId);
|
|
model.ParentTitle = parent != null ? parent.Title : "";
|
|
|
|
result.Html = await PartialView("_CreateCategory", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Produktkategorie
|
|
/// </summary>
|
|
/// <param name="id">Id der Kategorie</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProductCategories)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> EditCategory(int id)
|
|
{
|
|
var item = await _productCategoryService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
var model = _mapper.Map<ProductCategoryCrudVm>(item);
|
|
|
|
model.TextVms = new List<ProductCategoryTextVm>();
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ProductCategoryTextVm()
|
|
{
|
|
Id = item.Id,
|
|
Language = language,
|
|
Name = item.Get("Name", language, true),
|
|
Description = item.Get("Description", language, true),
|
|
ImageLanguage = item.Get("ImageLanguage", language, true)
|
|
});
|
|
}
|
|
|
|
if (item.ParentId == null)
|
|
model.ParentId = -1;
|
|
|
|
var parent = await _productCategoryService.GetAsync(model.ParentId);
|
|
model.ParentTitle = parent != null ? parent.Title : _localizer["Common_None"];
|
|
|
|
return PartialView("_EditCategory", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Produktkategorie
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProductCategories)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> EditCategory(ProductCategoryCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _productCategoryService.GetAsync(model.Id);
|
|
if (item is { Deleted: false })
|
|
{
|
|
var oldImage = item.Image;
|
|
|
|
_mapper.Map(model, item);
|
|
if (model.ParentId == -1)
|
|
item.ParentId = null;
|
|
|
|
//Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
item.Set("Description", textVm.Language, textVm.Description);
|
|
var oldLangImage = item.Get("ImageLanguage", textVm.Language, true);
|
|
item.Set("ImageLanguage", textVm.Language, textVm.ImageLanguage);
|
|
|
|
if (oldLangImage != textVm.ImageLanguage)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldLangImage))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldLangImage);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.ImageLanguage))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.ImageLanguage);
|
|
var fileName = Path.GetFileName(textVm.ImageLanguage);
|
|
var filenameToUse = FileServiceHelper.GetProductCategoryPath(item.Id) + $"cat_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("ImageLanguage", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.ImageLanguage);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (oldImage != item.Image)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldImage))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldImage);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(item.Image))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(item.Image);
|
|
var fileName = Path.GetFileName(item.Image);
|
|
var filenameToUse = FileServiceHelper.GetProductCategoryPath(item.Id) + $"cat-{Guid.NewGuid():N}{extension}";
|
|
item.Image = filenameToUse;
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
|
|
await _productCategoryService.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_Entity_Deleted"].Value);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("ParentTitle");
|
|
var parent = await _productCategoryService.GetAsync(model.ParentId);
|
|
model.ParentTitle = parent != null ? parent.Title : "";
|
|
|
|
result.Html = await PartialView("_EditCategory", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Produktkategorie.
|
|
/// Untergeordnete Kategorien rutschen somit 1 Ebene nach oben
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <param name="forceDelete">Sollen die Daten physisch gelöscht werden?</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProductCategories)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> DeleteCategory(List<int> ids, bool forceDelete = false)
|
|
{
|
|
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 _productCategoryService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
//Kinder hochsetzen
|
|
await _productCategoryService.ResetParentAsync(item.Id, item.ParentId);
|
|
|
|
var useCount = 0;
|
|
if (useCount == 0)
|
|
{
|
|
await _productService.ResetProductCategoryAsync(item.Id, false);
|
|
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Title, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
if (forceDelete)
|
|
{
|
|
var postingPath = FileServiceHelper.GetProductCategoryPath(item.Id);
|
|
await FileService.ClearDirectoryAsync(FileServiceHelper.DocumentContainer, postingPath);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 100);
|
|
|
|
var languages = item.GetLanguages("ImageLanguage");
|
|
foreach (var language in languages)
|
|
{
|
|
item.SetLanguage(language);
|
|
var imageLocalized = item.ImageLanguage;
|
|
if (string.IsNullOrWhiteSpace(imageLocalized)) continue;
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 100);
|
|
}
|
|
|
|
_productCategoryService.Remove(item);
|
|
}
|
|
else
|
|
{
|
|
item.Deleted = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (useCount != 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>{useCount}</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 _productCategoryService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Produkte / Artikel
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Produkt-Verwaltung zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
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.SettingsProducts)]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult GetProducts([FromBody] DataManager dm)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
|
|
if (dm.Where != null)
|
|
{
|
|
foreach (var whereFilter in dm.Where)
|
|
{
|
|
if (whereFilter.predicates == null)
|
|
continue;
|
|
foreach (var whereFilterPredicate in whereFilter.predicates)
|
|
{
|
|
if (whereFilterPredicate.Field == "visibility")
|
|
whereFilterPredicate.value = (ShopVisibility)((int)((long)whereFilterPredicate.value));
|
|
if (whereFilterPredicate.Field == "productType")
|
|
whereFilterPredicate.value = (ProductType)((int)((long)whereFilterPredicate.value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var resultList = _productService.FilterWithNames(dm?.SearchValue ?? "", SelectedLanguage, FallbackLanguage, false);
|
|
|
|
//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<ProductListVm>(item)).ToList();
|
|
|
|
foreach (var itemVm in resultListVm)
|
|
{
|
|
itemVm.Image = Tools.GetLogoThumb(itemVm.Image, 100);
|
|
itemVm.VisibilityText = itemVm.Visibility.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.ProductTypeText = itemVm.ProductType.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.PriceTypeText = itemVm.PriceType.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.PriceAliquotTypeText = itemVm.PriceAliquotType.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.ProductStartTypeText = itemVm.ProductStartType.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.BannerLocationText = itemVm.BannerLocation.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.BannerSizeText = itemVm.BannerSize.GetDisplayName(AnnotationsLocalizer);
|
|
}
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen eines Produktes
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Create()
|
|
{
|
|
var model = new ProductCrudVm()
|
|
{
|
|
ProductType = ProductTypeVm.Physical,
|
|
ListingType = ListingTypeVm.Base,
|
|
Visibility = ShopVisibilityVm.None,
|
|
IsShipEnabled = true,
|
|
OrderMinimumQuantity = 1,
|
|
OrderMaximumQuantity = 1,
|
|
Price = 0m,
|
|
OldPrice = 0m,
|
|
Weight = 0m,
|
|
Height = 0m,
|
|
Width = 0m,
|
|
Length = 0m,
|
|
DisplayOrder = 100,
|
|
Created = DateTimeOffset.UtcNow,
|
|
TaxRateId = -1,
|
|
AdvertisementCategoryName = _localizer["Common_None"],
|
|
ProductCategoryName = _localizer["Common_None"],
|
|
TaxRateName = _localizer["Common_None"],
|
|
BranchName = _localizer["Common_None"],
|
|
TextVms = new List<ProductTextVm>()
|
|
};
|
|
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ProductTextVm() { Id = model.Id, Language = language, Name = string.Empty, ShortDescription = string.Empty, Description = string.Empty, ImageLanguage = string.Empty });
|
|
}
|
|
|
|
return PartialView("_Create", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen eines Produktes
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(ProductCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var canCreate = true;
|
|
if (model.ProductType == ProductTypeVm.Listing)
|
|
{
|
|
var exists = await _productService.CheckListingExistsAsync((ListingType)model.ListingType, model.BranchId, (PriceType)model.PriceType);
|
|
if (exists)
|
|
{
|
|
canCreate = false;
|
|
ModelState.AddModelError("", _localizer["Err_Product_Combination_Exists"].Value);
|
|
}
|
|
}
|
|
//if (model.ProductType == ProductTypeVm.Advertisement)
|
|
//{
|
|
// var exists = await _productService.CheckAdvertisementExistsAsync(model.AdvertisementCategoryId);
|
|
// if (exists)
|
|
// {
|
|
// canCreate = false;
|
|
// ModelState.AddModelError("", _localizer["Err_Product_Combination_Exists"].Value);
|
|
// }
|
|
//}
|
|
|
|
if (canCreate)
|
|
{
|
|
var item = _productService.Create();
|
|
_mapper.Map(model, item);
|
|
item.Created = DateTimeOffset.UtcNow;
|
|
|
|
// Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
item.Set("ShortDescription", textVm.Language, textVm.ShortDescription);
|
|
item.Set("Description", textVm.Language, textVm.Description);
|
|
item.Set("ImageLanguage", textVm.Language, textVm.ImageLanguage);
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.ImageLanguage))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.ImageLanguage);
|
|
var fileName = Path.GetFileName(textVm.ImageLanguage);
|
|
var filenameToUse = FileServiceHelper.GetProductPath(item.Id) + $"p_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("ImageLanguage", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.ImageLanguage);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
|
|
_productService.Add(item);
|
|
await _productService.CommitAsync(User.Identity.Name);
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.Image))
|
|
{
|
|
//Nun das Bild umbenennen und dann kopieren. Ebenso Thumbnails erstellen
|
|
var extension = Path.GetExtension(model.Image);
|
|
var fileName = Path.GetFileName(model.Image);
|
|
|
|
var filenameToUse = FileServiceHelper.GetProductPath(item.Id) + $"p_{Guid.NewGuid():N}{extension}";
|
|
item.Image = filenameToUse;
|
|
await _productService.CommitAsync(User.Identity.Name);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
|
|
//Altes Löschen
|
|
await FileService.DeleteAsync(FileServiceHelper.TempContainer, model.Image);
|
|
}
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("ProductCategoryName");
|
|
var category = await _productCategoryService.GetAsync(model.ProductCategoryId);
|
|
model.ProductCategoryName = category != null ? category.Name : "";
|
|
|
|
ModelState.Remove("TaxRateName");
|
|
var taxRate = await _taxRateService.GetAsync(model.TaxRateId);
|
|
model.TaxRateName = taxRate != null ? $"{taxRate.Name} ({taxRate.Value:n2}%)" : "";
|
|
|
|
ModelState.Remove("AdvertisementCategoryName");
|
|
var advertisementCategory = await _advertisementCategoryService.GetAsync(model.AdvertisementCategoryId);
|
|
model.AdvertisementCategoryName = advertisementCategory != null ? advertisementCategory.Name : "";
|
|
|
|
ModelState.Remove("BranchName");
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten eines Produktes
|
|
/// </summary>
|
|
/// <param name="id">Id des Produktes</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Edit(int id)
|
|
{
|
|
var item = await _productService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
var model = _mapper.Map<ProductCrudVm>(item);
|
|
|
|
model.TextVms = new List<ProductTextVm>();
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new ProductTextVm()
|
|
{
|
|
Id = item.Id,
|
|
Language = language,
|
|
Name = item.Get("Name", language, true),
|
|
ShortDescription = item.Get("ShortDescription", language, true),
|
|
Description = item.Get("Description", language, true),
|
|
ImageLanguage = item.Get("ImageLanguage", language, true)
|
|
});
|
|
}
|
|
|
|
if (item.ProductCategoryId == null)
|
|
model.ProductCategoryId = -1;
|
|
|
|
var productCategory = await _productCategoryService.GetAsync(model.ProductCategoryId);
|
|
model.ProductCategoryName = productCategory != null ? productCategory.Name : _localizer["Common_None"];
|
|
|
|
var taxRate = await _taxRateService.GetAsync(model.TaxRateId);
|
|
model.TaxRateName = taxRate != null ? $"{taxRate.Name} ({taxRate.Value:n2}%)" : "";
|
|
|
|
var advertisementCategory = await _advertisementCategoryService.GetAsync(model.AdvertisementCategoryId);
|
|
model.AdvertisementCategoryName = advertisementCategory != null ? advertisementCategory.Name : "";
|
|
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
return PartialView("_Edit", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten eines Produktes
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Edit(ProductCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _productService.GetAsync(model.Id);
|
|
if (item is { Deleted: false })
|
|
{
|
|
var oldImage = item.Image;
|
|
|
|
_mapper.Map(model, item);
|
|
if (model.ProductCategoryId == -1)
|
|
item.ProductCategoryId = null;
|
|
|
|
//Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Name", textVm.Language, textVm.Name);
|
|
item.Set("ShortDescription", textVm.Language, textVm.ShortDescription);
|
|
item.Set("Description", textVm.Language, textVm.Description);
|
|
var oldLangImage = item.Get("ImageLanguage", textVm.Language, true);
|
|
item.Set("ImageLanguage", textVm.Language, textVm.ImageLanguage);
|
|
|
|
if (oldLangImage != textVm.ImageLanguage)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldLangImage))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldLangImage);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldLangImage, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(textVm.ImageLanguage))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(textVm.ImageLanguage);
|
|
var fileName = Path.GetFileName(textVm.ImageLanguage);
|
|
var filenameToUse = FileServiceHelper.GetProductPath(item.Id) + $"p_{textVm.Language}-{Guid.NewGuid():N}{extension}";
|
|
item.Set("ImageLanguage", textVm.Language, filenameToUse);
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, textVm.ImageLanguage);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (oldImage != item.Image)
|
|
{
|
|
//Alte Daten löschen, neue Daten anlegen
|
|
if (!string.IsNullOrWhiteSpace(oldImage))
|
|
{
|
|
await FileService.DeleteAsync(FileServiceHelper.DocumentContainer, oldImage);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, oldImage, 100);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(item.Image))
|
|
{
|
|
//Neue Bilddaten verwenden....
|
|
var extension = Path.GetExtension(item.Image);
|
|
var fileName = Path.GetFileName(item.Image);
|
|
var filenameToUse = FileServiceHelper.GetProductPath(item.Id) + $"p_{Guid.NewGuid():N}{extension}";
|
|
item.Image = filenameToUse;
|
|
|
|
//Kopieren
|
|
var tempFile = await FileService.GetAsync(FileServiceHelper.TempContainer, model.Image);
|
|
await FileService.StoreAsync(FileServiceHelper.DocumentContainer, filenameToUse, tempFile);
|
|
|
|
//Thumbnails
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 400);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 200);
|
|
await GenerateThumbnailByWidth(FileServiceHelper.DocumentContainer, filenameToUse, 100);
|
|
}
|
|
}
|
|
|
|
await _productService.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_Entity_Deleted"].Value);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("ProductCategoryName");
|
|
var category = await _productCategoryService.GetAsync(model.ProductCategoryId);
|
|
model.ProductCategoryName = category != null ? category.Name : "";
|
|
|
|
ModelState.Remove("TaxRateName");
|
|
var taxRate = await _taxRateService.GetAsync(model.TaxRateId);
|
|
model.TaxRateName = taxRate != null ? $"{taxRate.Name} ({taxRate.Value:n2}%)" : "";
|
|
|
|
ModelState.Remove("AdvertisementCategoryName");
|
|
var advertisementCategory = await _advertisementCategoryService.GetAsync(model.AdvertisementCategoryId);
|
|
model.AdvertisementCategoryName = advertisementCategory != null ? advertisementCategory.Name : "";
|
|
|
|
ModelState.Remove("BranchName");
|
|
var branch = await _branchService.GetAsync(model.BranchId);
|
|
model.BranchName = branch != null ? branch.Name : "";
|
|
|
|
result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen eines Produktes.
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <param name="forceDelete">Sollen die Daten physisch gelöscht werden?</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProductCategories)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Delete(List<int> ids, bool forceDelete = false)
|
|
{
|
|
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 _productService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
var useCount = 0;
|
|
if (useCount == 0)
|
|
{
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Name, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
if (forceDelete)
|
|
{
|
|
var postingPath = FileServiceHelper.GetProductPath(item.Id);
|
|
await FileService.ClearDirectoryAsync(FileServiceHelper.DocumentContainer, postingPath);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, item.Image, 100);
|
|
|
|
var languages = item.GetLanguages("ImageLanguage");
|
|
foreach (var language in languages)
|
|
{
|
|
item.SetLanguage(language);
|
|
var imageLocalized = item.ImageLanguage;
|
|
if (string.IsNullOrWhiteSpace(imageLocalized)) continue;
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 400);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 200);
|
|
await RemoveThumbnail(FileServiceHelper.DocumentContainer, imageLocalized, 100);
|
|
}
|
|
|
|
_productService.Remove(item);
|
|
}
|
|
else
|
|
{
|
|
item.Deleted = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (useCount != 0)
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.Name, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.Name}</strong>", $"<strong>{useCount}</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 _productService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Ländereinstellungen
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Ländereinstellungen für ein Produkt zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
public async Task<IActionResult> Countries(int productId)
|
|
{
|
|
var product = await _productService.GetAsync(productId);
|
|
if (product is { Deleted: false })
|
|
{
|
|
var model = _mapper.Map<ProductVm>(product);
|
|
return PartialView("_Countries", model);
|
|
}
|
|
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück
|
|
/// </summary>
|
|
/// <param name="dm">Abfragekriterien</param>
|
|
/// <param name="productId">Id des Produktes</param>
|
|
/// <returns>Liste von gefundenen Entitäten</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult GetCountries([FromBody] DataManager dm, int productId)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
}
|
|
|
|
var resultList = _productService.FilterCountry(dm?.SearchValue ?? "", productId);
|
|
|
|
//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<ProductCountryVm>(item)).ToList();
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Produkteinstellung für ein Land
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult CreateCountry(int productId)
|
|
{
|
|
var model = new ProductCountryCrudVm()
|
|
{
|
|
ProductId = productId,
|
|
Price = 0m,
|
|
OldPrice = 0m,
|
|
Price2 = 0m,
|
|
OldPrice2 = 0m,
|
|
CountryName = _localizer["Common_None"]
|
|
};
|
|
|
|
return PartialView("_CreateCountry", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Produkteinstellung für ein Land
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateCountry(ProductCountryCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var existing = await _productService.GetCountryAsync(model.ProductId, model.CountryIso);
|
|
if (existing == null)
|
|
{
|
|
var item = _productService.CreateCountry(model.ProductId, model.CountryIso);
|
|
_mapper.Map(model, item);
|
|
_productService.AddCountry(item);
|
|
await _productService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("CountryIso", _localizer["Err_ProductCountry_Exists"].Value);
|
|
}
|
|
}
|
|
|
|
ModelState.Remove("CountryName");
|
|
var country = _countryService.GetCountry(model.CountryIso);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
result.Html = await PartialView("_CreateCountry", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Produkteinstellung für ein Land
|
|
/// </summary>
|
|
/// <param name="id">Id der Produkteinstellung</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> EditCountry(int id)
|
|
{
|
|
var item = await _productService.GetCountryAsync(id);
|
|
if (item != null)
|
|
{
|
|
var model = _mapper.Map<ProductCountryCrudVm>(item);
|
|
|
|
var country = _countryService.GetCountry(model.CountryIso);
|
|
model.CountryName = country != null ? country.Name : "";
|
|
|
|
return PartialView("_EditCountry", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Produkteinstellung für ein Land
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> EditCountry(ProductCountryCrudVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _productService.GetCountryAsync(model.Id);
|
|
if (item != null)
|
|
{
|
|
_mapper.Map(model, item);
|
|
await _productService.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("_EditCountry", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer oder mehrerer Produkteinstellung
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entitäten</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProducts)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> DeleteCountry(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 _productService.GetCountryAsync(id);
|
|
if (item != null)
|
|
{
|
|
var usedCount = 0; //await UserService.CountByTenantAsync(item.Id);
|
|
|
|
if (usedCount == 0)
|
|
{
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.CountryIso, Success = true, NotFound = false, ErrorMessage = "" });
|
|
_productService.RemoveCountry(item);
|
|
}
|
|
else
|
|
{
|
|
batchResult.Success = false;
|
|
if (usedCount > 0)
|
|
{
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.CountryIso, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.CountryIso}</strong>", $"<strong>{usedCount}</strong>") + "<br/>" });
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
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 _productService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Lookup
|
|
|
|
/// <summary>
|
|
/// Gibt eine Kategorien-Baum für Kunden zurück
|
|
/// </summary>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.CustomerOnly)]
|
|
public async Task<IActionResult> GetCategoriesCustomerAsync()
|
|
{
|
|
var items = await _productCategoryService.GetTreeAsync(OrderSource.Customer, SelectedLanguage, FallbackLanguage, -1, false);
|
|
var result = _mapper.Map<List<ProductCategoryTreeVm>>(items);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Produktkategorien für LookUp zurück
|
|
/// </summary>
|
|
/// <param name="excludeId">Id der Kategorie die ausgeschlossen werden soll</param>
|
|
/// <param name="includeNone">Soll "Keine(r)" integriert werden</param>
|
|
/// <returns>Liste</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
public async Task<IActionResult> LookupParents(int excludeId = -1, bool includeNone = false)
|
|
{
|
|
var items = await _productCategoryService.GetTreeAsync(SelectedLanguage, FallbackLanguage, excludeId, false);
|
|
var result = _mapper.Map<List<ProductCategoryTreeVm>>(items);
|
|
|
|
if (includeNone)
|
|
result.Insert(0, new ProductCategoryTreeVm() { Id = -1, Name = _localizer["Common_None"].ToString() });
|
|
|
|
return Json(result);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|