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,6 +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="card text-white p-3 card-customization" style="background-color: #1000ff;">
<h5 class="small">Reservas hechas</h5>
Expand All @@ -17,7 +18,10 @@ <h1 class="dashboard-value">{{dashboard.incoming_guests}}</h1>
<h5 class="small">Huéspedes saliendo</h5>
<h1 class="dashboard-value">{{dashboard.outcoming_guests}}</h1>
</div>

<div class="card text-white p-3 card-customization" style="background-color: #a558ee;">
<h5 class="small">Ocupación actual</h5>
<h1 class="dashboard-value">{{dashboard.occupation_percentage |floatformat:"-2" }} %</h1>
</div>
<div class="card text-white p-3 card-customization" style="background-color: #ff7f7f;">
<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>
Expand Down
68 changes: 66 additions & 2 deletions pms/tests.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 17 additions & 4 deletions pms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = {
Expand Down