public function compare() { // get a list of all months: $months = array(); $first = BaseController::getFirst(); $first->modify('first day of this month midnight'); $today = new Carbon('now'); $today->modify('first day of this month midnight'); $prev = clone $today; $prev->sub(new DateInterval('P2D')); $prev->modify('first day of this month midnight'); while ($first <= $today) { $index = $first->format('Y-m-d'); $months[$index] = $first->format('F Y'); $first->add(new DateInterval('P1M')); } // account list: $accs = Auth::user()->accounts()->get(); $accounts = array(0 => '(all accounts)'); foreach ($accs as $acc) { $accounts[intval($acc->id)] = Crypt::decrypt($acc->name); } $account = Setting::getSetting('defaultCheckingAccount'); return View::make('pages.compare')->with('months', $months)->with('thisMonth', $today)->with('prevMonth', $prev)->with('account', $account)->with('accounts', $accounts); }
public function averagespending(Carbon $date = null) { $date = is_null($date) ? Session::get('period') : $date; $transactions = floatval($this->transactions()->where(DB::Raw('DATE_FORMAT(`date`,"%m-%Y")'), '!=', $date->format('m-Y'))->where('onetime', '=', 0)->where(DB::Raw('DATE_FORMAT(`date`,"%d")'), '<=', intval($date->format('j')))->sum('amount')) * -1; $sum = $transactions; $oldest = BaseController::getFirst(); $diff = $oldest->diff($date); $months = $diff->m + 12 * $diff->y; return $months > 0 ? $sum / $months : $sum; }
/** * Tries to predict how much you'll spend * on this day of the month. * @param DateTime $date */ public function predict(Carbon $date = null) { $date = is_null($date) ? Session::get('period') : $date; /** * select alle transacties, op vandaag (dag > 24) * en maand is niet deze (month != 6) * en flikker ze op een hoop (sum amount). * Gedeeld door aantal maanden bezig nu (5) == antwoord. */ $transactions = $this->transactions()->where(DB::Raw('DATE_FORMAT(`date`,"%d")'), '=', $date->format('d'))->where(DB::Raw('DATE_FORMAT(`date`,"%m")'), '!=', $date->format('m'))->where('amount', '<', 0)->get(array('amount')); $sum = 0; $first = BaseController::getFirst($this->id); $diff = $first->diff($date); foreach ($transactions as $t) { $sum += floatval($t->amount) * -1; } $months = $diff->y * 12 + $diff->m; return $months > 0 ? $sum / $months : $sum; }
public function predictionChart() { // in order to predict the future, we look at the past. //$baseAccount = ?; //$startBalance = ?; $setting = Auth::user()->settings()->where('name', '=', 'defaultAmount')->first(); $balance = intval(Crypt::decrypt($setting->value)); $account = Auth::user()->accounts()->orderBy('id', 'ASC')->first(); $debug = Input::get('debug') == 'true' ? true : false; $this->_debug = $debug; $key = $debug ? cacheKey('prediction', Session::get('period'), rand(1, 10000)) : cacheKey('prediction', Session::get('period')); // a setting related to corrections: $doCorrect = Setting::getSetting('correctPredictionChart') == 'true' ? true : false; if (Cache::has($key)) { $data = Cache::get($key); } else { $data = array('cols' => array(array('id' => 'day', 'label' => 'Day of the month', 'type' => 'string', 'p' => array('role' => 'domain')), array('id' => 'actualbalance', 'label' => 'Current balance', 'type' => 'number', 'p' => array('role' => 'data')), array('type' => 'boolean', 'p' => array('role' => 'certainty')), array('id' => 'predictedbalance', 'label' => 'Predicted balance', 'type' => 'number', 'p' => array('role' => 'data')), array('type' => 'number', 'p' => array('role' => 'interval')), array('type' => 'number', 'p' => array('role' => 'interval'))), 'rows' => array()); // set the data array: // some working vars: $first = BaseController::getFirst(); $this->_e('FIRST is ' . $first->format('d M Y')); $today = new Carbon('now'); $this->_e('Today is ' . $today->format('d M Y')); $chartdate = new Carbon('now'); $chartdate->modify('first day of this month'); $index = 0; $this->_e(''); $diff = $first->diff($today); $months = $diff->y * 12 + $diff->m; unset($diff); $specificAmount = Auth::user()->settings()->where('name', '=', 'monthlyAmount')->where('date', '=', $today->format('Y-m-d'))->first(); if ($specificAmount) { $balance = floatval(Crypt::decrypt($specificAmount->value)); } $this->_e('Opening balance: ' . $balance); // loop over each day of the month: $this->_e('start month loop'); for ($i = 1; $i <= intval($today->format('t')); $i++) { $this->_e('Now at day #' . $i); // this array will be used to collect average amounts: $this->_e('Chartdate is: ' . $chartdate->format('Y-m-d')); $this->_e('Today is: ' . $today->format('Y-m-d')); if ($doCorrect && $chartdate > $today || !$doCorrect) { $average = array(); // loop over each month: // get all transaction results for this day of the month: $transactions = Auth::user()->transactions()->where('amount', '<', 0)->where('onetime', '=', 0)->where(DB::Raw('DATE_FORMAT(`date`,"%e")'), '=', $i)->orderBy('amount', 'ASC')->get(); // lets see what we have if (count($transactions) > 0) { $min = floatval($transactions[count($transactions) - 1]->amount) * -1; $max = floatval($transactions[0]->amount) * -1; // fill the array for the averages later on: foreach ($transactions as $t) { //$this->_e('Add to avg['.count($average).'] for transactions: ' . (floatval($t->amount) * -1)); $average[] = floatval($t->amount) * -1; } } else { $min = 0; $max = 0; } // calc avg: $avg = $months > 0 ? array_sum($average) / $months : array_sum($average); //$this->_e('New avg: ' . $avg); $this->_e('Max: ' . $max . ', min: ' . $min . ', avg: ' . $avg); $data['rows'][$index]['c'][0]['v'] = $chartdate->format('j F'); $data['rows'][$index]['c'][1]['v'] = $account->balance($chartdate); // actual balance if ($chartdate > $today) { $data['rows'][$index]['c'][2]['v'] = false; } else { $data['rows'][$index]['c'][2]['v'] = true; } $data['rows'][$index]['c'][3]['v'] = $balance - $avg; // predicted balance $data['rows'][$index]['c'][4]['v'] = $balance - $max; // predicted max expenses. $data['rows'][$index]['c'][5]['v'] = $balance - $min; // predicted max expenses. $balance = $balance - $avg; } else { // don't predict. //$balance = $account->balance($chartdate); $this->_e('No prediction today!'); $balance = $account->balance($chartdate); $data['rows'][$index]['c'][0]['v'] = $chartdate->format('j F'); $data['rows'][$index]['c'][1]['v'] = $balance; // actual balance $data['rows'][$index]['c'][2]['v'] = true; $data['rows'][$index]['c'][3]['v'] = null; $data['rows'][$index]['c'][4]['v'] = null; $data['rows'][$index]['c'][5]['v'] = null; } $index++; $chartdate->addDay(); $this->_e(' '); } Cache::put($key, $data, 1440); } if ($debug) { return '<pre>' . print_r($data, true) . '</pre>'; } return Response::json($data); }
"></div> </td> <?php } ?> </table> </div> </div> <?php } ?> <?php $now = new DateTime('now'); $first = BaseController::getFirst(); $diff = $first->diff($now); if ($diff->m > 1) { ?> <div class="row-fluid"> <div class="span12"> <h4>Overspending</h4> <div id="ovcat"><em>You're doing fine!</em></div> </div> </div> <?php } ?> <script src="/js/home.js"></script> <?php require_once __DIR__ . '/../layouts/bottom.php';
public function overSpending($id) { $category = Auth::user()->categories()->find($id); if ($category) { $key = cacheKey('overspending', $id); if (Cache::has($key)) { $data = Cache::get($key); } else { $data = array(); $data['sum'] = 0; $period = Session::get('period'); // let's collect some intel. // first: transactions in this category. $trans = $category->transactions()->leftJoin('accounts', 'accounts.id', '=', 'account_id')->leftJoin('budgets', 'budgets.id', '=', 'budget_id')->leftJoin('beneficiaries', 'beneficiaries.id', '=', 'beneficiary_id')->where(DB::Raw('DATE_FORMAT(`transactions`.`date`,"%m-%Y")'), '=', $period->format('m-Y'))->orderBy('transactions.date', 'DESC')->get(array('transactions.id', 'transactions.date', 'account_id', 'accounts.name AS account_name', 'budget_id', 'budgets.name AS budget_name', 'beneficiary_id', 'beneficiaries.name AS beneficiary_name', 'transactions.date', 'description', 'transactions.amount', 'onetime')); $data['transactions'] = array(); foreach ($trans as $t) { $tr = array('id' => $t->id, 'date' => new Carbon($t->date), 'account_id' => $t->account_id, 'account_name' => Crypt::decrypt($t->account_name), 'budget_id' => $t->budget_id, 'budget_name' => is_null($t->budget_name) ? null : Crypt::decrypt($t->budget_name), 'beneficiary_id' => $t->beneficiary_id, 'beneficiary_name' => is_null($t->beneficiary_name) ? null : Crypt::decrypt($t->beneficiary_name), 'description' => Crypt::decrypt($t->description), 'amount' => floatval($t->amount) * -1, 'onetime' => $t->onetime == 1 ? true : false); if (!$tr['onetime']) { $data['sum'] += floatval($t->amount) * -1; } $data['transactions'][] = $tr; } // TODO REMOVE THIS. // van elke vorige maand de average. // dat getal moet overeen komen met de average uit de grafiek! $first = BaseController::getFirst(); $first->day = intval($period->format('d')); $last = BaseController::getLast(); $last->day = intval($period->format('d')); $data['past'] = array(); $sum = 0; // get spending so far for each month: while ($first <= $last) { $spent = $category->spent($first); $arr = array('date' => $first->format('F Y'), 'spent' => $spent, 'start_date' => $first->format('Y-m-') . '01', 'end_date' => $first->format('Y-m-t')); $data['past'][] = $arr; if ($first != $last) { $sum += $arr['spent']; } $first->addMonth(); } $data['average'] = $category->averagespending(); return View::make('categories.overspending')->with('category', $category)->with('data', $data); } } }
/** * Tries to predict how much you'll spend * on this day of the month. * @param DateTime $date */ public function predict(Carbon $date = null) { $date = is_null($date) ? Session::get('period') : $date; $name = Crypt::decrypt($this->name); $similar = array(); // find likewise budgets: foreach (Auth::user()->budgets()->get() as $b) { $b->name = Crypt::decrypt($b->name); if ($b->name == $name && $b->id != $this->id) { $similar[] = intval($b->id); } } if (count($similar) == 0) { return 0; } $similar[] = $this->id; /** * select alle transacties, na vandaag (dag > 24) * en maand is niet deze (month != 6) * en flikker ze op een hoop (sum amount). * Gedeeld door aantal maanden bezig nu (5) == antwoord. */ $total = Auth::user()->transactions()->where(DB::Raw('DATE_FORMAT(`date`,"%d")'), '=', $date->format('d'))->whereIn('budget_id', $similar)->where('amount', '<', 0)->sum('amount'); $oldest = BaseController::getFirst(); $diff = $oldest->diff($date); $months = $diff->m + 12 * $diff->y; return $months != 0 ? $total * -1 / $months : $total * -1; }