File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/public/js/fans.js
$(document).ready(function () {
let div_price = $("#div_price");
let div_file = $("#div_file");
let div_user = $("#div_user");
div_price.hide();
div_user.hide();
div_file.hide();
_table = $("#example2").DataTable({
responsive: true,
processing: true,
serverSide: false,
order: [],
language: {
url: window.DATATABLES_LANGUAGE_URL,
},
columns: [
{ data: "line", name: "line", width: "10%" },
{ data: "message", name: "message", width: "90%" },
],
});
});
function validateTicketTypeFields() {
let name_ticket_type_id = $("#name_ticket_type_id").val();
let div_price = $("#div_price");
// Tipo credito
if (name_ticket_type_id == 4) {
div_price.show();
$("#zone_id").empty();
// Tipo cortesia
} else if (name_ticket_type_id == 3) {
div_price.val(0);
div_price.hide();
}
}
function searchUser() {
let user_document = $("#user_document").val();
let ticket_type_id = $("#name_ticket_type_id").val();
let name_ticket_type = $("#name_ticket_type_id option:selected").text();
let match_event_id = $("#name_match_event_id").val();
if (!ticket_type_id || !match_event_id) {
swal("Por favor indique el evento y tipo de boleta");
return;
}
$("#user_data").val("");
if (user_document && user_document != "") {
$.ajax({
url: `/tickets/user/${user_document}/${match_event_id}/${ticket_type_id}`,
type: "GET",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
success: function (r) {
if (r.r) {
var current_user = r.data;
if (!current_user) {
swal(
"Advertencia",
"¡Documento no encontrado!",
"warning"
);
return;
}
$("#user_data").val(
user_document +
" - " +
current_user.first_name +
" " +
current_user.last_name +
" - " +
current_user.email
);
$("#document").val(user_document);
if (current_user) {
$("#user_id").val(current_user.id);
}
if (r.tickets > 0) {
swal({
title: "",
icon: "info",
text:
"Este usuario ya cuenta con " +
r.tickets +
" boletas de " +
name_ticket_type +
", desea reenviar el email ?",
buttons: {
cancel: "Cancelar",
Ok: "Continuar",
},
}).then((result) => {
if (result == "Ok") {
resendEmail(
current_user.id,
match_event_id,
ticket_type_id
);
}
});
}
}
},
});
} else {
swal("Advertencia", "¡Debes ingresar un documento valido!", "warning");
}
}
function resendEmail(user_id, match_event_id, ticket_type_id) {
let formData = new FormData();
formData.append("user_id", user_id);
formData.append("match_event_id", match_event_id);
formData.append("ticket_type_id", ticket_type_id);
$.ajax({
url: "/tickets/resend/list",
method: "POST",
dataType: "json",
processData: false,
contentType: false,
data: formData,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
success: (r) => {
if (r.s) {
swal("", r.m, "success");
} else {
swal("", r.m, "error");
}
},
error: (e) => { },
});
}
function loadSubZones() {
let main_zone_id = $("#main_zone_id").val();
let match_event_id = $("#name_match_event_id").val();
$("#zone_id").empty();
if (!main_zone_id) {
return;
}
$.ajax({
url: "/get_sub_zones/" + main_zone_id + '/' + match_event_id,
method: "GET",
success: (r) => {
fillSubZones(r);
},
error: (r) => { },
});
}
function loadPriceForZone() {
let zone_id = $("#zone_id").val();
let match_event_id = $("#name_match_event_id").val();
let price = $("#price");
let name_ticket_type_id = $("#name_ticket_type_id").val();
// Si es cortesia precio = 0.
if (name_ticket_type_id == 3) {
price.val(0);
}
// Soloobtiene precio para tipo crédito.
if (name_ticket_type_id != 4) {
return;
}
$.ajax({
url: "/get_price_zone/" + zone_id + "/" + match_event_id,
method: "GET",
success: (r) => {
if (r) {
price.val(r.price);
} else {
price.val(0);
}
},
error: (r) => { },
});
}
function fillSubZones(subzones) {
let zone_id = $("#zone_id").empty();
zone_id.append(
`<option value="" disabled required selected>-- Seleccione</option>`
);
subzones.forEach((s) => {
zone_id.append(`<option value="${s.id}">${s.name}</option>`);
});
}
function validateGenerationTypeFields() {
let generation_type_id = $("#generation_type_id").val();
let div_price = $("#div_price");
let div_file = $("#div_file");
let div_location = $("#div_location");
let div_user = $("#div_user");
// Tipo aleatoria
if (generation_type_id == "random") {
div_price.val(0);
div_file.hide();
div_location.show();
div_user.show();
} else if (generation_type_id == "template") {
div_price.val(0);
div_file.show();
div_location.hide();
div_user.hide();
}
}
function beginProcessImport() {
let generation_type_id = $("#generation_type_id").val();
let generation_type_text = $("#generation_type_id option:selected").text();
let user_name = $("#user_document").val();
if (!generation_type_id) {
swal("Completa los datos");
return;
}
swal({
title: "Confirmación",
icon: "info",
text:
"Desea generar el proceso: \n Tipo: " +
generation_type_text +
"\n Para el usuario: " +
user_name,
buttons: {
cancel: "Cancelar",
Ok: "Continuar",
},
}).then((result) => {
if (result == "Ok") {
if (generation_type_id == "template") {
importFile();
} else if (generation_type_id == "random") {
importRandom();
} else {
swal(
"",
"Por favor indique el proceso que desea realizar",
"error"
);
}
}
});
}
function importRandom() {
var t = $("#example2").DataTable();
t.clear().draw();
let ticket_type_id = $("#name_ticket_type_id").val();
var match_event_id = $("#name_match_event_id").val();
var price = $("#price").val();
var user_id = $("#user_id").val();
var zone_id = $("#zone_id").val();
var quantity = $("#quantity").val();
if (
!ticket_type_id ||
!match_event_id ||
!user_id ||
!zone_id ||
!quantity
) {
swal("", "Por favor complete todos los datos!", "error");
return;
}
if (!price) {
price = 0;
}
var formData = new FormData();
formData.append("ticket_type_id", ticket_type_id);
formData.append("match_event_id", match_event_id);
formData.append("price", price);
formData.append("user_id", user_id);
formData.append("zone_id", zone_id);
formData.append("quantity", quantity);
$("#button-import-product").addClass("displayNone");
$("#spinnerButtonProduct").removeClass("displayNone");
$.ajax({
url: "/massive_fans/import_random",
method: "POST",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
data: formData,
success: (r) => {
$("#button-import-product").removeClass("displayNone");
$("#spinnerButtonProduct").addClass("displayNone");
if (r.r) {
swal("Ok", r.m, "success");
} else {
swal("Error", r.m + "\n" + (r.d || ""), "error");
}
},
error: (e) => {
$("#button-import-product").removeClass("displayNone");
$("#spinnerButtonProduct").addClass("displayNone");
},
});
}
function importFile() {
var fExcel = document.getElementById("importFans");
fExcel = fExcel.files[0];
if (!fExcel) {
swal("Advertencia", "¡No hay archivo!", "warning");
return;
}
var t = $("#example22").DataTable();
t.clear().draw();
var nameFile = document.getElementById("importFans").files[0].name;
var name_ticket_type_id = $("#name_ticket_type_id").val();
var match_event_id = $("#name_match_event_id").val();
var price = $("#price").val();
if (name_ticket_type_id === null) {
swal("", "Debes seleccionar el tipo de boleta.!", "error");
}
if (!price) {
price = 0;
}
var formData = new FormData();
formData.append("importFans", fExcel);
if (name_ticket_type_id) {
formData.append("name_file", nameFile);
formData.append("name_ticket_type_id", name_ticket_type_id);
}
if (match_event_id) {
formData.append("name_match_event_id", match_event_id);
}
if (price) {
formData.append("price", price);
}
$("#button-import-product").addClass("displayNone");
$("#spinnerButtonProduct").removeClass("displayNone");
$.ajax({
type: "POST",
dataType: "json",
processData: false,
contentType: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
data: formData,
url: "/massive_fans/import",
success: function (r) {
$("#button-import-product").removeClass("displayNone");
$("#spinnerButtonProduct").addClass("displayNone");
if (r.r) {
if (r.d != null && r.d.length > 0) {
for (var i = 0; i < r.d.length; i++) {
t.row.add([
r.d[i].message,
r.d[i].line,
]);
}
$('#modal_import_fans').modal("hide");
$('#logModal').modal("show");
t.draw();
} else {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/massive_fans");
}
});
}
} else {
swal("Error", r.m, "error");
}
},
error: function (err) {
$("#button-import-product").removeClass("displayNone");
$("#spinnerButtonProduct").addClass("displayNone");
swal("Error", err.responseJSON.message, "error");
},
});
}
function alertLogs() {
$(location).attr("href", "/massive_fans");
}