Migrated getVendors, Receipts, Linkedlists in
receipts
This commit is contained in:
parent
cb240f7e97
commit
9931a8232f
@ -46,6 +46,7 @@ def getItems():
|
||||
return jsonify({"items":recordset, "end":math.ceil(count['count']/limit), "error":False, "message":"items fetched succesfully!"})
|
||||
return jsonify({"items":recordset, "end":math.ceil(count['count']/limit), "error":True, "message":"There was an error with this GET statement"})
|
||||
|
||||
# Added to Database
|
||||
@receipt_api.route('/api/getVendors', methods=["GET"])
|
||||
def getVendors():
|
||||
recordset = []
|
||||
@ -55,10 +56,7 @@ def getVendors():
|
||||
limit = int(request.args.get('limit', 10))
|
||||
site_name = session['selected_site']
|
||||
offset = (page - 1) * limit
|
||||
database_config = config()
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
payload = (limit, offset)
|
||||
recordset, count = postsqldb.VendorsTable.paginateVendors(conn, site_name, payload)
|
||||
recordset, count = receipts_database.paginateVendorsTuples(site_name, payload=(limit, offset))
|
||||
return jsonify({"vendors":recordset, "end":math.ceil(count/limit), "error":False, "message":"items fetched succesfully!"})
|
||||
return jsonify({"vendors":recordset, "end":math.ceil(count/limit), "error":True, "message":"There was an error with this GET statement"})
|
||||
|
||||
@ -71,13 +69,11 @@ def getLinkedLists():
|
||||
limit = int(request.args.get('limit', 10))
|
||||
site_name = session['selected_site']
|
||||
offset = (page - 1) * limit
|
||||
database_config = config()
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
payload = (limit, offset)
|
||||
recordset, count = postsqldb.ItemTable.paginateLinkedLists(conn, site_name, payload)
|
||||
recordset, count = receipts_database.paginateLinkedLists(site_name, payload=(limit, offset))
|
||||
return jsonify({"items":recordset, "end":math.ceil(count/limit), "error":False, "message":"items fetched succesfully!"})
|
||||
return jsonify({"items":recordset, "end":math.ceil(count/limit), "error":True, "message":"There was an error with this GET statement"})
|
||||
|
||||
# Added to database
|
||||
@receipt_api.route('/api/getReceipts', methods=["GET"])
|
||||
def getReceipts():
|
||||
recordset = []
|
||||
@ -86,23 +82,20 @@ def getReceipts():
|
||||
limit = int(request.args.get('limit', 50))
|
||||
offset = (page - 1) * limit
|
||||
site_name = session['selected_site']
|
||||
database_config = config()
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
recordset, count = database.getReceipts(conn, site_name, payload=(limit, offset), convert=True)
|
||||
return jsonify({'receipts':recordset, "end": math.ceil(count/limit), 'error': False, "message": "Get Receipts Successful!"})
|
||||
recordset, count = receipts_database.paginateReceiptsTuples(site_name, payload=(limit, offset))
|
||||
return jsonify({'receipts':recordset, "end": math.ceil(count/limit), 'error': False, "message": "Get Receipts Successful!"})
|
||||
return jsonify({'receipts': recordset, "end": math.ceil(count/limit), 'error': True, "message": "Something went wrong while getting receipts!"})
|
||||
|
||||
# Added to database
|
||||
@receipt_api.route('/api/getReceipt', methods=["GET"])
|
||||
def getReceipt():
|
||||
record = []
|
||||
receipt = []
|
||||
if request.method == "GET":
|
||||
receipt_id = int(request.args.get('id', 1))
|
||||
site_name = session['selected_site']
|
||||
database_config = config()
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
record = database.getReceiptByID(conn, site_name, payload=(receipt_id, ), convert=True)
|
||||
return jsonify({'receipt': record, 'error': False, "message": "Get Receipts Successful!"})
|
||||
return jsonify({'receipt': record, 'error': True, "message": "Something went wrong while getting receipts!"})
|
||||
receipt = receipts_database.getReceiptByID(site_name, (receipt_id, ))
|
||||
return jsonify({'receipt': receipt, 'error': False, "message": "Get Receipts Successful!"})
|
||||
return jsonify({'receipt': receipt, 'error': True, "message": "Something went wrong while getting receipts!"})
|
||||
|
||||
# added to database
|
||||
@receipt_api.route('/api/addReceipt', methods=["POST", "GET"])
|
||||
|
||||
@ -108,6 +108,129 @@ def getItemAllByID(site, payload, convert=True, conn=None):
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
raise postsqldb.DatabaseError(error, payload, getItemAllByID_sql)
|
||||
|
||||
def getReceiptByID(site, payload, convert=True, conn=None):
|
||||
receipt = []
|
||||
self_conn = False
|
||||
with open(f"application/receipts/sql/getReceiptByID.sql", "r+") as file:
|
||||
sql = file.read().replace("%%site_name%%", site)
|
||||
try:
|
||||
if not conn:
|
||||
database_config = config.config()
|
||||
conn = psycopg2.connect(**database_config)
|
||||
conn.autocommit = True
|
||||
self_conn = True
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql, payload)
|
||||
row = cur.fetchone()
|
||||
if row and convert:
|
||||
receipt = postsqldb.tupleDictionaryFactory(cur.description, row)
|
||||
if row and not convert:
|
||||
receipt = row
|
||||
|
||||
if self_conn:
|
||||
conn.close()
|
||||
|
||||
return receipt
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
raise postsqldb.DatabaseError(error, payload, sql)
|
||||
|
||||
def paginateReceiptsTuples(site, payload, convert=True, conn=None):
|
||||
"""payload=(limit, offset)"""
|
||||
receipts = []
|
||||
count = 0
|
||||
self_conn = False
|
||||
with open(f"application/receipts/sql/getReceipts.sql", "r+") as file:
|
||||
sql = file.read().replace("%%site_name%%", site)
|
||||
try:
|
||||
if not conn:
|
||||
database_config = config.config()
|
||||
conn = psycopg2.connect(**database_config)
|
||||
conn.autocommit = True
|
||||
self_conn = True
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql, payload)
|
||||
rows = cur.fetchall()
|
||||
if rows and convert:
|
||||
receipts = [postsqldb.tupleDictionaryFactory(cur.description, row) for row in rows]
|
||||
if rows and not convert:
|
||||
receipts = rows
|
||||
|
||||
cur.execute(f"SELECT COUNT(*) FROM {site}_receipts;")
|
||||
count = cur.fetchone()[0]
|
||||
|
||||
if self_conn:
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return receipts, count
|
||||
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
raise postsqldb.DatabaseError(error, payload, sql)
|
||||
|
||||
def paginateVendorsTuples(site, payload, convert=True, conn=None):
|
||||
"""payload (tuple): (limit, offset)"""
|
||||
recordset = ()
|
||||
count = 0
|
||||
self_conn = False
|
||||
sql = f"SELECT * FROM {site}_vendors LIMIT %s OFFSET %s;"
|
||||
try:
|
||||
if not conn:
|
||||
database_config = config.config()
|
||||
conn = psycopg2.connect(**database_config)
|
||||
conn.autocommit = True
|
||||
self_conn = True
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql, payload)
|
||||
rows = cur.fetchall()
|
||||
if rows and convert:
|
||||
recordset = [postsqldb.tupleDictionaryFactory(cur.description, row) for row in rows]
|
||||
elif rows and not convert:
|
||||
recordset = rows
|
||||
|
||||
cur.execute(f"SELECT COUNT(*) FROM {site}_vendors;")
|
||||
count = cur.fetchone()[0]
|
||||
|
||||
if self_conn:
|
||||
conn.close()
|
||||
|
||||
return recordset, count
|
||||
except Exception as error:
|
||||
raise postsqldb.DatabaseError(error, (), sql)
|
||||
|
||||
def paginateLinkedLists(site, payload, convert=True, conn=None):
|
||||
records = []
|
||||
count = 0
|
||||
self_conn = False
|
||||
sql = f"SELECT * FROM {site}_items WHERE row_type = 'list' LIMIT %s OFFSET %s;"
|
||||
sql_count = f"SELECT COUNT(*) FROM {site}_items WHERE row_type = 'list' LIMIT %s OFFSET %s;"
|
||||
try:
|
||||
if not conn:
|
||||
database_config = config.config()
|
||||
conn = psycopg2.connect(**database_config)
|
||||
conn.autocommit = True
|
||||
self_conn = True
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql, payload)
|
||||
rows = cur.fetchall()
|
||||
if rows and convert:
|
||||
records = [postsqldb.tupleDictionaryFactory(cur.description, row) for row in rows]
|
||||
if rows and not convert:
|
||||
records = rows
|
||||
|
||||
cur.execute(sql_count, payload)
|
||||
count = cur.fetchone()[0]
|
||||
|
||||
if self_conn:
|
||||
conn.close()
|
||||
|
||||
return records, count
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
raise postsqldb.DatabaseError(error, payload, sql)
|
||||
|
||||
def selectReceiptItemsTuple(site, payload, convert=True, conn=None):
|
||||
selected = ()
|
||||
self_conn = False
|
||||
|
||||
17
application/receipts/sql/getReceiptByID.sql
Normal file
17
application/receipts/sql/getReceiptByID.sql
Normal file
@ -0,0 +1,17 @@
|
||||
WITH passed_id AS (SELECT %s AS passed_id),
|
||||
cte_receipt_items AS (
|
||||
SELECT items.* ,
|
||||
(SELECT COALESCE(row_to_json(un), '{}') FROM units un WHERE un.id = items.uom LIMIT 1) AS uom
|
||||
FROM %%site_name%%_receipt_items items
|
||||
WHERE items.receipt_id = (SELECT passed_id FROM passed_id)
|
||||
)
|
||||
|
||||
SELECT (SELECT passed_id FROM passed_id) AS passed_id,
|
||||
%%site_name%%_receipts.*,
|
||||
logins.username as submitted_by,
|
||||
(SELECT COALESCE(array_agg(row_to_json(ris)), '{}') FROM cte_receipt_items ris) AS receipt_items,
|
||||
row_to_json(%%site_name%%_vendors.*) as vendor
|
||||
FROM %%site_name%%_receipts
|
||||
JOIN logins ON %%site_name%%_receipts.submitted_by = logins.id
|
||||
LEFT JOIN %%site_name%%_vendors ON %%site_name%%_receipts.vendor_id = %%site_name%%_vendors.id
|
||||
WHERE %%site_name%%_receipts.id=(SELECT passed_id FROM passed_id)
|
||||
7
application/receipts/sql/getReceipts.sql
Normal file
7
application/receipts/sql/getReceipts.sql
Normal file
@ -0,0 +1,7 @@
|
||||
SELECT %%site_name%%_receipts.*,
|
||||
logins.username as submitted_by
|
||||
FROM %%site_name%%_receipts
|
||||
JOIN logins ON %%site_name%%_receipts.submitted_by = logins.id
|
||||
ORDER BY %%site_name%%_receipts.id DESC
|
||||
LIMIT %s
|
||||
OFFSET %s;
|
||||
Loading…
x
Reference in New Issue
Block a user