123 lines
3.1 KiB
Python
123 lines
3.1 KiB
Python
import sqlite3
|
|
import csv
|
|
import os
|
|
from uuid import uuid4
|
|
from os import path
|
|
import chemical as chem
|
|
import recipe as rec
|
|
import supply as sup
|
|
|
|
RECIPES='recipes/'
|
|
SUPPLY='supply.csv'
|
|
|
|
con = sqlite3.connect('developers.sqlite')
|
|
cursor = con.cursor()
|
|
|
|
chems = chem.Chemical(con)
|
|
recs = rec.Recipe(con)
|
|
sups = sup.Supply(con)
|
|
|
|
def uuid () :
|
|
return str(uuid4())
|
|
|
|
def createComponent (chemical, recipe_id, chemical_id, g, ml, makes, note) :
|
|
id = uuid()
|
|
query="INSERT OR IGNORE INTO components (component_id,recipe_id,chemical_id,grams,milliliters,makes,note) VALUES (?,?,?,?,?,?,?);"
|
|
cursor.execute(query, (id, recipe_id, chemical_id, g, ml, makes, note))
|
|
con.commit()
|
|
val=f"{g}g" if g != 'NULL' else f"{ml}ml"
|
|
print(f"Added component {chemical} {val}")
|
|
|
|
def importRecipe (filePath) :
|
|
name = path.basename(filePath).replace('.csv', '').replace('_', ' ')
|
|
recipe_id = recs.exists(name)
|
|
|
|
if recipe_id != '' :
|
|
print(f"Recipe {name} already exists")
|
|
return
|
|
|
|
recipe_id = recs.create(name)
|
|
|
|
with open(filePath, newline='') as csvfile:
|
|
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
|
for row in reader:
|
|
chemical=row[0]
|
|
if chemical == 'chemical' :
|
|
continue
|
|
g=float(row[1]) if row[1] != '' else 'NULL'
|
|
ml=float(row[2]) if row[2] != '' else 'NULL'
|
|
makes=int(row[3])
|
|
note=row[4]
|
|
chemical_id=chems.identify(chemical)
|
|
createComponent(chemical, recipe_id, chemical_id, g, ml, makes, note)
|
|
print(f"Imported {name}")
|
|
|
|
def displayCents (val) :
|
|
return '${:,.2f}'.format(val / 100.0)
|
|
|
|
def displayCost (ml) :
|
|
query = "SELECT recipe_id,UPPER(name) FROM recipes ORDER BY name ASC;"
|
|
cursor.execute(query)
|
|
rows = cursor.fetchall()
|
|
for row in rows:
|
|
recipe = row[1]
|
|
print(f"{recipe}: {ml/1000} liters")
|
|
query = """SELECT a.name AS name, (c.grams * (s.price/s.grams)) / c.makes AS cost, (c.grams/c.makes) FROM components AS c
|
|
INNER JOIN supply AS s
|
|
ON c.chemical_id = s.chemical_id
|
|
INNER JOIN chemicals AS a
|
|
ON c.chemical_id = a.chemical_id
|
|
WHERE c.recipe_id = ?
|
|
GROUP BY name
|
|
ORDER BY cost ASC;
|
|
"""
|
|
|
|
cursor.execute(query, (row[0],))
|
|
components = cursor.fetchall()
|
|
|
|
total = 0
|
|
for component in components:
|
|
name = component[0]
|
|
price = component[1] * ml
|
|
grams = component[2] * ml
|
|
total += price
|
|
print(f"{name} : [{grams}g] {displayCents(price)}")
|
|
print("------")
|
|
print(f"Total: {displayCents(total)}")
|
|
print("------")
|
|
|
|
print('-------')
|
|
print('SUPPLY')
|
|
print('-------')
|
|
|
|
with open(SUPPLY, newline='') as csvfile:
|
|
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
|
for row in reader:
|
|
#print(row)
|
|
c=row[0]
|
|
if c == 'chemical' :
|
|
continue
|
|
url=row[1]
|
|
g=float(row[2]) if row[2] != '' else 'NULL'
|
|
ml=float(row[3]) if row[3] != '' else 'NULL'
|
|
price=int(row[4])
|
|
chemical_id = chems.identify(c)
|
|
#problem area
|
|
supply_id = sups.identify(chemical_id, url, g, ml, price)
|
|
sups.display(supply_id, c)
|
|
#exit()
|
|
print('-------')
|
|
print('RECIPES')
|
|
print('-------')
|
|
|
|
for recipe in os.listdir(RECIPES):
|
|
if recipe.endswith('.csv') :
|
|
filePath=f'{RECIPES}{recipe}'
|
|
importRecipe(filePath)
|
|
|
|
print('----')
|
|
print('COST')
|
|
print('----')
|
|
|
|
displayCost(125) #gallon
|