Jelajahi Sumber

activo 'hacer pedido'

David 10 tahun lalu
induk
melakukan
eccd85f6dd
5 mengubah file dengan 51 tambahan dan 22 penghapusan
  1. 10 1
      back/db.py
  2. 12 4
      back/web.py
  3. 24 14
      controllers/facturaCtrl.js
  4. 2 0
      css/style.css
  5. 3 3
      views/main.html

+ 10 - 1
back/db.py

@@ -27,5 +27,14 @@ class db():
 			pass
 
 		return ret
-	def pedido(self, pedido):
+	def pedido(self, productos,cliente):
+		cur = self.conn.cursor()
+		subtotal = sum([ p["precio"]*p["cantidad"] for p in productos])
+
+		params = (int(cliente["id"]),len(productos),subtotal)
+		ex  = cur.execute("insert into pedidos (cliente,cant_productos,subtotal,fecha) values (%s, %s, %s, NOW())", 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

+ 12 - 4
back/web.py

@@ -8,26 +8,34 @@ from flask import Flask, jsonify, request
 app = Flask(__name__)
 PORT = 8981
 BASE_PATH="/back" #changes based on webserver
+
+d = db()
 @app.route(BASE_PATH + '/cliente/<_hash>')
 def client(_hash):
-	out=db().client(_hash)
+	out=d.client(_hash)
 	if out is None:
 		return "{}", 400
 	return json.dumps(out)
 
 @app.route(BASE_PATH + '/productos/<_hash>')
 def products(_hash):
-	c=db().client(_hash)
+	c=d.client(_hash)
 	if c is None:
 		return "{}", 400
 
-	out=db().products()
+	out=d.products()
 	return json.dumps(out)
 
 @app.route(BASE_PATH + '/pedido', methods=["POST"])
 def pedido():
-	t = datetime.date.now()
+	t = datetime.datetime.now()
 	r = request.get_json()
+	productos = r["pedido"]["productos"]
+	cliente = r["pedido"]["cliente"]
+
+	d.pedido(productos,cliente)
+
+	return "{}"
 
 
 if __name__ == '__main__':

+ 24 - 14
controllers/facturaCtrl.js

@@ -13,20 +13,6 @@ app.controller('facturaCtrl', function ($scope, $http, $window, notify,$location
 		});
 	};
 
-	$scope.fin = function () {
-		var factura = {};
-		if ($scope.productos.length==0){
-			notify({message:"No hay productos",classes:"alert alert-danger"});
-			return;
-		}
-		$http.post('back/pedido', { factura: factura } )
-	        .success(function(data, status, headers, config) {
-				notify("Guardada correctamente");
-	        })
-	        .error(function(data, status, headers, config) {
-				notify({classes:"alert alert-danger", message:"Error: "+ data.err});
-	        });
-	}
 	$scope.filterCero = function(criteria) {
 		return function(item) {
 			if ($scope.cero)
@@ -47,7 +33,31 @@ app.controller('facturaCtrl', function ($scope, $http, $window, notify,$location
 		$scope.update();
 	};
 	$scope.ok = function () {
+		var pedido = {};
+		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;
+			}
+		}
+
+		pedido.productos = filtrados;
+		pedido.cliente = $scope.userdata;
+
+		$http.post('back/pedido', { pedido: pedido } )
+	        .success(function(data, status, headers, config) {
+				notify("Guardada correctamente");
+	        })
+	        .error(function(data, status, headers, config) {
+				notify({classes:"alert alert-danger", message:"Error: "+ data.err});
+	        });
 	};
 
 	$scope.reset();

+ 2 - 0
css/style.css

@@ -164,11 +164,13 @@ text-align: center;
 
 .css-button:hover {
     background-color: #56688C;
+	color:white;
 }
 
 .css-button:active {
     position:relative;
     top: 1px;
+	color:white;
 }
 
 .factura-datos{

+ 3 - 3
views/main.html

@@ -14,7 +14,7 @@
 	</div>
 </div>
 
-<div class="col-md-6">
+<div class="col-md-6 col-sm-12 col-xs-12">
 	<table class="table table-striped">
 		<tr>
 			<th>Cantidad</th>
@@ -32,7 +32,7 @@
 		</tr>
 	</table>
 </div>
-<div class="col-md-6">
+<div class="col-md-6 col-sm-12 col-xs-12">
 	<div class="row">
 		<div class="col-md-12">
 		<label>
@@ -49,7 +49,7 @@
 	</div>
 	<div class="row">
 		<div class="col-md-12">
-			<button class="btn css-button" ng-click="ok()">Enviar</button>
+			<button class="css-button" ng-click="ok()">Enviar</button>
 		</div>
 	</div>
 </div>