HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/public/js/academy_purchases.js
$(document).ready(function () {
    $('#start_term').on('change', function () {
        updateTerm();
    });

    $('#end_term').on('change', function () {
        updateTerm();
    });
});

function updateTerm() {
    const startDate = new Date($('#start_term').val());
    const endDate = new Date($('#end_term').val());

    startDate.setDate(startDate.getDate() + 1);
    endDate.setDate(endDate.getDate() + 1);

    const startMonth = getMonthName(startDate);
    const endMonth = getMonthName(endDate);
    const endMonthText = startMonth != endMonth ? '-' + endMonth : '';
    const term = startDate.getFullYear() + ',' + getMonthName(startDate) + endMonthText;

    $('#term').val(term);
}

function getMonthName(date) {
    const options = { month: 'long' };
    return new Intl.DateTimeFormat('es-CO', options).format(date);
}

function clickEditPayment(payment_id) {
    $(location).attr("href", "/academy_purchase/edit/" + payment_id);
}

function clickPay(payment_id) {
    $(location).attr("href", "/academy_purchase/pay/" + payment_id);
}

function editPurchase(typeAcademy) {
    $("#editPayment").validator("update");
    $("#editPayment")
        .validator("update")
        .on("submit", function (e) {
            if (e.isDefaultPrevented()) {
            } else {
                e.preventDefault();
                calculatePrice();
                if (!validateFormData()) {
                    return;
                }
                swal(Lang.get("messagesClient.academy_payments.update_payment"), {
                    buttons: {
                        cancel: "No",
                        Ok: true,
                    },
                }).then((val) => {
                    if (val == "Ok") {
                        $('#loading').show();
                        if ($("#active_payment").is(":checked")) {
                            active_check = 1;
                        } else {
                            active_check = 0;
                        }
                        var info = {
                            id: $("#btn-edit-payment").attr("data-id"),
                            price_payment: $("#editPayment input[name='price_payment']").val(),
                            price_discount: $("#editPayment input[name='price_discount']").val(),
                            discount: $("#editPayment input[name='discount']").val(),
                            subtotal: $("#editPayment input[name='subtotal']").val(),
                            service_charge: $("#editPayment input[name='service_charge']").val(),
                            price: $("#editPayment input[name='price']").val(),
                            term_type: $("#editPayment input[name='term_type']").val(),
                            term: $("#editPayment input[name='term']").val(),
                            start_term: $("#editPayment input[name='start_term']").val(),
                            end_term: $("#editPayment input[name='end_term']").val(),
                            payment_activation: $("#editPayment input[name='payment_activation']").val(),
                            payment_due_date: $("#editPayment input[name='payment_due_date']").val(),
                            pin: $("#editPayment input[name='pin']").val(),
                            reference: $("#editPayment input[name='reference']").val(),
                            state: $("#editPayment select[name='state']").val(),
                            comment: $("#editPayment input[name='comment']").val(),
                            payment_method_id: $("#editPayment select[name='payment_method_id']").val(),
                            active: active_check,
                        };
                        $.ajax({
                            url: "/academy_user_payment/update",
                            type: "PUT",
                            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: true,
                                        },
                                    }).then((val) => {
                                        if (val == "Ok") {
                                            localStorage.setItem("setPageTable", true);
                                            $(location).attr("href", "/academy_purchase/" + typeAcademy);
                                        }
                                    });
                                } else {
                                    swal("Error", r.m, "error");
                                }
                            },
                            error: function (e) {
                                $('#loading').hide();
                                swal("Error", Lang.get("messagesClient.academy_payments.error_update_payment"), "error");
                            },
                        });
                    }
                });
            }
        });
}

function purchase(typeAcademy) {
    $("#pay").validator("update");
    $("#pay")
        .validator("update")
        .on("submit", function (e) {
            if (e.isDefaultPrevented()) {
            } else {
                e.preventDefault();
                swal(Lang.get("messagesClient.academy_payments.confirm_payment"), {
                    buttons: {
                        cancel: "No",
                        Ok: true,
                    },
                }).then((val) => {
                    if (val == "Ok") {
                        $('#loading').show();
                        let payment_date = $("#pay input[name='start_date']").val() + ' ' + convertTime12to24($("#pay input[name='start_time']").val())
                        var data = {
                            id: $("#btnPay").attr("data-id"),
                            reference: $("#pay input[name='payment_reference']").val(),
                            state: $("#pay select[name='state_payment']").val(),
                            comment: $("#pay input[name='payment_comment']").val(),
                            payment_method_id: $("#pay select[name='payment_method_id']").val(),
                            support: document.getElementById("document").files.length ? document.getElementById("document").files[0] : '',
                            payment_date: payment_date,
                        };
                        formData = new FormData();
                        for (var key in data) {
                            formData.append(key, data[key]);
                        }
                        $.ajax({
                            url: "/academy_user_payment/pay",
                            type: "POST",
                            dataType: "json",
                            processData: false,
                            contentType: false,
                            headers: {
                                "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
                                    "content"
                                )
                            },
                            data: formData,
                            success: function (r) {
                                $('#loading').hide();
                                if (r.r) {
                                    swal(r.m, {
                                        icon: "success",
                                        buttons: {
                                            Ok: true,
                                        },
                                    }).then((val) => {
                                        if (val == "Ok") {
                                            localStorage.setItem("setPageTable", true);
                                            $(location).attr("href", "/academy_purchase/" + typeAcademy);
                                        }
                                    });
                                } else {
                                    swal("Error", r.m, "error");
                                }
                            },
                            error: function (e) {
                                $('#loading').hide();
                                swal("Error", Lang.get("messagesClient.academy_payments.error_confirm_payment"), "error");
                            },
                        });
                    }
                });
            }
        });
}

function modifyPaymentSchedule() {
    var academy_user_id = $(" #editPayment input[name='academy_user_id']").val();

    $.ajax({
        url: "/academy_purchase/modifyPayment/" + academy_user_id,
        type: "GET",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        success: function (r) {
            var data;
            if (r.r) {
                data = r.d;
                $("#modal_academy_category_id").val(data.academy_user.academy_category_id);
                $("#modal_userApp_id").val(data.academy_user.user_id);
                $("#modal_payment_schedule").modal('show');
            } else {
                swal("Error", r.m, "error");
            }
        },
        error: function (textStatus, errorThrown) {
            swal("Error", Lang.get("Error"), "error");
        },
    });
}

function sendingInfoChangeTypePayment() {
    var academy_user_id = $(" #editPayment input[name='academy_user_id']").val();
    var value_academy_category_id = $("#modal_academy_category_id").val();
    var value_userApp_id = $("#modal_userApp_id").val();
    var value_periods_academy_new = $("#periods_academy option:selected").val();

    swal(Lang.get("messagesClient.academy_purchases.edit_question"), {
        buttons: {
            cancel: "No",
            Ok: true,
        },
    }).then((val) => {
        if (val == "Ok") {
            var info = {
                id: academy_user_id,
                academy_category_id: value_academy_category_id,
                userApp_id: value_userApp_id,
                periods_academy_new: value_periods_academy_new
            };

            $.ajax({
                url: "/academy_purchase/sendingPaymentType",
                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) {
                    if (r) {
                        var idPayment = r.d.id;
                        swal(("Ok", r.m), {
                            icon: "success",
                            buttons: {
                                Ok: true,
                            },
                        }).then((val) => {
                            if (val == "Ok") {
                                var URLactual = jQuery(location).attr('href');
                                var arrayURLactual = URLactual.split('/');
                                let existeURLactual = arrayURLactual.find(element => element == 'academy_user_payment');
                                if (existeURLactual) {
                                    $(location).attr("href", "/academy_user_payment/edit/" + idPayment);
                                } else {
                                    $(location).attr("href", "/academy_purchase/edit/" + academy_user_id);
                                }
                            }
                        });
                    } else {
                        swal("Error", r.m, "error");
                    }
                },
                error: function (e) {
                    swal("Error", Lang.get("messagesClient.academy_purchases.error_modifying"), "error");
                },
            });
        }
    });

}

function clickDelete(value) {
    deleteRegister("/academy_purchase/delete/" + value, Lang.get("messagesClient.academy_purchases.delete_question"), "/academy_purchase");
}

function calculatePrice() {
    let pricePayment = $("#editPayment input[name='price_payment']").val();

    let price_discount = parseFloat($("#editPayment input[name='price_discount']").val());
    if (!price_discount)
        price_discount = 0;

    let discount = parseFloat($("#editPayment input[name='discount']").val());
    if (!discount)
        discount = 0;

    let service_charge = parseFloat($("#editPayment input[name='service_charge']").val());
    if (!service_charge)
        service_charge = 0;

    let subtotal = Math.floor(pricePayment - price_discount - Math.floor(pricePayment * (discount / 100)));
    let total = Math.floor(subtotal + service_charge);
    $("#editPayment input[name='subtotal']").val(subtotal);
    $("#editPayment input[name='price']").val(total);
}

function validateFormData() {
    if ($("#editPayment select[name='state']").val() == "CONFIRMED" && !$("#editPayment input[name='reference']").val()) {
        swal("Error", Lang.get("messagesClient.academy_purchases.reference_required"), "error").then(() => {
            $("#editPayment input[name='reference']").focus();
        });
        return false;
    }
    return true;
}