forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
117 lines
4.1 KiB
C#
117 lines
4.1 KiB
C#
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.Extensions;
|
|
using MagicCarpetContracts.Infrastructure;
|
|
using MagicCarpetContracts.Resources;
|
|
using Microsoft.Extensions.Localization;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetContracts.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<SaleTourDataModel>? Tours { 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<SaleTourDataModel> saleTours)
|
|
{
|
|
Id = id;
|
|
EmployeeId = employeeId;
|
|
ClientId = clientId;
|
|
DiscountType = discountType;
|
|
IsCancel = isCancel;
|
|
Tours = saleTours;
|
|
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 = Tours?.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<SaleTourDataModel> saleTours, EmployeeDataModel employee, ClientDataModel? client) : this(id, employeeId, clientId, discountType, isCancel, saleTours)
|
|
{
|
|
Sum = sum;
|
|
Discount = discount;
|
|
_employee = employee;
|
|
_client = client;
|
|
}
|
|
|
|
public SaleDataModel(string id, string employeeId, string? clientId, int discountType,
|
|
List<SaleTourDataModel> tours) : this(id, employeeId, clientId, (DiscountType)discountType, false, tours) { }
|
|
|
|
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"], "WorkerId"));
|
|
|
|
if (!EmployeeId.IsGuid())
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "WorkerId"));
|
|
|
|
if (!ClientId?.IsGuid() ?? !ClientId?.IsEmpty() ?? false)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "BuyerId"));
|
|
|
|
if (Sum <= 0)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Sum"));
|
|
|
|
if (Tours is null)
|
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotInitialized"], "Tours"));
|
|
|
|
if (Tours.Count == 0)
|
|
throw new ValidationException(localizer["ValidationExceptionMessageNoProductsInSale"]);
|
|
|
|
Tours.ForEach(x => x.Validate(localizer));
|
|
}
|
|
}
|