Fixed Line Resolving in Receipts Module
you can now resolve everything by item uuid
This commit is contained in:
parent
71db5e621b
commit
ce61b21a9e
Binary file not shown.
@ -100,12 +100,14 @@ def postNewBlankItem(site_name: str, user_id: int, data: dict, conn=None):
|
|||||||
|
|
||||||
database_items.postAddTransaction(site_name, creation_tuple.payload(), conn=conn)
|
database_items.postAddTransaction(site_name, creation_tuple.payload(), conn=conn)
|
||||||
|
|
||||||
|
item_uuid = item['item_uuid']
|
||||||
|
|
||||||
if self_conn:
|
if self_conn:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
return False
|
return False, item_uuid
|
||||||
|
|
||||||
return conn
|
return conn, item_uuid
|
||||||
|
|
||||||
def postLinkedItem(site, payload):
|
def postLinkedItem(site, payload):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -151,7 +151,39 @@ def getItemAllByBarcode(site, payload, convert=True, conn=None):
|
|||||||
return item
|
return item
|
||||||
except (Exception, psycopg2.DatabaseError) as error:
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
raise postsqldb.DatabaseError(error, payload, getItemAllByBarcode_sql)
|
raise postsqldb.DatabaseError(error, payload, getItemAllByBarcode_sql)
|
||||||
|
|
||||||
|
def getItemAllByUUID(site, payload, convert=True, conn=None):
|
||||||
|
item = ()
|
||||||
|
self_conn = False
|
||||||
|
|
||||||
|
if not conn:
|
||||||
|
database_config = config.config()
|
||||||
|
conn = psycopg2.connect(**database_config)
|
||||||
|
conn.autocommit = True
|
||||||
|
self_conn = True
|
||||||
|
|
||||||
|
if convert:
|
||||||
|
item = {}
|
||||||
|
|
||||||
|
|
||||||
|
with open(f"application/receipts/sql/getItemAllByUUID.sql", "r+") as file:
|
||||||
|
getItemAllByUUID_sql = file.read().replace("%%site_name%%", site)
|
||||||
|
try:
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(getItemAllByUUID_sql, payload)
|
||||||
|
rows = cur.fetchone()
|
||||||
|
if rows and convert:
|
||||||
|
item = postsqldb.tupleDictionaryFactory(cur.description, rows)
|
||||||
|
if rows and not convert:
|
||||||
|
item = rows
|
||||||
|
|
||||||
|
if self_conn:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return item
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
raise postsqldb.DatabaseError(error, payload, getItemAllByUUID_sql)
|
||||||
|
|
||||||
def getItemAllByID(site, payload, convert=True, conn=None):
|
def getItemAllByID(site, payload, convert=True, conn=None):
|
||||||
item = ()
|
item = ()
|
||||||
self_conn = False
|
self_conn = False
|
||||||
@ -587,6 +619,37 @@ def insertReceiptsTuple(site, payload, convert=True, conn=None):
|
|||||||
except Exception as error:
|
except Exception as error:
|
||||||
raise postsqldb.DatabaseError(error, payload, sql)
|
raise postsqldb.DatabaseError(error, payload, sql)
|
||||||
|
|
||||||
|
def insertBarcodesTuple(site: str, payload: list, convert=True, conn=None):
|
||||||
|
""" payload (tuple): (barcode, item_uuid, in_exchange, out_exchange, descriptor) """
|
||||||
|
record = ()
|
||||||
|
self_conn = False
|
||||||
|
|
||||||
|
sql = f"INSERT INTO {site}_barcodes (barcode, item_uuid, in_exchange, out_exchange, descriptor) VALUES (%s, %s, %s, %s, %s) RETURNING *;"
|
||||||
|
|
||||||
|
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.fetchone()
|
||||||
|
if rows and convert:
|
||||||
|
record = postsqldb.tupleDictionaryFactory(cur.description, rows)
|
||||||
|
elif rows and not convert:
|
||||||
|
record = rows
|
||||||
|
|
||||||
|
if self_conn:
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return record
|
||||||
|
|
||||||
|
except Exception as error:
|
||||||
|
raise postsqldb.DatabaseError(error, payload, sql)
|
||||||
|
|
||||||
def updateItemsTuple(site, payload, convert=True, conn=None):
|
def updateItemsTuple(site, payload, convert=True, conn=None):
|
||||||
"""payload (dict): {'id': row_id, 'update': {... column_to_update: value_to_update_to...}}"""
|
"""payload (dict): {'id': row_id, 'update': {... column_to_update: value_to_update_to...}}"""
|
||||||
updated = ()
|
updated = ()
|
||||||
|
|||||||
@ -107,28 +107,31 @@ def postLine(site, user_id, data, conn=None):
|
|||||||
conn = psycopg2.connect(**database_config)
|
conn = psycopg2.connect(**database_config)
|
||||||
conn.autocommit = False
|
conn.autocommit = False
|
||||||
self_conn = True
|
self_conn = True
|
||||||
|
|
||||||
transaction_time = datetime.datetime.now()
|
transaction_time = datetime.datetime.now()
|
||||||
receipt_item = receipts_database.selectReceiptItemsTuple(site, (data['line_id'],), conn=conn)
|
receipt_item = receipts_database.selectReceiptItemsTuple(site, (data['line_id'],), conn=conn)
|
||||||
receipt = receipts_database.getReceiptByID(site, (receipt_item['receipt_id'], ), conn=conn)
|
receipt = receipts_database.getReceiptByID(site, (receipt_item['receipt_id'], ), conn=conn)
|
||||||
conv_factor = 1.0
|
conv_factor = 1.0
|
||||||
|
|
||||||
if receipt_item['data']['expires'] is not False:
|
if receipt_item['data']['expires'] is not False:
|
||||||
expiration = datetime.datetime.strptime(receipt_item['data']['expires'], "%Y-%m-%d")
|
expiration = datetime.datetime.strptime(receipt_item['data']['expires'], "%Y-%m-%d")
|
||||||
else:
|
else:
|
||||||
expiration = None
|
expiration = None
|
||||||
|
|
||||||
if receipt_item['type'] == 'sku':
|
#if receipt_item['type'] == 'sku':
|
||||||
linked_item = receipts_database.getLinkedItemByBarcode(site, (receipt_item['barcode'], ), conn=conn)
|
# linked_item = receipts_database.getLinkedItemByBarcode(site, (receipt_item['barcode'], ), conn=conn)
|
||||||
if len(linked_item) > 1:
|
# if len(linked_item) > 1:
|
||||||
conv_factor = linked_item['conv_factor']
|
# conv_factor = linked_item['conv_factor']
|
||||||
receipt_item['data']['linked_child'] = linked_item['barcode']
|
# receipt_item['data']['linked_child'] = linked_item['barcode']
|
||||||
|
|
||||||
|
item_uuid = receipt_item['item_uuid']
|
||||||
if receipt_item['type'] == 'api':
|
if receipt_item['type'] == 'api':
|
||||||
new_item_data = {
|
new_item_data = {
|
||||||
'barcode': receipt_item['barcode'],
|
'barcode': receipt_item['barcode'],
|
||||||
'name': receipt_item['name'],
|
'name': receipt_item['name'],
|
||||||
'subtype': 'FOOD'
|
'subtype': 'FOOD'
|
||||||
}
|
}
|
||||||
postNewBlankItem(site, user_id, new_item_data, conn=conn)
|
_, item_uuid = postNewBlankItem(site, user_id, new_item_data, conn=conn)
|
||||||
|
|
||||||
if receipt_item['type'] == "new sku":
|
if receipt_item['type'] == "new sku":
|
||||||
new_item_data = {
|
new_item_data = {
|
||||||
@ -136,14 +139,24 @@ def postLine(site, user_id, data, conn=None):
|
|||||||
'name': receipt_item['name'],
|
'name': receipt_item['name'],
|
||||||
'subtype': 'FOOD'
|
'subtype': 'FOOD'
|
||||||
}
|
}
|
||||||
postNewBlankItem(site, user_id, new_item_data, conn=conn)
|
_, item_uuid = postNewBlankItem(site, user_id, new_item_data, conn=conn)
|
||||||
|
barcodes_tuple = database_payloads.BarcodesPayload(
|
||||||
|
barcode=receipt_item['barcode'],
|
||||||
|
item_uuid=item_uuid,
|
||||||
|
in_exchange=1.0,
|
||||||
|
out_exchange=1.0,
|
||||||
|
descriptor=receipt_item['name']
|
||||||
|
)
|
||||||
|
receipts_database.insertBarcodesTuple(site, barcodes_tuple.payload())
|
||||||
|
|
||||||
item = receipts_database.getItemAllByBarcode(site, (receipt_item['barcode'], ), conn=conn)
|
|
||||||
|
item = receipts_database.getItemAllByUUID(site, (item_uuid, ), conn=conn)
|
||||||
|
|
||||||
location = receipts_database.selectItemLocationsTuple(site, (item['id'], item['logistics_info']['primary_location']['id']), conn=conn)
|
location = receipts_database.selectItemLocationsTuple(site, (item['id'], item['logistics_info']['primary_location']['id']), conn=conn)
|
||||||
cost_layers: list = location['cost_layers']
|
cost_layers: list = location['cost_layers']
|
||||||
|
|
||||||
receipt_item['data']['location'] = item['logistics_info']['primary_location']['uuid']
|
receipt_item['data']['location'] = item['logistics_info']['primary_location']['uuid']
|
||||||
|
receipt_item['item_uuid'] = item_uuid
|
||||||
|
|
||||||
transaction = database_payloads.TransactionPayload(
|
transaction = database_payloads.TransactionPayload(
|
||||||
timestamp=transaction_time,
|
timestamp=transaction_time,
|
||||||
@ -182,8 +195,6 @@ def postLine(site, user_id, data, conn=None):
|
|||||||
|
|
||||||
receipts_database.updateReceiptItemsTuple(site, {'id': receipt_item['id'], 'update': {'status': "Resolved"}}, conn=conn)
|
receipts_database.updateReceiptItemsTuple(site, {'id': receipt_item['id'], 'update': {'status': "Resolved"}}, conn=conn)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if self_conn:
|
if self_conn:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
75
application/receipts/sql/GetItemAllByUUID.sql
Normal file
75
application/receipts/sql/GetItemAllByUUID.sql
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
WITH passed_id AS (SELECT id AS passed_id FROM %%site_name%%_items WHERE item_uuid=%s),
|
||||||
|
logistics_id AS (SELECT logistics_info_id FROM %%site_name%%_items WHERE id=(SELECT passed_id FROM passed_id)),
|
||||||
|
info_id AS (SELECT item_info_id FROM %%site_name%%_items WHERE id=(SELECT passed_id FROM passed_id)),
|
||||||
|
cte_item_info AS (
|
||||||
|
SELECT
|
||||||
|
%%site_name%%_item_info.*,
|
||||||
|
row_to_json(units.*) as uom
|
||||||
|
FROM %%site_name%%_item_info
|
||||||
|
LEFT JOIN units ON %%site_name%%_item_info.uom = units.id
|
||||||
|
WHERE %%site_name%%_item_info.id = (SELECT item_info_id FROM info_id)
|
||||||
|
),
|
||||||
|
cte_groups AS (
|
||||||
|
SELECT
|
||||||
|
%%site_name%%_groups.*,
|
||||||
|
%%site_name%%_group_items.uuid,
|
||||||
|
%%site_name%%_group_items.item_type,
|
||||||
|
%%site_name%%_group_items.qty
|
||||||
|
FROM %%site_name%%_groups
|
||||||
|
JOIN %%site_name%%_group_items ON %%site_name%%_groups.id = %%site_name%%_group_items.gr_id
|
||||||
|
WHERE %%site_name%%_group_items.item_id = (SELECT passed_id FROM passed_id)
|
||||||
|
),
|
||||||
|
cte_shopping_lists AS (
|
||||||
|
SELECT
|
||||||
|
%%site_name%%_shopping_lists.*,
|
||||||
|
%%site_name%%_shopping_list_items.uuid,
|
||||||
|
%%site_name%%_shopping_list_items.item_type,
|
||||||
|
%%site_name%%_shopping_list_items.qty
|
||||||
|
FROM %%site_name%%_shopping_lists
|
||||||
|
JOIN %%site_name%%_shopping_list_items ON %%site_name%%_shopping_lists.id = %%site_name%%_shopping_list_items.sl_id
|
||||||
|
WHERE %%site_name%%_shopping_list_items.item_id = (SELECT passed_id FROM passed_id)
|
||||||
|
),
|
||||||
|
cte_itemlinks AS (
|
||||||
|
SELECT * FROM %%site_name%%_itemlinks WHERE link=(SELECT passed_id FROM passed_id)
|
||||||
|
),
|
||||||
|
cte_item_locations AS (
|
||||||
|
SELECT * FROM %%site_name%%_item_locations
|
||||||
|
LEFT JOIN %%site_name%%_locations ON %%site_name%%_locations.id = %%site_name%%_item_locations.location_id
|
||||||
|
WHERE part_id = (SELECT passed_id FROM passed_id)
|
||||||
|
),
|
||||||
|
cte_logistics_info AS (
|
||||||
|
SELECT
|
||||||
|
li.*,
|
||||||
|
row_to_json(pl) AS primary_location,
|
||||||
|
row_to_json(ail) AS auto_issue_location,
|
||||||
|
row_to_json(pz) AS primary_zone,
|
||||||
|
row_to_json(aiz) AS auto_issue_zone
|
||||||
|
FROM %%site_name%%_logistics_info AS li
|
||||||
|
LEFT JOIN %%site_name%%_locations AS pl ON li.primary_location = pl.id
|
||||||
|
LEFT JOIN %%site_name%%_locations AS ail ON li.auto_issue_location = ail.id
|
||||||
|
LEFT JOIN %%site_name%%_zones AS pz ON li.primary_zone = pz.id
|
||||||
|
LEFT JOIN %%site_name%%_zones AS aiz ON li.auto_issue_zone = aiz.id
|
||||||
|
WHERE li.id=(SELECT logistics_info_id FROM logistics_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
(SELECT passed_id FROM passed_id) AS passed_id,
|
||||||
|
%%site_name%%_items.*,
|
||||||
|
(SELECT COALESCE(row_to_json(logis), '{}') FROM cte_logistics_info logis) AS logistics_info,
|
||||||
|
row_to_json(%%site_name%%_food_info.*) as food_info,
|
||||||
|
row_to_json(%%site_name%%_brands.*) as brand,
|
||||||
|
(SELECT COALESCE(row_to_json(ii), '{}') FROM cte_item_info ii) AS item_info,
|
||||||
|
(SELECT COALESCE(array_agg(row_to_json(g)), '{}') FROM cte_groups g) AS item_groups,
|
||||||
|
(SELECT COALESCE(array_agg(row_to_json(sl)), '{}') FROM cte_shopping_lists sl) AS item_shopping_lists,
|
||||||
|
(SELECT COALESCE(array_agg(row_to_json(il)), '{}') FROM cte_itemlinks il) AS linked_items,
|
||||||
|
(SELECT COALESCE(array_agg(row_to_json(ils)), '{}') FROM cte_item_locations ils) AS item_locations
|
||||||
|
FROM %%site_name%%_items
|
||||||
|
LEFT JOIN %%site_name%%_item_info ON %%site_name%%_items.item_info_id = %%site_name%%_item_info.id
|
||||||
|
LEFT JOIN %%site_name%%_food_info ON %%site_name%%_items.food_info_id = %%site_name%%_food_info.id
|
||||||
|
LEFT JOIN %%site_name%%_brands ON %%site_name%%_items.brand = %%site_name%%_brands.id
|
||||||
|
LEFT JOIN units ON %%site_name%%_item_info.uom = units.id
|
||||||
|
LEFT JOIN cte_groups ON %%site_name%%_items.id = cte_groups.id
|
||||||
|
LEFT JOIN cte_shopping_lists ON %%site_name%%_items.id = cte_shopping_lists.id
|
||||||
|
WHERE %%site_name%%_items.id=(SELECT passed_id FROM passed_id)
|
||||||
|
GROUP BY
|
||||||
|
%%site_name%%_items.id, %%site_name%%_item_info.id, %%site_name%%_food_info.id, %%site_name%%_brands.id;
|
||||||
@ -46,4 +46,7 @@
|
|||||||
sql='UPDATE test_barcodes SET in_exchange = %s, out_exchange = %s, descriptor = %s WHERE barcode=%s RETURNING *;')
|
sql='UPDATE test_barcodes SET in_exchange = %s, out_exchange = %s, descriptor = %s WHERE barcode=%s RETURNING *;')
|
||||||
2025-08-09 11:07:03.004000 --- ERROR --- DatabaseError(message='not all arguments converted during string formatting',
|
2025-08-09 11:07:03.004000 --- ERROR --- DatabaseError(message='not all arguments converted during string formatting',
|
||||||
payload=('PLU SKU', 30, None, '8cac7ce9-dfd1-4da8-8856-57f5bdbc752b', 'Bubble Bath Lavender', 1, 1, '{"cost": 0, "expires": false}', 'Unresolved'),
|
payload=('PLU SKU', 30, None, '8cac7ce9-dfd1-4da8-8856-57f5bdbc752b', 'Bubble Bath Lavender', 1, 1, '{"cost": 0, "expires": false}', 'Unresolved'),
|
||||||
sql='INSERT INTO test_receipt_items(type, receipt_id, barcode, name, qty, uom, data, status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING *;')
|
sql='INSERT INTO test_receipt_items(type, receipt_id, barcode, name, qty, uom, data, status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING *;')
|
||||||
|
2025-08-09 11:52:23.005675 --- ERROR --- DatabaseError(message='duplicate key value violates unique constraint "test_barcodes_pkey"DETAIL: Key (barcode)=(%6589%) already exists.',
|
||||||
|
payload=('%6589%', '7bdc9602-ca87-4e9f-b9ef-fc5e8f0eceb7', 1.0, 1.0, 'Test Item 22'),
|
||||||
|
sql='INSERT INTO test_barcodes (barcode, item_uuid, in_exchange, out_exchange, descriptor) VALUES (%s, %s, %s, %s, %s) RETURNING *;')
|
||||||
Loading…
x
Reference in New Issue
Block a user