| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- 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();
- });
|