<?php

// Expenses visualisation //
header('Content-Type: application/json');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    exit(json_encode(['error' => 'please login']));
}
switch ($_POST['do']) {
    case 'addNew':
        $dat = [];
        if ($id = $EXP->parseInsert($_POST['date'] . ' ' . $_POST['str'])) {
            //$dat['post']=$_POST;
            $dat['id'] = $id * 1;
            exit(json_encode($dat));
        }
        exit(json_encode($_POST));
    case 'save':
        //exit(json_encode($_POST));
        $dat = [];
        $updated = $EXP->update($_POST['ed_id'], $_POST);
        if ($updated) {
            $dat['msg'] = 'updated';
        } else {
            $dat['error'] = 'NOT updated';
        }
        exit(json_encode($dat));
    case 'expenseByDay':
        // on the same day
<?php

// admin :: Calendar controller
header('Content-Type: application/json');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    exit(json_encode(['error' => 'please login']));
}
//exit(json_encode($_GET));// test
switch ($_GET['do']) {
    case 'list':
        // list all user sessions
        //$user_id=$_POST['user_id'];
        //$sessions=$edxApp->sessions([$user_id])[$user_id];
        $from = date("Y-m-d", $_GET['start']);
        $to = date("Y-m-d", $_GET['end']);
        $sql = "SELECT ed_id, ed_name as title, ed_date as start FROM expenses_data WHERE ed_date>='{$from}' AND ed_date<='{$to}';";
        $q = $EXP->db()->query($sql) or die("Error:sql");
        $dat = [];
        while ($r = $q->fetch(PDO::FETCH_ASSOC)) {
            $dat[] = $r;
        }
        //exit(json_encode($_GET));// test
        exit(json_encode($dat));
        //print_r($sessions);
        break;
    default:
        //die("Error : unknow action ".$_POST['do']);
        exit(json_encode($_GET));
<?php

// Expenses visualisation //
header('Content-Type: text/html; charset=utf-8');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$admin = new LTE\AdminLte2();
$admin->title("Home");
echo $admin;
//
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    header("location:../login/");
    die("Error:!user_id");
}
$count = $EXP->count();
?>
<section class="content-header">
  <h1>Home 
  	<small><?php 
echo number_format($count);
?>
 records</small>
  </h1>
</section>


<div class="content">
    
    <div class="col-md-6">
	<?php 
<?php

// Expenses visualisation //
// Search expense
header('Content-Type: text/html; charset=utf-8');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$admin = new LTE\AdminLte2();
$admin->title("Search");
echo $admin;
//
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    header("location:../login/logout.php");
    die("Error:!user_id");
}
?>
<section class="content-header">
  <h1>Search</h1>
</section>

<section class="content">
	
    <div class="row">
        <div class='col-md-12'>
      	<?php 
// Search form //
include "box_search.php";
// search results //
$box = new LTE\Box();
$box->id("boxResult");
<?php

// Expenses visualisation //
// new expense
header('Content-Type: text/html; charset=utf-8');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$admin = new LTE\AdminLte2();
$admin->title("New");
echo $admin;
//
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    header("location:../login/logout.php");
    die("Error:!user_id");
}
?>

<section class="content-header">
  <h1><?php 
echo $EXP->user()['email'];
?>
</h1>
</section>

<section class="content">
	<div class="row">
  	<div class='col-md-6'>
	<?php 
$box = new LTE\Box();
$box->id("boxNew");
<?php

// Expenses visualisation //
header('Content-Type: application/json');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    exit(json_encode(['error' => 'please log in']));
}
switch ($_POST['do']) {
    case 'search':
        $WHERE = [];
        $WHERE[] = "ed_uid=" . $EXP->user_id();
        if ($_POST['str']) {
            $WHERE[] = "ed_name LIKE " . $EXP->db()->quote('%' . $_POST['str'] . '%');
        }
        if ($_POST['minmax']) {
            $min = $max = 0;
            if (preg_match("/<=?([0-9]+)/", $_POST['minmax'], $o)) {
                $max = $o[1] * 1;
            }
            if (preg_match("/>=?([0-9]+)/", $_POST['minmax'], $o)) {
                $min = $o[1] * 1;
            }
            if ($min > 0) {
                $WHERE[] = "ed_value >= " . $min;
            }
            if ($max > 0) {
                $WHERE[] = "ed_value <= " . $max;
            }
<?php

//Download expense data as CSV
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    header("location:../login/logout.php");
    die("Error:!user_id");
}
$sql = "SELECT ed_name, ed_value, ed_currency, ed_date FROM expenses_data ";
$sql .= "WHERE ed_uid=" . $EXP->user_id() . " ORDER BY ed_date;";
$q = $EXP->db()->query($sql) or die("Error {$sql}");
$dat = [];
while ($r = $q->fetch(PDO::FETCH_ASSOC)) {
    $dat[] = $r;
}
/*
function maybeEncodeCSVField($string) {
    if(strpos($string, ',') !== false || strpos($string, '"') !== false || strpos($string, "\n") !== false) {
        $string = '"' . str_replace('"', '""', $string) . '"';
    }
    return $string;
}
*/
//echo "<pre>";
//print_r($dat);
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=expenses.csv");
header("Pragma: no-cache");
header("Expires: 0");
<?php

// Expenses visualisation //
header('Content-Type: text/html; charset=utf-8');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$admin = new LTE\AdminLte2();
$admin->title("Edit");
echo $admin;
//
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    header("location:../login/logout.php");
    die("Error:!user_id");
}
$id = 0;
if (isset($_GET['id'])) {
    $id = $_GET['id'] * 1;
}
if (isset($_GET['date'])) {
    $x = $EXP->expenseByDay($_GET['date']);
    //array_pop($x)
    if (count($x)) {
        $id = $x[0]['ed_id'];
    }
    //print_r($x[0]);exit;
    //$id=$EXP->idByDate($_GET['date']);
}
if (!$id) {
    echo "<pre>Error</pre>";
    exit("<script>document.location.href='../search/';</script>");
<?php

// Expenses visualisation //
header('Content-Type: application/json');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    exit(json_encode(['error' => 'please log in']));
}
switch ($_POST['do']) {
    case 'today':
        $today = date("Y-m-d");
        $sql = "SELECT * FROM expenses_data WHERE ed_uid=" . $EXP->user_id() . " AND ed_date LIKE '{$today}%' ORDER BY ed_date;";
        $q = $EXP->db()->query($sql) or die;
        //echo $sql;exit;
        $dat = [];
        while ($r = $q->fetch(PDO::FETCH_ASSOC)) {
            $r['date'] = explode(" ", $r['ed_date'])[0];
            $r['time'] = explode(" ", $r['ed_date'])[1];
            $r['time'] = substr($r['time'], 0, 5);
            $dat[] = $r;
        }
        exit(json_encode($dat));
    case 'savenew':
        //print_r($_POST);exit;
        $dat = [];
        if ($id = $EXP->parseInsert($_POST['str'])) {
            $dat['inserted'] = $id;
        }
        exit(json_encode($dat));
<?php

// Expenses visualisation //
// new expense
header('Content-Type: application/json');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    exit(json_encode(['error' => 'please login']));
}
switch ($_POST['do']) {
    case 'save':
        //print_r($_POST);
        $dat = [];
        if ($id = $EXP->newExpense($_POST)) {
            $dat['id'] = $id;
            exit(json_encode($dat));
        }
        $dat['error'] = "nope";
        exit(json_encode($dat));
        break;
    case 'getExpenses':
        //print_r($_POST);
        $dat = $EXP->expenseByDay($_POST['day']);
        foreach ($dat as $k => $v) {
            $dat[$k]['time'] = substr($v['time'], 0, 5);
        }
        exit(json_encode($dat));
        break;
    case 'delete':
<?php

// upload //
header('Content-Type: text/html; charset=utf-8');
session_start();
require __DIR__ . "/../../vendor/autoload.php";
$EXP = new Expenses\Expenses();
if (!$EXP->user_id()) {
    header("location:../login/logout.php");
    die("Error:!user_id");
}
//echo "<pre>";print_r($_FILES);exit;
if (!isset($_FILES['xlsfile'])) {
    die('error');
}
if (!count($_FILES)) {
    die('error 2');
}
//echo "<pre>_FILES=";	print_r($_FILES);echo "</pre>";
$ext = explode(".", $_FILES['xlsfile']['name']);
$ext = $ext[count($ext) - 1];
//https://docs.google.com/spreadsheets/d/1cbU5xSwxA8BUJF9L2ptLdYs0BubgJWd7vORc7TjqSXk/export?format=csv&id=1cbU5xSwxA8BUJF9L2ptLdYs0BubgJWd7vORc7TjqSXk&gid=0
//exit($ext);
$imported = 0;
switch ($ext) {
    case 'csv':
        $row = 0;
        $rows = [];
        if (($handle = fopen($_FILES['xlsfile']['tmp_name'], "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);