806 lines
38 KiB
C#
806 lines
38 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using _0_Framework.Application;
|
|
using _0_Framework.InfraStructure;
|
|
|
|
using Company.Domain.InsuranceListAgg;
|
|
using Company.Domain.LeftWorkInsuranceAgg;
|
|
using Company.Domain.PersonnelCodeAgg;
|
|
using CompanyManagment.App.Contracts.InsuranceList;
|
|
using CompanyManagment.App.Contracts.LeftWork;
|
|
using CompanyManagment.App.Contracts.LeftWorkInsurance;
|
|
using CompanyManagment.App.Contracts.PersonnleCode;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class LeftWorkInsuranceRepository : RepositoryBase<long, LeftWorkInsurance>, ILeftWorkInsuranceRepository
|
|
{
|
|
private readonly CompanyContext _context;
|
|
public LeftWorkInsuranceRepository(CompanyContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public EditLeftWorkInsurance GetDetails(long id)
|
|
{
|
|
return _context.LeftWorkInsuranceList.Select(x => new EditLeftWorkInsurance()
|
|
{
|
|
Id = x.id,
|
|
LeftWorkDate = x.LeftWorkDate != null ? x.LeftWorkDate.ToFarsi() : "",
|
|
StartWorkDate = x.StartWorkDate.ToFarsi(),
|
|
EmployeeFullName = x.EmployeeFullName,
|
|
WorkshopName = x.WorkshopName,
|
|
WorkshopId = x.WorkshopId,
|
|
EmployeeId = x.EmployeeId
|
|
})
|
|
.FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
public string StartWork(long employeeId, long workshopId, string leftWork)
|
|
{
|
|
var checkExist = _context.LeftWorkInsuranceList.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId);
|
|
if (checkExist)
|
|
{
|
|
var LeftWorks = _context.LeftWorkInsuranceList
|
|
.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId)
|
|
.OrderByDescending(x => x.LeftWorkDate).ToList();
|
|
var lastLeft = LeftWorks.Select(x => x.LeftWorkDate).FirstOrDefault();
|
|
|
|
var leftWorkNew = leftWork.ToGeorgianDateTime();
|
|
|
|
var startWorkList = _context.Contracts
|
|
.Where(x => x.EmployeeId == employeeId && x.WorkshopIds == workshopId)
|
|
.Where(x => x.ContarctStart < leftWorkNew && x.ContarctStart > lastLeft)
|
|
.OrderBy(x => x.ContarctStart).ToList();
|
|
|
|
var startWorkDate = startWorkList.Select(x => x.ContarctStart).FirstOrDefault();
|
|
var result = startWorkDate.ToFarsi();
|
|
return result;
|
|
|
|
}
|
|
else
|
|
{
|
|
var leftWorkNew = leftWork.ToGeorgianDateTime();
|
|
|
|
var startWorkList = _context.Contracts
|
|
.Where(x => x.EmployeeId == employeeId && x.WorkshopIds == workshopId)
|
|
.Where(x => x.ContarctStart < leftWorkNew)
|
|
.OrderBy(x => x.ContarctStart).ToList();
|
|
|
|
var startWorkDate = startWorkList.Select(x => x.ContarctStart).FirstOrDefault();
|
|
var result = startWorkDate.ToFarsi();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public List<LeftWorkInsuranceViewModel> searchByWorkshopId(long workshopId)
|
|
{
|
|
var query = _context.LeftWorkInsuranceList.Select(x => new LeftWorkInsuranceViewModel()
|
|
{
|
|
Id = x.id,
|
|
WorkshopId = x.WorkshopId,
|
|
LeftWorkDate = x.LeftWorkDate != null ? x.LeftWorkDate.ToFarsi() : "",
|
|
StartWorkDate = x.StartWorkDate.ToFarsi(),
|
|
LeftWorkDateGr = x.LeftWorkDate,
|
|
StartWorkDateGr = x.StartWorkDate,
|
|
EmployeeFullName = x.EmployeeFullName,
|
|
WorkshopName = x.WorkshopName,
|
|
EmployeeId = x.EmployeeId,
|
|
JobId = x.JobId,
|
|
|
|
}).Where(x => x.WorkshopId == workshopId);
|
|
return query.ToList();
|
|
}
|
|
|
|
|
|
public List<LeftWorkInsuranceViewModel> search(LeftWorkInsuranceSearchModel searchModel)
|
|
{
|
|
|
|
var query = new List<LeftWorkInsurance>();
|
|
if (searchModel.EmployeeId != 0)
|
|
query = _context.LeftWorkInsuranceList.Where(x => x.EmployeeId == searchModel.EmployeeId).ToList();
|
|
else if (searchModel.WorkshopId != 0 && searchModel.EmployeeId != 0)
|
|
query = _context.LeftWorkInsuranceList.Where(x => x.WorkshopId == searchModel.WorkshopId && x.EmployeeId == searchModel.EmployeeId).ToList();
|
|
|
|
var list = new List<LeftWorkInsuranceViewModel>();
|
|
|
|
foreach (var item in query)
|
|
{
|
|
var jobObj = _context.Jobs.Where(x => x.id == item.JobId).FirstOrDefault();
|
|
var insuranceListIds = _context.EmployeeInsurancListDataSet.Where(x => x.EmployeeId == searchModel.EmployeeId && (x.StartWorkDate == item.StartWorkDate || x.LeftWorkDate == item.LeftWorkDate))
|
|
.Select(x => x.InsuranceListId).ToList();
|
|
var hasInsuranceList = _context.InsuranceListSet.Any(x => insuranceListIds.Contains(x.id) && x.WorkshopId == item.WorkshopId);
|
|
|
|
list.Add(new LeftWorkInsuranceViewModel()
|
|
{
|
|
Id = item.id,
|
|
LeftWorkDate = item.LeftWorkDate != null ? item.LeftWorkDate.ToFarsi() : "",
|
|
StartWorkDate = item.StartWorkDate.ToFarsi(),
|
|
LeftWorkDateGr = item.LeftWorkDate,
|
|
StartWorkDateGr = item.StartWorkDate,
|
|
EmployeeFullName = item.EmployeeFullName,
|
|
WorkshopName = item.WorkshopName,
|
|
WorkshopId = item.WorkshopId,
|
|
EmployeeId = item.EmployeeId,
|
|
JobId = item.JobId,
|
|
IncludeStatus = item.IncludeStatus,
|
|
JobName = jobObj != null ? jobObj.JobName : string.Empty,
|
|
JobCode = jobObj != null ? jobObj.JobCode : string.Empty,
|
|
HasInsuranceList = hasInsuranceList,
|
|
});
|
|
}
|
|
|
|
//if (searchModel.WorkshopId != 0 && searchModel.EmployeeId !=0)
|
|
// list = list.Where(x => x.WorkshopId == searchModel.WorkshopId && x.EmployeeId == searchModel.EmployeeId).ToList();
|
|
//if (searchModel.EmployeeId != 0)
|
|
// list = list.Where(x => x.EmployeeId == searchModel.EmployeeId).ToList();
|
|
return list.OrderByDescending(x => x.StartWorkDateGr).ToList();
|
|
}
|
|
|
|
public OperationResult RemoveLeftWorkInsurance(long id)
|
|
{
|
|
var op = new OperationResult();
|
|
|
|
var item = _context.LeftWorkInsuranceList.FirstOrDefault(x => x.id == id);
|
|
if (item != null)
|
|
_context.LeftWorkInsuranceList.Remove(item);
|
|
|
|
_context.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
public OperationResult RemoveAllLeftWorkInsurance(long workshopId, long employeeId)
|
|
{
|
|
var op = new OperationResult();
|
|
|
|
using (var transaction = _context.Database.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
bool hasInsurance = false;
|
|
var insuranceList = _context.InsuranceListSet.Where(x => x.WorkshopId == workshopId).ToList();
|
|
foreach (var item in insuranceList)
|
|
{
|
|
hasInsurance = _context.EmployeeInsurancListDataSet.Any(x => x.EmployeeId == employeeId && x.InsuranceListId == item.id);
|
|
if (hasInsurance)
|
|
break;
|
|
}
|
|
//tempChange
|
|
//if (hasInsurance)
|
|
//{
|
|
// return op.Failed("برای این پرسنل در این کارگاه لیست بیمه ارسال شده است.");
|
|
//}
|
|
var list = _context.LeftWorkInsuranceList.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId).ToList();
|
|
if (list != null && list.Count > 0)
|
|
_context.LeftWorkInsuranceList.RemoveRange(list);
|
|
|
|
bool hasLeftWork = _context.LeftWorkList.Any(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId);
|
|
if (!hasLeftWork)
|
|
{
|
|
var personelCodeList = _context.PersonnelCodeSet.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId).ToList();
|
|
_context.PersonnelCodeSet.RemoveRange(personelCodeList);
|
|
}
|
|
|
|
_context.SaveChanges();
|
|
transaction.Commit();
|
|
string Message = "";
|
|
|
|
if (hasLeftWork)
|
|
{
|
|
op.Message = "حذف با موفقیت انجام شد." + "<br/>" + "<span class='text-warning'>" + "کد پرسنلی این شخص در این کارگاه به دلیل استفاده در ترک کار قرارداد قابل حذف نمی باشد. " + "</span>";
|
|
op.IsSuccedded = true;
|
|
}
|
|
else
|
|
{
|
|
//op.Message = "حذف با موفقیت انجام شد";
|
|
op.IsSuccedded = true;
|
|
|
|
}
|
|
|
|
return op;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
return op.Failed("حذف اطلاعات با خطا مواجه شد");
|
|
}
|
|
}
|
|
}
|
|
|
|
public OperationResult RemoveAllLeftWorkInsuranceWithoutWorkshop(long employeeId)
|
|
{
|
|
var list = _context.LeftWorkInsuranceList.Where(x => x.EmployeeId == employeeId).ToList();
|
|
//if (list != null && list.Count > 0)
|
|
//{
|
|
|
|
//}
|
|
var op = new OperationResult();
|
|
return op;
|
|
}
|
|
|
|
public List<LeftWorkInsuranceViewModel> GetLeftPersonelByWorkshopId(List<long> workshopIds)
|
|
{
|
|
return _context.LeftWorkInsuranceList.Select(x => new LeftWorkInsuranceViewModel()
|
|
{
|
|
Id = x.id,
|
|
WorkshopId = x.WorkshopId,
|
|
EmployeeId = x.EmployeeId,
|
|
|
|
}).Where(x => workshopIds.Contains(x.WorkshopId)).ToList();
|
|
}
|
|
|
|
public List<LeftWorkInsuranceViewModel> SearchForCreateInsuranceList(EmployeeForCreateInsuranceListSearchModel searchModel)
|
|
{
|
|
List<LeftWorkInsuranceViewModel> listLeftWorkInsurance = new List<LeftWorkInsuranceViewModel>();
|
|
// IQueryable<LeftWorkInsuranceViewModel> query;
|
|
|
|
//if (searchModel.WorkshopIds != null && searchModel.WorkshopIds.Count > 0)
|
|
//{
|
|
// query = _context.LeftWorkInsuranceList.Where(x => searchModel.WorkshopIds.Contains(x.WorkshopId)).Select(x => new LeftWorkInsuranceViewModel()
|
|
// {
|
|
// Id = x.id,
|
|
// LeftWorkDate = x.LeftWorkDate != null ? x.LeftWorkDate.ToFarsi() : "",
|
|
// StartWorkDate = x.StartWorkDate.ToFarsi(),
|
|
// LeftWorkDateGr = x.LeftWorkDate,
|
|
// StartWorkDateGr = x.StartWorkDate,
|
|
// EmployeeFullName = x.EmployeeFullName,
|
|
// WorkshopName = x.WorkshopName,
|
|
// WorkshopId = x.WorkshopId,
|
|
// EmployeeId = x.EmployeeId,
|
|
// JobId=x.JobId,
|
|
// JobName = _context.Jobs.Where(i=>i.id== x.JobId).FirstOrDefault().JobName
|
|
// });
|
|
//}
|
|
//else
|
|
//{
|
|
// query = _context.LeftWorkInsuranceList.Select(x => new LeftWorkInsuranceViewModel()
|
|
// {
|
|
// Id = x.id,
|
|
// LeftWorkDate = x.LeftWorkDate != null ? x.LeftWorkDate.ToFarsi() : "",
|
|
// StartWorkDate = x.StartWorkDate.ToFarsi(),
|
|
// LeftWorkDateGr = x.LeftWorkDate,
|
|
// StartWorkDateGr = x.StartWorkDate,
|
|
// EmployeeFullName = x.EmployeeFullName,
|
|
// WorkshopName = x.WorkshopName,
|
|
// WorkshopId = x.WorkshopId,
|
|
// EmployeeId = x.EmployeeId,
|
|
// JobName = _context.Jobs.Where(i => i.id == x.JobId).FirstOrDefault().JobName
|
|
// });
|
|
//}
|
|
|
|
if (searchModel.WorkshopIds != null && searchModel.WorkshopIds.Count > 0)
|
|
{
|
|
var list = _context.LeftWorkInsuranceList.Where(x => searchModel.WorkshopIds.Contains(x.WorkshopId)).ToList();
|
|
foreach (var item in list)
|
|
{
|
|
var leftWorkInsurance = new LeftWorkInsuranceViewModel();
|
|
var job = _context.Jobs.Where(i => i.id == item.JobId).FirstOrDefault();
|
|
leftWorkInsurance.Id = item.id;
|
|
leftWorkInsurance.LeftWorkDate = item.LeftWorkDate != null ? item.LeftWorkDate.ToFarsi() : "";
|
|
leftWorkInsurance.StartWorkDate = item.StartWorkDate.ToFarsi();
|
|
leftWorkInsurance.StartWorkDateGr = item.StartWorkDate;
|
|
leftWorkInsurance.LeftWorkDateGr = item.LeftWorkDate;
|
|
leftWorkInsurance.EmployeeFullName = item.EmployeeFullName;
|
|
leftWorkInsurance.WorkshopName = item.WorkshopName;
|
|
leftWorkInsurance.WorkshopId = item.WorkshopId;
|
|
leftWorkInsurance.EmployeeId = item.EmployeeId;
|
|
leftWorkInsurance.IncludeStatus = item.IncludeStatus;
|
|
leftWorkInsurance.JobId = item.JobId;
|
|
leftWorkInsurance.JobName = job != null ? job.JobName : string.Empty;
|
|
leftWorkInsurance.JobCode = job != null ? job.JobCode : string.Empty;
|
|
listLeftWorkInsurance.Add(leftWorkInsurance);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var list = _context.LeftWorkInsuranceList.ToList();
|
|
foreach (var item in list)
|
|
{
|
|
var leftWorkInsurance = new LeftWorkInsuranceViewModel();
|
|
var job = _context.Jobs.Where(i => i.id == item.JobId).FirstOrDefault();
|
|
leftWorkInsurance.Id = item.id;
|
|
leftWorkInsurance.LeftWorkDate = item.LeftWorkDate != null ? item.LeftWorkDate.ToFarsi() : "";
|
|
leftWorkInsurance.StartWorkDate = item.StartWorkDate.ToFarsi();
|
|
leftWorkInsurance.StartWorkDateGr = item.StartWorkDate;
|
|
leftWorkInsurance.LeftWorkDateGr = item.LeftWorkDate;
|
|
leftWorkInsurance.EmployeeFullName = item.EmployeeFullName;
|
|
leftWorkInsurance.WorkshopName = item.WorkshopName;
|
|
leftWorkInsurance.WorkshopId = item.WorkshopId;
|
|
leftWorkInsurance.EmployeeId = item.EmployeeId;
|
|
leftWorkInsurance.JobId = item.JobId;
|
|
leftWorkInsurance.JobName = job != null ? job.JobName : string.Empty; ;
|
|
leftWorkInsurance.JobCode = job != null ? job.JobCode : string.Empty;
|
|
listLeftWorkInsurance.Add(leftWorkInsurance);
|
|
}
|
|
}
|
|
return listLeftWorkInsurance;
|
|
}
|
|
|
|
public LeftWorkInsuranceViewModel GetLeftPersonelByWorkshopIdAndEmployeeId(long workshopId, long employeeId)
|
|
{
|
|
var leftWorkInsurance = new LeftWorkInsuranceViewModel();
|
|
var leftWorkInsuranceObj = _context.LeftWorkInsuranceList.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId).OrderByDescending(x => x.id).FirstOrDefault();
|
|
|
|
if (leftWorkInsuranceObj != null)
|
|
{
|
|
leftWorkInsurance.Id = leftWorkInsuranceObj.id;
|
|
leftWorkInsurance.LeftWorkDateGr = leftWorkInsuranceObj.LeftWorkDate;
|
|
leftWorkInsurance.StartWorkDateGr = leftWorkInsuranceObj.StartWorkDate;
|
|
|
|
if (leftWorkInsuranceObj.LeftWorkDate != null)
|
|
leftWorkInsurance.LeftWorkDate = leftWorkInsuranceObj.LeftWorkDate.ToFarsi();
|
|
|
|
leftWorkInsurance.StartWorkDate = leftWorkInsuranceObj.StartWorkDate.ToFarsi();
|
|
leftWorkInsurance.WorkshopId = leftWorkInsuranceObj.WorkshopId;
|
|
leftWorkInsurance.EmployeeId = leftWorkInsuranceObj.EmployeeId;
|
|
leftWorkInsurance.EmployeeFullName = leftWorkInsuranceObj.EmployeeFullName;
|
|
leftWorkInsurance.WorkshopName = leftWorkInsuranceObj.WorkshopName;
|
|
//leftWorkInsurance.EndWorkDate = leftWorkInsuranceObj.EndWorkDate;
|
|
// leftWorkInsurance.IsActiveWorkshop = leftWorkInsuranceObj.IsActiveWorkshop;
|
|
leftWorkInsurance.JobId = leftWorkInsuranceObj.JobId;
|
|
//leftWorkInsurance.JobName = leftWorkInsuranceObj.JobName;
|
|
//leftWorkInsurance.JobCode = leftWorkInsuranceObj.JobCode;
|
|
leftWorkInsurance.IncludeStatus = leftWorkInsuranceObj.IncludeStatus;
|
|
}
|
|
|
|
return leftWorkInsurance;
|
|
}
|
|
|
|
|
|
public OperationResult CreateLeftWorkInsurance(InformationLeftworkInsurance command)
|
|
{
|
|
var op = new OperationResult();
|
|
using (var transaction = _context.Database.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
#region PersonelCode
|
|
|
|
if (command.PersonnelCodeList != null && command.PersonnelCodeList.Count > 0)
|
|
{
|
|
var personelcode = command.PersonnelCodeList.Where(x => x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId).FirstOrDefault();
|
|
if (personelcode != null)
|
|
{
|
|
if (_context.PersonnelCodeSet.Any(x => x.WorkshopId == command.WorkshopId && x.PersonnelCode == personelcode.PersonnelCode))
|
|
{
|
|
op.Failed(" کد پرسنلی در کارگاه " + command.WorkshopName + " تکراری می باشد. ");
|
|
}
|
|
|
|
if (personelcode.PersonnelCode <= 0)
|
|
{
|
|
return op.Failed(" کد پرسنلی در کارگاه " + command.WorkshopName + "نامعتبر است.");
|
|
}
|
|
_context.PersonnelCodeSet.Add(new PersonnelCodeDomain(command.WorkshopId, command.EmployeeId, personelcode.PersonnelCode));
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
if (command.ItemLeftworkInsuranceList != null && command.ItemLeftworkInsuranceList.Count > 0)
|
|
{
|
|
bool checkRegister = false;
|
|
foreach (var item2 in command.ItemLeftworkInsuranceList)
|
|
{
|
|
var leftWorkInsurance = _context.LeftWorkInsuranceList.Where(x => x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId).ToList();
|
|
_context.LeftWorkInsuranceList.RemoveRange(leftWorkInsurance);
|
|
|
|
DateTime? left = null;
|
|
if (!string.IsNullOrWhiteSpace(item2.LeftWorkDate))
|
|
left = item2.LeftWorkDate.ToGeorgianDateTime();
|
|
|
|
var start = item2.StartWorkDate.ToGeorgianDateTime();
|
|
|
|
var leftwork = new LeftWorkInsurance(left, start, item2.WorkshopId, item2.EmployeeId,
|
|
command.EmployeeFullName, item2.WorkshopName, item2.JobId, item2.IncludeStatus);
|
|
Create(leftwork);
|
|
}
|
|
}
|
|
|
|
_context.SaveChanges();
|
|
transaction.Commit();
|
|
op.Succcedded(-1, " ثبت اطلاعات کارگاه " + command.WorkshopName + " با موفقیت انجام شد. ");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
op.Failed(" ثبت اطلاعات کارگاه " + command.WorkshopName + " با خطا مواجه شد. ");
|
|
|
|
|
|
}
|
|
}
|
|
|
|
return op;
|
|
}
|
|
|
|
public OperationResult CreateLeftworkInsuranceByLeftworkGroups(string employeeFullName, long commandEmployeeId, List<PersonnelCodeViewModel> commandPersonnelCode, List<LeftWorkInsuranceGroup> leftworkInsuranceGroups)
|
|
{
|
|
var op = new OperationResult();
|
|
using (var transaction = _context.Database.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
foreach (var item in leftworkInsuranceGroups)
|
|
{
|
|
#region PersonelCode
|
|
|
|
if (commandPersonnelCode != null && commandPersonnelCode.Count > 0)
|
|
{
|
|
var personelcode = commandPersonnelCode.Where(x => x.EmployeeId == commandEmployeeId && x.WorkshopId == item.WorkshopId).FirstOrDefault();
|
|
if (personelcode != null && personelcode.HasPersonelCode == false)
|
|
{
|
|
if (_context.PersonnelCodeSet.Any(x => x.WorkshopId == item.WorkshopId && x.PersonnelCode == personelcode.PersonnelCode))
|
|
{
|
|
return op.Failed(" کد پرسنلی در کارگاه " + item.WorkshopName + " تکراری می باشد. ");
|
|
}
|
|
|
|
if (personelcode.PersonnelCode <= 0)
|
|
{
|
|
return op.Failed(" کد پرسنلی در کارگاه " + item.WorkshopName + "نامعتبر است.");
|
|
}
|
|
|
|
_context.PersonnelCodeSet.Add(new PersonnelCodeDomain(item.WorkshopId, commandEmployeeId, personelcode.PersonnelCode));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
if (item.LeftworkInsuranceViewModels != null && item.LeftworkInsuranceViewModels.Count > 0)
|
|
{
|
|
bool checkRegister = false;
|
|
|
|
var leftworkInsuranceList = _context.LeftWorkInsuranceList.Where(x => x.EmployeeId == commandEmployeeId && x.WorkshopId == item.WorkshopId).ToList();
|
|
|
|
var removeList = new List<LeftWorkInsurance>();
|
|
|
|
foreach (var removeItem in leftworkInsuranceList)
|
|
{
|
|
if (!item.LeftworkInsuranceViewModels.Any(x => x.Id == removeItem.id))
|
|
removeList.Add(removeItem);
|
|
}
|
|
foreach (var item2 in item.LeftworkInsuranceViewModels)
|
|
{
|
|
DateTime? left = null;
|
|
if (!string.IsNullOrWhiteSpace(item2.LeftWorkDate))
|
|
left = item2.LeftWorkDate.ToGeorgianDateTime();
|
|
var start = item2.StartWorkDate.ToGeorgianDateTime();
|
|
|
|
if (item2.Id == 0)
|
|
{
|
|
//var now = DateTime.Now;
|
|
var check = _context.LeftWorkInsuranceList.Any(x =>
|
|
x.EmployeeId == item2.EmployeeId && x.WorkshopId == item2.WorkshopId &&
|
|
x.StartWorkDate == start);
|
|
|
|
if (!check)
|
|
{
|
|
|
|
var leftworkInsuranceObj = new LeftWorkInsurance(left, start, item2.WorkshopId, item2.EmployeeId,
|
|
employeeFullName, item2.WorkshopName, item2.JobId, item2.IncludeStatus);
|
|
Create(leftworkInsuranceObj);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
var leftworkInsurance = _context.LeftWorkInsuranceList.Where(x => x.id == item2.Id)?.FirstOrDefault();
|
|
leftworkInsurance.Edit(left, start, item2.WorkshopId, item2.EmployeeId, item2.JobId, item2.IncludeStatus);
|
|
}
|
|
}
|
|
|
|
if (removeList != null && removeList.Count > 0)
|
|
{
|
|
_context.LeftWorkInsuranceList.RemoveRange(removeList);
|
|
}
|
|
}
|
|
}
|
|
_context.SaveChanges();
|
|
transaction.Commit();
|
|
op.Succcedded(-1, " ثبت با موفقیت انجام شد. ");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
transaction.Rollback();
|
|
op.Failed(" ثبت اطلاعات با خطا مواجه شد. ");
|
|
}
|
|
}
|
|
|
|
return op;
|
|
}
|
|
|
|
public OperationResult CheckDeleteLeftWorkInsurance(long workshopId, long employeeId, DateTime date, int type)
|
|
{
|
|
|
|
var op = new OperationResult();
|
|
bool hasInsurance = false;
|
|
var insuranceList = _context.InsuranceListSet.Where(x => x.WorkshopId == workshopId).ToList();
|
|
foreach (var item in insuranceList)
|
|
{
|
|
if (type == 1)
|
|
hasInsurance = _context.EmployeeInsurancListDataSet.Any(x => x.EmployeeId == employeeId && x.InsuranceListId == item.id && x.StartWorkDate.Date >= date.Date && item.ConfirmSentlist);
|
|
else
|
|
hasInsurance = _context.EmployeeInsurancListDataSet.Any(x => x.EmployeeId == employeeId && x.InsuranceListId == item.id && (x.LeftWorkDate != null && ((DateTime)x.LeftWorkDate).Date == date.Date));
|
|
|
|
if (hasInsurance)
|
|
break;
|
|
}
|
|
|
|
//در هر صورت میتواند شروع به کار را تغییر دهد
|
|
//if (type == 1 && hasInsurance)
|
|
//{
|
|
// return op.Failed("برای این پرسنل در این تاریخ لیست بیمه ارسال شده است.برای حذف تاریخ شروع به کار، ابتدا لیست بیمه مربوطه را حذف کنید");
|
|
//}
|
|
//else
|
|
//tempChange
|
|
//if (type == 2 && hasInsurance)
|
|
// return op.Failed("برای این پرسنل در این تاریخ لیست بیمه تنظیم شده است.برای حذف تاریخ ترک کار، ابتدا لیست بیمه مربوطه را حذف کنید");
|
|
|
|
|
|
return op.Succcedded();
|
|
}
|
|
public OperationResult CheckEditLeftWorkInsurance(long workshopId, long employeeId, DateTime date, int type)
|
|
{
|
|
var op = new OperationResult();
|
|
bool hasInsurance = false;
|
|
|
|
List<InsuranceList> insuranceList = new List<InsuranceList>();
|
|
|
|
//if (type == 1)
|
|
//{
|
|
// insuranceList = _context.InsuranceListSet.Where(x => x.WorkshopId == workshopId).ToList();
|
|
//}
|
|
//else if (type == 2)
|
|
//{
|
|
string pdate = date.ToFarsi();
|
|
string year = pdate.Split('/')[0];
|
|
string month = pdate.Split('/')[1];
|
|
|
|
insuranceList = _context.InsuranceListSet.Where(x => x.Year == year && x.Month == month && x.WorkshopId == workshopId).ToList();
|
|
if (insuranceList != null && insuranceList.Count > 0)
|
|
{
|
|
foreach (var item in insuranceList)
|
|
{
|
|
if (type == 1)
|
|
{
|
|
//TempChange
|
|
// بصورت موقت کامنت شد
|
|
|
|
//var insurance = _context.EmployeeInsurancListDataSet.Where(x =>
|
|
// x.EmployeeId == employeeId && x.InsuranceListId == item.id &&
|
|
// x.StartWorkDate.Date < date.Date).FirstOrDefault();
|
|
//if (insurance!=null)
|
|
//{
|
|
// var insuranceListObj= insuranceList.FirstOrDefault(x => x.id == item.id);
|
|
// string resulDate= insuranceListObj.Month.ToFarsiMonthByNumber() + " " + insuranceListObj.Year;
|
|
// return op.Failed(" به دلیل استفاده این تاریخ در ایجاد لیست بیمه، ویرایش تاریخ قبل از تاریخ " + resulDate + " امکان پذیر می باشد. ");
|
|
//}
|
|
}
|
|
|
|
else
|
|
{
|
|
var insurance = _context.EmployeeInsurancListDataSet.Where(x =>
|
|
x.EmployeeId == employeeId && x.InsuranceListId == item.id &&
|
|
(x.LeftWorkDate == null || (x.LeftWorkDate != null && ((DateTime)x.LeftWorkDate).Date >= date.Date))).FirstOrDefault();
|
|
if (insurance != null)
|
|
{
|
|
var insuranceListObj = insuranceList.FirstOrDefault(x => x.id == item.id);
|
|
string resulDate = insuranceListObj.Month.ToFarsiMonthByNumber() + " " + insuranceListObj.Year;
|
|
return op.Failed(resulDate);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
//در هر صورت میتواند شروع به کار را تغییر دهد
|
|
//if (type == 1 && hasInsurance)
|
|
//{
|
|
// return op.Failed("تاریخ وارد شده باید از تاریخ شروع به کار وارد شده در لیست بیمه کوچکتر باشد.");
|
|
//}
|
|
//else
|
|
//if (type == 2 && hasInsurance)
|
|
// return op.Failed("برای این پرسنل در این تاریخ لیست بیمه تنظیم شده است.برای ویرایش تاریخ ترک کار، ابتدا لیست بیمه مربوطه را حذف کنید");
|
|
|
|
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult CheckBeforeSaveLeftWorkInsurance(long workshopId, long employeeId, DateTime date, int type)
|
|
{
|
|
var op = new OperationResult();
|
|
bool hasInsurance = false;
|
|
|
|
List<InsuranceList> insuranceList = new List<InsuranceList>();
|
|
string pdate = date.ToFarsi();
|
|
string year = pdate.Split('/')[0];
|
|
string month = pdate.Split('/')[1];
|
|
|
|
insuranceList = _context.InsuranceListSet.Where(x => x.Year == year && x.Month == month && x.WorkshopId == workshopId).ToList();
|
|
if (insuranceList != null && insuranceList.Count > 0)
|
|
{
|
|
// return op.Failed(" به دلیل استفاده این تاریخ در ایجاد لیست بیمه، ویرایش تاریخ قبل از تاریخ " + resulDate + " امکان پذیر می باشد. ");
|
|
|
|
//foreach (var item in insuranceList)
|
|
//{
|
|
// if (type == 1)
|
|
// {
|
|
// var insurance = _context.EmployeeInsurancListDataSet.Where(x =>
|
|
// x.EmployeeId == employeeId && x.InsuranceListId == item.id &&
|
|
// x.StartWorkDate.Date < date.Date).FirstOrDefault();
|
|
// if (insurance != null)
|
|
// {
|
|
// var insuranceListObj = insuranceList.FirstOrDefault(x => x.id == item.id);
|
|
// string resulDate = insuranceListObj.Month.ToFarsiMonthByNumber() + " " + insuranceListObj.Year;
|
|
// return op.Failed(" به دلیل استفاده این تاریخ در ایجاد لیست بیمه، ویرایش تاریخ قبل از تاریخ " + resulDate + " امکان پذیر می باشد. ");
|
|
// }
|
|
// }
|
|
|
|
// else
|
|
// {
|
|
// var insurance = _context.EmployeeInsurancListDataSet.Where(x =>
|
|
// x.EmployeeId == employeeId && x.InsuranceListId == item.id &&
|
|
// (x.LeftWorkDate == null || (x.LeftWorkDate != null && ((DateTime)x.LeftWorkDate).Date >= date.Date))).FirstOrDefault();
|
|
// if (insurance != null)
|
|
// {
|
|
// var insuranceListObj = insuranceList.FirstOrDefault(x => x.id == item.id);
|
|
// string resulDate = insuranceListObj.Month.ToFarsiMonthByNumber() + " " + insuranceListObj.Year;
|
|
// return op.Failed(resulDate);
|
|
// }
|
|
// }
|
|
|
|
//}
|
|
if (type == 1)
|
|
{
|
|
string resulDate = month.ToFarsiMonthByNumber() + " " + year;
|
|
//tempChange
|
|
//return op.Failed("در صورت تمایل به ثبت تاریخ شروع به کار این پرسنل می بایست لیست تنظیمی "+ resulDate + " بیمه را حذف نمایید.");
|
|
}
|
|
else
|
|
{
|
|
string resulDate = month.ToFarsiMonthByNumber() + " " + year;
|
|
//tempChange
|
|
// return op.Failed("در صورت تمایل به ثبت تاریخ ترک کار به کار این پرسنل می بایست لیست تنظیمی بیمه "+ resulDate + " را حذف نمایید.");
|
|
}
|
|
}
|
|
|
|
return op.Succcedded();
|
|
|
|
}
|
|
|
|
public int TotalWorkingYears(long employeeId, long workshopId, DateTime startDate)
|
|
{
|
|
var lefts = _context.LeftWorkInsuranceList
|
|
.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId && x.StartWorkDate < startDate)
|
|
.OrderBy(x => x.StartWorkDate)
|
|
.ToList();
|
|
if (lefts.Count > 0)
|
|
{
|
|
var totalDays = new List<int>();
|
|
foreach (var item in lefts)
|
|
{
|
|
if (item.LeftWorkDate != null)
|
|
{
|
|
var span = ((DateTime)item.LeftWorkDate - item.StartWorkDate).TotalDays;
|
|
totalDays.Add((int)(span));
|
|
}
|
|
else
|
|
{
|
|
var span = (startDate - item.StartWorkDate).TotalDays;
|
|
totalDays.Add((int)(span));
|
|
}
|
|
}
|
|
|
|
var sum = totalDays.Sum(x => x);
|
|
return (sum / 365);
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
|
|
#region Insurance
|
|
|
|
public List<EmployeeDetailsForInsuranceListViewModel> GetEmployeeInsuranceLeftWorksAndInformation(long workshopId, DateTime startDate, DateTime endDate)
|
|
{
|
|
if (workshopId == 0)
|
|
return [];
|
|
var query = _context.LeftWorkInsuranceList.Include(x => x.Employee)
|
|
.Where(x => x.WorkshopId == workshopId)
|
|
.Where(x =>
|
|
((x.LeftWorkDate != null && x.LeftWorkDate != DateTime.MinValue) &&
|
|
((DateTime)x.LeftWorkDate >= startDate &&
|
|
(DateTime)x.LeftWorkDate <= endDate)) ||
|
|
((x.LeftWorkDate != null && x.LeftWorkDate != DateTime.MinValue) &&
|
|
(DateTime)x.LeftWorkDate >= endDate) ||
|
|
(x.LeftWorkDate == null || x.LeftWorkDate == DateTime.MinValue))
|
|
.Where(x => x.StartWorkDate <= endDate)
|
|
.Join(_context.Jobs,
|
|
left => left.JobId,
|
|
job => job.id,
|
|
(left, job) => new { left, job })
|
|
.GroupJoin(_context.InsuranceEmployeeInformationSet.AsSplitQuery(),
|
|
result => result.left.EmployeeId,
|
|
employeeInfo => employeeInfo.EmployeeId,
|
|
(result, employeeInfo) => new
|
|
{
|
|
result.left,
|
|
result.job,
|
|
employeeInfo
|
|
})
|
|
.SelectMany(x => x.employeeInfo.DefaultIfEmpty(), (x, employeeInfo) => new { x, employeeInfo })
|
|
.Select(result => new EmployeeDetailsForInsuranceListViewModel
|
|
{
|
|
StartWorkDateGr = result.x.left.StartWorkDate,
|
|
LeftWorkDateGr = result.x.left.LeftWorkDate,
|
|
IncludeStatus = result.x.left.IncludeStatus,
|
|
JobId = result.x.left.JobId,
|
|
JobName = result.x.job != null ? result.x.job.JobName : string.Empty,
|
|
JobCode = result.x.job != null ? result.x.job.JobCode : string.Empty,
|
|
InsuranceEmployeeInformationId = result.employeeInfo != null ? result.employeeInfo.id : 0,
|
|
EmployeeId = result.x.left.EmployeeId,
|
|
|
|
//اطلاعات هویتی
|
|
FName = result.employeeInfo != null ? result.employeeInfo.FName : result.x.left.Employee.FName,
|
|
LName = result.employeeInfo != null ? result.employeeInfo.LName : result.x.left.Employee.LName,
|
|
FatherName = result.employeeInfo != null ? result.employeeInfo.FatherName : result.x.left.Employee.FatherName,
|
|
DateOfBirthGr = result.employeeInfo != null ? result.employeeInfo.DateOfBirth : result.x.left.Employee.DateOfBirth,
|
|
DateOfIssueGr = result.employeeInfo != null ? result.employeeInfo.DateOfIssue : result.x.left.Employee.DateOfIssue,
|
|
PlaceOfIssue = result.employeeInfo != null ? result.employeeInfo.PlaceOfIssue : result.x.left.Employee.PlaceOfIssue,
|
|
IdNumber = result.employeeInfo != null ? result.employeeInfo.IdNumber : result.x.left.Employee.IdNumber,
|
|
Gender = result.employeeInfo != null ? result.employeeInfo.Gender : result.x.left.Employee.Gender,
|
|
NationalCode = result.x.left.Employee.NationalCode,
|
|
Nationality = result.x.left.Employee.Nationality,
|
|
InsuranceCode = result.x.left.Employee.InsuranceCode,
|
|
MaritalStatus = result.x.left.Employee.MaritalStatus,
|
|
IsMaritalStatusSet = !string.IsNullOrWhiteSpace(result.x.left.Employee.MaritalStatus)
|
|
}).ToList();
|
|
|
|
return query;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Mahan
|
|
|
|
public List<LeftWorkViewModel> GetEmployeesWithContractExitOnly(long workshopId)
|
|
{
|
|
var leftWorkedEmployees = _context.LeftWorkList
|
|
.Where(x => x.WorkshopId == workshopId)
|
|
.GroupBy(x => x.EmployeeId)
|
|
.Select(x => new
|
|
{
|
|
EmployeeId = x.Key,
|
|
x.OrderByDescending(s => s.StartWorkDate).FirstOrDefault().LeftWorkDate
|
|
}).Where(x => x.LeftWorkDate != new DateTime(2121, 3, 21))
|
|
.ToList();
|
|
|
|
var leftWorkEmployeeIds = leftWorkedEmployees.Select(x => x.EmployeeId);
|
|
|
|
var insuranceLeftWorkWithContractExitOnly = _context.LeftWorkInsuranceList.Where(x =>
|
|
x.WorkshopId == workshopId && leftWorkEmployeeIds.Contains(x.EmployeeId) && x.LeftWorkDate == null).ToList()
|
|
.Select(x =>
|
|
{
|
|
var employee = leftWorkedEmployees.First(l => l.EmployeeId == x.EmployeeId);
|
|
return new LeftWorkViewModel()
|
|
{
|
|
WorkshopId = x.WorkshopId,
|
|
EmployeeId = x.EmployeeId,
|
|
EmployeeFullName = x.EmployeeFullName,
|
|
LeftWorkDate = employee.LeftWorkDate.ToFarsi(),
|
|
LeftWorkDateGr = employee.LeftWorkDate
|
|
};
|
|
});
|
|
|
|
return insuranceLeftWorkWithContractExitOnly.ToList();
|
|
}
|
|
|
|
public LeftWorkInsurance GetLastLeftWorkByEmployeeIdAndWorkshopId(long workshopId, long employeeId)
|
|
{
|
|
return _context.LeftWorkInsuranceList.Where(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId)
|
|
.OrderByDescending(x => x.StartWorkDate).FirstOrDefault();
|
|
}
|
|
|
|
#endregion
|
|
} |