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(); ?>