Worked on getting transactions and locations
to match up, decided on using the name and barcode instead of the ids
This commit is contained in:
parent
ef9d6f8c52
commit
df267d8eec
Binary file not shown.
99
main.py
99
main.py
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
import psycopg2
|
import psycopg2
|
||||||
from config import config
|
from config import config
|
||||||
import json, datetime, copy
|
import json, datetime, copy, csv
|
||||||
|
|
||||||
def lst2pgarr(alist):
|
def lst2pgarr(alist):
|
||||||
return '{' + ','.join(alist) + '}'
|
return '{' + ','.join(alist) + '}'
|
||||||
@ -100,20 +100,18 @@ def add_transaction(site_name, barcode, qty, user_id, transaction_type = "info",
|
|||||||
with psycopg2.connect(**database_config) as conn:
|
with psycopg2.connect(**database_config) as conn:
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(f"SELECT item_name, logistics_info_id FROM {site_name}_items WHERE barcode=%s;", (barcode, ))
|
cur.execute(f"SELECT item_name, logistics_info_id FROM {site_name}_items WHERE barcode=%s;", (barcode, ))
|
||||||
row = cur.fetchone()
|
item = cur.fetchone()
|
||||||
print(row)
|
|
||||||
|
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(f"SELECT location_data, quantity_on_hand, primary_location FROM {site_name}_logistics_info WHERE id=%s;", (row[1],))
|
cur.execute(f"SELECT location_data, quantity_on_hand, primary_location, barcode FROM {site_name}_logistics_info WHERE id=%s;", (item[1],))
|
||||||
logistics_info = cur.fetchone()
|
logistics_info = cur.fetchone()
|
||||||
print(logistics_info)
|
|
||||||
|
|
||||||
new_trans = copy.deepcopy(transaction_payload)
|
new_trans = copy.deepcopy(transaction_payload)
|
||||||
new_trans["timestamp"] = datetime.datetime.now()
|
new_trans["timestamp"] = datetime.datetime.now()
|
||||||
new_trans["logistics_info_id"] = row[1]
|
new_trans["logistics_info_id"] = item[1]
|
||||||
new_trans["barcode"] = barcode
|
new_trans["barcode"] = barcode
|
||||||
new_trans["user_id"] = user_id
|
new_trans["user_id"] = user_id
|
||||||
new_trans["name"] = row[0]
|
new_trans["name"] = item[0]
|
||||||
new_trans["transaction_type"] = transaction_type
|
new_trans["transaction_type"] = transaction_type
|
||||||
new_trans["description"] = description
|
new_trans["description"] = description
|
||||||
new_trans["quantity"] = qty
|
new_trans["quantity"] = qty
|
||||||
@ -131,11 +129,34 @@ def add_transaction(site_name, barcode, qty, user_id, transaction_type = "info",
|
|||||||
print(error)
|
print(error)
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not location:
|
if not location:
|
||||||
mover = logistics_info[2]
|
mover = logistics_info[2]
|
||||||
else:
|
else:
|
||||||
mover = location
|
mover = location
|
||||||
|
|
||||||
|
|
||||||
|
location_items = None
|
||||||
|
location_id = None
|
||||||
|
try:
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(f"SELECT id, items FROM {site_name}_locations WHERE name=%s;", (mover, ))
|
||||||
|
location = cur.fetchone()
|
||||||
|
if location:
|
||||||
|
location_id = location[0]
|
||||||
|
location_items = location[1]
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
print(error)
|
||||||
|
conn.rollback()
|
||||||
|
return False
|
||||||
|
|
||||||
|
if logistics_info[3] in location_items.keys():
|
||||||
|
location_items[logistics_info[3]] = location_items[logistics_info[3]] + qty
|
||||||
|
else:
|
||||||
|
location_items[logistics_info[3]] = qty
|
||||||
|
|
||||||
|
print(location_items)
|
||||||
|
|
||||||
if mover in logistics_info[0].keys():
|
if mover in logistics_info[0].keys():
|
||||||
logistics_info[0][mover] = logistics_info[0][mover] + qty
|
logistics_info[0][mover] = logistics_info[0][mover] + qty
|
||||||
else:
|
else:
|
||||||
@ -143,10 +164,12 @@ def add_transaction(site_name, barcode, qty, user_id, transaction_type = "info",
|
|||||||
|
|
||||||
qty = logistics_info[1] + qty
|
qty = logistics_info[1] + qty
|
||||||
|
|
||||||
|
set_location_data = f"UPDATE {site_name}_locations SET items = %s WHERE id = %s;"
|
||||||
set_quantity_on_hand = f"UPDATE {site_name}_logistics_info SET quantity_on_hand = %s, location_data = %s WHERE id = %s;"
|
set_quantity_on_hand = f"UPDATE {site_name}_logistics_info SET quantity_on_hand = %s, location_data = %s WHERE id = %s;"
|
||||||
try:
|
try:
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(set_quantity_on_hand, (qty, json.dumps(logistics_info[0]), new_trans["logistics_info_id"]))
|
cur.execute(set_quantity_on_hand, (qty, json.dumps(logistics_info[0]), new_trans["logistics_info_id"]))
|
||||||
|
cur.execute(set_location_data, (json.dumps(location_items), location_id))
|
||||||
except (Exception, psycopg2.DatabaseError) as error:
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
print(error)
|
print(error)
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
@ -156,6 +179,8 @@ def add_transaction(site_name, barcode, qty, user_id, transaction_type = "info",
|
|||||||
|
|
||||||
def add_food_item(site_name: str, barcode: str, name: str, qty: float, payload: dict):
|
def add_food_item(site_name: str, barcode: str, name: str, qty: float, payload: dict):
|
||||||
|
|
||||||
|
# TODO: I need to validate the name so that it doesnt have characters against the SQL database schema such as '
|
||||||
|
|
||||||
defaults = config(filename=f"sites/{site_name}/site.ini", section="defaults")
|
defaults = config(filename=f"sites/{site_name}/site.ini", section="defaults")
|
||||||
payload["logistics_info"]["primary_location"] = defaults["default_primary_location"]
|
payload["logistics_info"]["primary_location"] = defaults["default_primary_location"]
|
||||||
payload["logistics_info"]["auto_issue_location"] = defaults["default_auto_issue_location"]
|
payload["logistics_info"]["auto_issue_location"] = defaults["default_auto_issue_location"]
|
||||||
@ -225,6 +250,9 @@ def delete_site(site_name):
|
|||||||
drop_table(f'sites/{site_name}/sql/drop/locations.sql')
|
drop_table(f'sites/{site_name}/sql/drop/locations.sql')
|
||||||
|
|
||||||
def create_site(site_name):
|
def create_site(site_name):
|
||||||
|
|
||||||
|
site_config = config(f"sites/{site_name}/site.ini", 'defaults')
|
||||||
|
|
||||||
create_table(f'sites/{site_name}/sql/create/logins.sql')
|
create_table(f'sites/{site_name}/sql/create/logins.sql')
|
||||||
create_table(f'sites/{site_name}/sql/create/groups.sql')
|
create_table(f'sites/{site_name}/sql/create/groups.sql')
|
||||||
create_table(f'sites/{site_name}/sql/create/linked_items.sql')
|
create_table(f'sites/{site_name}/sql/create/linked_items.sql')
|
||||||
@ -237,6 +265,33 @@ def create_site(site_name):
|
|||||||
create_table(f'sites/{site_name}/sql/create/zones.sql')
|
create_table(f'sites/{site_name}/sql/create/zones.sql')
|
||||||
create_table(f'sites/{site_name}/sql/create/locations.sql')
|
create_table(f'sites/{site_name}/sql/create/locations.sql')
|
||||||
|
|
||||||
|
sql = f"INSERT INTO {site_name}_zones(name) VALUES (%s) RETURNING id;"
|
||||||
|
sqltwo = f"INSERT INTO {site_name}_locations(name, zone_id, items) VALUES (%s, %s, %s);"
|
||||||
|
|
||||||
|
database_config = config()
|
||||||
|
with psycopg2.connect(**database_config) as conn:
|
||||||
|
zone_id = None
|
||||||
|
try:
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(sql, (site_config["default_zone"], ))
|
||||||
|
rows = cur.fetchone()
|
||||||
|
if rows:
|
||||||
|
zone_id = rows[0]
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
print(error)
|
||||||
|
conn.rollback()
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(sqltwo, (site_config["default_primary_location"], zone_id, json.dumps({})))
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
print(error)
|
||||||
|
conn.rollback()
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -281,16 +336,36 @@ payload_food_item = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def parse_csv(path_to_csv):
|
||||||
|
|
||||||
|
payload = copy.deepcopy(payload_food_item)
|
||||||
|
|
||||||
|
|
||||||
|
with open(path_to_csv, "r+", encoding="utf-8") as file:
|
||||||
|
reader = csv.reader(file)
|
||||||
|
for line in reader:
|
||||||
|
if line[0] != "id":
|
||||||
|
payload["item_info"]["packaging"] = line[10]
|
||||||
|
payload["item_info"]["uom"] = line[13]
|
||||||
|
payload["item_info"]["cost"] = line[15]
|
||||||
|
if line[17] != "None":
|
||||||
|
payload["item_info"]["safety_stock"] = line[17]
|
||||||
|
qty = float(line[30])
|
||||||
|
add_food_item(site_name="test", barcode=line[1], name=line[2], qty=qty, payload=payload)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
#print(add_item(site_name="main", barcode="1235", name="testone"))
|
#print(add_readitem(site_name="main", barcode="1235", name="testone"))
|
||||||
database_config = config()
|
database_config = config()
|
||||||
sql = "SELECT * FROM main_logistics_info WHERE id=2;"
|
sql = "SELECT items FROM test_locations WHERE id=1;"
|
||||||
with psycopg2.connect(**database_config) as conn:
|
with psycopg2.connect(**database_config) as conn:
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(sql)
|
cur.execute(sql)
|
||||||
|
|
||||||
rows = cur.fetchone()
|
items = cur.fetchone()[0]
|
||||||
print(rows)
|
for k, v in items.items():
|
||||||
print(type(rows[5]))
|
print(f"{k}: {v}")
|
||||||
|
|
||||||
|
#parse_csv(r"C:\\Users\\jadow\Downloads\\2024-09-30-Pantry.csv")
|
||||||
@ -1,9 +0,0 @@
|
|||||||
[site]
|
|
||||||
site_name=test
|
|
||||||
site_owner=test
|
|
||||||
email=
|
|
||||||
|
|
||||||
[defaults]
|
|
||||||
default_zone=default
|
|
||||||
default_primary_location=all
|
|
||||||
default_auto_issue_location=all
|
|
||||||
@ -1,4 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_brands (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
name VARCHAR(255)
|
|
||||||
);
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_food_info (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
food_groups TEXT [],
|
|
||||||
ingrediants TEXT [],
|
|
||||||
nutrients JSONB,
|
|
||||||
expires BOOLEAN
|
|
||||||
);
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_groups(
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
name VARCHAR(255) NOT NULL,
|
|
||||||
description TEXT,
|
|
||||||
included_items INTEGER [],
|
|
||||||
group_type VARCHAR(255),
|
|
||||||
UNIQUE (name)
|
|
||||||
);
|
|
||||||
@ -1,27 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_items(
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
barcode VARCHAR(255) NOT NULL,
|
|
||||||
item_name VARCHAR(255) NOT NULL,
|
|
||||||
brand INTEGER,
|
|
||||||
tags TEXT [],
|
|
||||||
links TEXT [],
|
|
||||||
item_info_id INTEGER NOT NULL,
|
|
||||||
logistics_info_id INTEGER NOT NULL,
|
|
||||||
food_info_id INTEGER,
|
|
||||||
row_type VARCHAR(255) NOT NULL,
|
|
||||||
item_type VARCHAR(255) NOT NULL,
|
|
||||||
search_string TEXT NOT NULL,
|
|
||||||
UNIQUE(barcode, item_info_id),
|
|
||||||
CONSTRAINT fk_item_info
|
|
||||||
FOREIGN KEY(item_info_id)
|
|
||||||
REFERENCES test_item_info(id),
|
|
||||||
CONSTRAINT fk_food_info
|
|
||||||
FOREIGN KEY(food_info_id)
|
|
||||||
REFERENCES test_food_info(id),
|
|
||||||
CONSTRAINT fk_brand
|
|
||||||
FOREIGN KEY(brand)
|
|
||||||
REFERENCES test_brands(id),
|
|
||||||
CONSTRAINT fk_logistics_info
|
|
||||||
FOREIGN KEY(logistics_info_id)
|
|
||||||
REFERENCES test_logistics_info(id)
|
|
||||||
);
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
CREATE TABLE IF NOt EXISTS test_item_info (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
barcode VARCHAR(255) NOT NULL,
|
|
||||||
linked_items INTEGER [],
|
|
||||||
shopping_lists INTEGER [],
|
|
||||||
recipes INTEGER [],
|
|
||||||
groups INTEGER [],
|
|
||||||
packaging VARCHAR(255),
|
|
||||||
uom VARCHAR(255),
|
|
||||||
cost FLOAT8,
|
|
||||||
safety_stock FLOAT8,
|
|
||||||
lead_time_days FLOAT8,
|
|
||||||
ai_pick BOOLEAN,
|
|
||||||
UNIQUE(barcode)
|
|
||||||
);
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_itemlinks (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
barcode VARCHAR(255) NOt NULL,
|
|
||||||
link INTEGER NOT NULL,
|
|
||||||
data JSONB NOT NULL,
|
|
||||||
conv_factor FLOAT8 NOt NULL,
|
|
||||||
UNIQUE(barcode)
|
|
||||||
);
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_locations(
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
name VARCHAR(32) NOT NULL,
|
|
||||||
zone_id INTEGER NOT NULL,
|
|
||||||
items JSONB,
|
|
||||||
UNIQUE(name),
|
|
||||||
CONSTRAINT fk_zone
|
|
||||||
FOREIGN KEY(zone_id)
|
|
||||||
REFERENCES test_zones(id)
|
|
||||||
);
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS logins(
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
username VARCHAR(255),
|
|
||||||
password VARCHAR(255),
|
|
||||||
favorites JSONB,
|
|
||||||
unseen_pantry_items INTEGER [],
|
|
||||||
unseen_groups INTEGER [],
|
|
||||||
unseen_shopping_lists INTEGER [],
|
|
||||||
unseen_recipes INTEGER [],
|
|
||||||
seen_pantry_items INTEGER [],
|
|
||||||
seen_groups INTEGER[],
|
|
||||||
seen_shopping_lists INTEGER [],
|
|
||||||
seen_recipes INTEGER [],
|
|
||||||
flags JSONB
|
|
||||||
);
|
|
||||||
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_logistics_info(
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
barcode VARCHAR(255) NOT NULL,
|
|
||||||
primary_location VARCHAR(16),
|
|
||||||
auto_issue_location VARCHAR(16),
|
|
||||||
dynamic_locations JSONB,
|
|
||||||
location_data JSONB,
|
|
||||||
quantity_on_hand FLOAT8 NOT NULL,
|
|
||||||
UNIQUE(barcode)
|
|
||||||
);
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_Transactions (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
timestamp TIMESTAMP,
|
|
||||||
logistics_info_id INTEGER NOT NULL,
|
|
||||||
barcode VARCHAR(255) NOT NULL,
|
|
||||||
name VARCHAR(255),
|
|
||||||
transaction_type VARCHAR(255) NOT NULL,
|
|
||||||
quantity FLOAT8 NOT NULL,
|
|
||||||
description TEXT,
|
|
||||||
user_id INTEGER NOT NULL,
|
|
||||||
data JSONB,
|
|
||||||
CONSTRAINT fk_logistics_info
|
|
||||||
FOREIGN KEY(logistics_info_id)
|
|
||||||
REFERENCES test_logistics_info(id)
|
|
||||||
);
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS test_zones(
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
name VARCHAR(32) NOT NULL,
|
|
||||||
UNIQUE(name)
|
|
||||||
);
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_brands CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_food_info CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_groups CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_item_info CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_items CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_itemlinks CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_locations CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_logistics_info CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_transactions CASCADE;
|
|
||||||
@ -1 +0,0 @@
|
|||||||
DROP TABLE test_zones CASCADE;
|
|
||||||
Loading…
x
Reference in New Issue
Block a user