institutioncontract task

This commit is contained in:
SamSys
2025-11-12 15:37:24 +03:30
parent 2164aeb5bc
commit 193e9f587f
15 changed files with 847 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using _0_Framework.Application;
using _0_Framework.Application.Enums;
@@ -21,8 +22,11 @@ using Company.Domain.InstitutionContractAmendmentTempAgg;
using Company.Domain.InstitutionContractContactInfoAgg;
using Company.Domain.InstitutionContractExtensionTempAgg;
using Company.Domain.InstitutionPlanAgg;
using Company.Domain.SmsResultAgg;
using Company.Domain.WorkshopAgg;
using CompanyManagment.App.Contracts.Employer;
using CompanyManagment.App.Contracts.FinancialStatment;
using CompanyManagment.App.Contracts.FinancilTransaction;
using CompanyManagment.App.Contracts.InstitutionContract;
using CompanyManagment.App.Contracts.InstitutionContractContactinfo;
using CompanyManagment.App.Contracts.Law;
@@ -37,6 +41,7 @@ using MongoDB.Driver;
using OfficeOpenXml.Packaging.Ionic.Zip;
using PersianTools.Core;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using SmsResult = CompanyManagment.EFCore.Migrations.SmsResult;
namespace CompanyManagment.EFCore.Repository;
@@ -2941,8 +2946,400 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
#endregion
public async Task DebtReminderSMS()
{
var checkDate = DateTime.Now;
var currentMonthStart = ($"{(checkDate.ToFarsi()).Substring(0, 8)}01").ToGeorgianDateTime();
var previusMonthEnd = currentMonthStart.AddDays(-1);
var previusMonthStart = ($"{(previusMonthEnd.ToFarsi()).Substring(0, 8)}01").ToGeorgianDateTime();
var typeOfSms = "یادآور بدهی ماهانه";
//var rollcallServiceList = _context.RollCallServices.Where(x => x.StartService.Date <= previusMonthStart.Date && x.EndService.Date >= previusMonthEnd.Date).ToList();
var institutionContracts =await _context.InstitutionContractSet.AsSplitQuery().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,
OfficialCompany = x.OfficialCompany
}).Where(x => x.ContractStartGr < checkDate && x.ContractEndGr >= checkDate &&
x.ContractAmountDouble > 0).GroupBy(x => x.ContractingPartyId).Select(x => x.First()).ToListAsync();
foreach (var item in institutionContracts)
{
try
{
var contractingParty =await _context.PersonalContractingParties.FirstOrDefaultAsync(x=>x.id == item.ContractingPartyId);
//var isSend = sendedSms.Any(x => x.ContractingPatyId == contractingParty.Id);
if (!string.IsNullOrWhiteSpace(contractingParty.LName))
{
//Thread.Sleep(500);
var partyName = contractingParty.LName;
if (partyName.Length > 25) partyName = $"{partyName.Substring(0, 25)}";
var isLegal = contractingParty.IsLegal == "حقوقی" ? true : false;
var isBlock = contractingParty.IsBlock == "true" ? true : false;
var isActive = contractingParty.IsActiveString == "true" ? true : false;
if (!string.IsNullOrWhiteSpace(contractingParty.IsActiveString) && isActive)
{
var hasFinancialStatement =await _context.FinancialStatments.Include(x => x.FinancialTransactionList).AnyAsync(
x => x.ContractingPartyId == item.ContractingPartyId && x.FinancialTransactionList.Count > 0);
Thread.Sleep(500);
var hasPhonNumber =await _context.InstitutionContractContactInfos
.AnyAsync(x => x.InstitutionContractId == item.Id && x.SendSms && x.PhoneType == "شماره همراه" &&
x.PhoneNumber.Length == 11);
Thread.Sleep(500);
if (hasFinancialStatement && hasPhonNumber)
{
var phoneNumbers =await _context.InstitutionContractContactInfos.Where(n =>
n.InstitutionContractId == item.Id && n.SendSms && n.PhoneType == "شماره همراه" &&
!string.IsNullOrWhiteSpace(n.PhoneNumber))
.Select(x => new CreateContactInfo
{
PhoneType = x.PhoneType,
PhoneNumber = x.PhoneNumber,
InstitutionContractId = x.InstitutionContractId,
SendSms = x.SendSms
}).Where(x => x.PhoneNumber.Length == 11).ToListAsync();
var transactions = GetFinancialByContractingPartyId(contractingParty.id).GetAwaiter().GetResult();
Thread.Sleep(500);
var debtor = transactions.FinancialTransactionViewModels.Sum(x => x.Deptor);
var creditor = transactions.FinancialTransactionViewModels.Sum(x => x.Creditor);
Thread.Sleep(500);
var id = $"{item.ContractingPartyId}";
var aprove = $"{transactions.Id}";
var balance = debtor - creditor;
if (balance > 0) // اگر بدهکار بود
{
//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 services =
// rollcallServiceList.Where(x => workshops.Contains(x.WorkshopId)).ToList();
//var hasRollCallService = services.Count > 0;
//موقت
var hasRollCallService = false;
//if (hasRollCallService)
//{
// var employees =
// _context.RollCallEmployees.Where(x => workshops.Contains(x.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)
// hasRollCallService = false;
//}
if (isLegal)
{
if (item.OfficialCompany == "Official") // حقوقی بدهکار رسمی
{
var balanceToMoney = balance.ToMoney();
foreach (var number in phoneNumbers)
{
if (!string.IsNullOrWhiteSpace(number.PhoneNumber) &&
number.PhoneNumber.Length == 11)
{
int messageId = 0;
string message = "";
bool isSuccessSend = false;
if (hasRollCallService)
{
string publicId = transactions.PublicId;
string code1 = publicId.Substring(0, 25);
string code2 = publicId.Substring(25);
var newSmsResult = _smsService.MonthlyBillNew(number.PhoneNumber, 789638,
partyName, balanceToMoney, code1, code2);
Thread.Sleep(1000);
messageId = newSmsResult.MessageId;
message = newSmsResult.Message;
isSuccessSend = newSmsResult.IsSuccedded;
}
else
{
var smsResult = _smsService.MonthlyBill(number.PhoneNumber, 161233,
partyName,
balanceToMoney, id, aprove);
Thread.Sleep(1000);
isSuccessSend = smsResult.IsSuccedded;
messageId = smsResult.MessageId;
message = smsResult.Message;
}
if (isSuccessSend)
{
var createSmsResult = new SmsResult(messageId,
message, typeOfSms, partyName, number.PhoneNumber,
item.ContractingPartyId, item.Id);
_smsResultRepository.Create(createSmsResult);
_smsResultRepository.SaveChanges();
Thread.Sleep(1000);
}
//Console.ForegroundColor = ConsoleColor.Black;
//Console.BackgroundColor = ConsoleColor.Yellow;
//Console.WriteLine(number.PhoneNumber + " debt : " + balance + " contractId = " + item.Id);
//Console.ResetColor();
}
}
}
else if (item.OfficialCompany == "NotOfficial") // حقوقی بدهکار غیر رسمی
{
var balanceToMoney = balance.ToMoney();
foreach (var number in phoneNumbers)
{
if (!string.IsNullOrWhiteSpace(number.PhoneNumber) &&
number.PhoneNumber.Length == 11)
{
int messageId = 0;
string message = "";
bool isSuccessSend = false;
if (hasRollCallService)
{
string publicId = transactions.PublicId;
string code1 = publicId.Substring(0, 25);
string code2 = publicId.Substring(25);
var newSmsResult = _smsService.MonthlyBillNew(number.PhoneNumber, 789638,
partyName, balanceToMoney, code1, code2);
Thread.Sleep(1000);
messageId = newSmsResult.MessageId;
message = newSmsResult.Message;
isSuccessSend = newSmsResult.IsSuccedded;
}
else
{
var smsResult = _smsService.MonthlyBill(number.PhoneNumber, 347415,
partyName,
balanceToMoney, id, aprove);
Thread.Sleep(1000);
isSuccessSend = smsResult.IsSuccedded;
messageId = smsResult.MessageId;
message = smsResult.Message;
}
if (isSuccessSend)
{
var createSmsResult = new SmsResult(messageId,
message, typeOfSms, partyName, number.PhoneNumber,
item.ContractingPartyId, item.Id);
_smsResultRepository.Create(createSmsResult);
_smsResultRepository.SaveChanges();
Thread.Sleep(1000);
}
//Console.ForegroundColor = ConsoleColor.Black;
//Console.BackgroundColor = ConsoleColor.Yellow;
//Console.WriteLine(number.PhoneNumber + " debt : " + balance + " contractId = " + item.Id);
//Console.ResetColor();
}
}
}
}
else
{
if (item.OfficialCompany == "Official") // حقیقی بدهکار رسمی
{
var balanceToMoney = balance.ToMoney();
foreach (var number in phoneNumbers)
{
if (!string.IsNullOrWhiteSpace(number.PhoneNumber) &&
number.PhoneNumber.Length == 11)
{
int messageId = 0;
string message = "";
bool isSuccessSend = false;
if (hasRollCallService)
{
string publicId = transactions.PublicId;
string code1 = publicId.Substring(0, 25);
string code2 = publicId.Substring(25);
var newSmsResult = _smsService.MonthlyBillNew(number.PhoneNumber, 768277,
partyName, balanceToMoney, code1, code2);
Thread.Sleep(1000);
messageId = newSmsResult.MessageId;
message = newSmsResult.Message;
isSuccessSend = newSmsResult.IsSuccedded;
}
else
{
var smsResult = _smsService.MonthlyBill(number.PhoneNumber, 998180,
partyName,
balanceToMoney, id, aprove);
Thread.Sleep(1000);
isSuccessSend = smsResult.IsSuccedded;
messageId = smsResult.MessageId;
message = smsResult.Message;
}
if (isSuccessSend)
{
var createSmsResult = new SmsResult(messageId,
message, typeOfSms, partyName, number.PhoneNumber,
item.ContractingPartyId, item.Id);
_smsResultRepository.Create(createSmsResult);
_smsResultRepository.SaveChanges();
Thread.Sleep(1000);
}
//Console.ForegroundColor = ConsoleColor.Black;
//Console.BackgroundColor = ConsoleColor.Yellow;
//Console.WriteLine(number.PhoneNumber + " debt : " + balance + " contractId = " + item.Id);
//Console.ResetColor();
}
}
}
else if (item.OfficialCompany == "NotOfficial") // حقیقی بدهکار غیر رسمی
{
var balanceToMoney = balance.ToMoney();
foreach (var number in phoneNumbers)
{
if (!string.IsNullOrWhiteSpace(number.PhoneNumber) &&
number.PhoneNumber.Length == 11)
{
int messageId = 0;
string message = "";
bool isSuccessSend = false;
if (hasRollCallService)
{
string publicId = transactions.PublicId;
string code1 = publicId.Substring(0, 25);
string code2 = publicId.Substring(25);
var newSmsResult = _smsService.MonthlyBillNew(number.PhoneNumber, 768277,
partyName, balanceToMoney, code1, code2);
Thread.Sleep(1000);
messageId = newSmsResult.MessageId;
message = newSmsResult.Message;
isSuccessSend = newSmsResult.IsSuccedded;
}
else
{
var smsResult = _smsService.MonthlyBill(number.PhoneNumber, 810539,
partyName,
balanceToMoney, id, aprove);
Thread.Sleep(1000);
isSuccessSend = smsResult.IsSuccedded;
messageId = smsResult.MessageId;
message = smsResult.Message;
}
if (isSuccessSend)
{
var createSmsResult = new SmsResult(messageId,
message, typeOfSms, partyName, number.PhoneNumber,
item.ContractingPartyId, item.Id);
_smsResultRepository.Create(createSmsResult);
_smsResultRepository.SaveChanges();
Thread.Sleep(1000);
}
//Console.ForegroundColor = ConsoleColor.Black;
//Console.BackgroundColor = ConsoleColor.Yellow;
//Console.WriteLine(number.PhoneNumber + " debt : " + balance + " contractId = " + item.Id);
////_logger.LogInformation(number.PhoneNumber + " debt : " + balance + " nolegal" + "NotOfficial");
//Console.ResetColor();
}
}
}
}
}
phoneNumbers = new List<CreateContactInfo>();
}
}
}
}
catch (Exception e)
{
string name = item.ContractingPartyName.Length > 18 ? item.ContractingPartyName.Substring(0, 18) : item.ContractingPartyName;
string errMess = $"{name}-خطا";
_smsService.Alarm("09114221321", errMess);
_logger.LogError(e, "ReminderSms");
}
}
}
#region ExternalMetods
private async Task<FinancialStatmentViewModel>? GetFinancialByContractingPartyId(long contractingPartyId)
{
return await _context.FinancialStatments.Include(x => x.FinancialTransactionList)
.Select(x => new FinancialStatmentViewModel
{
Id = x.id,
ContractingPartyId = x.ContractingPartyId,
ContractingPartyName = x.ContractingPartyName,
PublicId = x.PublicIdStr,
FinancialTransactionViewModels = x.FinancialTransactionList.Select(t => new FinancialTransactionViewModel
{
Id = t.id,
TdateFa = t.TdateFa,
TdateGr = t.TdateGr,
Description = t.TypeOfTransaction == "debt"
? "ایجاد درآمد" + " " + t.DescriptionOption + " " + t.Description
: "دریافت درآمد" + " " + t.DescriptionOption + " " + t.Description,
Deptor = t.Deptor,
DeptorString = t.Deptor != 0 ? t.Deptor.ToMoney() : "",
Creditor = t.Creditor,
CreditorString = t.Creditor != 0 ? t.Creditor.ToMoney() : "",
Balance = t.Balance,
MessageText = t.MessageText,
SentSms = t.SentSms,
SentSmsDateFa = t.SentSmsDateFa,
FinancialStatementId = t.FinancialStatementId,
TypeOfTransaction = t.TypeOfTransaction,
DescriptionOption = t.DescriptionOption
}).OrderBy(t => t.TdateGr).ToList()
})
.FirstOrDefaultAsync(x => x.ContractingPartyId == contractingPartyId);
}
#endregion
#region CustomViewModels
public class WorkshopsAndEmployeeViewModel