Bläddra i källkod

error (invalid userid), 'confirm' modal, edit stuff

David 9 år sedan
förälder
incheckning
d7f2d50e99
10 ändrade filer med 100 tillägg och 11 borttagningar
  1. 4 0
      app.js
  2. 22 2
      back/db.py
  3. 4 1
      back/web.py
  4. 5 1
      controllers/editPedidoCtrl.js
  5. 4 0
      controllers/errorCtrl.js
  6. 31 3
      controllers/facturaCtrl.js
  7. 2 1
      index.html
  8. 1 1
      views/createOrEditPedido.html
  9. 8 0
      views/error.html
  10. 19 2
      views/main.html

+ 4 - 0
app.js

@@ -3,6 +3,10 @@ var app = angular.module('app', ['ngRoute', 'ui.bootstrap', 'cgNotify']);
 app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
 	$locationProvider.html5Mode(true);
 	$routeProvider
+	.when('/', {
+		templateUrl: 'views/error.html',
+		controller: 'errorCtrl'
+	})
 	.when('/:hash', {
 		templateUrl: 'views/main.html',
 		controller: 'facturaCtrl'

+ 22 - 2
back/db.py

@@ -23,7 +23,7 @@ class db():
 		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")
+		pedido["fecha"] = pedido["fecha"].isoformat() #strftime("%Y-%m-%d %H:%M")
 
 		productos = self.products()
 
@@ -43,7 +43,7 @@ class db():
 		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")
+			row["fecha"] = row["fecha"].isoformat() #strftime("%Y-%m-%d %H:%M")
 			ret.append(row)
 
 		return ret
@@ -60,6 +60,26 @@ class db():
 			pass
 
 		return ret
+
+	def update_pedido(self, productos, _hash, id_pedido):
+		clienteID = int(self.client(_hash)["id"])
+		cur = self.conn.cursor()
+
+		cur.execute( "SELECT 1 FROM pedidos where id = %s and cliente = %s", (id_pedido, clienteID) )
+		if not cur.fetchone():
+			print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
+			return	
+
+		cur.execute( "DELETE FROM productos_pedido WHERE id_pedido = %s", (id_pedido,) )
+		subtotal = sum([ p["precio"]*p["cantidad"] for p in productos])
+		cantidad = sum([ p["cantidad"] for p in productos])
+
+		cur.execute("UPDATE pedidos SET cant_productos = %s, subtotal = %s, fecha=NOW() WHERE id = %s", ( cantidad, subtotal, id_pedido ))
+
+		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()
+
 	def create_pedido(self, productos, _hash):
 		clienteID = int(self.client(_hash)["id"])
 

+ 4 - 1
back/web.py

@@ -42,7 +42,10 @@ def pedido():
 	productos = r["pedido"]["productos"]
 	cliente = r["pedido"]["cliente"]
 
-	d.create_pedido(productos,cliente)
+	if "id" in r["pedido"]:
+		d.update_pedido(productos,cliente, r["pedido"]["id"])
+	else:
+		d.create_pedido(productos,cliente)
 
 	return "{}"
 

+ 5 - 1
controllers/editPedidoCtrl.js

@@ -14,6 +14,7 @@ app.controller('editPedidoCtrl', function ($scope, $http, $routeParams, notify,
 		$http.get('back/pedido/'+$routeParams.hash+'/'+$routeParams.id).then(
 		function(data) {
 	   		$scope.listaproductos = data.data.productos; 
+			$scope.updateTotal();
 		},
 		function(data) {
 		});
@@ -27,6 +28,7 @@ app.controller('editPedidoCtrl', function ($scope, $http, $routeParams, notify,
 		};
 		
 	};
+
 	$scope.updateTotal = function() {
 		var total=0;
 		total=$scope.listaproductos.reduce(
@@ -35,6 +37,7 @@ app.controller('editPedidoCtrl', function ($scope, $http, $routeParams, notify,
 			},0);
 		$scope.total=total;
 	};
+
 	$scope.reset = function() {
 		$scope.update();
 	};
@@ -56,10 +59,11 @@ app.controller('editPedidoCtrl', function ($scope, $http, $routeParams, notify,
 
 		pedido.productos = filtrados;
 		pedido.cliente = $routeParams.hash;
+		pedido.id = $routeParams.id;
 
 		$http.post('back/pedido', { pedido: pedido } )
 	        .success(function(data, status, headers, config) {
-				notify("Enviado correctamente. En unos minutos recibiras un e-mail de confirmacion");
+				notify("Actualizado correctamente");
 				$timeout(function() { $location.url("/"+$routeParams.hash) }, 2000);
 	        })
 	        .error(function(data, status, headers, config) {

+ 4 - 0
controllers/errorCtrl.js

@@ -0,0 +1,4 @@
+app.controller('errorCtrl', function ($scope, $http) {
+	$scope.email="asd";
+});
+

+ 31 - 3
controllers/facturaCtrl.js

@@ -1,8 +1,10 @@
-app.controller('facturaCtrl', function ($scope, $http, $routeParams) {
+app.controller('facturaCtrl', function ($scope, $http, $routeParams, $location, $uibModal ) {
 	$scope.pedidos=[];
+	$scope.modalInstance = null;
 	var user = $routeParams.hash;
-	if (user == undefined)
-		user="";
+	if (user == undefined){
+		$location.url("/");
+	}
 
 	$scope.update = function() {
 		$http.get('back/cliente/' + user).success(function(data) {
@@ -22,5 +24,31 @@ app.controller('facturaCtrl', function ($scope, $http, $routeParams) {
 	};
 
 	$scope.update();
+
+	$scope.openModal = function(id) {
+		$scope.id = id;
+		$scope.modalInstance = $uibModal.open({
+			templateUrl: 'confirmModal.tmpl.html',
+			scope: $scope,
+			id: id
+		});
+	};
+
+	$scope.closeModal = function(){
+		$scope.modalInstance.dismiss();
+	};
+	
+
+	$scope.confirm = function(){
+		$http.post('back/confirm/'+user+'/'+$scope.id).then(
+		function(data) {
+			$scope.update();
+			$scope.modalInstance.dismiss();
+		},
+		function(data) {
+			$scope.modalInstance.dismiss();
+		});
+	};
+
 });
 

+ 2 - 1
index.html

@@ -10,10 +10,11 @@
 	<link rel="styleSheet" href="/js/angular-notify/dist/angular-notify.css"/>
 	
 	<script type="text/javascript" src="/js/angular.min.js"></script>
-	<script type="text/javascript" src="/js/ui-bootstrap-tpls-0.13.4.min.js"></script>
+	<script type="text/javascript" src="/js/ui-bootstrap-custom-tpls-2.4.0.min.js"></script>
 	<script type="text/javascript" src="/js/angular-route.min.js"></script>
 	<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/errorCtrl.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>

+ 1 - 1
views/createOrEditPedido.html

@@ -50,7 +50,7 @@
 			<!--
 			<button class="css-button" ng-click="">Ver pedido</button>
 			-->
-			<button ng-show="cero==false" class="css-button" ng-click="ok()">Enviar Pedido</button>
+			<button ng-show="cero==false" class="css-button" ng-click="ok()">Guardar Pedido</button>
 		</div>
 	</div>
 </div>

+ 8 - 0
views/error.html

@@ -0,0 +1,8 @@
+<h1>ID Inválido</h1>
+
+<div class="input-group">
+	<label>E-mail</label>
+	<input type="text" ng-model="mail" />
+</div>
+
+<button class="btn">Recuperar link</button>

+ 19 - 2
views/main.html

@@ -23,16 +23,33 @@
 				<th># Productos</th>
 				<th>subtotal</th>
 				<th>Estado</th>
+				<th>Acciones</th>
 			</tr>
 			<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>{{p.estado}}</td>
 				<td>
-					<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>
+					<a ng-if="p.estado==='CREADO'" href="#" ng-href="edit/{{userdata.hash}}/{{p.id}}">Editar</a>
+					<a ng-if="p.estado==='CREADO'" href="#" ng-click="openModal(p.id)">Confirmar</a>
 				</td>
 			</tr>
 		</table>
 	</div>
 </div>
+
+<script type="text/ng-template" id="confirmModal.tmpl.html">
+    <div class="modal-header">
+        <h3>Confirmar</h3>
+    </div>   
+
+    <div class="modal-body">
+        <p>Seguro?</p>
+    </div>
+
+    <div class="modal-footer">
+        <button type="button" class="btn btn-default" ng-click="closeModal()" data-dismiss="modal">No</button>
+        <button type="button" class="btn btn-primary" ng-click="confirm()">Confirmar</button>
+    </div> 
+</script>