|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Banking; |
| 4 | + |
| 5 | +use HMS\Entities\User; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use HMS\Entities\Banking\Account; |
| 8 | +use App\Http\Controllers\Controller; |
| 9 | +use HMS\Repositories\UserRepository; |
| 10 | +use HMS\Factories\Banking\AccountFactory; |
| 11 | +use HMS\Repositories\Banking\AccountRepository; |
| 12 | + |
| 13 | +class AccountController extends Controller |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var AccountRepository |
| 17 | + */ |
| 18 | + protected $accountRepository; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var AccountFactory |
| 22 | + */ |
| 23 | + protected $accountFactory; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var UserRepository |
| 27 | + */ |
| 28 | + private $userRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * Create a new controller instance. |
| 32 | + * |
| 33 | + * @param AccountRepository $accountRepository |
| 34 | + * @param UserRepository $userRepository |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + AccountRepository $accountRepository, |
| 38 | + AccountFactory $accountFactory, |
| 39 | + UserRepository $userRepository |
| 40 | + ) { |
| 41 | + $this->accountRepository = $accountRepository; |
| 42 | + $this->accountFactory = $accountFactory; |
| 43 | + $this->userRepository = $userRepository; |
| 44 | + |
| 45 | + $this->middleware('can:profile.view.all')->only(['listJoint', 'show']); |
| 46 | + $this->middleware('can:profile.edit.all')->only(['linkUser', 'unlinkUser']); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Display a listing of the join Acounts. |
| 51 | + * |
| 52 | + * @return \Illuminate\Http\Response |
| 53 | + */ |
| 54 | + public function listJoint() |
| 55 | + { |
| 56 | + $joinAccounts = $this->accountRepository->paginateJointAccounts(); |
| 57 | + |
| 58 | + return view('banking.accounts.joint') |
| 59 | + ->with('jointAccounts', $joinAccounts); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Display the specified Acount. |
| 64 | + * |
| 65 | + * @param Account $account |
| 66 | + * |
| 67 | + * @return \Illuminate\Http\Response |
| 68 | + */ |
| 69 | + public function show(Account $account) |
| 70 | + { |
| 71 | + // $bankTransactions = $this->bankTransactionRepository->paginateByAccount($user->getAccount(), 10); |
| 72 | + |
| 73 | + return view('banking.accounts.show') |
| 74 | + ->with('account', $account); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Link a given User with this Account. |
| 79 | + * |
| 80 | + * @param Account $account |
| 81 | + * @param \Illuminate\Http\Request $request |
| 82 | + * |
| 83 | + * @return \Illuminate\Http\Response |
| 84 | + */ |
| 85 | + public function linkUser(Account $account, Request $request) |
| 86 | + { |
| 87 | + $valiidatedDate = $request->validate([ |
| 88 | + 'user_id' => [ |
| 89 | + 'required', |
| 90 | + 'exists:HMS\Entities\User,id', |
| 91 | + ], |
| 92 | + ]); |
| 93 | + |
| 94 | + $user = $this->userRepository->findOneById($valiidatedDate['user_id']); |
| 95 | + |
| 96 | + // TODO: As this will changes a Users account_id, we will orphan the old Account ref |
| 97 | + // if there are no bank_transaction against the ref we should be safe to delete it? |
| 98 | + $oldAccount = $user->getAccount(); |
| 99 | + |
| 100 | + $user->setAccount($account); |
| 101 | + $this->userRepository->save($user); |
| 102 | + |
| 103 | + // TODO: fire some user account changed event? |
| 104 | +
|
| 105 | + flash($user->getFullname() . ' linked to Account.')->success(); |
| 106 | + |
| 107 | + return redirect()->route('banking.accounts.show', $account->getId()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Unlink a given User with this Account. |
| 112 | + * |
| 113 | + * @param Account $account |
| 114 | + * @param \Illuminate\Http\Request $request |
| 115 | + * |
| 116 | + * @return \Illuminate\Http\Response |
| 117 | + */ |
| 118 | + public function unlinkUser(Account $account, Request $request) |
| 119 | + { |
| 120 | + $valiidatedDate = $request->validate([ |
| 121 | + 'user_id' => [ |
| 122 | + 'required', |
| 123 | + 'exists:HMS\Entities\User,id', |
| 124 | + ], |
| 125 | + 'new-account' => 'required|boolean', |
| 126 | + 'existing-account' => 'required_if:new-account,false|exists:HMS\Entities\Banking\Account,id', |
| 127 | + ]); |
| 128 | + |
| 129 | + $user = $this->userRepository->findOneById($valiidatedDate['user_id']); |
| 130 | + |
| 131 | + if ($valiidatedDate['new-account']) { |
| 132 | + $newAccount = $this->accountFactory->createNewAccount(); |
| 133 | + } else { |
| 134 | + $newAccount = $this->accountRepository->findOneById($valiidatedDate['existing-account']); |
| 135 | + } |
| 136 | + |
| 137 | + $user->setAccount($newAccount); |
| 138 | + $this->userRepository->save($user); |
| 139 | + |
| 140 | + // TODO: fire some user account changed event? |
| 141 | +
|
| 142 | + flash($user->getFullname() . ' un-linked from Account.')->success(); |
| 143 | + |
| 144 | + return redirect()->route('banking.accounts.show', $account->getId()); |
| 145 | + } |
| 146 | +} |
0 commit comments