File: /var/www/vhost/disk-apps/demo.sports-crowd.com/public/js/flash_ticket_selling.js
var matchEvent;
var subtotal;
var service_charge;
var total;
var tickets;
function selectEvent(sel) {
$('#web_price_container').hide();
$('#special_price_container').hide();
$('#courtesy_price_container').hide();
matchEvent = JSON.parse(sel.value);
if (matchEvent.web_price != null) {
$('#web_price_container').show();
$('#web_price').val(matchEvent.web_price);
$('#web_amount_label').html('Boletas (' + matchEvent.maximum_user_ticket_sales_box_office + ' Max)');
}
if (matchEvent.special_price != null) {
$('#special_price_container').show();
$('#special_price').val(matchEvent.special_price);
}
if (matchEvent.courtesy_price != null) {
$('#courtesy_price_container').show();
$('#courtesy_price').val(matchEvent.courtesy_price);
}
clearData();
$('#total_container').show();
}
function getSoldTicketsCount() {
$.ajax({
url: "/flash_ticket/getSoldCount/" + matchEvent.match_event_id + "/" + matchEvent.zone_id,
type: "GET",
contentType: "application/json",
async: false,
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
success: function (r) {
if (r.r) {
let detail = (r.d + ' Boletas vendidas') + ' / ' + (matchEvent.salable_capacity + ' Max. capacidad boletas');
$('#available_tickets').html(detail);
}
},
});
}
function calculateRelativeTotal(id) {
let serviceCharge = 0;
switch (id) {
case 'web_amount':
if ($('#web_amount').val() > matchEvent.maximum_user_ticket_sales_box_office) {
$('#web_amount').val(matchEvent.maximum_user_ticket_sales_box_office);
}
webAmount = $('#web_amount').val();
if (matchEvent.service_charge) {
serviceCharge = typeof matchEvent.service_charge === 'string' && matchEvent.service_charge.includes('%') ? (parseFloat(matchEvent.service_charge) / 100) * matchEvent.web_price : matchEvent.service_charge;
}
$('#web_total_price').val(webAmount * matchEvent.web_price);
$('#web_service_charge').val(webAmount * serviceCharge);
break;
case 'special_amount':
especialAmount = $('#special_amount').val();
if (matchEvent.service_charge) {
serviceCharge = typeof matchEvent.service_charge === 'string' && matchEvent.service_charge.includes('%') ? (parseFloat(matchEvent.service_charge) / 100) * matchEvent.special_price : matchEvent.service_charge;
}
$('#special_total_price').val(especialAmount * matchEvent.special_price);
$('#special_service_charge').val(especialAmount * serviceCharge);
if (!especialAmount || especialAmount == 0) {
$('#special_text').val('');
}
break;
case 'courtesy_amount':
courtesyAmount = $('#courtesy_amount').val();
if (matchEvent.service_charge) {
serviceCharge = typeof matchEvent.service_charge === 'string' && matchEvent.service_charge.includes('%') ? (parseFloat(matchEvent.service_charge) / 100) * matchEvent.courtesy_price : matchEvent.service_charge;
}
$('#courtesy_total_price').val(courtesyAmount * matchEvent.courtesy_price);
$('#courtesy_service_charge').val(courtesyAmount * serviceCharge);
if (!courtesyAmount || courtesyAmount == 0) {
$('#courtesy_text').val('');
}
break;
}
calculateTotal();
showPaymentButton();
}
function calculateTotal() {
subtotal =
Number($('#web_total_price').val()) +
Number($('#special_total_price').val()) +
Number($('#courtesy_total_price').val());
serviceCharge = Number($('#web_service_charge').val()) +
Number($('#special_service_charge').val()) +
Number($('#courtesy_service_charge').val());
total = subtotal + serviceCharge;
$('#sub_total').html('Subtotal: $' + subtotal);
$('#service_charge').html('Servicio: $' + serviceCharge);
$('#total').html('Total: $' + total);
}
function showPaymentButton() {
tickets =
Number($('#web_amount').val()) +
Number($('#special_amount').val()) +
Number($('#courtesy_amount').val());
if (tickets > 0) {
$('#div_footer').show();
} else {
$('#div_footer').hide();
}
}
function buyTickets(parameters) {
specialAmount = $('#special_amount').val();
specialText = $('#special_text').val();
courtesyAmount = $('#courtesy_amount').val();
courtesyText = $('#courtesy_text').val();
if (courtesyAmount && courtesyAmount > 0 && !courtesyText) {
swal(Lang.get("messagesClient.flash_ticket.tag_4"), {
text: Lang.get("messagesClient.flash_ticket.tag_4"),
icon: 'warning',
showCancelButton: true,
cancelButtonColor: '#d33',
});
return;
}
if (!tickets) {
swal(Lang.get("messagesClient.flash_ticket.tag_2"), {
text: "",
icon: 'warning',
showCancelButton: true,
cancelButtonColor: '#d33',
});
}
showActionsAlert = parameters.flash_ticket_selling_confirmation;
if (showActionsAlert) {
swal({
title: 'Venta taquilla',
text: '¿Estás seguro de que deseas continuar con la venta de boletas?',
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
sendDataToApi(showActionsAlert);
}
});
} else {
sendDataToApi(showActionsAlert);
}
}
function sendDataToApi(showActionsAlert) {
var info = {
subtotal: subtotal,
service_charge: service_charge,
total: total,
web_amount: $('#web_amount').val(),
special_amount: specialAmount,
courtesy_amount: courtesyAmount,
flash_ticket_configuration_id: matchEvent.id,
special_text: specialText,
courtesy_text: courtesyText,
};
$('#loading').show();
$.ajax({
url: "/flash_ticket/buy",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
$('#loading').hide();
if (r.r) {
if (r.ticket_main_id) {
localStorage.setItem('ticket_main_id', r.ticket_main_id);
}
tickets = r.d.join();
if (showActionsAlert) {
swal({
title: r.m,
text: "Boletas vendidas: " + tickets,
icon: "success",
buttons: false,
closeOnClickOutside: false,
content: createCustomButtons()
});
function createCustomButtons() {
const container = document.createElement('div');
container.className = 'swal-buttons-container';
container.innerHTML = `
<span id="keepSelling" class="swal-button-cancel" title="Cerrar">×</span>
<button id="sendEmail" class="swal-button swal-button-action">
<i class="fa fa-envelope"></i> Enviar por correo
</button>
<button id="sendWhatsApp" class="swal-button swal-button-action">
<i class="fa fa-whatsapp"></i> Enviar por WhatsApp
</button>
<button id="printTickets" class="swal-button swal-button-action">
<i class="fa fa-print"></i> Imprimir
</button>
`;
return container;
}
// Escuchar clicks después de que se muestre
setTimeout(() => {
document.getElementById('keepSelling').addEventListener('click', () => {
swal.close();
$(location).attr("href", window.location.pathname);
});
document.getElementById('sendEmail').addEventListener('click', () => {
swal.close();
$('#emailModal').modal('show');
});
document.getElementById('printTickets').addEventListener('click', () => {
swal.close();
printTickets(tickets);
});
document.getElementById('sendWhatsApp').addEventListener('click', () => {
swal.close();
$('#whatsappModal').modal('show');
});
}, 100);
} else {
printTickets(tickets);
}
} else {
if (r.r == false) {
swal("Error", r.m, "error");
} else {
swal("Error", Lang.get("messagesClient.user_client_tag3"), "error");
}
}
},
});
}
function clearData() {
$('#web_amount').val('');
$('#special_amount').val('');
$('#courtesy_amount').val('');
$('#web_total_price').val('');
$('#special_total_price').val('');
$('#courtesy_total_price').val('');
$('#web_service_charge').val('');
$('#special_text').val('');
$('#courtesy_text').val('');
calculateTotal();
showPaymentButton();
getSoldTicketsCount();
}
function printTickets(tickets) {
clearData();
window.open(window.location.origin + '/tickets/pdf/' + tickets);
}
function sendTicketByEmail(event) {
event.preventDefault();
email = $('#email_address').val();
if (email != $('#confirm_email_address').val()) {
swal("Advertencia", "Los correos electrónicos no coinciden", "warning");
return;
}
ticket_main_id = localStorage.getItem("ticket_main_id");
var info = {
ticket_main_id: ticket_main_id,
email: email,
url: window.location.origin + '/tickets/pdf/' + tickets
};
$('#loading').show();
$.ajax({
url: "/flash_ticket/sendByEmail",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
$('#loading').hide();
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: "Aceptar",
},
}).then((val) => {
$(location).attr("href", window.location.pathname);
});
} else {
if (r.r == false) {
swal("Error", r.m, "error");
} else {
swal("Error", "No se puede enviar por correo la boleta(s)", "error");
}
}
},
});
return false;
}
function sendTicketByWhatsApp(event) {
event.preventDefault();
var phone = $('#dial-code').val() + $('#phone').val();
var phoneConfirmation = $('#dial-code-confirmation').val() + $('#phone-confirmation').val();
if (phone != phoneConfirmation) {
swal("Advertencia", "Los números de WhatsApp no coinciden", "warning");
return;
}
ticket_main_id = localStorage.getItem("ticket_main_id");
var info = {
ticket_main_id: ticket_main_id,
phone: phone,
tickets: tickets
};
$('#loading').show();
$.ajax({
url: "/flash_ticket/sendByWhatsApp",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $(
'meta[name="csrf-token"]'
).attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
$('#loading').hide();
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: "Aceptar",
},
}).then((val) => {
$(location).attr("href", window.location.pathname);
});
} else {
if (r.r == false) {
swal("Error", r.m, "error");
} else {
swal("Error", "No se puede enviar por WhatsApp la boleta(s)", "error");
}
}
},
catch: function (e) {
console.error(e);
$('#loading').hide();
swal("Error", e.message || "No se puede enviar por WhatsApp la boleta(s)", "error");
}
});
return false;
}
function reloadPage() {
$(location).attr("href", window.location.pathname);
}
function initIntlTelInput(phoneInputId, dialCodeInputId, countryCodeInputId) {
const phoneInput = document.querySelector(phoneInputId);
const dialCodeInput = document.querySelector(dialCodeInputId);
const countryCodeInput = document.querySelector(countryCodeInputId);
if (phoneInput && window.intlTelInput) {
const iti = window.intlTelInput(phoneInput, {
initialCountry: "co", // o "auto"
preferredCountries: ["co", "us", "pa", "ec", "cl"],
separateDialCode: true,
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/utils.js",
});
if (dialCodeInput) dialCodeInput.value = iti.getSelectedCountryData().dialCode;
if (countryCodeInput) countryCodeInput.value = iti.getSelectedCountryData().iso2;
phoneInput.addEventListener("countrychange", function () {
if (dialCodeInput) dialCodeInput.value = iti.getSelectedCountryData().dialCode;
if (countryCodeInput) countryCodeInput.value = iti.getSelectedCountryData().iso2;
});
return iti;
}
return null;
}
const iti1 = initIntlTelInput("#phone", "#dial-code", "#country-code");
const iti2 = initIntlTelInput("#phone-confirmation", "#dial-code-confirmation", "#country-code-confirmation");
function isValidPhoneNumber(itiInstance) {
return itiInstance && itiInstance.isValidNumber();
}