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
6 changes: 5 additions & 1 deletion pms/templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<h1>Dashboard</h1>
<div class="card">
<h5 class="card-header">Hoy</h5>
<div class="d-flex justify-content-evenly pt-5 pb-5">
<div class="d-flex flex-wrap justify-content-center pt-5 pb-5 gap-3">
<div class="card text-white p-3 card-customization" style="background-color: #1000ff;">
<h5 class="small">Reservas hechas</h5>
<h1 class="dashboard-value">{{dashboard.new_bookings}}</h1>
Expand All @@ -22,6 +22,10 @@ <h1 class="dashboard-value">{{dashboard.outcoming_guests}}</h1>
<h5 class="small">Total facturado</h5>
<h1 class="dashboard-value">€ {% if dashboard.invoiced.total__sum == None %}0.00{% endif %} {{dashboard.invoiced.total__sum|floatformat:2}}</h1>
</div>
<div class="card text-white p-3 card-customization" style="background-color: #17a2b8;">
<h5 class="small">% Ocupación</h5>
<h1 class="dashboard-value">{{dashboard.occupancy_percentage}} %</h1>
</div>
</div>
</div>
{% endblock content%}
46 changes: 45 additions & 1 deletion pms/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
from django.test import TestCase
from django.urls import reverse
from datetime import date
from .models import Room, Room_type, Booking, Customer

# Create your tests here.
class DashboardViewTestCase(TestCase):
def setUp(self):
# Create Room Types
self.single_type = Room_type.objects.create(name="Individual", price=20, max_guests=1)

# Create Rooms
self.room1 = Room.objects.create(name="101", room_type=self.single_type, description="Room 101")
self.room2 = Room.objects.create(name="102", room_type=self.single_type, description="Room 102")
self.room3 = Room.objects.create(name="103", room_type=self.single_type, description="Room 103")
self.room4 = Room.objects.create(name="104", room_type=self.single_type, description="Room 104")

# Create Customer
self.customer = Customer.objects.create(name="Adan Perez", email="adan@perez.com", phone="123009988")

def test_occupancy_percentage_with_no_bookings(self):
# 0 bookings, 4 rooms === occupancy should be 0.0
response = self.client.get('/dashboard/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['dashboard']['occupancy_percentage'], 0.0)

def test_occupancy_percentage_with_bookings(self):
# Create *1 NEW* booking and *1 DEL* booking
Booking.objects.create(
state="NEW", checkin=date(2026, 4, 1), checkout=date(2026, 4, 5),
room=self.room1, guests=1, customer=self.customer, total=80, code="B001"
)
Booking.objects.create(
state="DEL", checkin=date(2026, 4, 1), checkout=date(2026, 4, 5),
room=self.room2, guests=1, customer=self.customer, total=80, code="B002"
)

# *1 confirmed* booking out of 4 rooms = 25.0%
response = self.client.get('/dashboard/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['dashboard']['occupancy_percentage'], 25.0)

def test_occupancy_percentage_zero_rooms(self):
Room.objects.all().delete()
# *0 confirmed* booking out of 0 rooms = 0.0% (to avoid divide by zero)
response = self.client.get('/dashboard/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['dashboard']['occupancy_percentage'], 0.0)
11 changes: 9 additions & 2 deletions pms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,20 @@ def get(self, request):
.aggregate(Sum('total'))
)

# get occupancy percentage
total_rooms = Room.objects.count()
total_confirmed_bookings = Booking.objects.filter(state="NEW").count()
occupancy_percentage = 0
if total_rooms > 0:
occupancy_percentage = (total_confirmed_bookings / total_rooms) * 100 # use formula to calculate occupancy percentage (Cantidad total de reservas en estado confirmada / número de habitaciones existentes.)

# preparing context data
dashboard = {
'new_bookings': new_bookings,
'incoming_guests': incoming,
'outcoming_guests': outcoming,
'invoiced': invoiced

'invoiced': invoiced,
'occupancy_percentage': round(occupancy_percentage, 2)
}

context = {
Expand Down