Commit bdbd4d53ad093d716acc7b25e52e21044b34a7ff

Authored by Eric Fernandez
1 parent 19913b391c
Exists in master

Ordeno un poco el código

Showing 1 changed file with 70 additions and 77 deletions   Show diff stats
src/js/controller.js
1 angular.module('focaCrearNotaPedido') 1 angular.module('focaCrearNotaPedido')
2 .controller('notaPedidoCtrl', 2 .controller('notaPedidoCtrl',
3 ['$scope', '$uibModal', '$location', 'crearNotaPedidoService', 'focaModalService', 3 ['$scope', '$uibModal', 'crearNotaPedidoService', 'focaModalService',
4 function($scope, $uibModal, $location, crearNotaPedidoService, focaModalService) { 4 function($scope, $uibModal, crearNotaPedidoService, focaModalService) {
5 $scope.show = false; 5 $scope.show = false;
6 $scope.cargando = true; 6 $scope.cargando = true;
7 $scope.dateOptions = { 7 $scope.dateOptions = {
8 maxDate: new Date(), 8 maxDate: new Date(),
9 minDate: new Date(2010, 0, 1) 9 minDate: new Date(2010, 0, 1)
10 }; 10 };
11 $scope.notaPedido = { 11 $scope.notaPedido = {
12 vendedor: {}, 12 vendedor: {},
13 cliente: {} 13 cliente: {},
14 domicilio: { dom: ''}
14 }; 15 };
15 $scope.articulosTabla = []; 16 $scope.articulosTabla = [];
16 var idLista; 17 var idLista;
17 var notaPedidoTemp = crearNotaPedidoService.getNotaPedido(); 18 var notaPedidoTemp = crearNotaPedidoService.getNotaPedido();
18 $scope.domiciliosCliente = crearNotaPedidoService.getDomicilios(1);
19 crearNotaPedidoService.getPrecioCondicion().then( 19 crearNotaPedidoService.getPrecioCondicion().then(
20 function(res) { 20 function(res) {
21 $scope.precioCondiciones = res.data; 21 $scope.precioCondiciones = res.data;
22 } 22 }
23 ); 23 );
24 if (notaPedidoTemp !== undefined) { 24 if (notaPedidoTemp !== undefined) {
25 notaPedidoTemp.fechaCarga = new Date(notaPedidoTemp.fechaCarga); 25 notaPedidoTemp.fechaCarga = new Date(notaPedidoTemp.fechaCarga);
26 $scope.notaPedido = notaPedidoTemp; 26 $scope.notaPedido = notaPedidoTemp;
27 $scope.notaPedido.flete = ($scope.notaPedido.flete).toString(); 27 $scope.notaPedido.flete = ($scope.notaPedido.flete).toString();
28 $scope.notaPedido.bomba = ($scope.notaPedido.bomba).toString(); 28 $scope.notaPedido.bomba = ($scope.notaPedido.bomba).toString();
29 idLista = $scope.notaPedido.precioCondicion; 29 idLista = $scope.notaPedido.precioCondicion;
30 crearNotaPedidoService 30 crearNotaPedidoService
31 .getArticulosByIdNotaPedido($scope.notaPedido.id).then( 31 .getArticulosByIdNotaPedido($scope.notaPedido.id).then(
32 function(res) { 32 function(res) {
33 $scope.articulosTabla = res.data; 33 $scope.articulosTabla = res.data;
34 } 34 }
35 ); 35 );
36 crearNotaPedidoService.getDomiciliosByIdNotaPedido($scope.notaPedido.id).then( 36 // crearNotaPedidoService.getDomiciliosByIdNotaPedido($scope.notaPedido.id).then(
37 function(res) { 37 // function(res) {
38 $scope.notaPedido.domicilio = res.data; 38 // $scope.notaPedido.domicilio = res.data;
39 } 39 // }
40 ); 40 // );
41 } else { 41 } else {
42 $scope.notaPedido.fechaCarga = new Date(); 42 $scope.notaPedido.fechaCarga = new Date();
43 $scope.notaPedido.domicilio = [{ id: 0 }]; 43 // $scope.notaPedido.domicilio = [{ id: 0 }];
44 $scope.notaPedido.bomba = '1'; 44 $scope.notaPedido.bomba = '0';
45 $scope.notaPedido.flete = '0'; 45 $scope.notaPedido.flete = '0';
46 idLista = undefined; 46 idLista = undefined;
47 } 47 }
48 $scope.addNewDom = function() { 48 $scope.addNewDom = function() {
49 $scope.notaPedido.domicilio.push({ 'id': 0 }); 49 $scope.notaPedido.domicilio.push({ 'id': 0 });
50 }; 50 };
51 $scope.removeNewChoice = function(choice) { 51 $scope.removeNewChoice = function(choice) {
52 if ($scope.notaPedido.domicilio.length > 1) { 52 if ($scope.notaPedido.domicilio.length > 1) {
53 $scope.notaPedido.domicilio.splice($scope.notaPedido.domicilio.findIndex( 53 $scope.notaPedido.domicilio.splice($scope.notaPedido.domicilio.findIndex(
54 function(c) { 54 function(c) {
55 return c.$$hashKey === choice.$$hashKey; 55 return c.$$hashKey === choice.$$hashKey;
56 } 56 }
57 ), 1); 57 ), 1);
58 } 58 }
59 }; 59 };
60 $scope.crearNotaPedido = function() { 60 $scope.crearNotaPedido = function() {
61 if($scope.notaPedido.domicilio.id === undefined) {
62 $scope.notaPedido.domicilio.id = 0;
63 }
64 var date = new Date();
61 var notaPedido = { 65 var notaPedido = {
62 id: 0, 66 id: 0,
63 fechaCarga: $scope.notaPedido.fechaCarga, 67 fechaCarga: new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
68 .toISOString().slice(0, 19).replace('T', ' '),
64 vendedor: $scope.notaPedido.vendedor.nombre, 69 vendedor: $scope.notaPedido.vendedor.nombre,
65 cliente: $scope.notaPedido.cliente.nombre, 70 idCliente: $scope.notaPedido.cliente.id,
66 domicilio: $scope.notaPedido.domicilio.id, 71 domicilio: $scope.notaPedido.domicilio,
67 precioCondicion: $scope.notaPedido.precioCondicion, 72 precioCondicion: $scope.notaPedido.precioCondicion,
68 bomba: $scope.notaPedido.bomba, 73 bomba: $scope.notaPedido.bomba,
69 flete: $scope.notaPedido.flete, 74 flete: $scope.notaPedido.flete,
70 total: $scope.getTotal() 75 total: $scope.getTotal()
71 }; 76 };
72 crearNotaPedidoService.crearNotaPedido(notaPedido).then( 77 crearNotaPedidoService.crearNotaPedido(notaPedido).then(
73 function() { 78 function(data) {
74 focaModalService.alert('Nota pedido creada'); 79 focaModalService.alert('Nota pedido creada');
75 // $location.path('/venta-nota-pedido'); 80 var flete = {
76 // var flete = { 81 idNotaPedido: data.data.id,
77 // idNotaPedido: data.data.id, 82 idTransportista: $scope.notaPedido.fleteId,
78 // idTransportista: $scope.notaPedido.fleteId, 83 idChofer: $scope.notaPedido.chofer.id,
79 // idChofer: $scope.chofer.id, 84 idVehiculo: $scope.notaPedido.vehiculo.id,
80 // idVehiculo: $scope.vehiculo.id, 85 kilometros: $scope.notaPedido.kilometros,
81 // kilometros: $scope.notaPedido.kilometros, 86 costoKilometro: $scope.notaPedido.costoUnitarioKmFlete
82 // costoKilometro: $scope.notaPedido.costoUnitarioKmFlete 87 };
83 // }; 88 crearNotaPedidoService.crearFlete(flete);
84 //TO DO - Insert de flete 89 var articulosNotaPedido = $scope.articulosTabla;
90 for(var i = 0; i< articulosNotaPedido.length;i++) {
91 delete articulosNotaPedido[i].edit;
92 articulosNotaPedido[i].idNotaPedido = data.data.id;
93 crearNotaPedidoService
94 .crearArticulosParaNotaPedido(articulosNotaPedido[i]);
95 }
96 $scope.limpiarPantalla();
85 } 97 }
86 ); 98 );
87 var articulosNotaPedido = $scope.articulosTabla;
88 for(var i = 0; i< articulosNotaPedido.length;i++) {
89 delete articulosNotaPedido[i].edit;
90 crearNotaPedidoService
91 .crearArticulosParaNotaPedido(articulosNotaPedido[i]).then(
92 function() {
93 return;
94 }
95 );
96 }
97 $scope.notaPedido.precioCondicion = '';
98 $scope.articulosTabla = [];
99 $scope.notaPedido.fleteNombre = '';
100 $scope.notaPedido.chofer = '';
101 $scope.notaPedido.vehiculo = '';
102 $scope.notaPedido.kilometros = '';
103 $scope.notaPedido.costoUnitarioKmFlete = '';
104 $scope.notaPedido.vendedor.nombre = '';
105 $scope.notaPedido.cliente.nombre = '';
106 $scope.domicilio = '';
107 $scope.notaPedido.flete = 0;
108 };
109 $scope.siguienteTab = function() {
110 $scope.active = 1;
111 }; 99 };
112 $scope.seleccionarArticulo = function() { 100 $scope.seleccionarArticulo = function() {
113 if (idLista === undefined) { 101 if (idLista === undefined) {
114 focaModalService.alert( 102 focaModalService.alert(
115 'primero seleccione una lista de precio y condicion'); 103 'primero seleccione una lista de precio y condicion');
116 return; 104 return;
117 } 105 }
118 var modalInstance = $uibModal.open( 106 var modalInstance = $uibModal.open(
119 { 107 {
120 ariaLabelledBy: 'Busqueda de Productos', 108 ariaLabelledBy: 'Busqueda de Productos',
121 templateUrl: 'modal-busqueda-productos.html', 109 templateUrl: 'modal-busqueda-productos.html',
122 controller: 'modalBusquedaProductosCtrl', 110 controller: 'modalBusquedaProductosCtrl',
123 resolve: { idLista: function() { return idLista; } }, 111 resolve: { idLista: function() { return idLista; } },
124 size: 'lg' 112 size: 'lg'
125 } 113 }
126 ); 114 );
127 modalInstance.result.then( 115 modalInstance.result.then(
128 function(producto) { 116 function(producto) {
129 var newArt = 117 var newArt =
130 { 118 {
131 id: 0, 119 id: 0,
132 codigo: producto.codigo, 120 codigo: producto.codigo,
133 sector: producto.sector, 121 sector: producto.sector,
134 descripcion: producto.descripcion, 122 descripcion: producto.descripcion,
135 item: $scope.articulosTabla.length + 1, 123 item: $scope.articulosTabla.length + 1,
136 nombre: producto.descripcion, 124 nombre: producto.descripcion,
137 precio: producto.precio.toFixed(2), 125 precio: producto.precio.toFixed(2),
138 costoUnitario: producto.costo, 126 costoUnitario: producto.costo,
139 edit: false 127 edit: false
140 }; 128 };
141 $scope.articuloACargar = newArt; 129 $scope.articuloACargar = newArt;
142 $scope.cargando = false; 130 $scope.cargando = false;
143 }, function() { 131 }, function() {
144 // funcion ejecutada cuando se cancela el modal 132 // funcion ejecutada cuando se cancela el modal
145 } 133 }
146 ); 134 );
147 }; 135 };
148 $scope.seleccionarVendedor = function() { 136 $scope.seleccionarVendedor = function() {
149 var modalInstance = $uibModal.open( 137 var modalInstance = $uibModal.open(
150 { 138 {
151 ariaLabelledBy: 'Busqueda de Vendedores', 139 ariaLabelledBy: 'Busqueda de Vendedores',
152 templateUrl: 'modal-vendedores.html', 140 templateUrl: 'modal-vendedores.html',
153 controller: 'modalVendedoresCtrl', 141 controller: 'modalVendedoresCtrl',
154 size: 'lg' 142 size: 'lg'
155 } 143 }
156 ); 144 );
157 modalInstance.result.then( 145 modalInstance.result.then(
158 function(vendedor) { 146 function(vendedor) {
159 $scope.notaPedido.vendedor.nombre = vendedor.NomVen; 147 $scope.notaPedido.vendedor.nombre = vendedor.NomVen;
160 }, function() { 148 }, function() {
161 149
162 } 150 }
163 ); 151 );
164 }; 152 };
165 $scope.seleccionarPetrolera = function() { 153 $scope.seleccionarPetrolera = function() {
166 var modalInstance = $uibModal.open( 154 var modalInstance = $uibModal.open(
167 { 155 {
168 ariaLabelledBy: 'Busqueda de Petrolera', 156 ariaLabelledBy: 'Busqueda de Petrolera',
169 templateUrl: 'modal-petroleras.html', 157 templateUrl: 'modal-petroleras.html',
170 controller: 'modalPetrolerasCtrl', 158 controller: 'modalPetrolerasCtrl',
171 size: 'lg' 159 size: 'lg'
172 } 160 }
173 ); 161 );
174 modalInstance.result.then( 162 modalInstance.result.then(
175 function(petrolera) { 163 function(petrolera) {
176 $scope.notaPedido.petrolera = petrolera.NOM; 164 $scope.notaPedido.petrolera = petrolera.NOM;
177 }, function() { 165 }, function() {
178 166
179 } 167 }
180 ); 168 );
181 }; 169 };
182 $scope.seleccionarCliente = function() { 170 $scope.seleccionarCliente = function() {
183 var modalInstance = $uibModal.open( 171 var modalInstance = $uibModal.open(
184 { 172 {
185 ariaLabelledBy: 'Busqueda de Cliente', 173 ariaLabelledBy: 'Busqueda de Cliente',
186 templateUrl: 'foca-busqueda-cliente-modal.html', 174 templateUrl: 'foca-busqueda-cliente-modal.html',
187 controller: 'focaBusquedaClienteModalController', 175 controller: 'focaBusquedaClienteModalController',
188 size: 'lg' 176 size: 'lg'
189 } 177 }
190 ); 178 );
191 modalInstance.result.then( 179 modalInstance.result.then(
192 function(cliente) { 180 function(cliente) {
181 $scope.limpiarPantalla();
193 $scope.notaPedido.cliente.nombre = cliente.nom; 182 $scope.notaPedido.cliente.nombre = cliente.nom;
183 $scope.notaPedido.cliente.id = cliente.cod;
184 crearNotaPedidoService.getDomiciliosByIdCliente(cliente.cod).then(
185 function(data) {
186 if(data.data.length === 0){
187 focaModalService
188 .alert('El cliente no tienen domicilios de entrega')
189 .then(
190 function() {
191 $scope.seleccionarCliente();
192 $scope.notaPedido.cliente = {nombre: ''};
193 }
194 );
195 return;
196 }
197 $scope.domiciliosCliente = data.data;
198 }
199 );
194 }, function() { 200 }, function() {
195 201
196 } 202 }
197 ); 203 );
198 }; 204 };
199 $scope.mostrarFichaCliente = function() { 205 $scope.mostrarFichaCliente = function() {
200 $uibModal.open( 206 $uibModal.open(
201 { 207 {
202 ariaLabelledBy: 'Datos del Cliente', 208 ariaLabelledBy: 'Datos del Cliente',
203 templateUrl: 'foca-crear-nota-pedido-ficha-cliente.html', 209 templateUrl: 'foca-crear-nota-pedido-ficha-cliente.html',
204 controller: 'focaCrearNotaPedidoFichaClienteController', 210 controller: 'focaCrearNotaPedidoFichaClienteController',
205 size: 'lg' 211 size: 'lg'
206 } 212 }
207 ); 213 );
208 }; 214 };
209 $scope.obtenerDomicilios = function(id) {
210 crearNotaPedidoService.getDomicilios(id).then(
211 function(res) {
212 $scope.notaPedido.domicilio = res.data;
213 }
214 );
215 };
216 $scope.getTotal = function() { 215 $scope.getTotal = function() {
217 var total = 0; 216 var total = 0;
218 var array = $scope.articulosTabla; 217 var array = $scope.articulosTabla;
219 for (var i = 0; i < array.length; i++) { 218 for (var i = 0; i < array.length; i++) {
220 total += array[i].precio * array[i].cantidad; 219 total += array[i].precio * array[i].cantidad;
221 } 220 }
222 return total.toFixed(2); 221 return total.toFixed(2);
223 }; 222 };
224 $scope.getSubTotal = function() { 223 $scope.getSubTotal = function() {
225 if($scope.articuloACargar) { 224 if($scope.articuloACargar) {
226 return $scope.articuloACargar.precio * $scope.articuloACargar.cantidad; 225 return $scope.articuloACargar.precio * $scope.articuloACargar.cantidad;
227 } 226 }
228 }; 227 };
229 $scope.cargarArticulos = function() {
230 idLista = $scope.notaPedido.precioCondicion;
231 $scope.articulosTabla = [];
232 };
233 $scope.abrirModalListaPrecio = function() { 228 $scope.abrirModalListaPrecio = function() {
234 var modalInstance = $uibModal.open( 229 var modalInstance = $uibModal.open(
235 { 230 {
236 ariaLabelledBy: 'Busqueda de Precio Condición', 231 ariaLabelledBy: 'Busqueda de Precio Condición',
237 templateUrl: 'modal-precio-condicion.html', 232 templateUrl: 'modal-precio-condicion.html',
238 controller: 'focaModalPrecioCondicionController', 233 controller: 'focaModalPrecioCondicionController',
239 size: 'lg' 234 size: 'lg'
240 } 235 }
241 ); 236 );
242 modalInstance.result.then( 237 modalInstance.result.then(
243 function(precioCondicion) { 238 function(precioCondicion) {
244 $scope.notaPedido.precioCondicion = precioCondicion.nombre; 239 $scope.notaPedido.precioCondicion = precioCondicion.nombre;
245 idLista = precioCondicion.idListaPrecio; 240 idLista = precioCondicion.idListaPrecio;
246 $scope.articulosTabla = []; 241 $scope.articulosTabla = [];
247 }, function() { 242 }, function() {
248 243
249 } 244 }
250 ); 245 );
251 }; 246 };
252 $scope.abrirModalFlete = function() { 247 $scope.abrirModalFlete = function() {
253 if($scope.notaPedido.flete === '1') { 248 if($scope.notaPedido.flete === '1') {
254 var modalInstance = $uibModal.open( 249 var modalInstance = $uibModal.open(
255 { 250 {
256 ariaLabelledBy: 'Busqueda de Flete', 251 ariaLabelledBy: 'Busqueda de Flete',
257 templateUrl: 'modal-flete.html', 252 templateUrl: 'modal-flete.html',
258 controller: 'focaModalFleteController', 253 controller: 'focaModalFleteController',
259 size: 'lg' 254 size: 'lg'
260 } 255 }
261 ); 256 );
262 modalInstance.result.then( 257 modalInstance.result.then(
263 function(flete) { 258 function(flete) {
264 $scope.choferes = ''; 259 $scope.limpiarFlete();
265 $scope.vehiculos = '';
266 $scope.notaPedido.chofer = '';
267 $scope.notaPedido.vehiculo = '';
268 $scope.notaPedido.costoUnitarioKmFlete = '';
269 $scope.notaPedido.fleteNombre = flete.nombre; 260 $scope.notaPedido.fleteNombre = flete.nombre;
270 $scope.notaPedido.fleteId = flete.id; 261 $scope.notaPedido.fleteId = flete.id;
271 $scope.choferes = flete.chofer; 262 $scope.choferes = flete.chofer;
272 $scope.vehiculos = flete.vehiculo; 263 $scope.vehiculos = flete.vehiculo;
273 }, function() { 264 }, function() {
274 265
275 } 266 }
276 ); 267 );
277 } 268 }
278 }; 269 };
279 $scope.agregarATabla = function(key) { 270 $scope.agregarATabla = function(key) {
280 if(key === 13) { 271 if(key === 13) {
281 if($scope.articuloACargar.cantidad === undefined || 272 if($scope.articuloACargar.cantidad === undefined ||
282 $scope.articuloACargar.cantidad === 0 || 273 $scope.articuloACargar.cantidad === 0 ||
283 $scope.articuloACargar.cantidad === null ){ 274 $scope.articuloACargar.cantidad === null ){
284 focaModalService.alert('El valor debe ser al menos 1'); 275 focaModalService.alert('El valor debe ser al menos 1');
285 return; 276 return;
286 } 277 }
287 $scope.articulosTabla.unshift($scope.articuloACargar); 278 $scope.articulosTabla.unshift($scope.articuloACargar);
288 $scope.cargando = true; 279 $scope.cargando = true;
289 } 280 }
290 }; 281 };
291 $scope.quitarArticulo = function(key) { 282 $scope.quitarArticulo = function(key) {
292 $scope.articulosTabla.splice(key, 1); 283 $scope.articulosTabla.splice(key, 1);
293 }; 284 };
294 $scope.editarArticulo = function(key, articulo) { 285 $scope.editarArticulo = function(key, articulo) {
295 if(key === 13) { 286 if(key === 13) {
296 if(articulo.cantidad === null || articulo.cantidad === 0 || 287 if(articulo.cantidad === null || articulo.cantidad === 0 ||
297 articulo.cantidad === undefined){ 288 articulo.cantidad === undefined){
298 focaModalService.alert('El valor debe ser al menos 1'); 289 focaModalService.alert('El valor debe ser al menos 1');
299 return; 290 return;
300 } 291 }
301 articulo.edit = false; 292 articulo.edit = false;
302 } 293 }
303 }; 294 };
304 $scope.cambioEdit = function(articulo) { 295 $scope.cambioEdit = function(articulo) {
305 articulo.edit = true; 296 articulo.edit = true;
306 }; 297 };
307 $scope.limpiarFlete = function() { 298 $scope.limpiarFlete = function() {
308 $scope.notaPedido.fleteNombre = ''; 299 $scope.notaPedido.fleteNombre = '';
309 $scope.notaPedido.chofer = ''; 300 $scope.notaPedido.chofer = '';
310 $scope.notaPedido.vehiculo = ''; 301 $scope.notaPedido.vehiculo = '';
311 $scope.notaPedido.kilometros = ''; 302 $scope.notaPedido.kilometros = '';
312 $scope.notaPedido.costoUnitarioKmFlete = ''; 303 $scope.notaPedido.costoUnitarioKmFlete = '';
304 $scope.choferes = '';
305 $scope.vehiculos = '';
313 }; 306 };
314 $scope.crearPedidoDemo = function() { 307 $scope.limpiarPantalla = function() {
315 focaModalService.alert('Pedido Creado'); 308 $scope.limpiarFlete();
309 $scope.notaPedido.flete = '0';
310 $scope.notaPedido.bomba = '0';
316 $scope.notaPedido.precioCondicion = ''; 311 $scope.notaPedido.precioCondicion = '';
317 $scope.articulosTabla = []; 312 $scope.articulosTabla = [];
318 $scope.notaPedido.fleteNombre = '';
319 $scope.notaPedido.chofer = '';
320 $scope.notaPedido.vehiculo = '';
321 $scope.notaPedido.kilometros = '';
322 $scope.notaPedido.costoUnitarioKmFlete = '';
323 $scope.notaPedido.vendedor.nombre = ''; 313 $scope.notaPedido.vendedor.nombre = '';
324 $scope.notaPedido.cliente.nombre = ''; 314 $scope.notaPedido.cliente = {nombre: ''};
325 $scope.domicilio.dom = ''; 315 $scope.notaPedido.domicilio = {dom: ''};
326 $scope.notaPedido.flete = 0; 316 $scope.domiciliosCliente = [];
327 }; 317 };
328 $scope.resetFilter = function() { 318 $scope.resetFilter = function() {
329 $scope.articuloACargar = {}; 319 $scope.articuloACargar = {};
330 $scope.cargando = true; 320 $scope.cargando = true;
331 }; 321 };
322 $scope.selectFocus = function ($event) {
323 $event.target.select();
324 };
332 } 325 }
333 ] 326 ]