diff --git a/admin/assets/js/dashboard-chart.js b/admin/assets/js/dashboard-chart.js index 793d5f5..fce85d6 100644 --- a/admin/assets/js/dashboard-chart.js +++ b/admin/assets/js/dashboard-chart.js @@ -7,12 +7,18 @@ $(document).ready(function() { fetchData() .then(function (response) { responseData = response.data; - console.log(responseData); + // console.log(responseData); const completedOrdersCount = countCompletedOrders(responseData); const toPayOrdersCount = countToPayOrders(responseData); const toShipOrdersCount = countToShipOrders(responseData); const returnedOrdersCount = countReturnedOrders(responseData); + const { cod_monthly_totals, obpay_monthly_totals, months } = countMonthlySales(responseData); + const { cod_daily_totals, obpay_daily_totals } = countDailySales(responseData); + const { cod_yearly_totals, obpay_yearly_totals, years } = countYearlySales(responseData); initializeChart(completedOrdersCount, toPayOrdersCount, toShipOrdersCount, returnedOrdersCount); + initializeSalesChart(cod_daily_totals, obpay_daily_totals, cod_monthly_totals, + obpay_monthly_totals, months, cod_yearly_totals, obpay_yearly_totals, years + ); }) .catch(function (error) { console.error('Error fetching data:', error); @@ -47,6 +53,207 @@ $(document).ready(function() { return completedOrdersCount; } + function countYearlySales(data) { + const now = new Date(); + const years = []; + const cod_yearly_totals = Array(5).fill(0); + const obpay_yearly_totals = Array(5).fill(0); + + // Initialize years array with the current year and the previous four years + for (let i = 4; i >= 0; i--) { + const year = new Date(now.getFullYear() - i, 0, 1); // January 1st of each year + years.push(year); + } + + data.forEach(order => { + const orderDate = new Date(order.order_date); + orderDate.setHours(0, 0, 0, 0); + + // Check if payment status exists and is not null + if (order.payment && order.payment.status) { + const payment_status = order.payment.status.toLowerCase(); + + years.forEach((year, index) => { + if (orderDate.getFullYear() === year.getFullYear()) { + if (payment_status === "paid") { + const total_amount = parseInt(order.total_amount); + if (order.payment_method === "Cash On Delivery") { + cod_yearly_totals[4 - index] += total_amount; // Adjust index since we are going from current to past + } else if (order.payment_method === "Obananapay") { + obpay_yearly_totals[4 - index] += total_amount; + } + } + } + }); + } + }); + cod_yearly_totals.reverse(); + obpay_yearly_totals.reverse(); + return { + cod_yearly_totals, + obpay_yearly_totals, + years: years.map(year => year.getFullYear().toString()) // Return year labels for use in charts or displays + }; +} + + + function countMonthlySales(data) { + const now = new Date(); + const months = []; + const cod_monthly_totals = Array(12).fill(0); + const obpay_monthly_totals = Array(12).fill(0); + for (let i = 11; i >= 0; i--) { + const month = new Date(now.getFullYear(), now.getMonth() - i, 1); + months.push(month); + } + + data.forEach(order => { + if (order.payment && order.payment.status){ + const orderDate = new Date(order.order_date); + orderDate.setHours(0, 0, 0, 0); + const payment_status = order.payment.status.toLowerCase(); + + months.forEach((month, index) => { + if (orderDate.getFullYear() === month.getFullYear() && orderDate.getMonth() === month.getMonth()) { + if (payment_status === "paid") { + const total_amount = parseInt(order.total_amount); + if (order.payment_method === "Cash On Delivery") { + cod_monthly_totals[11 - index] += total_amount; + } else if (order.payment_method === "Obananapay") { + obpay_monthly_totals[11 - index] += total_amount; + } + } + } + }); + } + }); + console.log(cod_monthly_totals) + cod_monthly_totals.reverse(); + obpay_monthly_totals.reverse(); + return { + cod_monthly_totals, + obpay_monthly_totals, + months: months.map(month => `${month.toLocaleString('default', { month: 'short' })} ${month.getFullYear()}`) // Return month labels for use in charts or displays + }; +} + + + + function countDailySales(data) { + const today = new Date(); + today.setHours(0, 0, 0, 0); + + const sevenDaysAgo = new Date(); + sevenDaysAgo.setDate(today.getDate() - 6); + sevenDaysAgo.setHours(0, 0, 0, 0); + + const sixDaysAgo = new Date(); + sixDaysAgo.setDate(today.getDate() - 5); + sixDaysAgo.setHours(0, 0, 0, 0); + + const fiveDaysAgo = new Date(); + fiveDaysAgo.setDate(today.getDate() - 4); + fiveDaysAgo.setHours(0, 0, 0, 0); + + const fourDaysAgo = new Date(); + fourDaysAgo.setDate(today.getDate() - 3); + fourDaysAgo.setHours(0, 0, 0, 0); + + const threeDaysAgo = new Date(); + threeDaysAgo.setDate(today.getDate() - 2); + threeDaysAgo.setHours(0, 0, 0, 0); + + const twoDaysAgo = new Date(); + twoDaysAgo.setDate(today.getDate() - 1); + twoDaysAgo.setHours(0, 0, 0, 0); + //variables for total daily sales on COD + let cod_total_7 = 0; + let cod_total_6 = 0; + let cod_total_5 = 0; + let cod_total_4 = 0; + let cod_total_3 = 0; + let cod_total_2 = 0; + let cod_total_1 = 0; + + //variables for total daily sales on ObananaPay + let obpay_total_7 = 0; + let obpay_total_6 = 0; + let obpay_total_5 = 0; + let obpay_total_4 = 0; + let obpay_total_3 = 0; + let obpay_total_2 = 0; + let obpay_total_1 = 0; + + + data.forEach(order => { + const orderDate = new Date(order.order_date); + orderDate.setHours(0, 0, 0, 0); + const payment_status = order.payment.status; + if (orderDate.getTime() === sevenDaysAgo.getTime() ) { + if(payment_status.toLowerCase() === "paid" && order.payment_method === "Cash On Delivery" ){ + cod_total_7 += parseInt(order.total_amount); + console.log(order) + // console.log("compare:" + sevenDaysAgo) + }else if (payment_status.toLowerCase() === "paid" && order.payment_method === "Obananapay") { + obpay_total_7 += parseInt(order.total_amount); + } + }else if (orderDate.getTime() === sixDaysAgo.getTime() ) { + if(payment_status.toLowerCase() === "paid" && order.payment_method === "Cash On Delivery" ){ + cod_total_6 += parseInt(order.total_amount); + console.log(order) + // console.log("compare:" + sevenDaysAgo) + }else if (payment_status.toLowerCase() === "paid" && order.payment_method === "Obananapay") { + obpay_total_6 += parseInt(order.total_amount); + } + }else if (orderDate.getTime() === fiveDaysAgo.getTime() ) { + if(payment_status.toLowerCase() === "paid" && order.payment_method === "Cash On Delivery" ){ + cod_total_5 += parseInt(order.total_amount); + console.log(order) + // console.log("compare:" + sevenDaysAgo) + }else if (payment_status.toLowerCase() === "paid" && order.payment_method === "Obananapay") { + obpay_total_5 += parseInt(order.total_amount); + } + }else if (orderDate.getTime() === fourDaysAgo.getTime() ) { + if(payment_status.toLowerCase() === "paid" && order.payment_method === "Cash on Delivery" ){ + cod_total_4 += parseInt(order.total_amount); + console.log(order) + // console.log("compare:" + sevenDaysAgo) + }else if (payment_status.toLowerCase() === "paid" && order.payment_method === "Obananapay") { + obpay_total_4 += parseInt(order.total_amount); + } + }else if (orderDate.getTime() === threeDaysAgo.getTime() ) { + if(payment_status.toLowerCase() === "paid" && order.payment_method === "Cash On Delivery" ){ + cod_total_3 += parseInt(order.total_amount); + console.log(order) + // console.log("compare:" + sevenDaysAgo) + }else if (payment_status.toLowerCase() === "paid" && order.payment_method === "Obananapay") { + obpay_total_3 += parseInt(order.total_amount); + } + }else if (orderDate.getTime() === twoDaysAgo.getTime() ) { + if(payment_status.toLowerCase() === "paid" && order.payment_method === "Cash On Delivery" ){ + cod_total_2 += parseInt(order.total_amount); + console.log(order) + // console.log("compare:" + sevenDaysAgo) + }else if (payment_status.toLowerCase() === "paid" && order.payment_method === "Obananapay") { + obpay_total_2 += parseInt(order.total_amount); + } + }else if (orderDate.getTime() === today.getTime() ) { + if(payment_status.toLowerCase() === "paid" && order.payment_method === "Cash On Delivery" ){ + cod_total_1 += parseInt(order.total_amount); + console.log(order) + // console.log("compare:" + sevenDaysAgo) + }else if (payment_status.toLowerCase() === "paid" && order.payment_method === "Obananapay") { + obpay_total_1 += parseInt(order.total_amount); + } + } + }); + const cod_daily_totals = [cod_total_7, cod_total_6, cod_total_5, cod_total_4, cod_total_3, cod_total_2, cod_total_1,]; + const obpay_daily_totals = [obpay_total_7, obpay_total_6, obpay_total_5, obpay_total_4, obpay_total_3, obpay_total_2, obpay_total_1,]; + // console.log(total_7); + return { cod_daily_totals, obpay_daily_totals }; + } + + function countToPayOrders(data) { const filteredData = getCurrentMonthData(data); let toPayOrdersCount = 0; @@ -127,11 +334,13 @@ $(document).ready(function() { } } }); + } else { + } } - + function initializeSalesChart(cod_daily_totals, obpay_daily_totals, cod_monthly_totals, obpay_monthly_totals, months, cod_yearly_totals, obpay_yearly_totals, years) { var acquisition = document.getElementById("salesChart"); if (acquisition !== null) { function updateDailyLabels() { @@ -151,23 +360,23 @@ $(document).ready(function() { // Updating labelsDaily dynamically var labelsDaily = updateDailyLabels(); - var labelsMonthly = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; - var labelsYearly = ["2021", "2022", "2023", "2024", "2025"]; + var labelsMonthly = months; + var labelsYearly = years; var acqData = [ { //daily data - first: [91, 180, 44, 75, 150, 66, 70], //COD - second: [300, 44, 177, 76, 23, 189, 12], //ObPay + first: cod_daily_totals, //COD + second: obpay_daily_totals, //ObPay third: [44, 167, 102, 123, 183, 88, 134] //Paymongo }, { //monthly data - first: [144, 44, 110, 5, 123, 89, 12], //COD - second: [22, 123, 45, 130, 112, 54, 181], //ObPay + first: cod_monthly_totals, //COD + second: obpay_monthly_totals, //ObPay third: [55, 44, 144, 75, 155, 166, 70] //Paymongo }, { //yearly data - first: [134, 80, 123, 65, 171, 33, 22], //COD - second: [44, 144, 77, 76, 123, 89, 112], //ObPay - third: [156, 23, 165, 88, 112, 54, 181] //Paymongo + first: cod_yearly_totals, //COD + second: obpay_yearly_totals, //ObPay + third: [156, 23, 165, 88, 112 ] //Paymongo } ]; @@ -253,8 +462,8 @@ $(document).ready(function() { }, ticks: { beginAtZero: true, - stepSize: 100, - max: 1000 + stepSize: 20000, + max: 200000 } } ] @@ -313,9 +522,10 @@ $(document).ready(function() { lineAcq.update(); }); -}); + }); items[0].click(); } + } function convertToCSV(data) { const now = new Date(); diff --git a/vendor-payments.php b/vendor-payments.php index e30f341..2474204 100644 --- a/vendor-payments.php +++ b/vendor-payments.php @@ -209,16 +209,16 @@ if ($_SESSION["isCustomer"] == true) {