|
|
@@ -4,9 +4,12 @@ class db():
|
|
|
DB = "EBA_STOCK"
|
|
|
USER = "root"
|
|
|
PASSWORD = "howdoiturnthison"
|
|
|
- def __init__(self):
|
|
|
+ def connect(self):
|
|
|
self.conn = pymysql.connect(unix_socket='/var/run/mysqld/mysqld.sock', user=db.USER, passwd=db.PASSWORD, db=db.DB,cursorclass=pymysql.cursors.DictCursor)
|
|
|
|
|
|
+ def __init__(self):
|
|
|
+ self.connect()
|
|
|
+
|
|
|
def client(self, _hash):
|
|
|
cur = self.conn.cursor()
|
|
|
cur.execute("SELECT id,nombre,apellido,hash FROM clientes where hash=%s", (_hash,))
|
|
|
@@ -14,6 +17,36 @@ class db():
|
|
|
if len(res) == 0:
|
|
|
return None
|
|
|
return res[0]
|
|
|
+
|
|
|
+
|
|
|
+ def get_pedido(self, clientid, pedidoid):
|
|
|
+ cur = self.conn.cursor()
|
|
|
+ cur.execute( "SELECT id, cant_productos, subtotal, fecha, estado FROM pedidos where cliente = %s and id = %s", (int(clientid),pedidoid) )
|
|
|
+ pedido = cur.fetchone()
|
|
|
+ pedido["fecha"] = pedido["fecha"].strftime("%Y-%m-%d %H:%M")
|
|
|
+
|
|
|
+ productos = self.products()
|
|
|
+
|
|
|
+ cur.execute( "SELECT id_producto, cantidad FROM productos_pedido where id_pedido = %s", (int(pedidoid),) )
|
|
|
+ for row in cur.fetchall():
|
|
|
+ for p in productos:
|
|
|
+ if p["id"] != row["id_producto"]:
|
|
|
+ continue
|
|
|
+ p["cantidad"] = row["cantidad"]
|
|
|
+
|
|
|
+ pedido["productos"] = productos
|
|
|
+ return pedido
|
|
|
+
|
|
|
+
|
|
|
+ def pedidos(self, clientid):
|
|
|
+ ret=[]
|
|
|
+ cur = self.conn.cursor()
|
|
|
+ cur.execute( "SELECT id, cant_productos, subtotal, fecha, estado FROM pedidos where cliente = %s", (int(clientid),) )
|
|
|
+ for row in cur.fetchall():
|
|
|
+ row["fecha"] = row["fecha"].strftime("%Y-%m-%d %H:%M")
|
|
|
+ ret.append(row)
|
|
|
+
|
|
|
+ return ret
|
|
|
|
|
|
def products(self, _id=None):
|
|
|
ret=[]
|
|
|
@@ -27,12 +60,15 @@ class db():
|
|
|
pass
|
|
|
|
|
|
return ret
|
|
|
- def pedido(self, productos,cliente):
|
|
|
+ def create_pedido(self, productos, _hash):
|
|
|
+ clienteID = int(self.client(_hash)["id"])
|
|
|
+
|
|
|
cur = self.conn.cursor()
|
|
|
subtotal = sum([ p["precio"]*p["cantidad"] for p in productos])
|
|
|
+ params = ( clienteID, sum([ p["cantidad"] for p in productos]), subtotal )
|
|
|
|
|
|
- params = (int(cliente["id"]),len(productos),subtotal)
|
|
|
- ex = cur.execute("insert into pedidos (cliente,cant_productos,subtotal,fecha) values (%s, %s, %s, NOW())", params)
|
|
|
+ #CREADO, CONFIRMADO, ARMADO, ENVIADO
|
|
|
+ ex = cur.execute("insert into pedidos (cliente,cant_productos,subtotal,fecha,estado) values (%s, %s, %s, NOW(), 'CREADO')", params)
|
|
|
_id = cur.lastrowid
|
|
|
lista_productos = [ (_id,p["id"],p["cantidad"]) for p in productos ]
|
|
|
cur.executemany("insert into productos_pedido(id_pedido,id_producto,cantidad) values (%s, %s, %s)", lista_productos)
|