300 lines
12 KiB
C#
300 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Transactions;
|
|
using _0_Framework.Application;
|
|
using Company.Domain.CustomizeCheckoutAgg;
|
|
using Company.Domain.CustomizeCheckoutTempAgg;
|
|
using Company.Domain.RewardAgg;
|
|
using CompanyManagment.App.Contracts.CustomizeCheckout;
|
|
using CompanyManagment.App.Contracts.Reward;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class RewardApplication : IRewardApplication
|
|
{
|
|
private readonly IRewardRepository _rewardRepository;
|
|
private readonly IAuthHelper _authHelper;
|
|
private readonly ICustomizeCheckoutRepository _customizeCheckoutRepository;
|
|
private readonly ICustomizeCheckoutTempRepository _customizeCheckoutTempRepository;
|
|
private readonly ICustomizeCheckoutApplication _customizeCheckoutApplication;
|
|
private readonly ICustomizeCheckoutTempApplication _customizeCheckoutTempApplication;
|
|
|
|
public RewardApplication(IRewardRepository rewardRepository, IAuthHelper authHelper, ICustomizeCheckoutRepository customizeCheckoutRepository, ICustomizeCheckoutTempRepository customizeCheckoutTempRepository, ICustomizeCheckoutTempApplication customizeCheckoutTempApplication, ICustomizeCheckoutApplication customizeCheckoutApplication)
|
|
{
|
|
_rewardRepository = rewardRepository;
|
|
_authHelper = authHelper;
|
|
_customizeCheckoutRepository = customizeCheckoutRepository;
|
|
_customizeCheckoutTempRepository = customizeCheckoutTempRepository;
|
|
_customizeCheckoutTempApplication = customizeCheckoutTempApplication;
|
|
_customizeCheckoutApplication = customizeCheckoutApplication;
|
|
}
|
|
|
|
public List<RewardViewModel> GetSearchList(RewardSearchModel searchModel)
|
|
{
|
|
return _rewardRepository.GetSearchList(searchModel);
|
|
}
|
|
|
|
|
|
public EditRewardViewModel GetDetails(long id)
|
|
{
|
|
return _rewardRepository.GetDetails(id);
|
|
}
|
|
|
|
public OperationResult Remove(long id)
|
|
{
|
|
OperationResult op = new OperationResult();
|
|
var entity = _rewardRepository.Get(id);
|
|
if (entity == null)
|
|
return op.Failed("این آیتم وجود ندارد");
|
|
|
|
var month = Convert.ToInt32(entity.GrantDate.ToFarsi().Substring(5, 2));
|
|
var year = Convert.ToInt32(entity.GrantDate.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("این پرسنل در این تاریخ دارای فیش حقوقی است");
|
|
}
|
|
|
|
if (_customizeCheckoutTempRepository.Exists(x => x.WorkshopId == entity.WorkshopId && entity.EmployeeId == x.EmployeeId &&
|
|
x.YearInt == year && x.MonthInt == month && x.ContractStart <= entity.GrantDate && x.ContractEnd >= entity.GrantDate))
|
|
{
|
|
return op.Failed("این پرسنل در این تاریخ دارای فیش حقوقی موقت است");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_rewardRepository.Remove(entity);
|
|
_rewardRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult RemoveRange(IEnumerable<long> ids)
|
|
{
|
|
OperationResult op = new OperationResult();
|
|
var rewards = _rewardRepository.GetBy(ids);
|
|
_rewardRepository.RemoveRange(rewards);
|
|
_rewardRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
|
|
}
|
|
|
|
public OperationResult Create(CreateRewardViewModel command)
|
|
{
|
|
var op = new OperationResult();
|
|
|
|
#region Validation
|
|
|
|
if (!command.GrantDate.TryToGeorgianDateTime(out var grantDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (grantDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت پاداش می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
|
|
var month = Convert.ToInt32(command.GrantDate.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.GrantDate.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("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی صادر شده است پاداشی دهید");
|
|
//}
|
|
|
|
//if (_customizeCheckoutTempRepository.Exists(x => x.WorkshopId == command.WorkshopId && command.EmployeeIds.Contains(x.EmployeeId) &&
|
|
// x.YearInt == year && x.MonthInt == month && x.ContractStart <= grantDate && x.ContractEnd >= grantDate))
|
|
//{
|
|
// return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی موقت صادر شده است پاداشی دهید");
|
|
//}
|
|
|
|
|
|
_ = DateTime.Now.Date.AddMonthsFa(-1, out var oneMonthAgoGr);
|
|
|
|
if (oneMonthAgoGr > grantDate)
|
|
{
|
|
var prevCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(grantDate,
|
|
oneMonthAgoGr, command.WorkshopId, command.EmployeeIds);
|
|
|
|
if (prevCheckouts.CustomizeCheckout || prevCheckouts.CustomizeCheckoutTemp)
|
|
{
|
|
return op.Failed("شما نمیتوانید در تاریخ قبل از یک ماه گذشته که فیش صادر شده باشد پاداشی دهید");
|
|
}
|
|
}
|
|
|
|
|
|
var existsCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(grantDate,
|
|
grantDate, command.WorkshopId, command.EmployeeIds);
|
|
|
|
//if (existsCheckouts.Checkout)
|
|
// return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی رسمی صادر شده است پاداشی دهید");
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
var (userId, userType, _) = _authHelper.GetUserTypeWithId();
|
|
|
|
using var transaction = new TransactionScope();
|
|
|
|
|
|
foreach (var employeeId in command.EmployeeIds)
|
|
{
|
|
var entity = new Reward(employeeId, command.WorkshopId, command.Amount.MoneyToDouble(), command.Description,
|
|
userId, userType, grantDate, command.Title);
|
|
|
|
_rewardRepository.Create(entity);
|
|
_rewardRepository.SaveChanges();
|
|
if (existsCheckouts.CustomizeCheckout)
|
|
{
|
|
var customizeCheckouts = _customizeCheckoutRepository.GetByWorkshopIdEmployeeIdMonthYear(
|
|
command.WorkshopId, employeeId,
|
|
year, month).GetAwaiter().GetResult();
|
|
if (customizeCheckouts != null)
|
|
{
|
|
|
|
var rewards = customizeCheckouts.CustomizeCheckoutRewards.ToList();
|
|
|
|
rewards.Add(new(entity.Amount.ToMoney(), entity.Description, entity.GrantDate, entity.GrantDate.ToFarsi(), entity.IsActive, entity.Title, entity.id));
|
|
customizeCheckouts.SetRewards(rewards);
|
|
}
|
|
}
|
|
|
|
if (existsCheckouts.CustomizeCheckoutTemp)
|
|
{
|
|
var customizeCheckoutTemp = _customizeCheckoutTempRepository.GetByWorkshopIdEmployeeIdInDate(
|
|
command.WorkshopId, employeeId, grantDate).GetAwaiter().GetResult();
|
|
if (customizeCheckoutTemp != null)
|
|
{
|
|
var rewards = customizeCheckoutTemp.CustomizeCheckoutRewards.ToList();
|
|
|
|
rewards.Add(new(entity.Amount.ToMoney(), entity.Description, entity.GrantDate, entity.GrantDate.ToFarsi(), entity.IsActive, entity.Title, entity.id));
|
|
customizeCheckoutTemp.SetRewards(rewards);
|
|
}
|
|
}
|
|
|
|
}
|
|
_customizeCheckoutRepository.SaveChanges();
|
|
|
|
transaction.Complete();
|
|
return op.Succcedded();
|
|
|
|
}
|
|
|
|
public OperationResult Edit(EditRewardViewModel command)
|
|
{
|
|
var op = new OperationResult();
|
|
|
|
if (!command.GrantDate.TryToGeorgianDateTime(out var grantDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (grantDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت پاداش می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
|
|
var entity = _rewardRepository.Get(command.Id);
|
|
if (entity == null)
|
|
return op.Failed("چنین آیتمی وجود ندارد");
|
|
|
|
var month = Convert.ToInt32(command.GrantDate.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.GrantDate.Substring(0, 4));
|
|
|
|
|
|
_ = DateTime.Now.Date.AddMonthsFa(-1, out var oneMonthAgoGr);
|
|
|
|
if (oneMonthAgoGr > grantDate)
|
|
{
|
|
var prevCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(grantDate,
|
|
oneMonthAgoGr, entity.WorkshopId, [entity.EmployeeId]);
|
|
|
|
if (prevCheckouts.CustomizeCheckout || prevCheckouts.CustomizeCheckoutTemp)
|
|
{
|
|
return op.Failed("شما نمیتوانید در تاریخ قبل از یک ماه گذشته که فیش صادر شده باشد پاداشی دهید");
|
|
}
|
|
}
|
|
var (userId, userType,_) = _authHelper.GetUserTypeWithId();
|
|
|
|
var existsCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(grantDate,
|
|
grantDate, entity.WorkshopId, [entity.EmployeeId]);
|
|
|
|
|
|
using var transaction = new TransactionScope();
|
|
|
|
entity.Edit(command.Amount.MoneyToDouble(), command.Description, userId, userType, grantDate, command.Title);
|
|
_rewardRepository.SaveChanges();
|
|
|
|
if (existsCheckouts.CustomizeCheckout)
|
|
{
|
|
var customizeCheckouts = _customizeCheckoutRepository.GetByWorkshopIdEmployeeIdMonthYear(
|
|
entity.WorkshopId, entity.EmployeeId,
|
|
year, month).GetAwaiter().GetResult();
|
|
if (customizeCheckouts != null)
|
|
{
|
|
|
|
var rewards = customizeCheckouts.CustomizeCheckoutRewards.ToList();
|
|
|
|
var existsReward = rewards.FirstOrDefault(x => x.EntityId == entity.id);
|
|
if (existsReward != null)
|
|
{
|
|
rewards.Remove(existsReward);
|
|
}
|
|
|
|
rewards.Add(new(entity.Amount.ToMoney(), entity.Description, entity.GrantDate, entity.GrantDate.ToFarsi(), entity.IsActive, entity.Title, entity.id));
|
|
customizeCheckouts.SetRewards(rewards);
|
|
}
|
|
}
|
|
|
|
if (existsCheckouts.CustomizeCheckoutTemp)
|
|
{
|
|
var customizeCheckoutTemp = _customizeCheckoutTempRepository.GetByWorkshopIdEmployeeIdInDate(
|
|
command.WorkshopId, entity.EmployeeId, grantDate).GetAwaiter().GetResult();
|
|
if (customizeCheckoutTemp != null)
|
|
{
|
|
|
|
var rewards = customizeCheckoutTemp.CustomizeCheckoutRewards.ToList();
|
|
|
|
var existsReward = rewards.FirstOrDefault(x => x.EntityId == entity.id);
|
|
if (existsReward != null)
|
|
{
|
|
rewards.Remove(existsReward);
|
|
}
|
|
|
|
rewards.Add(new(entity.Amount.ToMoney(), entity.Description, entity.GrantDate, entity.GrantDate.ToFarsi(), entity.IsActive, entity.Title, entity.id));
|
|
customizeCheckoutTemp.SetRewards(rewards);
|
|
}
|
|
}
|
|
_customizeCheckoutRepository.SaveChanges();
|
|
transaction.Complete();
|
|
return op.Succcedded(entity.id);
|
|
}
|
|
|
|
#region Pooya
|
|
|
|
|
|
/// <summary>
|
|
/// گروهبندی بر اساس ماه هنگام جستجو با انتخاب کارمند
|
|
/// </summary>
|
|
public RewardsGroupedViewModel GetSearchListAsGrouped(RewardSearchModel searchModel)
|
|
{
|
|
return _rewardRepository.GetSearchListAsGrouped(searchModel);
|
|
}
|
|
#endregion
|
|
|
|
} |