2659 lines
117 KiB
C#
2659 lines
117 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.Application.Enums;
|
|
using _0_Framework.Application.Sms;
|
|
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.InstitutionContractContactInfoAgg;
|
|
using Company.Domain.InstitutionContractExtensionTempAgg;
|
|
using Company.Domain.InstitutionPlanAgg;
|
|
using Company.Domain.WorkshopAgg;
|
|
using CompanyManagment.App.Contracts.Employer;
|
|
using CompanyManagment.App.Contracts.InstitutionContract;
|
|
using CompanyManagment.App.Contracts.InstitutionContractContactinfo;
|
|
using CompanyManagment.App.Contracts.TemporaryClientRegistration;
|
|
using CompanyManagment.App.Contracts.Workshop;
|
|
using CompanyManagment.App.Contracts.WorkshopPlan;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
using MongoDB.Driver;
|
|
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;
|
|
private readonly IMongoCollection<InstitutionContractExtensionTemp> _institutionExtensionTemp;
|
|
private readonly IPlanPercentageRepository _planPercentageRepository;
|
|
private readonly ISmsService _smsService;
|
|
|
|
public InstitutionContractRepository(CompanyContext context, IEmployerRepository employerRepository,
|
|
IWorkshopRepository workshopRepository, IMongoDatabase database,
|
|
IPlanPercentageRepository planPercentageRepository, ISmsService smsService) : base(context)
|
|
{
|
|
_context = context;
|
|
_employerRepository = employerRepository;
|
|
_workshopRepository = workshopRepository;
|
|
_planPercentageRepository = planPercentageRepository;
|
|
_smsService = smsService;
|
|
_institutionExtensionTemp =
|
|
database.GetCollection<InstitutionContractExtensionTemp>("InstitutionContractExtensionTemp");
|
|
}
|
|
|
|
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.WorkshopGroup)
|
|
.ThenInclude(x => x.InitialWorkshops)
|
|
.Include(x => x.WorkshopGroup)
|
|
.ThenInclude(x => x.CurrentWorkshops)
|
|
.Include(x => x.ContactInfoList).AsNoTracking();
|
|
|
|
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 })
|
|
.Select(x => new
|
|
{
|
|
x.contract,
|
|
x.contractingParty,
|
|
StatusPriority =
|
|
x.contract.VerificationStatus == InstitutionContractVerificationStatus.PendingForVerify
|
|
? (int)InstitutionContractListStatus.PendingForVerify
|
|
: 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.IsLegal == "حقیقی" ? x.contractingParty.SureName == null
|
|
? x.contractingParty.FName + " " + x.contractingParty.LName
|
|
: x.contractingParty.FName + " " + x.contractingParty.LName + " " + x.contractingParty.SureName
|
|
: x.contractingParty.SureName == null ? x.contractingParty.LName
|
|
: x.contractingParty.LName + " " + x.contractingParty.SureName).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 || x.contract.IsActiveString == "blue");
|
|
}
|
|
|
|
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;
|
|
|
|
case InstitutionContractListStatus.PendingForVerify:
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.contract.VerificationStatus == InstitutionContractVerificationStatus.PendingForVerify);
|
|
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 contractingPartyIds = list.Select(x => x.contractingParty.id).ToList();
|
|
|
|
var financialStatements = _context.FinancialStatments.Include(x => x.FinancialTransactionList)
|
|
.Where(x => contractingPartyIds.Contains(x.ContractingPartyId)).ToList();
|
|
var res = new PagedResult<GetInstitutionContractListItemsViewModel>()
|
|
{
|
|
TotalCount = await joinedQuery.CountAsync(),
|
|
List = list.Select(x =>
|
|
{
|
|
Console.WriteLine(x.contractingParty.id);
|
|
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());
|
|
List<InstitutionContractWorkshopBase> currentStateWorkshops = x.contract.WorkshopGroup?.CurrentWorkshops
|
|
.Cast<InstitutionContractWorkshopBase>().ToList();
|
|
|
|
var statement = financialStatements.FirstOrDefault(f => f.ContractingPartyId == x.contractingParty.id);
|
|
|
|
currentStateWorkshops?.AddRange(
|
|
x.contract.WorkshopGroup?.InitialWorkshops.Where(w => !w.WorkshopCreated) ?? []);
|
|
|
|
var workshopDetails = currentStateWorkshops?.Select(w =>
|
|
{
|
|
Workshop workshopSelected = null;
|
|
Console.WriteLine(w.WorkshopId);
|
|
if (w.WorkshopId != null && workshops.Select(ww => ww.id).Contains(w.WorkshopId.Value))
|
|
{
|
|
workshopSelected = workshops.First(ww => ww.id == w.WorkshopId.Value);
|
|
}
|
|
|
|
return new InstitutionContractListWorkshop()
|
|
{
|
|
EmployeeCount = w.PersonnelCount,
|
|
WorkshopName = workshopSelected?.WorkshopName ?? w.WorkshopName,
|
|
WorkshopServices = new WorkshopServicesViewModel()
|
|
{
|
|
Contract = w.Services.Contract,
|
|
ContractInPerson = w.Services.ContractInPerson,
|
|
CustomizeCheckout = w.Services.CustomizeCheckout,
|
|
Insurance = w.Services.Insurance,
|
|
InsuranceInPerson = w.Services.InsuranceInPerson,
|
|
RollCall = w.Services.RollCall,
|
|
RollCallInPerson = w.Services.RollCallInPerson
|
|
}
|
|
};
|
|
}).ToList() ?? [];
|
|
return new GetInstitutionContractListItemsViewModel()
|
|
{
|
|
ContractAmount = x.contract.ContractAmount,
|
|
Balance = statement?.FinancialTransactionList.Sum(ft => ft.Deptor - ft.Creditor) ?? 0,
|
|
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,
|
|
Workshops = workshopDetails
|
|
};
|
|
}).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 || x.contract.IsActiveString == "blue");
|
|
}
|
|
|
|
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;
|
|
|
|
case InstitutionContractListStatus.PendingForVerify:
|
|
joinedQuery = joinedQuery.Where(x =>
|
|
x.contract.VerificationStatus == InstitutionContractVerificationStatus.PendingForVerify);
|
|
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;
|
|
searchModel.PageSize = 1;
|
|
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.VerificationStatus == InstitutionContractVerificationStatus.PendingWorkflow)
|
|
.Include(x => x.WorkshopGroup)
|
|
.ThenInclude(x => x.InitialWorkshops)
|
|
.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.WorkshopGroup.InitialWorkshops.Count(w => w.WorkshopCreated),
|
|
TotalWorkshops =
|
|
x.institutionContract.WorkshopGroup.InitialWorkshops.Count,
|
|
UnDoneWorkshops =
|
|
x.institutionContract.WorkshopGroup.InitialWorkshops.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.WorkshopGroup).ThenInclude(institutionContractWorkshopGroup =>
|
|
institutionContractWorkshopGroup.InitialWorkshops).ThenInclude(institutionContractWorkshopDetail =>
|
|
institutionContractWorkshopDetail.Employers)
|
|
.FirstOrDefaultAsync(x => x.id == institutionContractId);
|
|
|
|
if (institutionContract == null)
|
|
throw new NotFoundException("قرارداد مؤسسه یافت نشد");
|
|
|
|
// استخراج شناسههای کارگاههایی که ایجاد شدهاند
|
|
var workshopIds = institutionContract.WorkshopGroup.InitialWorkshops
|
|
.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.WorkshopGroup.InitialWorkshops
|
|
.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.WorkshopGroup
|
|
.InitialWorkshops.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<InstitutionContractWorkshopInitial> GetInstitutionWorkshopInitialDetails(
|
|
long institutionWorkshopInitialId)
|
|
{
|
|
var result = await _context.InstitutionContractWorkshopInitials.Include(x => x.WorkshopGroup)
|
|
.Include(x => x.Employers)
|
|
.FirstOrDefaultAsync(x => x.id == institutionWorkshopInitialId);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<InstitutionContract> GetIncludeWorkshopDetailsAsync(long institutionContractId)
|
|
{
|
|
return await _context.InstitutionContractSet
|
|
.Include(x => x.WorkshopGroup)
|
|
.FirstOrDefaultAsync(x => x.id == institutionContractId);
|
|
}
|
|
|
|
public void UpdateStatusIfNeeded(long institutionContractId)
|
|
{
|
|
var institutionContract = _context.InstitutionContractSet
|
|
.Include(x => x.WorkshopGroup).ThenInclude(institutionContractWorkshopGroup =>
|
|
institutionContractWorkshopGroup.InitialWorkshops)
|
|
.FirstOrDefault(x => x.id == institutionContractId);
|
|
if (institutionContract == null)
|
|
throw new NotFoundException("قرارداد مؤسسه یافت نشد");
|
|
|
|
if (institutionContract.VerificationStatus == InstitutionContractVerificationStatus.Verified)
|
|
return;
|
|
if (institutionContract.WorkshopGroup.InitialWorkshops
|
|
.All(x => x.WorkshopCreated))
|
|
institutionContract.Verified();
|
|
|
|
_context.SaveChanges();
|
|
}
|
|
|
|
public async Task<GetInstitutionVerificationDetailsViewModel> GetVerificationDetails(Guid id)
|
|
{
|
|
var query = await _context.InstitutionContractSet.Where(x => x.PublicId == id)
|
|
.Include(x => x.WorkshopGroup).ThenInclude(institutionContractWorkshopGroup =>
|
|
institutionContractWorkshopGroup.InitialWorkshops)
|
|
.Include(institutionContract => institutionContract.Installments)
|
|
.Join(_context.PersonalContractingParties,
|
|
institutionContract => institutionContract.ContractingPartyId,
|
|
contractingParty => contractingParty.id,
|
|
(contract, party) => new { contract, party }).FirstOrDefaultAsync();
|
|
|
|
if (query == null)
|
|
{
|
|
throw new NotFoundException("قرارداد مؤسسه یافت نشد");
|
|
}
|
|
|
|
if (query.contract.VerificationStatus != InstitutionContractVerificationStatus.PendingForVerify)
|
|
{
|
|
throw new BadRequestException(
|
|
"قرارداد وارد شده تایید شده میباشد",
|
|
new Dictionary<string, object>
|
|
{
|
|
{ "isVerified", true }
|
|
}
|
|
);
|
|
}
|
|
|
|
int installmentNumber = 1;
|
|
|
|
|
|
var res = new GetInstitutionVerificationDetailsViewModel()
|
|
{
|
|
ContractStart = query.contract.ContractStartFa,
|
|
ContractEnd = query.contract.ContractEndFa,
|
|
ContractNo = query.contract.ContractNo,
|
|
CreationDate = query.contract.CreationDate.ToFarsi(),
|
|
TaxPrice = query.contract.ValueAddedTax.ToMoney(),
|
|
TotalPrice = (query.contract.TotalAmount - query.contract.ValueAddedTax).ToMoney(),
|
|
PaymentPrice = query.contract.TotalAmount.ToMoney(),
|
|
IsInstallment = query.contract.IsInstallment,
|
|
Installments = query.contract.Installments.OrderBy(x => x.InstallmentDateGr)
|
|
.Select(x =>
|
|
{
|
|
var res = new InstitutionContractInstallmentViewModel()
|
|
{
|
|
Amount = x.Amount.ToMoney(),
|
|
InstallmentIndex = GetInstallmentPersianNumber(installmentNumber),
|
|
Id = x.Id,
|
|
InstallmentDateFa = x.InstallmentDateFa,
|
|
InstallmentDateGr = x.InstallmentDateGr,
|
|
InstitutionContractId = x.InstitutionContractId,
|
|
};
|
|
installmentNumber++;
|
|
return res;
|
|
}).ToList(),
|
|
Workshops = query.contract.WorkshopGroup.InitialWorkshops
|
|
.Select(x => new GetInstitutionVerificationDetailsWorkshopsViewModel
|
|
{
|
|
Services = new WorkshopServicesViewModel()
|
|
{
|
|
Contract = x.Services.Contract,
|
|
ContractInPerson = x.Services.ContractInPerson,
|
|
CustomizeCheckout = x.Services.CustomizeCheckout,
|
|
Insurance = x.Services.Insurance,
|
|
InsuranceInPerson = x.Services.InsuranceInPerson,
|
|
RollCall = x.Services.RollCall,
|
|
RollCallInPerson = x.Services.RollCallInPerson
|
|
},
|
|
Name = x.WorkshopName,
|
|
PersonnelCount = x.PersonnelCount,
|
|
Price = x.Price.ToMoney()
|
|
}).ToList(),
|
|
SecondParty = new InstitutionContratVerificationParty()
|
|
{
|
|
Address = query.contract.Address,
|
|
PhoneNumber = query.party.Phone,
|
|
CeoName = query.party.IsLegal == "حقیقی"
|
|
? $"{query.party.FName} {query.party.LName}"
|
|
: $"{query.party.CeoFName} {query.party.CeoLName}",
|
|
CompanyNameOrFullName = query.party.IsLegal == "حقیقی"
|
|
? $"{query.party.FName} {query.party.LName}"
|
|
: query.party.LName,
|
|
NationalCodeOrNationalId =
|
|
query.party.IsLegal == "حقیقی" ? query.party.Nationalcode : query.party.NationalId,
|
|
LegalType = query.party.IsLegal == "حقیقی" ? LegalType.Real : LegalType.Legal,
|
|
},
|
|
FirstParty = new InstitutionContratVerificationParty()
|
|
{
|
|
Address = "رشت - خیابان حاجی آیاد - کوچه سپهدار - ساختمان داماش - واحد 17",
|
|
PhoneNumber = "09111111111",
|
|
CeoName = "سید حسن مصباح",
|
|
CompanyNameOrFullName = "نور داد مهر گستر کاسپین",
|
|
NationalCodeOrNationalId = "1111111111",
|
|
LegalType = LegalType.Legal
|
|
}
|
|
};
|
|
|
|
return res;
|
|
}
|
|
|
|
private string GetInstallmentPersianNumber(int number)
|
|
{
|
|
return number switch
|
|
{
|
|
1 => "اول",
|
|
2 => "دوم",
|
|
3 => "سوم",
|
|
4 => "چهارم",
|
|
5 => "پنجم",
|
|
6 => "ششم",
|
|
7 => "هفتم",
|
|
8 => "هشتم",
|
|
9 => "نهم",
|
|
10 => "دهم",
|
|
11 => "یازدهم",
|
|
12 => "دوازدهم",
|
|
_ => number.ToString()
|
|
};
|
|
}
|
|
|
|
public async Task<InstitutionContract> GetByPublicIdAsync(Guid id)
|
|
{
|
|
return await _context.InstitutionContractSet.Include(x=>x.ContactInfoList).FirstOrDefaultAsync(x => x.PublicId == id);
|
|
}
|
|
|
|
#region Extension
|
|
|
|
public async Task<InstitutionContractExtensionInquiryResult> GetExtensionInquiry(long previousContractId)
|
|
{
|
|
var institutionContracts = await _context.InstitutionContractSet
|
|
.Include(institutionContract => institutionContract.ContactInfoList)
|
|
.FirstOrDefaultAsync(x => x.id == previousContractId);
|
|
|
|
var contractingParty = await
|
|
_context.PersonalContractingParties.FirstOrDefaultAsync(x =>
|
|
x.id == institutionContracts.ContractingPartyId);
|
|
|
|
|
|
var legalType = contractingParty.IsLegal == "حقیقی" ? LegalType.Real : LegalType.Legal;
|
|
CreateInstitutionContractLegalPartyRequest legalPartyRequest = null;
|
|
CreateInstitutionContractRealPartyRequest realPartyRequest = null;
|
|
|
|
if (legalType == LegalType.Legal)
|
|
{
|
|
legalPartyRequest = new CreateInstitutionContractLegalPartyRequest()
|
|
{
|
|
NationalCode = contractingParty.Nationalcode,
|
|
BirthDateFa = contractingParty.DateOfBirth.ToFarsi(),
|
|
PhoneNumber = contractingParty.Phone,
|
|
FatherName = contractingParty.FatherName,
|
|
FName = contractingParty.CeoFName,
|
|
LName = contractingParty.CeoLName,
|
|
CompanyName = contractingParty.LName,
|
|
Gender = contractingParty.Gender,
|
|
IdNumber = contractingParty.IdNumber,
|
|
RegisterId = contractingParty.RegisterId,
|
|
IsAuth = contractingParty.IsAuthenticated,
|
|
NationalId = contractingParty.NationalId,
|
|
Position = contractingParty.LegalPosition,
|
|
ContractingPartyTempId = 0
|
|
};
|
|
}
|
|
else
|
|
{
|
|
realPartyRequest = new CreateInstitutionContractRealPartyRequest()
|
|
{
|
|
NationalCode = contractingParty.Nationalcode,
|
|
BirthDateFa = contractingParty.DateOfBirth.ToFarsi(),
|
|
PhoneNumber = contractingParty.Phone,
|
|
FatherName = contractingParty.FatherName,
|
|
FName = contractingParty.FName,
|
|
Gender = contractingParty.Gender,
|
|
IdNumber = contractingParty.IdNumber,
|
|
IsAuth = contractingParty.IsAuthenticated,
|
|
LName = contractingParty.LName,
|
|
ContractingPartyTempId = 0
|
|
};
|
|
}
|
|
|
|
var previousContractTemp = await _institutionExtensionTemp
|
|
.Find(x => x.PreviousId == previousContractId)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (previousContractTemp != null)
|
|
{
|
|
await _institutionExtensionTemp.DeleteOneAsync(x => x.Id == previousContractTemp.Id);
|
|
}
|
|
|
|
var institutionContractTemp = new InstitutionContractExtensionTemp(previousContractId);
|
|
|
|
await _institutionExtensionTemp.InsertOneAsync(institutionContractTemp);
|
|
|
|
|
|
var res = new InstitutionContractExtensionInquiryResult()
|
|
{
|
|
LegalType = legalType,
|
|
Address = contractingParty.Address,
|
|
City = contractingParty.City,
|
|
ContactInfoViewModels = institutionContracts.ContactInfoList.Select(x => new EditContactInfo()
|
|
{
|
|
Id = x.id,
|
|
FnameLname = x.FnameLname,
|
|
InstitutionContractId = x.InstitutionContractId,
|
|
PhoneNumber = x.PhoneNumber,
|
|
PhoneType = x.PhoneType,
|
|
Position = x.Position,
|
|
SendSms = x.SendSms
|
|
}).ToList(),
|
|
Province = contractingParty.State,
|
|
LegalParty = legalPartyRequest,
|
|
RealParty = realPartyRequest,
|
|
TemporaryId = institutionContractTemp.Id,
|
|
RepresentativeId = institutionContracts.RepresentativeId
|
|
};
|
|
return res;
|
|
}
|
|
|
|
public async Task<InstitutionContractExtensionWorkshopsResponse> GetExtensionWorkshops(
|
|
InstitutionContractExtensionWorkshopsRequest request)
|
|
{
|
|
var extenstionTemp = await _institutionExtensionTemp.Find(x => x.Id == request.TempId)
|
|
.FirstOrDefaultAsync();
|
|
if (extenstionTemp == null)
|
|
{
|
|
throw new BadRequestException("دیتای درخواست شده نامعتبر است");
|
|
}
|
|
|
|
var prevInstitutionContracts = await _context.InstitutionContractSet
|
|
.Include(x => x.WorkshopGroup)
|
|
.ThenInclude(institutionContractWorkshopGroup => institutionContractWorkshopGroup.CurrentWorkshops)
|
|
.FirstOrDefaultAsync(x => x.id == extenstionTemp.PreviousId);
|
|
if (prevInstitutionContracts == null)
|
|
{
|
|
throw new BadRequestException("قرارداد مالی قبلی یافت نشد");
|
|
}
|
|
|
|
extenstionTemp.SetContractingPartyInfos(request.Address, request.City, request.Province, request.ContactInfos);
|
|
await _institutionExtensionTemp.ReplaceOneAsync(
|
|
x => x.Id == extenstionTemp.Id,
|
|
extenstionTemp
|
|
);
|
|
|
|
var workshopIds = prevInstitutionContracts.WorkshopGroup.CurrentWorkshops.Select(x => x.WorkshopId.Value);
|
|
var workshops = await _context.Workshops.Where(x => workshopIds.Contains(x.id)).ToListAsync();
|
|
var workshopDetails = prevInstitutionContracts.WorkshopGroup.CurrentWorkshops
|
|
.Select(x =>
|
|
{
|
|
var workshop = workshops.FirstOrDefault(w => w.id == x.WorkshopId);
|
|
var service = x.Services;
|
|
var price = x.Price;
|
|
if (x.Price == 0)
|
|
{
|
|
var command = new WorkshopTempViewModel()
|
|
{
|
|
ContractAndCheckout = service.Contract,
|
|
ContractAndCheckoutInPerson = service.ContractInPerson,
|
|
CountPerson = x.PersonnelCount,
|
|
CustomizeCheckout = service.CustomizeCheckout,
|
|
Insurance = service.Insurance,
|
|
InsuranceInPerson = service.InsuranceInPerson,
|
|
RollCall = service.RollCall,
|
|
WorkshopName = x.WorkshopName,
|
|
RollCallInPerson = service.RollCallInPerson,
|
|
};
|
|
var institutionPlanForWorkshop = _planPercentageRepository.GetInstitutionPlanForWorkshop(command);
|
|
price = institutionPlanForWorkshop.OnlineAndInPersonSumAmountDouble;
|
|
}
|
|
|
|
return new WorkshopTempViewModel()
|
|
{
|
|
Id = x.id,
|
|
ContractAndCheckout = service.Contract,
|
|
ContractAndCheckoutInPerson = service.ContractInPerson,
|
|
CustomizeCheckout = service.CustomizeCheckout,
|
|
CountPerson = x.PersonnelCount,
|
|
Insurance = service.Insurance,
|
|
InsuranceInPerson = service.InsuranceInPerson,
|
|
RollCall = service.RollCall,
|
|
WorkshopName = workshop?.WorkshopName ?? "فاقد کارگاه",
|
|
WorkshopServicesAmount = price,
|
|
WorkshopServicesAmountStr = price.ToMoney(),
|
|
WorkshopId = workshop?.id ?? 0,
|
|
RollCallInPerson = service.RollCallInPerson
|
|
};
|
|
}).ToList();
|
|
var res = new InstitutionContractExtensionWorkshopsResponse()
|
|
{
|
|
TotalAmount = workshopDetails.Sum(x => x.WorkshopServicesAmount).ToMoney(),
|
|
WorkshopTemps = workshopDetails
|
|
};
|
|
return res;
|
|
}
|
|
|
|
public async Task<InstitutionContractExtensionPlanResponse> GetExtensionInstitutionPlan(
|
|
InstitutionContractExtensionPlanRequest request)
|
|
{
|
|
if (request.WorkshopTemps.Count == 0)
|
|
throw new BadRequestException("هیچ کارگاهی یافت نشد");
|
|
|
|
bool hasInPerson = request.WorkshopTemps.Any(x => x.ContractAndCheckoutInPerson);
|
|
double amount = request.TotalAmount.MoneyToDouble();
|
|
|
|
var institutionTemp = await _institutionExtensionTemp.Find(x => x.Id == request.TempId).FirstOrDefaultAsync();
|
|
|
|
var previousInstitution =
|
|
await _context.InstitutionContractSet.FirstOrDefaultAsync(x => x.id == institutionTemp.PreviousId);
|
|
|
|
var res = new InstitutionContractExtensionPlanResponse();
|
|
|
|
|
|
DateTime newContractStart;
|
|
if (previousInstitution.ContractEndGr <= DateTime.Now)
|
|
{
|
|
newContractStart = DateTime.Now;
|
|
}
|
|
else
|
|
{
|
|
newContractStart = previousInstitution.ContractEndGr.AddDays(1);
|
|
}
|
|
|
|
if (hasInPerson)
|
|
{
|
|
res.OneMonth = null;
|
|
|
|
res.ThreeMonths = null;
|
|
|
|
res.SixMonths = null;
|
|
|
|
res.TwelveMonths =
|
|
CalculateInstitutionPlan(InstitutionContractDuration.TwelveMonths, amount, true, newContractStart);
|
|
}
|
|
else
|
|
{
|
|
res.OneMonth =
|
|
CalculateInstitutionPlan(InstitutionContractDuration.OneMonth, amount, false, newContractStart);
|
|
|
|
res.ThreeMonths =
|
|
CalculateInstitutionPlan(InstitutionContractDuration.ThreeMonths, amount, false, newContractStart);
|
|
|
|
res.SixMonths =
|
|
CalculateInstitutionPlan(InstitutionContractDuration.SixMonths, amount, false, newContractStart);
|
|
|
|
res.TwelveMonths = CalculateInstitutionPlan(InstitutionContractDuration.TwelveMonths, amount, false,
|
|
newContractStart);
|
|
}
|
|
|
|
var workshops = request.WorkshopTemps
|
|
.Select(x => new InstitutionContractExtensionTempWorkshop(x.WorkshopName, x.CountPerson,
|
|
x.ContractAndCheckout, x.ContractAndCheckoutInPerson,
|
|
x.Insurance, x.InsuranceInPerson, x.RollCall, x.RollCall,
|
|
x.RollCallInPerson, x.WorkshopServicesAmount, x.WorkshopId)).ToList();
|
|
|
|
institutionTemp.SetWorkshopsAndPlanAmounts(workshops, res.OneMonth, res.ThreeMonths, res.SixMonths,
|
|
res.TwelveMonths, hasInPerson);
|
|
|
|
await _institutionExtensionTemp.ReplaceOneAsync(x => x.Id == institutionTemp.Id, institutionTemp);
|
|
|
|
return res;
|
|
}
|
|
|
|
public async Task<InstitutionContractExtensionPaymentResponse> GetExtensionPaymentMethod(
|
|
InstitutionContractExtensionPaymentRequest request)
|
|
{
|
|
var institutionTemp = await _institutionExtensionTemp.Find(x => x.Id == request.TempId).FirstOrDefaultAsync();
|
|
|
|
if (institutionTemp == null)
|
|
throw new BadRequestException("اطلاعات وارد شده نامعتبر است");
|
|
|
|
var duration = request.Duration;
|
|
|
|
|
|
var selectedPlan = duration switch
|
|
{
|
|
InstitutionContractDuration.OneMonth => institutionTemp.OneMonth,
|
|
InstitutionContractDuration.ThreeMonths => institutionTemp.ThreeMonths,
|
|
InstitutionContractDuration.SixMonths => institutionTemp.SixMonths,
|
|
InstitutionContractDuration.TwelveMonths => institutionTemp.TwelveMonths,
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
};
|
|
|
|
var baseAmount = selectedPlan.TotalPayment.MoneyToDouble();
|
|
var vatOrDiscount = baseAmount * 0.10;
|
|
|
|
var res = institutionTemp.HasContractInPerson
|
|
? CalculateInPersonPayment(selectedPlan, baseAmount, vatOrDiscount, request.Duration)
|
|
: CalculateOnlinePayment(baseAmount, vatOrDiscount);
|
|
|
|
institutionTemp.SetAmountAndDuration(duration, res.Monthly, res.OneTime);
|
|
|
|
await _institutionExtensionTemp.ReplaceOneAsync(x => x.Id == institutionTemp.Id, institutionTemp);
|
|
|
|
return res;
|
|
}
|
|
|
|
public async Task<OperationResult> ExtensionComplete(InstitutionContractExtensionCompleteRequest request)
|
|
{
|
|
var institutionContractTemp = await _institutionExtensionTemp.Find(x => x.Id == request.TemporaryId)
|
|
.FirstOrDefaultAsync();
|
|
if (institutionContractTemp == null)
|
|
throw new BadRequestException("اطلاعات وارد شده نامعتبر است");
|
|
var selectedDuration = institutionContractTemp.Duration switch
|
|
{
|
|
InstitutionContractDuration.OneMonth => institutionContractTemp.OneMonth,
|
|
InstitutionContractDuration.ThreeMonths => institutionContractTemp.ThreeMonths,
|
|
InstitutionContractDuration.SixMonths => institutionContractTemp.SixMonths,
|
|
InstitutionContractDuration.TwelveMonths => institutionContractTemp.TwelveMonths,
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
};
|
|
|
|
var previousInstitutionContract = await _context.InstitutionContractSet
|
|
.FirstOrDefaultAsync(x => x.id == institutionContractTemp.PreviousId);
|
|
|
|
if (previousInstitutionContract == null)
|
|
throw new NotFoundException("قرارداد مالی مورد نظر یافت نشد");
|
|
|
|
var contractingParty = await _context.PersonalContractingParties
|
|
.FirstOrDefaultAsync(x => x.id == previousInstitutionContract.ContractingPartyId);
|
|
|
|
var payment = request.IsInstallment
|
|
? institutionContractTemp.MonthlyPayment
|
|
: institutionContractTemp.OneTimePayment;
|
|
|
|
bool dateMessages = false;
|
|
string dateMaessageResult = String.Empty;
|
|
var opration = new OperationResult();
|
|
|
|
var extensionNo = previousInstitutionContract.ExtensionNo + 1;
|
|
|
|
if (_context.InstitutionContractSet.Any(x =>
|
|
x.ExtensionNo == extensionNo &&
|
|
x.ContractingPartyId == previousInstitutionContract.ContractingPartyId &&
|
|
x.TypeOfContract == previousInstitutionContract.TypeOfContract))
|
|
return opration.Failed("برای این قرارداد قبلا تمدید ایجاد شده است");
|
|
|
|
|
|
var contractStart = selectedDuration.ContractStart;
|
|
if (string.IsNullOrWhiteSpace(contractStart))
|
|
{
|
|
dateMaessageResult = "تاریخ قراراداد اجباری است. ";
|
|
dateMessages = true;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(contractStart))
|
|
{
|
|
dateMaessageResult += "تاریخ شروع قراراداد اجباری است. ";
|
|
dateMessages = true;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(selectedDuration.ContractEnd))
|
|
{
|
|
dateMaessageResult += "تاریخ پایان قراراداد اجباری است. ";
|
|
dateMessages = true;
|
|
}
|
|
|
|
if (dateMessages)
|
|
return opration.Failed(dateMaessageResult);
|
|
|
|
var firstContract = GetFirstContract(previousInstitutionContract.ContractingPartyId,
|
|
previousInstitutionContract.TypeOfContract);
|
|
|
|
var syear = firstContract.ContractStartFa.Substring(0, 4);
|
|
var smonth = firstContract.ContractStartFa.Substring(5, 2);
|
|
var sday = firstContract.ContractStartFa.Substring(8, 2);
|
|
//شماره قرارداد
|
|
var contractNo = $"{syear}{smonth}{sday}/{contractingParty.ArchiveCode}/{extensionNo}";
|
|
|
|
|
|
var contractStartGr = contractStart.ToGeorgianDateTime();
|
|
var contractEndGr = selectedDuration.ContractEnd.ToGeorgianDateTime();
|
|
|
|
var contractDateGr = contractStart.ToGeorgianDateTime();
|
|
|
|
if (Exists(x =>
|
|
((contractStartGr >= x.ContractStartGr && contractStartGr <= x.ContractEndGr) ||
|
|
(contractEndGr >= x.ContractStartGr && contractEndGr <= x.ContractEndGr)) &&
|
|
x.TypeOfContract == previousInstitutionContract.TypeOfContract
|
|
&& x.ContractingPartyId == previousInstitutionContract.ContractingPartyId))
|
|
return opration.Failed("تاریخ شروع و پایان وارد شده با قرارداد دیگری تداخل دارد");
|
|
|
|
if (institutionContractTemp.Address != null && institutionContractTemp.Province == null)
|
|
{
|
|
return opration.Failed("لطفا استان و شهر را انتخاب کنید");
|
|
}
|
|
|
|
if ((institutionContractTemp.Address != null && institutionContractTemp.Province != null) &&
|
|
institutionContractTemp.City == "شهرستان")
|
|
{
|
|
return opration.Failed("لطفا شهر را انتخاب کنید");
|
|
}
|
|
|
|
if (institutionContractTemp.Address == null && institutionContractTemp.Province != null)
|
|
{
|
|
return opration.Failed("لطفا آدرس را وارد کنید");
|
|
}
|
|
|
|
var hasValueAddedTax = payment.Tax.MoneyToDouble() > 0 ? "true" : "false";
|
|
|
|
var workshopsCount = institutionContractTemp.Workshops.Count.ToString();
|
|
|
|
var workshopDetails = institutionContractTemp
|
|
.Workshops.Select(x =>
|
|
{
|
|
var res = new InstitutionContractWorkshopInitial(x.WorkshopName, x.RollCall, x.RollCallInPerson,
|
|
x.CustomizeCheckout,
|
|
x.ContractAndCheckout,
|
|
x.ContractAndCheckoutInPerson, x.Insurance, x.InsuranceInPerson, x.CountPerson, x.Price);
|
|
if (x.WorkshopId != 0)
|
|
{
|
|
res.SetWorkshopId(x.WorkshopId);
|
|
}
|
|
return res;
|
|
}).ToList();
|
|
|
|
var employeeCount = institutionContractTemp.Workshops.Sum(x => x.CountPerson).ToString();
|
|
|
|
var oneMonthPayment = selectedDuration.OneMonthPaymentDiscounted.MoneyToDouble();
|
|
|
|
|
|
var contractingPartyFullName = previousInstitutionContract.ContractingPartyName;
|
|
|
|
var totalAmount = payment.PaymentAmount.MoneyToDouble();
|
|
|
|
var transaction = await _context.Database.BeginTransactionAsync();
|
|
|
|
var entity = new InstitutionContract(contractNo, previousInstitutionContract.RepresentativeId,
|
|
previousInstitutionContract.RepresentativeName,
|
|
previousInstitutionContract.ContractingPartyId,
|
|
contractingPartyFullName, contractDateGr, contractStart,
|
|
institutionContractTemp.Province, institutionContractTemp.City,
|
|
institutionContractTemp.Address, contractStartGr,
|
|
contractStart, contractEndGr, selectedDuration.ContractEnd,
|
|
oneMonthPayment, selectedDuration.DailyCompenseation.MoneyToDouble(),
|
|
selectedDuration.Obligation.MoneyToDouble(), totalAmount,
|
|
extensionNo, workshopsCount, employeeCount,
|
|
previousInstitutionContract.Description,
|
|
"NotOfficial", "JobRelation", hasValueAddedTax,
|
|
payment.Tax.MoneyToDouble(),[],
|
|
request.LawId);
|
|
|
|
await CreateAsync(entity);
|
|
await SaveChangesAsync();
|
|
foreach (var workshop in institutionContractTemp.Workshops)
|
|
{
|
|
var workshopDetail = new InstitutionContractWorkshopInitial(
|
|
workshop.WorkshopName, workshop.RollCall, workshop.RollCallInPerson,
|
|
workshop.CustomizeCheckout, workshop.ContractAndCheckout,
|
|
workshop.ContractAndCheckoutInPerson, workshop.Insurance,
|
|
workshop.InsuranceInPerson, workshop.CountPerson, workshop.Price);
|
|
|
|
workshopDetail.SetWorkshopGroup(entity.WorkshopGroup);
|
|
|
|
if (workshop.WorkshopId != 0)
|
|
{
|
|
workshopDetail.SetWorkshopId(workshop.WorkshopId);
|
|
}
|
|
// Set parent reference
|
|
|
|
// Add to the parent's collection
|
|
entity.WorkshopGroup.InitialWorkshops.Add(workshopDetail);
|
|
}
|
|
|
|
// Save the changes again
|
|
await SaveChangesAsync();
|
|
|
|
|
|
|
|
FinancialStatment financialStatement;
|
|
if (_context.FinancialStatments.Any(x => x.ContractingPartyId == contractingParty.id))
|
|
{
|
|
financialStatement =
|
|
await _context.FinancialStatments.FirstOrDefaultAsync(x => x.ContractingPartyId == contractingParty.id);
|
|
}
|
|
else
|
|
{
|
|
financialStatement = new FinancialStatment(contractingParty.id, contractingPartyFullName);
|
|
await _context.AddAsync(financialStatement);
|
|
}
|
|
|
|
var today = DateTime.Today;
|
|
if (request.IsInstallment && payment is InstitutionContractPaymentMonthlyViewModel monthly)
|
|
{
|
|
var installments = monthly.Installments.Select(x =>
|
|
new InstitutionContractInstallment(x.InstalmentDate.ToGeorgianDateTime(),
|
|
x.InstallmentAmountStr.MoneyToDouble(), "")).ToList();
|
|
|
|
// دریافت مبلغ اولین قسط
|
|
//این کار برای این هست که اولین قسط باید با تاریخ امروز باشد و باید به وضعیت مالی بدهی ایجاد شود که یوزر اولین بدهی را وارد کند
|
|
var firstInstallmentAmount = installments.First().Amount;
|
|
|
|
// حذف اولین قسط
|
|
installments.RemoveAt(0);
|
|
|
|
// ایجاد قسط جدید با تاریخ امروز
|
|
var todayInstallment = new InstitutionContractInstallment(DateTime.Today, firstInstallmentAmount, "");
|
|
|
|
var financialTransaction = new FinancialTransaction(0, today, today.ToFarsi(),
|
|
"قسط اول سرویس", "debt", "بابت خدمات", firstInstallmentAmount, 0, 0);
|
|
|
|
financialStatement.AddFinancialTransaction(financialTransaction);
|
|
|
|
// اضافه کردن قسط جدید به ابتدای لیست
|
|
installments.Insert(0, todayInstallment);
|
|
|
|
entity.SetInstallments(installments);
|
|
}
|
|
else
|
|
{
|
|
var financialTransaction = new FinancialTransaction(0, today, today.ToFarsi(),
|
|
"پرداخت کل سرویس", "debt", "بابت خدمات", totalAmount, 0, 0);
|
|
financialStatement.AddFinancialTransaction(financialTransaction);
|
|
}
|
|
|
|
foreach (var contactInfo in institutionContractTemp.ContactInfos)
|
|
{
|
|
if (contactInfo.PhoneNumber != null)
|
|
{
|
|
var contactinfo = new InstitutionContractContactInfo(contactInfo.PhoneType,
|
|
contactInfo.Position, contactInfo.PhoneNumber, contactInfo.FnameLname, entity.id,
|
|
contactInfo.SendSmsString == "true");
|
|
|
|
_context.InstitutionContractContactInfos.Add(contactinfo);
|
|
}
|
|
}
|
|
|
|
await SaveChangesAsync();
|
|
|
|
await _smsService.SendInstitutionVerificationLink(contractingParty.Phone, contractingPartyFullName,
|
|
entity.PublicId);
|
|
|
|
|
|
await SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
return opration.Succcedded();
|
|
}
|
|
|
|
#endregion
|
|
|
|
public async Task<InstitutionContractAmendmentWorkshopsResponse> GetAmendmentWorkshops(long institutionContractId)
|
|
{
|
|
var institutionContract = await _context.InstitutionContractSet
|
|
.Include(x => x.WorkshopGroup)
|
|
.ThenInclude(x => x.CurrentWorkshops)
|
|
.FirstOrDefaultAsync(x => x.id == institutionContractId);
|
|
|
|
var workshops = institutionContract.WorkshopGroup.CurrentWorkshops.Select(x => new WorkshopTempViewModel()
|
|
{
|
|
Id = x.id,
|
|
ContractAndCheckout = x.Services.Contract,
|
|
ContractAndCheckoutInPerson = x.Services.ContractInPerson,
|
|
CustomizeCheckout = x.Services.CustomizeCheckout,
|
|
CountPerson = x.PersonnelCount,
|
|
Insurance = x.Services.Insurance,
|
|
InsuranceInPerson = x.Services.InsuranceInPerson,
|
|
RollCall = x.Services.RollCall,
|
|
WorkshopName = x.WorkshopName,
|
|
WorkshopServicesAmountStr = x.Price.ToMoney(),
|
|
WorkshopServicesAmount = x.Price,
|
|
WorkshopId = x.WorkshopId ?? 0,
|
|
RollCallInPerson = x.Services.RollCallInPerson,
|
|
|
|
}).ToList();
|
|
var res = new InstitutionContractAmendmentWorkshopsResponse()
|
|
{
|
|
Workshops = workshops
|
|
};
|
|
return res;
|
|
}
|
|
|
|
public async Task<InsitutionContractAmendmentPaymentResponse> GetAmendmentPaymentDetails(InsitutionContractAmendmentPaymentRequest request)
|
|
{
|
|
var institutionContract =await _context.InstitutionContractSet
|
|
.Include(x => x.WorkshopGroup)
|
|
.ThenInclude(x => x.CurrentWorkshops)
|
|
.FirstOrDefaultAsync(x => x.id == request.InstitutionContractId);
|
|
if (institutionContract == null)
|
|
throw new NotFoundException("قرارداد مالی یافت نشد");
|
|
|
|
var amendmentStart = DateTime.Now;
|
|
var amendmentEnd = institutionContract.ContractEndGr;
|
|
//TODO : محاسبه مبلغ بر اساس کارگاه های انتخاب شده
|
|
var res = new InsitutionContractAmendmentPaymentResponse();
|
|
return res;
|
|
|
|
}
|
|
public async Task<List<InstitutionContractSelectListViewModel>> GetInstitutionContractSelectList(string search,
|
|
string selected)
|
|
{
|
|
var contractingParties = _context.PersonalContractingParties.Select(x => new InstitutionContractSelectListViewModel()
|
|
{
|
|
Id = x.id,
|
|
Text = x.IsLegal == "حقیقی" ? x.SureName == null
|
|
? x.FName + " " + x.LName
|
|
: x.FName + " " + x.LName + " " + x.SureName
|
|
: x.SureName == null ? x.LName
|
|
: x.LName + " " + x.SureName
|
|
});
|
|
|
|
var workshops = _context.Workshops.Select(x => new InstitutionContractSelectListViewModel()
|
|
{
|
|
Id = x.id,
|
|
Text = x.WorkshopFullName
|
|
});
|
|
var employers = _context.Employers.Select(x => new InstitutionContractSelectListViewModel()
|
|
{
|
|
Id = x.id,
|
|
Text = x.FName + " " + x.LName
|
|
});
|
|
var representatives = _context.RepresentativeSet.Select(x => new InstitutionContractSelectListViewModel()
|
|
{
|
|
Id = x.id,
|
|
Text = x.FName + " " + x.LName
|
|
});
|
|
|
|
var res = contractingParties
|
|
.Union(workshops)
|
|
.Union(employers)
|
|
.Union(representatives);
|
|
|
|
InstitutionContractSelectListViewModel idSelected = null;
|
|
|
|
if (!string.IsNullOrWhiteSpace(selected))
|
|
{
|
|
idSelected = await res.FirstOrDefaultAsync(x => x.Text == selected);
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
res = res.Where(x => x.Text.Contains(search));
|
|
}
|
|
|
|
var list = await res.Take(100).ToListAsync();
|
|
|
|
if (idSelected != null)
|
|
list.Add(idSelected);
|
|
|
|
return list.DistinctBy(x => x.Id).ToList();
|
|
|
|
}
|
|
|
|
public async Task<(double amount,long contractingPartyId,long institutionContractId)> GetFirstPaymentAmount(Guid id)
|
|
{
|
|
var institutionContract = await _context.InstitutionContractSet
|
|
.Include(institutionContract => institutionContract.Installments)
|
|
.FirstOrDefaultAsync(x => x.PublicId == id);
|
|
|
|
long contractingPartyId = institutionContract.ContractingPartyId;
|
|
long institutionContractId = institutionContract.id;
|
|
if (institutionContract == null)
|
|
throw new NotFoundException("قرارداد مالی یافت نشد");
|
|
|
|
if (!institutionContract.IsInstallment)
|
|
return (institutionContract.TotalAmount,contractingPartyId,institutionContractId);
|
|
|
|
var institutionContractInstallment = institutionContract.Installments.FirstOrDefault();
|
|
if (institutionContractInstallment == null)
|
|
throw new NotFoundException("قسطی برای این قرارداد یافت نشد");
|
|
return (institutionContract.TotalAmount,contractingPartyId,institutionContractId);
|
|
}
|
|
|
|
public InstitutionContract GetIncludeContacts(long institutionContractId)
|
|
{
|
|
var institutionContract = _context.InstitutionContractSet
|
|
.Include(x => x.ContactInfoList)
|
|
.FirstOrDefault(x => x.id == institutionContractId);
|
|
if (institutionContract == null)
|
|
throw new NotFoundException("قرارداد مالی یافت نشد");
|
|
return institutionContract;
|
|
}
|
|
|
|
|
|
private InstitutionContractExtensionPaymentResponse CalculateInPersonPayment(
|
|
InstitutionContractExtensionPlanDetail selectedPlan, double baseAmount, double tenPercent,
|
|
InstitutionContractDuration duration)
|
|
{
|
|
// حالت پرداخت یکجا
|
|
var oneTimeBase = baseAmount - tenPercent;
|
|
var oneTimeTax = oneTimeBase * 0.10;
|
|
var oneTimeTotal = oneTimeBase + oneTimeTax;
|
|
|
|
var res = new InstitutionContractExtensionPaymentResponse
|
|
{
|
|
OneTime = new()
|
|
{
|
|
TotalAmount = selectedPlan.TotalPayment,
|
|
Tax = oneTimeTax.ToMoney(),
|
|
PaymentAmount = oneTimeTotal.ToMoney()
|
|
}
|
|
};
|
|
|
|
// حالت پرداخت اقساطی
|
|
var monthlyTax = baseAmount * 0.10;
|
|
var monthlyTotal = baseAmount + monthlyTax;
|
|
var installments = InstitutionMonthlyInstallmentCaculation(duration, monthlyTotal, selectedPlan.ContractStart);
|
|
|
|
res.Monthly = new()
|
|
{
|
|
TotalAmount = baseAmount.ToMoney(),
|
|
Tax = monthlyTax.ToMoney(),
|
|
PaymentAmount = monthlyTotal.ToMoney(),
|
|
Installments = installments
|
|
};
|
|
|
|
return res;
|
|
}
|
|
|
|
private InstitutionContractExtensionPaymentResponse CalculateOnlinePayment(double baseAmount, double tenPercent)
|
|
{
|
|
var tax = tenPercent;
|
|
var total = baseAmount + tax;
|
|
|
|
return new InstitutionContractExtensionPaymentResponse
|
|
{
|
|
OneTime = new()
|
|
{
|
|
TotalAmount = baseAmount.ToMoney(),
|
|
Tax = tax.ToMoney(),
|
|
PaymentAmount = total.ToMoney()
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
private InstitutionContractExtensionPlanDetail CalculateInstitutionPlan(InstitutionContractDuration duration,
|
|
double amount,
|
|
bool hasInPersonContract, DateTime contractStart)
|
|
{
|
|
var result = new InstitutionContractExtensionPlanDetail();
|
|
var months = (int)duration;
|
|
//بدست آوردن جمع کل مبالغ کارگاه بر اساس مدت قراداد
|
|
var totalPrice = months * amount;
|
|
|
|
var contractStartFa = contractStart.ToFarsi();
|
|
|
|
var findeEnd = Tools.FindEndOfContract(contractStartFa, ((int)duration).ToString());
|
|
|
|
var contractEndDate = findeEnd.endDateGr;
|
|
|
|
result.ContractEnd = contractEndDate.ToFarsi();
|
|
result.ContractStart = contractStartFa;
|
|
|
|
result.Obligation = totalPrice.ToMoney();
|
|
result.DailyCompenseation = ((amount * 10) / 100).ToMoney();
|
|
|
|
|
|
if (hasInPersonContract)
|
|
{
|
|
result.OneMonthDiscount = "0";
|
|
result.OneMonthOriginalPayment = amount.ToMoney();
|
|
result.OneMonthPaymentDiscounted = amount.ToMoney();
|
|
result.TotalPayment = totalPrice.ToMoney();
|
|
}
|
|
else
|
|
{
|
|
var discount = duration switch
|
|
{
|
|
InstitutionContractDuration.OneMonth => 0,
|
|
InstitutionContractDuration.ThreeMonths => 5,
|
|
InstitutionContractDuration.SixMonths => 10,
|
|
InstitutionContractDuration.TwelveMonths => 15,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(duration), duration, null)
|
|
};
|
|
var oneMonthDiscountAmount = (amount * discount) / 100;
|
|
var oneMonthDiscountedPayment = amount - oneMonthDiscountAmount;
|
|
var totalDiscount = oneMonthDiscountAmount * months;
|
|
var discountedPayment = totalPrice - totalDiscount;
|
|
result.OneMonthDiscount = oneMonthDiscountAmount.ToMoney();
|
|
result.OneMonthPaymentDiscounted = oneMonthDiscountedPayment.ToMoney();
|
|
result.OneMonthOriginalPayment = totalPrice.ToMoney();
|
|
result.TotalPayment = discountedPayment.ToMoney();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static List<MonthlyInstallment> InstitutionMonthlyInstallmentCaculation(InstitutionContractDuration duration,
|
|
double monthlyTotalPaymentDouble,
|
|
string installmentstart)
|
|
{
|
|
var originalDay = int.Parse(installmentstart.Substring(8, 2));
|
|
var installmentList = new List<MonthlyInstallment>();
|
|
|
|
|
|
int instalmentCount = (int)duration;
|
|
var instalmentAmount = monthlyTotalPaymentDouble / instalmentCount;
|
|
int currentInstallmentStartDay = int.Parse(installmentstart.Substring(8, 2));
|
|
bool endOfMonth = currentInstallmentStartDay == 31;
|
|
|
|
// Loop through each installment period
|
|
for (int i = 1; i <= instalmentCount; i++)
|
|
{
|
|
string installmentDate;
|
|
// For first installment, use the initial date
|
|
if (i == 1)
|
|
{
|
|
installmentDate = installmentstart;
|
|
}
|
|
else
|
|
{
|
|
var currentDay = int.Parse(installmentstart.Substring(8, 2));
|
|
var currentMonth = int.Parse(installmentstart.Substring(5, 2));
|
|
var currentYear = int.Parse(installmentstart.Substring(0, 4));
|
|
|
|
// Get next month's date
|
|
var nextMonthFa = installmentstart.ToGeorgianDateTime().AddMonthsFa(1, out var nextMonth);
|
|
var maxDayInNextMonth = int.Parse(nextMonthFa.FindeEndOfMonth().Substring(8, 2));
|
|
|
|
// Use original day if possible, otherwise use last day of month
|
|
var dayToUse = Math.Min(originalDay, maxDayInNextMonth);
|
|
installmentDate = nextMonthFa.Substring(0, 8) + dayToUse.ToString("D2");
|
|
|
|
// Update installmentstart for next iteration
|
|
installmentstart = installmentDate;
|
|
}
|
|
|
|
installmentList.Add(new MonthlyInstallment()
|
|
{
|
|
InstallmentAmountStr = instalmentAmount.ToMoney(),
|
|
InstallmentCounter = i switch
|
|
{
|
|
1 => "اول",
|
|
2 => "دوم",
|
|
3 => "سوم",
|
|
4 => "چهارم",
|
|
5 => "پنجم",
|
|
6 => "ششم",
|
|
7 => "هفتم",
|
|
8 => "هشتم",
|
|
9 => "نهم",
|
|
10 => "دهم",
|
|
11 => "یازدهم",
|
|
12 => "دوازدهم",
|
|
_ => "دوازدهم",
|
|
},
|
|
InstalmentDate = installmentDate
|
|
});
|
|
}
|
|
|
|
return installmentList;
|
|
}
|
|
|
|
#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
|
|
} |