CreateTransaction completed
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
|
||||
using _0_Framework.Application;
|
||||
using Company.Domain.ContarctingPartyAgg;
|
||||
using Company.Domain.InstitutionContractAgg;
|
||||
using Hangfire;
|
||||
@@ -10,7 +11,7 @@ public class JobSchedulerRegistrator
|
||||
private readonly IBackgroundJobClient _backgroundJobClient;
|
||||
private readonly SmsReminder _smsReminder;
|
||||
private readonly IInstitutionContractRepository _institutionContractRepository;
|
||||
private static DateTime? _lastRunDebtReminder;
|
||||
private static DateTime? _lastRunCreateTransaction;
|
||||
|
||||
|
||||
public JobSchedulerRegistrator(SmsReminder smsReminder, IBackgroundJobClient backgroundJobClient, IInstitutionContractRepository institutionContractRepository)
|
||||
@@ -24,15 +25,57 @@ public class JobSchedulerRegistrator
|
||||
{
|
||||
|
||||
|
||||
//RecurringJob.AddOrUpdate(
|
||||
// "InstitutionContract.ReminderDebtSMS",
|
||||
// () => _institutionContractRepository.SendReminderSmsForBackgroundTask(),
|
||||
// "*/1 * * * *" // هر 1 دقیقه یکبار چک کن
|
||||
//);
|
||||
|
||||
RecurringJob.AddOrUpdate(
|
||||
"InstitutionContract.ReminderDebtSMS",
|
||||
() => _institutionContractRepository.SendReminderSmsForBackgroundTask(),
|
||||
"*/1 * * * *" // هر 1 دقیقه یکبار چک کن
|
||||
"InstitutionContract.CreateTransaction",
|
||||
() => CreateFinancialTransaction(),
|
||||
"*/1 * * * *" // هر 30 دقیقه یکبار چک کن
|
||||
);
|
||||
}
|
||||
|
||||
public void CreateFinancialTransaction()
|
||||
{
|
||||
var now = new DateTime(2025,11,21, 2, 0, 0);
|
||||
var endOfMonth = now.ToFarsi().FindeEndOfMonth();
|
||||
var endOfMonthGr = endOfMonth.ToGeorgianDateTime();
|
||||
|
||||
if (now.Date == endOfMonthGr.Date && now.Hour >= 2 && now.Hour < 4 &&
|
||||
now.Date != _lastRunCreateTransaction?.Date)
|
||||
{
|
||||
|
||||
var month = endOfMonth.Substring(5, 2);
|
||||
var year = endOfMonth.Substring(0, 4);
|
||||
var monthName = month.ToFarsiMonthByNumber();
|
||||
var description = $"{monthName} {year}";
|
||||
|
||||
|
||||
|
||||
var endnew = ($"{endOfMonth.Substring(0, 8)}01").FindeEndOfMonth();
|
||||
|
||||
var endNewGr = endnew.ToGeorgianDateTime();
|
||||
var endNewFa = endNewGr.ToFarsi();
|
||||
|
||||
try
|
||||
{
|
||||
_backgroundJobClient.Enqueue(() => _institutionContractRepository.CreateTransaction(endNewGr, endNewFa, description));
|
||||
_lastRunCreateTransaction = now;
|
||||
Console.WriteLine("CreateTransAction executed");
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//_smsService.Alarm("09114221321", "خطا-ایجاد سند مالی");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -26,6 +26,35 @@ public class FinancialTransaction : EntityBase
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ایجاد از طرف بک گراند سرویس
|
||||
/// </summary>
|
||||
/// <param name="financialStatementId"></param>
|
||||
/// <param name="tdateGr"></param>
|
||||
/// <param name="tdateFa"></param>
|
||||
/// <param name="description"></param>
|
||||
/// <param name="typeOfTransaction"></param>
|
||||
/// <param name="descriptionOption"></param>
|
||||
/// <param name="deptor"></param>
|
||||
/// <param name="creditor"></param>
|
||||
/// <param name="balance"></param>
|
||||
/// <param name="sentSms"></param>
|
||||
public FinancialTransaction(long financialStatementId, DateTime tdateGr, string tdateFa, string description,
|
||||
string typeOfTransaction, string descriptionOption, double deptor, double creditor, double balance,
|
||||
bool sentSms)
|
||||
{
|
||||
FinancialStatementId = financialStatementId;
|
||||
TdateGr = tdateGr;
|
||||
TdateFa = tdateFa;
|
||||
Description = description;
|
||||
TypeOfTransaction = typeOfTransaction;
|
||||
DescriptionOption = descriptionOption;
|
||||
Deptor = deptor;
|
||||
Creditor = creditor;
|
||||
Balance = balance;
|
||||
SentSms = sentSms;
|
||||
}
|
||||
|
||||
public long FinancialStatementId { get; private set; }
|
||||
public DateTime TdateGr { get; private set; }
|
||||
public string TdateFa { get; private set; }
|
||||
|
||||
@@ -101,4 +101,20 @@ public interface IInstitutionContractRepository : IRepository<long, InstitutionC
|
||||
Task SendReminderSmsToContractingParties(List<SmsListData> smsListData);
|
||||
|
||||
#endregion
|
||||
|
||||
#region CreateMontlyTransaction
|
||||
|
||||
/// <summary>
|
||||
/// ایجاد سند مالی برای قرارداد های قدیمی بدون قسط
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<bool> CreateTransactionOldInstitutionContracts(DateTime endOfMonthGr, string endOfMonthFa, string description);
|
||||
|
||||
/// <summary>
|
||||
/// ایجاد سند مالی از طرف بک گراند تسک
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task CreateTransaction(DateTime endOfMonthGr, string endOfMonthFa, string description);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -28,6 +28,12 @@ public class InstitutionContractInstallmentViewModel
|
||||
/// </summary>
|
||||
public string Amount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// مبلغ قسط
|
||||
/// Double
|
||||
/// </summary>
|
||||
public double AmountDouble { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// عدد قسط فارسی
|
||||
/// </summary>
|
||||
|
||||
@@ -71,4 +71,9 @@ public class InstitutionContractViewModel
|
||||
public int LeftWorkEmployeeCount { get; set; }
|
||||
public int InsuranceLeftWorkEmployeeCount { get; set; }
|
||||
public string IsExpier { get; set; }
|
||||
|
||||
public bool IsInstallment { get; set; }
|
||||
public string PublicId { get; set; }
|
||||
|
||||
public List<InstitutionContractInstallmentViewModel> InstallmentList { get; set; }
|
||||
}
|
||||
@@ -57,6 +57,8 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
private readonly IPlanPercentageRepository _planPercentageRepository;
|
||||
private readonly ISmsService _smsService;
|
||||
private readonly ISmsResultRepository _smsResultRepository;
|
||||
private readonly IFinancialTransactionRepository _financialTransactionRepository;
|
||||
private readonly IFinancialStatmentRepository _financialStatmentRepository;
|
||||
|
||||
private readonly InstitutionContratVerificationParty _firstParty = new()
|
||||
{
|
||||
@@ -70,7 +72,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
|
||||
public InstitutionContractRepository(CompanyContext context, IEmployerRepository employerRepository,
|
||||
IWorkshopRepository workshopRepository, IMongoDatabase database,
|
||||
IPlanPercentageRepository planPercentageRepository, ISmsService smsService, ISmsResultRepository smsResultRepository) : base(context)
|
||||
IPlanPercentageRepository planPercentageRepository, ISmsService smsService, ISmsResultRepository smsResultRepository, IFinancialTransactionRepository financialTransactionRepository, IFinancialStatmentRepository financialStatmentRepository) : base(context)
|
||||
{
|
||||
_context = context;
|
||||
_employerRepository = employerRepository;
|
||||
@@ -78,6 +80,8 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
_planPercentageRepository = planPercentageRepository;
|
||||
_smsService = smsService;
|
||||
_smsResultRepository = smsResultRepository;
|
||||
_financialTransactionRepository = financialTransactionRepository;
|
||||
_financialStatmentRepository = financialStatmentRepository;
|
||||
_institutionExtensionTemp =
|
||||
database.GetCollection<InstitutionContractExtensionTemp>("InstitutionContractExtensionTemp");
|
||||
_institutionAmendmentTemp =
|
||||
@@ -2950,6 +2954,8 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
|
||||
#endregion
|
||||
|
||||
#region DebtReminderSms
|
||||
|
||||
public async Task<bool> SendReminderSmsForBackgroundTask()
|
||||
{
|
||||
|
||||
@@ -3002,7 +3008,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
await SendReminderSmsToContractingParties(smsListData);
|
||||
|
||||
|
||||
Console.WriteLine("executed at : " + persianNow + " - " + hour +":"+minute);
|
||||
Console.WriteLine("executed at : " + persianNow + " - " + hour + ":" + minute);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3359,6 +3365,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
}
|
||||
|
||||
#region PrivateMetods
|
||||
|
||||
/// <summary>
|
||||
/// دریافت صورت حساب مالی با آی دی طرف حساب
|
||||
/// </summary>
|
||||
@@ -3401,6 +3408,234 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region CreateMontlyTransaction
|
||||
|
||||
/// <summary>
|
||||
/// ایجاد سند مالی برای قرارداد های قدیمی بدون قسط
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> CreateTransactionOldInstitutionContracts(DateTime endOfMonthGr, string endOfMonthFa, string description)
|
||||
{
|
||||
|
||||
#region FindeNextMonth 1th
|
||||
|
||||
var firstDayOfMonthGr = ($"{endOfMonthFa.Substring(0, 8)}01").ToGeorgianDateTime();
|
||||
var nextMonthGr = endOfMonthGr.AddDays(1);
|
||||
var endOfCurrentMonth = (($"{endOfMonthFa.Substring(0, 8)}/01").FindeEndOfMonth()).ToGeorgianDateTime();
|
||||
#endregion
|
||||
|
||||
|
||||
#region GetAvtiveContracts
|
||||
var institutionContracts = await _context.InstitutionContractSet.Where(x => x.IsActiveString == "true")
|
||||
.Include(x => x.Installments)
|
||||
.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,
|
||||
ContractAmountDouble = x.ContractAmount,
|
||||
TypeOfContract = x.TypeOfContract,
|
||||
IsInstallment = x.IsInstallment,
|
||||
PublicId = x.PublicId.ToString(),
|
||||
InstallmentList = x.Installments.Select(ins => new InstitutionContractInstallmentViewModel { AmountDouble = ins.Amount, InstallmentDateGr = ins.InstallmentDateGr }).OrderBy(ins => ins.InstallmentDateGr).Skip(1).ToList(),
|
||||
|
||||
}).Where(x =>
|
||||
x.ContractStartGr < endOfMonthGr && x.ContractEndGr >= endOfMonthGr && x.ContractAmountDouble > 0)
|
||||
.ToListAsync();
|
||||
#endregion
|
||||
|
||||
#region GetFutureContracts
|
||||
|
||||
List<InstitutionContractViewModel> futureContracts = await _context.InstitutionContractSet
|
||||
.Where(x => x.IsActiveString == "true" &&
|
||||
x.ContractStartGr.Date == nextMonthGr.Date && x.ContractAmount > 0)
|
||||
.Select(x => new InstitutionContractViewModel
|
||||
{
|
||||
Id = x.id,
|
||||
ContractingPartyId = x.ContractingPartyId,
|
||||
|
||||
}).ToListAsync();
|
||||
|
||||
|
||||
#region GetDectivedContractOnCurrentMonth
|
||||
|
||||
if (futureContracts.Any())
|
||||
{
|
||||
List<long> futureContractIds = futureContracts.Select(x => x.ContractingPartyId).ToList();
|
||||
List<InstitutionContractViewModel> deatcivedContract = await _context.InstitutionContractSet
|
||||
.Where(x => x.IsActiveString == "false" && futureContractIds.Contains(x.ContractingPartyId) &&
|
||||
x.ContractEndGr.Date == endOfCurrentMonth.Date && 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,
|
||||
ContractAmountDouble = x.ContractAmount,
|
||||
TypeOfContract = x.TypeOfContract,
|
||||
IsInstallment = x.IsInstallment,
|
||||
PublicId = x.PublicId.ToString(),
|
||||
InstallmentList = x.Installments.Select(ins => new InstitutionContractInstallmentViewModel { AmountDouble = ins.Amount, InstallmentDateGr = ins.InstallmentDateGr }).OrderBy(ins => ins.InstallmentDateGr).Skip(1).ToList(),
|
||||
}).ToListAsync();
|
||||
|
||||
if (deatcivedContract.Any())
|
||||
institutionContracts.AddRange(deatcivedContract);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
foreach (var item in institutionContracts)
|
||||
{
|
||||
var jobRelation = "بابت قرارداد مابین (روابط کار)";
|
||||
var taxAndFinancial = "بابت قرارداد مابین (حسابداری و مالیات)";
|
||||
var contractingParty = await
|
||||
_context.PersonalContractingParties.FirstOrDefaultAsync(x => x.id == item.ContractingPartyId);
|
||||
Thread.Sleep(500);
|
||||
|
||||
string partyName = item.ContractingPartyName;
|
||||
if (partyName.Length > 18) partyName = $"{partyName.Substring(0, 18)}";
|
||||
|
||||
var isblock = contractingParty.IsBlock == "true" ? true : false;
|
||||
if (contractingParty != null && contractingParty.IsActiveString == "true" &&
|
||||
endOfMonthGr <= item.ContractEndGr)
|
||||
{
|
||||
try
|
||||
{
|
||||
var hasFinancialStatment = await _context.FinancialStatments.AnyAsync(
|
||||
x => x.ContractingPartyId == item.ContractingPartyId);
|
||||
var computeOption = item.TypeOfContract == "JobRelation" ? jobRelation : taxAndFinancial;
|
||||
|
||||
if (hasFinancialStatment)
|
||||
{
|
||||
var financialStatmentData = await
|
||||
_context.FinancialStatments.FirstOrDefaultAsync(x =>
|
||||
x.ContractingPartyId == item.ContractingPartyId);
|
||||
var financialStatmentId = financialStatmentData.id;
|
||||
|
||||
var alreadyCreated = await _context.FinancialTransactions.AnyAsync(x =>
|
||||
x.DescriptionOption == computeOption && x.TypeOfTransaction == "debt" &&
|
||||
x.TdateGr.Date == endOfMonthGr.Date && x.FinancialStatementId == financialStatmentId);
|
||||
|
||||
if (!alreadyCreated)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
if (item.IsInstallment && !string.IsNullOrWhiteSpace(item.PublicId))
|
||||
{
|
||||
var instalment = item.InstallmentList
|
||||
.FirstOrDefault(x =>
|
||||
x.InstallmentDateGr >= firstDayOfMonthGr &&
|
||||
x.InstallmentDateGr <= endOfMonthGr);
|
||||
if (instalment != null)
|
||||
{
|
||||
var transaction = new FinancialTransaction(financialStatmentId, endOfMonthGr,
|
||||
endOfMonthFa,
|
||||
description,
|
||||
"debt", computeOption, instalment.AmountDouble, 0, 0, isblock);
|
||||
await _financialTransactionRepository.CreateAsync(transaction);
|
||||
await _financialTransactionRepository.SaveChangesAsync();
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var transaction = new FinancialTransaction(financialStatmentId, endOfMonthGr,
|
||||
endOfMonthFa,
|
||||
description,
|
||||
"debt", computeOption, item.ContractAmountDouble, 0, 0, isblock);
|
||||
await _financialTransactionRepository.CreateAsync(transaction);
|
||||
await _financialTransactionRepository.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var statement = new FinancialStatment(item.ContractingPartyId, item.ContractingPartyName);
|
||||
await _financialStatmentRepository.CreateAsync(statement);
|
||||
await _financialStatmentRepository.SaveChangesAsync();
|
||||
|
||||
|
||||
if (item.IsInstallment && !string.IsNullOrWhiteSpace(item.PublicId))
|
||||
{
|
||||
var instalment = item.InstallmentList
|
||||
.FirstOrDefault(x =>
|
||||
x.InstallmentDateGr >= firstDayOfMonthGr &&
|
||||
x.InstallmentDateGr <= endOfMonthGr);
|
||||
if (instalment != null)
|
||||
{
|
||||
var transaction = new FinancialTransaction(statement.id, endOfMonthGr, endOfMonthFa,
|
||||
description,
|
||||
"debt", computeOption, instalment.AmountDouble, 0, 0, isblock);
|
||||
await _financialTransactionRepository.CreateAsync(transaction);
|
||||
await _financialTransactionRepository.SaveChangesAsync();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var transaction = new FinancialTransaction(statement.id, endOfMonthGr, endOfMonthFa,
|
||||
description,
|
||||
"debt", computeOption, item.ContractAmountDouble, 0, 0, isblock);
|
||||
await _financialTransactionRepository.CreateAsync(transaction);
|
||||
await _financialTransactionRepository.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
|
||||
string name = partyName.Length > 18 ? partyName.Substring(0, 18) : partyName;
|
||||
string errMess = $"{name}-خطا";
|
||||
await _smsService.Alarm("09114221321", errMess);
|
||||
|
||||
}
|
||||
|
||||
count += 1;
|
||||
Console.WriteLine(count);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task CreateTransaction(DateTime endOfMonthGr, string endOfMonthFa, string description)
|
||||
{
|
||||
await CreateTransactionOldInstitutionContracts(endOfMonthGr, endOfMonthFa, description);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CustomViewModels
|
||||
|
||||
public class WorkshopsAndEmployeeViewModel
|
||||
|
||||
Reference in New Issue
Block a user