453 lines
17 KiB
C#
453 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using _0_Framework.Application;
|
|
using _0_Framework.InfraStructure;
|
|
using AccountMangement.Infrastructure.EFCore;
|
|
using Company.Domain.ContarctingPartyAgg;
|
|
using CompanyManagment.App.Contracts.Employer;
|
|
using CompanyManagment.App.Contracts.PersonalContractingParty;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class PersonalContractingPartyRepository : RepositoryBase<long, PersonalContractingParty>, IPersonalContractingPartyRepository
|
|
{
|
|
private readonly CompanyContext _context;
|
|
private readonly AccountContext _accountContext;
|
|
|
|
public PersonalContractingPartyRepository(CompanyContext context, AccountContext accountContext) : base(context)
|
|
{
|
|
_context = context;
|
|
_accountContext = accountContext;
|
|
}
|
|
public List<PersonalContractingPartyViewModel> GetPersonalContractingParties()
|
|
{
|
|
var res = _context.PersonalContractingParties.Select(x => new PersonalContractingPartyViewModel
|
|
{
|
|
id = x.id,
|
|
|
|
FName = x.FName,
|
|
LName = x.IsLegal == "حقیقی" ? $"{x.FName.Trim()} {x.LName.Trim()}" : $"{x.LName.Trim()}",
|
|
SureName = x.SureName.Trim(),
|
|
//Nationalcode = x.Nationalcode,
|
|
//IdNumber = x.IdNumber,
|
|
//RegisterId = x.RegisterId,
|
|
//NationalId = x.NationalId,
|
|
IsLegal = x.IsLegal,
|
|
//Phone = x.Phone,
|
|
//AgentPhone = x.AgentPhone,
|
|
|
|
|
|
}).ToList();
|
|
res = res.Select(x => new PersonalContractingPartyViewModel()
|
|
{
|
|
id = x.id,
|
|
|
|
FName = x.FName,
|
|
LName = !string.IsNullOrWhiteSpace(x.SureName) ? $"{x.LName} ({x.SureName})" : $"{x.LName}",
|
|
SureName = x.SureName,
|
|
FullName = !string.IsNullOrWhiteSpace(x.SureName) ? (x.LName+" "+ x.SureName) : $"{x.LName}",
|
|
//Nationalcode = x.Nationalcode,
|
|
//IdNumber = x.IdNumber,
|
|
//RegisterId = x.RegisterId,
|
|
//NationalId = x.NationalId,
|
|
|
|
IsLegal = x.IsLegal,
|
|
|
|
}).ToList();
|
|
//var result = new List<PersonalContractingPartyViewModel>();
|
|
//foreach (var item in res)
|
|
//{
|
|
|
|
// item.FullName = $"{item.LName}";
|
|
|
|
// result.Add(item);
|
|
//}
|
|
|
|
return res;
|
|
}
|
|
public EditPersonalContractingParty GetDetails(long id)
|
|
{
|
|
var res = _context.PersonalContractingParties.Select(x => new EditPersonalContractingParty()
|
|
{
|
|
Id = x.id,
|
|
|
|
FName = x.FName,
|
|
LName = x.IsLegal == "حقیقی" ? $"{x.FName} {x.LName}" : $"{x.LName}",
|
|
SureName = x.SureName,
|
|
Nationalcode = x.Nationalcode,
|
|
IdNumber = x.IdNumber,
|
|
RegisterId = x.RegisterId,
|
|
NationalId = x.NationalId,
|
|
IsLegal = x.IsLegal,
|
|
Phone = x.Phone,
|
|
AgentPhone = x.AgentPhone,
|
|
Address = x.Address,
|
|
ArchiveCode = x.ArchiveCode,
|
|
RepresentativeId = x.RepresentativeId,
|
|
RepresentativeFullName = x.RepresentativeFullName,
|
|
State = x.State,
|
|
City = x.City,
|
|
Zone = x.Zone,
|
|
IsActiveString = x.IsActiveString,
|
|
IsBlock = x.IsBlock,
|
|
BlockTimes = x.BlockTimes,
|
|
|
|
|
|
})
|
|
.FirstOrDefault(x => x.Id == id);
|
|
|
|
if (res != null)
|
|
{
|
|
res.LName = !string.IsNullOrWhiteSpace(res.SureName) ? $"{res.LName} ({res.SureName})" : $"{res.LName}";
|
|
}
|
|
|
|
return res;
|
|
}
|
|
public EditPersonalContractingParty GetDetailsToEdit(long id)
|
|
{
|
|
return _context.PersonalContractingParties.Select(x => new EditPersonalContractingParty()
|
|
{
|
|
Id = x.id,
|
|
|
|
FName = x.FName,
|
|
LName = x.LName,
|
|
SureName = x.SureName,
|
|
Nationalcode = x.Nationalcode,
|
|
IdNumber = x.IdNumber,
|
|
RegisterId = x.RegisterId,
|
|
NationalId = x.NationalId,
|
|
IsLegal = x.IsLegal,
|
|
Phone = x.Phone,
|
|
AgentPhone = x.AgentPhone,
|
|
Address = x.Address,
|
|
ArchiveCode = x.ArchiveCode,
|
|
RepresentativeId = x.RepresentativeId,
|
|
RepresentativeFullName = x.RepresentativeFullName,
|
|
State = x.State,
|
|
City = x.City,
|
|
Zone = x.Zone,
|
|
IsActiveString = x.IsActiveString,
|
|
IsBlock = x.IsBlock,
|
|
BlockTimes = x.BlockTimes,
|
|
|
|
|
|
})
|
|
.FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
|
|
public string GetFullName(long id)
|
|
{
|
|
string FullName = "";
|
|
var res = _context.PersonalContractingParties.Select(x => new PersonalContractingPartyViewModel
|
|
{
|
|
id = x.id,
|
|
FName = x.FName,
|
|
LName = x.IsLegal == "حقیقی" ? $"{x.FName} {x.LName}" : $"{x.LName}",
|
|
SureName = x.SureName
|
|
|
|
|
|
}).FirstOrDefault(x => x.id == id);
|
|
|
|
if (res != null)
|
|
{
|
|
|
|
return FullName = !string.IsNullOrWhiteSpace(res.SureName)
|
|
? $"{res.LName} ({res.SureName})"
|
|
: $"{res.LName}";
|
|
|
|
}
|
|
else
|
|
{
|
|
return "";
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public List<PersonalContractingPartyViewModel> Search(PersonalContractingPartySearchModel searchModel2)
|
|
{
|
|
var query = _context.PersonalContractingParties.Select(x => new PersonalContractingPartyViewModel
|
|
{
|
|
id = x.id,
|
|
|
|
FName = x.FName,
|
|
LName = !string.IsNullOrWhiteSpace(x.SureName) ? $"{x.LName} ({x.SureName})" : $"{x.LName}",
|
|
SureName = x.SureName,
|
|
Nationalcode = x.Nationalcode,
|
|
IdNumber = x.IdNumber,
|
|
RegisterId = x.RegisterId,
|
|
NationalId = x.NationalId,
|
|
IsLegal = x.IsLegal,
|
|
ArchiveCode = x.ArchiveCode,
|
|
|
|
State = x.State,
|
|
City = x.City,
|
|
Zone = x.Zone,
|
|
IsActiveString = x.IsActiveString,
|
|
IsBlock = x.IsBlock,
|
|
BlockTimes = x.BlockTimes,
|
|
|
|
CreationDate = x.CreationDate.ToString()
|
|
});
|
|
if (searchModel2.id > 0)
|
|
query = query.Where(x => x.id == searchModel2.id);
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.FName))
|
|
query = query.Where(x => x.FName.Contains(searchModel2.FName));
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.LName))
|
|
query = query.Where(x => x.LName.Contains(searchModel2.LName));
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.Nationalcode))
|
|
query = query.Where(x => x.Nationalcode.Contains(searchModel2.Nationalcode));
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.IdNumber))
|
|
query = query.Where(x => x.IdNumber.Contains(searchModel2.IdNumber));
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.RegisterId))
|
|
query = query.Where(x => x.RegisterId.Contains(searchModel2.RegisterId));
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.NationalId))
|
|
query = query.Where(x => x.NationalId.Contains(searchModel2.NationalId));
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.IsLegal))
|
|
query = query.Where(x => x.IsLegal.Contains(searchModel2.IsLegal));
|
|
|
|
return query.OrderByDescending(x => x.id).ToList();
|
|
}
|
|
|
|
public int GetLastArchiveCode()
|
|
{
|
|
|
|
var res = _context.PersonalContractingParties.Max(x => x.ArchiveCode);
|
|
|
|
if (res > 0)
|
|
{
|
|
|
|
res += 1;
|
|
return res;
|
|
}
|
|
else
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
#region Mahan
|
|
public List<string> SearchByName(string name)
|
|
{
|
|
var contractingParties = _context.PersonalContractingParties.Select(x => new PersonalContractingPartyViewModel()
|
|
{
|
|
FullName = x.IsLegal == "حقوقی" ? x.LName : $"{x.FName} {x.LName}"
|
|
}).ToList();
|
|
return contractingParties.Select(x => x.FullName).ToList();
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region NewByHeydari
|
|
public List<PersonalContractingPartyViewModel> GetPersonalContractingPartiesForNationalcode(string searchText)
|
|
{
|
|
var result = _context.PersonalContractingParties.Where(x => (!string.IsNullOrEmpty(x.Nationalcode) && x.Nationalcode.StartsWith(searchText)) )
|
|
.Select(x => new PersonalContractingPartyViewModel
|
|
{
|
|
id = x.id,
|
|
Nationalcode = x.Nationalcode,
|
|
|
|
}).Take(100).ToList();
|
|
|
|
return result;
|
|
}
|
|
public List<PersonalContractingPartyViewModel> SearchForMain(PersonalContractingPartySearchModel searchModel2)
|
|
{
|
|
var query = _context.PersonalContractingParties.Include(x=>x.Employers).Select(x => new PersonalContractingPartyViewModel
|
|
{
|
|
id = x.id,
|
|
|
|
FName = x.FName,
|
|
LName = !string.IsNullOrWhiteSpace(x.SureName) ? $"{x.LName} ({x.SureName})" : $"{x.LName}",
|
|
SureName = x.SureName,
|
|
Nationalcode = x.Nationalcode,
|
|
IdNumber = x.IdNumber,
|
|
RegisterId = x.RegisterId,
|
|
NationalId = x.NationalId,
|
|
IsLegal = x.IsLegal,
|
|
ArchiveCode = x.ArchiveCode,
|
|
FullName = x.FName+ " "+x.LName,
|
|
State = x.State,
|
|
City = x.City,
|
|
Zone = x.Zone,
|
|
IsActiveString = x.IsActiveString,
|
|
IsBlock = x.IsBlock,
|
|
BlockTimes = x.BlockTimes,
|
|
RepresentativeId = x.RepresentativeId,
|
|
CreationDate = x.CreationDate.ToString(),
|
|
EmployerList = x.Employers.Select(y => new EmployerViewModel() { Id = y.id, FullName = y.FullName}).ToList()
|
|
|
|
});
|
|
if (searchModel2.id > 0)
|
|
query = query.Where(x => x.id == searchModel2.id);
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.Nationalcode) && searchModel2.Nationalcode != "0")
|
|
query = query.Where(x => x.Nationalcode.Contains(searchModel2.Nationalcode));
|
|
|
|
if (searchModel2.RepresentativeId > 0)
|
|
query = query.Where(x => x.RepresentativeId== searchModel2.RepresentativeId);
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.IsLegal))
|
|
query = query.Where(x => x.IsLegal==searchModel2.IsLegal);
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel2.IsActiveString)&& searchModel2.IsActiveString!="both")
|
|
query = query.Where(x => x.IsActiveString==searchModel2.IsActiveString);
|
|
|
|
if (!string.IsNullOrEmpty(searchModel2.RepresentativeName))
|
|
{
|
|
var representativeIds = _context.RepresentativeSet.Where(x => !string.IsNullOrEmpty(x.FullName) && x.FullName.Contains(searchModel2.RepresentativeName)).Select(x=>x.id).ToList();
|
|
query = query.Where(x => representativeIds.Contains( x.RepresentativeId));
|
|
}
|
|
if (!string.IsNullOrEmpty(searchModel2.EmployeeName))
|
|
{
|
|
query = query.Where(x => (x.IsLegal == "حقیقی" &&
|
|
((!string.IsNullOrWhiteSpace(x.FName) && x.FName.StartsWith(searchModel2.EmployeeName)) ||
|
|
(!string.IsNullOrWhiteSpace(x.LName) && x.LName.StartsWith(searchModel2.EmployeeName))))
|
|
|| (x.IsLegal == "حقوقی" && (!string.IsNullOrWhiteSpace(x.LName) && x.LName.Contains(searchModel2.EmployeeName))));
|
|
}
|
|
return query.OrderByDescending(x => x.id).ToList();
|
|
}
|
|
public OperationResult DeletePersonalContractingParties(long id)
|
|
{
|
|
var op = new OperationResult();
|
|
|
|
try
|
|
{
|
|
var personalContractingParties = _context.PersonalContractingParties.Where(x => x.id == id)?.FirstOrDefault();
|
|
_context.PersonalContractingParties.Remove(personalContractingParties);
|
|
_context.SaveChanges();
|
|
return op.Succcedded(-1, "حذف با موفقیت انجام شد.");
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return op.Failed("حذف با خطا مواجه شد.");
|
|
}
|
|
|
|
}
|
|
public bool GetHasContract(long id)
|
|
{
|
|
return _context.InstitutionContractSet.Any(x =>x.IsActiveString== "true" && x.ContractingPartyId == id);
|
|
}
|
|
|
|
public OperationResult DeActiveAll(long id)
|
|
{
|
|
OperationResult result = new OperationResult();
|
|
using (var transaction = _context.Database.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
var personel = _context.PersonalContractingParties.FirstOrDefault(x => x.id == id);
|
|
personel.DeActive();
|
|
|
|
var employers = _context.Employers.Where(x => x.ContractingPartyId == id).ToList();
|
|
employers.ForEach(x=>x.DeActive());
|
|
|
|
var employerIds= employers.Select(x=>x.id).ToList();
|
|
var workshopIds=_context.WorkshopEmployers.Where(x=> employerIds.Contains(x.EmployerId)).Select(x=>x.WorkshopId).ToList();
|
|
var workshops = _context.Workshops.Where(x => workshopIds.Contains(x.id)).ToList();
|
|
workshops.ForEach(x => x.DeActive(x.ArchiveCode));
|
|
|
|
var contracts = _context.Contracts.Where(x => workshopIds.Contains(x.WorkshopIds)).ToList();
|
|
contracts.ForEach(x => x.DeActive());
|
|
|
|
var contractIds = contracts.Select(x => x.id).ToList();
|
|
var checkouts = _context.CheckoutSet.Where(x => contractIds.Contains(x.ContractId)).ToList();
|
|
checkouts.ForEach(x => x.DeActive());
|
|
|
|
_context.SaveChanges();
|
|
transaction.Commit();
|
|
result.Failed("DeActive");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
result.Failed("غیرفعال کردن طرف حساب با خطا مواجه شد");
|
|
transaction.Rollback();
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public OperationResult ActiveAll(long id)
|
|
{
|
|
OperationResult result = new OperationResult();
|
|
using (var transaction = _context.Database.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
var personel = _context.PersonalContractingParties.FirstOrDefault(x => x.id == id);
|
|
personel.Active();
|
|
|
|
var employers = _context.Employers.Where(x => x.ContractingPartyId == id).ToList();
|
|
employers.ForEach(x => x.Active());
|
|
|
|
var employerIds = employers.Select(x => x.id).ToList();
|
|
var workshopIds = _context.WorkshopEmployers.Where(x => employerIds.Contains(x.EmployerId)).Select(x => x.WorkshopId).ToList();
|
|
var workshops = _context.Workshops.Where(x => workshopIds.Contains(x.id)).ToList();
|
|
workshops.ForEach(x => x.Active(x.ArchiveCode));
|
|
|
|
var contracts = _context.Contracts.Where(x => workshopIds.Contains(x.WorkshopIds)).ToList();
|
|
contracts.ForEach(x => x.Active());
|
|
|
|
var contractIds = contracts.Select(x => x.id).ToList();
|
|
var checkouts = _context.CheckoutSet.Where(x => contractIds.Contains(x.ContractId)).ToList();
|
|
checkouts.ForEach(x => x.Active());
|
|
|
|
_context.SaveChanges();
|
|
transaction.Commit();
|
|
result.Succcedded();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
result.Failed("فعال کردن طرف حساب با خطا مواجه شد");
|
|
transaction.Rollback();
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public string IsBlockByEmployerId(long employerId)
|
|
{
|
|
var emp = _context.Employers.FirstOrDefault(x => x.id == employerId);
|
|
if (emp != null)
|
|
{
|
|
var res = _context.PersonalContractingParties.FirstOrDefault(x => x.id == emp.ContractingPartyId);
|
|
if (res != null)
|
|
return res.IsBlock;
|
|
|
|
return $"NotFound";
|
|
}
|
|
|
|
return $"NotFound";
|
|
}
|
|
|
|
#region ForClients
|
|
|
|
public ContractingPartyAndStatmentIdViewModel GetContractingpartyIdByAccountId(long accountId)
|
|
{
|
|
var contractingPartId = _context.ContractingPartyAccounts.Any(x => x.AccountId == accountId) ?
|
|
_context.ContractingPartyAccounts.FirstOrDefault(x => x.AccountId == accountId)!.PersonalContractingPartyId : 0;
|
|
var financialStatmentId = _context.FinancialStatments
|
|
.Any(x => x.ContractingPartyId == contractingPartId) ? _context.FinancialStatments
|
|
.FirstOrDefault(x => x.ContractingPartyId == contractingPartId)!.id : 0;
|
|
|
|
var res = new ContractingPartyAndStatmentIdViewModel()
|
|
{
|
|
ContractingPartyId = contractingPartId,
|
|
FinancialStatmentId = financialStatmentId,
|
|
};
|
|
|
|
return res;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
#endregion
|
|
|