You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.4 KiB
PHP

<?
include_once("../php/include-request.php");
verifyAuthorized();
$action = getMandatoryRequestValue("action");
initDatabaseConnection();
if($action == "graph-month-totals") {
// input
$year = intval(getMandatoryRequestValue("year"));
// fetch data
$data = array();
$months = array();
$names = array();
$rows = dbQuery("SELECT * FROM stat_month_totals WHERE year = ? AND household_id = ?", $year, getHouseholdId());
foreach($rows as $row) {
array_push($data, $row);
$months[$row['month']] = $row['month'];
$names[$row['account_name']] = $row['account_name'];
}
// render data
$xCategories = array();
foreach($months as $month) {
array_push($xCategories, getMonthStr($month));
}
$s = 0;
$t = 0;
function getNextPersonExpenseColor($expense) {
global $s, $t;
$d = "5";
if($expense) {
return "#".dechex(15 - ($s++) * 4).$d.$d;
} else {
return "#".$d.dechex(15 - ($t++) * 4).$d;
}
return "yellow";
}
$types = array("0", "1");
$series = array();
foreach($types as $type) {
foreach($names as $name) {
$serieData = array();
foreach($months as $month) {
$found = false;
for($i = 0; $i < sizeof($data); $i++) {
if($data[$i]['month'] == $month &&
$data[$i]['expense'] == $type &&
$data[$i]['account_name'] == $name) {
array_push($serieData, round($data[$i]['amount']));
$found = true;
}
}
if(!$found) {
array_push($serieData, 0);
}
}
$serie = array(
"name" => insertExpenseText($type, true)." ($name)",
"color" => getNextPersonExpenseColor($type),
"data" => $serieData,
"stack" => $type
);
array_push($series, $serie);
}
}
$response = array(
"year" => $year,
"xCategories" => $xCategories,
"series" => $series
);
requestOk(json_encode($response, JSON_PRETTY_PRINT));
} else if($action == "graph-month-plus") {
// input
$year = intval(getMandatoryRequestValue("year"));
// fetch data
$months = array();
$xCategories = array();
$seriesData = array();
$rows = dbQuery("SELECT * FROM stat_month_plus WHERE year = ? AND household_id = ?", $year, getHouseholdId());
foreach($rows as $row) {
$months[$row['month']] = $row['amount'];
$data = array(
"y" => round($row['amount']),
"color" => ($row['amount'] < 0 ? "#fc4954" : "#7ded55")
);
array_push($xCategories, getMonthStr($row['month']));
array_push($seriesData, $data);
}
$response = array(
"year" => $year,
"xCategories" => $xCategories,
"seriesData" => $seriesData
);
requestOk(json_encode($response, JSON_PRETTY_PRINT));
} else if($action == "graph-month-avg") {
// input
$year = intval(getMandatoryRequestValue("year"));
// fetch data
$monthRows = dbQuery("SELECT count(DISTINCT month) AS c FROM record_valid WHERE year = ? AND household_id = ?", $year, getHouseholdId());
$monthCount = $monthRows[0]['c'];
$rows1 = dbQuery("SELECT * FROM stat_month_sum1 WHERE year = ? AND household_id = ?", $year, getHouseholdId());
$rows2 = dbQuery("SELECT * FROM stat_month_sum2 WHERE year = ? AND household_id = ?", $year, getHouseholdId());
$rows3 = dbQuery("SELECT * FROM stat_month_sum3 WHERE year = ? AND household_id = ?", $year, getHouseholdId());
// put it together
$seriesData = array();
foreach($rows1 as $row1) {
$data = array(
"name" => insertExpenseText($row1['expense'], true),
"y" => round($row1['amount'] / $monthCount),
"color" => ($row1['expense'] == 1 ? "#fc4954" : "#7ded55"),
"drilldown" => insertExpenseText($row1['expense'])
);
array_push($seriesData, $data);
}
$drilldownSeries = array();
foreach($rows1 as $row1) {
$data = array();
foreach($rows2 as $row2) {
if($row1['expense'] == $row2['expense']) {
$obj = array(
"name" => $row2['category_group_name'],
"y" => round($row2['amount'] / $monthCount),
"drilldown" => $row2['category_group_id']
);
array_push($data, $obj);
}
}
$serie = array(
"id" => insertExpenseText($row1['expense']),
"name" => insertExpenseText($row1['expense'], true),
"data" => $data
);
array_push($drilldownSeries, $serie);
}
foreach($rows2 as $row2) {
$data = array();
foreach($rows3 as $row3) {
if($row2['expense'] == $row3['expense'] && $row2['category_group_id'] == $row3['category_group_id']) {
$obj = array(
"id" => $row3['category_id'],
"name" => $row3['category_name'],
"y" => round($row3['amount'] / $monthCount)
);
array_push($data, $obj);
}
}
$serie = array(
"id" => $row2['category_group_id'],
"name" => $row2['category_group_name'],
"data" => $data
);
array_push($drilldownSeries, $serie);
}
// send response
$response = array(
"year" => $year,
"seriesData" => $seriesData,
"drilldownSeries" => $drilldownSeries,
);
requestOk(json_encode($response, JSON_PRETTY_PRINT));
} else if($action == "category-details") {
// input
$year = intval(getMandatoryRequestValue("year"));
$categoryId = intval(getMandatoryRequestValue("categoryId"));
// fetch data
$monthRows = dbQuery("SELECT DISTINCT month FROM record_valid WHERE year = ? AND household_id = ? ORDER BY month", $year, getHouseholdId());
$months = array();
$yearSum = 0;
$detailsRows = dbQuery("SELECT * FROM stat_category_details WHERE year = ? AND category_id = ? AND household_id = ?", $year, $categoryId, getHouseholdId());
foreach($monthRows as $monthRow) {
$sum = 0;
foreach($detailsRows as $detailsRow) {
if($monthRow['month'] == $detailsRow['month']) {
$sum = $detailsRow['amount'];
}
$categoryGroupName = $detailsRow['category_group_name'];
$categoryName = $detailsRow['category_name'];
}
$data = array(
"month" => getMonthStr($monthRow['month']),
"sum" => round($sum)
);
array_push($months, $data);
$yearSum += $sum;
}
$response = array(
"year" => $year,
"categoryGroupName" => $categoryGroupName,
"categoryId" => $categoryId,
"categoryName" => $categoryName,
"yearSum" => round($yearSum),
"monthlyAverage" => round($yearSum / count($months)),
"months" => $months
);
requestOk(json_encode($response, JSON_PRETTY_PRINT));
} else {
requestFail("Unknown request action \"$action\"");
}
closeDatabaseConnection();
?>