From 6591fa9fb4fdbceb49bf3acf335927b4ba291f3e Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 23 Feb 2018 16:21:28 +0100 Subject: [PATCH] Small adjustments to fix tests. --- .../Account/ReconcileController.php | 2 +- .../Transaction/BulkController.php | 7 +- .../Transaction/SingleController.php | 9 +-- .../Account/AccountRepository.php | 4 +- .../Account/AccountRepositoryInterface.php | 4 +- app/Repositories/Bill/BillRepository.php | 4 +- .../Journal/JournalRepository.php | 69 ++++++++++++++++--- .../Journal/JournalRepositoryInterface.php | 30 ++++++++ .../Internal/Support/JournalServiceTrait.php | 3 + .../Internal/Update/JournalUpdateService.php | 47 ++++++++++++- .../Update/TransactionUpdateService.php | 32 ++++++++- .../Actions/SetDestinationAccount.php | 11 +-- .../Actions/SetSourceAccount.php | 11 +-- 13 files changed, 192 insertions(+), 41 deletions(-) diff --git a/app/Http/Controllers/Account/ReconcileController.php b/app/Http/Controllers/Account/ReconcileController.php index 359c3d3b2c..7ee63d8450 100644 --- a/app/Http/Controllers/Account/ReconcileController.php +++ b/app/Http/Controllers/Account/ReconcileController.php @@ -424,7 +424,7 @@ class ReconcileController extends Controller 'interest_date' => null, 'book_date' => null, 'transactions' => [[ - 'currency_id' => $journal->transaction_currency_id, + 'currency_id' => intval($journal->transaction_currency_id), 'currency_code' => null, 'description' => null, 'amount' => app('steam')->positive($submitted['amount']), diff --git a/app/Http/Controllers/Transaction/BulkController.php b/app/Http/Controllers/Transaction/BulkController.php index 001bb514fc..8930b3554a 100644 --- a/app/Http/Controllers/Transaction/BulkController.php +++ b/app/Http/Controllers/Transaction/BulkController.php @@ -150,23 +150,22 @@ class BulkController extends Controller foreach ($journalIds as $journalId) { $journal = $repository->find(intval($journalId)); if (!is_null($journal)) { - // TODO need to move this to update service. $count++; Log::debug(sprintf('Found journal #%d', $journal->id)); // update category if not told to ignore if ($ignoreCategory === false) { Log::debug(sprintf('Set category to %s', $request->string('category'))); - $service->updateCategory($journal, $request->string('category')); + $repository->updateCategory($journal, $request->string('category')); } // update budget if not told to ignore (and is withdrawal) if ($ignoreBudget === false) { Log::debug(sprintf('Set budget to %d', $request->integer('budget_id'))); - $service->updateBudget($journal, $request->integer('budget_id')); + $repository->updateBudget($journal, $request->integer('budget_id')); } if ($ignoreTags === false) { Log::debug(sprintf('Set tags to %s', $request->string('budget_id'))); - $service->updateTags($journal, explode(',', $request->string('tags'))); + $repository->updateTags($journal, explode(',', $request->string('tags'))); } // update tags if not told to ignore (and is withdrawal) } diff --git a/app/Http/Controllers/Transaction/SingleController.php b/app/Http/Controllers/Transaction/SingleController.php index 2129fdf108..9b2c43dfb7 100644 --- a/app/Http/Controllers/Transaction/SingleController.php +++ b/app/Http/Controllers/Transaction/SingleController.php @@ -26,7 +26,6 @@ use Carbon\Carbon; use ExpandedForm; use FireflyIII\Events\StoredTransactionJournal; use FireflyIII\Events\UpdatedTransactionJournal; -use FireflyIII\Factory\TransactionJournalFactory; use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\JournalFormRequest; @@ -366,13 +365,7 @@ class SingleController extends Controller 'foreign_amount' => null, ], ]; - var_dump($data);exit; - // todo call factory instead of repository - /** @var TransactionJournalFactory $factory */ - $factory = app(TransactionJournalFactory::class); - $factory->setUser(auth()->user()); - $journal = $factory->create($data); - //$journal = $repository->store($data); + $journal = $repository->store($data); if (null === $journal->id) { // error! Log::error('Could not store transaction journal: ', $journal->getErrors()->toArray()); diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 8f7103b749..601e2b4473 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -61,13 +61,13 @@ class AccountRepository implements AccountRepositoryInterface * Moved here from account CRUD. * * @param Account $account - * @param Account $moveTo + * @param Account|null $moveTo * * @return bool * * @throws \Exception */ - public function destroy(Account $account, Account $moveTo): bool + public function destroy(Account $account, ?Account $moveTo): bool { /** @var AccountDestroyService $service */ $service = app(AccountDestroyService::class); diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index 170eaf8508..70b74070b5 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -48,11 +48,11 @@ interface AccountRepositoryInterface * Moved here from account CRUD. * * @param Account $account - * @param Account $moveTo + * @param Account|null $moveTo * * @return bool */ - public function destroy(Account $account, Account $moveTo): bool; + public function destroy(Account $account, ?Account $moveTo): bool; /** * @param int $accountId diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index bf4f03cf1a..48a17e9ea1 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -106,8 +106,8 @@ class BillRepository implements BillRepositoryInterface /** @var Collection $set */ $set = $this->user->bills() ->where('active', 1) - ->sortBy('name') - ->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount'),]); + ->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount'),]) + ->sortBy('name'); return $set; } diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 2621dc19d2..1fa49a0174 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -195,6 +195,18 @@ class JournalRepository implements JournalRepositoryInterface return null; } + /** + * Get account of transaction that is more than zero. Only works with unsplit journals. + * + * @param TransactionJournal $journal + * + * @return Account + */ + public function getDestinationAccount(TransactionJournal $journal): Account + { + return $journal->transactions()->where('amount', '<', 0)->first()->account; + } + /** * @param TransactionJournal $journal * @@ -205,6 +217,18 @@ class JournalRepository implements JournalRepositoryInterface return $journal->notes()->first(); } + /** + * Get account of transaction that is less than zero. Only works with unsplit journals. + * + * @param TransactionJournal $journal + * + * @return Account + */ + public function getSourceAccount(TransactionJournal $journal): Account + { + return $journal->transactions()->where('amount', '>', 0)->first()->account; + } + /** * @return Collection */ @@ -318,26 +342,55 @@ class JournalRepository implements JournalRepositoryInterface } /** - * Get account of transaction that is more than zero. Only works with unsplit journals. + * Update budget for a journal. * * @param TransactionJournal $journal + * @param int $budgetId * - * @return Account + * @return TransactionJournal */ - public function getDestinationAccount(TransactionJournal $journal): Account + public function updateBudget(TransactionJournal $journal, int $budgetId): TransactionJournal { - return $journal->transactions()->where('amount','<',0)->first()->account; + /** @var JournalUpdateService $service */ + $service = app(JournalUpdateService::class); + $service->setUser($this->user); + + return $service->updateBudget($journal, $budgetId); } /** - * Get account of transaction that is less than zero. Only works with unsplit journals. + * Update category for a journal. * * @param TransactionJournal $journal + * @param string $category * - * @return Account + * @return TransactionJournal */ - public function getSourceAccount(TransactionJournal $journal): Account + public function updateCategory(TransactionJournal $journal, string $category): TransactionJournal { - return $journal->transactions()->where('amount','>',0)->first()->account; + /** @var JournalUpdateService $service */ + $service = app(JournalUpdateService::class); + $service->setUser($this->user); + + return $service->updateCategory($journal, $category); + } + + /** + * Update tag(s) for a journal. + * + * @param TransactionJournal $journal + * @param array $tags + * + * @return TransactionJournal + */ + public function updateTags(TransactionJournal $journal, array $tags): TransactionJournal + { + /** @var JournalUpdateService $service */ + $service = app(JournalUpdateService::class); + $service->setUser($this->user); + $service->connectTags($journal, $tags); + + return $journal; + } } diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php index cd606cb813..4a73ce0f62 100644 --- a/app/Repositories/Journal/JournalRepositoryInterface.php +++ b/app/Repositories/Journal/JournalRepositoryInterface.php @@ -171,4 +171,34 @@ interface JournalRepositoryInterface * @return TransactionJournal */ public function update(TransactionJournal $journal, array $data): TransactionJournal; + + /** + * Update budget for a journal. + * + * @param TransactionJournal $journal + * @param int $budgetId + * + * @return TransactionJournal + */ + public function updateBudget(TransactionJournal $journal, int $budgetId): TransactionJournal; + + /** + * Update category for a journal. + * + * @param TransactionJournal $journal + * @param string $category + * + * @return TransactionJournal + */ + public function updateCategory(TransactionJournal $journal, string $category): TransactionJournal; + + /** + * Update tag(s) for a journal. + * + * @param TransactionJournal $journal + * @param array $tags + * + * @return TransactionJournal + */ + public function updateTags(TransactionJournal $journal, array $tags): TransactionJournal; } diff --git a/app/Services/Internal/Support/JournalServiceTrait.php b/app/Services/Internal/Support/JournalServiceTrait.php index 6d0322b08e..8078f77d86 100644 --- a/app/Services/Internal/Support/JournalServiceTrait.php +++ b/app/Services/Internal/Support/JournalServiceTrait.php @@ -73,6 +73,9 @@ trait JournalServiceTrait $factory = app(TagFactory::class); $factory->setUser($journal->user); $set = []; + if (!is_array($data['tags'])) { + return; + } foreach ($data['tags'] as $string) { if (strlen($string) > 0) { $tag = $factory->findOrCreate($string); diff --git a/app/Services/Internal/Update/JournalUpdateService.php b/app/Services/Internal/Update/JournalUpdateService.php index a862ae864b..e5a00f3d2e 100644 --- a/app/Services/Internal/Update/JournalUpdateService.php +++ b/app/Services/Internal/Update/JournalUpdateService.php @@ -23,10 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Services\Internal\Update; -use FireflyIII\Factory\BillFactory; -use FireflyIII\Factory\TagFactory; use FireflyIII\Factory\TransactionFactory; -use FireflyIII\Factory\TransactionJournalMetaFactory; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Services\Internal\Support\JournalServiceTrait; @@ -128,4 +125,48 @@ class JournalUpdateService return $journal; } + /** + * Update budget for a journal. + * + * @param TransactionJournal $journal + * @param int $budgetId + * + * @return TransactionJournal + */ + public function updateBudget(TransactionJournal $journal, int $budgetId): TransactionJournal + { + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user); + + /** @var Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + $service->updateBudget($transaction, $budgetId); + } + + return $journal; + } + + /** + * Update category for a journal. + * + * @param TransactionJournal $journal + * @param string $category + * + * @return TransactionJournal + */ + public function updateCategory(TransactionJournal $journal, string $category): TransactionJournal + { + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user); + + /** @var Transaction $transaction */ + foreach ($journal->transactions as $transaction) { + $service->updateCategory($transaction, $category); + } + + return $journal; + } + } \ No newline at end of file diff --git a/app/Services/Internal/Update/TransactionUpdateService.php b/app/Services/Internal/Update/TransactionUpdateService.php index 37275627cc..464ebad2c6 100644 --- a/app/Services/Internal/Update/TransactionUpdateService.php +++ b/app/Services/Internal/Update/TransactionUpdateService.php @@ -38,7 +38,6 @@ class TransactionUpdateService /** @var User */ private $user; - /** * @param int $transactionId * @@ -130,5 +129,36 @@ class TransactionUpdateService return $transaction; } + /** + * Update budget for a journal. + * + * @param Transaction $transaction + * @param int $budgetId + * + * @return Transaction + */ + public function updateBudget(Transaction $transaction, int $budgetId): Transaction + { + $budget = $this->findBudget($budgetId, null); + $this->setBudget($transaction, $budget); + return $transaction; + + } + + /** + * Update category for a journal. + * + * @param Transaction $transaction + * @param string $category + * + * @return Transaction + */ + public function updateCategory(Transaction $transaction, string $category): Transaction + { + $category = $this->findCategory(0, $category); + $this->setCategory($transaction, $category); + + return $category; + } } \ No newline at end of file diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.php index f2869624b5..59d7577d9f 100644 --- a/app/TransactionRules/Actions/SetDestinationAccount.php +++ b/app/TransactionRules/Actions/SetDestinationAccount.php @@ -137,11 +137,12 @@ class SetDestinationAccount implements ActionInterface if (null === $account) { // create new revenue account with this name: $data = [ - 'name' => $this->action->action_value, - 'accountType' => 'expense', - 'virtualBalance' => 0, - 'active' => true, - 'iban' => null, + 'name' => $this->action->action_value, + 'accountType' => 'expense', + 'account_type_id' => null, + 'virtualBalance' => 0, + 'active' => true, + 'iban' => null, ]; $account = $this->repository->store($data); } diff --git a/app/TransactionRules/Actions/SetSourceAccount.php b/app/TransactionRules/Actions/SetSourceAccount.php index 1f90c8040e..570d697bbd 100644 --- a/app/TransactionRules/Actions/SetSourceAccount.php +++ b/app/TransactionRules/Actions/SetSourceAccount.php @@ -136,11 +136,12 @@ class SetSourceAccount implements ActionInterface if (null === $account) { // create new revenue account with this name: $data = [ - 'name' => $this->action->action_value, - 'accountType' => 'revenue', - 'virtualBalance' => 0, - 'active' => true, - 'iban' => null, + 'name' => $this->action->action_value, + 'accountType' => 'revenue', + 'account_type_id' => null, + 'virtualBalance' => 0, + 'active' => true, + 'iban' => null, ]; $account = $this->repository->store($data); }