public function showAll()
 {
     $key = cacheKey('Beneficiaries', 'showAll');
     if (Cache::has($key)) {
         $data = Cache::get($key);
     } else {
         $data = array();
         $beneficiaries = Auth::user()->beneficiaries()->orderBy('id', 'ASC')->get();
         // to get the avg per month we first need the number of months
         foreach ($beneficiaries as $ben) {
             $name = Crypt::decrypt($ben->name);
             $bene = array('id' => intval($ben->id), 'name' => $name);
             $now = new Carbon('now');
             $thisMonth = $ben->transactions()->where(DB::Raw('DATE_FORMAT(`date`,"%m-%Y")'), '=', $now->format('m-Y'))->sum('amount');
             $bene['month'] = floatval($thisMonth);
             $data[] = $bene;
         }
         unset($name);
         $name = array();
         // order by alfabet
         // Obtain a list of columns
         foreach ($data as $key => $row) {
             $id[$key] = $row['id'];
             $name[$key] = $row['name'];
         }
         array_multisort($name, SORT_ASC, $id, SORT_DESC, $data);
         Cache::put($key, $data, 1440);
     }
     return View::make('beneficiaries.all')->with('beneficiaries', $data);
 }
Example #2
0
 public function getHome()
 {
     $key = cacheKey('home', Session::get('period'));
     if (Cache::has($key)) {
         $data = Cache::get($key);
     } else {
         $max = 0;
         $min = 1000000;
         $data = array('accounts' => array(), 'budgets' => array(), 'targets' => array());
         // we need this list:
         $accounts = Auth::user()->accounts()->get();
         foreach ($accounts as $a) {
             $account = array('id' => intval($a->id), 'name' => Crypt::decrypt($a->name), 'currentbalance' => $a->balance());
             $account['header'] = $account['currentbalance'] < 0 ? array('style' => 'color:red;', 'class' => 'tt', 'title' => $account['name'] . ' has a balance below zero. Try to fix this.') : array();
             $min = $account['currentbalance'] < $min ? $account['currentbalance'] : $min;
             $max = $account['currentbalance'] > $max ? $account['currentbalance'] : $max;
             $data['accounts'][] = $account;
         }
         $min = $min > 0 ? 0 : $min;
         $max = $max < 0 ? 0 : $max;
         $min = floor($min / 1000) * 1000;
         $max = ceil($max / 1000) * 1000;
         $sum = 0;
         foreach ($data['accounts'] as $index => $account) {
             $sum += $account['currentbalance'];
         }
         $data['acc_data']['sum'] = $sum;
         // now everything for budgets:
         $data['budgets'] = Budget::getHomeOverview();
         // some extra budget data:
         $monthlyAmount = Setting::getSetting('monthlyAmount', Session::get('period')->format('Y-m-') . '01');
         if (is_null($monthlyAmount)) {
             $monthlyAmount = intval(Setting::getSetting('defaultAmount'));
         }
         $data['budget_data']['amount'] = $monthlyAmount;
         $data['budget_data']['spent_outside'] = floatval(Auth::user()->transactions()->where('amount', '<', 0)->whereNull('budget_id')->where(DB::Raw('DATE_FORMAT(`date`,"%m-%Y")'), '=', Session::get('period')->format('m-Y'))->sum('amount')) * -1;
         // targets, cant make it better im afraid.
         $data['targets'] = Target::getHomeOverview();
         Cache::put($key, $data, 2440);
     }
     // flash some warnings:
     if (Auth::user()->transactions()->count() == 0) {
         Session::flash('warning', 'There are no transactions saved yet. Create some to make this overview less boring (Create &rarr; New transaction).');
     }
     if (count($data['budgets']) == 0) {
         Session::flash('warning', 'You don\'t have any budgets defined.');
     }
     if (count($data['accounts']) == 0) {
         Session::flash('warning', 'You do not have any accounts added. You should do this first (Create &rarr; New account)');
     }
     if (Holmes::isMobile()) {
         return View::make('mobile.home.home')->with('data', $data);
     } else {
         return View::make('home.home')->with('data', $data);
     }
 }
Example #3
0
 public function budgetCharts()
 {
     $budgets = Budget::get();
     $BC = new BudgetController();
     $date = new Carbon('today');
     $count = 0;
     foreach ($budgets as $budget) {
         Auth::loginUsingId($budget->fireflyuser_id);
         // remove cached entry:
         $key = cacheKey('Budget', 'homeOverviewChart', $budget->id, $date);
         Cache::forget($key);
         $BC->homeOverviewChart($budget->id, $date);
         $count++;
         Auth::logout();
     }
     return 'Regenerated ' . $count . ' budget charts.';
 }
Example #4
0
 public function showAll()
 {
     $key = cacheKey('Transfers', 'showAll');
     if (Cache::has($key)) {
         $data = Cache::get($key);
     } else {
         $data = array();
         $transf = Auth::user()->transfers()->leftJoin('categories', 'categories.id', '=', 'category_id')->leftJoin('accounts as af', 'af.id', '=', 'account_from')->leftJoin('accounts as at', 'at.id', '=', 'account_to')->leftJoin('budgets', 'budgets.id', '=', 'budget_id')->leftJoin('targets', 'targets.id', '=', 'target_id')->orderBy('transfers.date', 'DESC')->orderBy('transfers.created_at', 'DESC')->get(array('transfers.id', 'category_id', 'categories.name AS category_name', 'account_to', 'at.name AS account_to_name', 'account_from', 'af.name AS account_from_name', 'budget_id', 'budgets.name AS budget_name', 'target_id', 'targets.description AS target_description', 'transfers.date', 'transfers.description', 'transfers.amount'));
         foreach ($transf as $t) {
             $month = new Carbon($t->date);
             $strMonth = $month->format('F Y');
             $data[$strMonth] = isset($data[$strMonth]) ? $data[$strMonth] : array();
             $strDate = $month->format('d F Y');
             $current = array('id' => intval($t->id), 'date' => $strDate, 'description' => Crypt::decrypt($t->description), 'amount' => mf(floatval($t->amount)), 'account_to' => $t->account_to, 'account_to_name' => Crypt::decrypt($t->account_to_name), 'account_from' => $t->account_from, 'account_from_name' => Crypt::decrypt($t->account_from_name), 'budget_id' => $t->budget_id, 'budget_name' => is_null($t->budget_id) ? null : Crypt::decrypt($t->budget_name), 'target_id' => $t->target_id, 'target_description' => is_null($t->target_id) ? null : Crypt::decrypt($t->target_description), 'category_id' => $t->category_id, 'category_name' => is_null($t->category_id) ? null : Crypt::decrypt($t->category_name));
             $data[$strMonth][] = $current;
         }
         //Cache::put($key, $data, 4000);
     }
     return View::make('transfers.all')->with('transfers', $data);
 }
 public function showAll()
 {
     $key = cacheKey('Transactions', 'showAll');
     if (Cache::has($key)) {
         $data = Cache::get($key);
     } else {
         $data = array();
         $trans = Auth::user()->transactions()->leftJoin('accounts', 'accounts.id', '=', 'account_id')->leftJoin('budgets', 'budgets.id', '=', 'budget_id')->leftJoin('categories', 'categories.id', '=', 'category_id')->leftJoin('beneficiaries', 'beneficiaries.id', '=', 'beneficiary_id')->orderBy('transactions.date', 'DESC')->get(array('transactions.id', 'account_id', 'accounts.name AS account_name', 'budget_id', 'budgets.name AS budget_name', 'category_id', 'categories.name AS category_name', 'beneficiary_id', 'beneficiaries.name AS beneficiary_name', 'transactions.date', 'description', 'transactions.amount', 'onetime'));
         foreach ($trans as $t) {
             $month = new Carbon($t->date);
             $strMonth = $month->format('F Y');
             $data[$strMonth] = isset($data[$strMonth]) ? $data[$strMonth] : array();
             $date = new Carbon($t->date);
             $strDate = $date->format('d F Y');
             $current = array('id' => intval($t->id), 'date' => $strDate, 'description' => Crypt::decrypt($t->description), 'amount' => mf(floatval($t->amount)), 'onetime' => $t->onetime == 1 ? true : false, 'account_id' => $t->account_id, 'account_name' => Crypt::decrypt($t->account_name), 'budget_id' => $t->budget_id, 'budget_name' => !is_null($t->budget_name) ? Crypt::decrypt($t->budget_name) : null, 'beneficiary_id' => $t->beneficiary_id, 'beneficiary_name' => !is_null($t->beneficiary_name) ? Crypt::decrypt($t->beneficiary_name) : null, 'category_id' => $t->category_id, 'category_name' => !is_null($t->category_name) ? Crypt::decrypt($t->category_name) : null);
             $data[$strMonth][] = $current;
         }
         Cache::put($key, $data, 1440);
     }
     return View::make('transactions.all')->with('transactions', $data);
 }
Example #6
0
 public function homeOverviewChart($id = 0, $date = null)
 {
     $date = is_null($date) ? Session::get('period') : $date;
     $key = cacheKey('Account', 'homeOverviewChart', $id, $date);
     if (Cache::has($key)) {
         return Response::json(Cache::get($key));
     }
     // 30 days into the past.
     $today = new Carbon(Session::get('period')->format('Y-m-d'));
     // we do some fixing in case we're in the future:
     $actuallyToday = new Carbon('now');
     if ($today > $actuallyToday) {
         $today->modify('last day of this month');
     }
     $past = clone $today;
     $past->subDays(30);
     $account = Auth::user()->accounts()->find($id);
     $data = array('cols' => array(array('id' => 'date', 'label' => 'Date', 'type' => 'date', 'p' => array('role' => 'domain')), array('id' => 'balance', 'label' => 'Balance', 'type' => 'number', 'p' => array('role' => 'data'))), 'rows' => array());
     $bdp_q = $account->balancedatapoints()->where('date', '>=', $past->format('Y-m-d'))->where('date', '<=', $today->format('Y-m-d'))->get();
     $bdp = array();
     foreach ($bdp_q as $b) {
         $bdp[$b->date] = floatval($b->balance);
     }
     $index = 0;
     while ($past <= $today) {
         $month = intval($past->format('n')) - 1;
         $year = intval($past->format('Y'));
         $day = intval($past->format('j'));
         $data['rows'][$index]['c'][0]['v'] = 'Date(' . $year . ', ' . $month . ', ' . $day . ')';
         $balance = isset($bdp[$past->format('Y-m-d')]) ? $bdp[$past->format('Y-m-d')] : null;
         $data['rows'][$index]['c'][1]['v'] = $balance;
         $past->add(new DateInterval('P1D'));
         $index++;
     }
     Cache::put($key, $data, 1440);
     return Response::json($data);
 }
Example #7
0
 public function homeOverviewChart($id = 0, $date = null)
 {
     $date = is_null($date) ? Session::get('period') : $date;
     $key = cacheKey('Budget', 'homeOverviewChart', $id, $date);
     if (Cache::has($key)) {
         return Response::json(Cache::get($key));
     }
     // 30 days into the past.
     $end = clone Session::get('period');
     $end->modify('last day of this month ');
     $today = new Carbon('now');
     $today->modify('midnight');
     $past = clone $end;
     $past->modify('first day of this month midnight');
     $budget = Auth::user()->budgets()->find($id);
     $data = array('cols' => array(array('id' => 'date', 'label' => 'Date', 'type' => 'date', 'p' => array('role' => 'domain')), array('id' => 'balance', 'label' => 'Left', 'type' => 'number', 'p' => array('role' => 'data')), array('type' => 'boolean', 'p' => array('role' => 'certainty'))), 'rows' => array());
     $index = 0;
     $balance = $budget->amount;
     // get the prediction points (if any):
     $points = $budget->budgetpredictionpoints()->get();
     $prediction = array();
     foreach ($points as $p) {
         $prediction[intval($p->day)] = floatval($p->amount);
     }
     while ($past <= $end) {
         $month = intval($past->format('n')) - 1;
         $year = intval($past->format('Y'));
         $day = intval($past->format('j'));
         $data['rows'][$index]['c'][0]['v'] = 'Date(' . $year . ', ' . $month . ', ' . $day . ')';
         if ($past <= $today) {
             $balance = $budget->left($past);
             $data['rows'][$index]['c'][1]['v'] = $balance;
             $data['rows'][$index]['c'][2]['v'] = true;
         } else {
             $balance = $balance - (isset($prediction[$day]) ? $prediction[$day] : 0);
             $data['rows'][$index]['c'][1]['v'] = $balance;
             $data['rows'][$index]['c'][2]['v'] = false;
         }
         $past->add(new DateInterval('P1D'));
         $index++;
     }
     Cache::put($key, $data, 1440);
     return Response::json($data);
 }
Example #8
0
 public function showOverExpendingCategories()
 {
     $key = cacheKey('overExpendingCategories', Session::get('period'));
     if (Cache::has($key)) {
         return Response::json(Cache::get($key));
     }
     $categories = Auth::user()->categories()->get();
     $data = array('cols' => array(array('id' => 'Category', 'label' => 'Category', 'type' => 'string'), array('id' => 'toomuchpct', 'label' => 'Overspent pct', 'type' => 'number'), array('id' => 'spent', 'label' => 'Spent so far in total', 'type' => 'number'), array('id' => 'judgement', 'label' => 'Judgement', 'type' => 'string'), array('id' => 'spentavg', 'label' => 'Spent on average', 'type' => 'number')), 'rows' => array());
     $collection = array();
     foreach ($categories as $category) {
         $avg_spent = $category->averagespending();
         $spent = $category->spent();
         $category->name = Crypt::decrypt($category->name);
         if ($avg_spent > 0) {
             if ($avg_spent < $spent) {
                 $current = array();
                 // overspent as part of average:
                 $spentpct = 100 - $avg_spent / $spent * 100;
                 $spentindex = round($spentpct / 10, 0);
                 if ($spentindex == 0) {
                     $descr = 'Overspent < 10%';
                 } else {
                     $descr = 'Overspent ~' . $spentindex * 10 . '%';
                 }
                 // 0: Naam van bolletje.
                 // 1: Verticale as (hoger is hoger)
                 // 2: Horizontale as (hoger is verder naar rechts
                 // 3: Kleur(groep)
                 // 4: grootte van bolletje.
                 $current['c'][0]['v'] = $category->name;
                 // name
                 //$current['c'][1]['v'] = $spent - $avg_spent; // over spent
                 $current['c'][1]['v'] = $spentpct;
                 // spent so far
                 $current['c'][2]['v'] = $spent;
                 // spent so far
                 $current['c'][3]['v'] = $descr;
                 // judge
                 $current['c'][4]['v'] = $avg_spent;
                 // spent on avg
                 $current['spentindex'] = $spentindex;
                 $collection[] = $current;
             }
         }
     }
     $tmp = array();
     foreach ($collection as &$ma) {
         $tmp[] =& $ma['spentindex'];
     }
     array_multisort($tmp, $collection);
     $index = 0;
     foreach ($collection as $c) {
         $data['rows'][$index]['c'] = $c['c'];
         $index++;
     }
     Cache::put($key, $data, 1440);
     return Response::json($data);
 }
Example #9
0
 public function homeOverviewChart($id = 0)
 {
     $target = Auth::user()->targets()->find($id);
     if ($target) {
         $key = cacheKey('Target', 'homeOverviewChart', $id, Session::get('period'));
         if (Cache::has($key)) {
             return Response::json(Cache::get($key));
         } else {
             $data = array('cols' => array(array('id' => 'date', 'label' => 'Date', 'type' => 'date', 'p' => array('role' => 'domain')), array('id' => 'guideline', 'label' => 'Guideline', 'type' => 'number', 'p' => array('role' => 'data')), array('id' => 'saved', 'label' => 'Saved', 'type' => 'number', 'p' => array('role' => 'data'))), 'rows' => array());
             $start = new Carbon($target->startdate);
             if ($target->duedate == '0000-00-00') {
                 $end = new Carbon('today');
             } else {
                 $end = new Carbon($target->duedate);
             }
             $current = clone $start;
             $index = 0;
             $guide = 0;
             $step = $target->guide($start, true);
             // transfers
             $transfers_q = $target->transfers()->where('date', '<=', $end->format('Y-m-d'))->get();
             $transferred = array();
             foreach ($transfers_q as $t) {
                 $transferred[$t->date] = isset($transferred[$t->date]) ? $transferred[$t->date] : 0;
                 if ($t->account_from == $target->account_id) {
                     $transferred[$t->date] -= floatval($t->amount);
                 } else {
                     if ($t->account_to == $target->account_id) {
                         $transferred[$t->date] += floatval($t->amount);
                     }
                 }
             }
             $saved = 0;
             while ($current <= $end) {
                 $saved += isset($transferred[$current->format('Y-m-d')]) ? $transferred[$current->format('Y-m-d')] : 0;
                 $month = intval($current->format('n')) - 1;
                 $year = intval($current->format('Y'));
                 $day = intval($current->format('j'));
                 $data['rows'][$index]['c'][0]['v'] = 'Date(' . $year . ', ' . $month . ', ' . $day . ')';
                 $data['rows'][$index]['c'][1]['v'] = $guide;
                 $data['rows'][$index]['c'][2]['v'] = $saved;
                 // $target->hassaved($current);
                 $current->add(new DateInterval('P1D'));
                 $guide += $step;
                 $index++;
             }
             Cache::put($key, $data, 1440);
             return Response::json($data);
         }
     } else {
         return App::abort(404);
     }
 }
Example #10
0
<?php 
$index = 1;
foreach ($budgets as $budget => $data) {
    ?>
  <div class="span4">
    <h4><?php 
    echo $budget;
    ?>
</h4>

    <script type="text/javascript">
      cached["<?php 
    echo $budget;
    ?>
"] = <?php 
    echo json_encode(Cache::get(cacheKey('budgetProgress', $budget, Session::get('period'))));
    ?>
    </script>

    <div id="budget_<?php 
    echo Str::slug($budget);
    ?>
" data-value="<?php 
    echo $budget;
    ?>
" class="loading budgetProgressChart"></div>
    <table class="table table-condensed">
      <tr>
        <td><?php 
    echo mf($data['spent']);
    ?>
Example #11
0
          <?php 
        $sum = 0;
        $spent = 0;
        foreach ($data['budgets'] as $budget) {
            $sum += $budget['amount'];
            $spent += $budget['spent'];
            ?>

            <tr>
              <th style="width:30%;">
                <script type="text/javascript">
                  budgetCache[<?php 
            echo $budget['id'];
            ?>
] = <?php 
            echo json_encode(Cache::get(cacheKey('Budget', 'homeOverviewChart', $budget['id'], Session::get('period'))));
            ?>
;
                </script>

                <?php 
            if ($budget['overflow'] === false) {
                ?>
                  <a href="/home/budget/overview/<?php 
                echo $budget['id'];
                ?>
"><?php 
                echo $budget['name'];
                ?>
</a>
                <?php 
Example #12
0
 /**
  * Returns date of very last transaction or
  * transfer
  */
 public static function getLast($accountID = null)
 {
     if (!is_null($accountID)) {
         $account = Auth::user()->accounts()->find($accountID);
         $last = cacheKey('getLast', $account->id);
     } else {
         $account = null;
         $last = cacheKey('getLast');
     }
     if (Cache::has($last)) {
         return Cache::get($last);
     }
     // build the query:
     $transf_query = Auth::user()->transfers()->orderBy('date', 'DESC');
     $transa_query = Auth::user()->transactions()->orderBy('date', 'DESC');
     if (!is_null($account)) {
         $transa_query->where('account_id', '=', $account->id);
         $transf_query->where(function ($query) use($account) {
             $query->where('account_from', '=', $account->id);
             $query->orWhere('account_to', '=', $account->id);
         });
     }
     $transfer = $transf_query->first();
     $transaction = $transa_query->first();
     $transf_date = is_null($transfer) ? new Carbon('now') : new Carbon($transfer->date);
     $transa_date = is_null($transaction) ? new Carbon('now') : new Carbon($transaction->date);
     $result = max($transf_date, $transa_date);
     Cache::put($last, $result, 5000);
     return $result;
 }
 public function compareCategories()
 {
     // three columns: category name, this month and the previous month.
     // can we force HTML in this table by adding it to the data?
     $this->_getValues();
     $key = cacheKey('categories', md5(json_encode(Input::all())));
     if (Cache::has($key)) {
         return Response::json(Cache::get($key));
     }
     $compares = array();
     foreach (Input::get('compare') as $c) {
         $d = new Carbon($c);
         $compares[] = $d->format('m-Y');
     }
     $data = array('cols' => array(array('id' => 'cat', 'label' => 'Category', 'type' => 'string', 'p' => array('role' => 'domain')), array('id' => 'base', 'label' => $this->_base->format('F Y'), 'type' => 'number', 'p' => array('role' => 'data')), array('id' => 'compare', 'label' => 'Selection', 'type' => 'number', 'p' => array('role' => 'data'))), 'rows' => array());
     if (count($this->_compares) == 1) {
         $data['cols'][2]['label'] = $this->_compares[0]->format('F Y');
     }
     // first a special row:
     // expenses without category.
     // needs to be said as well.
     $nocat_transactions = floatval(Auth::user()->transactions()->whereNull('category_id')->where(DB::Raw('DATE_FORMAT(`date`,"%m-%Y")'), '=', $this->_base->format('m-Y'))->sum('amount')) * -1;
     $nocat_prev_transactions = floatval(Auth::user()->transactions()->whereNull('category_id')->where(DB::Raw('DATE_FORMAT(`date`,"%d")'), '<=', DB::Raw(intval($this->_base->format('d'))))->whereIn(DB::Raw('DATE_FORMAT(`date`,"%m-%Y")'), $compares)->sum('amount')) / count($compares) * -1;
     $data['rows'][0]['c'][0]['v'] = '(no category)';
     $data['rows'][0]['c'][1]['v'] = $nocat_transactions;
     $data['rows'][0]['c'][2]['v'] = $nocat_prev_transactions;
     // for the 'past', we can look at $_base for the day?
     // go for current month:
     $categories = Auth::user()->categories()->get();
     $index = 1;
     foreach ($categories as $category) {
         // current month's expenses in this category:
         $transactions = floatval($category->transactions()->where(DB::Raw('DATE_FORMAT(`date`,"%m-%Y")'), '=', $this->_base->format('m-Y'))->sum('amount')) * -1;
         $sum = $transactions;
         if ($sum > 0) {
             $prev_transactions = floatval($category->transactions()->where(DB::Raw('DATE_FORMAT(`date`,"%d")'), '<=', intval($this->_base->format('d')))->whereIn(DB::Raw('DATE_FORMAT(`date`,"%m-%Y")'), $compares)->sum('amount')) * -1;
             $data['rows'][$index]['c'][0]['v'] = Crypt::decrypt($category->name);
             $data['rows'][$index]['c'][1]['v'] = $sum;
             $data['rows'][$index]['c'][2]['v'] = $prev_transactions;
             $index++;
         }
     }
     Cache::put($key, $data, 1440);
     return Response::json($data);
 }
Example #14
0
 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);
         }
     }
 }
Example #15
0
 function showTransactions($object)
 {
     $id = intval(Input::get('id'));
     $start = new Carbon(Input::get('start'));
     $end = new Carbon(Input::get('end'));
     $objects = Str::plural($object);
     // we might filter on expenses or incomes:
     $modifier = Input::get('modifier');
     // we might need to filter on a certain object.
     // this object is called the child.
     // so the object might be an account, and the child might be beneficiary
     $children = Input::get('childType');
     // yes this is confusing.
     $child = !is_null($children) ? Str::singular($children) : null;
     // find the ID that this child signifies.
     // this might be a beneficiary name or a budget name
     $selection = Input::get('childValue');
     $selectedItem = false;
     // false means no selection made.
     if (!strstr($selection, '(no ') === false) {
         $selectedItem = null;
         // null means select where NULL.
     }
     if (!is_null($selection) && $selectedItem === false) {
         // find it:
         switch ($child) {
             default:
                 // just find it.
                 $items = Auth::user()->{$children}()->get();
                 foreach ($items as $item) {
                     if (Crypt::decrypt($item->name) == $selection) {
                         $selectedItem = $item;
                     }
                 }
                 break;
             case 'budget':
                 // keep the date in mind!
                 $split = explode('(', $selection);
                 $budget_name = trim($split[0]);
                 $dateStr = trim(str_replace(')', '', $split[1]));
                 $date = new Carbon($dateStr);
                 $budgets = Auth::user()->budgets()->where('date', '=', $date->format('Y-m-d'))->get();
                 foreach ($budgets as $b) {
                     if (Crypt::decrypt($b->name) == $budget_name) {
                         $selectedItem = $b;
                     }
                 }
                 break;
         }
     }
     //var_dump($selectedItem);exit;
     $db = Auth::user()->{$objects}()->find($id);
     // create (and find)  a cache key:
     $key = cacheKey('PieChart', $id, $start, $end, $objects, $children, $modifier, $selection);
     if (Cache::has($key)) {
         return Response::json(Cache::get($key));
     }
     if (!$db) {
         return App::abort(404);
     }
     // find the transactions and transfers for $object in range.
     $index = 0;
     $data = array('cols' => array(array('id' => 'date', 'label' => 'Date', 'type' => 'date'), array('id' => 'description', 'label' => 'Description', 'type' => 'string'), array('id' => 'amount', 'label' => 'Amount', 'type' => 'number'), array('id' => 'account', 'label' => 'Account(s)', 'type' => 'string'), array('id' => 'budget', 'label' => 'Budget', 'type' => 'string'), array('id' => 'category', 'label' => 'Category', 'type' => 'string'), array('id' => 'beneficiary', 'label' => 'Beneficiary', 'type' => 'string'), array('id' => 'target', 'label' => 'Target', 'type' => 'string')), 'rows' => array());
     $transactionsQuery = $db->transactions()->leftJoin('accounts', 'accounts.id', '=', 'account_id')->leftJoin('budgets', 'budgets.id', '=', 'budget_id')->leftJoin('categories', 'categories.id', '=', 'category_id')->leftJoin('beneficiaries', 'beneficiaries.id', '=', 'beneficiary_id')->orderBy('transactions.date', 'DESC')->where('transactions.date', '>=', $start->format('Y-m-d'))->where('transactions.date', '<=', $end->format('Y-m-d'));
     if ($modifier == 'income') {
         $transactionsQuery->where('transactions.amount', '>', 0);
     } else {
         if ($modifier == 'expenses') {
             $transactionsQuery->where('transactions.amount', '<', 0);
         }
     }
     if (is_null($selectedItem)) {
         $transactionsQuery->whereNull($child . '_id');
     } else {
         if (!is_null($selectedItem) && !$selectedItem === false) {
             $transactionsQuery->where($child . '_id', '=', $selectedItem->id);
         }
     }
     $transactions = $transactionsQuery->get(array('transactions.id', 'accounts.name AS account_name', 'budgets.name AS budget_name', 'categories.name AS category_name', 'beneficiaries.name AS beneficiary_name', 'transactions.date', 'description', 'transactions.amount', 'onetime'));
     foreach ($transactions as $t) {
         $date = new Carbon($t->date);
         $data['rows'][$index]['c'][0]['v'] = 'Date(' . intval($date->format('Y')) . ', ' . (intval($date->format('n')) - 1) . ', ' . intval($date->format('j')) . ')';
         $data['rows'][$index]['c'][1]['v'] = Crypt::decrypt($t->description);
         $data['rows'][$index]['c'][2]['v'] = floatval($t->amount);
         $data['rows'][$index]['c'][3]['v'] = is_null($t->account_name) ? null : Crypt::decrypt($t->account_name);
         $data['rows'][$index]['c'][4]['v'] = is_null($t->budget_name) ? null : Crypt::decrypt($t->budget_name);
         $data['rows'][$index]['c'][5]['v'] = is_null($t->category_name) ? null : Crypt::decrypt($t->category_name);
         $data['rows'][$index]['c'][6]['v'] = is_null($t->beneficiary_name) ? null : Crypt::decrypt($t->beneficiary_name);
         $data['rows'][$index]['c'][7]['v'] = null;
         $index++;
     }
     Cache::put($key, $data, 1440);
     return Response::json($data);
 }