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
4 changes: 4 additions & 0 deletions pms/templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -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: #8e44ad;">
<h5 class="small">% Ocupación</h5>
<h1 class="dashboard-value">{{dashboard.occupancy}}%</h1>
</div>
</div>
</div>
{% endblock content%}
59 changes: 57 additions & 2 deletions pms/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
from django.test import TestCase
from datetime import date

# Create your tests here.
from django.test import TestCase, Client
from django.urls import reverse

from .models import Room, Room_type, Booking, Customer


class DashboardOccupancyTest(TestCase):
def setUp(self):
self.client = Client()
self.room_type = Room_type.objects.create(name="Individual", price=20, max_guests=1)
self.room1 = Room.objects.create(name="Room 1", room_type=self.room_type, description="")
self.room2 = Room.objects.create(name="Room 2", room_type=self.room_type, description="")
self.customer = Customer.objects.create(name="Test", email="test@test.com", phone="123")

def test_occupancy_zero_when_no_bookings(self):
response = self.client.get(reverse("dashboard"))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["dashboard"]["occupancy"], 0)

def test_occupancy_with_confirmed_bookings(self):
Booking.objects.create(
checkin=date(2025, 1, 1), checkout=date(2025, 1, 3),
room=self.room1, customer=self.customer,
guests=1, total=40, code="AAAA1111", state="NEW"
)
response = self.client.get(reverse("dashboard"))
# 1 confirmed / 2 rooms = 50%
self.assertEqual(response.context["dashboard"]["occupancy"], 50.0)

def test_occupancy_ignores_deleted_bookings(self):
Booking.objects.create(
checkin=date(2025, 1, 1), checkout=date(2025, 1, 3),
room=self.room1, customer=self.customer,
guests=1, total=40, code="AAAA1111", state="DEL"
)
response = self.client.get(reverse("dashboard"))
self.assertEqual(response.context["dashboard"]["occupancy"], 0)

def test_occupancy_100_percent(self):
Booking.objects.create(
checkin=date(2025, 1, 1), checkout=date(2025, 1, 3),
room=self.room1, customer=self.customer,
guests=1, total=40, code="AAAA1111", state="NEW"
)
Booking.objects.create(
checkin=date(2025, 1, 1), checkout=date(2025, 1, 3),
room=self.room2, customer=self.customer,
guests=1, total=40, code="BBBB2222", state="NEW"
)
response = self.client.get(reverse("dashboard"))
self.assertEqual(response.context["dashboard"]["occupancy"], 100.0)

def test_occupancy_no_rooms(self):
Room.objects.all().delete()
response = self.client.get(reverse("dashboard"))
self.assertEqual(response.context["dashboard"]["occupancy"], 0)
11 changes: 8 additions & 3 deletions pms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,25 @@ def get(self, request):
.values("id")
).count()

# get outcoming guests
# get invoiced total
invoiced = (Booking.objects
.filter(created__range=today_range)
.exclude(state="DEL")
.aggregate(Sum('total'))
)

# calculate occupancy percentage
total_rooms = Room.objects.count()
confirmed_bookings = Booking.objects.filter(state="NEW").count()
occupancy = round((confirmed_bookings / total_rooms) * 100, 2) if total_rooms > 0 else 0

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

'invoiced': invoiced,
'occupancy': occupancy,
}

context = {
Expand Down