From ed35067ecccf19dd376b77ecdd0e6b9b7f0f2f25 Mon Sep 17 00:00:00 2001 From: SamSys Date: Sat, 15 Nov 2025 19:00:39 +0330 Subject: [PATCH] CreateTransaction completed --- .../Jobs/JobSchedulerRegistrator.cs | 51 +++- .../FinancialTransaction.cs | 29 ++ .../IInstitutionContractRepository.cs | 16 ++ ...InstitutionContractInstallmentViewModel.cs | 8 +- .../InstitutionContractViewModel.cs | 5 + .../InstitutionContractRepository.cs | 251 +++++++++++++++++- 6 files changed, 347 insertions(+), 13 deletions(-) diff --git a/BackgroundInstitutionContract/BackgroundInstitutionContract.Task/Jobs/JobSchedulerRegistrator.cs b/BackgroundInstitutionContract/BackgroundInstitutionContract.Task/Jobs/JobSchedulerRegistrator.cs index e799bd95..4e0f79e9 100644 --- a/BackgroundInstitutionContract/BackgroundInstitutionContract.Task/Jobs/JobSchedulerRegistrator.cs +++ b/BackgroundInstitutionContract/BackgroundInstitutionContract.Task/Jobs/JobSchedulerRegistrator.cs @@ -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,14 +25,56 @@ 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", "خطا-ایجاد سند مالی"); + + } + + } + } diff --git a/Company.Domain/FinancialTransactionAgg/FinancialTransaction.cs b/Company.Domain/FinancialTransactionAgg/FinancialTransaction.cs index 65c20a40..061836b3 100644 --- a/Company.Domain/FinancialTransactionAgg/FinancialTransaction.cs +++ b/Company.Domain/FinancialTransactionAgg/FinancialTransaction.cs @@ -26,6 +26,35 @@ public class FinancialTransaction : EntityBase } + /// + /// ایجاد از طرف بک گراند سرویس + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + 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; } diff --git a/Company.Domain/InstitutionContractAgg/IInstitutionContractRepository.cs b/Company.Domain/InstitutionContractAgg/IInstitutionContractRepository.cs index 48cb43a5..3f24de19 100644 --- a/Company.Domain/InstitutionContractAgg/IInstitutionContractRepository.cs +++ b/Company.Domain/InstitutionContractAgg/IInstitutionContractRepository.cs @@ -101,4 +101,20 @@ public interface IInstitutionContractRepository : IRepository smsListData); #endregion + + #region CreateMontlyTransaction + + /// + /// ایجاد سند مالی برای قرارداد های قدیمی بدون قسط + /// + /// + Task CreateTransactionOldInstitutionContracts(DateTime endOfMonthGr, string endOfMonthFa, string description); + + /// + /// ایجاد سند مالی از طرف بک گراند تسک + /// + /// + Task CreateTransaction(DateTime endOfMonthGr, string endOfMonthFa, string description); + + #endregion } \ No newline at end of file diff --git a/CompanyManagment.App.Contracts/InstitutionContract/InstitutionContractInstallmentViewModel.cs b/CompanyManagment.App.Contracts/InstitutionContract/InstitutionContractInstallmentViewModel.cs index 86655873..77bee51d 100644 --- a/CompanyManagment.App.Contracts/InstitutionContract/InstitutionContractInstallmentViewModel.cs +++ b/CompanyManagment.App.Contracts/InstitutionContract/InstitutionContractInstallmentViewModel.cs @@ -27,7 +27,13 @@ public class InstitutionContractInstallmentViewModel /// مبلغ قسط /// public string Amount { get; set; } - + + /// + /// مبلغ قسط + /// Double + /// + public double AmountDouble { get; set; } + /// /// عدد قسط فارسی /// diff --git a/CompanyManagment.App.Contracts/InstitutionContract/InstitutionContractViewModel.cs b/CompanyManagment.App.Contracts/InstitutionContract/InstitutionContractViewModel.cs index 8371e5cd..464cf527 100644 --- a/CompanyManagment.App.Contracts/InstitutionContract/InstitutionContractViewModel.cs +++ b/CompanyManagment.App.Contracts/InstitutionContract/InstitutionContractViewModel.cs @@ -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 InstallmentList { get; set; } } \ No newline at end of file diff --git a/CompanyManagment.EFCore/Repository/InstitutionContractRepository.cs b/CompanyManagment.EFCore/Repository/InstitutionContractRepository.cs index 82cef7ed..f896a966 100644 --- a/CompanyManagment.EFCore/Repository/InstitutionContractRepository.cs +++ b/CompanyManagment.EFCore/Repository/InstitutionContractRepository.cs @@ -57,6 +57,8 @@ public class InstitutionContractRepository : RepositoryBase("InstitutionContractExtensionTemp"); _institutionAmendmentTemp = @@ -2950,6 +2954,8 @@ public class InstitutionContractRepository : RepositoryBase SendReminderSmsForBackgroundTask() { @@ -2987,9 +2993,9 @@ public class InstitutionContractRepository : RepositoryBase 18 ? item.PartyName.Substring(0, 18) : item.PartyName; string errMess = $"{name}-خطا"; await _smsService.Alarm("09114221321", errMess); - + } - + } @@ -3359,6 +3365,7 @@ public class InstitutionContractRepository : RepositoryBase /// دریافت صورت حساب مالی با آی دی طرف حساب /// @@ -3401,6 +3408,234 @@ public class InstitutionContractRepository : RepositoryBase + /// ایجاد سند مالی برای قرارداد های قدیمی بدون قسط + /// + /// + public async Task 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 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 futureContractIds = futureContracts.Select(x => x.ContractingPartyId).ToList(); + List 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