Ejemplo n.º 1
0
 /**
  * This table shows target-vs-actual allocations and info to help with rebalancing.
  *
  * @param int $year  the year for which to report investment allocations.
  *                  Reports data from the latest month in [$year - 1, $year] that has data.
  */
 private function rebalancetable($mysqli, $year)
 {
     $allocations = Db\TargetApi::getTargets($mysqli);
     $data = Db\InvestmentsApi::getInvestments($mysqli, $year - 1, $year);
     if (!$allocations || !$data) {
         $this->noDataChart();
         return;
     }
     $year_index = isset($data[$year]) ? $year : $year - 1;
     $month_index = max(array_keys($data[$year_index]));
     $total = 0;
     foreach ($allocations as $i) {
         if (isset($data[$year_index][$month_index][$i->key])) {
             $total += $data[$year_index][$month_index][$i->key];
         }
     }
     $total = (int) $total;
     if (!$total) {
         $this->noDataChart();
         return;
     }
     $rows = array();
     $rowSortData = array();
     foreach ($allocations as $i) {
         $current = (int) @$data[$year_index][$month_index][$i->key];
         $currentPercent = 100 * $current / $total;
         $target = $i->percent * $total / 100;
         $rows[] = array($i->name, $i->percent, round(100 * $current / $total, 2), '$ ' . round($current / 10) * 10, '$ ' . ($current < $target ? '(' : '') . abs(round(($current - $target) / 10) * 10) . ($current < $target ? ')' : ''), $current < $target ? 'Add funds' : 'Remove funds');
         $rowSortData[] = array($i->name, $i->percent, round($current / $total, 5), $current, round($current - $target, 5), round($current - $target, 5));
     }
     $tdata = array('headers' => array('Category', 'Target %', 'Actual %', '$ Actual', '$ Over (Under)', 'Action'), 'headerSortType' => array('', 'number', 'number', 'number', 'number', ''), 'rows' => $rows, 'rowSortData' => $rowSortData);
     echo json_encode(array("Table", $tdata, array('sortable' => true)));
 }
Ejemplo n.º 2
0
 private function show($mysqli)
 {
     $categories = Db\InvestmentsApi::getCategories($mysqli);
     $res = array();
     foreach ($categories as $i) {
         $res[] = array('term' => $i->name, 'definition' => $i->description, 'icon' => $i->icon);
     }
     echo json_encode(array("DefList", $res));
 }
Ejemplo n.º 3
0
 private function threecell($mysqli)
 {
     $data = Db\InvestmentsApi::getInvestments($mysqli, 0, 3000);
     if (!$data) {
         echo json_encode("Table", null);
         return;
     }
     $tmp = array_keys($data);
     $newest_year = $tmp[count($tmp) - 1];
     $tmp = array_keys($data[$newest_year]);
     $newest_month = $tmp[count($tmp) - 1];
     $newest = $newest_year * 12 + $newest_month - 1;
     $back_three = $newest - 3;
     $newest_net = array_sum($data[$newest_year][$newest_month]);
     if ($newest_month > 3) {
         $back_three = @array_sum($data[$newest_year][$newest_month - 3]);
     } else {
         $back_three = @array_sum($data[$newest_year - 1][$newest_month + 9]);
     }
     $last_year = @array_sum($data[$newest_year - 1][$newest_month]);
     $tdata = array('headers' => array('3-month Growth', '1-year growth', 'Total savings & investments'), 'rows' => array(array($back_three ? '$' . number_format($newest_net - $back_three, 2) : '--', $last_year ? '$' . number_format($newest_net - $last_year, 2) : '--', '$' . number_format($newest_net, 2))));
     echo json_encode(array("Table", $tdata));
 }
Ejemplo n.º 4
0
 public function yearReport($mysqli, $year)
 {
     echo '<h1>' . $year . ' Annual Report</h1>';
     $data = Db\AnnualApi::getYear($mysqli, $year);
     echo '<h2>Income</h2>';
     echo '<table border=1 style="border-collapse: collapse;"><tr><th>Key</th><th>Value</th></tr>';
     foreach ($data as $key => $value) {
         printf('<tr><td>%s</td><td>%s</td>', $key, $value);
     }
     echo '</table>';
     $this->renderAnnualPie($year);
     $categories = Db\InvestmentsApi::getCategories($mysqli);
     $data = Db\InvestmentsApi::getInvestments($mysqli, $year, $year);
     echo '<h2>Investments</h2>';
     echo '<table border=1 style="border-collapse: collapse;"><tr><th>Month</th>';
     foreach ($categories as $i) {
         printf('<th>%s</th>', $i->name);
     }
     echo '<th>Total</th></tr>';
     for ($i = 1; $i <= 12; $i++) {
         if (empty($data[$year][$i])) {
             continue;
         }
         echo '<tr><td>' . date("F", mktime(0, 0, 0, $i, 1, $year)) . '</td>';
         foreach ($categories as $category) {
             if (isset($data[$year][$i][$category->key])) {
                 $value = $data[$year][$i][$category->key];
             } else {
                 $value = '';
             }
             printf('<td>%s</td>', $value);
         }
         printf('<td>%s</td>', array_sum($data[$year][$i]));
         echo '</tr>';
     }
     echo '</table>';
     $id = 'canvaschart' . uniqid();
     echo '<div class="canvascontainer line"><canvas data-src="/data/investments/year/' . $year . '" id="' . $id . '"></div>';
 }
Ejemplo n.º 5
0
 private function multiYear($mysqli, $mode, $start_year, $end_year)
 {
     $segmented = Db\InvestmentsApi::getInvestmentsSegmented($mysqli, $start_year, $end_year);
     if (!$segmented) {
         // year() is better at showing empty data
         $this->year($mysqli, $start_year);
         return;
     }
     if ($mode == self::MODE_RETIREMENT) {
         $categories = array((object) array('name' => 'Non-retirement', 'key' => 'NO_RET'), (object) array('name' => 'Retirement', 'key' => 'RET'));
     } else {
         if ($mode == self::MODE_TAX) {
             $categories = array((object) array('name' => 'Post-tax', 'key' => 'POST_TAX'), (object) array('name' => 'Pre-tax', 'key' => 'PRE_TAX'));
         } else {
             if ($mode == self::MODE_TAX_RET) {
                 $categories = array((object) array('name' => 'Non-retirement funds', 'key' => 'NO_RET'), (object) array('name' => 'Pre-tax retirement funds', 'key' => 'PRE_TAX'), (object) array('name' => 'Post-tax retirement funds', 'key' => 'POST_TAX'));
             } else {
                 $categories = Db\InvestmentsApi::getCategories($mysqli);
             }
         }
     }
     $cdata = $this->getBaseData($categories);
     // compute date range
     $start_month = $end_month = null;
     for ($year = $start_year; $year <= $end_year; $year++) {
         for ($i = 1; $i <= 12; $i++) {
             if (isset($segmented[$year][$i])) {
                 if (!$start_month) {
                     $start_month = $year * 12 + $i - 1;
                 }
                 $end_month = $year * 12 + $i - 1;
             }
         }
     }
     // transform $segmented to $data depending on $mode
     $data = array();
     foreach ($segmented as $year => $i) {
         foreach ($i as $month => $j) {
             foreach ($j as $key => $k) {
                 if ($mode == self::MODE_RETIREMENT) {
                     if (empty($data[$year][$month])) {
                         $data[$year][$month] = array('RET' => 0, 'NO_RET' => 0);
                     }
                     $data[$year][$month]['RET'] += $k->value_ret;
                     $data[$year][$month]['NO_RET'] += $k->value_no_ret;
                 } else {
                     if ($mode == self::MODE_TAX) {
                         if (empty($data[$year][$month])) {
                             $data[$year][$month] = array('PRE_TAX' => 0, 'POST_TAX' => 0);
                         }
                         $data[$year][$month]['PRE_TAX'] += $k->value_ret_pretax;
                         $data[$year][$month]['POST_TAX'] += $k->value_posttax;
                     } else {
                         if ($mode == self::MODE_TAX_RET) {
                             if (empty($data[$year][$month])) {
                                 $data[$year][$month] = array('NO_RET' => 0, 'PRE_TAX' => 0, 'POST_TAX' => 0);
                             }
                             $data[$year][$month]['NO_RET'] += $k->value_no_ret;
                             $data[$year][$month]['PRE_TAX'] += $k->value_ret_pretax;
                             $data[$year][$month]['POST_TAX'] += $k->value_ret_posttax;
                         } else {
                             $data[$year][$month][$key] = $k->value;
                         }
                     }
                 }
             }
         }
     }
     // redo labels
     $labels = array();
     for ($i = $start_month; $i <= $end_month; $i++) {
         $labels[] = $cdata['labels'][$i % 12] . ' ' . intval($i / 12);
     }
     $cdata['labels'] = $labels;
     // format data for chart
     $max_value = 0;
     for ($i = count($categories) - 1; $i >= 0; $i--) {
         $category = $categories[$i];
         for ($j = $start_month; $j <= $end_month; $j++) {
             if (isset($data[intval($j / 12)][$j % 12 + 1][$category->key])) {
                 $value = $data[intval($j / 12)][$j % 12 + 1][$category->key];
             } else {
                 $value = null;
             }
             $cdata['datasets'][$i]['itemData'][$j - $start_month] = $value;
             if ($value !== null && isset($cdata['datasets'][$i + 1]['data'][$j - $start_month])) {
                 $value += $cdata['datasets'][$i + 1]['data'][$j - $start_month];
             }
             $cdata['datasets'][$i]['data'][$j - $start_month] = $value;
             $max_value = max($max_value, $value);
         }
     }
     echo json_encode(array("Line", $cdata, $this->getOptions($max_value, $start_year + 1 >= $end_year), $this->getExtra($categories)));
 }