SekiPOS server sync
This commit is contained in:
@@ -1,9 +1,18 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
import uuid as _uuid
|
||||
from datetime import datetime, timezone
|
||||
from flask import Blueprint, render_template, request, jsonify, current_app
|
||||
from flask_login import login_required, current_user
|
||||
from core.db import get_db_connection
|
||||
|
||||
finance_bp = Blueprint('finance', __name__)
|
||||
|
||||
def _log_deletion(conn, entity_type, entity_uuid):
|
||||
conn.execute("INSERT INTO sync_deletions (entity_type, entity_uuid) VALUES (?, ?)",
|
||||
(entity_type, entity_uuid))
|
||||
|
||||
def _instance_id():
|
||||
return current_app.config.get('INSTANCE_ID', '')
|
||||
|
||||
@finance_bp.route('/dicom')
|
||||
@login_required
|
||||
def dicom():
|
||||
@@ -51,26 +60,31 @@ def pay_debtor_ticket(debtor_id):
|
||||
data = request.get_json()
|
||||
ticket_id = data.get('ticket_id')
|
||||
amount = float(data.get('amount', 0))
|
||||
|
||||
inst_id = _instance_id()
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
if not ticket_id or amount <= 0:
|
||||
return jsonify({"error": "Monto inválido"}), 400
|
||||
|
||||
|
||||
with get_db_connection() as conn:
|
||||
conn.execute('''UPDATE debtor_tickets
|
||||
conn.execute('''UPDATE debtor_tickets
|
||||
SET amount_paid = amount_paid + ?,
|
||||
status = CASE WHEN (total - amount_paid - ?) <= 0 THEN 'paid' ELSE 'partial' END
|
||||
WHERE id = ?''', (amount, amount, ticket_id))
|
||||
|
||||
# Update status based on final values
|
||||
conn.execute('''UPDATE debtor_tickets
|
||||
SET status = CASE
|
||||
status = CASE WHEN (total - amount_paid - ?) <= 0 THEN 'paid' ELSE 'partial' END,
|
||||
updated_at = ?,
|
||||
updated_by = ?
|
||||
WHERE id = ?''', (amount, amount, now, inst_id, ticket_id))
|
||||
|
||||
conn.execute('''UPDATE debtor_tickets
|
||||
SET status = CASE
|
||||
WHEN total - amount_paid <= 0 THEN 'paid'
|
||||
WHEN amount_paid > 0 THEN 'partial'
|
||||
ELSE 'unpaid'
|
||||
END
|
||||
WHERE id = ?''', (ticket_id,))
|
||||
END,
|
||||
updated_at = ?,
|
||||
updated_by = ?
|
||||
WHERE id = ?''', (now, inst_id, ticket_id))
|
||||
conn.commit()
|
||||
|
||||
|
||||
return jsonify({"status": "success"})
|
||||
|
||||
@finance_bp.route('/api/dicom/pay', methods=['POST'])
|
||||
@@ -80,34 +94,34 @@ def dicom_pay():
|
||||
ticket_id = data.get('ticket_id')
|
||||
amount = float(data.get('amount', 0))
|
||||
payment_method = data.get('payment_method', 'efectivo')
|
||||
|
||||
inst_id = _instance_id()
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
if not ticket_id or amount <= 0:
|
||||
return jsonify({"error": "Monto inválido"}), 400
|
||||
|
||||
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
|
||||
# Update the debtor ticket payment
|
||||
cur.execute('UPDATE debtor_tickets SET amount_paid = amount_paid + ? WHERE id = ?', (amount, ticket_id))
|
||||
|
||||
# Update status based on final values
|
||||
cur.execute('''UPDATE debtor_tickets
|
||||
SET status = CASE
|
||||
conn.execute('UPDATE debtor_tickets SET amount_paid = amount_paid + ? WHERE id = ?', (amount, ticket_id))
|
||||
|
||||
conn.execute('''UPDATE debtor_tickets
|
||||
SET status = CASE
|
||||
WHEN total - amount_paid <= 0 THEN 'paid'
|
||||
WHEN amount_paid > 0 THEN 'partial'
|
||||
ELSE 'unpaid'
|
||||
END
|
||||
WHERE id = ?''', (ticket_id,))
|
||||
|
||||
# Insert into sales table to track daily revenue
|
||||
cur.execute('''INSERT INTO sales (date, total, payment_method)
|
||||
VALUES (CURRENT_TIMESTAMP, ?, ?)''', (amount, payment_method))
|
||||
|
||||
END,
|
||||
updated_at = ?,
|
||||
updated_by = ?
|
||||
WHERE id = ?''', (now, inst_id, ticket_id))
|
||||
|
||||
conn.execute('''INSERT INTO sales (date, total, payment_method, uuid)
|
||||
VALUES (CURRENT_TIMESTAMP, ?, ?, ?)''',
|
||||
(amount, payment_method, str(_uuid.uuid4())))
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
return jsonify({"status": "success", "amount": amount}), 200
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print(f"Dicom Pay Error: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
@@ -121,22 +135,27 @@ def update_dicom():
|
||||
notes = data.get('notes', '')
|
||||
image_url = data.get('image_url', '')
|
||||
action = data.get('action')
|
||||
|
||||
inst_id = _instance_id()
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
if not name or amount <= 0:
|
||||
return jsonify({"error": "Nombre y monto válidos son requeridos"}), 400
|
||||
|
||||
|
||||
if action == 'add':
|
||||
amount = -amount
|
||||
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
cur.execute('''INSERT INTO dicom (name, amount, notes, image_url, last_updated)
|
||||
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
amount = amount + excluded.amount,
|
||||
notes = excluded.notes,
|
||||
image_url = CASE WHEN excluded.image_url != "" THEN excluded.image_url ELSE dicom.image_url END,
|
||||
last_updated = CURRENT_TIMESTAMP''', (name, amount, notes, image_url))
|
||||
existing = conn.execute("SELECT uuid FROM dicom WHERE name = ?", (name,)).fetchone()
|
||||
ent_uuid = existing[0] if existing else str(_uuid.uuid4())
|
||||
conn.execute('''INSERT INTO dicom (uuid, name, amount, notes, image_url, updated_at, updated_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
amount = amount + excluded.amount,
|
||||
notes = excluded.notes,
|
||||
image_url = CASE WHEN excluded.image_url != "" THEN excluded.image_url ELSE dicom.image_url END,
|
||||
updated_at = ?,
|
||||
updated_by = ?''',
|
||||
(ent_uuid, name, amount, notes, image_url, now, inst_id, now, inst_id))
|
||||
conn.commit()
|
||||
return jsonify({"status": "success"}), 200
|
||||
|
||||
@@ -145,6 +164,9 @@ def update_dicom():
|
||||
def delete_dicom(debtor_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
row = conn.execute("SELECT uuid FROM dicom WHERE id = ?", (debtor_id,)).fetchone()
|
||||
if row:
|
||||
_log_deletion(conn, 'dicom', row[0])
|
||||
conn.execute('DELETE FROM dicom WHERE id = ?', (debtor_id,))
|
||||
conn.commit()
|
||||
return jsonify({"status": "success"}), 200
|
||||
@@ -189,13 +211,13 @@ def add_gasto():
|
||||
data = request.get_json()
|
||||
desc = data.get('description')
|
||||
amount = data.get('amount')
|
||||
|
||||
|
||||
if not desc or not amount:
|
||||
return jsonify({"error": "Faltan datos"}), 400
|
||||
|
||||
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
cur.execute("INSERT INTO expenses (description, amount) VALUES (?, ?)", (desc, int(amount)))
|
||||
conn.execute("INSERT INTO expenses (uuid, description, amount) VALUES (?, ?, ?)",
|
||||
(str(_uuid.uuid4()), desc, int(amount)))
|
||||
conn.commit()
|
||||
return jsonify({"success": True})
|
||||
|
||||
@@ -203,8 +225,10 @@ def add_gasto():
|
||||
@login_required
|
||||
def delete_gasto(gasto_id):
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
cur.execute("DELETE FROM expenses WHERE id = ?", (gasto_id,))
|
||||
row = conn.execute("SELECT uuid FROM expenses WHERE id = ?", (gasto_id,)).fetchone()
|
||||
if row:
|
||||
_log_deletion(conn, 'expense', row[0])
|
||||
conn.execute("DELETE FROM expenses WHERE id = ?", (gasto_id,))
|
||||
conn.commit()
|
||||
return jsonify({"success": True})
|
||||
|
||||
@@ -213,22 +237,17 @@ def delete_gasto(gasto_id):
|
||||
def get_debtors():
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
# Check if table exists
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='debtors'")
|
||||
if not cur.fetchone():
|
||||
print("Debtors table does not exist!")
|
||||
return jsonify([])
|
||||
|
||||
cur.execute('SELECT id, name, contact_info FROM debtors ORDER BY name')
|
||||
|
||||
cur.execute('SELECT id, name, contact_info, uuid FROM debtors ORDER BY name')
|
||||
debtors = cur.fetchall()
|
||||
print(f"Found {len(debtors)} debtors:", debtors)
|
||||
|
||||
return jsonify([{"id": d[0], "name": d[1], "contact_info": d[2]} for d in debtors])
|
||||
|
||||
return jsonify([{"id": d[0], "name": d[1], "contact_info": d[2], "uuid": d[3]} for d in debtors])
|
||||
except Exception as e:
|
||||
print(f"Error getting debtors: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return jsonify([])
|
||||
|
||||
@finance_bp.route('/api/dicom/checkout', methods=['POST'])
|
||||
@@ -240,51 +259,50 @@ def dicom_checkout():
|
||||
debtor_name = data.get('debtor_name', '').strip()
|
||||
contact_info = data.get('contact_info', '').strip()
|
||||
initial_payment = data.get('initial_payment', 0) or 0
|
||||
|
||||
inst_id = _instance_id()
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
if not cart:
|
||||
return jsonify({"error": "Carrito vacío"}), 400
|
||||
if not debtor_name:
|
||||
return jsonify({"error": "Nombre del deudor requerido"}), 400
|
||||
|
||||
|
||||
total = sum(item.get('subtotal', 0) for item in cart)
|
||||
|
||||
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
|
||||
# Upsert debtor
|
||||
cur.execute('''INSERT INTO debtors (name, contact_info)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
contact_info = excluded.contact_info''',
|
||||
(debtor_name, contact_info))
|
||||
|
||||
# Get debtor ID
|
||||
debtor_id = cur.execute('SELECT id FROM debtors WHERE name = ?', (debtor_name,)).fetchone()[0]
|
||||
|
||||
# Insert debtor ticket
|
||||
debtor_uuid = str(_uuid.uuid4())
|
||||
conn.execute('''INSERT INTO debtors (uuid, name, contact_info, updated_at, updated_by)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
contact_info = excluded.contact_info,
|
||||
updated_at = excluded.updated_at,
|
||||
updated_by = excluded.updated_by''',
|
||||
(debtor_uuid, debtor_name, contact_info, now, inst_id))
|
||||
|
||||
debtor_id = conn.execute('SELECT id FROM debtors WHERE name = ?', (debtor_name,)).fetchone()[0]
|
||||
|
||||
status = 'partial' if initial_payment > 0 else 'unpaid'
|
||||
cur.execute('''INSERT INTO debtor_tickets (debtor_id, total, amount_paid, status)
|
||||
VALUES (?, ?, ?, ?)''',
|
||||
(debtor_id, total, initial_payment, status))
|
||||
ticket_id = cur.lastrowid
|
||||
|
||||
# Insert ticket items and deduct stock
|
||||
ticket_uuid = str(_uuid.uuid4())
|
||||
c = conn.execute('''INSERT INTO debtor_tickets (uuid, debtor_id, total, amount_paid, status, updated_at, updated_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)''',
|
||||
(ticket_uuid, debtor_id, total, initial_payment, status, now, inst_id))
|
||||
ticket_id = c.lastrowid
|
||||
|
||||
for item in cart:
|
||||
cur.execute('''INSERT INTO debtor_ticket_items
|
||||
conn.execute('''INSERT INTO debtor_ticket_items
|
||||
(ticket_id, barcode, name, price, quantity, subtotal)
|
||||
VALUES (?, ?, ?, ?, ?, ?)''',
|
||||
(ticket_id, item.get('barcode', ''), item.get('name'),
|
||||
(ticket_id, item.get('barcode', ''), item.get('name'),
|
||||
item.get('price'), item.get('qty'), item.get('subtotal')))
|
||||
|
||||
# Deduct stock (skip for manual products)
|
||||
|
||||
if item.get('barcode') and not item.get('barcode', '').startswith(('MANUAL-', 'VARIOS-', 'RAPIDA-')):
|
||||
cur.execute('UPDATE products SET stock = stock - ? WHERE barcode = ?',
|
||||
conn.execute('UPDATE products SET stock = stock - ? WHERE barcode = ?',
|
||||
(item.get('qty'), item.get('barcode')))
|
||||
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
return jsonify({"status": "success", "ticket_id": ticket_id, "debtor": debtor_name}), 200
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print(f"Dicom Checkout Error: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
@@ -294,13 +312,15 @@ def dicom_checkout():
|
||||
def delete_debtor(debtor_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
# Delete items first
|
||||
cur.execute('DELETE FROM debtor_ticket_items WHERE ticket_id IN (SELECT id FROM debtor_tickets WHERE debtor_id = ?)', (debtor_id,))
|
||||
# Delete tickets
|
||||
cur.execute('DELETE FROM debtor_tickets WHERE debtor_id = ?', (debtor_id,))
|
||||
# Delete debtor
|
||||
cur.execute('DELETE FROM debtors WHERE id = ?', (debtor_id,))
|
||||
row = conn.execute("SELECT uuid FROM debtors WHERE id = ?", (debtor_id,)).fetchone()
|
||||
if row:
|
||||
_log_deletion(conn, 'debtor', row[0])
|
||||
tickets = conn.execute("SELECT uuid FROM debtor_tickets WHERE debtor_id = ?", (debtor_id,)).fetchall()
|
||||
for t in tickets:
|
||||
_log_deletion(conn, 'ticket', t[0])
|
||||
conn.execute('DELETE FROM debtor_ticket_items WHERE ticket_id IN (SELECT id FROM debtor_tickets WHERE debtor_id = ?)', (debtor_id,))
|
||||
conn.execute('DELETE FROM debtor_tickets WHERE debtor_id = ?', (debtor_id,))
|
||||
conn.execute('DELETE FROM debtors WHERE id = ?', (debtor_id,))
|
||||
conn.commit()
|
||||
return jsonify({"status": "success"}), 200
|
||||
except Exception as e:
|
||||
@@ -312,11 +332,11 @@ def delete_debtor(debtor_id):
|
||||
def delete_ticket(ticket_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
# Delete items first
|
||||
cur.execute('DELETE FROM debtor_ticket_items WHERE ticket_id = ?', (ticket_id,))
|
||||
# Delete ticket
|
||||
cur.execute('DELETE FROM debtor_tickets WHERE id = ?', (ticket_id,))
|
||||
row = conn.execute("SELECT uuid FROM debtor_tickets WHERE id = ?", (ticket_id,)).fetchone()
|
||||
if row:
|
||||
_log_deletion(conn, 'ticket', row[0])
|
||||
conn.execute('DELETE FROM debtor_ticket_items WHERE ticket_id = ?', (ticket_id,))
|
||||
conn.execute('DELETE FROM debtor_tickets WHERE id = ?', (ticket_id,))
|
||||
conn.commit()
|
||||
return jsonify({"status": "success"}), 200
|
||||
except Exception as e:
|
||||
@@ -328,28 +348,23 @@ def delete_ticket(ticket_id):
|
||||
def delete_item(item_id):
|
||||
try:
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
# Get item info to update ticket total
|
||||
item = cur.execute('SELECT ticket_id, subtotal FROM debtor_ticket_items WHERE id = ?', (item_id,)).fetchone()
|
||||
item = conn.execute('SELECT ticket_id, subtotal FROM debtor_ticket_items WHERE id = ?', (item_id,)).fetchone()
|
||||
if not item:
|
||||
return jsonify({"error": "Item no encontrado"}), 404
|
||||
|
||||
|
||||
ticket_id, item_subtotal = item
|
||||
|
||||
# Delete item
|
||||
cur.execute('DELETE FROM debtor_ticket_items WHERE id = ?', (item_id,))
|
||||
|
||||
# Check if ticket has remaining items
|
||||
remaining_items = cur.execute('SELECT COUNT(*) FROM debtor_ticket_items WHERE ticket_id = ?', (ticket_id,)).fetchone()[0]
|
||||
|
||||
conn.execute('DELETE FROM debtor_ticket_items WHERE id = ?', (item_id,))
|
||||
remaining_items = conn.execute('SELECT COUNT(*) FROM debtor_ticket_items WHERE ticket_id = ?', (ticket_id,)).fetchone()[0]
|
||||
|
||||
if remaining_items == 0:
|
||||
# Delete ticket if no items left
|
||||
cur.execute('DELETE FROM debtor_tickets WHERE id = ?', (ticket_id,))
|
||||
row = conn.execute("SELECT uuid FROM debtor_tickets WHERE id = ?", (ticket_id,)).fetchone()
|
||||
if row:
|
||||
_log_deletion(conn, 'ticket', row[0])
|
||||
conn.execute('DELETE FROM debtor_tickets WHERE id = ?', (ticket_id,))
|
||||
conn.commit()
|
||||
return jsonify({"status": "success", "ticket_deleted": True}), 200
|
||||
|
||||
# Update ticket total
|
||||
cur.execute('UPDATE debtor_tickets SET total = total - ? WHERE id = ?', (item_subtotal, ticket_id))
|
||||
|
||||
|
||||
conn.execute('UPDATE debtor_tickets SET total = total - ? WHERE id = ?', (item_subtotal, ticket_id))
|
||||
conn.commit()
|
||||
return jsonify({"status": "success", "ticket_deleted": False}), 200
|
||||
except Exception as e:
|
||||
@@ -363,36 +378,34 @@ def pay_all_debtor(debtor_id):
|
||||
data = request.get_json()
|
||||
amount = float(data.get('amount', 0))
|
||||
payment_method = data.get('payment_method', 'efectivo')
|
||||
|
||||
inst_id = _instance_id()
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
if amount <= 0:
|
||||
return jsonify({"error": "Monto inválido"}), 400
|
||||
|
||||
|
||||
with get_db_connection() as conn:
|
||||
cur = conn.cursor()
|
||||
|
||||
# Get all unpaid/partial tickets for this debtor
|
||||
tickets = cur.execute('''SELECT id, total, amount_paid, total - amount_paid as remaining
|
||||
FROM debtor_tickets
|
||||
tickets = conn.execute('''SELECT id, total, amount_paid, total - amount_paid as remaining
|
||||
FROM debtor_tickets
|
||||
WHERE debtor_id = ? AND status != 'paid' ''', (debtor_id,)).fetchall()
|
||||
|
||||
|
||||
for ticket in tickets:
|
||||
ticket_id = ticket[0]
|
||||
remaining = ticket[3]
|
||||
|
||||
|
||||
if remaining > 0:
|
||||
# Pay remaining amount
|
||||
cur.execute('UPDATE debtor_tickets SET amount_paid = amount_paid + ? WHERE id = ?',
|
||||
(remaining, ticket_id))
|
||||
# Update status
|
||||
cur.execute('''UPDATE debtor_tickets SET status = 'paid' WHERE id = ?''', (ticket_id,))
|
||||
# Record sale
|
||||
cur.execute('''INSERT INTO sales (date, total, payment_method)
|
||||
VALUES (CURRENT_TIMESTAMP, ?, ?)''', (remaining, payment_method))
|
||||
|
||||
conn.execute('UPDATE debtor_tickets SET amount_paid = amount_paid + ?, updated_at = ?, updated_by = ? WHERE id = ?',
|
||||
(remaining, now, inst_id, ticket_id))
|
||||
conn.execute('UPDATE debtor_tickets SET status = \'paid\', updated_at = ?, updated_by = ? WHERE id = ?',
|
||||
(now, inst_id, ticket_id))
|
||||
conn.execute('''INSERT INTO sales (date, total, payment_method, uuid)
|
||||
VALUES (CURRENT_TIMESTAMP, ?, ?, ?)''',
|
||||
(remaining, payment_method, str(_uuid.uuid4())))
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
return jsonify({"status": "success"}), 200
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print(f"Pay All Debtor Error: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
Reference in New Issue
Block a user