[fix] calculated rate on py side, added depends on for sales order item
diff --git a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
index 59f0322..78bae96 100644
--- a/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
+++ b/erpnext/accounts/doctype/pricing_rule/pricing_rule.py
@@ -129,9 +129,9 @@
 		# if ignore pricing rule then set the rate or amount field to zero
 		if item_details.doctype in ["Quotation Item", "Sales Order Item"]:
 			item_details.update({
-				"type":"Percentage",
+				"type":"",
 				"rate_or_amount": 0.00
-				})
+			})
 		return item_details
 
 	if not (args.item_group and args.brand):
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index 57a41d1..57baf15 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -61,7 +61,8 @@
 
 				if item.doctype in ['Quotation Item', 'Sales Order Item']:
 					item.total_margin = self.calculate_margin(item)
-					item.rate = item.total_margin if item.total_margin > 0 else item.rate
+					item.rate = flt(item.total_margin * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))\
+						if item.total_margin > 0 else item.rate
 					
 				item.net_rate = item.rate
 				item.amount = flt(item.rate * item.qty,	item.precision("amount"))
diff --git a/erpnext/selling/doctype/sales_order_item/sales_order_item.json b/erpnext/selling/doctype/sales_order_item/sales_order_item.json
index 3a44bef..84c4652 100644
--- a/erpnext/selling/doctype/sales_order_item/sales_order_item.json
+++ b/erpnext/selling/doctype/sales_order_item/sales_order_item.json
@@ -565,6 +565,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "depends_on": "eval: doc.type != \"\"", 
    "fieldname": "rate_or_amount", 
    "fieldtype": "Float", 
    "hidden": 0, 
@@ -589,6 +590,31 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "depends_on": "eval: doc.type != \"\"", 
+   "fieldname": "total_margin", 
+   "fieldtype": "Float", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Total Margin", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
    "fieldname": "base_rate", 
    "fieldtype": "Currency", 
    "hidden": 0,