public function update($logEntryId)
 {
     $reason = \Request::get('reason');
     if (!in_array($reason, ['training', 'testing'])) {
         throw new \BB\Exceptions\ValidationException("Not a valid reason");
     }
     $equipmentLog = $this->equipmentLogRepository->getById($logEntryId);
     /*
     if ($equipmentLog->user_id == \Auth::user()->id) {
         throw new \BB\Exceptions\ValidationException("You can't update your own record");
     }
     */
     if (!\Auth::user()->hasRole($equipmentLog->device) && !\Auth::user()->isAdmin()) {
         throw new \BB\Exceptions\ValidationException("You don't have permission to alter this record");
     }
     if (!empty($equipmentLog->reason)) {
         throw new \BB\Exceptions\ValidationException("Reason already set");
     }
     $billedStatus = $equipmentLog->billed;
     if ($equipmentLog->billed) {
         //the user has been billed, we need to undo this.
         $payments = $this->paymentRepository->getPaymentsByReference($equipmentLog->id . ':' . $equipmentLog->device);
         if ($payments->count() == 1) {
             $this->paymentRepository->delete($payments->first()->id);
             $billedStatus = false;
         } else {
             throw new \BB\Exceptions\ValidationException("Unable to locate related payment, please contact an admin");
         }
     }
     $this->equipmentLogRepository->update($logEntryId, ['reason' => $reason, 'billed' => $billedStatus]);
     \Notification::success("Record Updated");
     return \Redirect::back();
 }
 /**
  * Bill members based on the sub charges that are due
  */
 public function billMembers()
 {
     $subCharges = $this->subscriptionChargeRepository->getDue();
     foreach ($subCharges as $charge) {
         if ($charge->user->payment_method == 'gocardless-variable') {
             //Look the the previous attempts - there may be multiple failures
             $existingPayments = $this->paymentRepository->getPaymentsByReference($charge->id);
             if ($existingPayments->count() > 0) {
                 //We will let the user retry the payment if it fails
                 continue;
             }
             $bill = $this->goCardless->newBill($charge->user->subscription_id, $charge->user->monthly_subscription, $this->goCardless->getNameFromReason('subscription'));
             if ($bill) {
                 $this->paymentRepository->recordSubscriptionPayment($charge->user->id, 'gocardless-variable', $bill->id, $bill->amount, $bill->status, $bill->gocardless_fees, $charge->id);
             }
         }
     }
 }
 public function index()
 {
     $this->paymentRepository->reasonFilter('balance');
     $balancePaidIn = $this->paymentRepository->getTotalAmount();
     $this->paymentRepository->resetFilters();
     $this->paymentRepository->sourceFilter('balance');
     $balancePaidOut = $this->paymentRepository->getTotalAmount();
     $balanceLiability = $balancePaidIn - $balancePaidOut;
     $this->paymentRepository->resetFilters();
     $this->paymentRepository->reasonFilter('storage-box');
     $storageBoxLiability = $this->paymentRepository->getTotalAmount();
     $this->paymentRepository->resetFilters();
     $this->paymentRepository->reasonFilter('door-key');
     $doorKeyLiability = $this->paymentRepository->getTotalAmount();
     $laserCutterInvestment = $this->paymentRepository->getPaymentsByReference('laser-cutter')->sum('amount');
     $laserCutterMoneySpent = $this->paymentRepository->getEquipmentFeePayments('laser')->sum('amount');
     return \View::make('payment_overview.index')->with(compact('balancePaidIn', 'balancePaidOut', 'balanceLiability', 'storageBoxLiability', 'doorKeyLiability', 'laserCutterInvestment', 'laserCutterMoneySpent'));
 }
 /**
  * Bill members based on the sub charges that are due
  */
 public function billMembers()
 {
     $subCharges = $this->subscriptionChargeRepository->getDue();
     //Check each of the due charges, if they have previous attempted payments ignore them
     // we don't want to retry failed payments as for DD's this will generate bank charges
     $subCharges->reject(function ($charge) {
         return $this->paymentRepository->getPaymentsByReference($charge->id)->count() > 0;
     });
     //Filter the list into two gocardless and balance subscriptions
     $goCardlessUsers = $subCharges->filter(function ($charge) {
         return $charge->user->payment_method == 'gocardless-variable';
     });
     $balanceUsers = $subCharges->filter(function ($charge) {
         return $charge->user->payment_method == 'balance';
     });
     //Charge the balance users
     foreach ($balanceUsers as $charge) {
         if ($charge->user->monthly_subscription * 100 > $charge->user->cash_balance) {
             //user doesn't have enough money
             //If they have a secondary payment method of gocardless try that
             if ($charge->user->secondary_payment_method == 'gocardless-variable') {
                 //Add the charge to the gocardless list for processing
                 $goCardlessUsers->push($charge);
                 event(new SubscriptionPayment\InsufficientFundsTryingDirectDebit($charge->user->id, $charge->id));
             } else {
                 event(new SubscriptionPayment\FailedInsufficientFunds($charge->user->id, $charge->id));
             }
             continue;
         }
         $this->paymentRepository->recordSubscriptionPayment($charge->user->id, 'balance', '', $charge->user->monthly_subscription, 'paid', 0, $charge->id);
         event(new MemberBalanceChanged($charge->user->id));
     }
     //Charge the gocardless users
     foreach ($goCardlessUsers as $charge) {
         $bill = $this->goCardless->newBill($charge->user->subscription_id, $charge->user->monthly_subscription, $this->goCardless->getNameFromReason('subscription'));
         if ($bill) {
             $this->paymentRepository->recordSubscriptionPayment($charge->user->id, 'gocardless-variable', $bill->id, $bill->amount, $bill->status, $bill->gocardless_fees, $charge->id);
         }
     }
 }