David vor 9 Jahren
Ursprung
Commit
d8d9b5082f
9 geänderte Dateien mit 290 neuen und 130 gelöschten Zeilen
  1. 9 1
      app.js
  2. 40 4
      back/db.py
  3. 10 1
      back/web.py
  4. 69 0
      controllers/createPedidoCtrl.js
  5. 73 0
      controllers/editPedidoCtrl.js
  6. 13 65
      controllers/facturaCtrl.js
  7. 3 18
      index.html
  8. 56 0
      views/createOrEditPedido.html
  9. 17 41
      views/main.html

+ 9 - 1
app.js

@@ -3,10 +3,18 @@ var app = angular.module('app', ['ngRoute', 'ui.bootstrap', 'cgNotify']);
 app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
 	$locationProvider.html5Mode(true);
 	$routeProvider
-	.when('/', {
+	.when('/:hash', {
 		templateUrl: 'views/main.html',
 		controller: 'facturaCtrl'
 	})
+	.when('/create/:hash', {
+		templateUrl: 'views/createOrEditPedido.html',
+		controller: 'createPedidoCtrl'
+	})
+	.when('/edit/:hash/:id', {
+		templateUrl: 'views/createOrEditPedido.html',
+		controller: 'editPedidoCtrl'
+	})
 	.otherwise({
 		redirectTo: '/'
 	});

+ 40 - 4
back/db.py

@@ -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)

+ 10 - 1
back/web.py

@@ -13,6 +13,15 @@ d = db()
 @app.route(BASE_PATH + '/cliente/<_hash>')
 def client(_hash):
 	out=d.client(_hash)
+	if out is None:
+		return "{}", 400
+	out["pedidos"] = d.pedidos(int(out["id"]))
+	return json.dumps(out)
+
+@app.route(BASE_PATH + '/pedido/<_hash>/<pedidoid>')
+def get_pedido(_hash, pedidoid):
+	c = d.client(_hash)
+	out = d.get_pedido(c["id"], pedidoid)
 	if out is None:
 		return "{}", 400
 	return json.dumps(out)
@@ -33,7 +42,7 @@ def pedido():
 	productos = r["pedido"]["productos"]
 	cliente = r["pedido"]["cliente"]
 
-	d.pedido(productos,cliente)
+	d.create_pedido(productos,cliente)
 
 	return "{}"
 

+ 69 - 0
controllers/createPedidoCtrl.js

@@ -0,0 +1,69 @@
+app.controller('createPedidoCtrl', function ($scope, $http, $routeParams, notify, $location, $timeout) {
+	$scope.cero=true;
+	$scope.total=0;
+
+	$scope.modificar = function() {
+		$scope.cero=true;
+	}
+	$scope.confirmar = function() {
+		$scope.cero=false;
+	};
+	$scope.update = function() {
+		$http.get('back/productos/' + $routeParams.hash).success(function(data) {
+	   		$scope.listaproductos = data; 
+		}).error(function(data) {
+		});
+	};
+
+	$scope.filterCero = function(criteria) {
+		return function(item) {
+			if ($scope.cero)
+				return true;
+			return item.cantidad > 0;
+		};
+		
+	};
+	$scope.updateTotal = function() {
+		var total=0;
+		total=$scope.listaproductos.reduce(
+			function(acc,cur) {
+				return acc+(cur.cantidad*cur.precio)
+			},0);
+		$scope.total=total;
+	};
+	$scope.reset = function() {
+		$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 = $routeParams.hash;
+
+		$http.post('back/pedido', { pedido: pedido } )
+	        .success(function(data, status, headers, config) {
+				notify("Enviado correctamente. En unos minutos recibiras un e-mail de confirmacion");
+				$timeout(function() { $location.url("/"+$routeParams.hash) }, 2000);
+	        })
+	        .error(function(data, status, headers, config) {
+				notify({classes:"alert alert-danger", message:"Error: "+ data.err});
+	        });
+	};
+
+	$scope.reset();
+
+});
+

+ 73 - 0
controllers/editPedidoCtrl.js

@@ -0,0 +1,73 @@
+app.controller('editPedidoCtrl', function ($scope, $http, $routeParams, notify, $location, $timeout) {
+	$scope.cero=true;
+	$scope.total=0;
+
+	$scope.modificar = function() {
+		$scope.cero=true;
+	}
+
+	$scope.confirmar = function() {
+		$scope.cero=false;
+	};
+
+	$scope.update = function() {
+		$http.get('back/pedido/'+$routeParams.hash+'/'+$routeParams.id).then(
+		function(data) {
+	   		$scope.listaproductos = data.data.productos; 
+		},
+		function(data) {
+		});
+	};
+
+	$scope.filterCero = function(criteria) {
+		return function(item) {
+			if ($scope.cero)
+				return true;
+			return item.cantidad > 0;
+		};
+		
+	};
+	$scope.updateTotal = function() {
+		var total=0;
+		total=$scope.listaproductos.reduce(
+			function(acc,cur) {
+				return acc+(cur.cantidad*cur.precio)
+			},0);
+		$scope.total=total;
+	};
+	$scope.reset = function() {
+		$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 = $routeParams.hash;
+
+		$http.post('back/pedido', { pedido: pedido } )
+	        .success(function(data, status, headers, config) {
+				notify("Enviado correctamente. En unos minutos recibiras un e-mail de confirmacion");
+				$timeout(function() { $location.url("/"+$routeParams.hash) }, 2000);
+	        })
+	        .error(function(data, status, headers, config) {
+				notify({classes:"alert alert-danger", message:"Error: "+ data.err});
+	        });
+	};
+
+	$scope.reset();
+
+});
+

+ 13 - 65
controllers/facturaCtrl.js

@@ -1,78 +1,26 @@
-app.controller('facturaCtrl', function ($scope, $http, $window, notify,$location) {
-	$scope.error=false;
-	$scope.cero=true;
-	$scope.total=0;
-	var user = $location.search().user;
+app.controller('facturaCtrl', function ($scope, $http, $routeParams) {
+	$scope.pedidos=[];
+	var user = $routeParams.hash;
 	if (user == undefined)
 		user="";
-	$scope.modificar = function() {
-		$scope.cero=true;
-	}
-	$scope.confirmar = function() {
-		$scope.cero=false;
-	};
+
 	$scope.update = function() {
 		$http.get('back/cliente/' + user).success(function(data) {
 	   		$scope.userdata = data; 
-		}).error(function(data) {
-			$scope.error=true;
-		});
+			$scope.pedidos = data.pedidos.map(function(e) {
+				e.fecha = new Date(e.fecha);
+				return e;
+			});
+
+			$scope.pedidos.sort(function(a,b){
+				return b.fecha - a.fecha;
+			});
 
-		$http.get('back/productos/' + user).success(function(data) {
-	   		$scope.listaproductos = data; 
 		}).error(function(data) {
 			$scope.error=true;
 		});
 	};
 
-	$scope.filterCero = function(criteria) {
-		return function(item) {
-			if ($scope.cero)
-				return true;
-			return item.cantidad > 0;
-		};
-		
-	};
-	$scope.updateTotal = function() {
-		var total=0;
-		total=$scope.listaproductos.reduce(
-			function(acc,cur) {
-				return acc+(cur.cantidad*cur.precio)
-			},0);
-		$scope.total=total;
-	};
-	$scope.reset = function() {
-		$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("Enviado correctamente. En unos minutos recibiras un e-mail de confirmacion");
-	        })
-	        .error(function(data, status, headers, config) {
-				notify({classes:"alert alert-danger", message:"Error: "+ data.err});
-	        });
-	};
-
-	$scope.reset();
-
+	$scope.update();
 });
 

+ 3 - 18
index.html

@@ -15,29 +15,14 @@
 	<script type="text/javascript" src="/js/angular-notify/dist/angular-notify.js"></script>
 	<script type="text/javascript" src="/app.js"></script>
 	<script type="text/javascript" src="/controllers/facturaCtrl.js"></script>
+	<script type="text/javascript" src="/controllers/createPedidoCtrl.js"></script>
+	<script type="text/javascript" src="/controllers/editPedidoCtrl.js"></script>
     <base href="/" />
 </head>
 <body>
 	<div class="navbar">
 		<div class="nav-wrap">
-				<img src="images/logo-h.png">
-				<!--
-				<ul class="menu" ng-hide="isHome">
-					<li><a ng-href="/lista_prod_fact/">prod fecha</a></li>
-					<li><a ng-href="/vencimientos/">vencimientos</a></li>
-					<li><a ng-href="/scan/">scan</a></li>
-					<li><a ng-href="/stock">faltantes</a></li>
-					<li><a ng-href="/camaras/">camaras</a></li>
-					<li><a ng-href="/resumen/">resumen</a></li>
-					<li><a ng-href="/mp/">mp</a></li>
-					<li><a ng-href="/cc/">cc</a></li>
-					<li><a ng-href="/clientes/">clientes</a></li>
-					<li><a ng-href="/insumos/">insumos</a></li>
-					<li><a ng-href="/productos/">stock</a></li>
-					<li><a ng-href="/">factura</a></li>
-					<li><a ng-href="/lista_facturas/">lista de facturas</a></li>
-				</ul>
-				-->
+			<img src="images/logo-h.png">
 		</div>
 	</div>
 	<div class="clear"></div>

+ 56 - 0
views/createOrEditPedido.html

@@ -0,0 +1,56 @@
+<div class="row">
+	<div class="col-md-12">
+		<label>
+			<h3>Pedido {{userdata.nombre}} {{userdata.apellido}}</h3>
+		</label>
+	</div>
+</div>
+<div class="row">
+	<div class="col-md-6">
+		<label class="col-md-12">
+			Filtro:
+			<input type="text" class="form-control form-control-small" ng-model="filtro" style="display:block">
+		</label>
+	</div>
+</div>
+
+<div class="col-md-6 col-sm-12 col-xs-12">
+	<table class="table table-striped">
+		<tr>
+			<th>Cantidad</th>
+			<th>ID</th>
+			<th>Nombre</th>
+			<th>Precio</th>
+			<th>Total</th>
+		</tr>
+		<tr ng-repeat="p in listaproductos | filter: filtro | filter:filterCero(criteria) track by p.id">
+			<td>
+				<input ng-change="updateTotal()" type="number" onclick="this.select();" class="form-control" ng-model="p.cantidad" value="0" min="0" max="100"
+					ng-disabled="!cero"
+					style='width:70px;text-align:center;'>
+			</td>
+			<td>{{p.id}}</td>
+			<td>{{p.nombre}}</td>
+			<td>${{p.precio}}</td>
+			<td>${{p.cantidad*p.precio}}</td>
+		</tr>
+	</table>
+</div>
+<div class="col-md-6 col-sm-12 col-xs-12">
+	<div class="row">
+		<div class="col-md-12">
+			<b>Total sin descuento:</b>
+			${{total}}
+		</div>
+	</div>
+	<div class="row">
+		<div class="col-md-12">
+			<button ng-show="cero==true" class="css-button" ng-click="confirmar()">Confirmar</button>
+			<button ng-show="cero==false" class="css-button" ng-click="modificar()">Modificar</button>
+			<!--
+			<button class="css-button" ng-click="">Ver pedido</button>
+			-->
+			<button ng-show="cero==false" class="css-button" ng-click="ok()">Enviar Pedido</button>
+		</div>
+	</div>
+</div>

+ 17 - 41
views/main.html

@@ -4,59 +4,35 @@
 
 <div ng-if="!error">
 	<div class="row">
-		<div class="col-md-12">
+		<div class="col-md-3 col-md-offset-3">
 			<label>
 				<h3>Pedido {{userdata.nombre}} {{userdata.apellido}}</h3>
 			</label>
 		</div>
-	</div>
-	<div class="row">
-		<div class="col-md-6">
-			<label class="col-md-12">
-				Filtro:
-				<input type="text" class="form-control form-control-small" ng-model="filtro" style="display:block">
-			</label>
+		<div class="col-md-3">
+			<a ng-href="/create/{{userdata.hash}}">
+				<button class="btn">Crear</button>
+			</a>
 		</div>
 	</div>
-	
-	<div class="col-md-6 col-sm-12 col-xs-12">
+
+	<div class="col-md-6 col-md-offset-3 col-sm-12 col-xs-12">
 		<table class="table table-striped">
 			<tr>
-				<th>Cantidad</th>
-				<th>ID</th>
-				<th>Nombre</th>
-				<th>Precio</th>
-				<th>Total</th>
+				<th>Fecha</th>
+				<th># Productos</th>
+				<th>subtotal</th>
+				<th>Estado</th>
 			</tr>
-			<tr ng-repeat="p in listaproductos | filter: filtro | filter:filterCero(criteria) track by p.id">
+			<tr ng-repeat="p in pedidos">
+				<td>{{p.fecha | date: 'dd/MM/yyyy HH:mm'}}</td>
+				<td>{{p.cant_productos}}</td>
+				<td>${{p.subtotal}}</td>
 				<td>
-					<input ng-change="updateTotal()" type="number" onclick="this.select();" class="form-control" ng-model="p.cantidad" value="0" min="0" max="100"
-						ng-disabled="!cero"
-						style='width:70px;text-align:center;'>
+					<a ng-if="p.estado==='CREADO'" href="#" ng-href="edit/{{userdata.hash}}/{{p.id}}">Editar/Confirmar</a>
+					<p ng-if="p.estado!=='CREADO'">{{p.estado}}</p>
 				</td>
-				<td>{{p.id}}</td>
-				<td>{{p.nombre}}</td>
-				<td>${{p.precio}}</td>
-				<td>${{p.cantidad*p.precio}}</td>
 			</tr>
 		</table>
 	</div>
-	<div class="col-md-6 col-sm-12 col-xs-12">
-		<div class="row">
-			<div class="col-md-12">
-				<b>Total sin descuento:</b>
-				${{total}}
-			</div>
-		</div>
-		<div class="row">
-			<div class="col-md-12">
-				<button ng-show="cero==true" class="css-button" ng-click="confirmar()">Confirmar</button>
-				<button ng-show="cero==false" class="css-button" ng-click="modificar()">Modificar</button>
-				<!--
-				<button class="css-button" ng-click="">Ver pedido</button>
-				-->
-				<button ng-show="cero==false" class="css-button" ng-click="ok()">Enviar Pedido</button>
-			</div>
-		</div>
-	</div>
 </div>