mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 06:31:22 +00:00
Move common methods to traits
This commit is contained in:
@@ -25,15 +25,9 @@ namespace FireflyIII\Factory;
|
||||
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Services\Internal\Support\TransactionServiceTrait;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
@@ -42,19 +36,11 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
class TransactionFactory
|
||||
{
|
||||
/** @var AccountRepositoryInterface */
|
||||
private $accountRepository;
|
||||
use TransactionServiceTrait;
|
||||
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* TransactionFactory constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
@@ -87,6 +73,7 @@ class TransactionFactory
|
||||
*
|
||||
* @return Collection
|
||||
* @throws FireflyException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function createPair(TransactionJournal $journal, array $data): Collection
|
||||
{
|
||||
@@ -158,198 +145,7 @@ class TransactionFactory
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->accountRepository->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $direction
|
||||
*
|
||||
* @return string
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function accountType(TransactionJournal $journal, string $direction): string
|
||||
{
|
||||
$types = [];
|
||||
$type = $journal->transactionType->type;
|
||||
switch ($type) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('Cannot handle type "%s" in accountType()', $type));
|
||||
case TransactionType::WITHDRAWAL:
|
||||
$types['source'] = AccountType::ASSET;
|
||||
$types['destination'] = AccountType::EXPENSE;
|
||||
break;
|
||||
case TransactionType::DEPOSIT:
|
||||
$types['source'] = AccountType::REVENUE;
|
||||
$types['destination'] = AccountType::ASSET;
|
||||
break;
|
||||
case TransactionType::TRANSFER:
|
||||
$types['source'] = AccountType::ASSET;
|
||||
$types['destination'] = AccountType::ASSET;
|
||||
break;
|
||||
}
|
||||
if (!isset($types[$direction])) {
|
||||
throw new FireflyException(sprintf('No type set for direction "%s" and type "%s"', $type, $direction));
|
||||
}
|
||||
|
||||
return $types[$direction];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $expectedType
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function findAccount(string $expectedType, ?int $accountId, ?string $accountName): Account
|
||||
{
|
||||
$accountId = intval($accountId);
|
||||
$accountName = strval($accountName);
|
||||
|
||||
switch ($expectedType) {
|
||||
case AccountType::ASSET:
|
||||
if ($accountId > 0) {
|
||||
// must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
return $this->accountRepository->findNull($accountId);
|
||||
}
|
||||
|
||||
// alternatively, return by name. Validator should catch invalid names.
|
||||
return $this->accountRepository->findByName($accountName, [AccountType::ASSET]);
|
||||
break;
|
||||
case AccountType::EXPENSE:
|
||||
if ($accountId > 0) {
|
||||
// must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
return $this->accountRepository->findNull($accountId);
|
||||
}
|
||||
if (strlen($accountName) > 0) {
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate($accountName, AccountType::EXPENSE);
|
||||
}
|
||||
|
||||
// return cash account:
|
||||
return $this->accountRepository->getCashAccount();
|
||||
break;
|
||||
case AccountType::REVENUE:
|
||||
if ($accountId > 0) {
|
||||
// must be able to find it based on ID. Validator should catch invalid ID's.
|
||||
return $this->accountRepository->findNull($accountId);
|
||||
}
|
||||
if (strlen($accountName) > 0) {
|
||||
// alternatively, return by name.
|
||||
/** @var AccountFactory $factory */
|
||||
$factory = app(AccountFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate($accountName, AccountType::REVENUE);
|
||||
}
|
||||
|
||||
// return cash account:
|
||||
return $this->accountRepository->getCashAccount();
|
||||
|
||||
default:
|
||||
throw new FireflyException(sprintf('Cannot find account of type "%s".', $expectedType));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $budgetId
|
||||
* @param null|string $budgetName
|
||||
*
|
||||
* @return Budget|null
|
||||
*/
|
||||
protected function findBudget(?int $budgetId, ?string $budgetName): ?Budget
|
||||
{
|
||||
/** @var BudgetFactory $factory */
|
||||
$factory = app(BudgetFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->find($budgetId, $budgetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $categoryId
|
||||
* @param null|string $categoryName
|
||||
*
|
||||
* @return Category|null
|
||||
*/
|
||||
protected function findCategory(?int $categoryId, ?string $categoryName): ?Category
|
||||
{
|
||||
/** @var CategoryFactory $factory */
|
||||
$factory = app(CategoryFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
return $factory->findOrCreate($categoryId, $categoryName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $currencyId
|
||||
* @param null|string $currencyCode
|
||||
*
|
||||
* @return TransactionCurrency|null
|
||||
*/
|
||||
protected function findCurrency(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
|
||||
{
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
|
||||
return $factory->find($currencyId, $currencyCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param Budget|null $budget
|
||||
*/
|
||||
protected function setBudget(Transaction $transaction, ?Budget $budget): void
|
||||
{
|
||||
if (is_null($budget)) {
|
||||
return;
|
||||
}
|
||||
$transaction->budgets()->save($budget);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param Category|null $category
|
||||
*/
|
||||
protected function setCategory(Transaction $transaction, ?Category $category): void
|
||||
{
|
||||
if (is_null($category)) {
|
||||
return;
|
||||
}
|
||||
$transaction->categories()->save($category);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string $amount
|
||||
*/
|
||||
protected function setForeignAmount(Transaction $transaction, string $amount): void
|
||||
{
|
||||
$transaction->foreign_amount = $amount;
|
||||
$transaction->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param TransactionCurrency|null $currency
|
||||
*/
|
||||
protected function setForeignCurrency(Transaction $transaction, ?TransactionCurrency $currency): void
|
||||
{
|
||||
if (is_null($currency)) {
|
||||
return;
|
||||
}
|
||||
$transaction->foreign_currency_id = $currency->id;
|
||||
$transaction->save();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -27,13 +27,16 @@ use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Services\Internal\Support\JournalServiceTrait;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class TransactionJournalFactory
|
||||
*/
|
||||
class TransactionJournalFactory
|
||||
{
|
||||
use JournalServiceTrait;
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
@@ -47,6 +50,7 @@ class TransactionJournalFactory
|
||||
*/
|
||||
public function create(array $data): TransactionJournal
|
||||
{
|
||||
Log::debug('Start of TransactionJournalFactory::create()');
|
||||
// store basic journal first.
|
||||
$type = $this->findTransactionType($data['type']);
|
||||
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||
@@ -95,7 +99,7 @@ class TransactionJournalFactory
|
||||
$this->storeMeta($journal, $data, 'payment_date');
|
||||
$this->storeMeta($journal, $data, 'invoice_date');
|
||||
$this->storeMeta($journal, $data, 'internal_reference');
|
||||
|
||||
Log::debug('End of TransactionJournalFactory::create()');
|
||||
return $journal;
|
||||
}
|
||||
|
||||
@@ -109,25 +113,6 @@ class TransactionJournalFactory
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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($this->user);
|
||||
$bill = $factory->find($data['bill_id'], $data['bill_name']);
|
||||
|
||||
if (!is_null($bill)) {
|
||||
$journal->bill_id = $bill->id;
|
||||
$journal->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
@@ -146,23 +131,6 @@ class TransactionJournalFactory
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
*/
|
||||
protected function connectTags(TransactionJournal $journal, array $data): void
|
||||
{
|
||||
$factory = app(TagFactory::class);
|
||||
$factory->setUser($journal->user);
|
||||
if (is_null($data['tags'])) {
|
||||
return;
|
||||
}
|
||||
foreach ($data['tags'] as $string) {
|
||||
$tag = $factory->findOrCreate($string);
|
||||
$journal->tags()->save($tag);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the transaction type. Since this is mandatory, will throw an exception when nothing comes up. Will always
|
||||
* use TransactionType repository.
|
||||
@@ -183,39 +151,4 @@ class TransactionJournalFactory
|
||||
return $transactionType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param array $data
|
||||
* @param string $field
|
||||
*/
|
||||
protected function storeMeta(TransactionJournal $journal, array $data, string $field): void
|
||||
{
|
||||
$value = $data[$field] ?? null;
|
||||
if (!is_null($value)) {
|
||||
$set = [
|
||||
'journal' => $journal,
|
||||
'name' => $field,
|
||||
'data' => $data[$field],
|
||||
];
|
||||
/** @var TransactionJournalMetaFactory $factory */
|
||||
$factory = app(TransactionJournalMetaFactory::class);
|
||||
$factory->updateOrCreate($set);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $notes
|
||||
*/
|
||||
protected function storeNote(TransactionJournal $journal, string $notes): void
|
||||
{
|
||||
if (strlen($notes) > 0) {
|
||||
$note = new Note;
|
||||
$note->noteable()->associate($journal);
|
||||
$note->text = $notes;
|
||||
$note->save();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user