private function updateSubPayment($paymentId, $userId, $status)
 {
     $payment = $this->paymentRepository->getById($paymentId);
     $subCharge = $this->subscriptionChargeRepository->findCharge($userId);
     if (!$subCharge) {
         \Log::warning('Subscription payment without a sub charge. Payment ID:' . $paymentId);
         return;
     }
     //The sub charge record id gets saved onto the payment
     if (empty($payment->reference)) {
         $payment->reference = $subCharge->id;
         $payment->save();
     } else {
         if ($payment->reference != $subCharge->id) {
             throw new PaymentException('Attempting to update sub charge (' . $subCharge->id . ') but payment (' . $payment->id . ') doesn\'t match. Sub charge has an existing reference on it.');
         }
     }
     if ($status == 'paid') {
         $this->subscriptionChargeRepository->markChargeAsPaid($subCharge->id);
     } else {
         if ($status == 'pending') {
             $this->subscriptionChargeRepository->markChargeAsProcessing($subCharge->id);
         }
     }
     //The amount isn't stored on the sub charge record until its paid or processing
     if ($payment->amount != $subCharge->amount) {
         $this->subscriptionChargeRepository->updateAmount($subCharge->id, $payment->amount);
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $spreadsheetPath = \Input::file('statement')->getRealPath();
     $testProcess = \Request::get('test');
     if ($testProcess) {
         $testProcess = true;
         echo "Test Mode - no payment records are being created<br /><br />";
     } else {
         $testProcess = false;
         echo "Live Mode - Payments have been created<br /><br />";
     }
     $reader = new SpreadsheetReader($spreadsheetPath);
     $reader->ChangeSheet(0);
     $stringMatchUsers = User::active()->where('import_match_string', '!=', '')->get();
     $users = User::active()->get();
     echo '<br />' . PHP_EOL;
     echo '<table width="100%">';
     foreach ($reader as $i => $row) {
         echo "<tr>";
         $subPayment = false;
         $balancePayment = false;
         $paymentReference = null;
         //print_r($row);
         //If the payment isn't a credit then we don't care about it
         //if (($row[1] != 'CR') && ($row[1]) != 'BP')
         if ($row[1] != 'CR') {
             continue;
         }
         if (strpos(strtoupper($row[2]), 'GC C1 BUILDBRIGHTO') !== false) {
             continue;
         }
         if (strpos(strtoupper($row[2]), 'STRIPE TRANSFER') !== false) {
             continue;
         }
         if (strpos(strtoupper($row[2]), 'EVENTBRITE') !== false) {
             continue;
         }
         $date = new \Carbon\Carbon($row[0]);
         echo "<td>" . $date->format('d/m/y') . '</td>';
         //echo "<td>".$row[1].'</td>';
         if (strpos(strtoupper($row[2]), 'SUB') !== false) {
             $subPayment = true;
         } elseif (strpos($row[2], 'MEMBERSHIP') !== false) {
             $subPayment = true;
         } elseif (strpos($row[2], '-BALANCE-') !== false) {
             $balancePayment = true;
             $descriptionParts = explode('-BALANCE-', $row[2]);
             if (is_array($descriptionParts) && count($descriptionParts) > 1) {
                 $paymentReference = strtolower($descriptionParts[1]);
             }
         }
         if ($subPayment) {
             echo '<td>SUB</td>';
             $reasonString = 'subscription';
         } elseif ($balancePayment) {
             echo '<td>Balance</td>';
             $reasonString = 'balance';
         } else {
             echo '<td></td>';
             $reasonString = 'unknown';
         }
         $matchedUser = false;
         $paymentDescription = strtolower($row[2]);
         //Try matching against specific match strings first
         foreach ($stringMatchUsers as $user) {
             if (strpos($paymentDescription, strtolower($user->import_match_string)) !== false) {
                 $matchedUser = $user;
                 break;
             }
         }
         //If there was no match do a general surname match
         if (!$matchedUser) {
             foreach ($users as $user) {
                 if (strpos($paymentDescription, strtolower($user->family_name)) !== false) {
                     $matchedUser = $user;
                     break;
                 }
             }
         }
         if ($matchedUser) {
             echo '<td>' . $matchedUser->name . '</td>';
             if ($subPayment) {
                 $subCharge = $this->subscriptionChargeRepository->findCharge($matchedUser->id, $date);
                 if ($subCharge) {
                     echo '<td>Subscription Payment</td>';
                 } else {
                     echo '<td style="background-color: #ff8c14;">Unknown: Recording as balance top up</td>';
                     $subPayment = false;
                     $reasonString = 'balance';
                 }
             }
         } else {
             echo '<td style="background-color: #F00;">Unknown</td><td></td>';
         }
         echo '<td>' . $row[2] . '</td>';
         //echo '<td>'.$row[3].'</td>';
         echo '<td>' . $row[4] . '</td>';
         echo "</tr>";
         if (!$testProcess && $matchedUser) {
             if ($subPayment) {
                 if (isset($subCharge) && $subCharge) {
                     $paymentReference = $subCharge->id;
                     $this->subscriptionChargeRepository->markChargeAsPaid($subCharge->id, $date, $row[4]);
                 }
             }
             Payment::create(['created_at' => $date, 'reason' => $reasonString, 'source' => 'standing-order', 'user_id' => $matchedUser->id, 'amount' => $row[4], 'fee' => 0, 'amount_minus_fee' => $row[4], 'status' => 'paid', 'reference' => $paymentReference]);
             if ($subPayment) {
                 if ($matchedUser->payment_method == 'standing-order') {
                     $matchedUser->monthly_subscription = $row[4];
                 }
                 $matchedUser->save();
             }
         }
     }
     echo "</table>";
     exit;
 }