184 lines
7.2 KiB
C#
184 lines
7.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Company.Domain.SalaryAidAgg;
|
|
using CompanyManagment.App.Contracts.SalaryAid;
|
|
using _0_Framework.Application;
|
|
using Company.Domain.CheckoutAgg;
|
|
using CompanyManagment.App.Contracts.Checkout;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Company.Domain.CustomizeCheckoutAgg;
|
|
using Company.Domain.EmployeeAgg;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class SalaryAidApplication : ISalaryAidApplication
|
|
{
|
|
private readonly ISalaryAidRepository _salaryAidRepository;
|
|
private readonly ICustomizeCheckoutRepository _customizeCheckoutRepository;
|
|
private readonly IEmployeeRepository _employeeRepository;
|
|
|
|
public SalaryAidApplication(ISalaryAidRepository salaryAidRepository, ICustomizeCheckoutRepository customizeCheckoutRepository, IEmployeeRepository employeeRepository)
|
|
{
|
|
_salaryAidRepository = salaryAidRepository;
|
|
_customizeCheckoutRepository = customizeCheckoutRepository;
|
|
_employeeRepository = employeeRepository;
|
|
}
|
|
|
|
public List<SalaryAidViewModel> GetSearchList(SalaryAidSearchViewModel searchViewModel)
|
|
{
|
|
return _salaryAidRepository.GetSearchList(searchViewModel);
|
|
}
|
|
|
|
public EditSalaryAidViewModel GetDetails(long id)
|
|
{
|
|
return _salaryAidRepository.GetDetails(id);
|
|
}
|
|
|
|
public OperationResult Create(CreateSalaryAidViewModel command)
|
|
{
|
|
var op = new OperationResult();
|
|
if (!command.SalaryDateTime.TryToGeorgianDateTime(out var startDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (startDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت مساعده می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
|
|
var month = Convert.ToInt32(command.SalaryDateTime.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.SalaryDateTime.Substring(0, 4));
|
|
|
|
|
|
if (_customizeCheckoutRepository.Exists(x => x.WorkshopId == command.WorkshopId && command.EmployeeIds.Contains(x.EmployeeId) && x.YearInt == year && x.MonthInt == month))
|
|
{
|
|
return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی صادر شده است مساعده ای دهید");
|
|
}
|
|
|
|
foreach (var employeeId in command.EmployeeIds)
|
|
{
|
|
|
|
var entity = new SalaryAid(employeeId, command.WorkshopId, command.Amount.MoneyToDouble(), startDate);
|
|
_salaryAidRepository.Create(entity);
|
|
|
|
}
|
|
_salaryAidRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult Edit(EditSalaryAidViewModel command)
|
|
{
|
|
var op = new OperationResult();
|
|
|
|
if (!command.SalaryDateTime.TryToGeorgianDateTime(out var startDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (startDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت مساعده می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
var entity = _salaryAidRepository.Get(command.Id);
|
|
if (entity == null)
|
|
return op.Failed("چنین مساعده ای وجود ندارد");
|
|
|
|
var month = Convert.ToInt32(command.SalaryDateTime.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.SalaryDateTime.Substring(0, 4));
|
|
|
|
if (_customizeCheckoutRepository.Exists(x => x.WorkshopId == entity.WorkshopId && entity.EmployeeId == x.EmployeeId && x.YearInt == year && x.MonthInt == month))
|
|
return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی صادر شده است مساعده ای دهید");
|
|
|
|
|
|
entity.Edit(Tools.MoneyToDouble(command.Amount),startDate);
|
|
_salaryAidRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult Remove(long id)
|
|
{
|
|
OperationResult op = new OperationResult();
|
|
var entity = _salaryAidRepository.Get(id);
|
|
if (entity == null)
|
|
return op.Failed("این آیتم وجود ندارد");
|
|
|
|
var month = Convert.ToInt32(entity.SalaryAidDateTime.ToFarsi().Substring(5, 2));
|
|
var year = Convert.ToInt32(entity.SalaryAidDateTime.ToFarsi().Substring(0, 4));
|
|
|
|
if (_customizeCheckoutRepository.Exists(x => x.WorkshopId == entity.WorkshopId && entity.EmployeeId == x.EmployeeId && x.YearInt == year && x.MonthInt == month))
|
|
return op.Failed("این پرسنل در این تاریخ دارای فیش حقوقی است");
|
|
|
|
|
|
_salaryAidRepository.Remove(entity);
|
|
_salaryAidRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult RemoveRange(IEnumerable<long> ids)
|
|
{
|
|
OperationResult op = new OperationResult();
|
|
var salaries = _salaryAidRepository.GetBy(ids);
|
|
_salaryAidRepository.RemoveRange(salaries);
|
|
_salaryAidRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public async Task<OperationResult> CreateRange(List<CreateSalaryAidViewModel> commands)
|
|
{
|
|
var op = new OperationResult();
|
|
foreach (var command in commands)
|
|
{
|
|
|
|
if (!command.SalaryDateTime.TryToGeorgianDateTime(out var startDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (startDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت مساعده می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
|
|
var month = Convert.ToInt32(command.SalaryDateTime.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.SalaryDateTime.Substring(0, 4));
|
|
|
|
|
|
if (_customizeCheckoutRepository.Exists(x => x.WorkshopId == command.WorkshopId && command.EmployeeIds.Contains(x.EmployeeId) && x.YearInt == year && x.MonthInt == month))
|
|
{
|
|
return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی صادر شده است مساعده ای دهید");
|
|
}
|
|
|
|
foreach (var employeeId in command.EmployeeIds)
|
|
{
|
|
var id = employeeId;
|
|
if (employeeId == 0)
|
|
{
|
|
var employee = _employeeRepository.GetByNationalCode(command.NationalCode);
|
|
id = employee.id;
|
|
}
|
|
|
|
var entity = new SalaryAid(id, command.WorkshopId, command.Amount.MoneyToDouble(), startDate);
|
|
await _salaryAidRepository.CreateAsync(entity);
|
|
}
|
|
}
|
|
await _salaryAidRepository.SaveChangesAsync();
|
|
return op.Succcedded();
|
|
}
|
|
} |