/**
  * Start the creation of a new balance payment
  *   Details get posted into this method
  * @param $userId
  * @throws \BB\Exceptions\AuthenticationException
  * @throws \BB\Exceptions\FormValidationException
  * @throws \BB\Exceptions\NotImplementedException
  */
 public function store($userId)
 {
     $user = User::findWithPermission($userId);
     $this->bbCredit->setUserId($user->id);
     $requestData = \Request::only(['reason', 'amount', 'return_path', 'ref']);
     $amount = $requestData['amount'] * 1 / 100;
     $reason = $requestData['reason'];
     $returnPath = $requestData['return_path'];
     $ref = $requestData['ref'];
     //Can the users balance go below 0
     $minimumBalance = $this->bbCredit->acceptableNegativeBalance($reason);
     //What is the users balance
     $userBalance = $this->bbCredit->getBalance();
     //With this payment will the users balance go to low?
     if ($userBalance - $amount < $minimumBalance) {
         if (\Request::wantsJson()) {
             return \Response::json(['error' => 'You don\'t have the money for this'], 400);
         }
         \Notification::error("You don't have the money for this");
         return \Redirect::to($returnPath);
     }
     //Everything looks gooc, create the payment
     $this->paymentRepository->recordPayment($reason, $userId, 'balance', '', $amount, 'paid', 0, $ref);
     //Update the users cached balance
     $this->bbCredit->recalculate();
     if (\Request::wantsJson()) {
         return \Response::json(['message' => 'Payment made']);
     }
     \Notification::success("Payment made");
     return \Redirect::to($returnPath);
 }
 public function index($userId)
 {
     //Verify the user can access this user record
     $user = User::findWithPermission($userId);
     $this->bbCredit->setUserId($user->id);
     $userBalance = $this->bbCredit->getBalanceFormatted();
     $payments = $this->bbCredit->getBalancePaymentsPaginated();
     return \View::make('account.bbcredit.index')->with('user', $user)->with('payments', $payments)->with('userBalance', $userBalance);
 }
 /**
  * Remove cash from the users balance
  *
  * @param $userId
  * @return mixed
  * @throws \BB\Exceptions\AuthenticationException
  * @throws \BB\Exceptions\InvalidDataException
  */
 public function destroy($userId)
 {
     $user = User::findWithPermission($userId);
     $this->bbCredit->setUserId($userId);
     $amount = \Request::get('amount');
     $returnPath = \Request::get('return_path');
     $ref = \Request::get('ref');
     $minimumBalance = $this->bbCredit->acceptableNegativeBalance('withdrawal');
     if ($user->cash_balance + $minimumBalance * 100 < $amount * 100) {
         \Notification::error("Not enough money");
         return \Redirect::to($returnPath);
     }
     $this->paymentRepository->recordPayment('withdrawal', $userId, 'balance', '', $amount, 'paid', 0, $ref);
     $this->bbCredit->recalculate();
     \Notification::success("Payment recorded");
     return \Redirect::to($returnPath);
 }
 /**
  * This is a basic method for recording a payment transfer between two people
  * This should not exist and the normal balance payment controller should be used
  * If any more work is needed here please take the time and move it over!
  *
  * @param Request $request
  * @param integer $userId
  *
  * @return mixed
  * @throws ValidationException
  * @throws AuthenticationException
  */
 public function recordTransfer(Request $request, $userId)
 {
     $user = User::findWithPermission($userId);
     $this->bbCredit->setUserId($user->id);
     $amount = $request->get('amount');
     $targetUserId = $request->get('target_user_id');
     $targetUser = $this->userRepository->getById($targetUserId);
     if ($targetUserId === $userId) {
         throw new ValidationException('Your\'e trying to send money to yourself, no!');
     }
     //What is the users balance
     $userBalance = $this->bbCredit->getBalance();
     //With this payment will the users balance go to low?
     if ($userBalance - $amount < 0) {
         \Notification::error("You don't have the money for this");
         return \Redirect::route('account.balance.index', $user->id);
     }
     $this->paymentRepository->recordBalanceTransfer($user->id, $targetUser->id, $amount);
     \Notification::success("Transfer made");
     return \Redirect::route('account.balance.index', $user->id);
 }
 public function validateData()
 {
     //Verify the keyfob, device key and action
     //Validate the action
     if (!in_array($this->action, $this->deviceActions)) {
         throw new ValidationException('Invalid Device Action');
     }
     //Validate the device
     try {
         $this->device = $this->equipmentRepository->findBySlug($this->deviceKey);
     } catch (ModelNotFoundException $e) {
         throw new ValidationException('Invalid Device Key');
     }
     //Confirm the device is working
     if (!$this->device->working) {
         throw new ValidationException('Device Not Working');
     }
     //Validate the key fob
     $this->keyFob = $this->lookupKeyFob($this->keyFobId);
     //Make sure the user is active
     $this->user = $this->keyFob->user()->first();
     if (!$this->user || !$this->user->active) {
         throw new ValidationException('User Invalid');
     }
     //Make sure the user is allowed to use the device
     if ($this->device->requires_induction) {
         //Verify the user has training
         if (!$this->inductionRepository->isUserTrained($this->user->id, $this->deviceKey)) {
             throw new ValidationException('User Not Trained');
         }
     }
     //Make sure the member has enough money on their account
     $minimumBalance = $this->bbCredit->acceptableNegativeBalance('equipment-fee');
     if ($this->user->cash_balance + $minimumBalance * 100 <= 0) {
         throw new ValidationException('User doesn\'t have enough credit');
     }
 }
 /**
  * Handle the event.
  *
  * @param  MemberBalanceChanged  $event
  * @return void
  */
 public function handle(MemberBalanceChanged $event)
 {
     $this->memberCreditService->setUserId($event->userId);
     $this->memberCreditService->recalculate();
 }