Code cleanup

This commit is contained in:
James Cole
2021-03-21 09:15:40 +01:00
parent da1751940e
commit 206845575c
317 changed files with 7418 additions and 7362 deletions

View File

@@ -38,12 +38,12 @@ use Log;
*/
class TransactionFactory
{
private Account $account;
private TransactionCurrency $currency;
private Account $account;
private TransactionCurrency $currency;
private ?TransactionCurrency $foreignCurrency;
private TransactionJournal $journal;
private bool $reconciled;
private User $user;
private TransactionJournal $journal;
private bool $reconciled;
private User $user;
/**
@@ -62,8 +62,8 @@ class TransactionFactory
* @param string $amount
* @param string|null $foreignAmount
*
* @throws FireflyException
* @return Transaction
* @throws FireflyException
*/
public function createNegative(string $amount, ?string $foreignAmount): Transaction
{
@@ -77,14 +77,74 @@ class TransactionFactory
return $this->create(app('steam')->negative($amount), $foreignAmount);
}
/**
* @param string $amount
* @param string|null $foreignAmount
*
* @return Transaction
* @throws FireflyException
*/
private function create(string $amount, ?string $foreignAmount): Transaction
{
$result = null;
if ('' === $foreignAmount) {
$foreignAmount = null;
}
$data = [
'reconciled' => $this->reconciled,
'account_id' => $this->account->id,
'transaction_journal_id' => $this->journal->id,
'description' => null,
'transaction_currency_id' => $this->currency->id,
'amount' => $amount,
'foreign_amount' => null,
'foreign_currency_id' => null,
'identifier' => 0,
];
try {
$result = Transaction::create($data);
// @codeCoverageIgnoreStart
} catch (QueryException $e) {
Log::error(sprintf('Could not create transaction: %s', $e->getMessage()), $data);
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException('Query exception when creating transaction.');
}
if (null === $result) {
throw new FireflyException('Transaction is NULL.');
}
// @codeCoverageIgnoreEnd
if (null !== $result) {
Log::debug(
sprintf(
'Created transaction #%d (%s %s, account %s), part of journal #%d',
$result->id,
$this->currency->code,
$amount,
$this->account->name,
$this->journal->id
)
);
// do foreign currency thing: add foreign currency info to $one and $two if necessary.
if (null !== $this->foreignCurrency && null !== $foreignAmount && $this->foreignCurrency->id !== $this->currency->id && '' !== $foreignAmount) {
$result->foreign_currency_id = $this->foreignCurrency->id;
$result->foreign_amount = $foreignAmount;
}
$result->save();
}
return $result;
}
/**
* Create transaction with positive amount (for destination accounts).
*
* @param string $amount
* @param string|null $foreignAmount
*
* @throws FireflyException
* @return Transaction
* @throws FireflyException
*/
public function createPositive(string $amount, ?string $foreignAmount): Transaction
{
@@ -157,64 +217,4 @@ class TransactionFactory
{
$this->user = $user;
}
/**
* @param string $amount
* @param string|null $foreignAmount
*
* @throws FireflyException
* @return Transaction
*/
private function create(string $amount, ?string $foreignAmount): Transaction
{
$result = null;
if ('' === $foreignAmount) {
$foreignAmount = null;
}
$data = [
'reconciled' => $this->reconciled,
'account_id' => $this->account->id,
'transaction_journal_id' => $this->journal->id,
'description' => null,
'transaction_currency_id' => $this->currency->id,
'amount' => $amount,
'foreign_amount' => null,
'foreign_currency_id' => null,
'identifier' => 0,
];
try {
$result = Transaction::create($data);
// @codeCoverageIgnoreStart
} catch (QueryException $e) {
Log::error(sprintf('Could not create transaction: %s', $e->getMessage()), $data);
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException('Query exception when creating transaction.');
}
if (null === $result) {
throw new FireflyException('Transaction is NULL.');
}
// @codeCoverageIgnoreEnd
if (null !== $result) {
Log::debug(
sprintf(
'Created transaction #%d (%s %s, account %s), part of journal #%d',
$result->id,
$this->currency->code,
$amount,
$this->account->name,
$this->journal->id
)
);
// do foreign currency thing: add foreign currency info to $one and $two if necessary.
if (null !== $this->foreignCurrency && null !== $foreignAmount && $this->foreignCurrency->id !== $this->currency->id && '' !== $foreignAmount) {
$result->foreign_currency_id = $this->foreignCurrency->id;
$result->foreign_amount = $foreignAmount;
}
$result->save();
}
return $result;
}
}