Flask App for Database Queries
Flask App for Database Queries
import psycopg2
from psycopg2 import OperationalError
import pandas as pd
import os
app = Flask(__name__)
@[Link]('/')
def home():
return render_template_string('''
<!DOCTYPE html>
<html>
<head>
<title>Consultas Base de Datos</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
text-align: center;
}
h1 {
font-size: 2em;
font-weight: bold;
}
form {
margin-bottom: 20px;
}
table {
margin: 0 auto;
border-collapse: collapse;
width: 80%;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
#resultSection, #verificationSection, #applicationSection {
margin-top: 20px;
display: none;
}
#toggleButton, #toggleVerification, #toggleApplication {
font-weight: bold;
cursor: pointer;
}
#verificationResult, #applicationResult {
margin-top: 10px;
font-weight: bold;
}
.form-group {
margin-bottom: 15px;
}
input[type="text"], input[type="email"] {
padding: 8px;
width: calc(100% - 16px);
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>CONSULTAS BASE DE DATOS</h1>
<form method="post" action="/query">
<label for="curp">Cédula:</label>
<input type="text" id="curp" name="curp" required>
<button type="submit">Consultar</button>
</form>
<div id="resultSection">
<div id="toggleButton" onclick="toggleTable()">Resultado
consulta</div>
<div id="tableContainer" style="display: none;">
{% if data %}
<table>
<tr>
<th>Cedula</th>
<th>Nombre alumno</th>
<th>Tipo_concepto</th>
<th>Fecha_vencimiento</th>
<th>Colegio</th>
<th>Estado</th>
<th>Identificador invoice</th>
<th>Cantidad inicial</th>
<th>Cantidad pendiente</th>
<th>Identificador recibo</th>
<th>Factura</th>
<th>Factura 2</th>
<th>Identificador pago</th>
<th>Cantidad recibo</th>
<th>Referencia pago 1</th>
<th>Referencia pago 2</th>
<th>Monto pago</th>
<th>Mes pago</th>
<th>Entidad pago</th>
</tr>
{% for row in data %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</div>
<div id="verificationSection">
<div id="toggleVerification" onclick="toggleVerification()">Verificación
de pago</div>
<div id="verificationContainer" style="display: none;">
<form method="post" action="/verify">
<label for="payment_id">Payment ID:</label>
<input type="text" id="payment_id" name="payment_id"
required>
<button type="submit">Verificar</button>
</form>
<div id="verificationResult">{{ verification_message }}</div>
</div>
</div>
<div id="applicationSection">
<div id="toggleApplication" onclick="toggleApplication()">Aplicación
de pago</div>
<div id="applicationContainer" style="display: none;">
<form method="post" action="/apply">
<div class="form-group">
<label for="email">Correo:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="cedula">Cédula:</label>
<input type="text" id="cedula" name="cedula" required>
</div>
<div class="form-group">
<label for="payment_id_apply">Pago a aplicar:</label>
<input type="text" id="payment_id_apply"
name="payment_id_apply" required>
</div>
<div class="form-group">
<label for="concepto1">Concepto 1:</label>
<input type="text" id="concepto1" name="concepto1">
</div>
<div class="form-group">
<label for="concepto2">Concepto 2:</label>
<input type="text" id="concepto2" name="concepto2">
</div>
<div class="form-group">
<label for="concepto3">Concepto 3:</label>
<input type="text" id="concepto3" name="concepto3">
</div>
<div class="form-group">
<label for="concepto4">Concepto 4:</label>
<input type="text" id="concepto4" name="concepto4">
</div>
<div class="form-group">
<label for="concepto5">Concepto 5:</label>
<input type="text" id="concepto5" name="concepto5">
</div>
<div class="form-group">
<label for="concepto6">Concepto 6:</label>
<input type="text" id="concepto6" name="concepto6">
</div>
<button type="submit">Aplicar</button>
</form>
<div id="applicationResult">{{ application_message }}</div>
</div>
</div>
<script>
function toggleTable() {
var tableContainer = [Link]('tableContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
function toggleVerification() {
var verificationContainer =
[Link]('verificationContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
function toggleApplication() {
var applicationContainer =
[Link]('applicationContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
@[Link]('/query', methods=['POST'])
def query():
curp = [Link]['curp']
query = f"""
SELECT
[Link] AS cedula_alumno,
s.full_name AS nombre_alumno,
CASE
WHEN i.concept_type = 'membership' THEN 'pension'
WHEN i.concept_type = 'complement' THEN 'complemento'
WHEN i.concept_type = 'inscription' THEN 'matricula'
ELSE i.concept_type
END AS tipo_concepto,
i.due_date AS fecha_vencimiento,
[Link] AS colegio,
CASE
WHEN [Link] = 'successful' then 'Saldado'
WHEN [Link] ='expired' then 'Vencido'
WHEN [Link] = 'deleted' then 'Eliminado'
WHEN [Link] ='cancelled' then 'Cancelado'
WHEN [Link] ='pending' then 'Pendiente'
END as estado,
[Link],
i.initial_amount AS cantidad_inicial,
i.pending_amount AS cantidad_pendiente,
COALESCE([Link]::TEXT, 'Sin info') AS identificador_recibo,
COALESCE(r.reference_1, 'Sin info') AS factura,
COALESCE(r.cfdi_uuid::TEXT, 'Sin info') AS factura_ecuador,
COALESCE([Link]::TEXT, 'Sin info') AS identificador_pago,
COALESCE([Link], 0) AS cantidad_recibo,
COALESCE(p.transaction_reference, 'Sin info') AS referencia_pago,
COALESCE(p.ticket_number, 'Sin info') AS referencia_pago_2,
COALESCE([Link], 0) AS monto_pago,
COALESCE(TO_CHAR(p.payment_date, 'TMMonth'),'Sin info') AS mes_pago,
COALESCE([Link], 'Sin info') AS entidad_pago
FROM
invoices i
LEFT JOIN
students s ON i.student_id = [Link]
LEFT JOIN
receipts r ON r.invoice_id = [Link]
LEFT JOIN
payments p ON [Link] = r.payment_id
JOIN
campuses c ON i.campus_id = [Link]
WHERE
[Link] IN ('{curp}')
ORDER BY
due_date;
"""
cursor = [Link]()
[Link](query)
result = [Link]()
[Link]()
if not result:
return render_template_string('''
<h1>LA CÉDULA NO EXISTE EN NUESTRA BASE DE DATOS, VERIFICAR O
PUEDE NO TENER CONCEPTOS.</h1>
<a href="/">Volver</a>
''')
return render_template_string('''
<!DOCTYPE html>
<html>
<head>
<title>Consultas Base de Datos</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
text-align: center;
}
h1 {
font-size: 2em;
font-weight: bold;
}
form {
margin-bottom: 20px;
}
table {
margin: 0 auto;
border-collapse: collapse;
width: 80%;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
#resultSection, #verificationSection, #applicationSection {
margin-top: 20px;
}
#toggleButton, #toggleVerification, #toggleApplication {
font-weight: bold;
cursor: pointer;
}
#verificationResult, #applicationResult {
margin-top: 10px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>CONSULTAS BASE DE DATOS</h1>
<form method="post" action="/query">
<label for="curp">Cédula:</label>
<input type="text" id="curp" name="curp" required>
<button type="submit">Consultar</button>
</form>
<div id="resultSection">
<div id="toggleButton" onclick="toggleTable()">Resultado
consulta</div>
<div id="tableContainer" style="display: none;">
{% if data %}
<table>
<tr>
<th>Cedula</th>
<th>Nombre alumno</th>
<th>Tipo_concepto</th>
<th>Fecha_vencimiento</th>
<th>Colegio</th>
<th>Estado</th>
<th>Identificador invoice</th>
<th>Cantidad inicial</th>
<th>Cantidad pendiente</th>
<th>Identificador recibo</th>
<th>Factura</th>
<th>Factura 2</th>
<th>Identificador pago</th>
<th>Cantidad recibo</th>
<th>Referencia pago 1</th>
<th>Referencia pago 2</th>
<th>Monto pago</th>
<th>Mes pago</th>
<th>Entidad pago</th>
</tr>
{% for row in data %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</div>
<div id="verificationSection">
<div id="toggleVerification" onclick="toggleVerification()">Verificación
de pago</div>
<div id="verificationContainer" style="display: none;">
<form method="post" action="/verify">
<label for="payment_id">Payment ID:</label>
<input type="text" id="payment_id" name="payment_id"
required>
<button type="submit">Verificar</button>
</form>
<div id="verificationResult">{{ verification_message }}</div>
</div>
</div>
<div id="applicationSection">
<div id="toggleApplication" onclick="toggleApplication()">Aplicación
de pago</div>
<div id="applicationContainer" style="display: none;">
<form method="post" action="/apply">
<div class="form-group">
<label for="email">Correo:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="cedula">Cédula:</label>
<input type="text" id="cedula" name="cedula" required>
</div>
<div class="form-group">
<label for="payment_id_apply">Pago a aplicar:</label>
<input type="text" id="payment_id_apply"
name="payment_id_apply" required>
</div>
<div class="form-group">
<label for="concepto1">Concepto 1:</label>
<input type="text" id="concepto1" name="concepto1">
</div>
<div class="form-group">
<label for="concepto2">Concepto 2:</label>
<input type="text" id="concepto2" name="concepto2">
</div>
<div class="form-group">
<label for="concepto3">Concepto 3:</label>
<input type="text" id="concepto3" name="concepto3">
</div>
<div class="form-group">
<label for="concepto4">Concepto 4:</label>
<input type="text" id="concepto4" name="concepto4">
</div>
<div class="form-group">
<label for="concepto5">Concepto 5:</label>
<input type="text" id="concepto5" name="concepto5">
</div>
<div class="form-group">
<label for="concepto6">Concepto 6:</label>
<input type="text" id="concepto6" name="concepto6">
</div>
<button type="submit">Aplicar</button>
</form>
<div id="applicationResult">{{ application_message }}</div>
</div>
</div>
<script>
function toggleTable() {
var tableContainer = [Link]('tableContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
function toggleVerification() {
var verificationContainer =
[Link]('verificationContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
function toggleApplication() {
var applicationContainer =
[Link]('applicationContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
@[Link]('/verify', methods=['POST'])
def verify_payment():
payment_id = [Link]['payment_id']
verification_query = f"""
SELECT estado FROM co_ecu_ops.not_applied_pichincha
WHERE payment_id = '{payment_id}';
"""
cursor = connection_matti.cursor()
[Link](verification_query)
verification_result = [Link]()
[Link]()
if verification_result:
estado = verification_result[0]
if estado:
verification_message = "El pago efectivamente no se encuentra
conciliado"
else:
verification_message = "El pago ya se aplicó"
else:
verification_message = "Es muy probable que este pago ya esté
conciliado, consultar con L2."
return render_template_string('''
<!DOCTYPE html>
<html>
<head>
<title>Consultas Base de Datos</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
text-align: center;
}
h1 {
font-size: 2em;
font-weight: bold;
}
form {
margin-bottom: 20px;
}
table {
margin: 0 auto;
border-collapse: collapse;
width: 80%;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
#resultSection, #verificationSection, #applicationSection {
margin-top: 20px;
}
#toggleButton, #toggleVerification, #toggleApplication {
font-weight: bold;
cursor: pointer;
}
#verificationResult, #applicationResult {
margin-top: 10px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>CONSULTAS BASE DE DATOS</h1>
<form method="post" action="/query">
<label for="curp">Cédula:</label>
<input type="text" id="curp" name="curp" required>
<button type="submit">Consultar</button>
</form>
<div id="resultSection">
<div id="toggleButton" onclick="toggleTable()">Resultado
consulta</div>
<div id="tableContainer" style="display: none;">
{% if data %}
<table>
<tr>
<th>Cedula</th>
<th>Nombre alumno</th>
<th>Tipo_concepto</th>
<th>Fecha_vencimiento</th>
<th>Colegio</th>
<th>Estado</th>
<th>Identificador invoice</th>
<th>Cantidad inicial</th>
<th>Cantidad pendiente</th>
<th>Identificador recibo</th>
<th>Factura</th>
<th>Factura 2</th>
<th>Identificador pago</th>
<th>Cantidad recibo</th>
<th>Referencia pago 1</th>
<th>Referencia pago 2</th>
<th>Monto pago</th>
<th>Mes pago</th>
<th>Entidad pago</th>
</tr>
{% for row in data %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</div>
<div id="verificationSection">
<div id="toggleVerification" onclick="toggleVerification()">Verificación
de pago</div>
<div id="verificationContainer" style="display: none;">
<form method="post" action="/verify">
<label for="payment_id">Payment ID:</label>
<input type="text" id="payment_id" name="payment_id"
required>
<button type="submit">Verificar</button>
</form>
<div id="verificationResult">{{ verification_message }}</div>
</div>
</div>
<div id="applicationSection">
<div id="toggleApplication" onclick="toggleApplication()">Aplicación
de pago</div>
<div id="applicationContainer" style="display: none;">
<form method="post" action="/apply">
<div class="form-group">
<label for="email">Correo:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="payment_id_apply">Pago a aplicar:</label>
<input type="text" id="payment_id_apply"
name="payment_id_apply" required>
</div>
<div class="form-group">
<label for="concepto1">Concepto 1:</label>
<input type="text" id="concepto1" name="concepto1">
</div>
<div class="form-group">
<label for="concepto2">Concepto 2:</label>
<input type="text" id="concepto2" name="concepto2">
</div>
<div class="form-group">
<label for="concepto3">Concepto 3:</label>
<input type="text" id="concepto3" name="concepto3">
</div>
<div class="form-group">
<label for="concepto4">Concepto 4:</label>
<input type="text" id="concepto4" name="concepto4">
</div>
<div class="form-group">
<label for="concepto5">Concepto 5:</label>
<input type="text" id="concepto5" name="concepto5">
</div>
<div class="form-group">
<label for="concepto6">Concepto 6:</label>
<input type="text" id="concepto6" name="concepto6">
</div>
<button type="submit">Aplicar</button>
</form>
<div id="applicationResult">{{ application_message }}</div>
</div>
</div>
<script>
function toggleTable() {
var tableContainer = [Link]('tableContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
function toggleVerification() {
var verificationContainer =
[Link]('verificationContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
function toggleApplication() {
var applicationContainer =
[Link]('applicationContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
select_query = f"""
SELECT id, "payment_date ", concepto, payment_id, payment_amount,
ordenante, concepto_1, concepto_2, estado, email
FROM co_ecu_ops.not_applied_pichincha
WHERE payment_id = '{payment_id_apply}';
"""
cursor = connection_matti.cursor()
[Link](select_query)
payment_data = [Link]()
if payment_data:
# Get student_id using the cedula input
student_query = f"SELECT id FROM students WHERE curp = '{cedula}';"
student_cursor = [Link]()
student_cursor.execute(student_query)
student_id_result = student_cursor.fetchone()
student_cursor.close()
if student_id_result:
student_id = student_id_result[0]
else:
return render_template_string('''
<h1>ERROR: LA CÉDULA INGRESADA NO EXISTE EN LA BASE DE
DATOS.</h1>
<a href="/">Volver</a>
''')
# Create DataFrame
new_data = [Link]({
'student_id': [student_id],
'receipt_only': [False],
'invoices_id': [invoices_id],
'method_paid': ['cash'],
'transaction_reference': [int(payment_id.lstrip('0').strip())],
'ticket_number': [int(payment_id.lstrip('0').strip())],
'payment_date': [payment_date],
'entity': ['Pichincha'],
'amount': [pd.to_numeric(payment_amount)]
})
combined_data.to_excel(output_not, index=False)
update_query = f"""
UPDATE co_ecu_ops.not_applied_pichincha
SET estado = False, email = '{email}'
WHERE payment_id = '{payment_id_apply}';
"""
try:
[Link](update_query)
connection_matti.commit()
application_message = "El pago ha sido escalado a L2, en el transcurso
del día se verá aplicado."
except Exception as e:
connection_matti.rollback()
application_message = f"Error al aplicar el pago: {e}"
else:
application_message = "Error: El pago ingresado no se encuentra
registrado en la base."
[Link]()
return render_template_string('''
<!DOCTYPE html>
<html>
<head>
<title>Consultas Base de Datos</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
text-align: center;
}
h1 {
font-size: 2em;
font-weight: bold;
}
form {
margin-bottom: 20px;
}
table {
margin: 0 auto;
border-collapse: collapse;
width: 80%;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
#resultSection, #verificationSection, #applicationSection {
margin-top: 20px;
}
#toggleButton, #toggleVerification, #toggleApplication {
font-weight: bold;
cursor: pointer;
}
#verificationResult, #applicationResult {
margin-top: 10px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>CONSULTAS BASE DE DATOS</h1>
<form method="post" action="/query">
<label for="curp">Cédula:</label>
<input type="text" id="curp" name="curp" required>
<button type="submit">Consultar</button>
</form>
<div id="resultSection">
<div id="toggleButton" onclick="toggleTable()">Resultado
consulta</div>
<div id="tableContainer" style="display: none;">
{% if data %}
<table>
<tr>
<th>Cedula</th>
<th>Nombre alumno</th>
<th>Tipo_concepto</th>
<th>Fecha_vencimiento</th>
<th>Colegio</th>
<th>Estado</th>
<th>Identificador invoice</th>
<th>Cantidad inicial</th>
<th>Cantidad pendiente</th>
<th>Identificador recibo</th>
<th>Factura</th>
<th>Factura 2</th>
<th>Identificador pago</th>
<th>Cantidad recibo</th>
<th>Referencia pago 1</th>
<th>Referencia pago 2</th>
<th>Monto pago</th>
<th>Mes pago</th>
<th>Entidad pago</th>
</tr>
{% for row in data %}
<tr>
{% for item in row %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</div>
<div id="verificationSection">
<div id="toggleVerification" onclick="toggleVerification()">Verificación
de pago</div>
<div id="verificationContainer" style="display: none;">
<form method="post" action="/verify">
<label for="payment_id">Payment ID:</label>
<input type="text" id="payment_id" name="payment_id"
required>
<button type="submit">Verificar</button>
</form>
<div id="verificationResult">{{ verification_message }}</div>
</div>
</div>
<div id="applicationSection">
<div id="toggleApplication" onclick="toggleApplication()">Aplicación
de pago</div>
<div id="applicationContainer" style="display: none;">
<form method="post" action="/apply">
<div class="form-group">
<label for="email">Correo:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="cedula">Cédula:</label>
<input type="text" id="cedula" name="cedula" required>
</div>
<div class="form-group">
<label for="payment_id_apply">Pago a aplicar:</label>
<input type="text" id="payment_id_apply"
name="payment_id_apply" required>
</div>
<div class="form-group">
<label for="concepto1">Concepto 1:</label>
<input type="text" id="concepto1" name="concepto1">
</div>
<div class="form-group">
<label for="concepto2">Concepto 2:</label>
<input type="text" id="concepto2" name="concepto2">
</div>
<div class="form-group">
<label for="concepto3">Concepto 3:</label>
<input type="text" id="concepto3" name="concepto3">
</div>
<div class="form-group">
<label for="concepto4">Concepto 4:</label>
<input type="text" id="concepto4" name="concepto4">
</div>
<div class="form-group">
<label for="concepto5">Concepto 5:</label>
<input type="text" id="concepto5" name="concepto5">
</div>
<div class="form-group">
<label for="concepto6">Concepto 6:</label>
<input type="text" id="concepto6" name="concepto6">
</div>
<button type="submit">Aplicar</button>
</form>
<div id="applicationResult">{{ application_message }}</div>
</div>
</div>
<script>
function toggleTable() {
var tableContainer = [Link]('tableContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
function toggleVerification() {
var verificationContainer =
[Link]('verificationContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
function toggleApplication() {
var applicationContainer =
[Link]('applicationContainer');
if ([Link] === 'none') {
[Link] = 'block';
} else {
[Link] = 'none';
}
}
if __name__ == '__main__':
[Link](host='[Link]', port=5000)