This is a hotel management system.Guests checks in and out.I have a model for this and I would share that shortly.Amongs these guests are those who pay immedieately using different modes of payment and those who pay later(debtors).2AM is when we’re scheduled to close the account for the day.So at exactly 2am of each day,we want to be able to see the total money made for that day,those that paid and their means of payment and those who are debtors.My problem is,I don’t know how to write a for loop to collect this information and populate a table with it daily e.g table head is,Date,Total Sales,Money Received,Transfers,Cash,POS,Non-Paying,Debtors, and the table data would be each collected info at 2am.The earlier message I share showed the views for each section of this account summary.I would be looking forward to your response.
from django.db import models
from django.conf import settings
from rooms.models import Room
from django.urls import reverse
from django.utils import timezone
from django.core.exceptions import ValidationError
from datetime import datetime, date, timedelta, time
import math
from django.contrib.postgres.fields import DateRangeField
from django.utils import timezone
Create your models here.
class Reservation(models.Model):
discount_types = (
(None, 'No Discount'),
('10', '10%'),
('20', '20%'),
('30', '30%'),
('Oth', 'Other'),
)
room_stat_types = (
('reserve', 'reserve'),
('checkin', 'checkin'),
('checkout', 'checkout'),
)
roomstatus = models.CharField(
max_length=20, choices=room_stat_types, null=True, blank=True, default='reserve')
room_price = models.IntegerField(null=True, blank=True)
check_in = models.DateTimeField(
blank=True, null=True)
# check_in_in = models.DateTimeField(blank=True, null=True)
check_out = models.DateTimeField(
blank=True, null=True)
book_date = models.DateTimeField(default=timezone.now)
customer_name = models.CharField(max_length=225)
customer_address = models.CharField(max_length=225)
customer_number = models.CharField(max_length=225)
no_of_days = models.IntegerField(null=True, blank=True)
checking_in_now = models.BooleanField(default=False)
total_cost = models.IntegerField()
roomNumber = models.ForeignKey(
Room, related_name="guest_reserve", on_delete=models.CASCADE) # reservations
pay_types = (
('P', 'Paying'),
('PD', 'Paying Debtor'),
('NP', 'Non Paying'),
)
mode_pay_types = (
('C', 'Cash'),
('P', 'POS'),
('TF', 'Transfer'),
)
discount = models.CharField(
max_length=20, choices=discount_types, null=True, blank=True, default=None)
Other_amount = models.IntegerField(null=True, blank=True, default=None)
payment_type = models.CharField(
max_length=20, choices=pay_types, default="NP")
paid_date = models.DateTimeField(auto_now=True)
payment_mode = models.CharField(
max_length=20, choices=mode_pay_types, null=True, blank=True)
credit_balance = models.IntegerField(default=0)
def __str__(self):
return self.customer_name
def checkin(self):
self.check_in = timezone.now()
self.roomstatus = 'checkin'
self.save()
def checkout(self):
self.check_out = timezone.now()
self.roomstatus = 'checkout'
self.save()
# checking radion button when form is being filled
def check_in_now(self):
if self.checking_in_now == True:
self.check_in = timezone.now()
self.roomstatus = 'checkin'
# self.save()
def paid_date_update(self):
if self.payment_type == 'P':
self.paid_date == datetime.now()
# self.save()
def value(self):
if self.discount == '10':
price = self.roomNumber.roomtype.price
x = (price - (price*0.1))
return x
if self.discount == '20':
price = self.roomNumber.roomtype.price
x = (price - (price*0.2))
return x
if self.discount == '30':
price = self.roomNumber.roomtype.price
x = (price - (price*0.3))
return x
if self.discount == None:
x = self.roomNumber.roomtype.price
return x
# self.save()
def no_of_days_booked(self):
test_value = (
math.modf(abs(self.check_out - self.check_in).total_seconds()/60/60/24))
if ((test_value[0] > 0) and (test_value[0]) < 1):
# print(math.modf(test_value)[0])
return (test_value)[1] + 1
else:
return (test_value)[1]
def save(self, *args, **kwargs):
if self.Other_amount is not None:
self.room_price = self.Other_amount
else:
self.room_price = self.value()
if self.id:
print('updating')
else:
print('creating')
self.no_of_days = self.no_of_days_booked()
self.total_cost = self.no_of_days * self.room_price
if self.roomstatus == 'checkin':
self.credit_balance = self.total_cost - self.room_price
else:
self.credit_balance = self.total_cost
self.paid_date_update()
# print(datetime.now.date())
self.check_in_now()
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('reservations:all')