2025-02-18 11:43:34 +03:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using PapaCarloContracts.BusinessLogicContracts;
|
2025-02-17 14:16:47 +03:00
|
|
|
|
using PapaCarloContracts.DataModels;
|
2025-02-18 16:41:53 +03:00
|
|
|
|
using PapaCarloContracts.Exceptions;
|
|
|
|
|
using PapaCarloContracts.Extensions;
|
2025-02-18 11:43:34 +03:00
|
|
|
|
using PapaCarloContracts.StoragesContracts;
|
2025-02-17 14:16:47 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace PapaCarloBusinessLogic.Implementations;
|
|
|
|
|
|
2025-02-18 16:41:53 +03:00
|
|
|
|
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract,
|
|
|
|
|
ICuttingStorageContract cuttingStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract
|
2025-02-17 14:16:47 +03:00
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
|
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
|
|
|
|
|
private readonly ICuttingStorageContract _cuttingStorageContract = cuttingStorageContract;
|
|
|
|
|
private readonly IPostStorageContract _postStorageContract = postStorageContract;
|
|
|
|
|
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
|
2025-02-17 14:16:47 +03:00
|
|
|
|
|
|
|
|
|
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
|
|
|
|
|
{
|
2025-02-18 16:41:53 +03: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-17 14:16:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId)
|
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
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 CalculateSalaryByMounth(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 cuttings = _cuttingStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Products.Count) ??
|
|
|
|
|
throw new NullListException();
|
|
|
|
|
var post = _postStorageContract.GetElementById(worker.PostId) ??
|
|
|
|
|
throw new NullListException();
|
|
|
|
|
var salary = cuttings * 100;
|
|
|
|
|
_logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary);
|
|
|
|
|
_salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary));
|
|
|
|
|
}
|
2025-02-17 14:16:47 +03:00
|
|
|
|
}
|
|
|
|
|
}
|