1

so, I made a Django code for booking car online services , renter can rent a car by pressing on a booking button that moves him to form page to enter his his information and chooses a starting and end date, so once he submits, the balance from his account to the owner's account will be transfers according to a specific mount that depends on the number days he picked ,, but the problem is -> the mount is not linked to the date fields so weather he chooses the date or not the mount will still be the same here's the view that handles the transaction...

def transfer_rent(renter, owner, amount,start_date,end_date):
    if (renter.balance < amount):
        return False
    else:

        renter.balance -= amount
        owner.balance += amount 
        renter.save()
        owner.save()
        

        return True 



def booking(request,pk):
    form = bookingCreateForm(request.POST or None)
    user = request.user

    if form.is_valid():

        rent = Rent.objects.get(pk=pk)
        car_owner = rent.owner

        if transfer_rent(user, car_owner, rent.per_day):        
            form.save()
            messages.success(request,'Successfully Saved')
            return redirect('/list_history')


        else: 
            messages.error(request, 'Insufficient balance')
            return redirect('/list_history')

    context = {
        "form": form,
        "title": "Enter Your Credintials",
    }
    return render(request, "booking.html",context)
aynber
  • 22,380
  • 8
  • 50
  • 63
  • first, you need to check the difference between the start day and the end date.you check with delta: delta = start_date - end_date . now you can do some changes in the amount base on your plan. – Saeed Ramezani Mar 06 '21 at 11:18
  • thanks for your help mate , but to be honest, i don't know where to but this line or how can check the difference – Basil Awoda Mar 07 '21 at 10:41

1 Answers1

0

so here is an example that you looking for:

from datetime import date
def transfer_rent(renter, owner, amount,start_date,end_date):
    if (renter.balance < amount):
        return False
    else:
        date_diffs = end_date - start_date # difference of two dates
        date_diffs_day = date_diffs.days # difference in days ex=2 
        # now you can change amount by including date_diffs_day
        amount = amount * date_diffs_day 
        renter.balance -= amount
        owner.balance += amount 
        renter.save()
        owner.save()
        return True 

for more info about the date differences check this link

Saeed Ramezani
  • 462
  • 6
  • 20