Refactor institution contract status handling and improve query logic
This commit is contained in:
@@ -8,6 +8,5 @@ public enum InstitutionContractListStatus
|
||||
Block,
|
||||
Free,
|
||||
PendingForRenewal,
|
||||
WithoutWorkshop,
|
||||
Completed
|
||||
WithoutWorkshop
|
||||
}
|
||||
@@ -976,9 +976,8 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
double result = Math.Ceiling(roundFloor / 10000.0) * 10000;
|
||||
|
||||
|
||||
|
||||
Console.WriteLine(counter + " - " + rollCallService.StartService.ToFarsi() + " - " +
|
||||
rollCallService.WorkshopId + " - " + employeeCount +
|
||||
rollCallService.WorkshopId + " - " + employeeCount +
|
||||
$" - {totalAmonut} - round {result}");
|
||||
|
||||
var financialStatment =
|
||||
@@ -1019,6 +1018,10 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
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)
|
||||
@@ -1030,7 +1033,28 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
.Join(_context.FinancialStatments.Include(x => x.FinancialTransactionList),
|
||||
x => x.contractingParty.id,
|
||||
statement => statement.ContractingPartyId,
|
||||
(x, statement) => new { x.contractingParty, x.contract, statement });
|
||||
(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
|
||||
|
||||
@@ -1117,42 +1141,31 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
joinedQuery = joinedQuery.Where(x => x.contract.OfficialCompany == isOfficialStr);
|
||||
}
|
||||
|
||||
var now = DateTime.Today;
|
||||
var nowFa = now.ToFarsi();
|
||||
var endFa = nowFa.FindeEndOfMonth();
|
||||
var endThisMontGr = endFa.ToGeorgianDateTime();
|
||||
|
||||
if (searchModel.Status != null)
|
||||
{
|
||||
switch (searchModel.Status)
|
||||
{
|
||||
case InstitutionContractListStatus.DeactiveWithDebt:
|
||||
joinedQuery = joinedQuery.Where(x => x.contract.IsActiveString == "blue");
|
||||
joinedQuery = joinedQuery.Where(x =>
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.DeactiveWithDebt);
|
||||
break;
|
||||
case InstitutionContractListStatus.PendingForRenewal:
|
||||
joinedQuery = joinedQuery.Where(x =>
|
||||
x.contract.ContractEndGr >= now && x.contract.ContractEndGr <= endThisMontGr);
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.PendingForRenewal);
|
||||
break;
|
||||
case InstitutionContractListStatus.Block:
|
||||
joinedQuery = joinedQuery.Where(x => x.contractingParty.IsBlock == "true");
|
||||
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Block);
|
||||
break;
|
||||
case InstitutionContractListStatus.Free:
|
||||
joinedQuery = joinedQuery.Where(x => x.contract.ContractAmount == 0);
|
||||
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Free);
|
||||
break;
|
||||
case InstitutionContractListStatus.WithoutWorkshop:
|
||||
joinedQuery = joinedQuery.Where(x => !x.contractingParty.Employers
|
||||
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop)).Any());
|
||||
joinedQuery = joinedQuery.Where(x =>
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.WithoutWorkshop);
|
||||
break;
|
||||
case InstitutionContractListStatus.Active:
|
||||
joinedQuery = joinedQuery.Where(x =>
|
||||
x.contract.IsActiveString != "blue" && // Not DeactiveWithDebt
|
||||
!(x.contract.ContractEndGr >= now &&
|
||||
x.contract.ContractEndGr <= endThisMontGr) && // Not PendingForRenewal
|
||||
x.contractingParty.IsBlock != "true" && // Not Block
|
||||
x.contract.ContractAmount != 0 && // Not Free
|
||||
x.contractingParty.Employers
|
||||
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop))
|
||||
.Any() // Has at least one workshop => Not WithoutWorkshop
|
||||
);
|
||||
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Active);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1167,21 +1180,19 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
|
||||
var orderedQuery = joinedQuery
|
||||
.OrderBy(x =>
|
||||
x.contract.IsActiveString == "blue"
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.DeactiveWithDebt
|
||||
? 0
|
||||
: // DeactiveWithoutDebt
|
||||
(x.contract.ContractEndGr >= now && x.contract.ContractEndGr <= endOfMonth)
|
||||
(x.StatusPriority == (int)InstitutionContractListStatus.PendingForRenewal)
|
||||
? 1
|
||||
: // PendingToRenewal
|
||||
x.contractingParty.IsBlock == "true"
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.Block
|
||||
? 2
|
||||
: // Block
|
||||
x.contract.ContractAmount == 0
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.Free
|
||||
? 3
|
||||
: // Free
|
||||
!x.contractingParty.Employers
|
||||
.SelectMany(e => e.WorkshopEmployers)
|
||||
.Any()
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.WithoutWorkshop
|
||||
? 4
|
||||
: // WithoutWorkshop
|
||||
5 // Active
|
||||
@@ -1208,7 +1219,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
var minArchiveCode = arc.Count > 0 ? arc.Min(a => a) : 0;
|
||||
|
||||
var archiveCode = minArchiveCode == 10000000 ? 0 : minArchiveCode;
|
||||
var status = SetContractStatus(x.contract, x.contractingParty, x.statement);
|
||||
var status =Enum.Parse<InstitutionContractListStatus>(x.StatusPriority.ToString());
|
||||
return new GetInstitutionContractListItemsViewModel()
|
||||
{
|
||||
ContractAmount = x.contract.ContractAmount,
|
||||
@@ -1227,8 +1238,8 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
.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.status,
|
||||
IsExpired = status.isExpiered,
|
||||
ListStatus = status,
|
||||
IsExpired = x.contract.ContractEndGr <= endThisMontGr,
|
||||
ContractingPartyId = x.contractingParty.id,
|
||||
};
|
||||
}).ToList()
|
||||
@@ -1242,6 +1253,10 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
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)
|
||||
@@ -1253,8 +1268,30 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
.Join(_context.FinancialStatments.Include(x => x.FinancialTransactionList),
|
||||
x => x.contractingParty.id,
|
||||
statement => statement.ContractingPartyId,
|
||||
(x, statement) => new { x.contractingParty, x.contract, statement });
|
||||
(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))
|
||||
{
|
||||
@@ -1339,43 +1376,31 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
joinedQuery = joinedQuery.Where(x => x.contract.OfficialCompany == isOfficialStr);
|
||||
}
|
||||
|
||||
var now = DateTime.Today;
|
||||
var nowFa = now.ToFarsi();
|
||||
var endFa = nowFa.FindeEndOfMonth();
|
||||
var endThisMontGr = endFa.ToGeorgianDateTime();
|
||||
|
||||
if (searchModel.Status != null)
|
||||
{
|
||||
switch (searchModel.Status)
|
||||
{
|
||||
case InstitutionContractListStatus.DeactiveWithDebt:
|
||||
joinedQuery = joinedQuery.Where(x => x.contract.IsActiveString == "blue");
|
||||
joinedQuery = joinedQuery.Where(x =>
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.DeactiveWithDebt);
|
||||
break;
|
||||
case InstitutionContractListStatus.PendingForRenewal:
|
||||
joinedQuery = joinedQuery.Where(x =>
|
||||
x.contract.ContractEndGr >= now && x.contract.ContractEndGr <= endThisMontGr);
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.PendingForRenewal);
|
||||
break;
|
||||
case InstitutionContractListStatus.Block:
|
||||
joinedQuery = joinedQuery.Where(x => x.contractingParty.IsBlock == "true");
|
||||
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Block);
|
||||
break;
|
||||
case InstitutionContractListStatus.Free:
|
||||
joinedQuery = joinedQuery.Where(x => x.contract.ContractAmount == 0);
|
||||
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Free);
|
||||
break;
|
||||
case InstitutionContractListStatus.WithoutWorkshop:
|
||||
joinedQuery = joinedQuery.Where(x => !x.contractingParty.Employers
|
||||
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop)).Any());
|
||||
joinedQuery = joinedQuery.Where(x =>
|
||||
x.StatusPriority == (int)InstitutionContractListStatus.WithoutWorkshop);
|
||||
break;
|
||||
case InstitutionContractListStatus.Active:
|
||||
joinedQuery = joinedQuery.Where(x =>
|
||||
x.contract.IsActiveString != "blue" && // Not DeactiveWithDebt
|
||||
!(x.contract.ContractEndGr >= now &&
|
||||
x.contract.ContractEndGr <= endThisMontGr) && // Not PendingForRenewal
|
||||
x.contractingParty.IsBlock != "true" && // Not Block
|
||||
x.contract.ContractAmount != 0 && // Not Free
|
||||
x.contractingParty.Employers
|
||||
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop))
|
||||
.Any() // Has at least one workshop => Not WithoutWorkshop
|
||||
);
|
||||
joinedQuery = joinedQuery.Where(x => x.StatusPriority == (int)InstitutionContractListStatus.Active);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1384,6 +1409,8 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
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)
|
||||
@@ -1440,7 +1467,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
var institutionContract = await _context.InstitutionContractSet
|
||||
.Include(x => x.WorkshopDetails)
|
||||
.FirstOrDefaultAsync(x => x.id == institutionContractId);
|
||||
|
||||
|
||||
if (institutionContract == null)
|
||||
throw new NotFoundException("قرارداد مؤسسه یافت نشد");
|
||||
|
||||
@@ -1463,9 +1490,9 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
|
||||
// دریافت اطلاعات کارفرمایان در یک کوئری واحد
|
||||
var employersDict = (await _context.Employers
|
||||
.Where(e => allEmployerIds.Contains(e.id))
|
||||
.Select(e => new { e.id, e.FullName })
|
||||
.ToListAsync())
|
||||
.Where(e => allEmployerIds.Contains(e.id))
|
||||
.Select(e => new { e.id, e.FullName })
|
||||
.ToListAsync())
|
||||
.ToDictionary(e => e.id, e => e);
|
||||
|
||||
// ساخت نتیجه نهایی با استفاده از دادههای از پیش بارگذاری شده
|
||||
@@ -1473,7 +1500,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
{
|
||||
// پیدا کردن کارگاه مرتبط
|
||||
var workshop = workshops.FirstOrDefault(w => w.id == workshopDetail.WorkshopId);
|
||||
|
||||
|
||||
// ساخت لیست کارفرمایان این جزئیات کارگاه
|
||||
var employers = workshopDetail.Employers
|
||||
.Where(e => employersDict.ContainsKey(e.EmployerId))
|
||||
@@ -1502,6 +1529,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
PersonalContractingParty contractingParty,
|
||||
FinancialStatment financialStatment)
|
||||
{
|
||||
//if (contract.ContractEndGr <= endThisMontGr)
|
||||
var now = DateTime.Now;
|
||||
var nowFa = now.ToFarsi();
|
||||
var endFa = nowFa.FindeEndOfMonth();
|
||||
@@ -1552,8 +1580,9 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
return (listStatus, isExpier);
|
||||
}
|
||||
|
||||
|
||||
public async Task<InstitutionContractWorkshopDetail> GetInstitutionWorkshopDetails(long institutionWorkshopDetailsId)
|
||||
|
||||
public async Task<InstitutionContractWorkshopDetail> GetInstitutionWorkshopDetails(
|
||||
long institutionWorkshopDetailsId)
|
||||
{
|
||||
return await _context.InstitutionContractWorkshopDetails.FirstOrDefaultAsync(x =>
|
||||
x.id == institutionWorkshopDetailsId);
|
||||
@@ -1562,7 +1591,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
public async Task<InstitutionContract> GetIncludeWorkshopDetailsAsync(long institutionContractId)
|
||||
{
|
||||
return await _context.InstitutionContractSet
|
||||
.Include(x=>x.WorkshopDetails)
|
||||
.Include(x => x.WorkshopDetails)
|
||||
.FirstOrDefaultAsync(x => x.id == institutionContractId);
|
||||
}
|
||||
|
||||
@@ -1573,14 +1602,13 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
.FirstOrDefault(x => x.id == institutionContractId);
|
||||
if (institutionContract == null)
|
||||
throw new NotFoundException("قرارداد مؤسسه یافت نشد");
|
||||
|
||||
|
||||
if (institutionContract.Status == InstitutionContractStatus.Completed)
|
||||
return;
|
||||
if (institutionContract.WorkshopDetails.All(x => x.WorkshopCreated))
|
||||
institutionContract.Complete();
|
||||
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -1597,7 +1625,4 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user