$objPHPExcel->getActiveSheet()->getStyle("C" . $row . ":D" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $rnd = STRING::Random(10);
            $bfn = 'report_' . $date_created_fn . '.xlsx';
            $fn = 'reports/' . $bfn;
            $objWriter->save($fn);
            header('Content-disposition: filename="' . $bfn . '"');
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            echo file_get_contents($fn);
        } else {
            header("Access-Control-Allow-Origin: *");
            header('Content-Type: application/json');
            echo json_encode($ret);
        }
    } else {
        header("Access-Control-Allow-Origin: *");
        header('Content-Type: application/json');
        echo json_encode($ret);
    }
} else {
    $courts = Court::get_all();
    $smarty->assign('courts', $courts);
    if ($_GET['did']) {
        $smarty->assign('default_user', $_GET['did']);
    }
    $from_date = date('Y-m-01');
    $to_date = date('Y-m-d');
    $smarty->assign('from_date', $from_date);
    $smarty->assign('to_date', $to_date);
    $smarty->display('admin-report.tpl');
}
    public static function get_day_timeunits($date, $user = null)
    {
        global $DB;
        $courts = Court::get_all();
        $sql = '
				SELECT
					*
				FROM timeunits
				WHERE
					date=:date
				ORDER BY `date`, `from`
			';
        $sth = $DB->prepare($sql);
        $sth->bindParam(':date', $date, PDO::PARAM_STR);
        $sth->execute();
        $timeunits = array();
        foreach ($sth->fetchAll() as $tmp) {
            $timeunit = new TimeUnit($tmp, false);
            $timeunit->user = $user;
            if (!is_array($timeunits[$timeunit->court_id])) {
                $timeunits[$timeunit->court_id] = array();
            }
            $timeunits[$timeunit->court_id][] = $timeunit;
        }
        $ret = array();
        $ret['date'] = $date;
        $ret['fields'] = array();
        $i = 0;
        foreach ($courts as $court) {
            $j = 0;
            $ret['fields'][$i] = array();
            $ret['fields'][$i]['id'] = $court->id;
            $ret['fields'][$i]['title'] = $court->name;
            $ret['fields'][$i]['subtitle'] = $court->subtitle;
            $ret['fields'][$i]['intervals'] = array();
            if (is_array($timeunits[$court->id])) {
                foreach ($timeunits[$court->id] as $timeunit) {
                    $ret['fields'][$i]['intervals'][$j] = $timeunit->get_array();
                    $j++;
                }
            }
            $i++;
        }
        return $ret;
    }