| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import pymysql
- class db():
- DB = "EBA_STOCK"
- USER = "root"
- PASSWORD = "howdoiturnthison"
- 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,))
- res = cur.fetchall()
- 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=[]
- if _id is None:
- cur = self.conn.cursor()
- cur.execute("SELECT id,nombre,precio FROM productos where id between 1000 and 2999")
- for row in cur.fetchall():
- row["cantidad"]=0
- ret.append(row)
- else:
- pass
- return ret
- 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 )
- #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)
- self.conn.commit()
- return 0
|