<?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 cost,maincategorie,subcategorie, datum FROM ".$table." ".$whereDatum." GROUP BY datum ASC";
        $db->query($query);
        //echo $query;
        $result = $db->result();
        if($result > ""){
            $options="";//print_r($result);
            foreach ($result as $key => $item)
            {      if($key == 0){$akt=($item['cost'])*(-1);} else{$akt=$akt+($item['cost'])*(-1);} 
                $json[] = array(
                            "date" => $item['datum'],
                            "value" => $akt,"info" => $item['cost']."<br>".$item['maincategorie']."<br>".$item['subcategorie'],
                          );
            }
            echo json_encode($json, JSON_UNESCAPED_UNICODE);
        } else {
            echo $query;
        }
    }
?>