#@login_required(login_url='user_login')
def Container_booking(request):
shared_space =
[Link](category__name__contains='shared_spaces')
private_space =
[Link](category__name__contains='private_spaces')
context = {
'shared_spaces': shared_space,
'private_spaces': private_space,
}
return render(request,'dashboard_students/book_cont.html',context)
#@login_required(login_url='user_login')
def Book_spaces(request):
if [Link].is_authenticated:
if [Link] == 'POST':
user = [Link] # Get the authenticated user
if hasattr(user, 'student'):
student = [Link]
name = [Link]('name')
email = [Link]('email')
phonenumber = [Link]('phonenumber')
start_date = [Link]('start_date')
end_date = [Link]('end_date')
booking_number = str([Link](100000, 999999))
price = 0
space_ids = []
booking_spaces = {'spaces': []}
# Get the selected space IDs from the form
spaces = [Link]('spaces[]')
print(spaces)
if phonenumber:
if not ([Link]('254') and
len(phonenumber) == 12):
[Link](request, "Phone number must start
with '254' and have 12 digits.")
return render(request,
'dashboard_students/book_cont.html')
else:
[Link](request, "Phone number is required.")
return render(request,
'dashboard_students/book_cont.html')
if start_date and end_date:
try:
start_date_obj =
[Link](start_date)
end_date_obj = [Link](end_date)
except ValueError:
[Link](request, "Invalid date format.")
return render(request,
'dashboard_students/book_cont.html')
if start_date_obj > end_date_obj:
[Link](request, "End date should be after
start date.")
return render(request,
'dashboard_students/book_cont.html')
for space_id in spaces:
try:
container = [Link](pk=int(space_id))
# Container has available units, proceed with the
booking
if [Link] > 0:
# Calculate the number of days
start_date_obj =
[Link](start_date)
end_date_obj =
[Link](end_date)
num_days = (end_date_obj -
start_date_obj).days # Add 1 to include the end date
# Use the monthly rate as the space price
space_price = [Link] # Assuming
[Link] represents the monthly rate
# Calculate the price based on the number of
days
if num_days in [30, 31]:
mon_price = space_price
space_data = {
'id': [Link],
'name': [Link],
'price': mon_price # or [Link]
}
booking_spaces['spaces'].append(space_data)
# Decrease the number of units and
update status
[Link] -= 1
container.update_status()
price += mon_price
space_ids.append([Link])
else:
# Assuming there are 30 or 31 days in a
month for simplicity
daily_rate = space_price / 30 # Calculate
daily rate based on monthly rate
individual_price = num_days * daily_rate
round_price = round(individual_price, 2)
space_data = {
'id': [Link],
'name': [Link],
'price': round_price#individual_price #or
[Link]
}
booking_spaces['spaces'].append(space_data)
# Decrease the number of units and
update status
[Link] -= 1
container.update_status()
price += individual_price #or
[Link]
space_ids.append([Link])
else:
# Container has 0 units, prevent booking
[Link](request, f"{[Link]} is
fully booked and unavailable.")
except [Link]:
print(f"Container with ID {space_id} does not
exist.")
# Handle the case where a selected space does
not exist
#pass
if len(space_ids) > 0: # Create the booking record only if
there are selected spaces
# Create the booking record
price=sum(space['price'] for space in
booking_spaces['spaces']),
print(price)
if isinstance(price, tuple):
price = price[0]
if isinstance(price, Decimal):
numeric_value = [Link](Decimal('1.'),
rounding='ROUND_HALF_UP')
rounded_price = int(numeric_value)
print(rounded_price)
else:
rounded_price = price
formatted_price = "{:.2f}".format(rounded_price)
print(formatted_price)
booking = [Link](
price=rounded_price,
# price=sum(space['price'] for space in
booking_spaces['spaces']),
#price=price,
name=name,
email=email,
phonenumber=phonenumber,
start_date=start_date,
end_date=end_date,
booking_number=booking_number,
student=[Link], # Associate the booking
with the student
)
[Link](*space_ids)
context = {
'booking':booking,
'spaces': booking_spaces['spaces'],
'price': rounded_price,
'booking_id': [Link] # Include the booking ID
for reference
}
subject = 'Welcome to storage facility'
message = f'Hello {name},\n\nYour booking number is
{booking.booking_number}. Thank you for booking with us.\n\n The
booking period is from {booking.start_date} to {booking.end_date}'
send_mail(
subject,
message,
settings.EMAIL_HOST_USER,
[email],
fail_silently=False,
)
return render(request,
'dashboard_students/booking_confirmation.html', context)
else:
# Handle the case where no spaces were selected for
booking
[Link](request, "No spaces were selected for
booking.")
return render(request, 'dashboard_students/book_cont.html')
else:
return redirect('user_login')