112 lines
4.2 KiB
C#
112 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Domain.Shop;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von Versandkosten ermöglicht
|
|
/// </summary>
|
|
public class ShipmentCostService : ServiceBase<ShipmentCost>, IShipmentCostService
|
|
{
|
|
private readonly IShopSettingsService _shopSettingsService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
/// <param name="shopSettingsService">Instanz eines IShopSettingsService</param>
|
|
public ShipmentCostService(IUnitOfWork unitOfWork, IShopSettingsService shopSettingsService) : base(unitOfWork)
|
|
{
|
|
_shopSettingsService = shopSettingsService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Versandkosten zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener Versandkosten</returns>
|
|
public IQueryable<ShipmentCost> Filter(string filter)
|
|
{
|
|
return Repository.Query(c => c.TargetCountryIso.Contains(filter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen von Versandkosten
|
|
/// </summary>
|
|
/// <returns>Versandkosten</returns>
|
|
public ShipmentCost Create()
|
|
{
|
|
var item = new ShipmentCost
|
|
{
|
|
|
|
};
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von Versandkosten zurück. Sucht nur im Land
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener Versandkosten</returns>
|
|
public async Task<List<ShipmentCost>> SearchAsync(string filter)
|
|
{
|
|
return (await Repository.FindAsync(c => c.TargetCountryIso.Contains(filter)).ConfigureAwait(false)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Berechnen der Versandkosten
|
|
/// </summary>
|
|
/// <param name="targetCountry">Zielland</param>
|
|
/// <param name="weight">Gewicht</param>
|
|
/// <param name="subtotal">Zwischensumme netto</param>
|
|
/// <param name="additionalShipmentCosts">Summe der zusätzlichen Versandkosten je Produkt aber in Summe</param>
|
|
/// <returns>Versandkosten netto</returns>
|
|
public async Task<decimal> CalculateShipmentAsync(string targetCountry, decimal weight, decimal subtotal, decimal additionalShipmentCosts)
|
|
{
|
|
targetCountry = targetCountry.ToUpper();
|
|
var result = 0M;
|
|
var shopSettings = await _shopSettingsService.GetAsync();
|
|
|
|
var rulesQuery = Repository.Query(c => (c.TargetCountryIso == "--" || c.TargetCountryIso == targetCountry));
|
|
if (shopSettings.ShipmentCalculationType == ShipmentCalculationType.Weight)
|
|
{
|
|
rulesQuery = rulesQuery.Where(c => c.WeightFrom <= weight && c.WeightTo >= weight);
|
|
}
|
|
else
|
|
{
|
|
rulesQuery = rulesQuery.Where(c => c.SubtotalFrom <= subtotal && c.SubtotalTo >= subtotal);
|
|
}
|
|
|
|
var rules = await rulesQuery.ToListAsync();
|
|
|
|
if (rules.Any())
|
|
{
|
|
//Wenn es mehrere Regeln gibt, dann jene für das Zielland wählen
|
|
var ruleToAppy = rules.FirstOrDefault(c => c.TargetCountryIso == targetCountry) ?? rules.FirstOrDefault();
|
|
if (ruleToAppy != null)
|
|
{
|
|
if (shopSettings.ShipmentCalculationType == ShipmentCalculationType.Weight)
|
|
{
|
|
result = ruleToAppy.FixedPrice;
|
|
}
|
|
else
|
|
{
|
|
result = (subtotal / 100) * ruleToAppy.SubtotalPercent;
|
|
}
|
|
}
|
|
}
|
|
|
|
result += additionalShipmentCosts;
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|