mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-14 16:13:54 +00:00
Code cleanup.
This commit is contained in:
@@ -34,7 +34,6 @@ use FireflyIII\Services\Internal\Support\LocationServiceTrait;
|
||||
use FireflyIII\Services\Internal\Update\AccountUpdateService;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
* Factory to create or return accounts.
|
||||
@@ -56,8 +55,6 @@ class AccountFactory
|
||||
|
||||
/**
|
||||
* AccountFactory constructor.
|
||||
*
|
||||
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -70,12 +67,8 @@ class AccountFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accountName
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function findOrCreate(string $accountName, string $accountType): Account
|
||||
{
|
||||
@@ -107,11 +100,8 @@ class AccountFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function create(array $data): Account
|
||||
{
|
||||
@@ -133,10 +123,22 @@ class AccountFactory
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function find(string $accountName, string $accountType): ?Account
|
||||
{
|
||||
app('log')->debug(sprintf('Now in AccountFactory::find("%s", "%s")', $accountName, $accountType));
|
||||
$type = AccountType::whereType($accountType)->first();
|
||||
|
||||
// @var Account|null
|
||||
return $this->user->accounts()->where('account_type_id', $type->id)->where('name', $accountName)->first();
|
||||
}
|
||||
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->accountRepository->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return AccountType|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function getAccountType(array $data): ?AccountType
|
||||
@@ -161,6 +163,7 @@ class AccountFactory
|
||||
}
|
||||
if (null === $result) {
|
||||
app('log')->warning(sprintf('Found NO account type based on %d and "%s"', $accountTypeId, $accountTypeName));
|
||||
|
||||
throw new FireflyException(sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $accountTypeId, $accountTypeName));
|
||||
}
|
||||
app('log')->debug(sprintf('Found account type based on %d and "%s": "%s"', $accountTypeId, $accountTypeName, $result->type));
|
||||
@@ -169,27 +172,8 @@ class AccountFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accountName
|
||||
* @param string $accountType
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
public function find(string $accountName, string $accountType): ?Account
|
||||
{
|
||||
app('log')->debug(sprintf('Now in AccountFactory::find("%s", "%s")', $accountName, $accountType));
|
||||
$type = AccountType::whereType($accountType)->first();
|
||||
|
||||
/** @var Account|null */
|
||||
return $this->user->accounts()->where('account_type_id', $type->id)->where('name', $accountName)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AccountType $type
|
||||
* @param array $data
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function createAccount(AccountType $type, array $data): Account
|
||||
{
|
||||
@@ -257,12 +241,8 @@ class AccountFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function cleanMetaDataArray(Account $account, array $data): array
|
||||
{
|
||||
@@ -272,7 +252,7 @@ class AccountFactory
|
||||
$currency = $this->getCurrency($currencyId, $currencyCode);
|
||||
|
||||
// only asset account may have a role:
|
||||
if ($account->accountType->type !== AccountType::ASSET) {
|
||||
if (AccountType::ASSET !== $account->accountType->type) {
|
||||
$accountRole = '';
|
||||
}
|
||||
// only liability may have direction:
|
||||
@@ -285,17 +265,13 @@ class AccountFactory
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*/
|
||||
private function storeMetaData(Account $account, array $data): void
|
||||
{
|
||||
$fields = $this->validFields;
|
||||
if ($account->accountType->type === AccountType::ASSET) {
|
||||
if (AccountType::ASSET === $account->accountType->type) {
|
||||
$fields = $this->validAssetFields;
|
||||
}
|
||||
if ($account->accountType->type === AccountType::ASSET && 'ccAsset' === $data['account_role']) {
|
||||
if (AccountType::ASSET === $account->accountType->type && 'ccAsset' === $data['account_role']) {
|
||||
$fields = $this->validCCFields;
|
||||
}
|
||||
|
||||
@@ -304,7 +280,7 @@ class AccountFactory
|
||||
$list = config('firefly.valid_currency_account_types');
|
||||
if (!in_array($type, $list, true)) {
|
||||
$pos = array_search('currency_id', $fields, true);
|
||||
if ($pos !== false) {
|
||||
if (false !== $pos) {
|
||||
unset($fields[$pos]);
|
||||
}
|
||||
}
|
||||
@@ -329,9 +305,6 @@ class AccountFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function storeOpeningBalance(Account $account, array $data): void
|
||||
@@ -351,9 +324,6 @@ class AccountFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function storeCreditLiability(Account $account, array $data): void
|
||||
@@ -380,9 +350,6 @@ class AccountFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function storeOrder(Account $account, array $data): void
|
||||
@@ -402,13 +369,4 @@ class AccountFactory
|
||||
$updateService->setUser($account->user);
|
||||
$updateService->update($account, ['order' => $order]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->accountRepository->setUser($user);
|
||||
}
|
||||
}
|
||||
|
@@ -34,16 +34,10 @@ class AccountMetaFactory
|
||||
{
|
||||
/**
|
||||
* Create update or delete meta data.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param string $field
|
||||
* @param string $value
|
||||
*
|
||||
* @return AccountMeta|null
|
||||
*/
|
||||
public function crud(Account $account, string $field, string $value): ?AccountMeta
|
||||
{
|
||||
/** @var AccountMeta|null $entry */
|
||||
/** @var null|AccountMeta $entry */
|
||||
$entry = $account->accountMeta()->where('name', $field)->first();
|
||||
// must not be an empty string:
|
||||
if ('' !== $value) {
|
||||
@@ -65,11 +59,6 @@ class AccountMetaFactory
|
||||
return $entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return AccountMeta|null
|
||||
*/
|
||||
public function create(array $data): ?AccountMeta
|
||||
{
|
||||
return AccountMeta::create($data);
|
||||
|
@@ -38,9 +38,6 @@ class AttachmentFactory
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Attachment|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function create(array $data): ?Attachment
|
||||
@@ -51,7 +48,7 @@ class AttachmentFactory
|
||||
|
||||
// get journal instead of transaction.
|
||||
if (Transaction::class === $model) {
|
||||
/** @var Transaction|null $transaction */
|
||||
/** @var null|Transaction $transaction */
|
||||
$transaction = $this->user->transactions()->find((int)$data['attachable_id']);
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException('Unexpectedly could not find transaction');
|
||||
@@ -86,9 +83,6 @@ class AttachmentFactory
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
|
@@ -30,7 +30,6 @@ use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
|
||||
use FireflyIII\Services\Internal\Support\BillServiceTrait;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\QueryException;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
* Class BillFactory
|
||||
@@ -43,11 +42,8 @@ class BillFactory
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Bill|null
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function create(array $data): ?Bill
|
||||
{
|
||||
@@ -59,6 +55,7 @@ class BillFactory
|
||||
try {
|
||||
$skip = array_key_exists('skip', $data) ? $data['skip'] : 0;
|
||||
$active = array_key_exists('active', $data) ? $data['active'] : 0;
|
||||
|
||||
/** @var Bill $bill */
|
||||
$bill = Bill::create(
|
||||
[
|
||||
@@ -81,6 +78,7 @@ class BillFactory
|
||||
} catch (QueryException $e) {
|
||||
app('log')->error($e->getMessage());
|
||||
app('log')->error($e->getTraceAsString());
|
||||
|
||||
throw new FireflyException('400000: Could not store bill.', 0, $e);
|
||||
}
|
||||
|
||||
@@ -108,12 +106,6 @@ class BillFactory
|
||||
return $bill;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $billId
|
||||
* @param null|string $billName
|
||||
*
|
||||
* @return Bill|null
|
||||
*/
|
||||
public function find(?int $billId, ?string $billName): ?Bill
|
||||
{
|
||||
$billId = (int)$billId;
|
||||
@@ -133,19 +125,11 @@ class BillFactory
|
||||
return $bill;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return Bill|null
|
||||
*/
|
||||
public function findByName(string $name): ?Bill
|
||||
{
|
||||
return $this->user->bills()->where('name', 'LIKE', sprintf('%%%s%%', $name))->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
|
@@ -33,12 +33,6 @@ class BudgetFactory
|
||||
{
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @param int|null $budgetId
|
||||
* @param null|string $budgetName
|
||||
*
|
||||
* @return Budget|null
|
||||
*/
|
||||
public function find(?int $budgetId, ?string $budgetName): ?Budget
|
||||
{
|
||||
$budgetId = (int)$budgetId;
|
||||
@@ -50,7 +44,7 @@ class BudgetFactory
|
||||
|
||||
// first by ID:
|
||||
if ($budgetId > 0) {
|
||||
/** @var Budget|null $budget */
|
||||
/** @var null|Budget $budget */
|
||||
$budget = $this->user->budgets()->find($budgetId);
|
||||
if (null !== $budget) {
|
||||
return $budget;
|
||||
@@ -67,19 +61,11 @@ class BudgetFactory
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return Budget|null
|
||||
*/
|
||||
public function findByName(string $name): ?Budget
|
||||
{
|
||||
return $this->user->budgets()->where('name', $name)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
|
@@ -36,10 +36,6 @@ class CategoryFactory
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @param int|null $categoryId
|
||||
* @param null|string $categoryName
|
||||
*
|
||||
* @return Category|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category
|
||||
@@ -54,7 +50,7 @@ class CategoryFactory
|
||||
}
|
||||
// first by ID:
|
||||
if ($categoryId > 0) {
|
||||
/** @var Category|null $category */
|
||||
/** @var null|Category $category */
|
||||
$category = $this->user->categories()->find($categoryId);
|
||||
if (null !== $category) {
|
||||
return $category;
|
||||
@@ -66,6 +62,7 @@ class CategoryFactory
|
||||
if (null !== $category) {
|
||||
return $category;
|
||||
}
|
||||
|
||||
try {
|
||||
return Category::create(
|
||||
[
|
||||
@@ -77,6 +74,7 @@ class CategoryFactory
|
||||
} catch (QueryException $e) {
|
||||
app('log')->error($e->getMessage());
|
||||
app('log')->error($e->getTraceAsString());
|
||||
|
||||
throw new FireflyException('400003: Could not store new category.', 0, $e);
|
||||
}
|
||||
}
|
||||
@@ -84,19 +82,11 @@ 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
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
|
@@ -34,10 +34,6 @@ use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||
*/
|
||||
class PiggyBankEventFactory
|
||||
{
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param PiggyBank|null $piggyBank
|
||||
*/
|
||||
public function create(TransactionJournal $journal, ?PiggyBank $piggyBank): void
|
||||
{
|
||||
app('log')->debug(sprintf('Now in PiggyBankEventCreate for a %s', $journal->transactionType->type));
|
||||
|
@@ -33,12 +33,6 @@ class PiggyBankFactory
|
||||
{
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @param int|null $piggyBankId
|
||||
* @param null|string $piggyBankName
|
||||
*
|
||||
* @return PiggyBank|null
|
||||
*/
|
||||
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
|
||||
{
|
||||
$piggyBankId = (int)$piggyBankId;
|
||||
@@ -48,7 +42,7 @@ class PiggyBankFactory
|
||||
}
|
||||
// first find by ID:
|
||||
if ($piggyBankId > 0) {
|
||||
/** @var PiggyBank|null $piggyBank */
|
||||
/** @var null|PiggyBank $piggyBank */
|
||||
$piggyBank = $this->user->piggyBanks()->find($piggyBankId);
|
||||
if (null !== $piggyBank) {
|
||||
return $piggyBank;
|
||||
@@ -57,7 +51,7 @@ class PiggyBankFactory
|
||||
|
||||
// then find by name:
|
||||
if ('' !== $piggyBankName) {
|
||||
/** @var PiggyBank|null $piggyBank */
|
||||
/** @var null|PiggyBank $piggyBank */
|
||||
$piggyBank = $this->findByName($piggyBankName);
|
||||
if (null !== $piggyBank) {
|
||||
return $piggyBank;
|
||||
@@ -67,19 +61,11 @@ class PiggyBankFactory
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return PiggyBank|null
|
||||
*/
|
||||
public function findByName(string $name): ?PiggyBank
|
||||
{
|
||||
return $this->user->piggyBanks()->where('piggy_banks.name', $name)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
|
@@ -30,7 +30,6 @@ use FireflyIII\Services\Internal\Support\RecurringTransactionTrait;
|
||||
use FireflyIII\Services\Internal\Support\TransactionTypeTrait;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
* Class RecurrenceFactory
|
||||
@@ -45,8 +44,6 @@ class RecurrenceFactory
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -54,11 +51,8 @@ class RecurrenceFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Recurrence
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function create(array $data): Recurrence
|
||||
{
|
||||
@@ -125,6 +119,7 @@ class RecurrenceFactory
|
||||
}
|
||||
|
||||
$this->createRepetitions($recurrence, $data['repetitions'] ?? []);
|
||||
|
||||
try {
|
||||
$this->createTransactions($recurrence, $data['transactions'] ?? []);
|
||||
} catch (FireflyException $e) {
|
||||
@@ -133,24 +128,18 @@ class RecurrenceFactory
|
||||
$recurrence->forceDelete();
|
||||
$message = sprintf('Could not create recurring transaction: %s', $e->getMessage());
|
||||
$this->errors->add('store', $message);
|
||||
|
||||
throw new FireflyException($message, 0, $e);
|
||||
}
|
||||
|
||||
|
||||
return $recurrence;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function getErrors(): MessageBag
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
|
@@ -34,17 +34,12 @@ class TagFactory
|
||||
{
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @param string $tag
|
||||
*
|
||||
* @return Tag|null
|
||||
*/
|
||||
public function findOrCreate(string $tag): ?Tag
|
||||
{
|
||||
$tag = trim($tag);
|
||||
app('log')->debug(sprintf('Now in TagFactory::findOrCreate("%s")', $tag));
|
||||
|
||||
/** @var Tag|null $dbTag */
|
||||
/** @var null|Tag $dbTag */
|
||||
$dbTag = $this->user->tags()->where('tag', $tag)->first();
|
||||
if (null !== $dbTag) {
|
||||
app('log')->debug(sprintf('Tag exists (#%d), return it.', $dbTag->id));
|
||||
@@ -71,11 +66,6 @@ class TagFactory
|
||||
return $newTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Tag|null
|
||||
*/
|
||||
public function create(array $data): ?Tag
|
||||
{
|
||||
$zoomLevel = 0 === (int)$data['zoom_level'] ? null : (int)$data['zoom_level'];
|
||||
@@ -92,7 +82,8 @@ class TagFactory
|
||||
'longitude' => null,
|
||||
'zoomLevel' => null,
|
||||
];
|
||||
/** @var Tag|null $tag */
|
||||
|
||||
/** @var null|Tag $tag */
|
||||
$tag = Tag::create($array);
|
||||
if (null !== $tag && null !== $latitude && null !== $longitude) {
|
||||
// create location object.
|
||||
@@ -107,9 +98,6 @@ class TagFactory
|
||||
return $tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
|
@@ -33,9 +33,6 @@ use Illuminate\Database\QueryException;
|
||||
class TransactionCurrencyFactory
|
||||
{
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function create(array $data): TransactionCurrency
|
||||
@@ -68,18 +65,13 @@ class TransactionCurrencyFactory
|
||||
$result = null;
|
||||
app('log')->error(sprintf('Could not create new currency: %s', $e->getMessage()));
|
||||
app('log')->error($e->getTraceAsString());
|
||||
|
||||
throw new FireflyException('400004: Could not store new currency.', 0, $e);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $currencyId
|
||||
* @param null|string $currencyCode
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
*/
|
||||
public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
|
||||
{
|
||||
$currencyCode = e($currencyCode);
|
||||
|
@@ -33,7 +33,6 @@ use FireflyIII\Rules\UniqueIban;
|
||||
use FireflyIII\Services\Internal\Update\AccountUpdateService;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Validator;
|
||||
|
||||
/**
|
||||
* Class TransactionFactory
|
||||
@@ -49,8 +48,6 @@ class TransactionFactory
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -61,10 +58,6 @@ class TransactionFactory
|
||||
/**
|
||||
* Create transaction with negative amount (for source accounts).
|
||||
*
|
||||
* @param string $amount
|
||||
* @param string|null $foreignAmount
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function createNegative(string $amount, ?string $foreignAmount): Transaction
|
||||
@@ -80,10 +73,64 @@ class TransactionFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
* @param string|null $foreignAmount
|
||||
* Create transaction with positive amount (for destination accounts).
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function createPositive(string $amount, ?string $foreignAmount): Transaction
|
||||
{
|
||||
if ('' === $foreignAmount) {
|
||||
$foreignAmount = null;
|
||||
}
|
||||
if (null !== $foreignAmount) {
|
||||
$foreignAmount = app('steam')->positive($foreignAmount);
|
||||
}
|
||||
|
||||
return $this->create(app('steam')->positive($amount), $foreignAmount);
|
||||
}
|
||||
|
||||
public function setAccount(Account $account): void
|
||||
{
|
||||
$this->account = $account;
|
||||
}
|
||||
|
||||
public function setAccountInformation(array $accountInformation): void
|
||||
{
|
||||
$this->accountInformation = $accountInformation;
|
||||
}
|
||||
|
||||
public function setCurrency(TransactionCurrency $currency): void
|
||||
{
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|TransactionCurrency $foreignCurrency |null
|
||||
*/
|
||||
public function setForeignCurrency(?TransactionCurrency $foreignCurrency): void
|
||||
{
|
||||
$this->foreignCurrency = $foreignCurrency;
|
||||
}
|
||||
|
||||
public function setJournal(TransactionJournal $journal): void
|
||||
{
|
||||
$this->journal = $journal;
|
||||
}
|
||||
|
||||
public function setReconciled(bool $reconciled): void
|
||||
{
|
||||
$this->reconciled = $reconciled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
// empty function.
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function create(string $amount, ?string $foreignAmount): Transaction
|
||||
@@ -102,13 +149,15 @@ class TransactionFactory
|
||||
'foreign_currency_id' => null,
|
||||
'identifier' => 0,
|
||||
];
|
||||
|
||||
try {
|
||||
/** @var Transaction|null $result */
|
||||
/** @var null|Transaction $result */
|
||||
$result = Transaction::create($data);
|
||||
} catch (QueryException $e) {
|
||||
app('log')->error(sprintf('Could not create transaction: %s', $e->getMessage()), $data);
|
||||
app('log')->error($e->getMessage());
|
||||
app('log')->error($e->getTraceAsString());
|
||||
|
||||
throw new FireflyException(sprintf('Query exception when creating transaction: %s', $e->getMessage()), 0, $e);
|
||||
}
|
||||
if (null === $result) {
|
||||
@@ -127,9 +176,9 @@ class TransactionFactory
|
||||
);
|
||||
|
||||
// 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) {
|
||||
if (null !== $this->foreignCurrency
|
||||
&& null !== $foreignAmount
|
||||
&& $this->foreignCurrency->id !== $this->currency->id) {
|
||||
$result->foreign_currency_id = $this->foreignCurrency->id;
|
||||
$result->foreign_amount = $foreignAmount;
|
||||
}
|
||||
@@ -142,29 +191,32 @@ class TransactionFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function updateAccountInformation(): void
|
||||
{
|
||||
if (!array_key_exists('iban', $this->accountInformation)) {
|
||||
app('log')->debug('No IBAN information in array, will not update.');
|
||||
|
||||
return;
|
||||
}
|
||||
if ('' !== (string)$this->account->iban) {
|
||||
app('log')->debug('Account already has IBAN information, will not update.');
|
||||
|
||||
return;
|
||||
}
|
||||
if ($this->account->iban === $this->accountInformation['iban']) {
|
||||
app('log')->debug('Account already has this IBAN, will not update.');
|
||||
|
||||
return;
|
||||
}
|
||||
// validate info:
|
||||
$validator = Validator::make(['iban' => $this->accountInformation['iban']], [
|
||||
$validator = \Validator::make(['iban' => $this->accountInformation['iban']], [
|
||||
'iban' => ['required', new UniqueIban($this->account, $this->account->accountType->type)],
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
app('log')->debug('Invalid or non-unique IBAN, will not update.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -172,92 +224,4 @@ class TransactionFactory
|
||||
$service = app(AccountUpdateService::class);
|
||||
$service->update($this->account, ['iban' => $this->accountInformation['iban']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create transaction with positive amount (for destination accounts).
|
||||
*
|
||||
* @param string $amount
|
||||
* @param string|null $foreignAmount
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function createPositive(string $amount, ?string $foreignAmount): Transaction
|
||||
{
|
||||
if ('' === $foreignAmount) {
|
||||
$foreignAmount = null;
|
||||
}
|
||||
if (null !== $foreignAmount) {
|
||||
$foreignAmount = app('steam')->positive($foreignAmount);
|
||||
}
|
||||
|
||||
return $this->create(app('steam')->positive($amount), $foreignAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*
|
||||
|
||||
*/
|
||||
public function setAccount(Account $account): void
|
||||
{
|
||||
$this->account = $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $accountInformation
|
||||
*/
|
||||
public function setAccountInformation(array $accountInformation): void
|
||||
{
|
||||
$this->accountInformation = $accountInformation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
|
||||
*/
|
||||
public function setCurrency(TransactionCurrency $currency): void
|
||||
{
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency|null $foreignCurrency |null
|
||||
*
|
||||
|
||||
*/
|
||||
public function setForeignCurrency(?TransactionCurrency $foreignCurrency): void
|
||||
{
|
||||
$this->foreignCurrency = $foreignCurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
|
||||
*/
|
||||
public function setJournal(TransactionJournal $journal): void
|
||||
{
|
||||
$this->journal = $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $reconciled
|
||||
*
|
||||
|
||||
*/
|
||||
public function setReconciled(bool $reconciled): void
|
||||
{
|
||||
$this->reconciled = $reconciled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
// empty function.
|
||||
}
|
||||
}
|
||||
|
@@ -27,12 +27,9 @@ use FireflyIII\Exceptions\DuplicateTransactionException;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\User;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
* Class TransactionGroupFactory
|
||||
*
|
||||
|
||||
*/
|
||||
class TransactionGroupFactory
|
||||
{
|
||||
@@ -50,22 +47,21 @@ class TransactionGroupFactory
|
||||
/**
|
||||
* Store a new transaction journal.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function create(array $data): TransactionGroup
|
||||
{
|
||||
app('log')->debug('Now in TransactionGroupFactory::create()');
|
||||
$this->journalFactory->setUser($this->user);
|
||||
$this->journalFactory->setErrorOnHash($data['error_if_duplicate_hash'] ?? false);
|
||||
|
||||
try {
|
||||
$collection = $this->journalFactory->create($data);
|
||||
} catch (DuplicateTransactionException $e) {
|
||||
app('log')->warning('GroupFactory::create() caught journalFactory::create() with a duplicate!');
|
||||
|
||||
throw new DuplicateTransactionException($e->getMessage(), 0, $e);
|
||||
}
|
||||
$title = $data['group_title'] ?? null;
|
||||
@@ -91,8 +87,6 @@ class TransactionGroupFactory
|
||||
|
||||
/**
|
||||
* Set the user.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
|
@@ -25,7 +25,6 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Factory;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\DuplicateTransactionException;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Account;
|
||||
@@ -47,7 +46,6 @@ use FireflyIII\Support\NullArrayObject;
|
||||
use FireflyIII\User;
|
||||
use FireflyIII\Validation\AccountValidator;
|
||||
use Illuminate\Support\Collection;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
* Class TransactionJournalFactory
|
||||
@@ -70,7 +68,7 @@ class TransactionJournalFactory
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -91,12 +89,9 @@ class TransactionJournalFactory
|
||||
/**
|
||||
* Store a new (set of) transaction journals.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return Collection
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function create(array $data): Collection
|
||||
{
|
||||
@@ -112,6 +107,7 @@ class TransactionJournalFactory
|
||||
|
||||
return new Collection();
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var array $row */
|
||||
foreach ($transactions as $index => $row) {
|
||||
@@ -129,12 +125,14 @@ class TransactionJournalFactory
|
||||
app('log')->error($e->getMessage());
|
||||
app('log')->error($e->getTraceAsString());
|
||||
$this->forceDeleteOnError($collection);
|
||||
|
||||
throw new DuplicateTransactionException($e->getMessage(), 0, $e);
|
||||
} catch (FireflyException $e) {
|
||||
app('log')->warning('TransactionJournalFactory::create() caught an exception.');
|
||||
app('log')->error($e->getMessage());
|
||||
app('log')->error($e->getTraceAsString());
|
||||
$this->forceDeleteOnError($collection);
|
||||
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
@@ -142,12 +140,52 @@ class TransactionJournalFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $row
|
||||
*
|
||||
* @return TransactionJournal|null
|
||||
* Set the 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);
|
||||
}
|
||||
|
||||
public function setErrorOnHash(bool $errorOnHash): void
|
||||
{
|
||||
$this->errorOnHash = $errorOnHash;
|
||||
if (true === $errorOnHash) {
|
||||
app('log')->info('Will trigger duplication alert for this journal.');
|
||||
}
|
||||
}
|
||||
|
||||
protected function storeMeta(TransactionJournal $journal, NullArrayObject $data, string $field): void
|
||||
{
|
||||
$set = [
|
||||
'journal' => $journal,
|
||||
'name' => $field,
|
||||
'data' => (string)($data[$field] ?? ''),
|
||||
];
|
||||
if ($data[$field] instanceof Carbon) {
|
||||
$data[$field]->setTimezone(config('app.timezone'));
|
||||
app('log')->debug(sprintf('%s Date: %s (%s)', $field, $data[$field], $data[$field]->timezone->getName()));
|
||||
$set['data'] = $data[$field]->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
app('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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function createJournal(NullArrayObject $row): ?TransactionJournal
|
||||
{
|
||||
@@ -165,7 +203,7 @@ class TransactionJournalFactory
|
||||
$billId = TransactionType::WITHDRAWAL === $type->type && null !== $bill ? $bill->id : null;
|
||||
$description = (string)$row['description'];
|
||||
|
||||
/** Manipulate basic fields */
|
||||
// Manipulate basic fields
|
||||
$carbon->setTimezone(config('app.timezone'));
|
||||
|
||||
try {
|
||||
@@ -245,6 +283,7 @@ class TransactionJournalFactory
|
||||
$transactionFactory->setAccountInformation($sourceInfo);
|
||||
$transactionFactory->setForeignCurrency($foreignCurrency);
|
||||
$transactionFactory->setReconciled($row['reconciled'] ?? false);
|
||||
|
||||
try {
|
||||
$negative = $transactionFactory->createNegative((string)$row['amount'], (string)$row['foreign_amount']);
|
||||
} catch (FireflyException $e) {
|
||||
@@ -252,6 +291,7 @@ class TransactionJournalFactory
|
||||
app('log')->error($e->getMessage());
|
||||
app('log')->error($e->getTraceAsString());
|
||||
$this->forceDeleteOnError(new Collection([$journal]));
|
||||
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
@@ -265,6 +305,7 @@ class TransactionJournalFactory
|
||||
$transactionFactory->setCurrency($currency);
|
||||
$transactionFactory->setForeignCurrency($foreignCurrency);
|
||||
$transactionFactory->setReconciled($row['reconciled'] ?? false);
|
||||
|
||||
try {
|
||||
$transactionFactory->createPositive((string)$row['amount'], (string)$row['foreign_amount']);
|
||||
} catch (FireflyException $e) {
|
||||
@@ -274,40 +315,38 @@ class TransactionJournalFactory
|
||||
app('log')->warning('Delete negative transaction.');
|
||||
$this->forceTrDelete($negative);
|
||||
$this->forceDeleteOnError(new Collection([$journal]));
|
||||
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
// verify that journal has two transactions. Otherwise, delete and cancel.
|
||||
$journal->completed = true;
|
||||
$journal->save();
|
||||
|
||||
/** Link all other data to the journal. */
|
||||
// Link all other data to the journal.
|
||||
|
||||
/** Link budget */
|
||||
// Link budget
|
||||
$this->storeBudget($journal, $row);
|
||||
|
||||
/** Link category */
|
||||
// Link category
|
||||
$this->storeCategory($journal, $row);
|
||||
|
||||
/** Set notes */
|
||||
// Set notes
|
||||
$this->storeNotes($journal, $row['notes']);
|
||||
|
||||
/** Set piggy bank */
|
||||
// Set piggy bank
|
||||
$this->storePiggyEvent($journal, $row);
|
||||
|
||||
/** Set tags */
|
||||
// Set tags
|
||||
$this->storeTags($journal, $row['tags']);
|
||||
|
||||
/** set all meta fields */
|
||||
// set all meta fields
|
||||
$this->storeMetaFields($journal, $row);
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $row
|
||||
*
|
||||
* @return string
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function hashArray(NullArrayObject $row): string
|
||||
{
|
||||
@@ -324,10 +363,8 @@ class TransactionJournalFactory
|
||||
/**
|
||||
* If this transaction already exists, throw an error.
|
||||
*
|
||||
* @param string $hash
|
||||
*
|
||||
* @throws DuplicateTransactionException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function errorIfDuplicate(string $hash): void
|
||||
{
|
||||
@@ -336,11 +373,13 @@ class TransactionJournalFactory
|
||||
return;
|
||||
}
|
||||
app('log')->debug('Will verify duplicate!');
|
||||
/** @var TransactionJournalMeta|null $result */
|
||||
|
||||
/** @var null|TransactionJournalMeta $result */
|
||||
$result = TransactionJournalMeta::withTrashed()
|
||||
->where('data', json_encode($hash, JSON_THROW_ON_ERROR))
|
||||
->with(['transactionJournal', 'transactionJournal.transactionGroup'])
|
||||
->first();
|
||||
->where('data', json_encode($hash, JSON_THROW_ON_ERROR))
|
||||
->with(['transactionJournal', 'transactionJournal.transactionGroup'])
|
||||
->first()
|
||||
;
|
||||
if (null !== $result) {
|
||||
app('log')->warning(sprintf('Found a duplicate in errorIfDuplicate because hash %s is not unique!', $hash));
|
||||
$journal = $result->transactionJournal()->withTrashed()->first();
|
||||
@@ -349,13 +388,12 @@ class TransactionJournalFactory
|
||||
if (null === $group) {
|
||||
$groupId = 0;
|
||||
}
|
||||
|
||||
throw new DuplicateTransactionException(sprintf('Duplicate of transaction #%d.', $groupId));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NullArrayObject $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function validateAccounts(NullArrayObject $data): void
|
||||
@@ -395,40 +433,19 @@ class TransactionJournalFactory
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Account|null $sourceAccount
|
||||
* @param Account|null $destinationAccount
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function reconciliationSanityCheck(?Account $sourceAccount, ?Account $destinationAccount): array
|
||||
{
|
||||
app('log')->debug(sprintf('Now in %s', __METHOD__));
|
||||
if (null !== $sourceAccount && null !== $destinationAccount) {
|
||||
app('log')->debug('Both accounts exist, simply return them.');
|
||||
|
||||
return [$sourceAccount, $destinationAccount];
|
||||
}
|
||||
if (null === $destinationAccount) { // @phpstan-ignore-line
|
||||
app('log')->debug('Destination account is NULL, source account is not.');
|
||||
$account = $this->accountRepository->getReconciliation($sourceAccount);
|
||||
app('log')->debug(sprintf('Will return account #%d ("%s") of type "%s"', $account->id, $account->name, $account->accountType->type));
|
||||
|
||||
return [$sourceAccount, $account];
|
||||
}
|
||||
|
||||
@@ -436,21 +453,17 @@ class TransactionJournalFactory
|
||||
app('log')->debug('Source account is NULL, destination account is not.');
|
||||
$account = $this->accountRepository->getReconciliation($destinationAccount);
|
||||
app('log')->debug(sprintf('Will return account #%d ("%s") of type "%s"', $account->id, $account->name, $account->accountType->type));
|
||||
|
||||
return [$account, $destinationAccount];
|
||||
}
|
||||
app('log')->debug('Unused fallback'); // @phpstan-ignore-line
|
||||
|
||||
return [$sourceAccount, $destinationAccount];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param Account $source
|
||||
* @param Account $destination
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function getCurrencyByAccount(string $type, ?TransactionCurrency $currency, Account $source, Account $destination): TransactionCurrency
|
||||
{
|
||||
@@ -463,17 +476,14 @@ class TransactionJournalFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param Account $account
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function getCurrency(?TransactionCurrency $currency, Account $account): TransactionCurrency
|
||||
{
|
||||
app('log')->debug('Now in getCurrency()');
|
||||
/** @var TransactionCurrency|null $preference */
|
||||
|
||||
/** @var null|TransactionCurrency $preference */
|
||||
$preference = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $preference && null === $currency) {
|
||||
// return user's default:
|
||||
@@ -487,11 +497,6 @@ class TransactionJournalFactory
|
||||
|
||||
/**
|
||||
* Set foreign currency to NULL if it's the same as the normal currency:
|
||||
*
|
||||
* @param TransactionCurrency|null $currency
|
||||
* @param TransactionCurrency|null $foreignCurrency
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
*/
|
||||
private function compareCurrencies(?TransactionCurrency $currency, ?TransactionCurrency $foreignCurrency): ?TransactionCurrency
|
||||
{
|
||||
@@ -506,13 +511,8 @@ class TransactionJournalFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param TransactionCurrency|null $foreignCurrency
|
||||
* @param Account $destination
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
* @throws \JsonException
|
||||
*/
|
||||
private function getForeignByAccount(string $type, ?TransactionCurrency $foreignCurrency, Account $destination): ?TransactionCurrency
|
||||
{
|
||||
@@ -523,11 +523,6 @@ class TransactionJournalFactory
|
||||
return $foreignCurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getDescription(string $description): string
|
||||
{
|
||||
$description = '' === $description ? '(empty description)' : $description;
|
||||
@@ -538,13 +533,12 @@ class TransactionJournalFactory
|
||||
/**
|
||||
* Force the deletion of an entire set of transaction journals and their meta object in case of
|
||||
* an error creating a group.
|
||||
*
|
||||
* @param Collection $collection
|
||||
*/
|
||||
private function forceDeleteOnError(Collection $collection): void
|
||||
{
|
||||
app('log')->debug(sprintf('forceDeleteOnError on collection size %d item(s)', $collection->count()));
|
||||
$service = app(JournalDestroyService::class);
|
||||
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($collection as $journal) {
|
||||
app('log')->debug(sprintf('forceDeleteOnError on journal #%d', $journal->id));
|
||||
@@ -552,9 +546,6 @@ class TransactionJournalFactory
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*/
|
||||
private function forceTrDelete(Transaction $transaction): void
|
||||
{
|
||||
$transaction->delete();
|
||||
@@ -562,9 +553,6 @@ class TransactionJournalFactory
|
||||
|
||||
/**
|
||||
* Link a piggy bank to this journal.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $data
|
||||
*/
|
||||
private function storePiggyEvent(TransactionJournal $journal, NullArrayObject $data): void
|
||||
{
|
||||
@@ -581,50 +569,10 @@ class TransactionJournalFactory
|
||||
app('log')->debug('Create no piggy event');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $transaction
|
||||
*/
|
||||
private function storeMetaFields(TransactionJournal $journal, NullArrayObject $transaction): void
|
||||
{
|
||||
foreach ($this->fields as $field) {
|
||||
$this->storeMeta($journal, $transaction, $field);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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] ?? ''),
|
||||
];
|
||||
if ($data[$field] instanceof Carbon) {
|
||||
$data[$field]->setTimezone(config('app.timezone'));
|
||||
app('log')->debug(sprintf('%s Date: %s (%s)', $field, $data[$field], $data[$field]->timezone->getName()));
|
||||
$set['data'] = $data[$field]->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
app('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) {
|
||||
app('log')->info('Will trigger duplication alert for this journal.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -31,19 +31,15 @@ use FireflyIII\Models\TransactionJournalMeta;
|
||||
*/
|
||||
class TransactionJournalMetaFactory
|
||||
{
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionJournalMeta|null
|
||||
*/
|
||||
public function updateOrCreate(array $data): ?TransactionJournalMeta
|
||||
{
|
||||
//app('log')->debug('In updateOrCreate()');
|
||||
// app('log')->debug('In updateOrCreate()');
|
||||
$value = $data['data'];
|
||||
/** @var TransactionJournalMeta|null $entry */
|
||||
|
||||
/** @var null|TransactionJournalMeta $entry */
|
||||
$entry = $data['journal']->transactionJournalMeta()->where('name', $data['name'])->first();
|
||||
if (null === $value && null !== $entry) {
|
||||
//app('log')->debug('Value is empty, delete meta value.');
|
||||
// app('log')->debug('Value is empty, delete meta value.');
|
||||
$entry->delete();
|
||||
|
||||
return null;
|
||||
@@ -65,7 +61,7 @@ class TransactionJournalMetaFactory
|
||||
}
|
||||
|
||||
if (null === $entry) {
|
||||
//app('log')->debug('Will create new object.');
|
||||
// app('log')->debug('Will create new object.');
|
||||
app('log')->debug(sprintf('Going to create new meta-data entry to store "%s".', $data['name']));
|
||||
$entry = new TransactionJournalMeta();
|
||||
$entry->transactionJournal()->associate($data['journal']);
|
||||
|
@@ -31,11 +31,6 @@ use FireflyIII\Models\TransactionType;
|
||||
*/
|
||||
class TransactionTypeFactory
|
||||
{
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return TransactionType|null
|
||||
*/
|
||||
public function find(string $type): ?TransactionType
|
||||
{
|
||||
return TransactionType::whereType(ucfirst($type))->first();
|
||||
|
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
|
||||
/*
|
||||
* UserGroupFactory.php
|
||||
* Copyright (c) 2023 james@firefly-iii.org
|
||||
@@ -37,9 +36,6 @@ use FireflyIII\Models\UserRole;
|
||||
class UserGroupFactory
|
||||
{
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return UserGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function create(array $data): UserGroup
|
||||
@@ -62,5 +58,4 @@ class UserGroupFactory
|
||||
|
||||
return $userGroup;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user