110 lines
3.7 KiB
C#
110 lines
3.7 KiB
C#
using CandyHouseContracts.Enums;
|
|
using CandyHouseContracts.Exceptions;
|
|
using CandyHouseContracts.Extensions;
|
|
using CandyHouseContracts.Infrastructure;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CandyHouseContracts.DataModels;
|
|
|
|
public 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 void Validate()
|
|
{
|
|
if (Id.IsEmpty())
|
|
throw new ValidationException("Field Id is empty");
|
|
|
|
if (!Id.IsGuid())
|
|
throw new ValidationException("The value in the field Id is not a unique identifier");
|
|
|
|
if (EmployeeId.IsEmpty())
|
|
throw new ValidationException("Field EmployeeId is empty");
|
|
|
|
if (!EmployeeId.IsGuid())
|
|
throw new ValidationException("The value in the field EmployeeId is not a unique identifier");
|
|
|
|
if (!ClientId?.IsGuid() ?? !ClientId?.IsEmpty() ?? false)
|
|
throw new ValidationException("The value in the field EmploeerId is not a unique identifier");
|
|
|
|
if (Sum <= 0)
|
|
throw new ValidationException("Field Sum is less than or equal to 0");
|
|
|
|
if ((Products?.Count ?? 0) == 0)
|
|
throw new ValidationException("The sale must include products");
|
|
}
|
|
}
|