Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Fix calculation total orders
Browse files Browse the repository at this point in the history
  • Loading branch information
haneeva authored and mmafrar committed Jun 2, 2024
1 parent 9c2eb1d commit a14bd73
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 53 deletions.
72 changes: 32 additions & 40 deletions dashboard/static/js/dashboard-chart.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
// Dummy data for demonstration purposes
const totalOrders = 100;
const acceptedOrders = 90;
const rejectedOrders = 10;
const totalRevenue = 5000.00;
document.addEventListener('DOMContentLoaded', function () {
const ctx = document.getElementById('revenueChart').getContext('2d');
const weeks = JSON.parse('{{ weeks|escapejs }}');
const revenues = JSON.parse('{{ revenues|escapejs }}');

const monthlyRevenueData = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
datasets: [{
label: 'Revenue',
data: [1500, 2000, 1800, 2500, 2200, 2800, 3000, 3200, 2700, 3100, 3500, 4000], // Dummy monthly revenue data
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
tension: 0.4
}]
};
const weeklyRevenueData = {
labels: weeks,
datasets: [{
label: 'Revenue',
data: revenues,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
tension: 0.4
}]
};

// Update dashboard with dummy data
document.getElementById('totalOrders').innerText = totalOrders;
document.getElementById('acceptedOrders').innerText = acceptedOrders;
document.getElementById('rejectedOrders').innerText = rejectedOrders;
document.getElementById('totalRevenue').innerText = totalRevenue.toFixed(2);

// Render revenue chart
const ctx = document.getElementById('revenueChart').getContext('2d');
const revenueChart = new Chart(ctx, {
type: 'line',
data: monthlyRevenueData,
options: {
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Revenue (RM)'
}
},
x: {
title: {
display: true,
text: 'Month'
const revenueChart = new Chart(ctx, {
type: 'line',
data: weeklyRevenueData,
options: {
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Revenue (RM)'
}
},
x: {
title: {
display: true,
text: 'Week'
}
}
}
}
}
});
});
20 changes: 8 additions & 12 deletions dashboard/templates/dashboard/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,30 @@ <h1>Dashboard</h1>
<div style="display: flex; justify-content: space-between; margin-bottom: 20px;">
<div class="section card" id="totalOrdersSection">
<h4>Total Orders</h4>
<p><span id="totalOrders">0</span></p>
<p><span id="totalOrders">{{ total_orders }}</span></p>
</div>
<div class="section card" id="acceptedOrdersSection">
<h4>Total Accepted</h4>
<p><span id="acceptedOrders">0</span></p>
<p><span id="acceptedOrders">{{ total_accepted }}</span></p>
</div>
<div class="section card" id="rejectedOrdersSection">
<h4>Total Rejected</h4>
<p><span id="rejectedOrders">0</span></p>
<p><span id="rejectedOrders">{{ total_rejected }}</span></p>
</div>
<div class="section card" id="revenueSection">
<h4>Total Revenue</h4>
<p>RM <span id="totalRevenue">0.00</span></p>
<p>RM <span id="totalRevenue">{{ total_revenue }}</span></p>
</div>
</div>

<div class="section card" id="popularItemsSection">
<h4>Popular Menu Items</h4>
<ul id="popularItems">
<li>TUNA SAN</li>
<li>BERRY GRAPEFUL</li>
<li>PEANUT BUTTER COCONUT OATMEAL</li>
</ul>
<div class="section card" id="popularMenuSection">
<h4>Most Popular Menu Item</h4>
<p><span id="popularMenuItem"><strong>{{ popular_menu_item }}</strong></span></p>
</div>

<!-- Revenue Chart Section -->
<div class="section card" id="revenueChartSection">
<h4>Revenue Chart (Monthly)</h4>
<h4>Revenue Chart (Weekly)</h4>
<div class="chart-container">
<canvas id="revenueChart"></canvas>
</div>
Expand Down
40 changes: 39 additions & 1 deletion dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,50 @@
from orders.models import Order
from branches.models import Branch, OpeningHour
from branches.form import BranchForm, OpeningHourFormSet
from django.db.models import Sum, Count
from django.db.models.functions import TruncWeek
import json


class ViewDashboardView(View):

def get(self, request):
return render(request, "dashboard/index.html")
total_orders = Order.objects.count()
total_accepted = Order.objects.filter(order_status='2').count()
total_rejected = Order.objects.filter(order_status='3').count()
total_revenue = Order.objects.filter(order_status='2').aggregate(
total_revenue=Sum('total_amount'))['total_revenue']

popular_menu_item_data = Order.objects.values('menu_id').annotate(
count=Count('menu_id')).order_by('-count').first()
popular_menu_item = None
if popular_menu_item_data:
popular_menu_item = Menu.objects.get(
id=popular_menu_item_data['menu_id']).name

# Aggregate monthly revenue data
weekly_revenue = Order.objects.filter(order_status='2').annotate(
week=TruncWeek('created_at')
).values('week').annotate(
total_revenue=Sum('total_amount')
).order_by('week')

# Prepare data for Chart.js
weeks = [entry['week'].strftime('%Y-%m-%d')
for entry in weekly_revenue]
revenues = [entry['total_revenue'] for entry in weekly_revenue]

context = {
'total_orders': total_orders,
'total_accepted': total_accepted,
'total_rejected': total_rejected,
'total_revenue': total_revenue,
'popular_menu_item': popular_menu_item,
'weekly': json.dumps(weeks),
'revenues': json.dumps(revenues),
}

return render(request, "dashboard/index.html", context)


class ViewAdminBranchs(View):
Expand Down

0 comments on commit a14bd73

Please sign in to comment.