using System.Collections.Generic; using _0_Framework.Application; using Company.Domain.FinancialStatmentAgg; using CompanyManagment.App.Contracts.FinancialStatment; using CompanyManagment.App.Contracts.FinancilTransaction; namespace CompanyManagment.Application; public class FinancialStatmentApplication : IFinancialStatmentApplication { private readonly IFinancialStatmentRepository _financialStatmentRepository; private readonly IFinancialTransactionApplication _financialTransactionApplication; public FinancialStatmentApplication(IFinancialStatmentRepository financialStatmentRepository, IFinancialTransactionApplication financialTransactionApplication) { _financialStatmentRepository = financialStatmentRepository; _financialTransactionApplication = financialTransactionApplication; } 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(); } var tDateGr = command.TdateFa.ToGeorgianDateTime(); 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 Search(FinancialStatmentSearchModel searchModel) { return _financialStatmentRepository.Search(searchModel); } public FinancialStatmentViewModel GetDetailsByContractingPartyId(long contractingPartyId) { return _financialStatmentRepository.GetDetailsByContractingPartyId(contractingPartyId); } }