mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-24 12:47:00 +00:00
Code cleanup
This commit is contained in:
@@ -65,6 +65,31 @@ class AccountFactory
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accountName
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function findOrCreate(string $accountName, string $accountType): Account
|
||||
{
|
||||
Log::debug(sprintf('Searching for "%s" of type "%s"', $accountName, $accountType));
|
||||
/** @var AccountType $type */
|
||||
$type = AccountType::whereType($accountType)->first();
|
||||
$return = $this->user->accounts->where('account_type_id', $type->id)->where('name', $accountName)->first();
|
||||
|
||||
if (null === $return) {
|
||||
Log::debug('Found nothing. Will create a new one.');
|
||||
$return = $this->create(
|
||||
['user_id' => $this->user->id, 'name' => $accountName, 'account_type_id' => $type->id, 'account_type' => null, 'virtual_balance' => '0',
|
||||
'iban' => null, 'active' => true,]
|
||||
);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
@@ -150,52 +175,6 @@ class AccountFactory
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accountName
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function find(string $accountName, string $accountType): ?Account
|
||||
{
|
||||
$type = AccountType::whereType($accountType)->first();
|
||||
|
||||
return $this->user->accounts()->where('account_type_id', $type->id)->where('name', $accountName)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accountName
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function findOrCreate(string $accountName, string $accountType): Account
|
||||
{
|
||||
Log::debug(sprintf('Searching for "%s" of type "%s"', $accountName, $accountType));
|
||||
/** @var AccountType $type */
|
||||
$type = AccountType::whereType($accountType)->first();
|
||||
$return = $this->user->accounts->where('account_type_id', $type->id)->where('name', $accountName)->first();
|
||||
|
||||
if (null === $return) {
|
||||
Log::debug('Found nothing. Will create a new one.');
|
||||
$return = $this->create(
|
||||
['user_id' => $this->user->id, 'name' => $accountName, 'account_type_id' => $type->id, 'account_type' => null, 'virtual_balance' => '0',
|
||||
'iban' => null, 'active' => true,]
|
||||
);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $accountTypeId
|
||||
* @param null|string $accountType
|
||||
@@ -234,5 +213,26 @@ class AccountFactory
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accountName
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function find(string $accountName, string $accountType): ?Account
|
||||
{
|
||||
$type = AccountType::whereType($accountType)->first();
|
||||
|
||||
return $this->user->accounts()->where('account_type_id', $type->id)->where('name', $accountName)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -34,16 +34,6 @@ use Log;
|
||||
*/
|
||||
class AccountMetaFactory
|
||||
{
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return AccountMeta|null
|
||||
*/
|
||||
public function create(array $data): ?AccountMeta
|
||||
{
|
||||
return AccountMeta::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create update or delete meta data.
|
||||
*
|
||||
@@ -87,4 +77,14 @@ class AccountMetaFactory
|
||||
return $entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return AccountMeta|null
|
||||
*/
|
||||
public function create(array $data): ?AccountMeta
|
||||
{
|
||||
return AccountMeta::create($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,23 +40,24 @@ class AttachmentFactory
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @return Attachment|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function create(array $data): ?Attachment
|
||||
{
|
||||
// append if necessary.
|
||||
$model = false === strpos($data['attachable_type'], 'FireflyIII') ? sprintf('FireflyIII\\Models\\%s', $data['attachable_type']) : $data['attachable_type'];
|
||||
$model = false === strpos($data['attachable_type'], 'FireflyIII') ? sprintf('FireflyIII\\Models\\%s', $data['attachable_type'])
|
||||
: $data['attachable_type'];
|
||||
|
||||
// get journal instead of transaction.
|
||||
if (Transaction::class === $model) {
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $this->user->transactions()->find((int) $data['attachable_id']);
|
||||
$transaction = $this->user->transactions()->find((int)$data['attachable_id']);
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException('Unexpectedly could not find transaction'); // @codeCoverageIgnore
|
||||
}
|
||||
$data['attachable_id'] = $transaction->transaction_journal_id;
|
||||
$model = TransactionJournal::class;
|
||||
$model = TransactionJournal::class;
|
||||
}
|
||||
|
||||
// create attachment:
|
||||
@@ -74,7 +75,7 @@ class AttachmentFactory
|
||||
'uploaded' => 0,
|
||||
]
|
||||
);
|
||||
$notes = (string) ($data['notes'] ?? '');
|
||||
$notes = (string)($data['notes'] ?? '');
|
||||
if ('' !== $notes) {
|
||||
$note = new Note;
|
||||
$note->noteable()->associate($attachment);
|
||||
|
||||
@@ -41,8 +41,8 @@ class BudgetFactory
|
||||
*/
|
||||
public function find(?int $budgetId, ?string $budgetName): ?Budget
|
||||
{
|
||||
$budgetId = (int) $budgetId;
|
||||
$budgetName = (string) $budgetName;
|
||||
$budgetId = (int)$budgetId;
|
||||
$budgetName = (string)$budgetName;
|
||||
|
||||
if (0 === $budgetId && '' === $budgetName) {
|
||||
return null;
|
||||
|
||||
@@ -37,16 +37,6 @@ class CategoryFactory
|
||||
{
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return Category|null
|
||||
*/
|
||||
public function findByName(string $name): ?Category
|
||||
{
|
||||
return $this->user->categories()->where('name', $name)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $categoryId
|
||||
* @param null|string $categoryName
|
||||
@@ -94,6 +84,16 @@ class CategoryFactory
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return Category|null
|
||||
*/
|
||||
public function findByName(string $name): ?Category
|
||||
{
|
||||
return $this->user->categories()->where('name', $name)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
|
||||
@@ -42,8 +42,8 @@ class PiggyBankFactory
|
||||
*/
|
||||
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
|
||||
{
|
||||
$piggyBankId = (int) $piggyBankId;
|
||||
$piggyBankName = (string) $piggyBankName;
|
||||
$piggyBankId = (int)$piggyBankId;
|
||||
$piggyBankName = (string)$piggyBankName;
|
||||
if ('' === $piggyBankName && 0 === $piggyBankId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -73,13 +73,13 @@ class RecurrenceFactory
|
||||
|
||||
throw new FireflyException($message);
|
||||
}
|
||||
$firstDate = null;
|
||||
$repeatUntil = null;
|
||||
$repetitions = 0;
|
||||
$title = null;
|
||||
$description = '';
|
||||
$applyRules = true;
|
||||
$active = true;
|
||||
$firstDate = null;
|
||||
$repeatUntil = null;
|
||||
$repetitions = 0;
|
||||
$title = null;
|
||||
$description = '';
|
||||
$applyRules = true;
|
||||
$active = true;
|
||||
$repeatUntilString = null;
|
||||
if (array_key_exists('first_date', $data['recurrence'])) {
|
||||
/** @var Carbon $firstDate */
|
||||
|
||||
@@ -36,6 +36,43 @@ class TagFactory
|
||||
{
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @param string $tag
|
||||
*
|
||||
* @return Tag|null
|
||||
*/
|
||||
public function findOrCreate(string $tag): ?Tag
|
||||
{
|
||||
$tag = trim($tag);
|
||||
Log::debug(sprintf('Now in TagFactory::findOrCreate("%s")', $tag));
|
||||
|
||||
/** @var Tag $dbTag */
|
||||
$dbTag = $this->user->tags()->where('tag', $tag)->first();
|
||||
if (null !== $dbTag) {
|
||||
Log::debug(sprintf('Tag exists (#%d), return it.', $dbTag->id));
|
||||
|
||||
return $dbTag;
|
||||
}
|
||||
$newTag = $this->create(
|
||||
[
|
||||
'tag' => $tag,
|
||||
'date' => null,
|
||||
'description' => null,
|
||||
'latitude' => null,
|
||||
'longitude' => null,
|
||||
'zoom_level' => null,
|
||||
]
|
||||
);
|
||||
if (null === $newTag) {
|
||||
Log::error(sprintf('TagFactory::findOrCreate("%s") but tag is unexpectedly NULL!', $tag));
|
||||
|
||||
return null;
|
||||
}
|
||||
Log::debug(sprintf('Created new tag #%d ("%s")', $newTag->id, $newTag->tag));
|
||||
|
||||
return $newTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
@@ -43,9 +80,9 @@ class TagFactory
|
||||
*/
|
||||
public function create(array $data): ?Tag
|
||||
{
|
||||
$zoomLevel = 0 === (int) $data['zoom_level'] ? null : (int) $data['zoom_level'];
|
||||
$latitude = 0.0 === (float) $data['latitude'] ? null : (float) $data['latitude'];
|
||||
$longitude = 0.0 === (float) $data['longitude'] ? null : (float) $data['longitude'];
|
||||
$zoomLevel = 0 === (int)$data['zoom_level'] ? null : (int)$data['zoom_level'];
|
||||
$latitude = 0.0 === (float)$data['latitude'] ? null : (float)$data['latitude'];
|
||||
$longitude = 0.0 === (float)$data['longitude'] ? null : (float)$data['longitude'];
|
||||
$array = [
|
||||
'user_id' => $this->user->id,
|
||||
'tag' => trim($data['tag']),
|
||||
@@ -70,41 +107,6 @@ class TagFactory
|
||||
return $tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tag
|
||||
*
|
||||
* @return Tag|null
|
||||
*/
|
||||
public function findOrCreate(string $tag): ?Tag
|
||||
{
|
||||
$tag = trim($tag);
|
||||
Log::debug(sprintf('Now in TagFactory::findOrCreate("%s")', $tag));
|
||||
|
||||
/** @var Tag $dbTag */
|
||||
$dbTag = $this->user->tags()->where('tag', $tag)->first();
|
||||
if (null !== $dbTag) {
|
||||
Log::debug(sprintf('Tag exists (#%d), return it.', $dbTag->id));
|
||||
return $dbTag;
|
||||
}
|
||||
$newTag = $this->create(
|
||||
[
|
||||
'tag' => $tag,
|
||||
'date' => null,
|
||||
'description' => null,
|
||||
'latitude' => null,
|
||||
'longitude' => null,
|
||||
'zoom_level' => null,
|
||||
]
|
||||
);
|
||||
if (null === $newTag) {
|
||||
Log::error(sprintf('TagFactory::findOrCreate("%s") but tag is unexpectedly NULL!', $tag));
|
||||
return null;
|
||||
}
|
||||
Log::debug(sprintf('Created new tag #%d ("%s")', $newTag->id, $newTag->tag));
|
||||
|
||||
return $newTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
|
||||
@@ -40,8 +40,8 @@ class TransactionCurrencyFactory
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @return TransactionCurrency
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function create(array $data): TransactionCurrency
|
||||
{
|
||||
@@ -73,8 +73,8 @@ class TransactionCurrencyFactory
|
||||
*/
|
||||
public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
|
||||
{
|
||||
$currencyCode = (string) $currencyCode;
|
||||
$currencyId = (int) $currencyId;
|
||||
$currencyCode = (string)$currencyCode;
|
||||
$currencyId = (int)$currencyId;
|
||||
|
||||
if ('' === $currencyCode && 0 === $currencyId) {
|
||||
Log::debug('Cannot find anything on empty currency code and empty currency ID!');
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,8 +54,8 @@ class TransactionGroupFactory
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @throws DuplicateTransactionException
|
||||
* @return TransactionGroup
|
||||
* @throws DuplicateTransactionException
|
||||
*/
|
||||
public function create(array $data): TransactionGroup
|
||||
{
|
||||
|
||||
@@ -140,74 +140,6 @@ class TransactionJournalFactory
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $errorOnHash
|
||||
*/
|
||||
public function setErrorOnHash(bool $errorOnHash): void
|
||||
{
|
||||
$this->errorOnHash = $errorOnHash;
|
||||
if (true === $errorOnHash) {
|
||||
Log::info('Will trigger duplication alert for this journal.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->currencyRepository->setUser($this->user);
|
||||
$this->tagFactory->setUser($user);
|
||||
$this->billRepository->setUser($this->user);
|
||||
$this->budgetRepository->setUser($this->user);
|
||||
$this->categoryRepository->setUser($this->user);
|
||||
$this->piggyRepository->setUser($this->user);
|
||||
$this->accountRepository->setUser($this->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $data
|
||||
* @param string $field
|
||||
*/
|
||||
protected function storeMeta(TransactionJournal $journal, NullArrayObject $data, string $field): void
|
||||
{
|
||||
$set = [
|
||||
'journal' => $journal,
|
||||
'name' => $field,
|
||||
'data' => (string)($data[$field] ?? ''),
|
||||
];
|
||||
|
||||
Log::debug(sprintf('Going to store meta-field "%s", with value "%s".', $set['name'], $set['data']));
|
||||
|
||||
/** @var TransactionJournalMetaFactory $factory */
|
||||
$factory = app(TransactionJournalMetaFactory::class);
|
||||
$factory->updateOrCreate($set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set foreign currency to NULL if it's the same as the normal currency:
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
* @param TransactionCurrency|null $foreignCurrency
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
*/
|
||||
private function compareCurrencies(?TransactionCurrency $currency, ?TransactionCurrency $foreignCurrency): ?TransactionCurrency
|
||||
{
|
||||
if (null === $currency) {
|
||||
return null;
|
||||
}
|
||||
if (null !== $foreignCurrency && $foreignCurrency->id === $currency->id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $foreignCurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $row
|
||||
*
|
||||
@@ -360,6 +292,29 @@ class TransactionJournalFactory
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $row
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function hashArray(NullArrayObject $row): string
|
||||
{
|
||||
$dataRow = $row->getArrayCopy();
|
||||
|
||||
unset($dataRow['import_hash_v2'], $dataRow['original_source']);
|
||||
$json = json_encode($dataRow, JSON_THROW_ON_ERROR, 512);
|
||||
if (false === $json) {
|
||||
// @codeCoverageIgnoreStart
|
||||
$json = json_encode((string)microtime(), JSON_THROW_ON_ERROR, 512);
|
||||
Log::error(sprintf('Could not hash the original row! %s', json_last_error_msg()), $dataRow);
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
$hash = hash('sha256', $json);
|
||||
Log::debug(sprintf('The hash is: %s', $hash), $dataRow);
|
||||
|
||||
return $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* If this transaction already exists, throw an error.
|
||||
*
|
||||
@@ -387,6 +342,127 @@ class TransactionJournalFactory
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function validateAccounts(NullArrayObject $data): void
|
||||
{
|
||||
$transactionType = $data['type'] ?? 'invalid';
|
||||
$this->accountValidator->setUser($this->user);
|
||||
$this->accountValidator->setTransactionType($transactionType);
|
||||
|
||||
// validate source account.
|
||||
$sourceId = $data['source_id'] ? (int)$data['source_id'] : null;
|
||||
$sourceName = $data['source_name'] ? (string)$data['source_name'] : null;
|
||||
$validSource = $this->accountValidator->validateSource($sourceId, $sourceName, null);
|
||||
|
||||
// do something with result:
|
||||
if (false === $validSource) {
|
||||
throw new FireflyException(sprintf('Source: %s', $this->accountValidator->sourceError)); // @codeCoverageIgnore
|
||||
}
|
||||
Log::debug('Source seems valid.');
|
||||
// validate destination account
|
||||
$destinationId = $data['destination_id'] ? (int)$data['destination_id'] : null;
|
||||
$destinationName = $data['destination_name'] ? (string)$data['destination_name'] : null;
|
||||
$validDestination = $this->accountValidator->validateDestination($destinationId, $destinationName, null);
|
||||
// do something with result:
|
||||
if (false === $validDestination) {
|
||||
throw new FireflyException(sprintf('Destination: %s', $this->accountValidator->destError)); // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param Account $source
|
||||
* @param Account $destination
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
private function getCurrencyByAccount(string $type, ?TransactionCurrency $currency, Account $source, Account $destination): TransactionCurrency
|
||||
{
|
||||
Log::debug('Now ingetCurrencyByAccount()');
|
||||
switch ($type) {
|
||||
default:
|
||||
case TransactionType::WITHDRAWAL:
|
||||
case TransactionType::TRANSFER:
|
||||
return $this->getCurrency($currency, $source);
|
||||
case TransactionType::DEPOSIT:
|
||||
return $this->getCurrency($currency, $destination);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param Account $account
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
private function getCurrency(?TransactionCurrency $currency, Account $account): TransactionCurrency
|
||||
{
|
||||
Log::debug('Now in getCurrency()');
|
||||
$preference = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $preference && null === $currency) {
|
||||
// return user's default:
|
||||
return app('amount')->getDefaultCurrencyByUser($this->user);
|
||||
}
|
||||
$result = ($preference ?? $currency) ?? app('amount')->getSystemCurrency();
|
||||
Log::debug(sprintf('Currency is now #%d (%s) because of account #%d (%s)', $result->id, $result->code, $account->id, $account->name));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set foreign currency to NULL if it's the same as the normal currency:
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
* @param TransactionCurrency|null $foreignCurrency
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
*/
|
||||
private function compareCurrencies(?TransactionCurrency $currency, ?TransactionCurrency $foreignCurrency): ?TransactionCurrency
|
||||
{
|
||||
if (null === $currency) {
|
||||
return null;
|
||||
}
|
||||
if (null !== $foreignCurrency && $foreignCurrency->id === $currency->id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $foreignCurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param TransactionCurrency|null $foreignCurrency
|
||||
* @param Account $destination
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
*/
|
||||
private function getForeignByAccount(string $type, ?TransactionCurrency $foreignCurrency, Account $destination): ?TransactionCurrency
|
||||
{
|
||||
if (TransactionType::TRANSFER === $type) {
|
||||
return $this->getCurrency($foreignCurrency, $destination);
|
||||
}
|
||||
|
||||
return $foreignCurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getDescription(string $description): string
|
||||
{
|
||||
$description = '' === $description ? '(empty description)' : $description;
|
||||
|
||||
return substr($description, 0, 255);
|
||||
}
|
||||
|
||||
/**
|
||||
* Force the deletion of an entire set of transaction journals and their meta object in case of
|
||||
* an error creating a group.
|
||||
@@ -418,110 +494,6 @@ class TransactionJournalFactory
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param Account $account
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
private function getCurrency(?TransactionCurrency $currency, Account $account): TransactionCurrency
|
||||
{
|
||||
Log::debug('Now in getCurrency()');
|
||||
$preference = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $preference && null === $currency) {
|
||||
// return user's default:
|
||||
return app('amount')->getDefaultCurrencyByUser($this->user);
|
||||
}
|
||||
$result = ($preference ?? $currency) ?? app('amount')->getSystemCurrency();
|
||||
Log::debug(sprintf('Currency is now #%d (%s) because of account #%d (%s)', $result->id, $result->code, $account->id, $account->name));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param Account $source
|
||||
* @param Account $destination
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
private function getCurrencyByAccount(string $type, ?TransactionCurrency $currency, Account $source, Account $destination): TransactionCurrency
|
||||
{
|
||||
Log::debug('Now ingetCurrencyByAccount()');
|
||||
switch ($type) {
|
||||
default:
|
||||
case TransactionType::WITHDRAWAL:
|
||||
case TransactionType::TRANSFER:
|
||||
return $this->getCurrency($currency, $source);
|
||||
case TransactionType::DEPOSIT:
|
||||
return $this->getCurrency($currency, $destination);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getDescription(string $description): string
|
||||
{
|
||||
$description = '' === $description ? '(empty description)' : $description;
|
||||
|
||||
return substr($description, 0, 255);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param TransactionCurrency|null $foreignCurrency
|
||||
* @param Account $destination
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
*/
|
||||
private function getForeignByAccount(string $type, ?TransactionCurrency $foreignCurrency, Account $destination): ?TransactionCurrency
|
||||
{
|
||||
if (TransactionType::TRANSFER === $type) {
|
||||
return $this->getCurrency($foreignCurrency, $destination);
|
||||
}
|
||||
|
||||
return $foreignCurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $row
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function hashArray(NullArrayObject $row): string
|
||||
{
|
||||
$dataRow = $row->getArrayCopy();
|
||||
|
||||
unset($dataRow['import_hash_v2'], $dataRow['original_source']);
|
||||
$json = json_encode($dataRow, JSON_THROW_ON_ERROR, 512);
|
||||
if (false === $json) {
|
||||
// @codeCoverageIgnoreStart
|
||||
$json = json_encode((string)microtime(), JSON_THROW_ON_ERROR, 512);
|
||||
Log::error(sprintf('Could not hash the original row! %s', json_last_error_msg()), $dataRow);
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
$hash = hash('sha256', $json);
|
||||
Log::debug(sprintf('The hash is: %s', $hash), $dataRow);
|
||||
|
||||
return $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $transaction
|
||||
*/
|
||||
private function storeMetaFields(TransactionJournal $journal, NullArrayObject $transaction): void
|
||||
{
|
||||
foreach ($this->fields as $field) {
|
||||
$this->storeMeta($journal, $transaction, $field);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Link a piggy bank to this journal.
|
||||
*
|
||||
@@ -549,35 +521,63 @@ class TransactionJournalFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $transaction
|
||||
*/
|
||||
private function validateAccounts(NullArrayObject $data): void
|
||||
private function storeMetaFields(TransactionJournal $journal, NullArrayObject $transaction): void
|
||||
{
|
||||
$transactionType = $data['type'] ?? 'invalid';
|
||||
$this->accountValidator->setUser($this->user);
|
||||
$this->accountValidator->setTransactionType($transactionType);
|
||||
|
||||
// validate source account.
|
||||
$sourceId = $data['source_id'] ? (int)$data['source_id'] : null;
|
||||
$sourceName = $data['source_name'] ? (string)$data['source_name'] : null;
|
||||
$validSource = $this->accountValidator->validateSource($sourceId, $sourceName, null);
|
||||
|
||||
// do something with result:
|
||||
if (false === $validSource) {
|
||||
throw new FireflyException(sprintf('Source: %s', $this->accountValidator->sourceError)); // @codeCoverageIgnore
|
||||
foreach ($this->fields as $field) {
|
||||
$this->storeMeta($journal, $transaction, $field);
|
||||
}
|
||||
Log::debug('Source seems valid.');
|
||||
// validate destination account
|
||||
$destinationId = $data['destination_id'] ? (int)$data['destination_id'] : null;
|
||||
$destinationName = $data['destination_name'] ? (string)$data['destination_name'] : null;
|
||||
$validDestination = $this->accountValidator->validateDestination($destinationId, $destinationName, null);
|
||||
// do something with result:
|
||||
if (false === $validDestination) {
|
||||
throw new FireflyException(sprintf('Destination: %s', $this->accountValidator->destError)); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $data
|
||||
* @param string $field
|
||||
*/
|
||||
protected function storeMeta(TransactionJournal $journal, NullArrayObject $data, string $field): void
|
||||
{
|
||||
$set = [
|
||||
'journal' => $journal,
|
||||
'name' => $field,
|
||||
'data' => (string)($data[$field] ?? ''),
|
||||
];
|
||||
|
||||
Log::debug(sprintf('Going to store meta-field "%s", with value "%s".', $set['name'], $set['data']));
|
||||
|
||||
/** @var TransactionJournalMetaFactory $factory */
|
||||
$factory = app(TransactionJournalMetaFactory::class);
|
||||
$factory->updateOrCreate($set);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $errorOnHash
|
||||
*/
|
||||
public function setErrorOnHash(bool $errorOnHash): void
|
||||
{
|
||||
$this->errorOnHash = $errorOnHash;
|
||||
if (true === $errorOnHash) {
|
||||
Log::info('Will trigger duplication alert for this journal.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->currencyRepository->setUser($this->user);
|
||||
$this->tagFactory->setUser($user);
|
||||
$this->billRepository->setUser($this->user);
|
||||
$this->budgetRepository->setUser($this->user);
|
||||
$this->categoryRepository->setUser($this->user);
|
||||
$this->piggyRepository->setUser($this->user);
|
||||
$this->accountRepository->setUser($this->user);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ class TransactionJournalMetaFactory
|
||||
Log::debug('Is a carbon object.');
|
||||
$value = $data['data']->toW3cString();
|
||||
}
|
||||
if ('' === (string) $value) {
|
||||
if ('' === (string)$value) {
|
||||
Log::debug('Is an empty string.');
|
||||
// don't store blank strings.
|
||||
if (null !== $entry) {
|
||||
|
||||
@@ -26,7 +26,6 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Factory;
|
||||
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class TransactionTypeFactory
|
||||
|
||||
Reference in New Issue
Block a user