Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. def bestellen(dic, customerid):
  2.     conn = pg.connect(host=HOST, database=DATABASE, user=USER, password=PW)
  3.     curs = conn.cursor()
  4.     curs.execute("BEGIN")
  5.     wheres = []
  6.     for key in dic:
  7.         wheres.append("prod_id = " + str(key) + " ")
  8.  
  9.     curs.execute("SELECT * FROM inventory WHERE " + "OR ".join(wheres) + "FOR UPDATE")
  10.  
  11.     # überprüft ob noch genug Artikel vorhanden sind
  12.     curs.execute("SELECT prod_id, quan_in_stock FROM inventory WHERE " + "OR ".join(wheres))
  13.     for x in curs.fetchall():
  14.         if not int(dic[x[0]]) <= int(x[1]):
  15.             return False
  16.  
  17.     g = 0
  18.     try:
  19.         for key in dic:
  20.             # quan_in_stock = quan_in_stock - (ausgewählte Anzahl an Produkten)
  21.             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))
  22.             # sales = sales + (ausgewählte Anzahl an Produkten)
  23.             curs.execute("UPDATE inventory SET sales = (SELECT sales FROM inventory WHERE prod_id = " + str(key) + ") + " + str(dic[key]) + " WHERE prod_id = " + str(key))
  24.  
  25.             # von jedem key * Anzahl: Preis berechnen
  26.             curs.execute("SELECT price FROM products WHERE prod_id = " + str(key))
  27.             g += curs.fetchone()[0] * dic[key]
  28.  
  29.         # nächst höhere orderid bestimmen
  30.         curs.execute("SELECT max(orderid) FROM orders")
  31.         orderid = str(int(curs.fetchone()[0]) + 1)
  32.         netamount = (float(g)/100.)*(100-TAX)
  33.         tax = (float(g)/100.)*TAX
  34.  
  35.         # new order
  36.         curs.execute("INSERT INTO orders VALUES(" + str(orderid) + ", (SELECT CURRENT_DATE), " + str(customerid) + ", " + str(netamount) + ", " + str(tax) + ", " + str(g) + ")")
  37.         # cust_hist/ orderlines erweitern
  38.         orderlineid = 1
  39.         for key in dic:
  40.             curs.execute("INSERT INTO cust_hist VALUES(" + str(customerid) + ", " + orderid + ", " + str(key) + ")")
  41.             curs.execute("INSERT INTO orderlines VALUES(" + str(orderlineid) + ", " + orderid + ", " + str(key) + ", " + str(dic[key]) + ", (SELECT CURRENT_DATE))")
  42.             orderlineid += 1
  43.         conn.commit()
  44.     except pg.DatabaseError:
  45.         conn.rollback()
  46.         bestellen(dic, customerid)
  47.     return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement