107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using _0_Framework.Application;
|
|
using _0_Framework.InfraStructure;
|
|
using Company.Domain.FinancialStatmentAgg;
|
|
using Company.Domain.FinancialTransactionAgg;
|
|
using CompanyManagment.App.Contracts.FinancilTransaction;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class FinancialTransactionRepository : RepositoryBase<long, FinancialTransaction>, IFinancialTransactionRepository
|
|
|
|
{
|
|
private readonly CompanyContext _context;
|
|
public FinancialTransactionRepository(CompanyContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public EditFinancialTransaction GetDetails(long id)
|
|
{
|
|
return _context.FinancialTransactions.Select(x => new EditFinancialTransaction()
|
|
{
|
|
Id = x.id,
|
|
TdateFa = x.TdateFa,
|
|
TdateGr = x.TdateGr,
|
|
Description = x.Description,
|
|
Deptor = x.Deptor,
|
|
DeptorString = x.Deptor.ToMoney(),
|
|
Creditor = x.Creditor,
|
|
CreditorString = x.Creditor.ToMoney(),
|
|
Balance = x.Balance,
|
|
MessageText = x.MessageText,
|
|
SentSms = x.SentSms,
|
|
SentSmsDateFa = x.SentSmsDateFa,
|
|
FinancialStatementId = x.FinancialStatementId,
|
|
TypeOfTransaction = x.TypeOfTransaction,
|
|
DescriptionOption = x.DescriptionOption.Trim(),
|
|
|
|
|
|
}).FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
public void RemoveFinancialTransaction(long id)
|
|
{
|
|
var res = _context.FinancialTransactions.FirstOrDefault(x => x.id == id);
|
|
if (res != null)
|
|
{
|
|
_context.FinancialTransactions.Remove(res);
|
|
_context.SaveChanges();
|
|
}
|
|
|
|
}
|
|
|
|
public OperationResult CreateDebtFromExcel(long contractingPartyId, string transactionDate, double debt, string description)
|
|
{
|
|
var op = new OperationResult();
|
|
var now = DateTime.Now;
|
|
#region Validation
|
|
|
|
var contractingParty = _context.PersonalContractingParties.FirstOrDefault(x => x.id == contractingPartyId);
|
|
|
|
if (contractingParty == null)
|
|
return op.Failed("طرف حساب وجود ندارد");
|
|
|
|
var activeInstitutionContract = _context.InstitutionContractSet.FirstOrDefault(x => x.ContractingPartyId == contractingPartyId &&
|
|
x.IsActiveString == "true" && x.ContractStartGr <= now && x.ContractEndGr >= now);
|
|
|
|
if (activeInstitutionContract == null)
|
|
return op.Failed("قرارداد مالی فعال برای این طرف حساب وجود ندارد");
|
|
|
|
if (debt <= 0)
|
|
return op.Failed("مبلغ را وارد کنید");
|
|
|
|
#endregion
|
|
|
|
|
|
var descriptionOption = "بابت خرید استند حضور غیاب";
|
|
var financialStatment = _context.FinancialStatments.FirstOrDefault(x => x.ContractingPartyId == contractingPartyId);
|
|
if (financialStatment != null)
|
|
{
|
|
|
|
var transaction = new FinancialTransaction(financialStatment.id, now, now.ToFarsi(),
|
|
description,
|
|
"debt", descriptionOption, debt, 0, 0);
|
|
_context.FinancialTransactions.Add(transaction);
|
|
_context.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
else
|
|
{
|
|
var statement = new FinancialStatment(contractingPartyId, activeInstitutionContract.ContractingPartyName);
|
|
_context.FinancialStatments.Add(statement);
|
|
_context.SaveChanges();
|
|
|
|
var transaction = new FinancialTransaction(statement.id, now, now.ToFarsi(),
|
|
description,
|
|
"debt", descriptionOption, debt, 0, 0);
|
|
_context.FinancialTransactions.Add(transaction);
|
|
_context.SaveChanges();
|
|
|
|
return op.Succcedded();
|
|
}
|
|
}
|
|
} |