148 lines
5.2 KiB
C#
148 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using _0_Framework.Application;
|
|
using Company.Domain.CheckoutAgg;
|
|
using Company.Domain.CustomizeCheckoutAgg;
|
|
using Company.Domain.RewardAgg;
|
|
using CompanyManagment.App.Contracts.Reward;
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class RewardApplication : IRewardApplication
|
|
{
|
|
private readonly IRewardRepository _rewardRepository;
|
|
private readonly IAuthHelper _authHelper;
|
|
private readonly ICustomizeCheckoutRepository _customizeCheckoutRepository;
|
|
|
|
public RewardApplication(IRewardRepository rewardRepository, IAuthHelper authHelper, ICustomizeCheckoutRepository customizeCheckoutRepository)
|
|
{
|
|
_rewardRepository = rewardRepository;
|
|
_authHelper = authHelper;
|
|
_customizeCheckoutRepository = customizeCheckoutRepository;
|
|
}
|
|
|
|
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("این پرسنل در این تاریخ دارای فیش حقوقی است");
|
|
}
|
|
_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("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی صادر شده است پاداشی دهید");
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
foreach (var employeeId in command.EmployeeIds)
|
|
{
|
|
var entity = new Reward(employeeId, command.WorkshopId, command.Amount.MoneyToDouble(), command.Description,
|
|
command.RewardedByAccountId, grantDate);
|
|
|
|
_rewardRepository.Create(entity);
|
|
}
|
|
|
|
_rewardRepository.SaveChanges();
|
|
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));
|
|
|
|
if (_customizeCheckoutRepository.Exists(x => x.WorkshopId == command.WorkshopId && command.EmployeeId==x.EmployeeId && x.YearInt == year && x.MonthInt == month))
|
|
{
|
|
return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی صادر شده است پاداشی دهید");
|
|
}
|
|
|
|
entity.Edit(command.Amount.MoneyToDouble(),command.Description,command.RewardedByAccountId, grantDate);
|
|
_rewardRepository.SaveChanges();
|
|
return op.Succcedded(entity.id);
|
|
}
|
|
|
|
} |