<?php

include('config.php');
include('function.php');

date_default_timezone_set('UTC');

define('GX_PATH', realpath(__DIR__.'/'));
define('GX_LIB', GX_PATH.'/lib/');

require 'autoload.php';
new Db();
// $login = login();

// var_dump($login);
$today = isset($_GET['date']) ? $_GET['date']: date("Y-m-d", strtotime("Yesterday"));
$auth = authToken();
$data = array(
    'start_date' => urlencode($today),
    'end_date' => urlencode($today),
    'timezone' => "+00:00",
    "date_segment" => "day",
    "include_archived" => 0,
    "limit" => 500
);
$res = APIFetch('/statistics/sm', $auth['access_token'], http_build_query($data), "GET");
$d = $res['data'];
print_r($d);

Db::delete(array(
    'table' => 'payment',
    'where' => array(
        'date' => $today
    )
));
$c = count($d);
echo $c;
if($c > 1) {
    for ( $i = 0; $i < $c; $i++) {
        addPayment($d[$i], $today);
    }
} else {
    exit;
}