Explorar o código

bajo stock + no stock + css

David %!s(int64=9) %!d(string=hai) anos
pai
achega
fa6a715cdc

+ 9 - 5
back/db.py

@@ -8,7 +8,6 @@ class db():
 	def get_cursor(self):
 		if not self.conn.open:
 			self.connect()
-		
 		try:
 			cur = self.conn.cursor()
 		except:
@@ -27,6 +26,7 @@ class db():
 		cur = self.get_cursor()
 		cur.execute("SELECT id,nombre,apellido,hash FROM clientes where hash=%s", (_hash,))
 		res = cur.fetchall()
+		cur.close()
 		if len(res) == 0:
 			return None
 		return res[0]
@@ -48,6 +48,7 @@ class db():
 				p["cantidad"] = row["cantidad"]
 
 		pedido["productos"] = productos
+		cur.close()
 		return pedido
 
 
@@ -59,19 +60,19 @@ class db():
 			row["fecha"] = row["fecha"].isoformat() #strftime("%Y-%m-%d %H:%M")
 			ret.append(row)
 
+		cur.close()
 		return ret
 
 	def products(self, _id=None):
 		ret=[]
 		if _id is None:
 			cur = self.get_cursor()
-			cur.execute("SELECT id,nombre,precio FROM productos where id between 1000 and 2999")
+			cur.execute("SELECT id, nombre, precio, stock > 0 as disponible, stock < 25 as low, stock FROM productos where id between 1000 and 2999 and id not in (1019, 1014, 2014, 1015, 2015, 1047, 2038, 2030)")
 			for row in cur.fetchall():
 				row["cantidad"]=0
 				ret.append(row)
-		else:
-			pass
 
+		cur.close()
 		return ret
 
 	def update_pedido(self, productos, _hash, obs, id_pedido):
@@ -92,6 +93,7 @@ class db():
 		lista_productos = [ (id_pedido,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()
+		cur.close()
 
 	def create_pedido(self, productos, _hash, obs):
 		clienteID = int(self.client(_hash)["id"])
@@ -101,14 +103,16 @@ class db():
 		params = ( clienteID, sum([ p["cantidad"] for p in productos]), subtotal, obs )
 
 		#CREADO, CONFIRMADO, ARMADO, ENVIADO
-		ex  = cur.execute("insert into pedidos (cliente,cant_productos,subtotal,fecha,estado) values (%s, %s, %s, %s, NOW(), 'CREADO')", params)
+		ex  = cur.execute("insert into pedidos (cliente, cant_productos, subtotal, observaciones, fecha, estado) values (%s, %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()
+		cur.close()
 	
 	def confirm(self, _hash, _id):
 		c = self.client(_hash)
 		cur = self.get_cursor()
 		cur.execute("UPDATE pedidos set estado = 'CONFIRMADO', fecha=NOW() where id = %s and cliente = %s and estado = 'CREADO'", (_id,c["id"]))
 		self.conn.commit()
+		cur.close()

+ 25 - 1
controllers/createPedidoCtrl.js

@@ -8,10 +8,27 @@ app.controller('createPedidoCtrl', function ($scope, $http, $routeParams, notify
 		$scope.cero=true;
 	}
 	$scope.confirmar = function() {
+		var filtrados = $scope.listaproductos.filter(function(el) { return el.cantidad > 0; });
+
+		if (filtrados.length==0){
+			notify({message:"No hay productos",classes:"alert alert-danger"});
+			return;
+		}
+		for(var f in $scope.listaproductos){
+			var prod = $scope.listaproductos[f];
+			if(isNaN(prod.cantidad)){
+				notify({message:prod.nombre+" tiene cantidad incorrecta",classes:"alert alert-danger"});
+				return;
+			} else if (prod.cantidad > prod.stock) {
+				notify({message:"La cantidad pedida de " + prod.nombre+" es mayor al stock actual (" + prod.stock  + ")",classes:"alert alert-danger"});
+				return;
+			}
+		}
+
 		$scope.cero=false;
 	};
 	$scope.update = function() {
-		$http.get('back/cliente/' + $routeParams.hash).success(function(data) {
+		$http.get('back/cliente/' + $routeParams.hash).then(function(data) {
 	   		$scope.userdata = data.data; 
 		});
 
@@ -35,7 +52,10 @@ app.controller('createPedidoCtrl', function ($scope, $http, $routeParams, notify
 		if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
             e.preventDefault();
         }
+		if ( this.p.cantidad >= 10 )
+            e.preventDefault();
 	};
+
 	$scope.updateTotal = function() {
 		var total=0;
 		total=$scope.listaproductos.reduce(
@@ -55,11 +75,15 @@ app.controller('createPedidoCtrl', function ($scope, $http, $routeParams, notify
 			notify({message:"No hay productos",classes:"alert alert-danger"});
 			return;
 		}
+
 		for(var f in $scope.listaproductos){
 			var prod = $scope.listaproductos[f];
 			if(isNaN(prod.cantidad)){
 				notify({message:prod.nombre+" tiene cantidad incorrecta",classes:"alert alert-danger"});
 				return;
+			} else if (prod.cantidad > prod.stock) {
+				notify({message:"La cantidad pedida de " + prod.nombre+" es mayor al stock actual (" + prod.stock  + ")",classes:"alert alert-danger"});
+				return;
 			}
 		}
 

+ 21 - 0
controllers/editPedidoCtrl.js

@@ -9,6 +9,22 @@ app.controller('editPedidoCtrl', function ($scope, $http, $routeParams, notify,
 	}
 
 	$scope.confirmar = function() {
+		var filtrados = $scope.listaproductos.filter(function(el) { return el.cantidad > 0; });
+
+		if (filtrados.length==0){
+			notify({message:"No hay productos",classes:"alert alert-danger"});
+			return;
+		}
+		for(var f in $scope.listaproductos){
+			var prod = $scope.listaproductos[f];
+			if(isNaN(prod.cantidad)){
+				notify({message:prod.nombre+" tiene cantidad incorrecta",classes:"alert alert-danger"});
+				return;
+			} else if (prod.cantidad > prod.stock) {
+				notify({message:"La cantidad pedida de " + prod.nombre+" es mayor al stock actual (" + prod.stock  + ")",classes:"alert alert-danger"});
+				return;
+			}
+		}
 		$scope.cero=false;
 	};
 
@@ -42,6 +58,8 @@ app.controller('editPedidoCtrl', function ($scope, $http, $routeParams, notify,
 		if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
             e.preventDefault();
         }
+		if ( this.p.cantidad >= 10 )
+            e.preventDefault();
 	};
 
 	$scope.updateTotal = function() {
@@ -69,6 +87,9 @@ app.controller('editPedidoCtrl', function ($scope, $http, $routeParams, notify,
 			if(isNaN(prod.cantidad)){
 				notify({message:prod.nombre+" tiene cantidad incorrecta",classes:"alert alert-danger"});
 				return;
+			} else if (prod.cantidad > prod.stock) {
+				notify({message:"La cantidad pedida de " + prod.nombre+" es mayor al stock actual (" + prod.stock  + ")",classes:"alert alert-danger"});
+				return;
 			}
 		}
 

+ 3 - 0
css/style.css

@@ -5,6 +5,9 @@ body,html{
 	font-family: Century Gothic, sans-serif;
 }
 
+.no-disponible { color: #ccc; }
+.low { color: #c22;display:block;font-size:smaller; }
+
 .modal-content { position:fixed !important; left: 5%; right: 5%; width: 90%; top: 5%; height: 90%; }
 .modal-content > .ng-scope { background-color:white; }
 .column { width:50%; float:left;}

+ 8 - 4
views/createOrEditPedido.html

@@ -23,14 +23,18 @@
 			<th>Precio</th>
 			<th>Total</th>
 		</tr>
-		<tr ng-repeat="p in listaproductos | filter: filtro | filter:filterCero(criteria) track by p.id">
+		<tr ng-repeat="p in listaproductos | filter: filtro | filter:filterCero(criteria) track by p.id" ng-class="{'no-disponible': p.disponible==0}">
 			<td>
-				<input ng-keydown="validateKeypress($event)" ng-change="updateTotal(this)" type="number" onclick="this.select();" class="form-control" ng-model="p.cantidad" value="0" min="0" step="1"
-					ng-disabled="!cero"
+				<input ng-keydown="validateKeypress($event)" ng-change="updateTotal(this)" type="number" onclick="this.select();" class="form-control" ng-model="p.cantidad" value="0" min="0" max="100" max-length="2" step="1"
+					ng-disabled="!cero || p.disponible==0"
 					style='width:70px;text-align:center;'>
 			</td>
 			<td>{{p.id}}</td>
-			<td>{{p.nombre}}</td>
+			<td>
+				{{p.nombre}}
+				<div class="low" ng-if="p.disponible==0">Este producto no est&aacute; en stock</div>
+				<div class="low" ng-if="p.low==1 && p.disponible==1">Este producto tiene bajo stock, confirmar el pedido</div>
+			</td>
 			<td>${{p.precio}}</td>
 			<td>${{p.cantidad*p.precio}}</td>
 		</tr>

+ 3 - 3
views/main.html

@@ -4,12 +4,12 @@
 
 <div ng-if="!error">
 	<div class="row">
-		<div class="col-md-3 col-md-offset-3">
+		<div class="col-md-3 col-md-offset-2">
 			<label>
-				<h3>Pedido {{userdata.nombre}} {{userdata.apellido}}</h3>
+				<h3>Pedidos de {{userdata.nombre}} {{userdata.apellido}}</h3>
 			</label>
 		</div>
-		<div class="col-md-3">
+		<div class="col-md-3 pull-right">
 			<a ng-href="/create/{{userdata.hash}}">
 				<button class="btn">Generar Pedido</button>
 			</a>