public function index()
 {
     $this->load->library('prediction');
     $money = $this->input->get('money');
     $date = $this->input->get('date');
     $months = $this->input->get('months');
     $data = $this->prediction->setTransactions($this->getAllTransactions($months ?: 3))->getData($money, $date);
     $this->load->view('layout/header');
     $error = $this->session->flashdata('message');
     $this->load->view('element/message', ['success' => $error]);
     $this->load->view('page/manager', $data);
     $this->load->view('layout/footer');
 }
 /**
  * Get all expenses so far
  * in json format
  * 
  * @return json
  */
 public function getAllExpensesJson()
 {
     $response = array(array('Expense', 'Total expense'));
     $prediction = new Prediction();
     $expenses = $prediction->getAllSpentPredictions(Auth::user()->id);
     if (count($expenses)) {
         foreach ($expenses as $expense) {
             $response[] = array($expense->name, (double) $expense->value);
         }
     } else {
         $response[] = array('Nothing', 0);
     }
     return Response::json($response);
 }
 protected function _populateTabletWithPreviousData($newTablet)
 {
     $lastInactiveTablet = $this->_getCurrentUser()->getLastInactiveTablet();
     $oldPredictions = $lastInactiveTablet->predictions;
     if (count($oldPredictions)) {
         foreach ($oldPredictions as $oldPrediction) {
             $prediction = new Prediction();
             $prediction->tablet_id = $newTablet->id;
             $prediction->name = $oldPrediction->name;
             $startingSum = $oldPrediction->getTotalExpenses() ? $oldPrediction->getTotalExpenses() : $oldPrediction->predicted;
             $prediction->predicted = $startingSum;
             $prediction->value = $startingSum;
             $prediction->save();
         }
     }
 }
 /**
  * Delete prediction action
  * @return string
  */
 public function delete()
 {
     $response = array('success' => false);
     if (Request::ajax()) {
         $predictionIds = Input::get('predictionIds');
         $tabletId = null;
         if ($predictionIds) {
             $predictionIdsArray = $this->_getPredictionIdsArray($predictionIds);
             foreach ($predictionIdsArray as $predictionId) {
                 $prediction = Prediction::find($predictionId);
                 if (!$tabletId) {
                     $tablet = Tablet::find($prediction->tablet_id);
                 }
                 $predictionExpenses = $prediction->expenses;
                 foreach ($predictionExpenses as $expense) {
                     $tablet->total_expenses = $tablet->total_expenses - $expense->value;
                     $tablet->current_sum = $tablet->current_sum + $expense->value;
                 }
                 $tablet->save();
             }
             Prediction::destroy($predictionIdsArray);
             $response['success'] = true;
         }
     }
     return Response::json($response);
 }
 public function Get_record_file_handle()
 {
     //<--this function helps to find and return clculating records data
     $csvStored_data = "";
     $start_date = $this->get_date_period_start();
     $end_date = $this->get_date_period_ends();
     $month_start = $start_date[1] - 1;
     //<--special arrangement for yahoo finance data (0 -> january)
     $month_ends = $end_date[1] - 1;
     //<--special arrangement for yahoo finance data (0 -> january)
     $file = 'http://ichart.yahoo.com/table.csv?s=' . $this->company_symbol . '&a=' . $month_start . '&b=' . $start_date[2] . '&c=' . $start_date[0] . '&d=' . $month_ends . '&e=' . $end_date[2] . '&f=' . $end_date[0] . '&g=' . $this->interval . '&ignore=.csv';
     $stored_data = CompanyData::get_last_updated_record($this->company_symbol);
     //<-- this function finds the data from database
     if ($stored_data) {
         if (date("Y-m-d", strtotime($stored_data->updated)) < date("Y-m-d", time()) || $stored_data->accuracy < $this->precision) {
             $csvStored_data = csv_to_array($file, ',');
             //<----exploring CSV file
             if (count($csvStored_data) > 4) {
                 //<--checks for the genuinity
                 $company_id = Company::find_company_id_company_symbol($this->company_symbol);
                 $prediction = CompanyData::where('comp_id_fk', '=', $company_id)->delete();
                 $company = CompanyData::create(array('comp_id_fk' => $company_id, 'csvdata' => serialize($csvStored_data), 'updated' => date("Y-m-d H:i:s", time()), 'accuracy' => $this->precision));
             }
         } else {
             $csvStored_data = unserialize($stored_data->csvdata);
         }
         $this->company_description = $stored_data->description;
         $this->company_address = $stored_data->address;
     } else {
         $company = new Company();
         $company->company_name = trim($this->company_name);
         $this->company_description = get_Company_data($this->company_symbol, 'desci');
         get_Company_logo($this->company_symbol);
         //<--fuction used to copy image
         $company->description = $this->company_description;
         $company->company_symbol = trim($this->company_symbol);
         $csvStored_data = csv_to_array($file, ',');
         //<----exploring CSV file
         if (count($csvStored_data) > 4) {
             //<--checks for the genuinity
             $company = Company::create(array('company_name' => trim($this->company_name), 'company_description' => get_Company_data($this->company_symbol, 'desci'), 'description' => $this->company_description, 'company_symbol' => trim($this->company_symbol)));
             if ($company) {
                 if ($csvStored_data) {
                     get_Company_logo($this->company_symbol);
                     //<--fuction used to copy image
                 }
                 $CompanyData = CompanyData::create(array('comp_id_fk' => trim($company->id), 'csvdata' => serialize($csvStored_data), 'updated' => date("Y-m-d H:i:s", time()), 'accuracy' => $this->precision));
                 $predict_data = Prediction::create(array('comp_id_fk' => trim($company->id)));
             }
         }
     }
     return !empty($csvStored_data) ? $csvStored_data : false;
 }
 */
Event::listen('expense.create.success', function ($expense) {
    $predictionId = $expense->prediction_id;
    $prediction = Prediction::find($predictionId);
    if ($prediction->id) {
        $tablet = $prediction->tablet;
        $tablet->current_sum = $tablet->current_sum - $expense->value;
        $tablet->save();
    }
});
/**
 * Update Prediction value upon expense.create.success
 */
Event::listen('expense.create.success', function ($expense) {
    $predictionId = $expense->prediction_id;
    $prediction = Prediction::find($predictionId);
    if ($prediction->id) {
        $currentValue = (double) $prediction->value;
        $expenseValue = (double) $expense->value;
        $prediction->value = $currentValue - $expenseValue;
        $prediction->save();
    }
});
/**
 * Update current_sum on Tablet upon income.create.success
 */
Event::listen('income.create.success', function ($incomeValue, $tabletId) {
    $tablet = Tablet::find($tabletId);
    if ($tablet->id) {
        $oldCurrentSum = $tablet->current_sum;
        $newCurrentSum = $oldCurrentSum + $incomeValue;
 /**
  * Action for ajax autocomplete by name
  * 
  * @return json
  */
 public function autocomplete()
 {
     $term = Input::get('term');
     $prediction = new Prediction();
     $suggestions = $prediction->getBySuggestedTerm($term);
     $response = array();
     foreach ($suggestions as $suggestion) {
         $response[] = $suggestion->name;
     }
     return Response::json($response);
 }