Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions other/alarms.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,17 @@ function insert_new(){
"trying to insert the new alarm: " . $db->errorInfo(),
$alarm=true);
}

// If this is a purchase reminder, insert first dateplot row
if (str_contains($data["description"], "[purchase_reminders]")){
$query = "INSERT INTO dateplots_purchase_reminders (type, value) values (:i, :v)";
$stmt = $db->prepare($query);
$stmt->bindValue(':i', $latest_id);
$stmt->bindValue(':v', 0);
$stmt->execute();
}


return true;
}

Expand Down
93 changes: 93 additions & 0 deletions other/order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
include("../common_functions_v2.php");
echo(html_header());

$id = intval($_GET["id"] ?? 0);
$action = trim(strtolower(htmlspecialchars($_GET["action"] ?? '')));

$db = std_db($user='alarm_user');


/** Produces the HTML for the existing purchase_reminders */
function existing_purchase_reminders(){
global $db;

# Get the alarms
$query = "SELECT id, description FROM alarm WHERE description like \"[purchase_reminders]%\"";
$result = $db->query($query);

# Start the table
echo("<div style=\"width:45%;float:left\">");
echo("<h1><a id=\"existing\"></a>Existing ordering items</h1>\n");
echo("<table border=\"1\" class=\"nicetable\">\n");
echo("\n<tr>\n");
echo("<th>ID</th>\n<th>Description</th>\n<th>Value</th>\n<th colspan=3>Actions</th>\n");
echo("</tr>");

# Loop over alarms
while($row = $result->fetch()) {

$query = "select unix_timestamp(time), value from dateplots_purchase_reminders where type=" . $row[0] . " order by id desc limit 1";
// $latest_time = single_sql_value($db, $query, 0); Not really needed for now
$latest_value = single_sql_value($db, $query, 1);

echo("\n\n<tr>\n");
echo("<td>{$row[0]}</td>\n");
echo("<td>{$row[1]}</td>\n");
echo("<td>$latest_value</td>\n");
echo("<td><a href=\"order.php?id=" . $row[0] . "&action=qr\">QR</a></td>\n");
echo("<td><a href=\"order.php?id=" . $row[0] . "&action=reset\">Reset</a></td>\n");
echo("<td><a href=\"order.php?id=" . $row[0] . "\">Order</a></td>\n");
echo("</tr>");
}

# End the table
echo("\n\n</table>\n");
echo("</div>\n");
}

function update_value_in_purchase_reminders($value){
global $db;
global $id;
$query = "INSERT INTO dateplots_purchase_reminders (type, value) values (:i, :v)";
$stmt = $db->prepare($query);
$stmt->bindValue(':i', $id);
$stmt->bindValue(':v', $value);
$stmt->execute();
}

// Handle the given action

if (($action === '') && ($id > 0)){
$query = "select unix_timestamp(time), value from dateplots_purchase_reminders where type=" . $id . " order by id desc limit 1";
// $latest_time = single_sql_value($db, $query, 0); Not really needed for now
$latest_value = single_sql_value($db, $query, 1);
update_value_in_purchase_reminders($latest_value + 1);
echo("<h1>Thank you for your notifying</h1>\n");
}

if ($action === 'reset'){
update_value_in_purchase_reminders(0);
echo("<h1>Thank you for ordering supplies</h1>\n");
}

if ($action=='qr'){
ob_start();
$url = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$command = './qr_generator.py ' . $id . ' ' . $url;
passthru($command, $return_code);
$content_grabbed=ob_get_contents();
ob_end_clean();
echo($content_grabbed);

echo("<h1>QR for item {$id}</h1>\n");
echo("<img src=\"../figures/qr_{$id}.png\">\n");
}

// Print the overview
existing_purchase_reminders();

echo("\n\n\n");
echo(html_footer());

?>
30 changes: 30 additions & 0 deletions other/qr_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/python3
import qrcode
import argparse

from pathlib import Path


def make_qr(order_type: int, url: str):
qr = qrcode.QRCode(
version=10,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=20,
border=4,
)
url = 'http://' + url + '?id={}'
url = url.format(order_type)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
p = Path.cwd()

img.save(p.parents[0] / 'figures' / 'qr_{}.png'.format(order_type))


if __name__ == '__main__':
parser = argparse.ArgumentParser('QR generator for cinfdata ordering system')
parser.add_argument('id', type=int)
parser.add_argument('url', type=str)
args = parser.parse_args()
make_qr(args.id, args.url)