/**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function boot(Router $router)
 {
     parent::boot($router);
     $router->bind('username', function ($username) {
         return \App\User::where('username', $username)->firstOrFail();
     });
     $router->bind('gwid', function ($gwid) {
         return \App\Gameweek::where('id', $gwid)->firstOrFail();
     });
     $router->bind('monthid', function ($monthid) {
         return \App\Month::where('id', $monthid)->firstOrFail();
     });
 }
 /**
  * Show the application dashboard to the user.
  *
  * @return Response
  */
 public function index()
 {
     //add check for row in month table with correct month
     //make it if not there.
     $user = Auth::user();
     $month = Input::get('month');
     $year = Input::get('year');
     $banks = DB::select('select * from accounts where userID = :id and accountType = "b"', ['id' => $user->id]);
     $cc = DB::select('select * from accounts where userID = :id and accountType = "c"', ['id' => $user->id]);
     $credits = DB::select('select * from income where userID = :id and month = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => $year]);
     $accounts = DB::select('select * from accounts where userID = :id', ['id' => $user->id]);
     $debits = DB::select('select * from transactions where userID = :id and month = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => $year]);
     $transfers = DB::select('select * from transfers where userID = :id and month = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => $year]);
     $payments = DB::select('select * from payments where userID = :id and month = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => $year]);
     if ($credits || $debits) {
         $accountNames = [];
         foreach ($accounts as $account) {
             $accountNames = $accountNames + [$account->id => $account->name];
         }
         $monthData = Month::where('userID', $user->id)->where('name', $month)->where('year', $year)->first();
         $income = $monthData->income;
         $profit = $monthData->profit;
         //prepare spending amounts for types for the pie chart.
         $types = DB::select('select * from types where userID = :id order by name ASC', ['id' => $user->id]);
         $spending = [];
         $typeNames = [];
         foreach ($types as $type) {
             $typeNames = $typeNames + [$type->id => $type->name];
             $tmp = DB::select('select * from transactions where userID = :id and month = :month and typeID = :typeID and year = :year', ['id' => $user->id, 'month' => $month, 'typeID' => $type->id, 'year' => $year]);
             $sum = 0;
             foreach ($tmp as $i) {
                 $sum = $sum + $i->amount;
             }
             if ($sum != 0) {
                 array_push($spending, ["sum" => $sum, "name" => $type->name]);
             }
         }
         arsort($spending);
         return view('view-past')->with('month', $month)->with('year', $year)->with('income', $income)->with('profit', $profit)->with('spending', $spending)->with('accountNames', $accountNames)->with('typeNames', $typeNames)->with('transactions', $debits)->with('incomeData', $credits)->with('payments', $payments)->with('transfers', $transfers);
     } else {
         return view('view-past-no-data');
     }
 }
 /**
  * Show the application dashboard to the user.
  *
  * @return Response
  */
 public function index()
 {
     //add check for row in month table with correct month
     //make it if not there.
     $user = Auth::user();
     $month = date("M");
     $year = date('Y');
     $banks = DB::select('select * from accounts where userID = :id and accountType = "b"', ['id' => $user->id]);
     $cc = DB::select('select * from accounts where userID = :id and accountType = "c"', ['id' => $user->id]);
     $credits = DB::select('select * from income where userID = :id and month = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => $year]);
     $accounts = DB::select('select * from accounts where userID = :id', ['id' => $user->id]);
     $debits = DB::select('select * from transactions where userID = :id and month = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => $year]);
     $transfers = DB::select('select * from transfers where userID = :id and month = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => $year]);
     $payments = DB::select('select * from payments where userID = :id and month = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => $year]);
     $accountNames = [];
     foreach ($accounts as $account) {
         $accountNames = $accountNames + [$account->id => $account->name];
     }
     $income = 0;
     $spent = 0;
     //sum  up for months profit
     foreach ($credits as $credit) {
         $income += $credit->amount;
     }
     foreach ($debits as $debit) {
         if ($debit->accountID == 0 && $debit->month == date("M")) {
             $spent = $spent + $debit->amount;
         } else {
             //check to make sure teh cc id is not a bank if so this is a transfer and doesn't get counted
             $account2 = Account::find($debit->accountID);
             if ($account2->accountType == 'b' && $debit->month == date("M")) {
                 //this is a payment on a cc
                 $spent = $spent + $debit->amount;
             }
         }
     }
     foreach ($payments as $payment) {
         if ($payment->month == date("M")) {
             $spent = $spent + $payment->amount;
         }
     }
     $profit = $income - $spent;
     $monthData = Month::where('userID', $user->id)->where('name', date("M"))->where('year', date("Y"))->first();
     $monthData->income = $income;
     $monthData->profit = $profit;
     $monthData->save();
     //prepare spending amounts for types for the pie chart.
     $types = DB::select('select * from types where userID = :id order by name ASC', ['id' => $user->id]);
     $spending = [];
     $typeNames = [];
     foreach ($types as $type) {
         $typeNames = $typeNames + [$type->id => $type->name];
         $tmp = DB::select('select * from transactions where userID = :id and month = :month and typeID = :typeID and year = :year', ['id' => $user->id, 'month' => $month, 'typeID' => $type->id, 'year' => $year]);
         $sum = 0;
         foreach ($tmp as $i) {
             $sum = $sum + $i->amount;
         }
         if ($sum != 0) {
             array_push($spending, ["sum" => $sum, "name" => $type->name]);
         }
     }
     arsort($spending);
     $cash = DB::select('select cash from month where userID = :id and name = :month and year = :year', ['id' => $user->id, 'month' => $month, 'year' => date("Y")]);
     return view('home')->with('banks', $banks)->with('cc', $cc)->with('income', $income)->with('profit', $profit)->with('spending', $spending)->with('cash', $cash)->with('accountNames', $accountNames)->with('typeNames', $typeNames)->with('transactions', $debits)->with('incomeData', $credits)->with('payments', $payments)->with('transfers', $transfers);
 }
 public function postIndex()
 {
     $user = Auth::user();
     $id = Input::get('payment');
     /*
      *There is an invisible field called form that tells what form was submitted
      *
      */
     if (Input::get('form') == 'trans') {
         $monthNum = substr(Input::get('date'), 0, 2);
         $month = date('M', mktime(0, 0, 0, $monthNum, 10));
         $year = substr(Input::get('date'), -4);
         $transaction = new Transaction();
         $transaction->userID = $user->id;
         $transaction->date = Input::get('date');
         $transaction->amount = Input::get('amount');
         $transaction->typeID = Input::get('type');
         $transaction->note = Input::get('note');
         $transaction->year = $year;
         $transaction->month = $month;
         if ($id == 'cash') {
             $month = Month::where('userID', $user->id)->where('name', $month)->where('year', date("Y"))->first();
             $month->cash -= Input::get('amount');
             $month->save();
             $transaction->accountID = 0;
         } else {
             $transaction->accountID = $id;
             $account = Account::find($id);
             //add for credit subtract for bank
             if ($account->accountType == 'c') {
                 $account->balance += Input::get('amount');
             } else {
                 $account->balance -= Input::get('amount');
             }
             $account->save();
         }
         $transaction->save();
         return redirect('options')->with('message', 'Transaction added successfully.');
     } else {
         if (Input::get('form') == 'type') {
             $type = new Type();
             $type->userID = $user->id;
             $type->name = Input::get('name');
             $type->save();
             return redirect('options')->with('message', 'Category added successfully.');
         } else {
             if (Input::get('form') == 'payment') {
                 $monthNum = substr(Input::get('date'), 0, 2);
                 $month = date('M', mktime(0, 0, 0, $monthNum, 10));
                 $year = substr(Input::get('date'), -4);
                 $amount = Input::get('amount');
                 $bankID = Input::get('bank');
                 $ccID = Input::get('payment');
                 $note = Input::get('note');
                 $date = Input::get('date');
                 if ($ccID == 'cash') {
                     $month2 = Month::where('userID', $user->id)->where('name', $month)->first();
                     $month2->cash = $month2->cash + $amount;
                     $month2->save();
                     if ($bankID == 'cash') {
                         $month2 = Month::where('userID', $user->id)->where('name', $month)->first();
                         $month2->cash = $month2->cash - $amount;
                         $month2->save();
                     } else {
                         $bank = Account::find($bankID);
                         $bank->balance = $bank->balance - $amount;
                         $bank->save();
                     }
                     $transfer = new Transfer();
                     $transfer->userID = $user->id;
                     $transfer->creditAccountID = $bankID;
                     $transfer->debitAccountID = 0;
                     $transfer->amount = $amount;
                     $transfer->note = $note;
                     $transfer->date = $date;
                     $transfer->year = $year;
                     $transfer->month = $month;
                     $transfer->save();
                 } else {
                     $cc = Account::find($ccID);
                     if ($bankID == 'cash') {
                         $month2 = Month::where('userID', $user->id)->where('name', $month)->first();
                         $month2->cash = $month2->cash - $amount;
                         $month2->save();
                     } else {
                         $bank = Account::find($bankID);
                         $bank->balance = $bank->balance - $amount;
                         $bank->save();
                     }
                     if ($cc->accountType == 'b') {
                         //transfer
                         $cc->balance = $cc->balance + $amount;
                         $transfer = new Transfer();
                         $transfer->userID = $user->id;
                         $transfer->creditAccountID = $bankID;
                         $transfer->debitAccountID = $ccID;
                         $transfer->amount = $amount;
                         $transfer->note = $note;
                         $transfer->date = $date;
                         $transfer->year = $year;
                         $transfer->month = $month;
                         $transfer->save();
                     } else {
                         //payment
                         $cc->balance = $cc->balance - $amount;
                         $payment = new Payment();
                         $payment->userID = $user->id;
                         $payment->creditAccountID = $bankID;
                         $payment->debitAccountID = $ccID;
                         $payment->amount = $amount;
                         $payment->note = $note;
                         $payment->date = $date;
                         $payment->year = $year;
                         $payment->month = $month;
                         $payment->save();
                     }
                     $cc->save();
                 }
                 return redirect('options')->with('message', 'Payment saved successfully.');
             } else {
                 if (Input::get('form') == 'cc') {
                     $cc = new Account();
                     $cc->userID = $user->id;
                     $cc->name = Input::get('name');
                     $cc->balance = Input::get('balance');
                     $cc->creditLimit = Input::get('limit');
                     $cc->statementDay = Input::get('date');
                     $cc->accountType = 'c';
                     $cc->save();
                     return redirect('options')->with('message', 'Credit Card added successfully.');
                 } else {
                     if (Input::get('form') == 'bank') {
                         $bank = new Account();
                         $bank->userID = $user->id;
                         $bank->name = Input::get('name');
                         $bank->balance = Input::get('balance');
                         $bank->accountType = 'b';
                         $bank->save();
                         return redirect('options')->with('message', 'Bank Account added successfully.');
                     } else {
                         if (Input::get('form') == 'income') {
                             $date = Input::get('date');
                             $monthNum = substr($date, 0, 2);
                             $month = date('M', mktime(0, 0, 0, $monthNum, 10));
                             $amount = Input::get('amount');
                             $bankID = Input::get('bank');
                             $year = substr(Input::get('date'), -4);
                             $income = new Income();
                             $income->userID = $user->id;
                             $income->month = $month;
                             $income->amount = $amount;
                             $income->note = Input::get('note');
                             $income->date = $date;
                             $income->year = $year;
                             if ($bankID == "cash") {
                                 $income->accountID = 0;
                                 $month = Month::where('userID', $user->id)->where('name', $month)->first();
                                 $month->cash = $month->cash + $amount;
                                 $month->save();
                             } else {
                                 $income->accountID = $bankID;
                                 $bank = Account::find($bankID);
                                 $bank->balance = $bank->balance + $amount;
                                 $bank->save();
                             }
                             $income->save();
                             return redirect('options')->with('message', 'Income added successfully.');
                         }
                     }
                 }
             }
         }
     }
 }
 public function incomeDelete($id)
 {
     $user = Auth::user();
     $date = Input::get('date');
     $amount = Input::get('amount');
     $bankID = Input::get('bank');
     $note = Input::get('note');
     $income = Income::where('id', $id)->where('userID', $user->id)->first();
     if ($income->accountID == 0) {
         $month = Month::where('userID', $user->id)->where('name', date('M'))->first();
         $month->cash = $month->cash - $income->amount;
         $month->save();
     } else {
         $origBank = Account::find($income->accountID);
         //add original transaction value to bank account
         $origBank->balance = $origBank->balance - $income->amount;
         $origBank->save();
     }
     $income->forceDelete();
     return redirect('home')->with('message', 'Income deleted successfully.');
 }