<?php
    include('../../xcrud/xcrud.php');
    header('Content-Type: text/html; charset=' . Xcrud_config::$mbencoding);
    $db = Xcrud_db::get_instance();
    $start = $_POST['start'];
    $end = $_POST['end'];
    $table = $_POST['table'];
     if($start > "" AND $end >""){
        $whereDatum = "WHERE (datum BETWEEN '".$_POST['start']."' AND '".$_POST['end']."')";
    } else {
        $whereDatum ="";
    }
    if ($table > ''){
        $query = "SELECT YEAR(datum) As Year,
                    MONTH(datum) As Month,
                    maincategorie,
                    SUM(cost) As sum
                    FROM ktsgkbank
                    GROUP BY YEAR(datum), MONTH(datum), maincategorie 
                    ORDER BY YEAR, Month, sum";
        $db->query($query);
        $result = $db->result();
        if($result > ""){
            $options="";//print_r($result);
            foreach ($result as $key => $item)
            {   
               $maincategories[] = $item['maincategorie'];
               $json[$item['maincategorie']]["Month".$item['Month']] =floatval($item['sum']);
                        /*$json[] = array(
                        "Month" => $item['Month'],
                        "maincat" => $item['maincategorie'],
                        "sum" => $item['sum']
                          );*/
            }
            
            foreach ($json as $key => $item)
            {   
                $json[$key]["maincat"] = $key;
            }
           foreach ($json as $key => $item)
            {   
                $json2[] = $item;
            }
            echo json_encode($json2, JSON_UNESCAPED_UNICODE);
        } else {
            echo $query;
        }
    }
?>