diff --git a/.idea/testbookingengine.iml b/.idea/testbookingengine.iml index 690497d10..766f607d0 100644 --- a/.idea/testbookingengine.iml +++ b/.idea/testbookingengine.iml @@ -13,8 +13,10 @@ - - + + + + diff --git a/pms/templates/dashboard.html b/pms/templates/dashboard.html index 10f0285cc..952fbf66d 100644 --- a/pms/templates/dashboard.html +++ b/pms/templates/dashboard.html @@ -1,27 +1,30 @@ {% extends "main.html"%} {% block content %} -

Dashboard

-
+

Dashboard

+
Hoy
-
-
Reservas hechas
-

{{dashboard.new_bookings}}

-
-
-
Huéspedes ingresando
-

{{dashboard.incoming_guests}}

-
-
-
Huéspedes saliendo
-

{{dashboard.outcoming_guests}}

-
- -
-
Total facturado
-

€ {% if dashboard.invoiced.total__sum == None %}0.00{% endif %} {{dashboard.invoiced.total__sum|floatformat:2}}

-
+
+
Reservas hechas
+

{{dashboard.new_bookings}}

+
+
+
Huéspedes ingresando
+

{{dashboard.incoming_guests}}

+
+
+
Huéspedes saliendo
+

{{dashboard.outcoming_guests}}

+
+
+
% ocupación
+

{{dashboard.occupation_percentage|floatformat:2}}%

+
+
+
Total facturado
+

€ {% if dashboard.invoiced.total__sum == None %}0.00{% endif %} {{dashboard.invoiced.total__sum|floatformat:2}}

+
-
+
{% endblock content%} \ No newline at end of file diff --git a/pms/tests.py b/pms/tests.py index 7ce503c2d..c98bc53be 100644 --- a/pms/tests.py +++ b/pms/tests.py @@ -1,3 +1,49 @@ -from django.test import TestCase +from django.test import TestCase, override_settings +from django.urls import reverse + +from pms.models import Room, Room_type, Customer, Booking + # Create your tests here. +@override_settings(STATICFILES_STORAGE="django.contrib.staticfiles.storage.StaticFilesStorage") +class DashboardViewTest(TestCase): + def setUp(self): + room_type = Room_type.objects.create( + name="Single", + max_guests=1, + price=20 + ) + + room_1 = Room.objects.create(name="Room 1.1", room_type=room_type) + Room.objects.create(name="Room 1.2", room_type=room_type) + + customer = Customer.objects.create( + name="John Doe", + email="john@example.com", + phone="123456789" + ) + + Booking.objects.create( + room=room_1, + customer=customer, + checkin="2022-01-01", + checkout="2022-01-02", + guests=1, + total=20, + code="ABC123", + state="NEW" + ) + + def test_dashboard_shows_occupation_percentage(self): + response = self.client.get(reverse("dashboard")) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, "% ocupación") + self.assertContains(response, "50.00%") + + def test_dashboard_shows_zero_occupation_when_no_rooms_exist(self): + response = self.client.get(reverse("dashboard")) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, "% ocupación") + self.assertContains(response, "0.00%") \ No newline at end of file diff --git a/pms/views.py b/pms/views.py index f38563933..4bf61b6af 100644 --- a/pms/views.py +++ b/pms/views.py @@ -208,14 +208,24 @@ def get(self, request): .exclude(state="DEL") .aggregate(Sum('total')) ) + # calculate the occupation percentage + total_rooms = Room.objects.count() + + # get the total number of confirmed bookings + confirmed_bookings = Booking.objects.filter(state="NEW").count() + + # prevent division by zero when no rooms exist + occupation_percentage = 0 + if total_rooms > 0: + occupation_percentage = (confirmed_bookings / total_rooms) * 100 # preparing context data dashboard = { 'new_bookings': new_bookings, 'incoming_guests': incoming, 'outcoming_guests': outcoming, - 'invoiced': invoiced - + 'invoiced': invoiced, + 'occupation_percentage': occupation_percentage } context = {