diff --git a/app/Api/V1/Controllers/AccountController.php b/app/Api/V1/Controllers/AccountController.php index d1f76c53c8..81adb7f2bf 100644 --- a/app/Api/V1/Controllers/AccountController.php +++ b/app/Api/V1/Controllers/AccountController.php @@ -80,7 +80,7 @@ class AccountController extends Controller */ public function delete(Account $account) { - $this->repository->destroy($account, new Account); + $this->repository->destroy($account, null); return response()->json([], 204); } diff --git a/app/Api/V1/Controllers/TransactionController.php b/app/Api/V1/Controllers/TransactionController.php index 4ffb7a1875..7510679f17 100644 --- a/app/Api/V1/Controllers/TransactionController.php +++ b/app/Api/V1/Controllers/TransactionController.php @@ -33,7 +33,7 @@ use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; -use FireflyIII\Services\Internal\JournalUpdateService; +use FireflyIII\Services\Internal\Update\JournalUpdateService; use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Http\Request; use Illuminate\Support\Collection; @@ -91,7 +91,6 @@ class TransactionController extends Controller * @param Request $request * * @return \Illuminate\Http\JsonResponse - * @throws \FireflyIII\Exceptions\FireflyException */ public function index(Request $request) { @@ -152,7 +151,6 @@ class TransactionController extends Controller $include = $request->get('include') ?? ''; $manager->parseIncludes($include); - // needs a lot of extra data to match the journal collector. Or just expand that one. // collect transactions using the journal collector $collector = app(JournalCollectorInterface::class); $collector->setUser(auth()->user()); diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index afde27a421..d29d5a458f 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -139,7 +139,7 @@ class AccountController extends Controller $type = $account->accountType->type; $typeName = config('firefly.shortNamesByFullName.' . $type); $name = $account->name; - $moveTo = $this->repository->find(intval($request->get('move_account_before_delete'))); + $moveTo = $this->repository->findNull(intval($request->get('move_account_before_delete'))); $this->repository->destroy($account, $moveTo); diff --git a/app/Http/Controllers/Transaction/SingleController.php b/app/Http/Controllers/Transaction/SingleController.php index 0ee52c19be..2129fdf108 100644 --- a/app/Http/Controllers/Transaction/SingleController.php +++ b/app/Http/Controllers/Transaction/SingleController.php @@ -334,17 +334,44 @@ class SingleController extends Controller * @param JournalRepositoryInterface $repository * * @return \Illuminate\Http\RedirectResponse + * @throws \FireflyIII\Exceptions\FireflyException */ public function store(JournalFormRequest $request, JournalRepositoryInterface $repository) { - $doSplit = 1 === intval($request->get('split_journal')); - $createAnother = 1 === intval($request->get('create_another')); - $data = $request->getJournalData(); - + $doSplit = 1 === intval($request->get('split_journal')); + $createAnother = 1 === intval($request->get('create_another')); + $data = $request->getJournalData(); + $data['user'] = auth()->user()->id; + $data['bill_id'] = null; + $data['bill_name'] = null; + $data['piggy_bank_name'] = null; + $data['transactions'] = [ + [ + 'amount' => $data['amount'], + 'currency_id' => $data['currency_id'], + 'description' => null, + 'reconciled' => false, + 'identifier' => 0, + 'currency_code' => null, + 'budget_id' => $data['budget_id'], + 'budget_name' => null, + 'category_id' => null, + 'category_name' => $data['category'], + 'source_id' => (int)$data['source_account_id'], + 'source_name' => $data['source_account_name'], + 'destination_id' => (int)$data['destination_account_id'], + 'destination_name' => $data['destination_account_name'], + 'foreign_currency_id' => null, + 'foreign_currency_code' => null, + '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 = $repository->store($data); + $journal = $factory->create($data); //$journal = $repository->store($data); if (null === $journal->id) { // error! diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index f23dcdd650..5db610e704 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -47,7 +47,7 @@ class JournalFormRequest extends Request public function getJournalData() { $data = [ - 'what' => $this->get('what'), // type. can be 'deposit', 'withdrawal' or 'transfer' + 'type' => $this->get('what'), // type. can be 'deposit', 'withdrawal' or 'transfer' 'date' => $this->date('date'), 'tags' => explode(',', $this->string('tags')), 'currency_id' => $this->integer('amount_currency_id_amount'), diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 9cd5f2d8d5..55ef84493d 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -122,20 +122,7 @@ class EventServiceProvider extends ServiceProvider */ protected function registerDeleteEvents() { - Account::deleted( - function (Account $account) { - Log::debug('Now trigger account delete response #' . $account->id); - /** @var Transaction $transaction */ - foreach ($account->transactions()->get() as $transaction) { - Log::debug('Now at transaction #' . $transaction->id); - $journal = $transaction->transactionJournal()->first(); - if (null !== $journal) { - Log::debug('Call for deletion of journal #' . $journal->id); - $journal->delete(); - } - } - } - ); + TransactionJournal::deleted( function (TransactionJournal $journal) { diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 469a83667d..f31a6f8f52 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -35,6 +35,7 @@ use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Tag\TagRepositoryInterface; +use FireflyIII\Services\Internal\Destroy\AccountDestroyService; use FireflyIII\User; use Log; use Validator; @@ -82,12 +83,9 @@ class AccountRepository implements AccountRepositoryInterface */ public function destroy(Account $account, Account $moveTo): bool { - if (null !== $moveTo->id) { - DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]); - } - if (null !== $account) { - $account->delete(); - } + /** @var AccountDestroyService $service */ + $service = app(AccountDestroyService::class); + $service->destroy($account, $moveTo); return true; } diff --git a/app/Services/Internal/Destroy/AccountDestroyService.php b/app/Services/Internal/Destroy/AccountDestroyService.php new file mode 100644 index 0000000000..27db718d0c --- /dev/null +++ b/app/Services/Internal/Destroy/AccountDestroyService.php @@ -0,0 +1,68 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Services\Internal\Destroy; + +use DB; +use FireflyIII\Models\Account; +use FireflyIII\Models\Transaction; +use Log; + +/** + * Class AccountDestroyService + */ +class AccountDestroyService +{ + /** + * @param Account $account + * @param Account|null $moveTo + * + * @return bool + */ + public function destroy(Account $account, ?Account $moveTo): bool + { + if (null !== $moveTo) { + DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]); + } + + Log::debug('Now trigger account delete response #' . $account->id); + /** @var Transaction $transaction */ + foreach ($account->transactions()->get() as $transaction) { + Log::debug('Now at transaction #' . $transaction->id); + $journal = $transaction->transactionJournal()->first(); + if (null !== $journal) { + Log::debug('Call for deletion of journal #' . $journal->id); + $journal->delete(); + } + } + try { + $account->delete(); + } catch (\Exception $e) { + // don't care + Log::error($e->getMessage()); + } + + return true; + } + +} \ No newline at end of file diff --git a/app/Services/Internal/JournalUpdateService.php b/app/Services/Internal/Update/JournalUpdateService.php similarity index 99% rename from app/Services/Internal/JournalUpdateService.php rename to app/Services/Internal/Update/JournalUpdateService.php index 76f50c017c..d08c66c5cb 100644 --- a/app/Services/Internal/JournalUpdateService.php +++ b/app/Services/Internal/Update/JournalUpdateService.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace FireflyIII\Services\Internal; +namespace FireflyIII\Services\Internal\Update; use FireflyIII\Factory\BillFactory; use FireflyIII\Factory\TagFactory; diff --git a/app/Services/Internal/TransactionUpdateService.php b/app/Services/Internal/Update/TransactionUpdateService.php similarity index 99% rename from app/Services/Internal/TransactionUpdateService.php rename to app/Services/Internal/Update/TransactionUpdateService.php index c175a8fa5c..f7ccc2ca54 100644 --- a/app/Services/Internal/TransactionUpdateService.php +++ b/app/Services/Internal/Update/TransactionUpdateService.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace FireflyIII\Services\Internal; +namespace FireflyIII\Services\Internal\Update; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\AccountFactory;