Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def bestellen(dic, customerid):
- conn = pg.connect(host=HOST, database=DATABASE, user=USER, password=PW)
- curs = conn.cursor()
- curs.execute("BEGIN")
- wheres = []
- for key in dic:
- wheres.append("prod_id = " + str(key) + " ")
- curs.execute("SELECT * FROM inventory WHERE " + "OR ".join(wheres) + "FOR UPDATE")
- # überprüft ob noch genug Artikel vorhanden sind
- curs.execute("SELECT prod_id, quan_in_stock FROM inventory WHERE " + "OR ".join(wheres))
- for x in curs.fetchall():
- if not int(dic[x[0]]) <= int(x[1]):
- return False
- g = 0
- try:
- for key in dic:
- # quan_in_stock = quan_in_stock - (ausgewählte Anzahl an Produkten)
- curs.execute("UPDATE inventory SET quan_in_stock = (SELECT quan_in_stock FROM inventory WHERE prod_id = " + str(key) + ") - " + str(dic[key]) + " WHERE prod_id = " + str(key))
- # sales = sales + (ausgewählte Anzahl an Produkten)
- curs.execute("UPDATE inventory SET sales = (SELECT sales FROM inventory WHERE prod_id = " + str(key) + ") + " + str(dic[key]) + " WHERE prod_id = " + str(key))
- # von jedem key * Anzahl: Preis berechnen
- curs.execute("SELECT price FROM products WHERE prod_id = " + str(key))
- g += curs.fetchone()[0] * dic[key]
- # nächst höhere orderid bestimmen
- curs.execute("SELECT max(orderid) FROM orders")
- orderid = str(int(curs.fetchone()[0]) + 1)
- netamount = (float(g)/100.)*(100-TAX)
- tax = (float(g)/100.)*TAX
- # new order
- curs.execute("INSERT INTO orders VALUES(" + str(orderid) + ", (SELECT CURRENT_DATE), " + str(customerid) + ", " + str(netamount) + ", " + str(tax) + ", " + str(g) + ")")
- # cust_hist/ orderlines erweitern
- orderlineid = 1
- for key in dic:
- curs.execute("INSERT INTO cust_hist VALUES(" + str(customerid) + ", " + orderid + ", " + str(key) + ")")
- curs.execute("INSERT INTO orderlines VALUES(" + str(orderlineid) + ", " + orderid + ", " + str(key) + ", " + str(dic[key]) + ", (SELECT CURRENT_DATE))")
- orderlineid += 1
- conn.commit()
- except pg.DatabaseError:
- conn.rollback()
- bestellen(dic, customerid)
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement