2025-02-16 21:09:05 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using TheCyclopsContracts.BusinessLogicContracts;
|
2025-02-16 05:35:41 +04:00
|
|
|
|
using TheCyclopsContracts.DataModels;
|
2025-02-16 21:31:02 +04:00
|
|
|
|
using TheCyclopsContracts.Exceptions;
|
|
|
|
|
using TheCyclopsContracts.Extensions;
|
2025-02-16 21:09:05 +04:00
|
|
|
|
using TheCyclopsContracts.StoragesContracts;
|
2025-02-16 05:35:41 +04:00
|
|
|
|
|
|
|
|
|
namespace TheCyclopsBusinessLogic.Implementations;
|
|
|
|
|
|
2025-02-16 21:09:05 +04:00
|
|
|
|
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract,
|
|
|
|
|
IInstallationStorageContract installationStorageContract,
|
|
|
|
|
IPostStorageContract postStorageContract, IEmployeeStorageContract employeeStorageContract,
|
|
|
|
|
ILogger logger) : ISalaryBusinessLogicContract
|
2025-02-16 05:35:41 +04:00
|
|
|
|
{
|
2025-02-16 21:09:05 +04:00
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
|
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
|
|
|
|
|
private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract;
|
|
|
|
|
private readonly IPostStorageContract _postStorageContract = postStorageContract;
|
|
|
|
|
private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract;
|
|
|
|
|
|
2025-02-16 05:35:41 +04:00
|
|
|
|
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
|
|
|
|
|
{
|
2025-02-16 21:31:02 +04:00
|
|
|
|
_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();
|
2025-02-16 05:35:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SalaryDataModel> GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId)
|
|
|
|
|
{
|
2025-02-16 21:31:02 +04:00
|
|
|
|
if (fromDate.IsDateNotOlder(toDate))
|
|
|
|
|
{
|
|
|
|
|
throw new IncorrectDatesException(fromDate, toDate);
|
|
|
|
|
}
|
|
|
|
|
if (employeeId.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(employeeId));
|
|
|
|
|
}
|
|
|
|
|
if (!employeeId.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("The value in the field employeeId is not a unique identifier.");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {employeeId}", fromDate, toDate, employeeId);
|
|
|
|
|
return _salaryStorageContract.GetList(fromDate, toDate, employeeId) ?? throw new NullListException();
|
2025-02-16 05:35:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CalculateSalaryByMounth(DateTime date)
|
|
|
|
|
{
|
2025-02-16 21:31:02 +04:00
|
|
|
|
_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 employees = _employeeStorageContract.GetList() ?? throw new NullListException();
|
|
|
|
|
foreach (var employee in employees)
|
|
|
|
|
{
|
|
|
|
|
var installations = _installationStorageContract.GetList(startDate, finishDate, employeeId: employee.Id)?.Sum(x => x.InstallationPrice) ??
|
|
|
|
|
throw new NullListException();
|
|
|
|
|
var post = _postStorageContract.GetElementById(employee.PostId) ??
|
|
|
|
|
throw new NullListException();
|
|
|
|
|
var salary = post.Salary + installations * 0.1;
|
|
|
|
|
_logger.LogDebug("The employee {employeeId} was paid a salary of {salary}", employee.Id, salary);
|
|
|
|
|
_salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, finishDate, salary));
|
|
|
|
|
}
|
2025-02-16 05:35:41 +04:00
|
|
|
|
}
|
|
|
|
|
}
|