149 lines
6.2 KiB
C#
149 lines
6.2 KiB
C#
using _0_Framework.Application;
|
|
using _0_Framework.Exceptions;
|
|
using _0_Framework.InfraStructure;
|
|
using Company.Domain.FinancialStatmentAgg;
|
|
using CompanyManagment.App.Contracts.FinancialStatment;
|
|
using CompanyManagment.App.Contracts.FinancilTransaction;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using AccountManagement.Application.Contracts.SubAccount;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class FinancialStatmentRepository : RepositoryBase<long, FinancialStatment>, IFinancialStatmentRepository
|
|
|
|
{
|
|
private readonly CompanyContext _context;
|
|
|
|
public FinancialStatmentRepository(CompanyContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
|
|
public FinancialStatmentViewModel GetDetailsByContractingPartyId(long contractingPartyId)
|
|
{
|
|
var res = _context.FinancialStatments.Include(x => x.FinancialTransactionList)
|
|
.FirstOrDefault(x => x.ContractingPartyId == contractingPartyId);
|
|
if (res == null)
|
|
{
|
|
return new FinancialStatmentViewModel();
|
|
}
|
|
else
|
|
{
|
|
|
|
return new FinancialStatmentViewModel()
|
|
{
|
|
Id = res.id,
|
|
ContractingPartyId = res.ContractingPartyId,
|
|
ContractingPartyName = res.ContractingPartyName,
|
|
FinancialTransactionViewModels = res.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
|
|
}).OrderBy(t => t.TdateGr).ToList(),
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
public List<FinancialStatmentViewModel> Search(FinancialStatmentSearchModel searchModel)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<ClientFinancialStatementViewModel> GetClientFinancialStatement(long accountId,
|
|
ClientFinancialStatementSearchModel searchModel)
|
|
{
|
|
var contractingPartyId = _context.ContractingPartyAccounts.Any(x => x.AccountId == accountId) ?
|
|
_context.ContractingPartyAccounts.FirstOrDefault(x => x.AccountId == accountId)!.PersonalContractingPartyId : 0;
|
|
|
|
if (contractingPartyId == 0)
|
|
throw new BadRequestException("طرف حساب مورد نظر یافت نشد");
|
|
|
|
var resStatement = await _context.FinancialStatments.Include(x=>x.FinancialTransactionList)
|
|
.FirstOrDefaultAsync(x => x.ContractingPartyId == contractingPartyId);
|
|
|
|
if (resStatement == null)
|
|
throw new BadRequestException("صورت حساب مالی شخص یافت نشد");
|
|
|
|
var resTransaction = resStatement.FinancialTransactionList.ToList();
|
|
|
|
#region Search
|
|
|
|
//if (searchModel.FromAmount > 0 || searchModel.ToAmount > 0)
|
|
//{
|
|
// if (searchModel.FromAmount > 0 && searchModel.ToAmount > 0)
|
|
// {
|
|
// resTransaction = resTransaction.Where(x => x.>= searchModel.FromAmount && x.Amount <= searchModel);
|
|
// }
|
|
// else if (searchModel.FromAmount > 0)
|
|
// {
|
|
// query = query.Where(x => x.Amount >= searchModel.FromAmount);
|
|
// }
|
|
// else if (searchModel.ToAmount > 0)
|
|
// {
|
|
// query = query.Where(x => x.Amount <= searchModel.ToAmount);
|
|
// }
|
|
//}
|
|
if (!string.IsNullOrWhiteSpace(searchModel.FromDate) && !string.IsNullOrWhiteSpace(searchModel.ToDate))
|
|
{
|
|
if (searchModel.FromDate.TryToGeorgianDateTime(out var fromDate) == false)
|
|
throw new BadRequestException("تاریخ وارد شده نامعتبر است");
|
|
|
|
if (searchModel.FromDate.TryToGeorgianDateTime(out var toDate) == false)
|
|
throw new BadRequestException("تاریخ وارد شده نامعتبر است");
|
|
|
|
resTransaction = resTransaction.Where(x => x.TdateGr >= fromDate && x.TdateGr <= toDate).ToList();
|
|
}
|
|
|
|
if (searchModel.Type != null)
|
|
{
|
|
var type = searchModel.Type switch
|
|
{
|
|
FinancialTransactionType.Credit => "credit",
|
|
FinancialTransactionType.Debt => "debt"
|
|
};
|
|
|
|
resTransaction = resTransaction.Where(x => x.TypeOfTransaction == type).ToList();
|
|
}
|
|
#endregion
|
|
|
|
return new ClientFinancialStatementViewModel()
|
|
{
|
|
Id = resStatement.id,
|
|
TotalAmountPayable = resStatement.FinancialTransactionList.Sum(x=>x.Deptor) - resStatement.FinancialTransactionList.Sum(x=>x.Creditor),
|
|
TotalCredit = resTransaction.Sum(x=>x.Creditor),
|
|
TotalDebt = resTransaction.Sum(x=>x.Deptor),
|
|
Transactions = resTransaction.Select(t => new ClientFinancialTransactionViewModel()
|
|
{
|
|
DateTimeGr = t.TdateGr,
|
|
DateFa = t.TdateGr.ToFarsi(),
|
|
TimeFa = $"{t.TdateGr:HH:mm}",
|
|
Description = t.DescriptionOption + " " + t.Description,
|
|
Debtor = t.Deptor,
|
|
Creditor = t.Creditor,
|
|
Balance = t.Balance,
|
|
Type = t.TypeOfTransaction =="debt"? FinancialTransactionType.Debt :FinancialTransactionType.Credit,
|
|
TypeStr = t.TypeOfTransaction =="debt"? "ایجاد درآمد" : "دریافت درآمد"
|
|
}).OrderByDescending(t => t.DateTimeGr).ToList(),
|
|
};
|
|
|
|
}
|
|
} |