[fix] total margin calculations
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index 9713d86..1a79a42 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -87,24 +87,24 @@
 	get_gross_profit(out)
 
 	if args.document_type in ["Quotation Item", "Sales Order Item"]:
-		data = set_margin_fields(out.pricing_rule, out.price_list_rate)
-		out.update(data)
-
 		# calculate rate by appling discount to total_margin
 		if(args.total_margin):
 			out["rate"] = discount_on_total_margin(args.total_margin, out.discount_percentage);
-		else:
+		elif not args.type:
+			data = set_margin_fields(out.pricing_rule, out.price_list_rate)
 			out["rate"] = discount_on_total_margin(data.get("total_margin"), out.discount_percentage)
+			out.update(data)
 
 	return out
 
 def set_margin_fields(pricing_rule=None, price_list_rate=None):
-	"""
-		Todo: get the values of Margin (type and rate)
-		calculate the the Margin Amount, discount etc
-	"""
 	# margin_details dict will hold the all data variable regrading margin
-	margin_details = frappe._dict({})
+	margin_details = frappe._dict({
+		"type":"",
+		"rate_or_amount": 0.0,
+		"total_margin": 0.0
+	})
+
 	if pricing_rule and price_list_rate:
 		margin_details.update(get_margin_details(pricing_rule))
 		margin_details.update(calculate_total_margin(margin_details.get("type"),margin_details.get("rate_or_amount"), price_list_rate))
@@ -116,11 +116,11 @@
 		get the margin details from pricing_rule
 	"""
 	margin_details = frappe._dict({
-			"type":"Percentage",
+			"type":"",
 			"rate_or_amount":0.0,
 		})
 
-	records = frappe.db.sql("""SELECT type, rate FROM `tabPricing Rule` WHERE name = '%s'""" % pricing_rule, as_dict = 1)
+	records = frappe.db.get_values("Pricing Rule", pricing_rule, ["type", "rate"], as_dict=True)
 	
 	for record in records:
 		margin_details["type"] = record.get("type")
@@ -133,20 +133,14 @@
 		calculate margin amount as follows
 		if type is percentage then calculate percentage of price_list_rate
 	"""
-
-	data = frappe._dict({
-			"total_margin":0.0,
-			"margin_amt":0.0,
-		})
-
-	if margin_type == "Amount":
-		data["margin_amt"] = rate_or_amount
+	default = frappe._dict({ "total_margin":0.0 })
+	if not margin_type:
+		return default
+	elif margin_type == "Amount":
+		default.total_margin = price_list_rate + rate_or_amount
 	else:
-		data["margin_amt"] = price_list_rate * ( rate_or_amount / 100 )
-
-	data["total_margin"] = price_list_rate + data["margin_amt"] 
-
-	return data
+		default.total_margin = price_list_rate + ( price_list_rate * ( rate_or_amount / 100 ) )
+	return default
 
 def discount_on_total_margin(total_margin, discount):
 	"""
@@ -169,6 +163,8 @@
 		args.item_code = get_item_code(barcode=args.barcode)
 	elif not args.item_code and args.serial_no:
 		args.item_code = get_item_code(serial_no=args.serial_no)
+	
+	set_transaction_type(args)
 
 	set_transaction_type(args)
 
@@ -579,4 +575,4 @@
 		})
 	
 	return out
-	
\ No newline at end of file
+