Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion BookStation/BookStation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
'orders',
'home',
'staff',

'reviews',
'analytics',
]
AUTH_USER_MODEL = 'accounts.Users'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Expand Down
3 changes: 3 additions & 0 deletions BookStation/BookStation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
path('orders/', include('orders.urls')),
path('',include('home.urls')),
path('staff/', include('staff.urls')),
path('reviews/', include('reviews.urls')),
path('analytics/', include('analytics.urls')),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG:
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions BookStation/analytics/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions BookStation/analytics/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AnalyticsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'analytics'
Empty file.
3 changes: 3 additions & 0 deletions BookStation/analytics/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
114 changes: 114 additions & 0 deletions BookStation/analytics/templates/analytics/book_inventory_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{% extends 'base.html' %}
{% load static %}

{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/analytics/book_inventory_detail.css' %}">
{% endblock %}

{% block content %}
<div class="analytics-container">
<div class="inventory-header">
<div class="stats-header">
<h1>Thống Kê BookStation</h1>
<a href="{% url 'analytics:dashboard' %}" class = "dashboard-btn">Dashboard</a>
</div>
<!-- Tổng quan -->
<div class="stats-grid">
<div class="stat-card">
<div class="stat-label">Tổng Số Sách</div>
<div class="stat-number">{{ total_books }}</div>
</div>
<div class="stat-card">
<div class="stat-label">Hết Hàng</div>
<div class="stat-number">{{ out_of_stock }}</div>
</div>
<div class="stat-card">
<div class="stat-label">Sắp Hết</div>
<div class="stat-number">{{ low_stock }}</div>
</div>
</div>

<!-- Thanh công cụ -->
<div class="toolbar">
<form method="GET" class="search-form">
<input type="text" name="search" value="{{ search_query }}"
placeholder="Tìm kiếm sách, tác giả, NXB...">
<select name="sort">
<option value="-stock" {% if sort_by == '-stock' %}selected{% endif %}>
Tồn kho (Cao → Thấp)
</option>
<option value="stock" {% if sort_by == 'stock' %}selected{% endif %}>
Tồn kho (Thấp → Cao)
</option>
<option value="-total_sold" {% if sort_by == '-total_sold' %}selected{% endif %}>
Đã bán (Cao → Thấp)
</option>
<option value="title" {% if sort_by == 'title' %}selected{% endif %}>
Tên sách (A → Z)
</option>
</select>

<button type="submit" class="btn btn-primary">Lọc</button>
<a href="?export=excel" class="btn btn-success">Xuất Excel</a>
</form>
</div>
</div>

<!-- Bảng dữ liệu -->
<div class="inventory-table">
<table class="data-table">
<thead>
<tr>
<th>Tên Sách</th>
<th>Tác Giả</th>
<th>NXB</th>
<th>Tồn Kho</th>
<th>Đã Bán</th>
<th>Giá ($)</th>
<th>Trạng Thái</th>
</tr>
</thead>
<tbody>
{% for book in page_obj %}
<tr {% if book.stock == 0 %}class="out-of-stock"{% elif book.stock <= 5 %}class="low-stock"{% endif %}>
<td>{{ book.title }}</td>
<td>{{ book.author.name }}</td>
<td>{{ book.publisher.name }}</td>
<td>{{ book.stock }}</td>
<td>{{ book.total_sold|default:"0" }}</td>
<td>{{ book.price }}</td>
<td>
{% if book.stock == 0 %}
<span class="status-badge status-out">Hết hàng</span>
{% elif book.stock <= 5 %}
<span class="status-badge status-low">Sắp hết</span>
{% else %}
<span class="status-badge status-ok">Còn hàng</span>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>

<!-- Phân trang -->
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page=1&search={{ search_query }}&sort={{ sort_by }}">&laquo; Đầu</a>
<a href="?page={{ page_obj.previous_page_number }}&search={{ search_query }}&sort={{ sort_by }}">Trước</a>
{% endif %}

<span class="current">
Trang {{ page_obj.number }} / {{ page_obj.paginator.num_pages }}
</span>

{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}&search={{ search_query }}&sort={{ sort_by }}">Tiếp</a>
<a href="?page={{ page_obj.paginator.num_pages }}&search={{ search_query }}&sort={{ sort_by }}">Cuối &raquo;</a>
{% endif %}
</span>
</div>
</div>
</div>
{% endblock %}
109 changes: 109 additions & 0 deletions BookStation/analytics/templates/analytics/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{% extends 'base.html' %}
{% load static %}

{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/analytics/analytics.css' %}">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
{% endblock %}

{% block content %}
<div class="analytics-container">
<div class="stats-header">
<h1>Thống Kê BookStation</h1>

<!-- Navigation Tabs -->
<div class="nav-tabs">
<a href="{% url 'analytics:dashboard' %}" class="nav-button active">
Dashboard
</a>
<a href="{% url 'analytics:book_inventory_detail' %}" class="nav-button">
Thống Kê Kho Sách
</a>
</div>
</div>

<!-- Thống kê doanh thu -->
<div class="section">
<div class="section-header">
<h2>Doanh Thu & Đơn Hàng</h2>
</div>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-label">Tổng Doanh Thu</div>
<div class="stat-number">${{ total_revenue|floatformat:2 }}</div>
</div>
<div class="stat-card">
<div class="stat-label">Tổng Đơn Hàng</div>
<div class="stat-number">{{ total_orders }}</div>
</div>
<div class="stat-card">
<div class="stat-label">Khách Hàng Mới (30 ngày)</div>
<div class="stat-number">{{ new_customers }}</div>
</div>
</div>

<!-- Biểu đồ doanh thu -->
<div class="chart-container">
<canvas id="revenueChart"></canvas>
</div>

<!-- Bảng chi tiết -->
<div class="data-table-container">
<h3>Chi Tiết Doanh Thu Theo Tháng</h3>
<table class="data-table">
<thead>
<tr>
<th>Tháng</th>
<th>Doanh Thu</th>
<th>Số Đơn Hàng</th>
</tr>
</thead>
<tbody>
{% for month in revenue_by_month %}
<tr>
<td>{{ month.month|date:"m/Y" }}</td>
<td>${{ month.revenue|floatformat:2 }}</td>
<td>{{ month.order_count }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>

<!-- Thống kê sách -->
<div class="section">
<div class="section-header">
<h2>Kho Sách</h2>
<a href="{% url 'analytics:book_inventory_detail' %}" class="nav-button">
Xem Chi Tiết Kho Sách →
</a>
</div>

<div class="stats-grid">
<div class="stat-card">
<div class="stat-label">Tổng Số Sách</div>
<div class="stat-number">{{ total_books }}</div>
</div>
<div class="stat-card {% if out_of_stock_books.count > 0 %}alert-danger{% endif %}">
<div class="stat-label">Sách Hết Hàng</div>
<div class="stat-number">{{ out_of_stock_books.count }}</div>
</div>
<div class="stat-card {% if low_stock_books.count > 0 %}alert-warning{% endif %}">
<div class="stat-label">Sách Sắp Hết</div>
<div class="stat-number">{{ low_stock_books.count }}</div>
</div>
</div>

<!-- Top sách bán chạy -->
<div class="chart-container">
<h3>Top 10 Sách Bán Chạy</h3>
<canvas id="bestSellingChart"></canvas>
</div>
</div>
</div>

{% block extra_js %}
// ...existing chart.js code...
{% endblock %}
{% endblock %}
3 changes: 3 additions & 0 deletions BookStation/analytics/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions BookStation/analytics/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from . import views

app_name = 'analytics'

urlpatterns = [
path('dashboard/', views.analytics_dashboard, name='dashboard'),
path('books/inventory/', views.book_inventory_detail, name='book_inventory_detail'),
]
Loading
Loading