111 lines
4.2 KiB
C#
111 lines
4.2 KiB
C#
using Microsoft.Extensions.Localization;
|
|
using CandyHouseContracts.Enums;
|
|
using CandyHouseContracts.Exceptions;
|
|
using CandyHouseContracts.Extensions;
|
|
using CandyHouseContracts.Infrastructure;
|
|
using CandyHouseContracts.Resources;
|
|
|
|
namespace CandyHouseContracts.DataModels;
|
|
|
|
internal class SaleDataModel : IValidation
|
|
{
|
|
private readonly ClientDataModel? _client;
|
|
|
|
private readonly EmployeeDataModel? _employee;
|
|
|
|
public string Id { get; private set; }
|
|
|
|
public string EmployeeId { get; private set; }
|
|
|
|
public string? ClientId { get; private set; }
|
|
|
|
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
|
|
|
|
public double Sum { get; private set; }
|
|
|
|
public DiscountType DiscountType { get; private set; }
|
|
|
|
public double Discount { get; private set; }
|
|
|
|
public bool IsCancel { get; private set; }
|
|
|
|
public List<SaleProductDataModel> Products { get; private set; }
|
|
|
|
public string ClientFIO => _client?.FIO ?? string.Empty;
|
|
|
|
public string EmployeeFIO => _employee?.FIO ?? string.Empty;
|
|
|
|
public SaleDataModel(string id, string employeeId, string? clientId, DiscountType discountType, bool isCancel, List<SaleProductDataModel> saleProducts)
|
|
{
|
|
Id = id;
|
|
EmployeeId = employeeId;
|
|
ClientId = clientId;
|
|
DiscountType = discountType;
|
|
IsCancel = isCancel;
|
|
Products = saleProducts;
|
|
var percent = 0.0;
|
|
foreach (DiscountType elem in Enum.GetValues<DiscountType>())
|
|
{
|
|
if ((elem & discountType) != 0)
|
|
{
|
|
switch (elem)
|
|
{
|
|
case DiscountType.None:
|
|
break;
|
|
case DiscountType.OnSale:
|
|
percent += 0.1;
|
|
break;
|
|
case DiscountType.RegularCustomer:
|
|
percent += 0.5;
|
|
break;
|
|
case DiscountType.Certificate:
|
|
percent += 0.3;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
Sum = Products?.Sum(x => x.Price * x.Count) ?? 0;
|
|
Discount = Sum * percent;
|
|
}
|
|
|
|
public SaleDataModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType, double discount, bool isCancel, List<SaleProductDataModel> saleProducts, EmployeeDataModel employee, ClientDataModel? client) : this(id, employeeId, clientId, discountType, isCancel, saleProducts)
|
|
{
|
|
Sum = sum;
|
|
Discount = discount;
|
|
_employee = employee;
|
|
_client = client;
|
|
}
|
|
|
|
public SaleDataModel(string id, string employeeId, string? clientId, int discountType, List<SaleProductDataModel> products) : this(id, employeeId, clientId, (DiscountType)discountType, false, products) { }
|
|
|
|
public SaleDataModel() : this(string.Empty, string.Empty, null, DiscountType.None, false, new List<SaleProductDataModel>()) { }
|
|
|
|
public void Validate(IStringLocalizer<Messages> localizer)
|
|
{
|
|
if (Id.IsEmpty())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Id"));
|
|
|
|
if (!Id.IsGuid())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id"));
|
|
|
|
if (EmployeeId.IsEmpty())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "EmployeeId"));
|
|
|
|
if (!EmployeeId.IsGuid())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "EmployeeId"));
|
|
|
|
if (!ClientId?.IsGuid() ?? !ClientId?.IsEmpty() ?? false)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "ClientId"));
|
|
|
|
if (Sum <= 0)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Sum"));
|
|
|
|
if (Products is null)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotInitialized"], "Products"));
|
|
|
|
if (Products.Count == 0)
|
|
throw new ValidationException(localizer["ValidationExceptionMessageNoProductsInSale"]);
|
|
|
|
Products.ForEach(x => x.Validate(localizer));
|
|
}
|
|
} |