I want to create a sever script formula , that multiplies number of items*rate to give the total amount , but it does not work please help
import frappe
from frappe.model.document import Document
class CashRequisition(Document):
def validate(self):
# Trigger calculation on validation before saving the document
self.calculate_totals()
def calculate_totals(self):
total_quantity = 0
total_amount = 0
# Loop through each row in the child table "Cash Requisition Item"
for item in self.items:
# Calculate amount for each row
if item.number_of_item and item.rate:
item.amount = item.number_of_item * item.rate
else:
item.amount = 0 # Handle cases where values might be missing
# Add to the total amount and total quantity
total_quantity += item.number_of_item or 0
total_amount += item.amount
# Set the total_quantity and total_amount fields using self.set to track changes
self.set('total_quantity', total_quantity)
self.set('total_amount', total_amount)