|
1
|
|
|
<?php namespace BB\Http\Controllers; |
|
2
|
|
|
|
|
3
|
|
|
use BB\Entities\User; |
|
4
|
|
|
use BB\Exceptions\AuthenticationException; |
|
5
|
|
|
use BB\Exceptions\ValidationException; |
|
6
|
|
|
use BB\Repo\PaymentRepository; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
class BalanceController extends Controller |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var \BB\Repo\UserRepository |
|
14
|
|
|
*/ |
|
15
|
|
|
private $userRepository; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \BB\Services\Credit |
|
18
|
|
|
*/ |
|
19
|
|
|
private $bbCredit; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var PaymentRepository |
|
22
|
|
|
*/ |
|
23
|
|
|
private $paymentRepository; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var \BB\Validators\WithdrawalValidator |
|
26
|
|
|
*/ |
|
27
|
|
|
private $withdrawalValidator; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(\BB\Repo\UserRepository $userRepository, \BB\Services\Credit $bbCredit, PaymentRepository $paymentRepository, \BB\Validators\WithdrawalValidator $withdrawalValidator) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->userRepository = $userRepository; |
|
32
|
|
|
$this->bbCredit = $bbCredit; |
|
33
|
|
|
$this->paymentRepository = $paymentRepository; |
|
34
|
|
|
$this->withdrawalValidator = $withdrawalValidator; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function index($userId) |
|
38
|
|
|
{ |
|
39
|
|
|
//Verify the user can access this user record |
|
40
|
|
|
$user = User::findWithPermission($userId); |
|
41
|
|
|
$this->bbCredit->setUserId($user->id); |
|
42
|
|
|
|
|
43
|
|
|
$userBalance = $this->bbCredit->getBalanceFormatted(); |
|
44
|
|
|
$userBalanceSign = $this->bbCredit->getBalanceSign(); |
|
45
|
|
|
|
|
46
|
|
|
$payments = $this->bbCredit->getBalancePaymentsPaginated(); |
|
47
|
|
|
|
|
48
|
|
|
$memberList = $this->userRepository->getAllAsDropdown(); |
|
49
|
|
|
|
|
50
|
|
|
return \View::make('account.bbcredit.index') |
|
51
|
|
|
->with('user', $user) |
|
52
|
|
|
->with('payments', $payments) |
|
53
|
|
|
->with('userBalance', $userBalance) |
|
54
|
|
|
->with('userBalanceSign', $userBalanceSign) |
|
55
|
|
|
->with('rawBalance', number_format($user->cash_balance / 100, 2)) |
|
|
|
|
|
|
56
|
|
|
->with('memberList', $memberList); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function withdrawal(Request $request, $userId) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->withdrawalValidator->validate(\Request::only(['amount', 'sort_code', 'account_number'])); |
|
62
|
|
|
|
|
63
|
|
|
$user = User::findWithPermission($userId); |
|
64
|
|
|
$amount = $request->get('amount'); |
|
65
|
|
|
$sortCode = $request->get('sort_code'); |
|
66
|
|
|
$accountNumber = $request->get('account_number'); |
|
67
|
|
|
|
|
68
|
|
|
$memberName = $user->name; |
|
69
|
|
|
\Mail::queue('emails.withdrawal', [ |
|
70
|
|
|
'memberName' => $memberName, |
|
71
|
|
|
'amount' => $amount, |
|
72
|
|
|
'sortCode' => $sortCode, |
|
73
|
|
|
'accountNumber' => $accountNumber |
|
74
|
|
|
], function ($message) { |
|
75
|
|
|
$message->to('[email protected]', 'Arthur Guy')->subject('User requested a withdrawal'); |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
\Notification::success("Request sent"); |
|
|
|
|
|
|
79
|
|
|
return \Redirect::route('account.balance.index', $user->id); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* This is a basic method for recording a payment transfer between two people |
|
85
|
|
|
* This should not exist and the normal balance payment controller should be used |
|
86
|
|
|
* If any more work is needed here please take the time and move it over! |
|
87
|
|
|
* |
|
88
|
|
|
* @param Request $request |
|
89
|
|
|
* @param integer $userId |
|
90
|
|
|
* |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
* @throws ValidationException |
|
93
|
|
|
* @throws AuthenticationException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function recordTransfer(Request $request, $userId) |
|
96
|
|
|
{ |
|
97
|
|
|
$user = User::findWithPermission($userId); |
|
98
|
|
|
$this->bbCredit->setUserId($user->id); |
|
99
|
|
|
|
|
100
|
|
|
$amount = $request->get('amount'); |
|
101
|
|
|
$targetUserId = $request->get('target_user_id'); |
|
102
|
|
|
$targetUser = $this->userRepository->getById($targetUserId); |
|
103
|
|
|
|
|
104
|
|
|
if ($targetUserId === $userId) { |
|
105
|
|
|
throw new ValidationException('Your\'e trying to send money to yourself, no!'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
//What is the users balance |
|
109
|
|
|
$userBalance = $this->bbCredit->getBalance(); |
|
110
|
|
|
|
|
111
|
|
|
//With this payment will the users balance go to low? |
|
112
|
|
|
if (($userBalance - $amount) < 0) { |
|
113
|
|
|
|
|
114
|
|
|
\Notification::error("You don't have the money for this"); |
|
115
|
|
|
return \Redirect::route('account.balance.index', $user->id); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->paymentRepository->recordBalanceTransfer($user->id, $targetUser->id, $amount); |
|
119
|
|
|
|
|
120
|
|
|
\Notification::success("Transfer made"); |
|
|
|
|
|
|
121
|
|
|
return \Redirect::route('account.balance.index', $user->id); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.