How to override a function server side

I need to override the function that calculate the amount in the quotation item quantity * rate by quantity * rate * length * width knowing i had changed in the client side but when saving the quotation it returns to the original formula I tried the two following script but I can’t seem to access the field anyone got any ideas ? thank you in advance.
I’m using doctype event:

amount = flt(doc.quantity) * flt(doc.rate) * flt(doc.length) * flt(doc.width)
frappe.db.set_value("Quotation Item",, "amount", amount)
print("Script is working")

and the second one:

def before_save(doc, event):
    doc.amount = doc.quantity * doc.rate * doc.length * doc.width
    print("Script is working")

Not sure if it’s the same, but I needed to overwrite a standard item price on occasion for Sales Invoices. My solution is janky, but it worked. I added a setTimeout so my custom prices were able to populate in the rate field after frappe had pulled the stand price through.

setTimeout(function () {
    frappe.model.set_value(child_row.doctype,, "rate", item_rate);
}, 1000);

but isn’t your code for client-side ?

Yeah it’s client side. I’m sure there’d be a better solution for my application, but it’s working …for now.

I have already did the client-side logic but it is not enough since it does not override the original formula when saving the quotation

You could try using validate? Also make sure the method is added to hooks (doc_events).

def validate(doc, method):
    for item in doc.items:
        item.amount = flt(item.qty) * flt(item.rate) * flt(item.get('length', 0)) * flt(item.get('width', 0))
doc_events = {
    "Quotation": {
        "validate": "your_app.your_module.your_method"

This is untested…