244 lines
10 KiB
C#
244 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using _0_Framework.Application;
|
|
using Company.Domain.ContarctingPartyAgg;
|
|
using Company.Domain.FinancialInvoiceAgg;
|
|
using Company.Domain.FinancialStatmentAgg;
|
|
using CompanyManagment.App.Contracts.FinancialInvoice;
|
|
using CompanyManagment.App.Contracts.FinancialStatment;
|
|
using CompanyManagment.App.Contracts.FinancilTransaction;
|
|
using CompanyManagment.App.Contracts.SepehrPaymentGateway;
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class FinancialStatmentApplication : IFinancialStatmentApplication
|
|
{
|
|
private readonly IFinancialStatmentRepository _financialStatmentRepository;
|
|
private readonly IFinancialTransactionApplication _financialTransactionApplication;
|
|
private readonly IPersonalContractingPartyRepository _contractingPartyRepository;
|
|
private readonly IFinancialInvoiceRepository _financialInvoiceRepository;
|
|
private readonly ISepehrPaymentGatewayService _sepehrPaymentGatewayService;
|
|
|
|
public FinancialStatmentApplication(IFinancialStatmentRepository financialStatmentRepository,
|
|
IFinancialTransactionApplication financialTransactionApplication,
|
|
IPersonalContractingPartyRepository contractingPartyRepository,
|
|
IFinancialInvoiceRepository financialInvoiceRepository,
|
|
ISepehrPaymentGatewayService sepehrPaymentGatewayService)
|
|
{
|
|
_financialStatmentRepository = financialStatmentRepository;
|
|
_financialTransactionApplication = financialTransactionApplication;
|
|
_contractingPartyRepository = contractingPartyRepository;
|
|
_financialInvoiceRepository = financialInvoiceRepository;
|
|
_sepehrPaymentGatewayService = sepehrPaymentGatewayService;
|
|
}
|
|
|
|
public OperationResult CreateFromBankGateway(CreateFinancialStatment command)
|
|
{
|
|
var op = new OperationResult();
|
|
|
|
if (command.Creditor == 0)
|
|
return op.Failed("فیلد بدهکار خالیاست");
|
|
|
|
if (_financialStatmentRepository.Exists(x => x.ContractingPartyId == command.ContractingPartyId))
|
|
{
|
|
var financialStatment =
|
|
_financialStatmentRepository.GetDetailsByContractingPartyId(command.ContractingPartyId);
|
|
var transaction = new CreateFinancialTransaction()
|
|
{
|
|
FinancialStatementId = financialStatment.Id,
|
|
TdateGr = DateTime.Now,
|
|
TdateFa = DateTime.Now.ToFarsi(),
|
|
Description = command.Description,
|
|
Deptor = 0,
|
|
Creditor = command.Creditor,
|
|
TypeOfTransaction = command.TypeOfTransaction,
|
|
DescriptionOption = command.DescriptionOption
|
|
};
|
|
|
|
var createTransaction = _financialTransactionApplication.Create(transaction);
|
|
if (createTransaction.IsSuccedded)
|
|
return op.Succcedded();
|
|
return op.Failed("خطا در انجام عملیات");
|
|
}
|
|
else
|
|
{
|
|
var contractingPartyName = _contractingPartyRepository.GetFullName(command.ContractingPartyId);
|
|
var statement = new FinancialStatment(command.ContractingPartyId, contractingPartyName);
|
|
_financialStatmentRepository.Create(statement);
|
|
_financialStatmentRepository.SaveChanges();
|
|
|
|
var transaction = new CreateFinancialTransaction()
|
|
{
|
|
FinancialStatementId = statement.id,
|
|
TdateGr = DateTime.Now,
|
|
TdateFa = DateTime.Now.ToFarsi(),
|
|
Description = command.Description,
|
|
Deptor = 0,
|
|
Creditor = command.Creditor,
|
|
Balance = 0,
|
|
TypeOfTransaction = command.TypeOfTransaction,
|
|
DescriptionOption = command.DescriptionOption
|
|
};
|
|
var createTransaction = _financialTransactionApplication.Create(transaction);
|
|
if (createTransaction.IsSuccedded)
|
|
return op.Succcedded();
|
|
return op.Failed("خطا در انجام عملیات");
|
|
}
|
|
}
|
|
|
|
public OperationResult Create(CreateFinancialStatment command)
|
|
{
|
|
var op = new OperationResult();
|
|
|
|
double creditor = 0;
|
|
double debtor = 0;
|
|
if (command.TypeOfTransaction == "debt" && string.IsNullOrWhiteSpace(command.DeptorString))
|
|
return op.Failed("فیلد بدهکار خالیاست");
|
|
if (command.TypeOfTransaction == "credit" && string.IsNullOrWhiteSpace(command.CreditorString))
|
|
return op.Failed("فیلد بستانکار خالی است");
|
|
if (string.IsNullOrWhiteSpace(command.TdateFa))
|
|
return op.Failed("فیلد تاریخ خالی است");
|
|
|
|
if (command.TypeOfTransaction == "credit")
|
|
{
|
|
debtor = 0;
|
|
creditor = command.CreditorString.MoneyToDouble();
|
|
}
|
|
else if (command.TypeOfTransaction == "debt")
|
|
{
|
|
creditor = 0;
|
|
debtor = command.DeptorString.MoneyToDouble();
|
|
}
|
|
|
|
if (!command.TdateFa.TryToGeorgianDateTime(out var tDateGr))
|
|
{
|
|
return op.Failed("تاریخ وارد شده صحیح نمی باشد");
|
|
}
|
|
|
|
if (_financialStatmentRepository.Exists(x => x.ContractingPartyId == command.ContractingPartyId))
|
|
{
|
|
var financialStatment =
|
|
_financialStatmentRepository.GetDetailsByContractingPartyId(command.ContractingPartyId);
|
|
var transaction = new CreateFinancialTransaction()
|
|
{
|
|
FinancialStatementId = financialStatment.Id,
|
|
TdateGr = tDateGr,
|
|
TdateFa = command.TdateFa,
|
|
Description = command.Description,
|
|
Deptor = debtor,
|
|
Creditor = creditor,
|
|
TypeOfTransaction = command.TypeOfTransaction,
|
|
DescriptionOption = command.DescriptionOption
|
|
};
|
|
|
|
var createTransaction = _financialTransactionApplication.Create(transaction);
|
|
if (createTransaction.IsSuccedded)
|
|
return op.Succcedded();
|
|
return op.Failed("خطا در انجام عملیات");
|
|
}
|
|
else
|
|
{
|
|
var statement = new FinancialStatment(command.ContractingPartyId, command.ContractingPartyName);
|
|
_financialStatmentRepository.Create(statement);
|
|
_financialStatmentRepository.SaveChanges();
|
|
|
|
var transaction = new CreateFinancialTransaction()
|
|
{
|
|
FinancialStatementId = statement.id,
|
|
TdateGr = tDateGr,
|
|
TdateFa = command.TdateFa,
|
|
Description = command.Description,
|
|
Deptor = debtor,
|
|
Creditor = creditor,
|
|
Balance = 0,
|
|
TypeOfTransaction = command.TypeOfTransaction,
|
|
DescriptionOption = command.DescriptionOption
|
|
};
|
|
var createTransaction = _financialTransactionApplication.Create(transaction);
|
|
if (createTransaction.IsSuccedded)
|
|
return op.Succcedded();
|
|
return op.Failed("خطا در انجام عملیات");
|
|
}
|
|
}
|
|
|
|
|
|
public List<FinancialStatmentViewModel> Search(FinancialStatmentSearchModel searchModel)
|
|
{
|
|
return _financialStatmentRepository.Search(searchModel);
|
|
}
|
|
|
|
public FinancialStatmentViewModel GetDetailsByContractingPartyId(long contractingPartyId)
|
|
{
|
|
return _financialStatmentRepository.GetDetailsByContractingPartyId(contractingPartyId);
|
|
}
|
|
|
|
public async Task<ClientFinancialStatementViewModel> GetClientFinancialStatement(
|
|
FinancialStatementSearchModel searchModel, long accountId)
|
|
{
|
|
return await _financialStatmentRepository.GetClientFinancialStatement(accountId, searchModel);
|
|
}
|
|
|
|
public async Task<OperationResult<ClientFinancialStatementViewModel>> GetDetailsByPublicId(
|
|
string publicId)
|
|
{
|
|
return await _financialStatmentRepository.GetDetailsByPublicId(publicId);
|
|
}
|
|
|
|
public Task<GetFinancialStatementBalanceAmount> GetBalanceAmount(long id)
|
|
{
|
|
return _financialStatmentRepository.GetBalanceAmount(id);
|
|
}
|
|
|
|
public Task<double> GetClientDebtAmount(long accountId)
|
|
{
|
|
return _financialStatmentRepository.GetClientDebtAmount(accountId);
|
|
}
|
|
|
|
public async Task<FinancialStatmentDetailsByContractingPartyViewModel> GetDetailsByContractingParty(
|
|
long contractingPartyId, FinancialStatementSearchModel searchModel)
|
|
{
|
|
return await _financialStatmentRepository.GetDetailsByContractingParty(contractingPartyId, searchModel);
|
|
}
|
|
|
|
public async Task<OperationResult<CreateSepehrPaymentGatewayResponse>> CreatePaymentGateWayAndCreateInvoice(
|
|
CreateFinancialPayRequest request, string gateWayCallBackUrl)
|
|
{
|
|
var op = new OperationResult<CreateSepehrPaymentGatewayResponse>();
|
|
// گام 1: دریافت موجودی حساب
|
|
var balanceAmount = await GetBalanceAmount(request.Id);
|
|
if (balanceAmount.Amount <= 0)
|
|
{
|
|
return op.Failed("موجودی حساب شما صفر است");
|
|
}
|
|
// گام 2: ایجاد درگاه پرداخت سپهر
|
|
|
|
var financialInvoice = await _financialInvoiceRepository
|
|
.GetUnPaidFinancialInvoiceByContractingPartyIdAndAmount(balanceAmount.ContractingPartyId,
|
|
balanceAmount.Amount);
|
|
|
|
if (financialInvoice == null)
|
|
{
|
|
financialInvoice = new FinancialInvoice(balanceAmount.Amount, balanceAmount.ContractingPartyId,
|
|
"پرداخت بدهی صورت حساب مالی");
|
|
|
|
var items = new FinancialInvoiceItem("پرداخت بدهی صورت حساب مالی", balanceAmount.Amount,
|
|
financialInvoice.id, FinancialInvoiceItemType.PreviousDebt, 0);
|
|
financialInvoice.AddItem(items);
|
|
await _financialInvoiceRepository.CreateAsync(financialInvoice);
|
|
await _financialInvoiceRepository.SaveChangesAsync();
|
|
}
|
|
|
|
var gatewayResult = await _sepehrPaymentGatewayService.CreateSepehrPaymentGateway(
|
|
amount: balanceAmount.Amount,
|
|
contractingPartyId: balanceAmount.ContractingPartyId,
|
|
frontCallbackUrl: request.BaseUrl,
|
|
gatewayCallbackUrl: gateWayCallBackUrl,
|
|
financialInvoiceId: financialInvoice.id,
|
|
extraData: null);
|
|
|
|
return gatewayResult;
|
|
}
|
|
} |