diff --git a/pms/templates/dashboard.html b/pms/templates/dashboard.html index 10f0285cc..d65d4be8f 100644 --- a/pms/templates/dashboard.html +++ b/pms/templates/dashboard.html @@ -4,6 +4,7 @@

Dashboard

Hoy
+
Reservas hechas
@@ -17,7 +18,10 @@

{{dashboard.incoming_guests}}

Huéspedes saliendo

{{dashboard.outcoming_guests}}

- +
+
Ocupación actual
+

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

+
Total facturado

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

diff --git a/pms/tests.py b/pms/tests.py index 7ce503c2d..5ac2e18ef 100644 --- a/pms/tests.py +++ b/pms/tests.py @@ -1,3 +1,67 @@ -from django.test import TestCase +from django.test import TestCase, Client,override_settings +from django.utils import timezone +from datetime import timedelta +from pms.models import Customer, Room_type, Room, Booking -# Create your tests here. +@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage') +class DashboardViewTests(TestCase): + def setUp(self): + self.client = Client() + self.url = '/dashboard/' + self.field = "occupation_percentage" + # Dates + self.today = timezone.now().date() + self.yesterday = self.today - timedelta(days=1) + self.tomorrow = self.today + timedelta(days=1) + + # Create customer and room type + self.customer = Customer.objects.create(name="Test User", email="test@test.com", phone="123456789") + self.room_type = Room_type.objects.create(name="Doble", price=50.0, max_guests=2) + + # Create 2 rooms + self.room1 = Room.objects.create(name="101", room_type=self.room_type, description="A") + self.room2 = Room.objects.create(name="102", room_type=self.room_type, description="B") + + + # Reservation A: incoming today, outcoming tomorrow + self.b1 = Booking.objects.create( + checkin=self.today, checkout=self.tomorrow, room=self.room1, + guests=1, customer=self.customer, total=100.0, code="B001", state=Booking.NEW + ) + + # Reservation B: incoming yesterday, outcoming today + self.b2 = Booking.objects.create( + checkin=self.yesterday, checkout=self.today, room=self.room2, + guests=1, customer=self.customer, total=150.0, code="B002", state=Booking.NEW + ) + + # Reservation C: incoming today, but is cancelled + self.b3 = Booking.objects.create( + checkin=self.today, checkout=self.tomorrow, room=self.room1, + guests=1, customer=self.customer, total=500.0, code="B003", state=Booking.DELETED + ) + + def test_dashboard_occupation_percentage_calculation(self): + """ + Case: 2 rooms total, 1 active reservation crossing today, 1 checkout today, 1 cancelled. + Expect: 50.0% occupation + """ + response = self.client.get(self.url) + dashboard = response.context['dashboard'] + + self.assertEqual(dashboard[self.field], 50.0) + + + def test_dashboard_zero_rooms_available(self): + """ + Case: 0 rooms available in DB + Expect: 0% occupation + """ + + # Delete rooms + Room.objects.all().delete() + + response = self.client.get(self.url) + dashboard = response.context['dashboard'] + + self.assertEqual(dashboard[self.field], 0) \ No newline at end of file diff --git a/pms/views.py b/pms/views.py index f38563933..6f8663615 100644 --- a/pms/views.py +++ b/pms/views.py @@ -187,7 +187,7 @@ def get(self, request): .filter(created__range=today_range) .values("id") ).count() - + # get incoming guests incoming = (Booking.objects .filter(checkin=today) @@ -208,14 +208,27 @@ def get(self, request): .exclude(state="DEL") .aggregate(Sum('total')) ) - + + # get total rooms + total_rooms = Room.objects.count() + + if total_rooms == 0: + occupation_percentage = 0 + else: + # get reservations in progress during today + confirmed_bookings_today = Booking.objects.filter( + checkin__lte=today, + checkout__gt=today + ).exclude(state="DEL").count() + occupation_percentage = (confirmed_bookings_today / 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 = {