169 lines
5.7 KiB
C#
169 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using _0_Framework.Application;
|
|
using Company.Domain.CheckoutAgg;
|
|
using Company.Domain.CustomizeCheckoutAgg;
|
|
using Company.Domain.EmployeeAgg;
|
|
using Company.Domain.File1;
|
|
using Company.Domain.FineAgg;
|
|
using Company.Domain.WorkshopAgg;
|
|
using CompanyManagment.App.Contracts.Fine;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class FineApplication : IFineApplication
|
|
{
|
|
private readonly IFineRepository _fineRepository;
|
|
private readonly IWorkshopRepository _workshopRepository;
|
|
private readonly IEmployeeRepository _employeeRepository;
|
|
private readonly ICustomizeCheckoutRepository _customizeCheckoutRepository;
|
|
|
|
public FineApplication(IFineRepository fineRepository, IEmployeeRepository employeeRepository, IWorkshopRepository workshopRepository, ICustomizeCheckoutRepository customizeCheckoutRepository)
|
|
{
|
|
_fineRepository = fineRepository;
|
|
_employeeRepository = employeeRepository;
|
|
_workshopRepository = workshopRepository;
|
|
_customizeCheckoutRepository = customizeCheckoutRepository;
|
|
}
|
|
|
|
public List<FineViewModel> GetSearchList(FineSearchViewModel searchModel)
|
|
{
|
|
return _fineRepository.GetSearchList(searchModel);
|
|
}
|
|
|
|
public EditFineViewModel GetDetails(long id)
|
|
{
|
|
return _fineRepository.GetDetails(id);
|
|
}
|
|
|
|
public OperationResult Remove(long id)
|
|
{
|
|
OperationResult op = new OperationResult();
|
|
var entity = _fineRepository.Get(id);
|
|
if (entity == null)
|
|
return op.Failed("چنین آیتمی وجود ندارد");
|
|
|
|
var month = Convert.ToInt32(entity.FineDate.ToFarsi().Substring(5, 2));
|
|
var year = Convert.ToInt32(entity.FineDate.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("این پرسنل در این تاریخ دارای فیش حقوقی است");
|
|
}
|
|
|
|
|
|
_fineRepository.Remove(entity);
|
|
_fineRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult RemoveRange(List<long> ids)
|
|
{
|
|
OperationResult op = new OperationResult();
|
|
var fines = _fineRepository.GetBy(ids);
|
|
_fineRepository.RemoveRange(fines);
|
|
_fineRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult Create(CreateFineViewModel command)
|
|
{
|
|
OperationResult op = new();
|
|
|
|
#region Validation
|
|
|
|
if (!_workshopRepository.Exists(x => x.id == command.WorkshopId))
|
|
return op.Failed("خطای سیستمی");
|
|
if (!_employeeRepository.Exists(x => command.EmployeeIds.Any(a => a == x.id)))
|
|
return op.Failed("خطای سیستمی");
|
|
|
|
if (!command.FineDate.TryToGeorgianDateTime(out var fineDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
if (fineDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت جریمه می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
var month = Convert.ToInt32(command.FineDate.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.FineDate.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
|
|
DateTime date = command.FineDate.ToGeorgianDateTime();
|
|
|
|
foreach (var employeeId in command.EmployeeIds)
|
|
{
|
|
Fine entity = new Fine(employeeId, command.WorkshopId, command.Title, command.Amount.MoneyToDouble(), date);
|
|
_fineRepository.Create(entity);
|
|
|
|
}
|
|
|
|
_fineRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult Edit(EditFineViewModel command)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _fineRepository.Get(command.Id);
|
|
if (entity == null)
|
|
return op.Failed("چنین جریمه ای یافت نشد");
|
|
if (command.EmployeeId <= 0)
|
|
{
|
|
return op.Failed("خطای سیستمی!");
|
|
}
|
|
if (!command.FineDate.TryToGeorgianDateTime(out var fineDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
if (fineDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت جریمه می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
var month = Convert.ToInt32(command.FineDate.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.FineDate.Substring(0, 4));
|
|
|
|
|
|
|
|
if (_customizeCheckoutRepository.Exists(x => x.WorkshopId == command.WorkshopId && command.EmployeeId == x.EmployeeId && x.YearInt == year && x.MonthInt == month))
|
|
{
|
|
return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی صادر شده است جریمه ای دهید");
|
|
|
|
}
|
|
|
|
if (!_employeeRepository.Exists(x=>x.id == command.EmployeeId))
|
|
{
|
|
return op.Failed("شخص وارد شده معتبر نمیباشد");
|
|
}
|
|
DateTime date = command.FineDate.ToGeorgianDateTime();
|
|
|
|
|
|
entity.Edit(command.EmployeeId, command.WorkshopId, command.Title, command.Amount.MoneyToDouble(), date);
|
|
|
|
_fineRepository.SaveChanges();
|
|
return op.Succcedded(entity.id);
|
|
|
|
}
|
|
} |