Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/manufacturing/doctype/bom/bom.py b/manufacturing/doctype/bom/bom.py
index 00523c5..94f3d20 100644
--- a/manufacturing/doctype/bom/bom.py
+++ b/manufacturing/doctype/bom/bom.py
@@ -95,12 +95,12 @@
 		
 	def get_bom_material_detail(self, args=None):
 		""" Get raw material details like uom, desc and rate"""
-
 		if not args:
 			args = webnotes.form_dict.get('args')
-			import json
-			args = json.loads(args)
-
+			
+		import json
+		args = json.loads(args)
+				
 		item = self.get_item_det(args['item_code'])
 		self.validate_rm_item(item)
 		
diff --git a/selling/doctype/customer/customer.txt b/selling/doctype/customer/customer.txt
index 5b8e323..f4e068b 100644
--- a/selling/doctype/customer/customer.txt
+++ b/selling/doctype/customer/customer.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-06-11 14:26:44", 
   "docstatus": 0, 
-  "modified": "2013-07-11 16:53:18", 
+  "modified": "2013-07-22 12:47:19", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -334,6 +334,21 @@
   "permlevel": 0
  }, 
  {
+  "doctype": "DocField", 
+  "fieldname": "customer_discount_section", 
+  "fieldtype": "Section Break", 
+  "label": "Customer Discount", 
+  "permlevel": 0
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "customer_discounts", 
+  "fieldtype": "Table", 
+  "label": "Customer Discounts", 
+  "options": "Customer Discount", 
+  "permlevel": 0
+ }, 
+ {
   "amend": 0, 
   "cancel": 0, 
   "create": 1, 
diff --git a/selling/doctype/customer_discount/__init__.py b/selling/doctype/customer_discount/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/selling/doctype/customer_discount/__init__.py
diff --git a/selling/doctype/customer_discount/customer_discount.py b/selling/doctype/customer_discount/customer_discount.py
new file mode 100644
index 0000000..928aa9f
--- /dev/null
+++ b/selling/doctype/customer_discount/customer_discount.py
@@ -0,0 +1,8 @@
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import webnotes
+
+class DocType:
+	def __init__(self, d, dl):
+		self.doc, self.doclist = d, dl
\ No newline at end of file
diff --git a/selling/doctype/customer_discount/customer_discount.txt b/selling/doctype/customer_discount/customer_discount.txt
new file mode 100644
index 0000000..e7b7822
--- /dev/null
+++ b/selling/doctype/customer_discount/customer_discount.txt
@@ -0,0 +1,42 @@
+[
+ {
+  "creation": "2013-07-22 12:43:40", 
+  "docstatus": 0, 
+  "modified": "2013-07-22 12:49:32", 
+  "modified_by": "Administrator", 
+  "owner": "Administrator"
+ }, 
+ {
+  "doctype": "DocType", 
+  "istable": 1, 
+  "module": "Selling", 
+  "name": "__common__"
+ }, 
+ {
+  "doctype": "DocField", 
+  "in_list_view": 1, 
+  "name": "__common__", 
+  "parent": "Customer Discount", 
+  "parentfield": "fields", 
+  "parenttype": "DocType", 
+  "permlevel": 0, 
+  "reqd": 1
+ }, 
+ {
+  "doctype": "DocType", 
+  "name": "Customer Discount"
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "item_group", 
+  "fieldtype": "Link", 
+  "label": "Item Group", 
+  "options": "Item Group"
+ }, 
+ {
+  "doctype": "DocField", 
+  "fieldname": "discount", 
+  "fieldtype": "Currency", 
+  "label": "Discount (%)"
+ }
+]
\ No newline at end of file
diff --git a/selling/utils.py b/selling/utils.py
index 5f2cfbd..cbfaee7 100644
--- a/selling/utils.py
+++ b/selling/utils.py
@@ -66,6 +66,8 @@
 		
 		if args.price_list_name and args.price_list_currency:
 			out.update(_get_price_list_rate(args, item_bean, meta))
+			
+	out.update(_get_item_discount(out.item_group, args.customer))
 	
 	if out.warehouse or out.reserved_warehouse:
 		out.update(get_available_qty(args.item_code, out.warehouse or out.reserved_warehouse))
@@ -119,7 +121,6 @@
 				or webnotes.conn.get_value("Company", args.company, "default_expense_account"),
 			"cost_center": item.default_sales_cost_center or args.cost_center,
 			"qty": 1.0,
-			"adj_rate": 0.0,
 			"export_amount": 0.0,
 			"amount": 0.0,
 			"batch_no": None,
@@ -147,6 +148,23 @@
 	validate_currency(args, item_bean.doc, meta)
 	
 	return {"ref_rate": flt(base_ref_rate[0].ref_rate * args.plc_conversion_rate / args.conversion_rate)}
+	
+def _get_item_discount(item_group, customer):
+	parent_item_groups = [x[0] for x in webnotes.conn.sql("""SELECT parent.name 
+		FROM `tabItem Group` AS node, `tabItem Group` AS parent 
+		WHERE parent.lft <= node.lft and parent.rgt >= node.rgt and node.name = %s
+		GROUP BY parent.name 
+		ORDER BY parent.lft desc""", item_group)]
+		
+	discount = 0
+	for d in parent_item_groups:
+		res = webnotes.conn.sql("""select discount, name from `tabCustomer Discount` 
+			where parent = %s and item_group = %s""", (customer, d))
+		if res:
+			discount = flt(res[0][0])
+			break
+			
+	return {"adj_rate": discount}
 
 @webnotes.whitelist()
 def get_available_qty(item_code, warehouse):
@@ -162,7 +180,7 @@
 def get_pos_settings(company):
 	pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` where user = %s 
 		and company = %s""", (webnotes.session['user'], company), as_dict=1)
-		
+	
 	if not pos_settings:
 		pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` 
 			where ifnull(user,'') = '' and company = %s""", company, as_dict=1)