MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 8.0.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/demo.imensosoftware.com/matrix/js/admin_report.js
// Report Admin Panel
var app = app || {};

app.location = window.location.origin + "/matrix/admin/";
if (window.location.hostname === "sh018.hostgator.tempwebhost.net") {
    app.location = "http://sh018.hostgator.tempwebhost.net/~a16102wt/matrix/admin/";
}

app.filter = { start_date: "", end_date: "" };

// ✅ Central config for users (NO MORE HARD CODING EVERYWHERE)
app.users = [
    "Sachin",
    "Ashwin",
    "Varun",
    "Mansi",
    "Dipanjali",
    "Kiranteja",
    "Deeksha"
];

app.userColors = {
    Sachin: "rgba(0,69,238,.7)",
    Ashwin: "rgba(112,173,71,.7)",
    Deeksha: "rgba(255,192,37,.7)",
    Varun: "rgba(87,87,87,.7)",
    Dipanjali: "rgba(255,99,132,.7)",
    Kiranteja: "rgba(54,162,235,.7)",
    Mansi: "rgba(153,102,255,.7)"
};

$(document).ready(function () {

    const curr = new Date();
    const firstday = new Date();
    const lastday = new Date(curr.setDate(curr.getDate() - curr.getDay()));

    app.filter.start_date = formatDate(lastday);
    app.filter.end_date = formatDate(firstday);

    app.getReport();

    $('#start_date').datepicker({
        uiLibrary: 'bootstrap4',
        format: 'yyyy-mm-dd',
        value: formatDate(lastday),
        maxDate: () => $('#end_date').val()
    });

    $('#end_date').datepicker({
        uiLibrary: 'bootstrap4',
        format: 'yyyy-mm-dd',
        value: formatDate(firstday),
        minDate: () => $('#start_date').val()
    });

    $('#start_date, #end_date').change(function () {
        app.handleDate(
            $("#start_date").val(),
            $("#end_date").val()
        );
    });
});

/* ================= DATE HANDLER ================= */
app.handleDate = function (start_date, end_date) {
    if (!start_date || !end_date) {
        alert("Please select date");
        return;
    }

    app.filter.start_date = start_date;
    app.filter.end_date = end_date;

    app.getReport();
};

/* ================= API ================= */
app.getReport = function () {

    $.ajax({
        url: app.location + "report_code.php",
        type: "POST",
        data: { filter: app.filter },
        dataType: "json",

        success: function (data) {

            if (!data) {
                console.error("Invalid response");
                return;
            }

            // ✅ Dynamic loop instead of 7 repetitive loops
            app.users.forEach(user => {
                const key = "data" + user;
                if (data[key]) {
                    data[key] = data[key].map(item => ({
                        ...item,
                        x: new Date(item.label)
                    }));
                } else {
                    data[key] = [];
                }
            });

            app.reportChart(data);
        },

        error: function (xhr) {
            console.error("API Error:", xhr.responseText);
            alert("Something went wrong while fetching report");
        }
    });
};

/* ================= CHART ================= */
app.reportChart = function (data) {

    const totalDay = calcDate(
        new Date(app.filter.end_date),
        new Date(app.filter.start_date)
    );

    const { interval, intervalType } = getInterval(totalDay);

    // ✅ Dynamic chart series
    const chartData = app.users.map(user => ({
        toolTipContent: "{label}<br/> <strong>{name}</strong> : <strong>{y}</strong>",
        name: user,
        type: "line",
        showInLegend: true,
        legendMarkerType: "square",
        yValueFormatString: "###",
        color: app.userColors[user] || getRandomColor(), // ✅ fallback
        dataPoints: data["data" + user] || []
    }));

    const chart = new CanvasJS.Chart("chartContainer", {
        animationEnabled: true,
        legend: {
            verticalAlign: "top",
            horizontalAlign: "center",
            fontSize: 14
        },
        axisX: {
            valueFormatString: "DD MMM YYYY",
            interval: interval,
            intervalType: intervalType
        },
        axisY: {},
        data: chartData
    });

    chart.render();
};

/* ================= HELPERS ================= */

// ✅ Fixed interval logic bug (your >200, >300 never executed)
function getInterval(days) {
    if (days <= 5) return { interval: 1, intervalType: "day" };
    if (days <= 15) return { interval: 2, intervalType: "day" };
    if (days <= 30) return { interval: 4, intervalType: "day" };
    if (days <= 60) return { interval: 7, intervalType: "day" };
    if (days <= 100) return { interval: 12, intervalType: "day" };
    if (days <= 200) return { interval: 1, intervalType: "month" };
    if (days <= 300) return { interval: 2, intervalType: "month" };

    return { interval: 3, intervalType: "month" };
}

function calcDate(date1, date2) {
    return Math.floor((date1 - date2) / (1000 * 60 * 60 * 24));
}

function formatDate(date) {
    return date.toISOString().split('T')[0];
}