Add total deposit calculation to Rental model
This commit is contained in:
@@ -93,15 +93,10 @@ class RentalAdmin(admin.ModelAdmin):
|
||||
obj.author = request.user
|
||||
super().save_model(request, obj, form, change)
|
||||
|
||||
@admin.display(description="Kaution (EUR)")
|
||||
@admin.display(description="Kaution insgesamt")
|
||||
def total_disposit(self, obj):
|
||||
total_disposit = 0
|
||||
|
||||
if not obj.intern:
|
||||
for elem in obj.rentalitems.all():
|
||||
total_disposit += elem.deposit
|
||||
|
||||
return f"{total_disposit}"
|
||||
total_disposit = obj.calc_total_deposit()
|
||||
return f"{total_disposit} €"
|
||||
|
||||
|
||||
@admin.register(RentalItem)
|
||||
|
||||
@@ -111,3 +111,12 @@ class Rental(models.Model):
|
||||
|
||||
if self.date_start > self.date_end:
|
||||
raise ValidationError("Das Abholdatum muss vor dem Rückgabedatum liegen.")
|
||||
|
||||
def calc_total_deposit(self) -> int:
|
||||
total_deposit = 0
|
||||
|
||||
if not self.intern:
|
||||
for item in self.rentalitems.all():
|
||||
total_deposit += item.deposit
|
||||
|
||||
return total_deposit
|
||||
|
||||
@@ -27,12 +27,7 @@ def generate_rental_pdf(rental: Rental) -> bool:
|
||||
},
|
||||
)
|
||||
|
||||
total_deposit = 0
|
||||
|
||||
for i, item in enumerate(rental.rentalitems.all(), start=1):
|
||||
if not rental.intern:
|
||||
total_deposit += item.deposit
|
||||
|
||||
data.update(
|
||||
{
|
||||
f"Produkt Row{i}": item.name,
|
||||
@@ -41,6 +36,7 @@ def generate_rental_pdf(rental: Rental) -> bool:
|
||||
},
|
||||
)
|
||||
|
||||
total_deposit = rental.calc_total_deposit()
|
||||
data.update({"Gesamtkaution": str(total_deposit)})
|
||||
|
||||
# Write data in pdf
|
||||
|
||||
Reference in New Issue
Block a user