crearRolPolicia.js 12 KB
Newer Older
1
function listarRolesPolicias(objParams = {}) {
2

3 4 5 6 7
	ajaxWebService.get(URI_ROL_POLICIA, objParams).then((response) => {
		debugger
		let columns = [
			{
				title: `N°`,
8
//                data: `numeroFila`
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
				data: `id`
			},
			{
				title: `APELLIDOS Y NOMBRES`,
				data: `policia_nombres`
			},
			{
				title: `SEDE`,
				data: `sede_descripcion`
			},
			{
				title: `FECHA INICIO`,
				data: `fecha_inicio`
			},
			{
				title: `FECHA FIN`,
				data: `fecha_fin`
			},
			{
				title: `TIPO`,
				data: `tiporol_descripcion`
			},
			{
				title: `FRECUENCIA`,
				data: `frecuencia_descripcion`
			},
			{
				title: `ESTADO`,
				data: `estado`,
				render: (data) => {
					if (Object.is(data, 1)) {
						return `<span class="label label-success">ACTIVO</span>`;
					} else {
						return `<span class="label label-danger">INACTIVO</span>`;
					}
				}
			},
			{
				title: `ACCIONES`,
				data: `estado`,
				className: 'text-center',
				render: (data) => {
					console.log(data);
					if (Object.is(data, 1)) {
						return `   <span data-placement="left" title="Ver detalle" data-toggle="tooltip" style="cursor: pointer"> <i class='ver-detalle icon-calendar2 text-primary-700'></i>&nbsp;&nbsp; </span>
54
                                    <span data-toggle="tooltip" data-placement="left" title="Desactivar Rol" style="cursor: pointer"> <i class='eliminar icon-cancel-circle2 text-danger-700'></i>&nbsp;&nbsp; </span>
55
                                `;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
					} else {
						return null;
					}

				}
			}
		];

		let columnDefs = [
			{orderable: false, width: '1%', targets: 0, className: "text-center"},
			{orderable: false, width: '8%', targets: 1},
			{orderable: false, width: '10%', targets: 2, className: "text-center"},
			{orderable: false, width: '7%', targets: 3, className: "text-center"},
			{orderable: false, width: '7%', targets: 4, className: "text-center"},
			{orderable: false, width: '10%', targets: 5, className: "text-center"},
			{orderable: false, width: '8%', targets: 6, className: "text-center"},
			{orderable: false, width: '8%', targets: 7, className: "text-center"}
		];

		let wrapsNameDatatable = `divRoles`;
		let classNameForDatatable = `display table table-bordered`;

		makeDatatable(wrapsNameDatatable,
		  {
			  data: response.data,
			  columns,
			  columnDefs,
		  },
		  classNameForDatatable).then((nodes) => {
85 86
			nodes.container.off();
			nodes.container.on("click", ".ver-detalle", function () {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
				let data = nodes.datatable.DataTable().row($(this).parents("tr")).data();
				console.log(data);

				ajaxModal.get("policias/mantenimientoRol/detalleRol.jspf").then((response) => {
					debugger
					swal({
						title: '<strong>Detalle de rol</strong>',
						html: response.data,
						showCloseButton: true,
						showCancelButton: true,
						focusConfirm: false,
						confirmButtonText: '<i class="fa fa-thumbs-up"></i> Great!',
						confirmButtonAriaLabel: 'Thumbs up, great!',
						cancelButtonText: 'Cancelar',
						cancelButtonAriaLabel: 'Thumbs down',
						width: '30%',
						onOpen: () => {
							$("#spanNombres").html(data.policia_nombres);
							$("#spanSede").html(data.sede_descripcion);
							$("#spanHorario").html(`${data.tiporol_descripcion} ${data.frecuencia_descripcion}`);
							$("#spanRangoFechas").html(`${data.fecha_inicio} - ${data.fecha_fin}`);
							let htmlList = ``;
							data.detalles_rol.forEach(x => htmlList += `<li class="list-group-item" id="${x.id}">${x.fecha_entrada}</li>`);
							$("#listaDias").html(htmlList);
						}
					});

				});
			});
116 117 118 119


			nodes.container.on("click", ".eliminar", function () {
				let data = nodes.datatable.DataTable().row($(this).parents("tr")).data();
Billy Larru committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
				mostrarModalEliminacion(data);
			});
		});

	});
}


const mostrarModalEliminacion = (data) => {
	let {id, estado} = data;
	estado = 0;

	swal({
		title: 'Está seguro de eliminar el rol?',
		text: "",
		type: 'warning',
		showCancelButton: true,
		confirmButtonColor: '#3085d6',
		cancelButtonColor: '#d33',
		confirmButtonText: 'Sí, eliminar!'
	}).then((result) => {
		if (result.value) {
			ajaxWebService.patch(`${URI_ROL_POLICIA}/${id}`, {estado}).then((response) => {
143 144 145 146 147 148 149 150 151
				if (response.status) {
					swal({
						type: 'success',
						title: '¡Eliminado correctamente!',
						showConfirmButton: false,
						timer: 1500
					});
					listarRolesPolicias();
				}
152
			});
Billy Larru committed
153
		}
154
	});
155

Billy Larru committed
156 157 158 159 160 161 162 163





};

const listarTipoRoles = () => {
164 165 166 167 168 169
	ajaxWebService.get(URI_TIPOROL_POLICIA).then((response) => {
		let tipoRoles = response.data;
		let html = "<option>[SELECCIONE]</option>";
		tipoRoles.forEach(tp => html += `<option data-hora-entrada="${tp.hora_entrada}"  data-hora-salida="${tp.hora_salida}" value="${tp.codigo}">${tp.descripcion}</option>`);
		$("#cboTipoRol").html(html);
	});
170 171 172 173
}


function listarFrecuenciaRol() {
174 175 176 177 178 179
	ajaxWebService.get(URI_FRECUENCIA_ROL_POLICIA).then((result) => {
		let frecuenciasRol = result.data;
		let html = "<option>[SELECCIONE]</option>";
		frecuenciasRol.forEach(f => html += `<option value="${f.codigo}">${f.descripcion}</option>`);
		$("#cboFrecuenciaRol").html(html);
	});
180 181
}

182
function listarSedes(selectorName) {
183 184 185 186 187 188 189
	ajaxWebService.get(URI_SEDES).then((result) => {
		let sedes = result.data;
		sedes = _.orderBy(sedes, s => s.descripcion);
		let html = `<option value="">[SELECCIONE]</option>`;
		sedes.forEach(sede => html += `<option value="${sede.codigo}">${sede.descripcion}</option>`);
		$(`${selectorName}`).html(html);
	});
190 191
}

192
function listarPolicias(selectorName) {
193 194 195 196 197 198 199 200
	ajaxWebService.get(URI_POLICIAS).then((result) => {
		let policias = result.data;
		policias = _.orderBy(policias, p => p.nombres);
		let html = `<option value="">[SELECCIONE]</option>`;
		policias.forEach(p => html += `<option documento_identidad="${p.dni}" value="${p.id}">${p.nombres}</option>`);
		$(`${selectorName}`).html(html);
		$('.select-search').select2();
	});
201 202 203
}


204 205
function modificarServicio() {
//    debugger
206 207 208 209 210 211 212 213 214
	$("#detalle_rol").addClass("hidden");
	$("#editar_rol").removeClass("hidden");
	$("#modal-title").html('Cambiar servicio');
	$("#modal-header").removeClass('bg-primary');
	$("#modal-header").addClass('bg-jade');
	$("#botones-editar").removeClass('hidden');
	$("#btnCerrar").addClass("hidden");

	animar();
215
//    e.preventDefault();
Billy Larru committed
216
}
217

Billy Larru committed
218
function registrarRolPolicia() {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	let policia_nombres = $("#cboPolicias option:selected").text();
	let policia_id = $("#cboPolicias").val();
	let tiporol_id = $("#cboTipoRol").val();
	let tiporol_descripcion = $("#cboTipoRol option:selected").text();
	let rango_fechas = $("#dpRangoFechas").val();
	let split_fechas = rango_fechas.split("-").map(f => f.trim());
	let fecha_inicio = split_fechas[0];
	let fecha_fin = split_fechas[1];
	let frecuencia_id = $("#cboFrecuenciaRol").val();
	let frecuencia_descripcion = $("#cboFrecuenciaRol option:selected").text();
	let sede_id = $("#cboSedesModal").val();
	let sede_descripcion = $("#cboSedesModal option:selected").text();
	let estado = 1;
	let policia_dni = $("#cboPolicias option:selected").attr("documento_identidad");
	let hora_entrada = $("#cboTipoRol option:selected").data('hora-entrada');
	let hora_salida = $("#cboTipoRol option:selected").data('hora-salida');


	debugger
	let fechas = generarFechas(fecha_inicio, fecha_fin);
	let detalles_rol = [];
	let numero = 1;

	fechas
	  .map((f) => {
		  let obj = {
			  fecha_entrada: `${f} ${hora_entrada}`,
			  fecha_salida: `${f} ${hora_salida}`
		  }

		  return obj;
	  })
	  .forEach((f) => {
		  debugger
		  let detalle_rol = {
			  id: numero++,
			  fecha_entrada: f.fecha_entrada,
			  fecha_salida: f.fecha_salida,
			  estado: 1
		  };
		  detalles_rol.push(detalle_rol);
	  });

	let params = {
		numeroFila: policia_id,
		policia_id,
		policia_dni,
		policia_nombres,
		sede_id,
		sede_descripcion,
		fecha_inicio,
		fecha_fin,
		tiporol_id,
		tiporol_descripcion,
		frecuencia_id,
		frecuencia_descripcion,
		detalles_rol,
		estado
	};

	ajaxWebService.post(URI_ROL_POLICIA, params)
280
	  .then((response) => {
281
		  debugger;
282 283 284 285 286 287 288
		  if (response.status) {
			  swal({
				  type: 'success',
				  title: '¡Rol registrado exitosamente!',
				  showConfirmButton: false,
				  timer: 1500
			  })
289
			  listarRolesPolicias();
290
			  console.log(response.data);
291 292 293 294 295 296 297
		  } else {

		  }
	  })
	  .catch(error => {
		  console.log(error);
	  });
298 299
}

300
function generarFechas(desde, hasta) {
301 302 303 304 305 306 307 308 309 310 311 312
	let reverseDesde = desde.split("/").reverse().join("/");
	let reverseHasta = hasta.split("/").reverse().join("/");
	desde = moment(reverseDesde);
	hasta = moment(reverseHasta);
	let diaActual = desde;

	let fechas = [];
	while (diaActual.isSameOrBefore(hasta)) {
		fechas.push(diaActual.format('DD/MM/YYYY'));
		diaActual.add(1, 'days');
	}
	return fechas;
313 314
}

315
function cancelar() {
316 317 318 319 320 321 322 323
	$("#detalle_rol").removeClass("hidden");
	$("#editar_rol").addClass("hidden");
	$("#modal-title").html('Ver detalle rol');
	$("#modal-header").addClass('bg-primary');
	$("#modal-header").removeClass('bg-jade');
	$("#botones-editar").addClass('hidden');
	$("#btnCerrar").removeClass("hidden");
	animar();
324 325
}

326
function animar() {
327
	let animation = `fadeIn`;
328

329 330 331
	$("#modal_body_tombo2").addClass(`animated ${animation}`).one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend", function () {
		$(this).removeClass(`animated ${animation}`);
	});
332 333 334
}


335
function guardarCambios() {
336 337
	cancelar();
	$("#servicio13").css("background-color", "lightpink");
338 339 340
}


341
function mostrarModalNuevoRol() {
342 343 344
	ajaxModal.get("policias/mantenimientoRol/crearRol.jspf").then((response) => {
		swal({
			title: '<strong>Nuevo Rol</strong>',
345
//            type: 'info',
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
			html: response.data,
			showCloseButton: true,
			showCancelButton: true,
			focusConfirm: false,
			confirmButtonText: '<i class="fa fa-thumbs-up"></i> Registrar rol',
			confirmButtonAriaLabel: 'Registrar rol',
			cancelButtonText: 'Cancelar',
			cancelButtonAriaLabel: 'Thumbs down',
			width: '30%',
			customClass: 'swal2-overflow',
			onOpen: () => {
				listarTipoRoles();
				listarFrecuenciaRol();
				listarSedes("#cboSedesModal");
				listarPolicias("#cboPolicias");


				$('.selectpicker').selectpicker({
					style: 'btn-info',
					size: 4
				});


				$('.select-search').select2();


				$(".btn.dropdown-toggle.bs-placeholder").removeClass("btn-info");
				$(".btn.dropdown-toggle.bs-placeholder").css({
					border: "1px solid #ddd",
					backgroundColor: "#fff"
				});

				$("#dpRangoFechas").daterangepicker({
					"autoApply": true,
					"locale": {
						"format": "DD/MM/YYYY",
						"separator": " - ",
						"applyLabel": "Apply",
						"cancelLabel": "Cancel",
						"fromLabel": "From",
						"toLabel": "To",
						"customRangeLabel": "Custom",
						"weekLabel": "W",
						"daysOfWeek": [
							"Do",
							"Lu",
							"Ma",
							"Mi",
							"Ju",
							"Vi",
							"Sa"
						],
						"monthNames": [
							"Enero",
							"Febrero",
							"Marzo",
							"Abril",
							"Mayo",
							"Junio",
							"Julio",
							"Agosto",
							"Setiembre",
							"Octubre",
							"Noviembre",
							"Diciembre"
						],
						"firstDay": 1
					},
					"startDate": "17/08/2018",
					"endDate": "23/08/2018"
				}, function (start, end, label) {
417
//                    console.log('New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')');
418 419 420 421 422 423 424 425 426 427 428 429
				});
				$("#dpRangoFechas").datepicker('setDate', 'now');


				defaultConfigDatePicker();
			}
		}).then((result) => {
			if (result.value) {
				registrarRolPolicia();
			}
		});
	});
430 431
}

432 433

function buscarRoles() {
434 435 436 437
	let policia_id = $("#cboPoliciasFiltro").val();
	let sede_id = $("#cboSedesFiltro").val();
	let fecha_inicio = $("#dpFechaInicio").val();
	let fecha_fin = $("#dpFechaFin").val();
438

439 440 441 442 443
	let params = cleanQueryParams({
		policia_id,
		sede_id,
		fecha_inicio
	});
444

445
	console.log(params);
446

447
	listarRolesPolicias({params});
448 449
}

450
$().ready(function () {
451 452 453 454 455 456 457
	initDatePicker("#dpFechaInicio", "#dpFechaFin");
	listarSedes("#cboSedesFiltro");
	listarPolicias("#cboPoliciasFiltro");
	$('#servicio13').click(modificarServicio);
	$("#btnCancelar").click(cancelar);
	$("#btnGuardarCambios").click(guardarCambios);
	$("#btnNuevoRol").click(mostrarModalNuevoRol);
458 459
	$("#btnAplicarFiltro").click(buscarRoles);
	$("#btnMostrarTodo").click(listarRolesPolicias);
460
});