Various improvements.

This commit is contained in:
James Cole
2019-03-08 05:47:51 +01:00
parent e6d7c0ddbd
commit 431cf08401
11 changed files with 813 additions and 430 deletions

View File

@@ -96,42 +96,13 @@ class MigrateToGroups extends Command
$data = [ $data = [
// mandatory fields. // mandatory fields.
'type' => strtolower($journal->transactionType->type), 'type' => strtolower($journal->transactionType->type),
'date' => $journal->date, 'date' => $journal->date,
'user' => $journal->user_id, 'user' => $journal->user_id,
'description' => $journal->description,
// currency fields: // transactions:
'currency' => null, 'transactions' => [],
'currency_id' => null,
'currency_code' => null,
// all custom fields:
'internal_reference' => $this->journalRepository->getMetaField($journal, 'internal-reference'),
'sepa-cc' => $this->journalRepository->getMetaField($journal, 'sepa-cc'),
'sepa-ct-op' => $this->journalRepository->getMetaField($journal, 'sepa-ct-op'),
'sepa-ct-id' => $this->journalRepository->getMetaField($journal, 'sepa-ct-id'),
'sepa-db' => $this->journalRepository->getMetaField($journal, 'sepa-db'),
'sepa-country' => $this->journalRepository->getMetaField($journal, 'sepa-country'),
'sepa-ep' => $this->journalRepository->getMetaField($journal, 'sepa-ep'),
'sepa-ci' => $this->journalRepository->getMetaField($journal, 'sepa-ci'),
'sepa-batch-id' => $this->journalRepository->getMetaField($journal, 'sepa-batch-id'),
'interest_date' => $this->journalRepository->getMetaDateString($journal, 'interest_date'),
'book_date' => $this->journalRepository->getMetaDateString($journal, 'book_date'),
'process_date' => $this->journalRepository->getMetaDateString($journal, 'process_date'),
'due_date' => $this->journalRepository->getMetaDateString($journal, 'due_date'),
'payment_date' => $this->journalRepository->getMetaDateString($journal, 'payment_date'),
'invoice_date' => $this->journalRepository->getMetaDateString($journal, 'invoice_date'),
'external_id' => $this->journalRepository->getMetaField($journal, 'external-id'),
'original-source' => $this->journalRepository->getMetaField($journal, 'original-source'),
// journal data:
'description' => $journal->description,
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => $journal->bill_id,
'bill_name' => null,
'tags' => null,
'notes' => null,
'transactions' => [],
]; ];
@@ -157,40 +128,88 @@ class MigrateToGroups extends Command
return; return;
} }
$tArray = [ $tArray = [
'notes' => $this->journalRepository->getNoteText($journal),
'tags' => $journal->tags->pluck('tag')->toArray(), // currency and foreign currency
'currency' => null, 'currency' => null,
'currency_id' => $transaction->transaction_currency_id, 'currency_id' => $transaction->transaction_currency_id,
'currency_code' => null, 'currency_code' => null,
'description' => $transaction->description, 'foreign_currency' => null,
'amount' => $transaction->amount,
'budget' => null,
'budget_id' => $budgetId,
'budget_name' => null,
'category' => null,
'category_id' => $categoryId,
'category_name' => null,
'source' => null,
'source_id' => $opposing->account_id,
'source_name' => null,
'destination' => null,
'destination_id' => $transaction->account_id,
'destination_name' => null,
'foreign_currency' => null,
'foreign_currency_id' => $transaction->foreign_currency_id, 'foreign_currency_id' => $transaction->foreign_currency_id,
'foreign_currency_code' => null, 'foreign_currency_code' => null,
// amount and foreign amount
'amount' => $transaction->amount,
'foreign_amount' => $transaction->foreign_amount, 'foreign_amount' => $transaction->foreign_amount,
// description
'description' => $transaction->description,
// source
'source' => null,
'source_id' => $opposing->account_id,
'source_name' => null,
// destination
'destination' => null,
'destination_id' => $transaction->account_id,
'destination_name' => null,
// budget
'budget' => null,
'budget_id' => $budgetId,
'budget_name' => null,
// category
'category' => null,
'category_id' => $categoryId,
'category_name' => null,
// piggy bank (if transfer)
'piggy_bank' => null,
'piggy_bank_id' => null,
'piggy_bank_name' => null,
// bill (if withdrawal)
'bill' => null,
'bill_id' => $journal->bill_id,
'bill_name' => null,
// some other interesting properties
'reconciled' => false, 'reconciled' => false,
'notes' => $this->journalRepository->getNoteText($journal),
'tags' => $journal->tags->pluck('tag')->toArray(),
// all custom fields:
'internal_reference' => $this->journalRepository->getMetaField($journal, 'internal-reference'),
'sepa-cc' => $this->journalRepository->getMetaField($journal, 'sepa-cc'),
'sepa-ct-op' => $this->journalRepository->getMetaField($journal, 'sepa-ct-op'),
'sepa-ct-id' => $this->journalRepository->getMetaField($journal, 'sepa-ct-id'),
'sepa-db' => $this->journalRepository->getMetaField($journal, 'sepa-db'),
'sepa-country' => $this->journalRepository->getMetaField($journal, 'sepa-country'),
'sepa-ep' => $this->journalRepository->getMetaField($journal, 'sepa-ep'),
'sepa-ci' => $this->journalRepository->getMetaField($journal, 'sepa-ci'),
'sepa-batch-id' => $this->journalRepository->getMetaField($journal, 'sepa-batch-id'),
'interest_date' => $this->journalRepository->getMetaDate($journal, 'interest_date'),
'book_date' => $this->journalRepository->getMetaDate($journal, 'book_date'),
'process_date' => $this->journalRepository->getMetaDate($journal, 'process_date'),
'due_date' => $this->journalRepository->getMetaDate($journal, 'due_date'),
'payment_date' => $this->journalRepository->getMetaDate($journal, 'payment_date'),
'invoice_date' => $this->journalRepository->getMetaDate($journal, 'invoice_date'),
'external_id' => $this->journalRepository->getMetaField($journal, 'external-id'),
'original-source' => $this->journalRepository->getMetaField($journal, 'original-source'),
'recurrence_id' => $this->journalRepository->getMetaField($journal, 'recurrence_id'),
'bunq_payment_id' => $this->journalRepository->getMetaField($journal, 'bunq_payment_id'),
'importHash' => $this->journalRepository->getMetaField($journal, 'importHash'),
'importHashV2' => $this->journalRepository->getMetaField($journal, 'importHashV2'),
]; ];
$data['transactions'][] = $tArray; $data['transactions'][] = $tArray;
} }
$this->journalFactory->create($data); $result = $this->journalFactory->create($data);
// create a new transaction journal based on this particular transaction using the factory. // create a new transaction journal based on this particular transaction using the factory.
// delete the old transaction journal. // delete the old transaction journal.
//$journal->delete(); //$journal->delete();
Log::debug(sprintf('Migrated journal #%d into %s', $journal->id, implode(', ', $result->pluck('id')->toArray())));
} }
/** /**

View File

@@ -28,7 +28,6 @@ use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Services\Internal\Support\BillServiceTrait; use FireflyIII\Services\Internal\Support\BillServiceTrait;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection;
use Log; use Log;
/** /**
@@ -126,20 +125,9 @@ class BillFactory
*/ */
public function findByName(string $name): ?Bill public function findByName(string $name): ?Bill
{ {
/** @var Collection $collection */ $query = sprintf('%%%s%%', $name);
$collection = $this->user->bills()->get();
$return = null;
/** @var Bill $bill */
foreach ($collection as $bill) {
Log::debug(sprintf('"%s" vs. "%s"', $bill->name, $name));
if ($bill->name === $name) {
$return = $bill;
break;
}
}
Log::debug(sprintf('Bill::find("%s") by name returns null? %s', $name, var_export($return, true)));
return $return; return $this->user->bills()->where('name', 'LIKE', $query)->first();
} }
/** /**

View File

@@ -26,11 +26,13 @@ namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Services\Internal\Support\TransactionServiceTrait; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -40,11 +42,13 @@ use Log;
*/ */
class TransactionFactory class TransactionFactory
{ {
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var TransactionJournal */
private $journal;
/** @var User */ /** @var User */
private $user; private $user;
use TransactionServiceTrait;
/** /**
* Constructor. * Constructor.
*/ */
@@ -53,108 +57,151 @@ class TransactionFactory
if ('testing' === config('app.env')) { if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
} }
$this->accountRepository = app(AccountRepositoryInterface::class);
} }
/** //use TransactionServiceTrait;
* @param array $data
*
* @return Transaction
*/
public function create(array $data): ?Transaction
{
$data['foreign_amount'] = '' === (string)$data['foreign_amount'] ? null : $data['foreign_amount'];
Log::debug(sprintf('Create transaction for account #%d ("%s") with amount %s', $data['account']->id, $data['account']->name, $data['amount']));
return Transaction::create( /**
* @param Account $account
* @param TransactionCurrency $currency
* @param string $amount
*
* @return Transaction|null
*/
public function create(Account $account, TransactionCurrency $currency, string $amount): ?Transaction
{
$result = Transaction::create(
[ [
'reconciled' => $data['reconciled'], 'reconciled' => false,
'account_id' => $data['account']->id, 'account_id' => $account->id,
'transaction_journal_id' => $data['transaction_journal']->id, 'transaction_journal_id' => $this->journal->id,
'description' => $data['description'], 'description' => null,
'transaction_currency_id' => $data['currency']->id, 'transaction_currency_id' => $currency->id,
'amount' => $data['amount'], 'amount' => $amount,
'foreign_amount' => $data['foreign_amount'], 'foreign_amount' => null,
'foreign_currency_id' => $data['foreign_currency'] ? $data['foreign_currency']->id : null, 'foreign_currency_id' => null,
'identifier' => 0, 'identifier' => 0,
] ]
); );
if (null !== $result) {
Log::debug(
sprintf(
'Created transaction #%d (%s %s), part of journal #%d', $result->id,
$currency->code, $amount, $this->journal->id
)
);
}
return $result;
} }
/** /**
* Create a pair of transactions based on the data given in the array. * @param TransactionCurrency $currency
* * @param array $data
* @param TransactionJournal $journal
* @param array $data
* *
* @return Collection * @return Collection
* @throws FireflyException * @throws FireflyException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/ */
public function createPair(TransactionJournal $journal, array $data): Collection public function createPair(TransactionCurrency $currency, array $data): Collection
{ {
Log::debug('Start of TransactionFactory::createPair()' ); $sourceAccount = $this->getAccount('source', $data['source'], $data['source_id'], $data['source_name']);
$destinationAccount = $this->getAccount('destination', $data['destination'], $data['destination_id'], $data['destination_name']);
$amount = $this->getAmount($data['amount']);
// type of source account and destination account depends on journal type: $one = $this->create($sourceAccount, $currency, app('steam')->negative($amount));
$sourceType = $this->accountType($journal, 'source'); $two = $this->create($destinationAccount, $currency, app('steam')->positive($amount));
$destinationType = $this->accountType($journal, 'destination');
Log::debug(sprintf('Journal is a %s.', $journal->transactionType->type)); return new Collection([$one, $two]);
Log::debug(sprintf('Expect source account to be of type "%s"', $sourceType));
Log::debug(sprintf('Expect source destination to be of type "%s"', $destinationType));
// find source and destination account: // Log::debug('Start of TransactionFactory::createPair()' );
$sourceAccount = $this->findAccount($sourceType, (int)$data['source_id'], $data['source_name']); //
$destinationAccount = $this->findAccount($destinationType, (int)$data['destination_id'], $data['destination_name']); // // type of source account and destination account depends on journal type:
// $sourceType = $this->accountType($journal, 'source');
// $destinationType = $this->accountType($journal, 'destination');
//
// Log::debug(sprintf('Journal is a %s.', $journal->transactionType->type));
// Log::debug(sprintf('Expect source account to be of type "%s"', $sourceType));
// Log::debug(sprintf('Expect source destination to be of type "%s"', $destinationType));
//
// // find source and destination account:
// $sourceAccount = $this->findAccount($sourceType, $data['source'], (int)$data['source_id'], $data['source_name']);
// $destinationAccount = $this->findAccount($destinationType, $data['destination'], (int)$data['destination_id'], $data['destination_name']);
//
// if (null === $sourceAccount || null === $destinationAccount) {
// $debugData = $data;
// $debugData['source_type'] = $sourceType;
// $debugData['dest_type'] = $destinationType;
// Log::error('Info about source/dest:', $debugData);
// throw new FireflyException('Could not determine source or destination account.');
// }
//
// Log::debug(sprintf('Source type is "%s", destination type is "%s"', $sourceAccount->accountType->type, $destinationAccount->accountType->type));
//
// // based on the source type, destination type and transaction type, the system can start throwing FireflyExceptions.
// $this->validateTransaction($sourceAccount->accountType->type, $destinationAccount->accountType->type, $journal->transactionType->type);
// $source = $this->create(
// [
// 'description' => null,
// 'amount' => app('steam')->negative((string)$data['amount']),
// 'foreign_amount' => $data['foreign_amount'] ? app('steam')->negative((string)$data['foreign_amount']): null,
// 'currency' => $data['currency'],
// 'foreign_currency' => $data['foreign_currency'],
// 'account' => $sourceAccount,
// 'transaction_journal' => $journal,
// 'reconciled' => $data['reconciled'],
// ]
// );
// $dest = $this->create(
// [
// 'description' => null,
// 'amount' => app('steam')->positive((string)$data['amount']),
// 'foreign_amount' => $data['foreign_amount'] ? app('steam')->positive((string)$data['foreign_amount']): null,
// 'currency' => $data['currency'],
// 'foreign_currency' => $data['foreign_currency'],
// 'account' => $destinationAccount,
// 'transaction_journal' => $journal,
// 'reconciled' => $data['reconciled'],
// ]
// );
// if (null === $source || null === $dest) {
// throw new FireflyException('Could not create transactions.'); // @codeCoverageIgnore
// }
//
// return new Collection([$source, $dest]);
}
if (null === $sourceAccount || null === $destinationAccount) { // /**
$debugData = $data; // * @param array $data
$debugData['source_type'] = $sourceType; // *
$debugData['dest_type'] = $destinationType; // * @return Transaction
Log::error('Info about source/dest:', $debugData); // */
throw new FireflyException('Could not determine source or destination account.'); // public function create(array $data): ?Transaction
} // {
// $data['foreign_amount'] = '' === (string)$data['foreign_amount'] ? null : $data['foreign_amount'];
// Log::debug(sprintf('Create transaction for account #%d ("%s") with amount %s', $data['account']->id, $data['account']->name, $data['amount']));
//
// return Transaction::create(
// [
// 'reconciled' => $data['reconciled'],
// 'account_id' => $data['account']->id,
// 'transaction_journal_id' => $data['transaction_journal']->id,
// 'description' => $data['description'],
// 'transaction_currency_id' => $data['currency']->id,
// 'amount' => $data['amount'],
// 'foreign_amount' => $data['foreign_amount'],
// 'foreign_currency_id' => $data['foreign_currency'] ? $data['foreign_currency']->id : null,
// 'identifier' => 0,
// ]
// );
// }
Log::debug(sprintf('Source type is "%s", destination type is "%s"', $sourceAccount->accountType->type, $destinationAccount->accountType->type)); /**
* @param TransactionJournal $journal
// based on the source type, destination type and transaction type, the system can start throwing FireflyExceptions. */
$this->validateTransaction($sourceAccount->accountType->type, $destinationAccount->accountType->type, $journal->transactionType->type); public function setJournal(TransactionJournal $journal): void
$source = $this->create( {
[ $this->journal = $journal;
'description' => $data['description'],
'amount' => app('steam')->negative((string)$data['amount']),
'foreign_amount' => null,
'currency' => $data['currency'],
'foreign_currency' => $data['foreign_currency'],
'account' => $sourceAccount,
'transaction_journal' => $journal,
'reconciled' => $data['reconciled'],
]
);
$dest = $this->create(
[
'description' => $data['description'],
'amount' => app('steam')->positive((string)$data['amount']),
'foreign_amount' => null,
'currency' => $data['currency'],
'foreign_currency' => $data['foreign_currency'],
'account' => $destinationAccount,
'transaction_journal' => $journal,
'reconciled' => $data['reconciled'],
]
);
if (null === $source || null === $dest) {
throw new FireflyException('Could not create transactions.'); // @codeCoverageIgnore
}
// set foreign amount:
if (null !== $data['foreign_amount']) {
$this->setForeignAmount($source, app('steam')->negative((string)$data['foreign_amount']));
$this->setForeignAmount($dest, app('steam')->positive((string)$data['foreign_amount']));
}
return new Collection([$source, $dest]);
} }
/** /**
@@ -163,30 +210,142 @@ class TransactionFactory
public function setUser(User $user): void public function setUser(User $user): void
{ {
$this->user = $user; $this->user = $user;
$this->accountRepository->setUser($user);
} }
/** /**
* @param string $sourceType * @param string $direction
* @param string $destinationType * @param Account|null $source
* @param string $transactionType * @param int|null $sourceId
* @param string|null $sourceName
* *
* @return Account
* @throws FireflyException * @throws FireflyException
*/ */
private function validateTransaction(string $sourceType, string $destinationType, string $transactionType): void private function getAccount(string $direction, ?Account $source, ?int $sourceId, ?string $sourceName): Account
{ {
// throw big fat error when source type === dest type and it's not a transfer or reconciliation. // expected type of source account, in order of preference
if ($sourceType === $destinationType && $transactionType !== TransactionType::TRANSFER) { $array = [
throw new FireflyException(sprintf('Source and destination account cannot be both of the type "%s"', $destinationType)); 'source' => [
TransactionType::WITHDRAWAL => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
TransactionType::DEPOSIT => [AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
TransactionType::TRANSFER => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
TransactionType::OPENING_BALANCE => [AccountType::INITIAL_BALANCE, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,
AccountType::MORTGAGE],
TransactionType::RECONCILIATION => [AccountType::RECONCILIATION, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,
AccountType::MORTGAGE],
],
'destination' => [
TransactionType::WITHDRAWAL => [AccountType::EXPENSE, AccountType::ASSET],
TransactionType::DEPOSIT => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
TransactionType::TRANSFER => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
TransactionType::OPENING_BALANCE => [AccountType::INITIAL_BALANCE, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,
AccountType::MORTGAGE],
TransactionType::RECONCILIATION => [AccountType::RECONCILIATION, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,
AccountType::MORTGAGE],
],
];
$expectedTypes = $array[$direction];
unset($array);
// and now try to find it, based on the type of transaction.
$transactionType = $this->journal->transactionType->type;
Log::debug(
sprintf(
'Based on the fact that the transaction is a %s, the %s account should be in %s', $transactionType, $direction,
implode(', ', $expectedTypes[$transactionType])
)
);
// first attempt, check the "source" object.
if (null !== $source && $source->user_id === $this->user->id && \in_array($source->accountType->type, $expectedTypes[$transactionType], true)) {
Log::debug(sprintf('Found "account" object for %s: #%d, %s', $direction, $source->id, $source->name));
return $source;
} }
// source must be in this list AND dest must be in this list:
$list = [AccountType::DEFAULT, AccountType::ASSET, AccountType::CREDITCARD, AccountType::CASH, AccountType::DEBT, AccountType::MORTGAGE, // second attempt, find by ID.
AccountType::LOAN, AccountType::MORTGAGE]; if (null !== $sourceId) {
if ( $source = $this->accountRepository->findNull($sourceId);
!\in_array($sourceType, $list, true) if (null !== $source && \in_array($source->accountType->type, $expectedTypes[$transactionType], true)) {
&& !\in_array($destinationType, $list, true)) { Log::debug(sprintf('Found "account_id" object for %s: #%d, %s', $direction, $source->id, $source->name));
throw new FireflyException(sprintf('At least one of the accounts must be an asset account (%s, %s).', $sourceType, $destinationType));
return $source;
}
} }
// third attempt, find by name.
if (null !== $sourceName) {
// find by preferred type.
$source = $this->accountRepository->findByName($sourceName, [$expectedTypes[$transactionType][0]]);
// or any type.
$source = $source ?? $this->accountRepository->findByName($sourceName, $expectedTypes[$transactionType]);
if (null !== $source) {
Log::debug(sprintf('Found "account_name" object for %s: #%d, %s', $direction, $source->id, $source->name));
return $source;
}
}
// final attempt, create it.
$preferredType = $expectedTypes[$transactionType][0];
if (AccountType::ASSET === $preferredType) {
throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with ID #%d or name "%s".', $sourceId, $sourceName));
}
return $this->accountRepository->store(
[
'account_type_id' => null,
'accountType' => $preferredType,
'name' => $sourceName,
'active' => true,
'iban' => null,
]
);
} }
/**
* @param string $amount
*
* @return string
* @throws FireflyException
*/
private function getAmount(string $amount): string
{
if ('' === $amount) {
throw new FireflyException(sprintf('The amount cannot be an empty string: "%s"', $amount));
}
if (0 === bccomp('0', $amount)) {
throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount));
}
return $amount;
}
//
// /**
// * @param string $sourceType
// * @param string $destinationType
// * @param string $transactionType
// *
// * @throws FireflyException
// */
// private function validateTransaction(string $sourceType, string $destinationType, string $transactionType): void
// {
// // throw big fat error when source type === dest type and it's not a transfer or reconciliation.
// if ($sourceType === $destinationType && $transactionType !== TransactionType::TRANSFER) {
// throw new FireflyException(sprintf('Source and destination account cannot be both of the type "%s"', $destinationType));
// }
// // source must be in this list AND dest must be in this list:
// $list = [AccountType::DEFAULT, AccountType::ASSET, AccountType::CREDITCARD, AccountType::CASH, AccountType::DEBT, AccountType::MORTGAGE,
// AccountType::LOAN, AccountType::MORTGAGE];
// if (
// !\in_array($sourceType, $list, true)
// && !\in_array($destinationType, $list, true)) {
// throw new FireflyException(sprintf('At least one of the accounts must be an asset account (%s, %s).', $sourceType, $destinationType));
// }
// }
} }

View File

@@ -24,12 +24,11 @@ declare(strict_types=1);
namespace FireflyIII\Factory; namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException; use Carbon\Carbon;
use FireflyIII\Models\TransactionCurrency; use Exception;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Services\Internal\Support\JournalServiceTrait; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Services\Internal\Support\TransactionTypeTrait; use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -39,23 +38,36 @@ use Log;
*/ */
class TransactionJournalFactory class TransactionJournalFactory
{ {
private $fields; /** @var CurrencyRepositoryInterface */
private $currencyRepository;
/** @var TransactionFactory */
private $transactionFactory;
/** @var TransactionTypeRepositoryInterface */
private $typeRepository;
// private $fields;
/** @var User The user */ /** @var User The user */
private $user; private $user;
use JournalServiceTrait, TransactionTypeTrait; //
// use JournalServiceTrait, TransactionTypeTrait;
/** /**
* Constructor. * Constructor.
*/ */
public function __construct() public function __construct()
{ {
$this->fields = ['sepa-cc', 'sepa-ct-op', 'sepa-ct-id', 'sepa-db', 'sepa-country', 'sepa-ep', 'sepa-ci', 'interest_date', 'book_date', 'process_date', // $this->fields = ['sepa-cc', 'sepa-ct-op', 'sepa-ct-id', 'sepa-db', 'sepa-country', 'sepa-ep', 'sepa-ci', 'interest_date', 'book_date', 'process_date',
'due_date', 'recurrence_id', 'payment_date', 'invoice_date', 'internal_reference', 'bunq_payment_id', 'importHash', 'importHashV2', // 'due_date', 'recurrence_id', 'payment_date', 'invoice_date', 'internal_reference', 'bunq_payment_id', 'importHash', 'importHashV2',
'external_id', 'sepa-batch-id', 'original-source']; // 'external_id', 'sepa-batch-id', 'original-source'];
if ('testing' === config('app.env')) { if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
} }
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
$this->typeRepository = app(TransactionTypeRepositoryInterface::class);
$this->transactionFactory = app(TransactionFactory::class);
} }
/** /**
@@ -64,46 +76,31 @@ class TransactionJournalFactory
* @param array $data * @param array $data
* *
* @return Collection * @return Collection
* @throws FireflyException * @throws Exception
*/ */
public function create(array $data): Collection public function create(array $data): Collection
{ {
Log::debug('Start of TransactionJournalFactory::create()'); Log::debug('Start of TransactionJournalFactory::create()');
$collection = new Collection;
$factory = app(TransactionFactory::class); $transactions = $data['transactions'] ?? [];
$journals = new Collection; $type = $this->typeRepository->findTransactionType(null, $data['type']);
$carbon = $data['date']; $description = app('steam')->cleanString($data['description']);
$type = $this->findTransactionType($data['type']); $carbon = $data['date'] ?? new Carbon;
$description = app('steam')->cleanString($data['description']);
$carbon->setTimezone(config('app.timezone')); $carbon->setTimezone(config('app.timezone'));
$factory->setUser($this->user);
Log::debug(sprintf('New journal(group): %s with description "%s"', $type->type, $description)); /** @var array $transaction */
foreach ($transactions as $transaction) {
// loop each transaction. /** Get basic fields */
/** $currency = $this->currencyRepository->findCurrency($transaction['currency'], $transaction['currency_id'], $transaction['currency_code']);
* @var int $index
* @var array $transactionData
*/
foreach ($data['transactions'] as $index => $transactionData) {
Log::debug(sprintf('Now at journal #%d from %d', $index + 1, count($data['transactions'])));
// catch to stop empty amounts: /** Create a basic journal. */
if ('' === (string)$transactionData['amount'] || 0.0 === (float)$transactionData['amount']) {
continue;
}
// currency & foreign currency
$transactionData['currency'] = $this->getCurrency($data, $index);
$transactionData['foreign_currency'] = $this->getForeignCurrency($data, $index);
// store basic journal first.
$journal = TransactionJournal::create( $journal = TransactionJournal::create(
[ [
'user_id' => $data['user'], 'user_id' => $this->user->id,
'transaction_type_id' => $type->id, 'transaction_type_id' => $type->id,
'bill_id' => null, 'bill_id' => null,
'transaction_currency_id' => $transactionData['currency']->id, 'transaction_currency_id' => $currency->id,
'description' => $description, 'description' => $description,
'date' => $carbon->format('Y-m-d H:i:s'), 'date' => $carbon->format('Y-m-d H:i:s'),
'order' => 0, 'order' => 0,
@@ -111,65 +108,128 @@ class TransactionJournalFactory
'completed' => 0, 'completed' => 0,
] ]
); );
Log::debug(sprintf('Stored journal under ID #%d', $journal->id));
// store transactions for this journal: /** Create two transactions. */
$factory->createPair($journal, $transactionData); $this->transactionFactory->setJournal($journal);
$children = $this->transactionFactory->createPair($currency, $transaction);
// save journal:
$journal->completed = true;
$journal->save();
// // link bill TODO
// $this->connectBill($journal, $data);
//
// // link piggy bank (if transfer) TODO
// $this->connectPiggyBank($journal, $data);
//
// // link tags: TODO
// $this->connectTags($journal, $transactionData);
//
// // store note: TODO
// $this->storeNote($journal, $transactionData['notes']);
//
// if ($journal->transactionType->type !== TransactionType::WITHDRAWAL) {
// $transactionData['budget_id'] = null;
// $transactionData['budget_name'] = null;
// }
// // save budget TODO
// $budget = $this->findBudget($data['budget_id'], $data['budget_name']);
// $this->setBudget($journal, $budget);
//
// // set category TODO
// $category = $this->findCategory($data['category_id'], $data['category_name']);
// $this->setCategory($journal, $category);
//
// // store meta data TODO
// foreach ($this->fields as $field) {
// $this->storeMeta($journal, $data, $field);
// }
// add to array
$journals->push($journal);
}
// create group if necessary $collection->push($journal);
if ($journals->count() > 1) { Log::debug(sprintf('Created journal #%d', $journal->id));
$group = new TransactionGroup;
$group->user()->associate($this->user);
$group->title = $description;
$group->save();
$group->transactionJournals()->saveMany($journals);
Log::debug(sprintf('More than one journal, created group #%d.', $group->id)); return $collection;
/** Create two basic transactions */
} }
Log::debug('End of TransactionJournalFactory::create()'); // /** @var TransactionFactory $factory */
// invalidate cache. // $factory = app(TransactionFactory::class);
app('preferences')->mark(); // $journals = new Collection;
// ;
return $journals; // $type = $this->findTransactionType($data['type']);
//
//
//
// $factory->setUser($this->user);
//
// Log::debug(sprintf('New journal(group): %s with description "%s"', $type->type, $description));
//
// // loop each transaction.
// /**
// * @var int $index
// * @var array $transactionData
// */
// foreach ($data['transactions'] as $index => $transactionData) {
// Log::debug(sprintf('Now at journal #%d from %d', $index + 1, count($data['transactions'])));
//
// // catch to stop empty amounts:
// if ('' === (string)$transactionData['amount'] || 0.0 === (float)$transactionData['amount']) {
// continue;
// }
// // currency & foreign currency
// $transactionData['currency'] = $this->getCurrency($transactionData, $index);
// $transactionData['foreign_currency'] = $this->getForeignCurrency($transactionData, $index);
//
// // store basic journal first.
// $journal = TransactionJournal::create(
// [
// 'user_id' => $data['user'],
// 'transaction_type_id' => $type->id,
// 'bill_id' => null,
// 'transaction_currency_id' => $transactionData['currency']->id,
// 'description' => $description,
// 'date' => $carbon->format('Y-m-d H:i:s'),
// 'order' => 0,
// 'tag_count' => 0,
// 'completed' => 0,
// ]
// );
// Log::debug(sprintf('Stored journal under ID #%d', $journal->id));
//
// // store transactions for this journal:
// $factory->createPair($journal, $transactionData);
//
// // link bill
// Log::debug('Connect bill');
// $this->connectBill($journal, $transactionData);
//
// // link piggy bank (if transfer)
// $this->connectPiggyBank($journal, $transactionData);
//
// // link tags
// $this->connectTags($journal, $transactionData);
//
// // store note
// $this->storeNote($journal, $transactionData['notes']);
//
// // save journal:
// $journal->completed = true;
// $journal->save();
//
//
//
// // if ($journal->transactionType->type !== TransactionType::WITHDRAWAL) {
// // $transactionData['budget_id'] = null;
// // $transactionData['budget_name'] = null;
// // }
// // // save budget TODO
// // $budget = $this->findBudget($data['budget_id'], $data['budget_name']);
// // $this->setBudget($journal, $budget);
// //
// // // set category TODO
// // $category = $this->findCategory($data['category_id'], $data['category_name']);
// // $this->setCategory($journal, $category);
// //
// // // store meta data TODO
// // foreach ($this->fields as $field) {
// // $this->storeMeta($journal, $data, $field);
// // }
//
// // add to array
// $journals->push($journal);
// }
//
// // create group if necessary
// if ($journals->count() > 1) {
// $group = new TransactionGroup;
// $group->user()->associate($this->user);
// $group->title = $description;
// $group->save();
// $group->transactionJournals()->saveMany($journals);
//
// Log::debug(sprintf('More than one journal, created group #%d.', $group->id));
// }
//
//
// Log::debug('End of TransactionJournalFactory::create()');
// // invalidate cache.
// app('preferences')->mark();
//
// return $journals;
} }
@@ -181,148 +241,167 @@ class TransactionJournalFactory
public function setUser(User $user): void public function setUser(User $user): void
{ {
$this->user = $user; $this->user = $user;
$this->currencyRepository->setUser($this->user);
$this->transactionFactory->setUser($this->user);
} }
/** // /**
* Link a piggy bank to this journal. // * Connect bill if present.
* // *
* @param TransactionJournal $journal // * @param TransactionJournal $journal
* @param array $data // * @param array $data
*/ // */
protected function connectPiggyBank(TransactionJournal $journal, array $data): void // protected function connectBill(TransactionJournal $journal, array $data): void
{ // {
/** @var PiggyBankFactory $factory */ // if (!$journal->isWithdrawal()) {
$factory = app(PiggyBankFactory::class); // Log::debug(sprintf('Journal #%d is not a withdrawal', $journal->id));
$factory->setUser($this->user); //
// return;
$piggyBank = $factory->find($data['piggy_bank_id'], $data['piggy_bank_name']); // }
if (null !== $piggyBank) { // /** @var BillFactory $factory */
/** @var PiggyBankEventFactory $factory */ // $factory = app(BillFactory::class);
$factory = app(PiggyBankEventFactory::class); // $factory->setUser($journal->user);
$factory->create($journal, $piggyBank); //
} // $bill = null;
} //
// if (isset($data['bill']) && $data['bill'] instanceof Bill && $data['bill']->user_id === $this->user->id) {
/** // Log::debug('Bill object found and belongs to user');
* @param array $data // $bill = $data['bill'];
* @param int $index // }
* // if (null === $data['bill']) {
* @return TransactionCurrency // Log::debug('Bill object not found, search by bill data.');
*/ // $bill = $factory->find((int)$data['bill_id'], $data['bill_name']);
private function getCurrency(array $data, int $index): TransactionCurrency // }
{ //
// first check the transaction row itself. // if (null !== $bill) {
$row = $data['transactions'][$index]; // Log::debug(sprintf('Connected bill #%d (%s) to journal #%d', $bill->id, $bill->name, $journal->id));
$currency = null; // $journal->bill_id = $bill->id;
// $journal->save();
// check currency object: //
if (null === $currency && isset($row['currency']) && $row['currency'] instanceof TransactionCurrency) { // return;
$currency = $row['currency']; // }
} // Log::debug('Bill data is NULL.');
// $journal->bill_id = null;
// check currency ID: // $journal->save();
if (null === $currency && isset($row['currency_id']) && (int)$row['currency_id'] > 0) { //
$currencyId = (int)$row['currency_id']; // }
$currency = TransactionCurrency::find($currencyId); //
} // /**
// * Link a piggy bank to this journal.
// check currency code // *
if (null === $currency && isset($row['currency_code']) && 3 === \strlen($row['currency_code'])) { // * @param TransactionJournal $journal
$currency = TransactionCurrency::whereCode($row['currency_code'])->first(); // * @param array $data
} // */
// protected function connectPiggyBank(TransactionJournal $journal, array $data): void
// continue with journal itself. // {
// if (!$journal->isTransfer()) {
// check currency object: //
if (null === $currency && isset($data['currency']) && $data['currency'] instanceof TransactionCurrency) { // return;
$currency = $data['currency']; // }
} // /** @var PiggyBankFactory $factory */
// $factory = app(PiggyBankFactory::class);
// check currency ID: // $factory->setUser($this->user);
if (null === $currency && isset($data['currency_id']) && (int)$data['currency_id'] > 0) { // $piggyBank = null;
$currencyId = (int)$data['currency_id']; //
$currency = TransactionCurrency::find($currencyId); // if (isset($data['piggy_bank']) && $data['piggy_bank'] instanceof PiggyBank && $data['piggy_bank']->account->user_id === $this->user->id) {
} // Log::debug('Piggy found and belongs to user');
// $piggyBank = $data['piggy_bank'];
// check currency code // }
if (null === $currency && isset($data['currency_code']) && 3 === \strlen($data['currency_code'])) { // if (null === $data['piggy_bank']) {
$currency = TransactionCurrency::whereCode($data['currency_code'])->first(); // Log::debug('Piggy not found, search by piggy data.');
} // $piggyBank = $factory->find($data['piggy_bank_id'], $data['piggy_bank_name']);
if (null === $currency) { // }
// return user's default currency: //
$currency = app('amount')->getDefaultCurrencyByUser($this->user); // if (null !== $piggyBank) {
} // /** @var PiggyBankEventFactory $factory */
// $factory = app(PiggyBankEventFactory::class);
// enable currency: // $factory->create($journal, $piggyBank);
if (false === $currency->enabled) { // Log::debug('Create piggy event.');
$currency->enabled = true; //
$currency->save(); // return;
} // }
Log::debug(sprintf('Journal currency will be #%d (%s)', $currency->id, $currency->code)); // Log::debug('Create no piggy event');
// }
return $currency; //
// /**
} // * @param array $data
// * @param int $index
/** // *
* @param array $data // * @return TransactionCurrency
* @param int $index // */
* // private function getCurrency(array $data, int $index): TransactionCurrency
* @return TransactionCurrency|null // {
*/ // $currency = null;
private function getForeignCurrency(array $data, int $index): ?TransactionCurrency // // check currency object:
{ // if (null === $currency && isset($data['currency']) && $data['currency'] instanceof TransactionCurrency) {
// first check the transaction row itself. // $currency = $data['currency'];
$row = $data['transactions'][$index]; // }
$currency = null; //
// // check currency ID:
// check currency object: // if (null === $currency && isset($data['currency_id']) && (int)$data['currency_id'] > 0) {
if (null === $currency && isset($row['foreign_currency']) && $row['foreign_currency'] instanceof TransactionCurrency) { // $currencyId = (int)$data['currency_id'];
$currency = $row['foreign_currency']; // $currency = TransactionCurrency::find($currencyId);
} // }
//
// check currency ID: // // check currency code
if (null === $currency && isset($row['foreign_currency_id']) && (int)$row['foreign_currency_id'] > 0) { // if (null === $currency && isset($data['currency_code']) && 3 === \strlen($data['currency_code'])) {
$currencyId = (int)$row['foreign_currency_id']; // $currency = TransactionCurrency::whereCode($data['currency_code'])->first();
$currency = TransactionCurrency::find($currencyId); // }
} // if (null === $currency) {
// // return user's default currency:
// check currency code // $currency = app('amount')->getDefaultCurrencyByUser($this->user);
if (null === $currency && isset($row['foreign_currency_code']) && 3 === \strlen($row['foreign_currency_code'])) { // }
$currency = TransactionCurrency::whereCode($row['foreign_currency_code'])->first(); //
} // // enable currency:
// if (false === $currency->enabled) {
// continue with journal itself. // $currency->enabled = true;
// $currency->save();
// check currency object: // }
if (null === $currency && isset($data['foreign_currency']) && $data['foreign_currency'] instanceof TransactionCurrency) { // Log::debug(sprintf('Journal currency will be #%d (%s)', $currency->id, $currency->code));
$currency = $data['foreign_currency']; //
} // return $currency;
//
// check currency ID: // }
if (null === $currency && isset($data['foreign_currency_id']) && (int)$data['foreign_currency_id'] > 0) { //
$currencyId = (int)$data['foreign_currency_id']; // /**
$currency = TransactionCurrency::find($currencyId); // * @param array $data
} // * @param int $index
// *
// check currency code // * @return TransactionCurrency|null
if (null === $currency && isset($data['foreign_currency_code']) && 3 === \strlen($data['foreign_currency_code'])) { // */
$currency = TransactionCurrency::whereCode($data['foreign_currency_code'])->first(); // private function getForeignCurrency(array $data, int $index): ?TransactionCurrency
} // {
// $currency = null;
// enable currency: //
if (null !== $currency && false === $currency->enabled) { // // check currency object:
$currency->enabled = true; // if (null === $currency && isset($data['foreign_currency']) && $data['foreign_currency'] instanceof TransactionCurrency) {
$currency->save(); // $currency = $data['foreign_currency'];
} // }
if (null !== $currency) { //
Log::debug(sprintf('Journal foreign currency will be #%d (%s)', $currency->id, $currency->code)); // // check currency ID:
} // if (null === $currency && isset($data['foreign_currency_id']) && (int)$data['foreign_currency_id'] > 0) {
if (null === $currency) { // $currencyId = (int)$data['foreign_currency_id'];
Log::debug('Journal foreign currency will be NULL'); // $currency = TransactionCurrency::find($currencyId);
} // }
//
return $currency; // // check currency code
} // if (null === $currency && isset($data['foreign_currency_code']) && 3 === \strlen($data['foreign_currency_code'])) {
// $currency = TransactionCurrency::whereCode($data['foreign_currency_code'])->first();
// }
//
// // enable currency:
// if (null !== $currency && false === $currency->enabled) {
// $currency->enabled = true;
// $currency->save();
// }
// if (null !== $currency) {
// Log::debug(sprintf('Journal foreign currency will be #%d (%s)', $currency->id, $currency->code));
// }
// if (null === $currency) {
// Log::debug('Journal foreign currency will be NULL');
// }
//
// return $currency;
// }
} }

View File

@@ -45,6 +45,8 @@ use FireflyIII\Helpers\Report\PopupReport;
use FireflyIII\Helpers\Report\PopupReportInterface; use FireflyIII\Helpers\Report\PopupReportInterface;
use FireflyIII\Helpers\Report\ReportHelper; use FireflyIII\Helpers\Report\ReportHelper;
use FireflyIII\Helpers\Report\ReportHelperInterface; use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Repositories\TransactionType\TransactionTypeRepository;
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
use FireflyIII\Repositories\User\UserRepository; use FireflyIII\Repositories\User\UserRepository;
use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Services\Currency\ExchangeRateInterface; use FireflyIII\Services\Currency\ExchangeRateInterface;
@@ -71,8 +73,6 @@ use FireflyIII\Validation\FireflyValidator;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Twig; use Twig;
use Twig_Extension_Debug;
use TwigBridge\Extension\Loader\Functions;
use Validator; use Validator;
/** /**
@@ -176,6 +176,7 @@ class FireflyServiceProvider extends ServiceProvider
// export: // export:
$this->app->bind(ProcessorInterface::class, ExpandedProcessor::class); $this->app->bind(ProcessorInterface::class, ExpandedProcessor::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class); $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
$this->app->bind(TransactionTypeRepositoryInterface::class, TransactionTypeRepository::class);
$this->app->bind(AttachmentHelperInterface::class, AttachmentHelper::class); $this->app->bind(AttachmentHelperInterface::class, AttachmentHelper::class);
// more generators: // more generators:

View File

@@ -255,6 +255,41 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return TransactionCurrency::whereSymbol($currencySymbol)->first(); return TransactionCurrency::whereSymbol($currencySymbol)->first();
} }
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param TransactionCurrency|null $currency
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrency(?TransactionCurrency $currency, ?int $currencyId, ?string $currencyCode): TransactionCurrency
{
$result = null;
if (null !== $currency) {
$result = $currency;
}
if (null === $result) {
$result = $this->find((int)$currencyId);
}
if (null === $result) {
$result = $this->findByCode((string)$currencyCode);
}
if (null === $result) {
$result = app('amount')->getDefaultCurrencyByUser($this->user);
}
if (null === $result) {
$result = $this->findByCode('EUR');
}
if (false === $result->enabled) {
$this->enable($result);
}
return $result;
}
/** /**
* Find by ID, return NULL if not found. * Find by ID, return NULL if not found.
* Used in Import Currency! * Used in Import Currency!

View File

@@ -132,6 +132,17 @@ interface CurrencyRepositoryInterface
*/ */
public function findBySymbolNull(string $currencySymbol): ?TransactionCurrency; public function findBySymbolNull(string $currencySymbol): ?TransactionCurrency;
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param TransactionCurrency|null $currency
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrency(?TransactionCurrency $currency, ?int $currencyId, ?string $currencyCode): TransactionCurrency;
/** /**
* Find by ID, return NULL if not found. * Find by ID, return NULL if not found.
* *

View File

@@ -0,0 +1,64 @@
<?php
/**
* TransactionTypeRepository.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Repositories\TransactionType;
use FireflyIII\Models\TransactionType;
/**
* Class TransactionTypeRepository
*/
class TransactionTypeRepository implements TransactionTypeRepositoryInterface
{
/**
* @param string $type
*
* @return TransactionType|null
*/
public function findByType(string $type): ?TransactionType
{
$search = ucfirst($type);
return TransactionType::whereType($search)->first();
}
/**
* @param TransactionType|null $type
* @param string|null $typeString
*
* @return TransactionType
*/
public function findTransactionType(?TransactionType $type, ?string $typeString): TransactionType
{
if (null !== $type) {
return $type;
}
$search = $this->findByType($typeString);
if (null === $search) {
$search = $this->findByType(TransactionType::WITHDRAWAL);
}
return $search;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* TransactionTypeRepositoryInterface.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Repositories\TransactionType;
use FireflyIII\Models\TransactionType;
/**
* Interface TransactionTypeRepositoryInterface
*/
interface TransactionTypeRepositoryInterface
{
/**
* @param TransactionType|null $type
* @param string|null $typeString
*
* @return TransactionType
*/
public function findTransactionType(?TransactionType $type, ?string $typeString): TransactionType;
/**
* @param string $type
*
* @return TransactionType|null
*/
public function findByType(string $type): ?TransactionType;
}

View File

@@ -29,6 +29,7 @@ use FireflyIII\Factory\BudgetFactory;
use FireflyIII\Factory\CategoryFactory; use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Factory\TagFactory; use FireflyIII\Factory\TagFactory;
use FireflyIII\Factory\TransactionJournalMetaFactory; use FireflyIII\Factory\TransactionJournalMetaFactory;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget; use FireflyIII\Models\Budget;
use FireflyIII\Models\Category; use FireflyIII\Models\Category;
use FireflyIII\Models\Note; use FireflyIII\Models\Note;
@@ -59,7 +60,7 @@ trait JournalServiceTrait
return; // @codeCoverageIgnore return; // @codeCoverageIgnore
} }
foreach ($data['tags'] as $string) { foreach ($data['tags'] as $string) {
if ('' != $string) { if ('' !== $string) {
$tag = $factory->findOrCreate($string); $tag = $factory->findOrCreate($string);
if (null !== $tag) { if (null !== $tag) {
$set[] = $tag->id; $set[] = $tag->id;
@@ -69,29 +70,6 @@ trait JournalServiceTrait
$journal->tags()->sync($set); $journal->tags()->sync($set);
} }
/**
* Connect bill if present.
*
* @param TransactionJournal $journal
* @param array $data
*/
protected function connectBill(TransactionJournal $journal, array $data): void
{
/** @var BillFactory $factory */
$factory = app(BillFactory::class);
$factory->setUser($journal->user);
$bill = $factory->find((int)$data['bill_id'], $data['bill_name']);
if (null !== $bill) {
$journal->bill_id = $bill->id;
$journal->save();
return;
}
$journal->bill_id = null;
$journal->save();
}
/** /**
* @param int|null $budgetId * @param int|null $budgetId

View File

@@ -25,13 +25,9 @@ namespace FireflyIII\Services\Internal\Support;
use FireflyIII\Factory\AccountFactory; use FireflyIII\Factory\AccountFactory;
use FireflyIII\Factory\BudgetFactory;
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
@@ -86,8 +82,14 @@ trait TransactionServiceTrait
* @throws \FireflyIII\Exceptions\FireflyException * @throws \FireflyIII\Exceptions\FireflyException
* @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/ */
public function findAccount(?string $expectedType, ?int $accountId, ?string $accountName): ?Account public function findAccount(?string $expectedType, ?Account $account, ?int $accountId, ?string $accountName): ?Account
{ {
$result = null;
if (null !== $account && $account->user_id === $this->user->id) {
return $account;
}
$accountId = (int)$accountId; $accountId = (int)$accountId;
$accountName = (string)$accountName; $accountName = (string)$accountName;
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);