facturaCtrl.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. app.controller('facturaCtrl', function ($scope, $http, $window, notify,$location) {
  2. $scope.cero=true;
  3. $scope.total=0;
  4. var user = $location.search().user;
  5. if (user == undefined)
  6. user="";
  7. $scope.update = function() {
  8. $http.get('back/cliente/' + user).success(function(data) {
  9. $scope.userdata = data;
  10. });
  11. $http.get('back/productos/' + user).success(function(data) {
  12. $scope.listaproductos = data;
  13. });
  14. };
  15. $scope.filterCero = function(criteria) {
  16. return function(item) {
  17. if ($scope.cero)
  18. return true;
  19. return item.cantidad > 0;
  20. };
  21. };
  22. $scope.updateTotal = function() {
  23. var total=0;
  24. total=$scope.listaproductos.reduce(
  25. function(acc,cur) {
  26. return acc+(cur.cantidad*cur.precio)
  27. },0);
  28. $scope.total=total;
  29. };
  30. $scope.reset = function() {
  31. $scope.update();
  32. };
  33. $scope.ok = function () {
  34. var pedido = {};
  35. var filtrados = $scope.listaproductos.filter(function(el) { return el.cantidad > 0; });
  36. if (filtrados.length==0){
  37. notify({message:"No hay productos",classes:"alert alert-danger"});
  38. return;
  39. }
  40. for(var f in $scope.listaproductos){
  41. var prod = $scope.listaproductos[f];
  42. if(isNaN(prod.cantidad)){
  43. notify({message:prod.nombre+" tiene cantidad incorrecta",classes:"alert alert-danger"});
  44. return;
  45. }
  46. }
  47. pedido.productos = filtrados;
  48. pedido.cliente = $scope.userdata;
  49. $http.post('back/pedido', { pedido: pedido } )
  50. .success(function(data, status, headers, config) {
  51. notify("Guardada correctamente");
  52. })
  53. .error(function(data, status, headers, config) {
  54. notify({classes:"alert alert-danger", message:"Error: "+ data.err});
  55. });
  56. };
  57. $scope.reset();
  58. });