Skip to content

Commit

Permalink
add script for reports
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab committed Nov 23, 2023
1 parent 7cb9ae3 commit 472b7ce
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 40 deletions.
2 changes: 2 additions & 0 deletions conda/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ dependencies:
- pip
- makim 1.8.3
- containers-sugar 1.9.0
- jinja2
- requests
- pip:
- pygments
- compose-go>=1.23
1 change: 1 addition & 0 deletions scripts/reports/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output
8 changes: 8 additions & 0 deletions scripts/reports/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -ex

rm -f output/subscription_list.tex
python subscribers_list.py
cd output
pdflatex subscription_list.tex
54 changes: 54 additions & 0 deletions scripts/reports/subscribers_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import requests
import json
from jinja2 import Environment, FileSystemLoader

# Define the API endpoint URL
api_url = "http://localhost:8081/api/subscription/list.php"

# Make a GET request to the API
response = requests.get(api_url)

# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the JSON response
data = json.loads(response.text)

# Check if the API response contains data
if 'data' in data:
subscriptions = data['data']

# Create a Jinja2 environment
env = Environment(loader=FileSystemLoader('.'))

# Load the LaTeX template
template = env.get_template('subscription_template.tex')

for subscription in subscriptions:
image_url = subscription["qr"]
# response = requests.get(image_url)

tex_file_path = f"images/{image_url.split('/')[-1]}"
local_file_path = f"./output/{tex_file_path}"
# image_data = response.content

subscription["qr_filename"] = f"./{tex_file_path}"

# Save the image to the local file
# with open(local_file_path, 'wb') as file:
# file.write(image_data)


# Render the LaTeX document with data
rendered_latex = template.render(subscriptions=subscriptions)

# Save the rendered LaTeX document to a .tex file
with open("output/subscription_list.tex", "w") as tex_file:
tex_file.write(rendered_latex)

print("LaTeX document created successfully.")
else:
print("No subscription data found in the API response.")
exit(0)
else:
print("Failed to fetch data from the API. Status code:", response.status_code)
exit(0)
38 changes: 38 additions & 0 deletions scripts/reports/subscription_template.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
\documentclass[a4paper]{article}

% Include the geometry package to set page layout
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{longtable} % To support multi-page tables
\usepackage{geometry} % To adjust page margins
\usepackage[utf8]{inputenc}
\usepackage{booktabs,multirow,array}

% Set custom page margins
\geometry{left=0.5cm, right=0.5cm, top=0.5cm, bottom=0.5cm}

\begin{document}
\title{Subscription List}

\begin{longtable}{|c|c|}
\hline
\textbf{Personal Information} & \textbf{QR Code} \\
\hline
\endhead
{% for subscription in subscriptions -%}
\multirow{3}{*}{
\begin{tabular}{@{}c@{}}
\texttt{ {{- subscription["person"]["fullname"] -}} } \\
\texttt{ {{- subscription["person"]["email"].replace("_", "\\_").replace("%", "\\%") -}} } \\
\texttt{ {{- subscription["person"]["phone"] -}} }
\end{tabular} } &
\begin{tabular}{@{}c@{}}
\setlength{\fboxsep}{20pt} % Adjust the padding here
\fbox{ \includegraphics[width=2in]{ {{- subscription.qr_filename -}} } }
\end{tabular} \\
\hline
{% endfor %}
\end{longtable}
\end{document}
33 changes: 33 additions & 0 deletions src/api/subscription/list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require_once dirname(dirname(__DIR__)) . '/lib/subscription.php'; // Adjust the path as needed

// Set the response content type to JSON
header('Content-Type: application/json');

// Fetch subscription data using Subscription::list()
try {
$subscriptions = Subscription::list(["active" => 1]);

if ($subscriptions === null) {
$response = [
'success' => true,
'message' => 'No subscriptions found',
'data' => []
];
} else {
$response = [
'success' => true,
'message' => 'Subscriptions retrieved successfully',
'data' => $subscriptions
];
}
} catch (Exception $e) {
// Handle any exceptions or errors
$response = [
'success' => false,
'message' => 'Error retrieving subscriptions: ' . $e->getMessage()
];
}

// Encode the response array as JSON and print it
echo json_encode($response);
2 changes: 1 addition & 1 deletion src/lib/attendance.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function list(): array {
$db = get_db();

// Perform a query to retrieve all attendance records
$query = "SELECT * FROM attendance";
$query = "SELECT * FROM attendance ORDER BY event_session_id, log_time";
$result = $db->query($query);

$attendanceList = [];
Expand Down
10 changes: 8 additions & 2 deletions src/lib/subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public static function get(array $data): ?Subscription {
*/
public static function list(?array $filter=null): ?array {
$db = get_db();
$query = "SELECT * FROM subscription WHERE 1=1";
$query = "
SELECT subscription.*
FROM subscription INNER JOIN person
ON (subscription.person_id=person.id)
WHERE 1=1
";

if ($filter) {
// Iterate over the data dictionary
Expand All @@ -80,10 +85,11 @@ public static function list(?array $filter=null): ?array {
$escapedValue = $db->escapeString($value);

// Add the key-value pair to the WHERE clause
$query .= " AND $key='$escapedValue'";
$query .= " AND subscription.{$key}='$escapedValue'";
}
}

$query .= " ORDER BY person.fullname";
$result = $db->query($query);

$subscription_list = [];
Expand Down
73 changes: 36 additions & 37 deletions src/templates/attendance/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,44 @@

global $BASE_URL;
?>
<!DOCTYPE html>
<html>
<head>
<title>Lista de asistencia</title>
<link rel="stylesheet" href="<?php echo $BASE_URL; ?>/static/style.css">
</head>
<body>
<h2>Attendance Log</h2>

<a href="../" class="btn btn-warning my-3">Back to the menu</a>
<a href="./form.php" class="btn btn-primary my-3">Log</a>

<?php
$result = Attendance::list();

if ($result): ?>
<table>

<h2>Attendance Log</h2>

<a href="../" class="btn btn-warning my-3">Back to the menu</a>
<a href="./form.php" class="btn btn-primary my-3">Log</a>

<?php
$result = Attendance::list();

if ($result): ?>

<table class="w-100">
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Email</th>
<th>Phone</th>
<th>Event</th>
<th>Session</th>
<th>Log Time</th>
</tr>
<?php foreach ($result as $attendance): ?>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Email</th>
<th>Phone</th>
<th>Log Time</th>
<td><?php echo $attendance->id; ?></td>
<td><?php echo $attendance->person->fullname; ?></td>
<td><?php echo $attendance->person->email; ?></td>
<td><?php echo $attendance->person->phone; ?></td>
<td><?php echo $attendance->eventSession->event->name; ?></td>
<td><?php echo $attendance->eventSession->name; ?></td>
<td><?php echo $attendance->logTime->format('Y-m-d H:i:s'); ?></td>
</tr>
<?php foreach ($result as $attendance): ?>
<tr>
<td><?php echo $attendance->id; ?></td>
<td><?php echo $attendance->person->fullname; ?></td>
<td><?php echo $attendance->person->email; ?></td>
<td><?php echo $attendance->person->phone; ?></td>
<td><?php echo $attendance->logTime->format('Y-m-d H:i:s'); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<p>No records found.</p>
<?php endif; ?>

<a href="../" class="btn btn-warning my-3">Back to the menu</a>
<?php endforeach; ?>
</table>
<?php else: ?>
<p>No records found.</p>
<?php endif; ?>

<a href="../" class="btn btn-warning my-3">Back to the menu</a>

<?php
include dirname(__DIR__) . "/footer.php";
Expand Down

0 comments on commit 472b7ce

Please sign in to comment.