75 lines
3.2 KiB
C#
75 lines
3.2 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using SmallSoftwareContracts.BusinessLogicsContracts;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.Extensions;
|
|
using SmallSoftwareContracts.StoragesContracts;
|
|
namespace SmallSoftwareBusinessLogic.Implementations;
|
|
|
|
|
|
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract,
|
|
IRequestStorageContract requestStorageContract, IPostStorageContract postStorageContract,
|
|
IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract
|
|
{
|
|
private readonly ILogger _logger = logger;
|
|
private readonly ISalaryStorageContract _salaryStorageContract =
|
|
salaryStorageContract;
|
|
private readonly IRequestStorageContract _requestStorageContract =
|
|
requestStorageContract;
|
|
private readonly IPostStorageContract _postStorageContract =
|
|
postStorageContract;
|
|
private readonly IWorkerStorageContract _workerStorageContract =
|
|
workerStorageContract;
|
|
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate,
|
|
DateTime toDate)
|
|
{
|
|
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}",
|
|
fromDate, toDate);
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new
|
|
NullListException();
|
|
}
|
|
public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime
|
|
fromDate, DateTime toDate, string workerId)
|
|
{
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
{
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
}
|
|
if (workerId.IsEmpty())
|
|
{
|
|
throw new ArgumentNullException(nameof(workerId));
|
|
}
|
|
if (!workerId.IsGuid())
|
|
{
|
|
throw new ValidationException("The value in the field workerId is not a unique identifier.");
|
|
}
|
|
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, { workerId} ", fromDate, toDate, workerId);
|
|
return _salaryStorageContract.GetList(fromDate, toDate, workerId) ??
|
|
throw new NullListException();
|
|
}
|
|
|
|
|
|
public void CalculateSalaryByMonth(DateTime date)
|
|
{
|
|
_logger.LogInformation("CalculateSalaryByMounth: {date}", date);
|
|
var startDate = new DateTime(date.Year, date.Month, 1);
|
|
var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
|
|
var workers = _workerStorageContract.GetList() ?? throw new NullListException();
|
|
foreach (var worker in workers)
|
|
{
|
|
var requests = _requestStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ??
|
|
throw new NullListException();
|
|
var post = _postStorageContract.GetElementById(worker.PostId) ??
|
|
throw new NullListException();
|
|
var salary = post.Salary + requests * 0.1;
|
|
_logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary);
|
|
_salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary));
|
|
}
|
|
}
|
|
}
|
|
|