1634 lines
72 KiB
C#
1634 lines
72 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using _0_Framework.Application;
|
|
using _0_Framework.Exceptions;
|
|
using _0_Framework.InfraStructure;
|
|
using Company.Domain.ContarctingPartyAgg;
|
|
using Company.Domain.ContractingPartyAccountAgg;
|
|
using Company.Domain.empolyerAgg;
|
|
using Company.Domain.FinancialStatmentAgg;
|
|
using Company.Domain.FinancialTransactionAgg;
|
|
using Company.Domain.InstitutionContractAgg;
|
|
using Company.Domain.WorkshopAgg;
|
|
using CompanyManagment.App.Contracts.Employer;
|
|
using CompanyManagment.App.Contracts.InstitutionContract;
|
|
using CompanyManagment.App.Contracts.Workshop;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
using OfficeOpenXml.Packaging.Ionic.Zip;
|
|
using PersianTools.Core;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class InstitutionContractRepository : RepositoryBase<long, InstitutionContract>, IInstitutionContractRepository
|
|
{
|
|
private readonly CompanyContext _context;
|
|
private readonly IEmployerRepository _employerRepository;
|
|
private readonly IWorkshopRepository _workshopRepository;
|
|
|
|
public InstitutionContractRepository(CompanyContext context, IEmployerRepository employerRepository,
|
|
IWorkshopRepository workshopRepository) : base(context)
|
|
{
|
|
_context = context;
|
|
_employerRepository = employerRepository;
|
|
_workshopRepository = workshopRepository;
|
|
}
|
|
|
|
public EditInstitutionContract GetDetails(long id)
|
|
{
|
|
return _context.InstitutionContractSet.Select(x => new EditInstitutionContract()
|
|
{
|
|
Id = x.id,
|
|
ContractNo = x.ContractNo,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
RepresentativeName = x.RepresentativeName,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
RepresentativeId = x.RepresentativeId,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractDateFa = x.ContractDateFa,
|
|
State = x.State,
|
|
City = x.City,
|
|
Address = x.Address,
|
|
Description = x.Description,
|
|
WorkshopManualCount = x.WorkshopManualCount,
|
|
EmployeeManualCount = x.EmployeeManualCount,
|
|
ContractAmountString = x.ContractAmount.ToMoney(),
|
|
ContractAmount = x.ContractAmount,
|
|
DailyCompenseationString = x.DailyCompenseation.ToMoney(),
|
|
ObligationString = x.Obligation.ToMoney(),
|
|
TotalAmountString = x.TotalAmount.ToMoney(),
|
|
ExtensionNo = x.ExtensionNo,
|
|
OfficialCompany = x.OfficialCompany,
|
|
TypeOfContract = x.TypeOfContract,
|
|
Signature = x.Signature,
|
|
HasValueAddedTax = x.HasValueAddedTax,
|
|
ValueAddedTax = x.ValueAddedTax,
|
|
})
|
|
.FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
public EditInstitutionContract GetFirstContract(long contractingPartyId, string typeOfContract)
|
|
{
|
|
return _context.InstitutionContractSet.Select(x => new EditInstitutionContract()
|
|
{
|
|
Id = x.id,
|
|
ContractNo = x.ContractNo,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
RepresentativeName = x.RepresentativeName,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
RepresentativeId = x.RepresentativeId,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractDateFa = x.ContractDateFa,
|
|
State = x.State,
|
|
City = x.City,
|
|
Address = x.Address,
|
|
Description = x.Description,
|
|
WorkshopManualCount = x.WorkshopManualCount,
|
|
EmployeeManualCount = x.EmployeeManualCount,
|
|
ContractAmountString = x.ContractAmount.ToMoney(),
|
|
DailyCompenseationString = x.DailyCompenseation.ToMoney(),
|
|
ObligationString = x.Obligation.ToMoney(),
|
|
TotalAmountString = x.TotalAmount.ToMoney(),
|
|
ExtensionNo = x.ExtensionNo,
|
|
OfficialCompany = x.OfficialCompany,
|
|
TypeOfContract = x.TypeOfContract,
|
|
Signature = x.Signature
|
|
})
|
|
.Where(x => x.ContractingPartyId == contractingPartyId && x.TypeOfContract == typeOfContract)
|
|
.OrderBy(x => x.ExtensionNo).FirstOrDefault();
|
|
}
|
|
|
|
public List<InstitutionContractViewModel> InstitutionContractsWithoutAccount()
|
|
{
|
|
var now = DateTime.Now;
|
|
var contractHasClientAccountList = _context.InstitutionContractSet.AsSplitQuery().Where(x =>
|
|
x.IsActiveString == "true" && x.ContractStartGr <= now && x.ContractEndGr >= now)
|
|
.Join(_context.ContractingPartyAccounts,
|
|
contract => contract.ContractingPartyId,
|
|
acc => acc.PersonalContractingPartyId,
|
|
((contract, account) => new { contract, account })).Select(x => x.contract);
|
|
|
|
var allActiveContracts = _context.InstitutionContractSet.Where(x =>
|
|
x.IsActiveString == "true" && x.ContractStartGr <= now && x.ContractEndGr >= now);
|
|
|
|
var contractWithoutAccountList = allActiveContracts.Except(contractHasClientAccountList);
|
|
|
|
return contractWithoutAccountList.Select(x => new InstitutionContractViewModel
|
|
{
|
|
Id = x.id,
|
|
ContractNo = x.ContractNo,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
RepresentativeId = x.RepresentativeId,
|
|
RepresentativeName = x.RepresentativeName,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractAmount = x.ContractAmount.ToMoney(),
|
|
TotalAmount = x.TotalAmount.ToMoney(),
|
|
SearchAmount = x.ContractAmount,
|
|
IsActiveString = x.IsActiveString,
|
|
OfficialCompany = x.OfficialCompany,
|
|
TypeOfContract = x.TypeOfContract,
|
|
}).ToList();
|
|
}
|
|
|
|
|
|
public List<InstitutionContractViewModel> ContractWithoutValidContactInfo()
|
|
{
|
|
var now = DateTime.Now;
|
|
var contractHasContactInfo = _context.InstitutionContractSet.AsSplitQuery().Where(x =>
|
|
x.IsActiveString == "true" && x.ContractStartGr <= now && x.ContractEndGr >= now)
|
|
.Join(_context.InstitutionContractContactInfos,
|
|
contract => contract.id,
|
|
contactInfo => contactInfo.InstitutionContractId,
|
|
((contract, contactInfo) => new { contract, contactInfo }))
|
|
.Where(x => x.contactInfo.SendSms && x.contactInfo.Position == "طرف قرارداد" &&
|
|
x.contactInfo.PhoneType == "شماره همراه")
|
|
.Select(x => x.contract);
|
|
|
|
var allvalidCcntactInfoContracts = _context.InstitutionContractSet.Where(x =>
|
|
x.IsActiveString == "true" && x.ContractStartGr <= now && x.ContractEndGr >= now);
|
|
|
|
var contractWithoutAccountList = allvalidCcntactInfoContracts.Except(contractHasContactInfo);
|
|
|
|
return contractWithoutAccountList.Select(x => new InstitutionContractViewModel
|
|
{
|
|
Id = x.id,
|
|
ContractNo = x.ContractNo,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
RepresentativeId = x.RepresentativeId,
|
|
RepresentativeName = x.RepresentativeName,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractAmount = x.ContractAmount.ToMoney(),
|
|
TotalAmount = x.TotalAmount.ToMoney(),
|
|
SearchAmount = x.ContractAmount,
|
|
IsActiveString = x.IsActiveString,
|
|
OfficialCompany = x.OfficialCompany,
|
|
TypeOfContract = x.TypeOfContract,
|
|
}).ToList();
|
|
}
|
|
|
|
public List<InstitutionContractViewModel> Search(InstitutionContractSearchModel searchModel)
|
|
{
|
|
//var stored = _context.InstitutionContractSet.FromSqlInterpolated($"SelectQuery_InstitutionContract").AsNoTracking()
|
|
// .ToList();
|
|
|
|
var query = _context.InstitutionContractSet.Select(x => new InstitutionContractViewModel()
|
|
{
|
|
Id = x.id,
|
|
ContractNo = x.ContractNo,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
RepresentativeId = x.RepresentativeId,
|
|
RepresentativeName = x.RepresentativeName,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractAmount = x.ContractAmount.ToMoney(),
|
|
TotalAmount = x.TotalAmount.ToMoney(),
|
|
SearchAmount = x.ContractAmount,
|
|
IsActiveString = x.IsActiveString,
|
|
OfficialCompany = x.OfficialCompany,
|
|
TypeOfContract = x.TypeOfContract,
|
|
Signature = x.Signature
|
|
});
|
|
|
|
if (searchModel.Id != 0)
|
|
query = query.Where(x => x.Id == searchModel.Id);
|
|
if (searchModel.RepresentativeId != 0)
|
|
query = query.Where(x => x.RepresentativeId == searchModel.RepresentativeId);
|
|
if (searchModel.ContractingPartyId != 0)
|
|
query = query.Where(x => x.ContractingPartyId == searchModel.ContractingPartyId);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartAmount) &&
|
|
!string.IsNullOrWhiteSpace(searchModel.EndAmount))
|
|
{
|
|
var start = Convert.ToDouble(searchModel.StartAmount);
|
|
var end = Convert.ToDouble(searchModel.EndAmount);
|
|
query = query.Where(x =>
|
|
x.SearchAmount >= start && x.SearchAmount <= end);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartAmount) &&
|
|
string.IsNullOrWhiteSpace(searchModel.EndAmount))
|
|
{
|
|
var start = Convert.ToDouble(searchModel.StartAmount);
|
|
var end = Convert.ToDouble(searchModel.EndAmount);
|
|
query = query.Where(x =>
|
|
x.SearchAmount >= start);
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(searchModel.StartAmount) &&
|
|
!string.IsNullOrWhiteSpace(searchModel.EndAmount))
|
|
{
|
|
var start = Convert.ToDouble(searchModel.StartAmount);
|
|
var end = Convert.ToDouble(searchModel.EndAmount);
|
|
query = query.Where(x =>
|
|
x.SearchAmount >= start);
|
|
query = query.Where(x =>
|
|
x.SearchAmount <= end);
|
|
}
|
|
|
|
if (searchModel.IsActiveString == null)
|
|
{
|
|
query = query.Where(x => x.IsActiveString == "true" || x.IsActiveString == "blue");
|
|
}
|
|
|
|
if (searchModel.IsActiveString == "false")
|
|
{
|
|
query = query.Where(x => x.IsActiveString == "false");
|
|
}
|
|
else if (searchModel.IsActiveString == "both")
|
|
{
|
|
query = query.Where(x =>
|
|
x.IsActiveString == "false" || x.IsActiveString == "true" || x.IsActiveString == "blue");
|
|
}
|
|
|
|
if (searchModel.OfficialCompany == "Official")
|
|
query = query.Where(x => x.OfficialCompany == "Official");
|
|
if (searchModel.OfficialCompany == "NotOfficial")
|
|
query = query.Where(x => x.OfficialCompany == "NotOfficial");
|
|
|
|
if (searchModel.TypeOfContract == "both")
|
|
{
|
|
query = query.Where(x => x.TypeOfContract == "JobRelation" || x.TypeOfContract == "JobRelation");
|
|
}
|
|
else if (searchModel.TypeOfContract == "JobRelation" || string.IsNullOrWhiteSpace(searchModel.TypeOfContract))
|
|
{
|
|
query = query.Where(x => x.TypeOfContract == "JobRelation");
|
|
}
|
|
else if (searchModel.TypeOfContract == "taxAndFinancial")
|
|
{
|
|
query = query.Where(x => x.TypeOfContract == "taxAndFinancial");
|
|
}
|
|
|
|
|
|
if (searchModel.Signature == "2")
|
|
{
|
|
query = query.Where(x => x.Signature == "0" || x.Signature == "1");
|
|
}
|
|
|
|
if (searchModel.Signature == "1")
|
|
{
|
|
query = query.Where(x => x.Signature == "1");
|
|
}
|
|
else if (searchModel.Signature == "0")
|
|
{
|
|
query = query.Where(x => x.Signature == "0");
|
|
}
|
|
//var sumList = query.Select(x => x.SearchAmount).ToList();
|
|
//double sum = 0;
|
|
//foreach (var amount in sumList)
|
|
//{
|
|
// sum += amount;
|
|
//}
|
|
//Console.WriteLine(sum);
|
|
|
|
|
|
return query.OrderByDescending(x => x.Id).ToList();
|
|
}
|
|
|
|
public List<InstitutionContractViewModel> NewSearch(InstitutionContractSearchModel searchModel)
|
|
{
|
|
var timer = Stopwatch.StartNew();
|
|
var query = _context.InstitutionContractSet.AsSplitQuery()
|
|
.Select(x => new InstitutionContractViewModel()
|
|
{
|
|
Id = x.id,
|
|
ContractNo = x.ContractNo,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
RepresentativeId = x.RepresentativeId,
|
|
RepresentativeName = x.RepresentativeName,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractAmount = x.ContractAmount.ToMoney(),
|
|
TotalAmount = x.TotalAmount.ToMoney(),
|
|
SearchAmount = x.ContractAmount,
|
|
IsActiveString = x.IsActiveString,
|
|
OfficialCompany = x.OfficialCompany,
|
|
TypeOfContract = x.TypeOfContract,
|
|
Signature = x.Signature,
|
|
|
|
WorkshopCount = "",
|
|
//WorkshopViewModels = _context.Workshops.Include(w=>w.WorkshopEmployers).Include(w => w.LeftWorks).Include(w => w.LeftWorkInsurances).Select(w => new WorkshopViewModel()
|
|
//{
|
|
// Id = w.id,
|
|
// WorkshopName = w.WorkshopName,
|
|
// WorkshopFullName = w.WorkshopFullName,
|
|
// ArchiveCode = w.ArchiveCode,
|
|
// ContractingPartId = w.WorkshopEmployers.Select(e => e.Employer.ContractingPartyId).FirstOrDefault(),
|
|
// LeftWorkIds = w.LeftWorks.Where(l => l.StartWorkDate <= DateTime.Now && l.LeftWorkDate > DateTime.Now).Select(l => l.EmployeeId).ToList(),
|
|
// InsuranceLeftWorkIds = w.LeftWorkInsurances.Where(l => (l.StartWorkDate <= DateTime.Now && l.LeftWorkDate > DateTime.Now) || (l.StartWorkDate <= DateTime.Now && l.LeftWorkDate == null)).Select(l => l.EmployeeId).ToList(),
|
|
//}).Where(w=>w.ContractingPartId == x.ContractingPartyId).ToList(),
|
|
WorkshopIds = _context.Workshops
|
|
.Include(w => w.WorkshopEmployers).Select(w => new WorkshopViewModel()
|
|
{
|
|
Id = w.id,
|
|
ContractingPartId = w.WorkshopEmployers.Select(e => e.Employer.ContractingPartyId)
|
|
.FirstOrDefault(),
|
|
}).Where(c => c.ContractingPartId == x.ContractingPartyId).Select(w => w.Id).ToList(),
|
|
EmployerViewModels = _context.Employers.Where(e => e.ContractingPartyId == x.ContractingPartyId)
|
|
.Select(e =>
|
|
new EmployerViewModel()
|
|
{
|
|
Id = e.id,
|
|
FullName = e.FName + " " + e.LName,
|
|
}).GroupBy(e => e.Id).Select(e => e.First()).ToList(),
|
|
EmployerIds = _context.Employers.AsSplitQuery().Where(e => e.ContractingPartyId == x.ContractingPartyId)
|
|
.Select(e => e.id).ToList(),
|
|
EmployerNo = "",
|
|
EmployerName = "",
|
|
|
|
IsContractingPartyBlock =
|
|
_context.PersonalContractingParties.AsSplitQuery()
|
|
.Any(p => p.id == x.ContractingPartyId && p.IsBlock == "true")
|
|
? "true"
|
|
: "false",
|
|
BlockTimes = _context.PersonalContractingParties.AsSplitQuery().Any(p => p.id == x.ContractingPartyId)
|
|
? _context.PersonalContractingParties.FirstOrDefault(p => p.id == x.ContractingPartyId).BlockTimes
|
|
: 0,
|
|
});
|
|
|
|
if (searchModel.Id != 0)
|
|
query = query.Where(x => x.Id == searchModel.Id);
|
|
if (searchModel.RepresentativeId != 0)
|
|
query = query.Where(x => x.RepresentativeId == searchModel.RepresentativeId);
|
|
if (searchModel.ContractingPartyId != 0)
|
|
query = query.Where(x => x.ContractingPartyId == searchModel.ContractingPartyId);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartAmount) &&
|
|
!string.IsNullOrWhiteSpace(searchModel.EndAmount))
|
|
{
|
|
var start = Convert.ToDouble(searchModel.StartAmount);
|
|
var end = Convert.ToDouble(searchModel.EndAmount);
|
|
query = query.Where(x =>
|
|
x.SearchAmount >= start && x.SearchAmount <= end);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartAmount) &&
|
|
string.IsNullOrWhiteSpace(searchModel.EndAmount))
|
|
{
|
|
var start = Convert.ToDouble(searchModel.StartAmount);
|
|
var end = Convert.ToDouble(searchModel.EndAmount);
|
|
query = query.Where(x =>
|
|
x.SearchAmount >= start);
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(searchModel.StartAmount) &&
|
|
!string.IsNullOrWhiteSpace(searchModel.EndAmount))
|
|
{
|
|
var start = Convert.ToDouble(searchModel.StartAmount);
|
|
var end = Convert.ToDouble(searchModel.EndAmount);
|
|
query = query.Where(x =>
|
|
x.SearchAmount >= start);
|
|
query = query.Where(x =>
|
|
x.SearchAmount <= end);
|
|
}
|
|
|
|
|
|
if (searchModel.IsActiveString == null)
|
|
{
|
|
query = query.Where(x => x.IsActiveString == "true" || x.IsActiveString == "blue");
|
|
}
|
|
|
|
if (searchModel.IsActiveString == "false")
|
|
{
|
|
query = query.Where(x => x.IsActiveString == "false");
|
|
}
|
|
else if (searchModel.IsActiveString == "both")
|
|
{
|
|
query = query.Where(x =>
|
|
x.IsActiveString == "false" || x.IsActiveString == "true" || x.IsActiveString == "blue");
|
|
}
|
|
|
|
if (searchModel.OfficialCompany == "Official")
|
|
query = query.Where(x => x.OfficialCompany == "Official");
|
|
if (searchModel.OfficialCompany == "NotOfficial")
|
|
query = query.Where(x => x.OfficialCompany == "NotOfficial");
|
|
|
|
if (searchModel.TypeOfContract == "both")
|
|
{
|
|
query = query.Where(x => x.TypeOfContract == "JobRelation" || x.TypeOfContract == "taxAndFinancial");
|
|
}
|
|
else if (searchModel.TypeOfContract == "JobRelation" || string.IsNullOrWhiteSpace(searchModel.TypeOfContract))
|
|
{
|
|
query = query.Where(x => x.TypeOfContract == "JobRelation");
|
|
}
|
|
else if (searchModel.TypeOfContract == "taxAndFinancial")
|
|
{
|
|
query = query.Where(x => x.TypeOfContract == "taxAndFinancial");
|
|
}
|
|
|
|
if (searchModel.WorkshopId != 0)
|
|
{
|
|
query = query.Where(x => x.WorkshopIds.Count > 0 && x.WorkshopIds.Any(e => e == searchModel.WorkshopId));
|
|
}
|
|
|
|
if (searchModel.EmployerId != 0)
|
|
{
|
|
query = query.Where(x => x.EmployerIds.Count > 0 && x.EmployerIds.Any(e => e == searchModel.EmployerId));
|
|
}
|
|
|
|
if (searchModel.Signature == "2")
|
|
{
|
|
query = query.Where(x => x.Signature == "0" || x.Signature == "1");
|
|
}
|
|
|
|
if (searchModel.Signature == "1")
|
|
{
|
|
query = query.Where(x => x.Signature == "1");
|
|
}
|
|
else if (searchModel.Signature == "0")
|
|
{
|
|
query = query.Where(x => x.Signature == "0");
|
|
}
|
|
|
|
|
|
var listQuery = query.ToList();
|
|
|
|
|
|
listQuery = listQuery.Select(x => new InstitutionContractViewModel()
|
|
{
|
|
Id = x.Id,
|
|
ContractNo = x.ContractNo,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
RepresentativeId = x.RepresentativeId,
|
|
RepresentativeName = x.RepresentativeName,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractAmount = x.ContractAmount,
|
|
TotalAmount = x.TotalAmount,
|
|
SearchAmount = x.SearchAmount,
|
|
IsActiveString = x.IsActiveString,
|
|
OfficialCompany = x.OfficialCompany,
|
|
TypeOfContract = x.TypeOfContract,
|
|
Signature = x.Signature,
|
|
ExpireColor = ExpColor(x.ContractEndGr, x.SearchAmount,
|
|
x.IsActiveString).result,
|
|
IsExpier = ExpColor(x.ContractEndGr, x.SearchAmount,
|
|
x.IsActiveString).isExpier,
|
|
BalanceDouble = TotalBalance(x.ContractingPartyId).TotalBalanceDbl,
|
|
BalanceStr = TotalBalance(x.ContractingPartyId).TotalBalanceStr,
|
|
EmployerViewModels = x.EmployerViewModels,
|
|
EmployerNo = x.EmployerNo,
|
|
// EmployerName = x.EmployerViewModels.Select(n => n.FullName).FirstOrDefault(),
|
|
// WorkshopViewModels = x.WorkshopViewModels,
|
|
WorkshopCount = Convert.ToString(x.WorkshopIds.Count),
|
|
IsContractingPartyBlock = x.IsContractingPartyBlock,
|
|
BlockTimes = x.BlockTimes,
|
|
// EmployeeCount = ((x.WorkshopViewModels.Sum(w => w.LeftWorkIds.Count)) + (x.WorkshopViewModels.Sum(w => w.InsuranceLeftWorkIds.Count(c => !w.LeftWorkIds.Contains(c))))).ToString(),
|
|
// ArchiveCode = x.WorkshopViewModels.Count > 0 ? ArchiveCodeFinder(x.WorkshopViewModels) : 0,
|
|
WorkshopViewModels = _context.Workshops.AsSplitQuery().Where(w => x.WorkshopIds.Contains(w.id))
|
|
.Include(w => w.LeftWorks).Include(w => w.LeftWorkInsurances).Select(w => new WorkshopViewModel()
|
|
{
|
|
Id = w.id,
|
|
WorkshopName = w.WorkshopName,
|
|
WorkshopFullName = w.WorkshopFullName,
|
|
ArchiveCode = w.ArchiveCode,
|
|
ContractingPartId = w.WorkshopEmployers.Select(e => e.Employer.ContractingPartyId).FirstOrDefault(),
|
|
LeftWorkIds = w.LeftWorks
|
|
.Where(l => l.StartWorkDate <= DateTime.Now && l.LeftWorkDate > DateTime.Now)
|
|
.Select(l => l.EmployeeId).ToList(),
|
|
InsuranceLeftWorkIds = w.LeftWorkInsurances
|
|
.Where(l => (l.StartWorkDate <= DateTime.Now && l.LeftWorkDate > DateTime.Now) ||
|
|
(l.StartWorkDate <= DateTime.Now && l.LeftWorkDate == null))
|
|
.Select(l => l.EmployeeId).ToList(),
|
|
}).ToList(),
|
|
}).ToList();
|
|
listQuery = listQuery.Select(x => new InstitutionContractViewModel()
|
|
{
|
|
Id = x.Id,
|
|
ContractNo = x.ContractNo,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
RepresentativeId = x.RepresentativeId,
|
|
RepresentativeName = x.RepresentativeName,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractAmount = x.ContractAmount,
|
|
TotalAmount = x.TotalAmount,
|
|
SearchAmount = x.SearchAmount,
|
|
IsActiveString = x.IsActiveString,
|
|
OfficialCompany = x.OfficialCompany,
|
|
TypeOfContract = x.TypeOfContract,
|
|
Signature = x.Signature,
|
|
ExpireColor = x.ExpireColor,
|
|
IsExpier = x.IsExpier,
|
|
BalanceDouble = x.BalanceDouble,
|
|
BalanceStr = x.BalanceStr,
|
|
EmployerViewModels = x.EmployerViewModels,
|
|
EmployerNo = x.EmployerNo,
|
|
EmployerName = x.EmployerViewModels.Select(n => n.FullName).FirstOrDefault(),
|
|
WorkshopViewModels = x.WorkshopViewModels,
|
|
WorkshopCount = x.WorkshopCount,
|
|
IsContractingPartyBlock = x.IsContractingPartyBlock,
|
|
BlockTimes = x.BlockTimes,
|
|
EmployeeCount =
|
|
((x.WorkshopViewModels.Sum(w => w.LeftWorkIds.Count)) + (x.WorkshopViewModels.Sum(w =>
|
|
w.InsuranceLeftWorkIds.Count(c => !w.LeftWorkIds.Contains(c))))).ToString(),
|
|
ArchiveCode = x.WorkshopViewModels.Count > 0 ? ArchiveCodeFinder(x.WorkshopViewModels) : 0,
|
|
}).OrderBy(x => x.WorkshopCount != "0" && string.IsNullOrWhiteSpace(x.ExpireColor))
|
|
.ThenBy(x => x.WorkshopCount == "0" && string.IsNullOrWhiteSpace(x.ExpireColor))
|
|
.ThenBy(x => x.IsExpier == "true")
|
|
.ThenBy(x => x.ExpireColor == "purple")
|
|
.ThenBy(x => x.ExpireColor == "black").ToList();
|
|
Console.WriteLine("test >>> " + timer.Elapsed);
|
|
return listQuery;
|
|
}
|
|
|
|
|
|
public List<InstitutionContractViewModel> PrintAll(List<long> id)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public InstitutionContractViewModel PrintOne(long id)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void RemoveContract(long id)
|
|
{
|
|
var op = new OperationResult();
|
|
var institutionContarct = _context.InstitutionContractSet.FirstOrDefault(x => x.id == id);
|
|
var contactInfo = _context.InstitutionContractContactInfos.Where(x => x.InstitutionContractId == id)
|
|
.ToList();
|
|
if (contactInfo.Count > 0)
|
|
{
|
|
foreach (var item in contactInfo)
|
|
{
|
|
_context.InstitutionContractContactInfos.Remove(item);
|
|
_context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
if (institutionContarct != null)
|
|
{
|
|
_context.InstitutionContractSet.Remove(institutionContarct);
|
|
|
|
_context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public void CreateContractingPartyAccount(long contractingPartyid, long accountId)
|
|
{
|
|
var create = new ContractingPartyAccount(contractingPartyid, accountId);
|
|
_context.ContractingPartyAccounts.Add(create);
|
|
_context.SaveChanges();
|
|
}
|
|
|
|
public double GetcontractAmount(int countPerson)
|
|
{
|
|
var planPercentage = _context.PlanPercentages.FirstOrDefault();
|
|
int contarctAndCheckoutPercent = 100;
|
|
int insurancePercent = 50;
|
|
int rollCallPercent = 100;
|
|
int customizeCkeckoutPercen = 50;
|
|
int contarctAndCheckoutInPersonPercent = 900;
|
|
int insuranceInPersonPercent = 500;
|
|
if (planPercentage != null)
|
|
{
|
|
contarctAndCheckoutPercent = planPercentage.ContractAndCheckoutPercent;
|
|
insurancePercent = planPercentage.InsurancePercent;
|
|
rollCallPercent = planPercentage.RollCallPercent;
|
|
customizeCkeckoutPercen = planPercentage.CustomizeCheckoutPercent;
|
|
contarctAndCheckoutInPersonPercent = planPercentage.ContractAndCheckoutInPersonPercent;
|
|
insuranceInPersonPercent = planPercentage.InsuranceInPersonPercent;
|
|
}
|
|
|
|
|
|
var dailyWageYearlySalery = _context.YearlySalaries.Include(i => i.YearlySalaryItemsList).FirstOrDefault(x =>
|
|
x.StartDate.Date <= DateTime.Now.Date && x.EndDate >= DateTime.Now.Date);
|
|
double res = 0;
|
|
if (countPerson > 0 && dailyWageYearlySalery != null)
|
|
{
|
|
var dailyWage = dailyWageYearlySalery.YearlySalaryItemsList.Where(x => x.ItemName == "مزد روزانه")
|
|
.Select(x => x.ItemValue).FirstOrDefault();
|
|
|
|
|
|
var plan = _context.InstitutionPlans.FirstOrDefault(x => x.CountPerson == countPerson);
|
|
if (plan != null)
|
|
{
|
|
//مبلغ قرارداد و تصفیه
|
|
var contarctAndCheckout = ((dailyWage * contarctAndCheckoutPercent) / 100) * countPerson *
|
|
plan.IncreasePercentage;
|
|
//خدمات بیمه
|
|
var insurance = ((dailyWage * insurancePercent) / 100) * countPerson *
|
|
plan.IncreasePercentage;
|
|
////خدمات حضور غیاب
|
|
//var rollCall = ((dailyWage * rollCallPercent) / 100) * countPerson *
|
|
// plan.IncreasePercentage;
|
|
|
|
////خدمات فیش حقوقی غیر رسمی
|
|
//var customizeCkeckout = ((dailyWage * customizeCkeckoutPercen) / 100) * countPerson *
|
|
// plan.IncreasePercentage;
|
|
|
|
//خدمات حضوری قرارداد و تصفیه
|
|
var contarctAndCheckoutInPerson =
|
|
((dailyWage * contarctAndCheckoutInPersonPercent) / 100) * countPerson *
|
|
plan.IncreasePercentage;
|
|
|
|
//خدمات حضوری بیمه
|
|
var insuranceInPerson = ((dailyWage * insuranceInPersonPercent) / 100) * countPerson *
|
|
plan.IncreasePercentage;
|
|
|
|
|
|
//جمع کل
|
|
res = contarctAndCheckout + insurance + contarctAndCheckoutInPerson + insuranceInPerson;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
public (string result, string isExpier) ExpColor(DateTime contractEndGr, double contractAmount,
|
|
string isActiveString)
|
|
{
|
|
var now = DateTime.Now;
|
|
var nowFa = now.ToFarsi();
|
|
var endFa = nowFa.FindeEndOfMonth();
|
|
var endThisMontGr = endFa.ToGeorgianDateTime();
|
|
string result = "";
|
|
string isExpier = "false";
|
|
if (contractEndGr < now)
|
|
{
|
|
result = "black";
|
|
isExpier = "true";
|
|
}
|
|
|
|
if (contractEndGr >= now && contractEndGr <= endThisMontGr)
|
|
{
|
|
result = "red";
|
|
isExpier = "true";
|
|
}
|
|
|
|
if (contractAmount == 0)
|
|
{
|
|
result = "purple";
|
|
if ((contractEndGr >= now && contractEndGr <= endThisMontGr) || (contractEndGr < now))
|
|
{
|
|
isExpier = "true";
|
|
}
|
|
}
|
|
|
|
if (isActiveString == "blue")
|
|
{
|
|
result = "blue";
|
|
isExpier = "false";
|
|
}
|
|
|
|
return (result, isExpier);
|
|
}
|
|
|
|
|
|
#region ExteraMetods
|
|
|
|
public TotalbalancViewModel TotalBalance(long contractingPartyId)
|
|
{
|
|
var result = new TotalbalancViewModel();
|
|
|
|
var firstGetStatement = _context.FinancialStatments.Include(x => x.FinancialTransactionList)
|
|
.FirstOrDefault(x => x.ContractingPartyId == contractingPartyId);
|
|
|
|
if (firstGetStatement != null)
|
|
{
|
|
if (firstGetStatement.FinancialTransactionList != null)
|
|
{
|
|
var allTransactions = firstGetStatement.FinancialTransactionList;
|
|
allTransactions = allTransactions.OrderBy(x => x.TdateGr).ToList();
|
|
var debt = allTransactions.Sum(x => x.Deptor);
|
|
var credit = allTransactions.Sum(x => x.Creditor);
|
|
result.TotalBalanceDbl = debt - credit;
|
|
result.TotalBalanceStr = result.TotalBalanceDbl.ToMoney();
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public int ArchiveCodeFinder(List<WorkshopViewModel> workshopViewModels)
|
|
{
|
|
var workshop = workshopViewModels.OrderBy(x => x.Id)?.ToList();
|
|
|
|
var arc = workshop.Select(x => new ArchiveCodConvertoint
|
|
{
|
|
ArchiveCodeInt = x.ArchiveCode.Substring(0, 1) == "b" ? 10000000 : x.ArchiveCode.ConvertToInt(),
|
|
}).OrderBy(x => x.ArchiveCodeInt).ToList();
|
|
var minArchiveCode = arc.Min(x => x.ArchiveCodeInt);
|
|
return minArchiveCode == 10000000 ? 0 : minArchiveCode;
|
|
}
|
|
|
|
#endregion
|
|
|
|
public InstitutionContract InstitutionContractByEmployerId(long employerId)
|
|
{
|
|
long contractingPryId = _context.Employers.FirstOrDefault(x => x.id == employerId)!.ContractingPartyId;
|
|
if (_context.PersonalContractingParties.Any(x => x.id == contractingPryId && x.IsBlock == "true"))
|
|
return new();
|
|
var contracts = _context.InstitutionContractSet.Where(x =>
|
|
x.ContractingPartyId == contractingPryId).ToList();
|
|
var contract = contracts.FirstOrDefault(x => x.IsActiveString != "false" &&
|
|
x.ContractStartGr.Date <= DateTime.Now.Date &&
|
|
x.ContractEndGr >= DateTime.Now.Date);
|
|
if (contract != null)
|
|
{
|
|
return contract;
|
|
}
|
|
else
|
|
{
|
|
var future = contracts.FirstOrDefault(x => x.IsActiveString != "false" &&
|
|
x.ContractStartGr.Date >= DateTime.Now.Date &&
|
|
x.ContractEndGr >= DateTime.Now.Date);
|
|
if (future != null)
|
|
return future;
|
|
return new();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// ایجاد سند مالی حضور غیاب
|
|
/// </summary>
|
|
/// <param name="now"></param>
|
|
/// <param name="endOfMonthGr"></param>
|
|
/// <param name="endOfMonth"></param>
|
|
/// <param name="description"></param>
|
|
|
|
#region RollcallServicCreateTransaction
|
|
|
|
public void RollcallServiceCreateTransaction()
|
|
{
|
|
DateTime now = DateTime.Now;
|
|
var nowFa = now.ToFarsi();
|
|
var endOfMonth = nowFa.FindeEndOfMonth();
|
|
|
|
DateTime endOfMonthGr = endOfMonth.ToGeorgianDateTime();
|
|
|
|
|
|
#region FindeNextMonth 1th
|
|
|
|
var year = Convert.ToInt32(endOfMonth.Substring(0, 4));
|
|
var month = Convert.ToInt32(endOfMonth.Substring(5, 2));
|
|
var nextM = new PersianDateTime(year, month, 1).AddMonths(1);
|
|
var nextMonthGr = ($"{nextM}").ToGeorgianDateTime();
|
|
var endOfCurrentMonth = (($"{endOfMonth.Substring(0, 8)}/01").FindeEndOfMonth()).ToGeorgianDateTime();
|
|
|
|
#endregion
|
|
|
|
#region GetAvtiveContracts
|
|
|
|
var institutionContracts = _context.InstitutionContractSet.Where(x => x.IsActiveString == "true").Select(x =>
|
|
new InstitutionContractViewModel
|
|
{
|
|
Id = x.id,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
IsActiveString = x.IsActiveString,
|
|
SearchAmount = x.ContractAmount,
|
|
TypeOfContract = x.TypeOfContract
|
|
}).Where(x =>
|
|
x.ContractStartGr < endOfMonthGr && x.ContractEndGr >= endOfMonthGr && x.SearchAmount > 0)
|
|
.ToList();
|
|
|
|
#endregion
|
|
|
|
#region GetFutureContracts
|
|
|
|
List<InstitutionContractViewModel> futureContracts = _context.InstitutionContractSet
|
|
.Where(x => x.IsActiveString == "true" &&
|
|
x.ContractStartGr == nextMonthGr && x.ContractAmount > 0)
|
|
.Select(x => new InstitutionContractViewModel
|
|
{
|
|
Id = x.id,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
IsActiveString = x.IsActiveString,
|
|
TypeOfContract = x.TypeOfContract,
|
|
ExtensionNo = x.ExtensionNo,
|
|
}).ToList();
|
|
|
|
#endregion
|
|
|
|
#region GetDectivedContractOnCurrentMonth
|
|
|
|
if (futureContracts.Any())
|
|
{
|
|
List<long> futureContractIds = futureContracts.Select(x => x.ContractingPartyId).ToList();
|
|
List<InstitutionContractViewModel> deatcivedContract = _context.InstitutionContractSet
|
|
.Where(x => x.IsActiveString == "false" && futureContractIds.Contains(x.ContractingPartyId) &&
|
|
x.ContractEndGr == endOfCurrentMonth && x.ContractAmount > 0)
|
|
.Select(x => new InstitutionContractViewModel
|
|
{
|
|
Id = x.id,
|
|
ContractingPartyId = x.ContractingPartyId,
|
|
ContractingPartyName = x.ContractingPartyName,
|
|
ContractStartGr = x.ContractStartGr,
|
|
ContractStartFa = x.ContractStartFa,
|
|
ContractEndGr = x.ContractEndGr,
|
|
ContractEndFa = x.ContractEndFa,
|
|
IsActiveString = x.IsActiveString,
|
|
SearchAmount = x.ContractAmount,
|
|
TypeOfContract = x.TypeOfContract
|
|
}).ToList();
|
|
|
|
if (deatcivedContract.Any())
|
|
institutionContracts.AddRange(deatcivedContract);
|
|
}
|
|
|
|
List<long> exceptionContractingPartyIds = [30520, 30739];
|
|
institutionContracts = institutionContracts
|
|
.Where(x => !exceptionContractingPartyIds.Contains(x.ContractingPartyId)).ToList();
|
|
int counter = 0;
|
|
var rollcallServiceList =
|
|
_context.RollCallServices.Where(x => x.IsActiveString == "true").ToList();
|
|
var activeStatusDate = new DateTime(2121, 03, 21);
|
|
|
|
foreach (var item in institutionContracts)
|
|
{
|
|
//var isblock = contractingParty.IsBlock == "true" ? true : false;
|
|
|
|
var employers = _context.Employers.Where(x => x.ContractingPartyId == item.ContractingPartyId)
|
|
.Select(x => x.id);
|
|
var workshops = _context.WorkshopEmployers.Where(x => employers.Contains(x.EmployerId))
|
|
.Select(x => x.WorkshopId).Distinct().ToList();
|
|
|
|
|
|
var srvices =
|
|
rollcallServiceList.Where(x => workshops.Contains(x.WorkshopId)).ToList();
|
|
if (rollcallServiceList.Count > 0)
|
|
{
|
|
foreach (var rollCallService in srvices)
|
|
{
|
|
//var spaning = (int)(endOfMonthGr - rollCallService.StartService).TotalDays + 1;
|
|
int monthCounter = 0;
|
|
|
|
var currentMonthStart = ($"{(endOfMonthGr.ToFarsi()).Substring(0, 8)}01").ToGeorgianDateTime();
|
|
var prevMonthEnd = currentMonthStart.AddDays(-1);
|
|
var prevMonthStart = ($"{(prevMonthEnd.ToFarsi()).Substring(0, 8)}01").ToGeorgianDateTime();
|
|
|
|
var monthCurrent = endOfMonth.Substring(5, 2);
|
|
var yearCurrent = endOfMonth.Substring(0, 4);
|
|
var currentMonthName = monthCurrent.ToFarsiMonthByNumber();
|
|
var workshop = _context.Workshops.FirstOrDefault(x => x.id == rollCallService.WorkshopId);
|
|
|
|
string description = "";
|
|
//if (rollCallService.StartService <= prevMonthStart)
|
|
//{
|
|
// var monthPrev = prevMonthEnd.ToFarsi().Substring(5, 2);
|
|
// var yearPrev = prevMonthEnd.ToFarsi().Substring(0, 4);
|
|
// var prevMonthName = monthPrev.ToFarsiMonthByNumber();
|
|
// description =
|
|
// $"{prevMonthName} و {currentMonthName} {yearCurrent} - {workshop.WorkshopFullName}";
|
|
// monthCounter = 2;
|
|
//}
|
|
//else if (rollCallService.StartService <= currentMonthStart &&
|
|
// rollCallService.StartService > prevMonthStart)
|
|
//{
|
|
// monthCounter = 1;
|
|
|
|
// description = $"{currentMonthName} {yearCurrent} - {workshop.WorkshopFullName}";
|
|
//}
|
|
|
|
if (rollCallService.StartService <= currentMonthStart)
|
|
{
|
|
monthCounter = 1;
|
|
|
|
description = $"{currentMonthName} {yearCurrent} - {workshop.WorkshopFullName}";
|
|
}
|
|
|
|
var employees =
|
|
_context.RollCallEmployees.Where(x => x.WorkshopId == rollCallService.WorkshopId)
|
|
.Select(x => x.id);
|
|
|
|
var employeeCount = _context.RollCallEmployeesStatus
|
|
.Where(x => employees.Contains(x.RollCallEmployeeId))
|
|
.Count(x => x.EndDate.Date == activeStatusDate.Date);
|
|
|
|
if (employeeCount > 0 && monthCounter > 0)
|
|
{
|
|
counter++;
|
|
|
|
|
|
var dailyWageYearlySalery = _context.YearlySalaries.Include(i => i.YearlySalaryItemsList)
|
|
.FirstOrDefault(x =>
|
|
x.StartDate.Date <= DateTime.Now.Date && x.EndDate >= DateTime.Now.Date);
|
|
|
|
var dailyWage = dailyWageYearlySalery.YearlySalaryItemsList
|
|
.Where(x => x.ItemName == "مزد روزانه")
|
|
.Select(x => x.ItemValue).FirstOrDefault();
|
|
|
|
var planPercentage = _context.PlanPercentages.FirstOrDefault();
|
|
|
|
var countPersonnel = employeeCount;
|
|
var planByCountPerson = _context.InstitutionPlans
|
|
.FirstOrDefault(x => x.CountPerson == countPersonnel);
|
|
|
|
var amountForOneMonth = (((dailyWage * planPercentage.RollCallPercent) / 100) *
|
|
planByCountPerson.CountPerson *
|
|
planByCountPerson.IncreasePercentage);
|
|
|
|
|
|
var amountWithoutTax = amountForOneMonth * monthCounter;
|
|
|
|
|
|
var tenPercent = amountWithoutTax * 10 / 100;
|
|
var totalAmonut = amountWithoutTax + tenPercent;
|
|
|
|
double roundFloor = Math.Floor(totalAmonut);
|
|
double result = Math.Ceiling(roundFloor / 10000.0) * 10000;
|
|
|
|
|
|
Console.WriteLine(counter + " - " + rollCallService.StartService.ToFarsi() + " - " +
|
|
rollCallService.WorkshopId + " - " + employeeCount +
|
|
$" - {totalAmonut} - round {result}");
|
|
|
|
var financialStatment =
|
|
_context.FinancialStatments.FirstOrDefault(x =>
|
|
x.ContractingPartyId == item.ContractingPartyId);
|
|
long financialStatementId = 0;
|
|
if (financialStatment != null)
|
|
{
|
|
financialStatementId = financialStatment.id;
|
|
}
|
|
else
|
|
{
|
|
var statement = new FinancialStatment(item.ContractingPartyId, item.ContractingPartyName);
|
|
_context.FinancialStatments.Add(statement);
|
|
_context.SaveChanges();
|
|
|
|
financialStatementId = statement.id;
|
|
}
|
|
|
|
var transaction = new FinancialTransaction(financialStatementId, endOfMonthGr, endOfMonth,
|
|
description,
|
|
"debt", "بابت سرویس حضور غیاب", result, 0, 0);
|
|
_context.FinancialTransactions.Add(transaction);
|
|
_context.SaveChanges();
|
|
|
|
//Console.WriteLine(" number : " + counter + " - " + rollCallService.StartService.ToFarsi() + " - WorkshopId : " + rollCallService.WorkshopId + " - monthCount : " + monthCounter + " - employeeCount : " + employeeCount + $" - Amount : {amountWithoutTax.ToMoney()}" + $" - ten : {tenPercent.ToMoney()} total : {totalAmonut.ToMoney()}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public async Task<PagedResult<GetInstitutionContractListItemsViewModel>> GetList(
|
|
InstitutionContractListSearchModel searchModel)
|
|
{
|
|
var query = _context.InstitutionContractSet
|
|
.Include(x => x.ContactInfoList);
|
|
|
|
var now = DateTime.Today;
|
|
var nowFa = now.ToFarsi();
|
|
var endFa = nowFa.FindeEndOfMonth();
|
|
var endThisMontGr = endFa.ToGeorgianDateTime();
|
|
|
|
var joinedQuery = query.Join(_context.PersonalContractingParties
|
|
.Include(x => x.Employers)
|
|
.ThenInclude(x => x.WorkshopEmployers)
|
|
.ThenInclude(x => x.Workshop),
|
|
contract => contract.ContractingPartyId,
|
|
contractingParty => contractingParty.id,
|
|
(contract, contractingParty) => new { contract, contractingParty })
|
|
.Join(_context.FinancialStatments.Include(x => x.FinancialTransactionList),
|
|
x => x.contractingParty.id,
|
|
statement => statement.ContractingPartyId,
|
|
(x, statement) => new { x.contractingParty, x.contract, statement })
|
|
.Select(x => new
|
|
{
|
|
x.contract,
|
|
x.contractingParty,
|
|
x.statement,
|
|
StatusPriority =
|
|
x.contract.IsActiveString == "blue"
|
|
? (int)InstitutionContractListStatus.DeactiveWithDebt
|
|
: x.contract.ContractEndGr < now
|
|
? (int)InstitutionContractListStatus.Deactive
|
|
: (x.contract.ContractEndGr >= now && x.contract.ContractEndGr <= endThisMontGr)
|
|
? (int)InstitutionContractListStatus.PendingForRenewal
|
|
: x.contractingParty.IsBlock == "true"
|
|
? (int)InstitutionContractListStatus.Block
|
|
: x.contract.ContractAmount == 0
|
|
? (int)InstitutionContractListStatus.Free
|
|
: !x.contractingParty.Employers
|
|
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop)).Any()
|
|
? (int)InstitutionContractListStatus.WithoutWorkshop
|
|
: (int)InstitutionContractListStatus.Active
|
|
});
|
|
|
|
#region Search
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.EmployerOrWorkshopOrContractingPartyOrRepresentativeName))
|
|
{
|
|
var keyword = searchModel.EmployerOrWorkshopOrContractingPartyOrRepresentativeName;
|
|
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.contractingParty.RepresentativeFullName.Contains(keyword) ||
|
|
(x.contractingParty.FName + " " + x.contractingParty.LName).Contains(keyword) ||
|
|
x.contractingParty.Employers.Any(e =>
|
|
e.FullName.Contains(keyword) ||
|
|
e.WorkshopEmployers.Any(we =>
|
|
we.Workshop.WorkshopFullName.Contains(keyword)))
|
|
);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.City))
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.City == searchModel.City);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.Province))
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.State == searchModel.Province);
|
|
}
|
|
|
|
if (searchModel.AmountFrom > 0)
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.ContractAmount >= searchModel.AmountFrom);
|
|
}
|
|
|
|
if (searchModel.AmountTo > 0)
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.ContractAmount <= searchModel.AmountTo);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.ContractDateFrom) &&
|
|
!string.IsNullOrWhiteSpace(searchModel.ContractDateTo))
|
|
{
|
|
if (!searchModel.ContractDateFrom.TryToGeorgianDateTime(out var dateFrom))
|
|
throw new BadRequestException("تاریخ وارد شده نامعتبر است");
|
|
|
|
if (!searchModel.ContractDateTo.TryToGeorgianDateTime(out var dateTo))
|
|
{
|
|
throw new BadRequestException("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (dateFrom > dateTo)
|
|
{
|
|
throw new BadRequestException("تاریخ شروع نمیتواند بزرگ تر از تاریخ پایان باشد");
|
|
}
|
|
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.contract.ContractStartGr <= dateTo && x.contract.ContractEndGr <= dateFrom);
|
|
}
|
|
|
|
if (searchModel.HasSignature != null)
|
|
{
|
|
var hasSignature = searchModel.HasSignature == true ? "1" : "0";
|
|
joinedQuery = joinedQuery.Where(x => x.contract.Signature == hasSignature);
|
|
}
|
|
|
|
if (searchModel.IsActive != null)
|
|
{
|
|
var isActiveStr = searchModel.IsActive == true ? "true" : "false";
|
|
joinedQuery = joinedQuery.Where(x => x.contract.IsActiveString == isActiveStr);
|
|
}
|
|
|
|
if (searchModel.Type != null)
|
|
{
|
|
var typeStr = searchModel.Type switch
|
|
{
|
|
InstitutionContractType.JobRelation => "JobRelation",
|
|
InstitutionContractType.TaxAndFinancial => "taxAndFinancial",
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
};
|
|
joinedQuery = joinedQuery.Where(x => x.contract.TypeOfContract == typeStr);
|
|
}
|
|
|
|
if (searchModel.IsOfficial != null)
|
|
{
|
|
var isOfficialStr = searchModel.IsOfficial == true ? "Official" : "NotOfficial";
|
|
joinedQuery = joinedQuery.Where(x => x.contract.OfficialCompany == isOfficialStr);
|
|
}
|
|
|
|
|
|
if (searchModel.Status != null)
|
|
{
|
|
switch (searchModel.Status)
|
|
{
|
|
case InstitutionContractListStatus.DeactiveWithDebt:
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.StatusPriority == (int)InstitutionContractListStatus.DeactiveWithDebt);
|
|
break;
|
|
case InstitutionContractListStatus.PendingForRenewal:
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.StatusPriority == (int)InstitutionContractListStatus.PendingForRenewal);
|
|
break;
|
|
case InstitutionContractListStatus.Block:
|
|
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Block);
|
|
break;
|
|
case InstitutionContractListStatus.Free:
|
|
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Free);
|
|
break;
|
|
case InstitutionContractListStatus.WithoutWorkshop:
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.StatusPriority == (int)InstitutionContractListStatus.WithoutWorkshop);
|
|
break;
|
|
case InstitutionContractListStatus.Active:
|
|
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Active);
|
|
break;
|
|
case InstitutionContractListStatus.Deactive:
|
|
joinedQuery = joinedQuery.Where(x => x.contract.ContractEndGr < now);
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.IsActiveString != "blue");
|
|
}
|
|
|
|
#endregion
|
|
|
|
var endOfMonth = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
|
|
|
|
var orderedQuery = joinedQuery
|
|
.OrderBy(x =>
|
|
x.StatusPriority == (int)InstitutionContractListStatus.DeactiveWithDebt
|
|
? 0
|
|
: // DeactiveWithoutDebt
|
|
(x.StatusPriority == (int)InstitutionContractListStatus.PendingForRenewal)
|
|
? 1
|
|
: // PendingToRenewal
|
|
x.StatusPriority == (int)InstitutionContractListStatus.Block
|
|
? 2
|
|
: // Block
|
|
x.StatusPriority == (int)InstitutionContractListStatus.Free
|
|
? 3
|
|
: // Free
|
|
x.StatusPriority == (int)InstitutionContractListStatus.WithoutWorkshop
|
|
? 4
|
|
: // WithoutWorkshop
|
|
5 // Active
|
|
);
|
|
|
|
var list = await orderedQuery.ApplyPagination(searchModel.PageIndex, searchModel.PageSize).ToListAsync();
|
|
|
|
var res = new PagedResult<GetInstitutionContractListItemsViewModel>()
|
|
{
|
|
TotalCount = joinedQuery.Count(),
|
|
List = list.Select(x =>
|
|
{
|
|
var workshops = x.contractingParty.Employers
|
|
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop)).DistinctBy(w => w.id).ToList();
|
|
|
|
var employers = x.contractingParty.Employers.ToList();
|
|
|
|
var arc = workshops.Select(w =>
|
|
w.ArchiveCode.Substring(0, 1) == "b"
|
|
? 10000000
|
|
: w.ArchiveCode.ConvertToInt()
|
|
).OrderBy(w => w).ToList();
|
|
|
|
var minArchiveCode = arc.Count > 0 ? arc.Min(a => a) : 0;
|
|
|
|
var archiveCode = minArchiveCode == 10000000 ? 0 : minArchiveCode;
|
|
var status = Enum.Parse<InstitutionContractListStatus>(x.StatusPriority.ToString());
|
|
return new GetInstitutionContractListItemsViewModel()
|
|
{
|
|
ContractAmount = x.contract.ContractAmount,
|
|
Balance = x.statement.FinancialTransactionList.Sum(ft => ft.Deptor - ft.Creditor),
|
|
WorkshopsCount = workshops.Count(),
|
|
ContractStartFa = x.contract.ContractStartGr.ToFarsi(),
|
|
ContractEndFa = x.contract.ContractEndGr.ToFarsi(),
|
|
ContractingPartyName = x.contract.ContractingPartyName,
|
|
WorkshopNames = workshops.Select(w => w.WorkshopFullName).ToList(),
|
|
RepresentativeName = x.contractingParty.RepresentativeFullName,
|
|
HasSigniture = x.contract.Signature == "1",
|
|
Id = x.contract.id,
|
|
ContractNo = x.contract.ContractNo,
|
|
ArchiveNo = archiveCode.ToString(),
|
|
EmployeesCount = _context.LeftWorkList
|
|
.Where(l => workshops.Select(w => w.id).Contains(l.id))
|
|
.Count(l => l.StartWorkDate <= DateTime.Now && l.LeftWorkDate >= DateTime.Now),
|
|
EmployerNames = employers.Select(e => e.FullName).ToList(),
|
|
ListStatus = status,
|
|
IsExpired = x.contract.ContractEndGr <= endThisMontGr,
|
|
ContractingPartyId = x.contractingParty.id,
|
|
};
|
|
}).ToList()
|
|
};
|
|
return res;
|
|
}
|
|
|
|
public async Task<GetInstitutionContractListStatsViewModel> GetListStats(
|
|
InstitutionContractListSearchModel searchModel)
|
|
{
|
|
var query = _context.InstitutionContractSet
|
|
.Include(x => x.ContactInfoList);
|
|
|
|
var now = DateTime.Today;
|
|
var nowFa = now.ToFarsi();
|
|
var endFa = nowFa.FindeEndOfMonth();
|
|
var endThisMontGr = endFa.ToGeorgianDateTime();
|
|
|
|
var joinedQuery = query.Join(_context.PersonalContractingParties
|
|
.Include(x => x.Employers)
|
|
.ThenInclude(x => x.WorkshopEmployers)
|
|
.ThenInclude(x => x.Workshop),
|
|
contract => contract.ContractingPartyId,
|
|
contractingParty => contractingParty.id,
|
|
(contract, contractingParty) => new { contract, contractingParty })
|
|
.Join(_context.FinancialStatments.Include(x => x.FinancialTransactionList),
|
|
x => x.contractingParty.id,
|
|
statement => statement.ContractingPartyId,
|
|
(x, statement) => new { x.contractingParty, x.contract, statement })
|
|
.Select(x => new
|
|
{
|
|
x.contract,
|
|
x.contractingParty,
|
|
x.statement,
|
|
StatusPriority =
|
|
x.contract.IsActiveString == "blue"
|
|
? (int)InstitutionContractListStatus.DeactiveWithDebt
|
|
: x.contract.ContractEndGr < now
|
|
? (int)InstitutionContractListStatus.Deactive
|
|
: (x.contract.ContractEndGr >= now && x.contract.ContractEndGr <= endThisMontGr)
|
|
? (int)InstitutionContractListStatus.PendingForRenewal
|
|
: x.contractingParty.IsBlock == "true"
|
|
? (int)InstitutionContractListStatus.Block
|
|
: x.contract.ContractAmount == 0
|
|
? (int)InstitutionContractListStatus.Free
|
|
: !x.contractingParty.Employers
|
|
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop)).Any()
|
|
? (int)InstitutionContractListStatus.WithoutWorkshop
|
|
: (int)InstitutionContractListStatus.Active
|
|
});
|
|
|
|
#region Search
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.EmployerOrWorkshopOrContractingPartyOrRepresentativeName))
|
|
{
|
|
var keyword = searchModel.EmployerOrWorkshopOrContractingPartyOrRepresentativeName;
|
|
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.contractingParty.RepresentativeFullName.Contains(keyword) ||
|
|
(x.contractingParty.FName + " " + x.contractingParty.LName).Contains(keyword) ||
|
|
x.contractingParty.Employers.Any(e =>
|
|
e.FullName.Contains(keyword) ||
|
|
e.WorkshopEmployers.Any(we =>
|
|
we.Workshop.WorkshopFullName.Contains(keyword)))
|
|
);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.City))
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.City == searchModel.City);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.Province))
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.State == searchModel.Province);
|
|
}
|
|
|
|
if (searchModel.AmountFrom > 0)
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.ContractAmount >= searchModel.AmountFrom);
|
|
}
|
|
|
|
if (searchModel.AmountTo > 0)
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.ContractAmount <= searchModel.AmountTo);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.ContractDateFrom) &&
|
|
!string.IsNullOrWhiteSpace(searchModel.ContractDateTo))
|
|
{
|
|
if (!searchModel.ContractDateFrom.TryToGeorgianDateTime(out var dateFrom))
|
|
throw new BadRequestException("تاریخ وارد شده نامعتبر است");
|
|
|
|
if (!searchModel.ContractDateTo.TryToGeorgianDateTime(out var dateTo))
|
|
{
|
|
throw new BadRequestException("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (dateFrom > dateTo)
|
|
{
|
|
throw new BadRequestException("تاریخ شروع نمیتواند بزرگ تر از تاریخ پایان باشد");
|
|
}
|
|
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.contract.ContractStartGr <= dateTo && x.contract.ContractEndGr <= dateFrom);
|
|
}
|
|
|
|
if (searchModel.HasSignature != null)
|
|
{
|
|
var hasSignature = searchModel.HasSignature == true ? "1" : "0";
|
|
joinedQuery = joinedQuery.Where(x => x.contract.Signature == hasSignature);
|
|
}
|
|
|
|
if (searchModel.IsActive != null)
|
|
{
|
|
var isActiveStr = searchModel.IsActive == true ? "true" : "false";
|
|
joinedQuery = joinedQuery.Where(x => x.contract.IsActiveString == isActiveStr);
|
|
}
|
|
|
|
if (searchModel.Type != null)
|
|
{
|
|
var typeStr = searchModel.Type switch
|
|
{
|
|
InstitutionContractType.JobRelation => "JobRelation",
|
|
InstitutionContractType.TaxAndFinancial => "taxAndFinancial",
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
};
|
|
joinedQuery = joinedQuery.Where(x => x.contract.TypeOfContract == typeStr);
|
|
}
|
|
|
|
if (searchModel.IsOfficial != null)
|
|
{
|
|
var isOfficialStr = searchModel.IsOfficial == true ? "Official" : "NotOfficial";
|
|
joinedQuery = joinedQuery.Where(x => x.contract.OfficialCompany == isOfficialStr);
|
|
}
|
|
|
|
|
|
if (searchModel.Status != null)
|
|
{
|
|
switch (searchModel.Status)
|
|
{
|
|
case InstitutionContractListStatus.DeactiveWithDebt:
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.StatusPriority == (int)InstitutionContractListStatus.DeactiveWithDebt);
|
|
break;
|
|
case InstitutionContractListStatus.PendingForRenewal:
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.StatusPriority == (int)InstitutionContractListStatus.PendingForRenewal);
|
|
break;
|
|
case InstitutionContractListStatus.Block:
|
|
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Block);
|
|
break;
|
|
case InstitutionContractListStatus.Free:
|
|
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Free);
|
|
break;
|
|
case InstitutionContractListStatus.WithoutWorkshop:
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.StatusPriority == (int)InstitutionContractListStatus.WithoutWorkshop);
|
|
break;
|
|
case InstitutionContractListStatus.Active:
|
|
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Active);
|
|
break;
|
|
case InstitutionContractListStatus.Deactive:
|
|
joinedQuery = joinedQuery.Where(x => x.contract.ContractEndGr < now);
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
joinedQuery = joinedQuery.Where(x => x.contract.IsActiveString != "blue");
|
|
}
|
|
|
|
#endregion
|
|
|
|
var totalAmount = await joinedQuery.SumAsync(x => x.contract.ContractAmount);
|
|
|
|
var totalDebt = await _context.FinancialStatments.Include(x => x.FinancialTransactionList)
|
|
.Where(x => joinedQuery.Select(i => i.contract.ContractingPartyId).Contains(x.ContractingPartyId))
|
|
.SelectMany(x => x.FinancialTransactionList)
|
|
.SumAsync(x => x.Deptor - x.Creditor);
|
|
var counts = new List<InstitutionContractStatusCount>();
|
|
foreach (var name in typeof(InstitutionContractListStatus).GetEnumNames())
|
|
{
|
|
var @enum = Enum.Parse<InstitutionContractListStatus>(name);
|
|
searchModel.Status = @enum;
|
|
var count = (await GetList(searchModel)).TotalCount;
|
|
counts.Add(new() { ListStatus = @enum, Count = count });
|
|
}
|
|
|
|
var res = new GetInstitutionContractListStatsViewModel()
|
|
{
|
|
TotalDebt = totalDebt,
|
|
TotalAmount = totalAmount,
|
|
Counts = counts
|
|
};
|
|
return res;
|
|
}
|
|
|
|
public async Task<List<RegistrationWorkflowMainListViewModel>> RegistrationWorkflowMainList()
|
|
{
|
|
return await _context.InstitutionContractSet.Where(x => x.Status == InstitutionContractStatus.Incomplete)
|
|
.Include(x => x.WorkshopDetails)
|
|
.Join(_context.PersonalContractingParties,
|
|
institutionContract => institutionContract.ContractingPartyId,
|
|
contractingParty => contractingParty.id,
|
|
(institutionContract, contractingParty) => new { institutionContract, contractingParty }).Select(x =>
|
|
new RegistrationWorkflowMainListViewModel
|
|
{
|
|
InstitutionContractId = x.institutionContract.id,
|
|
ContractingPartyFullName = $"{x.contractingParty.FName} {x.contractingParty.LName}",
|
|
Phone = x.contractingParty.Phone,
|
|
Amount = x.institutionContract.TotalAmount,
|
|
DoneWorkshops = x.institutionContract.WorkshopDetails.Count(w => w.WorkshopCreated),
|
|
TotalWorkshops = x.institutionContract.WorkshopDetails.Count(),
|
|
UnDoneWorkshops = x.institutionContract.WorkshopDetails.Count(w => !w.WorkshopCreated),
|
|
ContractingPartyId = x.contractingParty.id
|
|
}).ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// دریافت لیست اقلام گردش کار ثبت نام
|
|
/// </summary>
|
|
/// <param name="institutionContractId">شناسه قرارداد نهاد</param>
|
|
/// <returns>لیست اقلام گردش کار ثبت نام</returns>
|
|
public async Task<List<RegistrationWorkflowItemsViewModel>> RegistrationWorkflowItems(long institutionContractId)
|
|
{
|
|
// دریافت قرارداد نهاد همراه با جزئیات کارگاهها
|
|
var institutionContract = await _context.InstitutionContractSet
|
|
.Include(x => x.WorkshopDetails)
|
|
.FirstOrDefaultAsync(x => x.id == institutionContractId);
|
|
|
|
if (institutionContract == null)
|
|
throw new NotFoundException("قرارداد مؤسسه یافت نشد");
|
|
|
|
// استخراج شناسههای کارگاههایی که ایجاد شدهاند
|
|
var workshopIds = institutionContract.WorkshopDetails
|
|
.Where(x => x.WorkshopId != null)
|
|
.Select(x => x.WorkshopId.Value)
|
|
.ToList();
|
|
|
|
// دریافت کارگاهها همراه با کارفرمایان در یک کوئری
|
|
var workshops = await _context.Workshops
|
|
.Where(x => workshopIds.Contains(x.id))
|
|
.ToListAsync();
|
|
|
|
// استخراج تمامی شناسههای کارفرمایان از جزئیات کارگاهها
|
|
var allEmployerIds = institutionContract.WorkshopDetails
|
|
.SelectMany(x => x.Employers.Select(e => e.EmployerId))
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
// دریافت اطلاعات کارفرمایان در یک کوئری واحد
|
|
var employersDict = (await _context.Employers
|
|
.Where(e => allEmployerIds.Contains(e.id))
|
|
.Select(e => new { e.id, e.FullName })
|
|
.ToListAsync())
|
|
.ToDictionary(e => e.id, e => e);
|
|
|
|
// ساخت نتیجه نهایی با استفاده از دادههای از پیش بارگذاری شده
|
|
var items = institutionContract.WorkshopDetails.Select(workshopDetail =>
|
|
{
|
|
// پیدا کردن کارگاه مرتبط
|
|
var workshop = workshops.FirstOrDefault(w => w.id == workshopDetail.WorkshopId);
|
|
|
|
// ساخت لیست کارفرمایان این جزئیات کارگاه
|
|
var employers = workshopDetail.Employers
|
|
.Where(e => employersDict.ContainsKey(e.EmployerId))
|
|
.Select(e => new RegistrationWorkflowItemsEmployerViewModel
|
|
{
|
|
Id = e.EmployerId,
|
|
FullName = employersDict[e.EmployerId].FullName
|
|
})
|
|
.ToList();
|
|
|
|
return new RegistrationWorkflowItemsViewModel
|
|
{
|
|
Price = workshopDetail.Price,
|
|
IsDone = workshop != null,
|
|
PersonnelCount = workshopDetail.PersonnelCount,
|
|
WorkshopName = workshopDetail.WorkshopName,
|
|
Employers = employers,
|
|
WorkshopDetailsId = workshopDetail.id
|
|
};
|
|
}).ToList();
|
|
|
|
return items;
|
|
}
|
|
|
|
private (InstitutionContractListStatus status, bool isExpiered) SetContractStatus(InstitutionContract contract,
|
|
PersonalContractingParty contractingParty,
|
|
FinancialStatment financialStatment)
|
|
{
|
|
//if (contract.ContractEndGr <= endThisMontGr)
|
|
var now = DateTime.Now;
|
|
var nowFa = now.ToFarsi();
|
|
var endFa = nowFa.FindeEndOfMonth();
|
|
var endThisMontGr = endFa.ToGeorgianDateTime();
|
|
InstitutionContractListStatus listStatus = InstitutionContractListStatus.Active;
|
|
bool isExpier = false;
|
|
if (contract.ContractEndGr < now)
|
|
{
|
|
listStatus = InstitutionContractListStatus.Deactive;
|
|
isExpier = true;
|
|
}
|
|
|
|
if (contract.ContractEndGr >= now && contract.ContractEndGr <= endThisMontGr)
|
|
{
|
|
listStatus = InstitutionContractListStatus.PendingForRenewal;
|
|
isExpier = true;
|
|
}
|
|
|
|
if (contract.ContractAmount == 0)
|
|
{
|
|
listStatus = InstitutionContractListStatus.Free;
|
|
if ((contract.ContractEndGr >= now && contract.ContractEndGr <= endThisMontGr) ||
|
|
(contract.ContractEndGr < now))
|
|
{
|
|
isExpier = true;
|
|
}
|
|
}
|
|
|
|
if (contract.IsActiveString == "blue")
|
|
{
|
|
listStatus = InstitutionContractListStatus.DeactiveWithDebt;
|
|
isExpier = true;
|
|
}
|
|
|
|
var workshops = contractingParty.Employers
|
|
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop)).DistinctBy(w => w.id).ToList();
|
|
|
|
if (workshops.Count == 0)
|
|
{
|
|
listStatus = InstitutionContractListStatus.Free;
|
|
}
|
|
|
|
if (contractingParty.IsBlock == "true")
|
|
{
|
|
listStatus = InstitutionContractListStatus.Block;
|
|
}
|
|
|
|
return (listStatus, isExpier);
|
|
}
|
|
|
|
|
|
public async Task<InstitutionContractWorkshopDetail> GetInstitutionWorkshopDetails(
|
|
long institutionWorkshopDetailsId)
|
|
{
|
|
return await _context.InstitutionContractWorkshopDetails.FirstOrDefaultAsync(x =>
|
|
x.id == institutionWorkshopDetailsId);
|
|
}
|
|
|
|
public async Task<InstitutionContract> GetIncludeWorkshopDetailsAsync(long institutionContractId)
|
|
{
|
|
return await _context.InstitutionContractSet
|
|
.Include(x => x.WorkshopDetails)
|
|
.FirstOrDefaultAsync(x => x.id == institutionContractId);
|
|
}
|
|
|
|
public void UpdateStatusIfNeeded(long institutionContractId)
|
|
{
|
|
var institutionContract = _context.InstitutionContractSet
|
|
.Include(x => x.WorkshopDetails)
|
|
.FirstOrDefault(x => x.id == institutionContractId);
|
|
if (institutionContract == null)
|
|
throw new NotFoundException("قرارداد مؤسسه یافت نشد");
|
|
|
|
if (institutionContract.Status == InstitutionContractStatus.Completed)
|
|
return;
|
|
if (institutionContract.WorkshopDetails.All(x => x.WorkshopCreated))
|
|
institutionContract.Complete();
|
|
|
|
_context.SaveChanges();
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region CustomViewModels
|
|
|
|
public class WorkshopsAndEmployeeViewModel
|
|
{
|
|
public List<WorkshopViewModel> WorkshopViewModels { get; set; }
|
|
public string WorkshopCount { get; set; }
|
|
public string EmployeeCount { get; set; }
|
|
public int ArchiveCode { get; set; }
|
|
}
|
|
|
|
#endregion
|
|
} |