Discount and Margin in Sales invoice items


I have issue with discount and margin in Sales invoice item,
I have custom child table in Customer doctype, and I need to apply the discount in the sales invoice for each customer depend on the child table,

But when I update the fields (discount percentage, discount amount) it’s not update the rate and amount,

I think I miss some built in functionality

  def validate(self):
    customer = frappe.get_doc("Customer", self.customer)
    for i in customer.item_group_discount:
      for j in self.items:
        if i.item_group == j.item_group:
          j.margin_type = "Amount"
          j.discount_percentage = i.discount_percentage
          j.discount_amount = (j.rate * (j.discount_percentage / 100))

This what I see after validate, it’s fetch the right discount percentage but without any impact

Any help ?

I solve it