Add reports (close #7)
parent
db72d8d38d
commit
98aa7d3217
Binary file not shown.
@ -0,0 +1,13 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
CREATE TABLE IF NOT EXISTS `report_access_codes` (
|
||||
`id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||
`code` VARCHAR(45) NULL DEFAULT NULL,
|
||||
`expires` DATETIME NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE INDEX `id_UNIQUE` (`id` ASC))
|
||||
ENGINE = InnoDB
|
||||
DEFAULT CHARACTER SET = utf8
|
@ -0,0 +1,261 @@
|
||||
<?php
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
|
||||
// Detect if loaded by the user or by PHP
|
||||
if (count(get_included_files()) == 1) {
|
||||
define("LOADED", true);
|
||||
} else {
|
||||
define("LOADED", false);
|
||||
}
|
||||
|
||||
require_once __DIR__ . "/../required.php";
|
||||
|
||||
use League\Csv\Writer;
|
||||
use League\Csv\HTMLConverter;
|
||||
use odsPhpGenerator\ods;
|
||||
use odsPhpGenerator\odsTable;
|
||||
use odsPhpGenerator\odsTableRow;
|
||||
use odsPhpGenerator\odsTableColumn;
|
||||
use odsPhpGenerator\odsTableCellString;
|
||||
use odsPhpGenerator\odsStyleTableColumn;
|
||||
use odsPhpGenerator\odsStyleTableCell;
|
||||
|
||||
// Allow access with a download code, for mobile app and stuff
|
||||
$date = date("Y-m-d H:i:s");
|
||||
if (isset($VARS['code']) && LOADED) {
|
||||
if (!$database->has('report_access_codes', ["AND" => ['code' => $VARS['code'], 'expires[>]' => $date]])) {
|
||||
dieifnotloggedin();
|
||||
}
|
||||
} else {
|
||||
dieifnotloggedin();
|
||||
}
|
||||
|
||||
// Delete old DB entries
|
||||
$database->delete('report_access_codes', ['expires[<=]' => $date]);
|
||||
|
||||
if (LOADED) {
|
||||
if (isset($VARS['type']) && isset($VARS['format'])) {
|
||||
generateReport($VARS['type'], $VARS['format']);
|
||||
die();
|
||||
} else {
|
||||
lang("invalid parameters");
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a 2d array of the items in the database.
|
||||
* @global type $database
|
||||
* @param array $filter Medoo WHERE clause.
|
||||
* @return string
|
||||
*/
|
||||
function getItemReport($filter = []) {
|
||||
global $database;
|
||||
$items = $database->select(
|
||||
"items", [
|
||||
"[>]locations" => ["locid"],
|
||||
"[>]categories" => ["catid"]
|
||||
], [
|
||||
"itemid",
|
||||
"name",
|
||||
"catname",
|
||||
"locname",
|
||||
"code1",
|
||||
"code2",
|
||||
"qty",
|
||||
"want",
|
||||
"userid",
|
||||
"text1",
|
||||
"text2",
|
||||
"text3"
|
||||
], $filter
|
||||
);
|
||||
$header = [
|
||||
lang("itemid", false),
|
||||
lang("name", false),
|
||||
lang("category", false),
|
||||
lang("location", false),
|
||||
lang("code 1", false),
|
||||
lang("code 2", false),
|
||||
lang("quantity", false),
|
||||
lang("want", false),
|
||||
lang("assigned to", false),
|
||||
lang("description", false),
|
||||
lang("notes", false),
|
||||
lang("comments", false)
|
||||
];
|
||||
$out = [$header];
|
||||
for ($i = 0; $i < count($items); $i++) {
|
||||
$user = "";
|
||||
if (!is_null($items[$i]["userid"])) {
|
||||
require_once __DIR__ . "/userinfo.php";
|
||||
$u = getUserByID($items[$i]["userid"]);
|
||||
$user = $u['name'] . " (" . $u['username'] . ')';
|
||||
}
|
||||
$out[] = [
|
||||
$items[$i]["itemid"],
|
||||
$items[$i]["name"],
|
||||
$items[$i]["catname"],
|
||||
$items[$i]["locname"],
|
||||
$items[$i]["code1"],
|
||||
$items[$i]["code2"],
|
||||
$items[$i]["qty"],
|
||||
$items[$i]["want"],
|
||||
$user,
|
||||
$items[$i]["text1"],
|
||||
$items[$i]["text2"],
|
||||
$items[$i]["text3"]
|
||||
];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function getCategoryReport() {
|
||||
global $database;
|
||||
$cats = $database->select('categories', [
|
||||
'catid',
|
||||
'catname'
|
||||
]);
|
||||
$header = [lang("id", false), lang("category", false), lang("item count", false)];
|
||||
$out = [$header];
|
||||
for ($i = 0; $i < count($cats); $i++) {
|
||||
$itemcount = $database->count('items', ['catid' => $cats[$i]['catid']]);
|
||||
$out[] = [
|
||||
$cats[$i]["catid"],
|
||||
$cats[$i]["catname"],
|
||||
$itemcount . ""
|
||||
];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function getLocationReport() {
|
||||
global $database;
|
||||
$locs = $database->select('locations', [
|
||||
'locid',
|
||||
'locname',
|
||||
'loccode'
|
||||
]);
|
||||
$header = [lang("id", false), lang("location", false), lang("code", false), lang("item count", false)];
|
||||
$out = [$header];
|
||||
for ($i = 0; $i < count($locs); $i++) {
|
||||
$itemcount = $database->count('items', ['locid' => $locs[$i]['locid']]);
|
||||
$out[] = [
|
||||
$locs[$i]["locid"],
|
||||
$locs[$i]["locname"],
|
||||
$locs[$i]["loccode"],
|
||||
$itemcount . ""
|
||||
];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
function getReportData($type) {
|
||||
switch ($type) {
|
||||
case "item":
|
||||
return getItemReport();
|
||||
break;
|
||||
case "category":
|
||||
return getCategoryReport();
|
||||
break;
|
||||
case "location":
|
||||
return getLocationReport();
|
||||
break;
|
||||
case "itemstock":
|
||||
return getItemReport(["AND" => ["qty[<]want", "want[>]" => 0]]);
|
||||
break;
|
||||
default:
|
||||
return [["error"]];
|
||||
}
|
||||
}
|
||||
|
||||
function dataToCSV($data, $name = "report") {
|
||||
$csv = Writer::createFromString('');
|
||||
$csv->insertAll($data);
|
||||
header('Content-type: text/csv');
|
||||
header('Content-Disposition: attachment; filename="' . $name . "_" . date("Y-m-d_Hi") . ".csv" . '"');
|
||||
echo $csv;
|
||||
die();
|
||||
}
|
||||
|
||||
function dataToODS($data, $name = "report") {
|
||||
$ods = new ods();
|
||||
$styleColumn = new odsStyleTableColumn();
|
||||
$styleColumn->setUseOptimalColumnWidth(true);
|
||||
$headerstyle = new odsStyleTableCell();
|
||||
$headerstyle->setFontWeight("bold");
|
||||
$table = new odsTable($name);
|
||||
|
||||
for ($i = 0; $i < count($data[0]); $i++) {
|
||||
$table->addTableColumn(new odsTableColumn($styleColumn));
|
||||
}
|
||||
|
||||
$rowid = 0;
|
||||
foreach ($data as $datarow) {
|
||||
$row = new odsTableRow();
|
||||
foreach ($datarow as $cell) {
|
||||
if ($rowid == 0) {
|
||||
$row->addCell(new odsTableCellString($cell, $headerstyle));
|
||||
} else {
|
||||
$row->addCell(new odsTableCellString($cell));
|
||||
}
|
||||
}
|
||||
$table->addRow($row);
|
||||
$rowid++;
|
||||
}
|
||||
$ods->addTable($table);
|
||||
$ods->downloadOdsFile($name . "_" . date("Y-m-d_Hi") . ".ods");
|
||||
}
|
||||
|
||||
function dataToHTML($data, $name = "report") {
|
||||
global $SECURE_NONCE;
|
||||
// HTML exporter doesn't like null values
|
||||
for ($i = 0; $i < count($data); $i++) {
|
||||
for ($j = 0; $j < count($data[$i]); $j++) {
|
||||
if (is_null($data[$i][$j])) {
|
||||
$data[$i][$j] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
header('Content-type: text/html');
|
||||
$converter = new HTMLConverter();
|
||||
$out = "<!DOCTYPE html>\n"
|
||||
. "<meta charset=\"utf-8\">\n"
|
||||
. "<meta name=\"viewport\" content=\"width=device-width\">\n"
|
||||
. "<title>" . $name . "_" . date("Y-m-d_Hi") . "</title>\n"
|
||||
. <<<STYLE
|
||||
<style nonce="$SECURE_NONCE">
|
||||
.table-csv-data {
|
||||
border-collapse: collapse;
|
||||
}
|
||||
.table-csv-data tr:first-child {
|
||||
font-weight: bold;
|
||||
}
|
||||
.table-csv-data tr td {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
STYLE
|
||||
. $converter->convert($data);
|
||||
echo $out;
|
||||
}
|
||||
|
||||
function generateReport($type, $format) {
|
||||
$data = getReportData($type);
|
||||
switch ($format) {
|
||||
case "ods":
|
||||
dataToODS($data, $type);
|
||||
break;
|
||||
case "html":
|
||||
dataToHTML($data, $type);
|
||||
break;
|
||||
case "csv":
|
||||
default:
|
||||
echo dataToCSV($data, $type);
|
||||
break;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
require_once __DIR__ . '/../required.php';
|
||||
|
||||
redirectifnotloggedin();
|
||||
?>
|
||||
|
||||
<form action="lib/reports.php" method="GET" target="_BLANK">
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-sm-6">
|
||||
<label for="type"><?php lang("report type"); ?></label>
|
||||
<select name="type" class="form-control" required>
|
||||
<option value="item"><?php lang("items") ?></option>
|
||||
<option value="category"><?php lang("categories") ?></option>
|
||||
<option value="location"><?php lang("locations") ?></option>
|
||||
<option value="itemstock"><?php lang("understocked items") ?></option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-xs-12 col-sm-6">
|
||||
<label for="type"><?php lang("format"); ?></label>
|
||||
<select name="format" class="form-control" required>
|
||||
<option value="csv"><?php lang("csv file") ?></option>
|
||||
<option value="ods"><?php lang("ods file") ?></option>
|
||||
<option value="html"><?php lang("html file") ?></option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<br />
|
||||
<?php
|
||||
$code = uniqid(rand(10000000,99999999), true);
|
||||
$database->insert('report_access_codes', ['code' => $code, 'expires' => date("Y-m-d H:i:s", strtotime("+5 minutes"))]);
|
||||
?>
|
||||
<input type="hidden" name="code" value="<?php echo $code; ?>" />
|
||||
|
||||
<button type="submit" class="btn btn-success" id="genrptbtn"><i class="fa fa-download"></i> <?php lang("generate report"); ?></button>
|
||||
</form>
|
@ -0,0 +1,9 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
$("#genrptbtn").click(function () {
|
||||
setTimeout(function () {
|
||||
window.location.reload();
|
||||
}, 1000)
|
||||
});
|
Loading…
Reference in New Issue