public function evaluateWeight($id, $loan_id)
 {
     $finance = FinancialProfile::where('user_id', '=', $id)->first();
     $count = LoanApp::where('user_id', '=', $id)->count();
     $loan_app = LoanApp::where('loan_id', '=', $loan_id)->first();
     $current_bids = Bid::where('loan_id', '=', $loan_id)->count();
     $amout = $loan_app->amount;
     $term = $loan_app->term;
     $rate = $loan_app->pref_rate;
     $residence = $finance->residence_status;
     $year_income = $finance->monthly_income * 12;
     $home_loan = $finance->home_loan;
     $car_loan = $finance->car_loan;
     $other_loan = $finance->other_loan;
     $expense = $finance->loan_repayment;
     $other_exp = $finance->other_expense;
     $house_owner = 0;
     if (strcmp($residence, 'Mortage') == 0 || strcmp($residence, 'noMortage') == 0) {
         $house_owner = 1;
     }
     $property = $finance->property;
     $vehicle = $finance->vehicle;
     $share = $finance->share;
     $other_property = $finance->others;
     $active_loan = $total_liab = $home_loan + $car_loan + $other_loan;
     $income_debt_ratio = $year_income / (1 + $count + $total_liab);
     $total_assests = $property + $vehicle + $share + $other_property;
     $disposable_income = $year_income - $total_liab;
     $sum = $rate + $house_owner + $total_assests + $income_debt_ratio + $current_bids + $disposable_income;
     $weight = pow($sum, 1 / 6);
     //$loan_app-> weight = $weight;
     return $weight;
 }
 public function makeBid($loan_id)
 {
     $id = Auth::user()->id;
     $loan_app = LoanApp::where('loan_id', '=', $loan_id)->first();
     $bid_Id;
     if (!Bid::isMadeBefore($loan_id, $id)) {
         $bid = new Bid();
         $bid->user_id = $id;
         $bid->loan_id = $loan_id;
         $bid->bid_date = date('Y-m-d H:i:s');
         $bid->bid_amount = $loan_app->amount;
         /*$bid-> bid_term = $loan_app-> term;
         		$bid-> bid_rate = $loan_app-> pref_rate; */
         $bid->save();
         /*$bid_Id= $bid-> bid_id;*/
         /*$bid_acc = BidAccept::firstOrNew('where','=',$bid_Id);
         		$bid_acc -> loan_id = $loan_id;
         		$bid_acc -> accepte = 0;*/
         //update the loan weight
         $weight = $loan_app->evaluateWeight($id, $loan_id);
         $loan_app->save();
     }
     /*	$bid_acc = BidAccept::firstOrNew(array('bid_id'=>$bid_Id));
     		$bid_acc -> loan_id = $loan_id;
     		$bid_acc -> accepted = 0;
     		$bid_acc->save();*/
     /*$pdata = $profile->getProfile($id);   // method defined in its model
     		$fdata = $financial->getFinancialProfile($id); */
     // methd defined in its model
     return Redirect::route('lend');
 }
 public function createContract($loan_id)
 {
     // Find the profile of the person who's id matches the id of the person currently logged in.  Find it in DB.
     $profile = UserProfile::where('id', '=', Auth::user()->id)->first();
     try {
         $loan = LoanApp::findOrFail($loan_id);
     } catch (Exception $e) {
         return Redirect::route('mytransaction')->with('message', "Could not load loan: " . $e->getMessage());
     }
     $lenders = $this->getLenders($loan_id);
     if (empty($lenders)) {
         return Redirect::route('mytransaction')->with('message', 'Could not find lenders for this loan request');
     }
     $contract = Contract::firstOrCreate(array('offer_id' => Input::get('offer_id', $loan_id)));
     if ($contract->getAttribute('status') === 'complete') {
         return Redirect::to('previewContract/' . $loan_id);
     }
     // todo: set these using the form
     $contract->setAttribute('start_date', date('d/m/Y'));
     $contract->setAttribute('end_date', date('d/m/Y'));
     if (Request::isMethod('post')) {
         // Check if actually posting data.
         $save = $contract->fill(Input::all())->save();
         $errors = Session::pull('messages', array());
         if ($save === false && empty($errors)) {
             $errors[] = 'Failed to save contract';
         }
         if (empty($errors)) {
             return Redirect::to('previewContract/' . $loan_id);
         }
     }
     $prepayment_rules = array("There are no penalties for paying off the loan early.", "Borrower must pay 5% of the original loan amount.", "Borrower must pay the complete interest of the loan.", "Borrower must pay \$100");
     return View::make('createContract', compact('loan', 'lenders', 'profile', 'errors', 'contract', 'prepayment_rules'));
 }
 public function cancelLoans()
 {
     $id = Auth::user()->id;
     //$loan_app =  LoanApp::create(array('user_id' => $id));
     $loanId = Input::get('loanId');
     return LoanApp::where('loan_id', '=', $loanId)->delete();
     // $loanId = $loan_app-> loan_id;
     // $weight= $loan_app-> evaluateWeight($id, $loanId);
     // $loan_app->weight = $weight;
     //$loan_app-> user_id=  $id;
     //$loan_app->save();
     //return Redirect::to('confirm');
     //return $weight;
 }
 public function profile()
 {
     $id = Auth::user()->id;
     $usermail = Auth::user()->email;
     $profile = UserProfile::where('id', '=', $id)->first();
     $financial = FinancialProfile::where('user_id', $id)->first();
     $bids = DB::table('bids')->join('loan_app', 'bids.loan_id', '=', 'loan_app.loan_id')->join('profile', 'profile.id', '=', 'loan_app.user_id')->select('bid_amount', 'pref_rate', 'term', 'purpose', 'fname', 'lname')->orderBy('bid_date', 'DESC')->get();
     $loanApp = LoanApp::where('user_id', '=', $id)->get();
     $allLoan = DB::table('loan_app')->join('profile', 'loan_app.user_id', '=', 'profile.id')->select('profile.fname', 'profile.lname', 'loan_app.amount', 'loan_app.pref_rate', 'loan_app.term', 'loan_app.purpose', 'loan_app.loan_id')->where('loan_app.user_id', '<>', $id)->orderBy('weight', 'DESC')->get();
     // think about cacahe below two queries
     $pdata = $profile->getProfile($id);
     //  method defiend in its model
     $fdata = $financial->getFinancialProfile($id);
     // methd defined in its model
     return View::make('myprofiles', compact('bids', 'pdata', 'fdata', 'loanApp', 'allLoan'));
 }
 public function search()
 {
     $id = Auth::user()->id;
     $lendPrefers = LendRref::where('id', '=', $id)->first();
     //get lender prferences
     $maxAmount = $lendPrefers->max_amount;
     $minAmount = $lendPrefers->min_amount;
     $maxRate = $lendPrefers->max_rate;
     $minRate = $lendPrefers->min_rate;
     $maxTerm = $lendPrefers->max_term;
     $minTerm = $lendPrefers->min_term;
     //search for matched application
     $loanApps = LoanApp::where(function ($query) {
         $query->whereBetween('amount', array($minAmount, $maxAmount))->whereBetween('term', array($minTerm, $maxTerm))->whereBetween('pref_rate', array($minRate, $maxRate))->orderBy('weight', 'desc');
     })->get();
     return $loanApps;
 }
 public function applyLoans()
 {
     $id = Auth::user()->id;
     $loan_app = LoanApp::create(array('user_id' => $id));
     $profile = FinancialProfile::firstOrNew(array('user_id' => $id));
     $loan_app->amount = Input::get('amount');
     $loan_app->term = Input::get('term');
     $loan_app->pref_rate = Input::get('pref_rate');
     $loan_app->purpose = Input::get('purpose');
     $loan_app->description = Input::get('description');
     $profile->residence_status = Input::get('residence');
     $profile->year_cur_addr = Input::get('year_cur_addr');
     $profile->month_cur_addr = Input::get('month_cur_addr');
     $profile->year_old_addr = Input::get('year_old_addr');
     $profile->month_old_addr = Input::get('year_old_addr');
     $profile->employ_status = Input::get('employment');
     $profile->employer = Input::get('employer');
     $profile->position = Input::get('position');
     $profile->year_cur_job = Input::get('year_cur_job');
     $profile->month_cur_job = Input::get('month_cur_job');
     $profile->year_old_job = Input::get('year_old_job');
     $profile->month_old_job = Input::get('month_old_job');
     $profile->monthly_income = Input::get('income');
     $profile->loan_repayments = Input::get('payment');
     $profile->other_expense = Input::get('expense');
     $profile->home_loan = Input::get('homeloan');
     $profile->car_loan = Input::get('carloan');
     $profile->other_loan = Input::get('otherloan');
     $profile->property = Input::get('property');
     $profile->vehicle = Input::get('vehicle');
     $profile->share = Input::get('share');
     $profile->others = Input::get('otherproperty');
     $profile->save();
     $loanId = $loan_app->loan_id;
     $weight = $loan_app->evaluateWeight($id, $loanId);
     $loan_app->weight = $weight;
     $loan_app->save();
     return $weight;
 }
 public function saveLendPref()
 {
     $id = Auth::user()->id;
     $loanApp = LoanApp::where('user_id', '=', $id)->get();
     $profile = UserProfile::where('id', '=', $id)->first();
     $financial = FinancialProfile::where('user_id', $id)->first();
     $lender_prefers = LenderPref::firstOrNew(array('user_id' => $id));
     $lender_prefers->min_amount = Input::get('minLoan');
     $lender_prefers->min_term = Input::get('minTerm');
     $lender_prefers->min_rate = Input::get('minRate');
     $lender_prefers->max_amount = Input::get('maxLoan');
     $lender_prefers->max_term = Input::get('maxTerm');
     $lender_prefers->max_rate = Input::get('maxRate');
     $lender_prefers->save();
     $results = $this->search();
     $pdata = $profile->getProfile($id);
     // method defined in its model
     $fdata = $financial->getFinancialProfile($id);
     // methd defined in its model
     $bids = DB::table('bids')->join('loan_app', 'bids.loan_id', '=', 'loan_app.loan_id')->join('profile', 'profile.id', '=', 'loan_app.user_id')->select('bid_amount', 'pref_rate', 'term', 'purpose', 'fname', 'lname')->orderBy('bid_date', 'DESC')->get();
     return View::make('myprofiles', compact('bids', 'pdata', 'fdata', 'results', 'loanApp'));
     /*	return Redirect::route('myprofile');   */
 }
 public function deleteLoan($loanId)
 {
     LoanApp::where('loan_id', '=', $loanId)->delete();
     return Redirect::to('mytransaction');
 }
 public function mytransaction()
 {
     $id = Auth::user()->id;
     $usermail = Auth::user()->email;
     $user = User::where('id', '=', $id)->get();
     if ($user[0]->profile_complete == 0) {
         return Redirect::route('newUserProfile');
     }
     $profile = UserProfile::where('id', '=', $id)->first();
     $financial = FinancialProfile::where('user_id', $id)->first();
     $bids = DB::table('bids')->join('loan_app', 'bids.loan_id', '=', 'loan_app.loan_id')->join('profile', 'profile.id', '=', 'loan_app.user_id')->join('bids_accepted', 'bids.bid_id', '=', 'bids_accepted.bid_id')->select('bid_amount', 'pref_rate', 'term', 'purpose', 'fname', 'lname', 'loan_app.amount', 'loan_app.progress', 'bids.bid_id', 'loan_app.user_id')->where('bids.user_id', '=', $id)->where('bids_accepted.accepted', '=', '0')->orderBy('bid_date', 'DESC')->get();
     $loanApp = LoanApp::where('user_id', '=', $id)->get();
     $allLoan = DB::table('loan_app')->join('profile', 'loan_app.user_id', '=', 'profile.id')->select('profile.fname', 'profile.lname', 'loan_app.amount', 'loan_app.pref_rate', 'loan_app.term', 'loan_app.purpose', 'loan_app.loan_id')->where('loan_app.user_id', '<>', $id)->orderBy('weight', 'DESC')->get();
     $accepted_bid = DB::table('bids')->join('bids_accepted', 'bids.bid_id', '=', 'bids_accepted.bid_id')->select('bids.loan_id', 'bid_amount')->where('user_id', '=', $id)->where('bids_accepted.accepted', '=', '1')->get();
     $loanIds = array();
     foreach ($accepted_bid as $b) {
         array_push($loanIds, $b->loan_id);
     }
     if (sizeof($loanIds) == 0) {
         array_push($loanIds, 'z');
     }
     $accepted_loan = DB::table('loan_app')->join('profile', 'profile.id', '=', 'loan_app.user_id')->join('users', 'users.id', '=', 'loan_app.user_id')->join('bids', 'loan_app.loan_id', '=', 'bids.loan_id')->select('bid_amount', 'users.email', 'term', 'pref_rate', 'purpose', 'progress', 'fname', 'lname', 'loan_app.user_id', 'bids.match_date')->whereIn('loan_app.loan_id', $loanIds)->where('bids.user_id', '=', $id)->orderBy('bids.match_date', 'DESC')->get();
     $active_loan = DB::table('loan_app')->select('amount', 'term', 'pref_rate', 'purpose', 'progress', 'loan_id', 'match_date')->where(DB::raw('loan_app.amount'), '=', DB::raw('loan_app.progress'))->where('user_id', '=', $id)->orderBy('match_date', 'DESC')->get();
     $loan_requests = DB::table('loan_app')->select('amount', 'term', 'pref_rate', 'purpose', 'progress', 'loan_id')->where(DB::raw('loan_app.amount'), '<>', DB::raw('loan_app.progress'))->where('user_id', '=', $id)->get();
     $active_offers = DB::table('loan_offers')->select('amount', 'rate', 'term', 'offer_id')->where('user_id', '=', $id)->where('matched', '=', 0)->get();
     $active_obids = DB::table('offers_bids')->join('loan_offers', 'loan_offers.offer_id', '=', 'offers_bids.offer_id')->join('profile', 'profile.id', '=', 'loan_offers.user_id')->select('profile.fname', 'profile.lname', 'loan_offers.amount', 'offers_bids.rate', 'offers_bids.term', 'obid_id', 'loan_offers.user_id')->where('offers_bids.user_id', '=', $id)->where('offers_bids.accepted', '=', 0)->get();
     $matched_offers = DB::table('loan_offers')->join('offers_bids', 'offers_bids.offer_id', '=', 'loan_offers.offer_id')->join('profile', 'profile.id', '=', 'offers_bids.user_id')->join('users', 'users.id', '=', 'offers_bids.user_id')->select('fname', 'lname', 'email', 'loan_offers.amount', 'offers_bids.rate', 'offers_bids.term', 'offers_bids.user_id')->where('loan_offers.user_id', '=', $id)->where('offers_bids.accepted', '=', 1)->get();
     $matched_obids = DB::table('offers_bids')->join('loan_offers', 'loan_offers.offer_id', '=', 'offers_bids.offer_id')->join('profile', 'profile.id', '=', 'loan_offers.user_id')->join('users', 'users.id', '=', 'loan_offers.user_id')->select('fname', 'lname', 'email', 'loan_offers.amount', 'offers_bids.rate', 'offers_bids.term', 'loan_offers.user_id')->where('offers_bids.user_id', '=', $id)->where('offers_bids.accepted', '=', 1)->get();
     // think about cacahe below two queries
     $pdata = $profile->getProfile($id);
     //  method defiend in its model
     $fdata = $financial->getFinancialProfile($id);
     // methd defined in its model
     return View::make('mytransaction', compact('loanIds', 'bids', 'pdata', 'fdata', 'loanApp', 'allLoan', 'accepted_loan', 'active_loan', 'loan_requests', 'active_offers', 'active_obids', 'matched_offers', 'matched_obids'));
 }