line_ids = client["account.invoice.line"].search([("invoice_id.type", "in", ["out_invoice", "out_refund"]), ("margin", "=", 0.0),("purchase_price", "!=", 0.0)]) lines = client ["account.invoice.line"].browse (line_ids) for line in lines: tmp_margin = line.price_subtotal (line.purchase_price* line.quantity) sign-1 if line.invoice_id.type in ["in_refund", "out_refund"] else 1 line.write( { "margin": tmp_margin, "margin_signed": tmp_margin * sign, "margin_percent": ( tmp_margin / line.price_subtotal * 100.0 if line.price_subtotal else 0.0 ), } )