app.controller('facturaCtrl', function ($scope, $http, $window, notify,$location) { $scope.error=false; $scope.cero=true; $scope.total=0; var user = $location.search().user; 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; }); $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(); });