Merge pull request #6705 from KanchanChauhan/get-serial-no

Fetch serial no by default
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 83fc83c..0011dfe 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -591,61 +591,7 @@
 
 		if not frappe.db.exists("POS Profile", "_Test POS Profile"):
 			pos_profile.insert()
-
-	def test_si_gl_entry_with_perpetual_inventory_and_update_stock_with_warehouse_but_no_account(self):
-		set_perpetual_inventory()
-		frappe.delete_doc("Account", "_Test Warehouse No Account - _TC")
-
-		# insert purchase receipt
-		from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import test_records \
-			as pr_test_records
-		pr = frappe.copy_doc(pr_test_records[0])
-		pr.naming_series = "_T-Purchase Receipt-"
-		pr.get("items")[0].warehouse = "_Test Warehouse No Account - _TC"
-		pr.insert()
-		pr.submit()
-
-		si_doc = copy.deepcopy(test_records[1])
-		si_doc["update_stock"] = 1
-		# si_doc["posting_time"] = "12:05"
-		si_doc.get("items")[0]["warehouse"] = "_Test Warehouse No Account - _TC"
-
-		si = frappe.copy_doc(si_doc)
-		si.insert()
-		si.submit()
-
-		# check stock ledger entries
-		sle = frappe.db.sql("""select * from `tabStock Ledger Entry`
-			where voucher_type = 'Sales Invoice' and voucher_no = %s""",
-			si.name, as_dict=1)[0]
-		self.assertTrue(sle)
-		self.assertEquals([sle.item_code, sle.warehouse, sle.actual_qty],
-			["_Test Item", "_Test Warehouse No Account - _TC", -1.0])
-
-		# check gl entries
-		gl_entries = frappe.db.sql("""select account, debit, credit
-			from `tabGL Entry` where voucher_type='Sales Invoice' and voucher_no=%s
-			order by account asc, debit asc""", si.name, as_dict=1)
-		self.assertTrue(gl_entries)
-
-		expected_gl_entries = dict((d[0], d) for d in [
-			[si.debit_to, 630.0, 0.0],
-			[si_doc.get("items")[0]["income_account"], 0.0, 500.0],
-			[si_doc.get("taxes")[0]["account_head"], 0.0, 80.0],
-			[si_doc.get("taxes")[1]["account_head"], 0.0, 50.0],
-		])
-		for i, gle in enumerate(gl_entries):
-			self.assertEquals(expected_gl_entries[gle.account][0], gle.account)
-			self.assertEquals(expected_gl_entries[gle.account][1], gle.debit)
-			self.assertEquals(expected_gl_entries[gle.account][2], gle.credit)
-
-		si.cancel()
-		gle = frappe.db.sql("""select * from `tabGL Entry`
-			where voucher_type='Sales Invoice' and voucher_no=%s""", si.name)
-
-		self.assertFalse(gle)
-		set_perpetual_inventory(0)
-
+			
 	def test_sales_invoice_gl_entry_with_perpetual_inventory_no_item_code(self):
 		set_perpetual_inventory()
 
diff --git a/erpnext/accounts/page/pos/pos.js b/erpnext/accounts/page/pos/pos.js
index dbcfc2a..f43a436 100644
--- a/erpnext/accounts/page/pos/pos.js
+++ b/erpnext/accounts/page/pos/pos.js
@@ -1061,7 +1061,7 @@
 
 	validate_warehouse: function(){
 		if(!this.items[0].default_warehouse){
-			frappe.throw(__("Deafault warehouse is required for selected item"))
+			frappe.throw(__("Default warehouse is required for selected item"))
 		}
 	}
 })
\ No newline at end of file
diff --git a/erpnext/accounts/report/balance_sheet/balance_sheet.py b/erpnext/accounts/report/balance_sheet/balance_sheet.py
index 5547b49..fbfaa41 100644
--- a/erpnext/accounts/report/balance_sheet/balance_sheet.py
+++ b/erpnext/accounts/report/balance_sheet/balance_sheet.py
@@ -38,7 +38,8 @@
 		
 	if provisional_profit_loss:
 		data.append(provisional_profit_loss)
-	data.append(total_credit)		
+	if total_credit:
+		data.append(total_credit)		
 
 	columns = get_columns(filters.periodicity, period_list, company=filters.company)
 	
@@ -86,6 +87,7 @@
 		if has_value:
 			return provisional_profit_loss, total_row
 		return None,total_row
+	return None, None	
 
 def check_opening_balance(asset, liability, equity):
 	# Check if previous year balance sheet closed
diff --git a/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json b/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
index 466300c..6fb6af7 100755
--- a/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
+++ b/erpnext/buying/doctype/purchase_order_item/purchase_order_item.json
@@ -118,7 +118,7 @@
    "unique": 0
   }, 
   {
-   "allow_on_submit": 0, 
+   "allow_on_submit": 1, 
    "bold": 1, 
    "collapsible": 0, 
    "columns": 0, 
@@ -145,6 +145,32 @@
    "unique": 0
   }, 
   {
+   "allow_on_submit": 1, 
+   "bold": 1, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "expected_delivery_date", 
+   "fieldtype": "Date", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Expected Delivery Date", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 1, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 1, 
@@ -1465,7 +1491,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2016-09-22 05:46:00.860706", 
+ "modified": "2016-10-26 16:07:40.592325", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Purchase Order Item", 
@@ -1477,4 +1503,4 @@
  "sort_field": "modified", 
  "sort_order": "DESC", 
  "track_seen": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py
index aa84929..e48ca1a 100644
--- a/erpnext/controllers/stock_controller.py
+++ b/erpnext/controllers/stock_controller.py
@@ -69,6 +69,10 @@
 						warehouse_with_no_account.append(sle.warehouse)
 
 		if warehouse_with_no_account:
+			for wh in warehouse_with_no_account:
+				if frappe.db.get_value("Warehouse", wh, "company"):
+					frappe.throw(_("Warehouse {0} is not linked to any account, please create/link the corresponding (Asset) account for the warehouse.").format(wh))
+					
 			msgprint(_("No accounting entries for the following warehouses") + ": \n" +
 				"\n".join(warehouse_with_no_account))
 
diff --git a/erpnext/crm/doctype/lead/lead.py b/erpnext/crm/doctype/lead/lead.py
index 605b9e9..18c0db4 100644
--- a/erpnext/crm/doctype/lead/lead.py
+++ b/erpnext/crm/doctype/lead/lead.py
@@ -4,7 +4,7 @@
 from __future__ import unicode_literals
 import frappe
 from frappe import _
-from frappe.utils import cstr, validate_email_add, cint, comma_and, has_gravatar
+from frappe.utils import cstr, validate_email_add, cint, comma_and, has_gravatar, nowdate
 from frappe.model.mapper import get_mapped_doc
 
 from erpnext.controllers.selling_controller import SellingController
@@ -38,11 +38,16 @@
 				validate_email_add(self.email_id, True)
 
 			if self.email_id == self.lead_owner:
-				# Lead Owner cannot be same as the Lead
-				self.lead_owner = None
+				frappe.throw(_("Lead Owner cannot be same as the Lead"))
+
+			if self.email_id == self.contact_by:
+				frappe.throw(_("Next Contact By cannot be same as the Lead Email id"))
 
 			self.image = has_gravatar(self.email_id)
 
+		if self.contact_date and self.contact_date < nowdate():
+			frappe.throw(_("Next Contact Date cannot be in the past"))
+
 	def on_update(self):
 		self.add_calendar_event()
 
diff --git a/erpnext/docs/assets/img/project/percent-complete-calc.png b/erpnext/docs/assets/img/project/percent-complete-calc.png
new file mode 100644
index 0000000..25e170d
--- /dev/null
+++ b/erpnext/docs/assets/img/project/percent-complete-calc.png
Binary files differ
diff --git a/erpnext/docs/assets/img/project/percent-complete-formula.png b/erpnext/docs/assets/img/project/percent-complete-formula.png
new file mode 100644
index 0000000..42ff53a
--- /dev/null
+++ b/erpnext/docs/assets/img/project/percent-complete-formula.png
Binary files differ
diff --git a/erpnext/docs/assets/img/project/project-percent-complete.png b/erpnext/docs/assets/img/project/project-percent-complete.png
new file mode 100644
index 0000000..cebfde3
--- /dev/null
+++ b/erpnext/docs/assets/img/project/project-percent-complete.png
Binary files differ
diff --git a/erpnext/docs/assets/img/project/task-weights.png b/erpnext/docs/assets/img/project/task-weights.png
new file mode 100644
index 0000000..c3cb256
--- /dev/null
+++ b/erpnext/docs/assets/img/project/task-weights.png
Binary files differ
diff --git a/erpnext/docs/assets/img/project/tasks.png b/erpnext/docs/assets/img/project/tasks.png
new file mode 100644
index 0000000..7ee008b
--- /dev/null
+++ b/erpnext/docs/assets/img/project/tasks.png
Binary files differ
diff --git a/erpnext/docs/user/manual/en/customize-erpnext/custom-field.md b/erpnext/docs/user/manual/en/customize-erpnext/custom-field.md
index 6410c77..92c5224 100644
--- a/erpnext/docs/user/manual/en/customize-erpnext/custom-field.md
+++ b/erpnext/docs/user/manual/en/customize-erpnext/custom-field.md
@@ -44,7 +44,9 @@
 
 <img alt="Select Document Type" class="screenshot" src="{{docs_base_url}}/assets/img/customize/custom-field-5.png">
 
-For other field types like Data, Date, Currency, Option field will be left blank.
+For Data field, Option can be set to "Email" or "Phone" and the field will be validated accordingly.
+
+For other field types like Date, Currency, Option field will be left blank.
 
 ####Set More Properties
 
diff --git a/erpnext/docs/user/manual/en/projects/project.md b/erpnext/docs/user/manual/en/projects/project.md
index e8a0e0e..7522396 100644
--- a/erpnext/docs/user/manual/en/projects/project.md
+++ b/erpnext/docs/user/manual/en/projects/project.md
@@ -2,6 +2,20 @@
 
 <img class="screenshot" alt="Project" src="{{docs_base_url}}/assets/img/project/project.png">
 
+You can also track % Completion of a Project using different methods.
+
+  1. Task Completion
+  2. Task Progress
+  3. Task Weight
+
+<img class="screenshot" alt="Project" src="{{docs_base_url}}/assets/img/project/project-percent-complete.png">
+
+Some examples of how the % Completion is calculated based on Tasks.
+
+<img class="screenshot" alt="Project" src="{{docs_base_url}}/assets/img/project/percent-complete-calc.png">
+
+<img class="screenshot" alt="Project" src="{{docs_base_url}}/assets/img/project/percent-complete-formula.png">
+
 ### Managing tasks
 Project can be divided into multiple Tasks.
 Task can be created via Project document itself or can be created via  [Task]({{docs_base_url}}/user/manual/en/projects/tasks.html)
@@ -18,6 +32,12 @@
 
 <img class="screenshot" alt="Project - Task Grid" src="{{docs_base_url}}/assets/img/project/project_task_grid.png">
 
+* To add Weights to Tasks you can follow the below steps
+
+<img class="screenshot" alt="Project - Task Grid" src="{{docs_base_url}}/assets/img/project/tasks.png">
+<img class="screenshot" alt="Project - Task Grid" src="{{docs_base_url}}/assets/img/project/task-weights.png">
+
+
 ### Managing time
 
 ERPNext uses [Time Log]({{docs_base_url}}/user/manual/en/projects/time-log.html) to track the progress of a Project.
diff --git a/erpnext/manufacturing/doctype/bom/bom.js b/erpnext/manufacturing/doctype/bom/bom.js
index 94f9d38..ce4c4cb 100644
--- a/erpnext/manufacturing/doctype/bom/bom.js
+++ b/erpnext/manufacturing/doctype/bom/bom.js
@@ -83,8 +83,10 @@
 				d = locals[cdt][cdn];
 				$.extend(d, r.message);
 				refresh_field("items");
+				refresh_field("scrap_items");
 				doc = locals[doc.doctype][doc.name];
 				erpnext.bom.calculate_rm_cost(doc);
+				erpnext.bom.calculate_scrap_materials_cost(doc);
 				erpnext.bom.calculate_total(doc);
 			},
 			freeze: true
@@ -120,6 +122,7 @@
 	refresh_field('operating_cost');
 }
 
+// rm : raw material
 erpnext.bom.calculate_rm_cost = function(doc) {
 	var rm = doc.items || [];
 	total_rm_cost = 0;
@@ -133,6 +136,15 @@
 	cur_frm.set_value("raw_material_cost", total_rm_cost);
 }
 
+//sm : scrap material
+erpnext.bom.calculate_scrap_materials_cost = function(doc) {
+	var sm = doc.scrap_items || [];
+	total_sm_cost = 0;
+	for(var i=0;i<sm.length;i++) {
+		amt =	flt(sm[i].rate) * flt(sm[i].qty);
+		set_multiple('BOM Scrap Item',sm[i].name, {'amount': amt}, 'scrap_items');
+	}
+}
 
 // Calculate Total Cost
 erpnext.bom.calculate_total = function(doc) {
@@ -176,6 +188,7 @@
 cur_frm.cscript.validate = function(doc, dt, dn) {
 	erpnext.bom.calculate_op_cost(doc);
 	erpnext.bom.calculate_rm_cost(doc);
+	erpnext.bom.calculate_scrap_materials_cost(doc);
 	erpnext.bom.calculate_total(doc);
 }
 
diff --git a/erpnext/manufacturing/doctype/bom/bom.json b/erpnext/manufacturing/doctype/bom/bom.json
index ae23d5e..f1b5392 100644
--- a/erpnext/manufacturing/doctype/bom/bom.json
+++ b/erpnext/manufacturing/doctype/bom/bom.json
@@ -8,11 +8,13 @@
  "docstatus": 0, 
  "doctype": "DocType", 
  "document_type": "Setup", 
+ "editable_grid": 0, 
  "fields": [
   {
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "Item to be manufactured or repacked", 
    "fieldname": "item", 
    "fieldtype": "Link", 
@@ -41,6 +43,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "item_name", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -66,6 +69,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "default": "1", 
    "description": "Quantity of item obtained after manufacturing / repacking from given quantities of raw materials", 
    "fieldname": "quantity", 
@@ -94,6 +98,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "cb0", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -117,6 +122,7 @@
    "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "default": "1", 
    "fieldname": "is_active", 
    "fieldtype": "Check", 
@@ -144,6 +150,7 @@
    "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "default": "1", 
    "fieldname": "is_default", 
    "fieldtype": "Check", 
@@ -171,6 +178,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "Manage cost of operations", 
    "fieldname": "with_operations", 
    "fieldtype": "Check", 
@@ -196,6 +204,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "rm_cost_as_per", 
    "fieldtype": "Select", 
    "hidden": 0, 
@@ -221,6 +230,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "depends_on": "eval:doc.rm_cost_as_per===\"Price List\"", 
    "fieldname": "buying_price_list", 
    "fieldtype": "Link", 
@@ -247,6 +257,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "depends_on": "", 
    "description": "Specify the operations, operating cost and give a unique Operation no to your operations.", 
    "fieldname": "operations_section", 
@@ -274,6 +285,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "operations", 
    "fieldtype": "Table", 
    "hidden": 0, 
@@ -301,6 +313,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "materials_section", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -326,6 +339,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "items", 
    "fieldtype": "Table", 
    "hidden": 0, 
@@ -352,7 +366,61 @@
   {
    "allow_on_submit": 0, 
    "bold": 0, 
+   "collapsible": 1, 
+   "columns": 0, 
+   "fieldname": "scrap_section", 
+   "fieldtype": "Section Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Scrap", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "scrap_items", 
+   "fieldtype": "Table", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Scrap Items", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "BOM Scrap Item", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "costing", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -378,6 +446,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "operating_cost", 
    "fieldtype": "Currency", 
    "hidden": 0, 
@@ -403,6 +472,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "raw_material_cost", 
    "fieldtype": "Currency", 
    "hidden": 0, 
@@ -428,6 +498,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "cb1", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -451,6 +522,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "total_cost", 
    "fieldtype": "Currency", 
    "hidden": 0, 
@@ -476,6 +548,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "more_info_section", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -500,6 +573,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "project", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -527,6 +601,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "company", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -553,6 +628,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "amended_from", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -578,6 +654,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "col_break23", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -601,6 +678,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "uom", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -626,6 +704,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "section_break_25", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -650,6 +729,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "description", 
    "fieldtype": "Small Text", 
    "hidden": 0, 
@@ -657,7 +737,7 @@
    "ignore_xss_filter": 0, 
    "in_filter": 0, 
    "in_list_view": 0, 
-   "label": "Item Description", 
+   "label": "Item Desription", 
    "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
@@ -674,6 +754,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "column_break_27", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -698,6 +779,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "image", 
    "fieldtype": "Attach", 
    "hidden": 0, 
@@ -723,6 +805,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "image_view", 
    "fieldtype": "Image", 
    "hidden": 0, 
@@ -749,6 +832,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "depends_on": "eval:!doc.__islocal", 
    "fieldname": "section_break0", 
    "fieldtype": "Section Break", 
@@ -774,6 +858,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "exploded_items", 
    "fieldtype": "Table", 
    "hidden": 0, 
@@ -802,13 +887,14 @@
  "hide_toolbar": 0, 
  "icon": "icon-sitemap", 
  "idx": 1, 
+ "image_view": 0, 
  "in_create": 0, 
  "in_dialog": 0, 
  "is_submittable": 1, 
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-05-13 18:28:53.557967", 
+ "modified": "2016-10-24 11:26:08.751123", 
  "modified_by": "Administrator", 
  "module": "Manufacturing", 
  "name": "BOM", 
@@ -824,6 +910,7 @@
    "export": 0, 
    "if_owner": 0, 
    "import": 0, 
+   "is_custom": 0, 
    "permlevel": 0, 
    "print": 1, 
    "read": 1, 
@@ -844,6 +931,7 @@
    "export": 0, 
    "if_owner": 0, 
    "import": 0, 
+   "is_custom": 0, 
    "permlevel": 0, 
    "print": 1, 
    "read": 1, 
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index 580d1ba..c6a3f1f 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -74,14 +74,13 @@
 		return item
 
 	def validate_rm_item(self, item):
-		if item[0]['name'] == self.item:
+		if (item[0]['name'] in [it.item_code for it in self.items]) and item[0]['name'] == self.item:
 			frappe.throw(_("Raw material cannot be same as main Item"))
 
 	def set_bom_material_details(self):
 		for item in self.get("items"):
 			ret = self.get_bom_material_detail({"item_code": item.item_code, "item_name": item.item_name, "bom_no": item.bom_no,
 				"qty": item.qty})
-
 			for r in ret:
 				if not item.get(r):
 					item.set(r, ret[r])
@@ -103,12 +102,12 @@
 
 		rate = self.get_rm_rate(args)
 		ret_item = {
-			 'item_name'	: item and args['item_name'] or '',
-			 'description'  : item and args['description'] or '',
-			 'image'		: item and args['image'] or '',
-			 'stock_uom'	: item and args['stock_uom'] or '',
-			 'bom_no'		: args['bom_no'],
-			 'rate'			: rate
+			'item_name'	: item and args['item_name'] or '',
+			'description'  : item and args['description'] or '',
+			'image'		: item and args['image'] or '',
+			'stock_uom'	: item and args['stock_uom'] or '',
+			'bom_no'	: args['bom_no'],
+			'rate'		: rate
 		}
 		return ret_item
 
@@ -377,7 +376,7 @@
 				if not d.description:
 					d.description = frappe.db.get_value('Operation', d.operation, 'description')
 
-def get_bom_items_as_dict(bom, company, qty=1, fetch_exploded=1):
+def get_bom_items_as_dict(bom, company, qty=1, fetch_exploded=1, fetch_scrap_items=0):
 	item_dict = {}
 
 	# Did not use qty_consumed_per_unit in the query, as it leads to rounding loss
@@ -406,11 +405,13 @@
 		query = query.format(table="BOM Explosion Item",
 			conditions="""and item.is_sub_contracted_item = 0""")
 		items = frappe.db.sql(query, { "qty": qty,	"bom": bom }, as_dict=True)
+	elif fetch_scrap_items:
+		query = query.format(table="BOM Scrap Item", conditions="")
+		items = frappe.db.sql(query, { "qty": qty, "bom": bom }, as_dict=True)
 	else:
 		query = query.format(table="BOM Item", conditions="")
 		items = frappe.db.sql(query, { "qty": qty, "bom": bom }, as_dict=True)
 
-	# make unique
 	for item in items:
 		if item_dict.has_key(item.item_code):
 			item_dict[item.item_code]["qty"] += flt(item.qty)
diff --git a/erpnext/manufacturing/doctype/bom/test_records.json b/erpnext/manufacturing/doctype/bom/test_records.json
index 30be2a7..da8d67b 100644
--- a/erpnext/manufacturing/doctype/bom/test_records.json
+++ b/erpnext/manufacturing/doctype/bom/test_records.json
@@ -28,9 +28,20 @@
   "quantity": 1.0
  },
  {
+  "scrap_items":[
+  {
+   "amount": 2000.0,
+   "doctype": "BOM Item",
+   "item_code": "_Test Item Home Desktop 100",
+   "parentfield": "items",
+   "qty": 1.0,
+   "rate": 2000.0,
+   "stock_uom": "_Test UOM"
+  }
+  ],
   "items": [
    {
-    "amount": 5000.0,
+    "amount": 10000.0,
     "doctype": "BOM Item",
     "item_code": "_Test Item",
     "parentfield": "items",
@@ -81,7 +92,7 @@
     "doctype": "BOM Item",
     "item_code": "_Test Item Home Desktop Manufactured",
     "parentfield": "items",
-    "qty": 2.0,
+    "qty": 3.0,
     "rate": 1000.0,
     "stock_uom": "_Test UOM"
    }
diff --git a/erpnext/manufacturing/doctype/bom_scrap_item/__init__.py b/erpnext/manufacturing/doctype/bom_scrap_item/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_scrap_item/__init__.py
diff --git a/erpnext/manufacturing/doctype/bom_scrap_item/bom_scrap_item.json b/erpnext/manufacturing/doctype/bom_scrap_item/bom_scrap_item.json
new file mode 100644
index 0000000..ea5e6c1
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_scrap_item/bom_scrap_item.json
@@ -0,0 +1,248 @@
+{
+ "allow_copy": 0, 
+ "allow_import": 0, 
+ "allow_rename": 0, 
+ "beta": 0, 
+ "creation": "2016-09-26 02:19:21.642081", 
+ "custom": 0, 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "", 
+ "editable_grid": 1, 
+ "fields": [
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "item_code", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Item Code", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Item", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "item_name", 
+   "fieldtype": "Data", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Item Name", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "quantity_and_rate", 
+   "fieldtype": "Section Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Quantity and Rate", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "qty", 
+   "fieldtype": "Float", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Qty", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 1, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "rate", 
+   "fieldtype": "Currency", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Rate", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Company:company:default_currency", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "column_break_6", 
+   "fieldtype": "Column Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "stock_uom", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Stock UOM", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "UOM", 
+   "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, 
+   "columns": 0, 
+   "fieldname": "amount", 
+   "fieldtype": "Currency", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Amount", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Company:company:default_currency", 
+   "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
+  }
+ ], 
+ "hide_heading": 0, 
+ "hide_toolbar": 0, 
+ "idx": 0, 
+ "image_view": 0, 
+ "in_create": 0, 
+ "in_dialog": 0, 
+ "is_submittable": 0, 
+ "issingle": 0, 
+ "istable": 1, 
+ "max_attachments": 0, 
+ "modified": "2016-09-26 02:58:58.433348", 
+ "modified_by": "Administrator", 
+ "module": "Manufacturing", 
+ "name": "BOM Scrap Item", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [], 
+ "quick_entry": 1, 
+ "read_only": 0, 
+ "read_only_onload": 0, 
+ "sort_field": "modified", 
+ "sort_order": "DESC", 
+ "track_seen": 0
+}
\ No newline at end of file
diff --git a/erpnext/manufacturing/doctype/bom_scrap_item/bom_scrap_item.py b/erpnext/manufacturing/doctype/bom_scrap_item/bom_scrap_item.py
new file mode 100644
index 0000000..b6d423f
--- /dev/null
+++ b/erpnext/manufacturing/doctype/bom_scrap_item/bom_scrap_item.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe.model.document import Document
+
+class BOMScrapItem(Document):
+	pass
diff --git a/erpnext/manufacturing/doctype/production_order/production_order.js b/erpnext/manufacturing/doctype/production_order/production_order.js
index d0e0d86..d8eb25b 100644
--- a/erpnext/manufacturing/doctype/production_order/production_order.js
+++ b/erpnext/manufacturing/doctype/production_order/production_order.js
@@ -225,6 +225,10 @@
 				$.each(["description", "stock_uom", "bom_no"], function(i, field) {
 					cur_frm.set_value(field, r.message[field]);
 				});
+
+				if(r.message["set_scrap_wh_mandatory"]){
+					cur_frm.toggle_reqd("scrap_warehouse", true);
+				}
 			}
 		});
 	},
@@ -260,7 +264,12 @@
 	bom_no: function() {
 		return this.frm.call({
 			doc: this.frm.doc,
-			method: "set_production_order_operations"
+			method: "set_production_order_operations",
+			callback: function(r) {
+				if(r.message["set_scrap_wh_mandatory"]){
+					cur_frm.toggle_reqd("scrap_warehouse", true);
+				}
+			}
 		});
 	},
 
diff --git a/erpnext/manufacturing/doctype/production_order/production_order.json b/erpnext/manufacturing/doctype/production_order/production_order.json
index 28c1176..304c1c4 100644
--- a/erpnext/manufacturing/doctype/production_order/production_order.json
+++ b/erpnext/manufacturing/doctype/production_order/production_order.json
@@ -456,6 +456,33 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "scrap_warehouse", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Scrap Warehouse", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Warehouse", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "time", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -1133,7 +1160,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-09-19 02:48:09.412858", 
+ "modified": "2016-09-26 07:01:12.863755", 
  "modified_by": "Administrator", 
  "module": "Manufacturing", 
  "name": "Production Order", 
diff --git a/erpnext/manufacturing/doctype/production_order/production_order.py b/erpnext/manufacturing/doctype/production_order/production_order.py
index a0dc554..3934935 100644
--- a/erpnext/manufacturing/doctype/production_order/production_order.py
+++ b/erpnext/manufacturing/doctype/production_order/production_order.py
@@ -222,6 +222,8 @@
 		self.set('operations', operations)
 		self.calculate_time()
 
+		return check_if_scrap_warehouse_mandatory(self.bom_no)
+
 	def calculate_time(self):
 		bom_qty = frappe.db.get_value("BOM", self.bom_no, "quantity")
 
@@ -457,11 +459,25 @@
 		return {}
 
 	res = res[0]
+
 	res["bom_no"] = frappe.db.get_value("BOM", filters={"item": item, "is_default": 1})
 	if not res["bom_no"]:
 		variant_of= frappe.db.get_value("Item", item, "variant_of")
 		if variant_of:
 			res["bom_no"] = frappe.db.get_value("BOM", filters={"item": variant_of, "is_default": 1})
+
+	res.update(check_if_scrap_warehouse_mandatory(res["bom_no"]))
+
+	return res
+
+@frappe.whitelist()
+def check_if_scrap_warehouse_mandatory(bom_no):
+	res = {"set_scrap_wh_mandatory": False }
+	bom = frappe.get_doc("BOM", bom_no)
+
+	if len(bom.scrap_items) > 0:
+		res["set_scrap_wh_mandatory"] = True
+
 	return res
 
 @frappe.whitelist()
diff --git a/erpnext/manufacturing/doctype/production_order/test_production_order.py b/erpnext/manufacturing/doctype/production_order/test_production_order.py
index 49db299..1454c10 100644
--- a/erpnext/manufacturing/doctype/production_order/test_production_order.py
+++ b/erpnext/manufacturing/doctype/production_order/test_production_order.py
@@ -233,6 +233,45 @@
 		self.pro_order.reload()
 		self.assertEqual(len(self.pro_order.required_items), 0)
 
+	def test_scrap_material_qty(self):
+		prod_order = make_prod_order_test_record(planned_start_date=now(), qty=2)
+
+		# add raw materials to stores
+		test_stock_entry.make_stock_entry(item_code="_Test Item",
+			target="Stores - _TC", qty=10, basic_rate=5000.0)
+		test_stock_entry.make_stock_entry(item_code="_Test Item Home Desktop 100",
+			target="Stores - _TC", qty=10, basic_rate=1000.0)
+
+		s = frappe.get_doc(make_stock_entry(prod_order.name, "Material Transfer for Manufacture", 2))
+		for d in s.get("items"):
+			d.s_warehouse = "Stores - _TC"
+		s.insert()
+		s.submit()
+
+		s = frappe.get_doc(make_stock_entry(prod_order.name, "Manufacture", 2))
+		s.insert()
+		s.submit()
+
+		prod_order_details = frappe.db.get_value("Production Order", prod_order.name,
+			["scrap_warehouse", "qty", "produced_qty", "bom_no"], as_dict=1)
+
+		scrap_item_details = get_scrap_item_details(prod_order_details.bom_no)
+
+		self.assertEqual(prod_order_details.produced_qty, 2)
+
+		for item in s.items:
+			if item.bom_no and item.item_code in scrap_item_details:
+				self.assertEqual(prod_order_details.scrap_warehouse, item.t_warehouse)
+				self.assertEqual(flt(prod_order_details.qty)*flt(scrap_item_details[item.item_code]), item.qty)
+
+def get_scrap_item_details(bom_no):
+	scrap_items = {}
+	for item in frappe.db.sql("""select item_code, qty from `tabBOM Scrap Item`
+		where parent = %s""", bom_no, as_dict=1):
+		scrap_items[item.item_code] = item.qty
+
+	return scrap_items
+
 def make_prod_order_test_record(**args):
 	args = frappe._dict(args)
 
@@ -243,6 +282,7 @@
 	pro_order.qty = args.qty or 10
 	pro_order.wip_warehouse = args.wip_warehouse or "_Test Warehouse - _TC"
 	pro_order.fg_warehouse = args.fg_warehouse or "_Test Warehouse 1 - _TC"
+	pro_order.scrap_warehouse = args.fg_warehouse or "_Test Scrap Warehouse - _TC"
 	pro_order.company = args.company or "_Test Company"
 	pro_order.stock_uom = args.stock_uom or "_Test UOM"
 	pro_order.set_production_order_operations()
diff --git a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
index 8521b5e..aa9e78c 100644
--- a/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
+++ b/erpnext/manufacturing/doctype/production_planning_tool/production_planning_tool.py
@@ -344,10 +344,11 @@
 		self.make_items_dict(item_list)
 
 	def get_subitems(self,bom_wise_item_details, bom, parent_qty, include_sublevel, only_raw, supply_subs,non_stock_item=0):
-		for d in frappe.db.sql("""select bom_item.item_code, default_material_request_type,
+		for d in frappe.db.sql("""SELECT bom_item.item_code, default_material_request_type,
 			ifnull(%(parent_qty)s * sum(bom_item.qty/ifnull(bom.quantity, 1)), 0) as qty, 
-			item.is_sub_contracted_item as is_sub_contracted, item.default_bom as default_bom
-			from `tabBOM Item` bom_item, `tabBOM` bom, tabItem item
+			item.is_sub_contracted_item as is_sub_contracted, item.default_bom as default_bom,
+			bom_item.description as description,  bom_item.stock_uom as stock_uom,  item.min_order_qty 
+			as min_order_qty FROM `tabBOM Item` bom_item, `tabBOM` bom, tabItem item
 			where bom.name = bom_item.parent and bom.name = %(bom)s and bom_item.docstatus < 2
 			and bom_item.item_code = item.name
 			""" + ("and item.is_stock_item = 1","")[non_stock_item] + """
diff --git a/erpnext/projects/doctype/project/project.json b/erpnext/projects/doctype/project/project.json
index f094428..9855bf0 100644
--- a/erpnext/projects/doctype/project/project.json
+++ b/erpnext/projects/doctype/project/project.json
@@ -3,16 +3,19 @@
  "allow_import": 1, 
  "allow_rename": 1, 
  "autoname": "field:project_name", 
+ "beta": 0, 
  "creation": "2013-03-07 11:55:07", 
  "custom": 0, 
  "docstatus": 0, 
  "doctype": "DocType", 
  "document_type": "Setup", 
+ "editable_grid": 0, 
  "fields": [
   {
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "", 
    "fieldname": "project_name", 
    "fieldtype": "Data", 
@@ -39,6 +42,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "default": "Open", 
    "fieldname": "status", 
    "fieldtype": "Select", 
@@ -67,6 +71,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "project_type", 
    "fieldtype": "Select", 
    "hidden": 0, 
@@ -94,6 +99,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "is_active", 
    "fieldtype": "Select", 
    "hidden": 0, 
@@ -121,6 +127,35 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
+   "default": "Task Completion", 
+   "fieldname": "percent_complete_method", 
+   "fieldtype": "Select", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "% Complete Method", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Task Completion\nTask Progress\nTask Weight", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "column_break_5", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -145,6 +180,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "priority", 
    "fieldtype": "Select", 
    "hidden": 0, 
@@ -172,6 +208,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "expected_start_date", 
    "fieldtype": "Date", 
    "hidden": 0, 
@@ -198,6 +235,7 @@
    "allow_on_submit": 0, 
    "bold": 1, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "expected_end_date", 
    "fieldtype": "Date", 
    "hidden": 0, 
@@ -222,8 +260,34 @@
   }, 
   {
    "allow_on_submit": 0, 
+   "bold": 1, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "percent_complete", 
+   "fieldtype": "Percent", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "% Completed", 
+   "length": 0, 
+   "no_copy": 1, 
+   "permlevel": 0, 
+   "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": 1, 
+   "columns": 0, 
    "fieldname": "customer_details", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -250,6 +314,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "customer", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -277,6 +342,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "column_break_14", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -301,6 +367,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "sales_order", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -327,6 +394,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 1, 
+   "columns": 0, 
    "fieldname": "users_section", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -352,6 +420,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "Project will be accessible on the website to these users", 
    "fieldname": "users", 
    "fieldtype": "Table", 
@@ -379,6 +448,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "sb_milestones", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -405,6 +475,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "tasks", 
    "fieldtype": "Table", 
    "hidden": 0, 
@@ -429,32 +500,9 @@
   }, 
   {
    "allow_on_submit": 0, 
-   "bold": 1, 
-   "collapsible": 0, 
-   "fieldname": "percent_complete", 
-   "fieldtype": "Percent", 
-   "hidden": 0, 
-   "ignore_user_permissions": 0, 
-   "ignore_xss_filter": 0, 
-   "in_filter": 0, 
-   "in_list_view": 0, 
-   "label": "% Tasks Completed", 
-   "length": 0, 
-   "no_copy": 1, 
-   "permlevel": 0, 
-   "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": 1, 
+   "columns": 0, 
    "fieldname": "section_break0", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -481,6 +529,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "notes", 
    "fieldtype": "Text Editor", 
    "hidden": 0, 
@@ -507,6 +556,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 1, 
+   "columns": 0, 
    "fieldname": "section_break_18", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -532,6 +582,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "actual_start_date", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -557,6 +608,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "actual_time", 
    "fieldtype": "Float", 
    "hidden": 0, 
@@ -582,6 +634,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "column_break_20", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -606,6 +659,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "actual_end_date", 
    "fieldtype": "Date", 
    "hidden": 0, 
@@ -632,6 +686,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 1, 
+   "columns": 0, 
    "fieldname": "project_details", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -658,6 +713,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "estimated_costing", 
    "fieldtype": "Currency", 
    "hidden": 0, 
@@ -685,6 +741,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "", 
    "fieldname": "total_costing_amount", 
    "fieldtype": "Currency", 
@@ -711,6 +768,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "", 
    "fieldname": "total_expense_claim", 
    "fieldtype": "Currency", 
@@ -737,6 +795,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "company", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -762,6 +821,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "cost_center", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -787,6 +847,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "column_break_28", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -811,6 +872,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "description": "", 
    "fieldname": "total_billing_amount", 
    "fieldtype": "Currency", 
@@ -837,6 +899,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "total_purchase_cost", 
    "fieldtype": "Currency", 
    "hidden": 0, 
@@ -862,6 +925,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 1, 
+   "columns": 0, 
    "fieldname": "margin", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -888,6 +952,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "gross_margin", 
    "fieldtype": "Currency", 
    "hidden": 0, 
@@ -915,6 +980,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "column_break_37", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -939,6 +1005,7 @@
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "per_gross_margin", 
    "fieldtype": "Percent", 
    "hidden": 0, 
@@ -967,13 +1034,14 @@
  "hide_toolbar": 0, 
  "icon": "icon-puzzle-piece", 
  "idx": 29, 
+ "image_view": 0, 
  "in_create": 0, 
  "in_dialog": 0, 
  "is_submittable": 0, 
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 4, 
- "modified": "2016-04-22 03:15:39.635420", 
+ "modified": "2016-10-23 01:02:40.632849", 
  "modified_by": "Administrator", 
  "module": "Projects", 
  "name": "Project", 
@@ -989,6 +1057,7 @@
    "export": 0, 
    "if_owner": 0, 
    "import": 0, 
+   "is_custom": 0, 
    "permlevel": 0, 
    "print": 1, 
    "read": 1, 
@@ -1009,6 +1078,7 @@
    "export": 0, 
    "if_owner": 0, 
    "import": 0, 
+   "is_custom": 0, 
    "permlevel": 1, 
    "print": 0, 
    "read": 1, 
@@ -1027,4 +1097,4 @@
  "sort_order": "DESC", 
  "timeline_field": "customer", 
  "track_seen": 1
-}
\ No newline at end of file
+}
diff --git a/erpnext/projects/doctype/project/project.py b/erpnext/projects/doctype/project/project.py
index bdf3c3b..388f43e 100644
--- a/erpnext/projects/doctype/project/project.py
+++ b/erpnext/projects/doctype/project/project.py
@@ -36,7 +36,8 @@
 				"start_date": task.exp_start_date,
 				"end_date": task.exp_end_date,
 				"description": task.description,
-				"task_id": task.name
+				"task_id": task.name,
+				"task_weight": task.task_weight
 			})
 
 	def get_tasks(self):
@@ -44,6 +45,7 @@
 
 	def validate(self):
 		self.validate_dates()
+		self.validate_weights()
 		self.sync_tasks()
 		self.tasks = []
 		self.send_welcome_email()
@@ -52,6 +54,14 @@
 		if self.expected_start_date and self.expected_end_date:
 			if getdate(self.expected_end_date) < getdate(self.expected_start_date):
 				frappe.throw(_("Expected End Date can not be less than Expected Start Date"))
+				
+	def validate_weights(self):
+		sum = 0
+		for task in self.tasks:
+			if task.task_weight > 0:
+				sum = sum + task.task_weight
+		if sum > 0 and sum != 1:
+			frappe.throw(_("Total of all task weights should be 1. Please adjust weights of all Project tasks accordingly"))
 
 	def sync_tasks(self):
 		"""sync tasks and remove table"""
@@ -64,13 +74,13 @@
 			else:
 				task = frappe.new_doc("Task")
 				task.project = self.name
-
 			task.update({
 				"subject": t.title,
 				"status": t.status,
 				"exp_start_date": t.start_date,
 				"exp_end_date": t.end_date,
 				"description": t.description,
+				"task_weight": t.task_weight
 			})
 
 			task.flags.ignore_links = True
@@ -93,13 +103,28 @@
 		self.save(ignore_permissions = True)
 
 	def update_percent_complete(self):
-		total = frappe.db.sql("""select count(*) from tabTask where project=%s""", self.name)[0][0]
-		if total:
-			completed = frappe.db.sql("""select count(*) from tabTask where
+		total = frappe.db.sql("""select count(name) from tabTask where project=%s""", self.name)[0][0]
+		if (self.percent_complete_method == "Task Completion" and total > 0) or (not self.percent_complete_method and total > 0):
+			completed = frappe.db.sql("""select count(name) from tabTask where
 				project=%s and status in ('Closed', 'Cancelled')""", self.name)[0][0]
-
 			self.percent_complete = flt(flt(completed) / total * 100, 2)
 
+		if (self.percent_complete_method == "Task Progress" and total > 0):
+			progress = frappe.db.sql("""select sum(progress) from tabTask where
+				project=%s""", self.name)[0][0]
+			self.percent_complete = flt(flt(progress) / total, 2)
+
+		if (self.percent_complete_method == "Task Weight" and total > 0):
+			weight_sum = frappe.db.sql("""select sum(task_weight) from tabTask where
+				project=%s""", self.name)[0][0]
+			if weight_sum == 1:
+				weighted_progress = frappe.db.sql("""select progress,task_weight from tabTask where
+					project=%s""", self.name,as_dict=1)
+				pct_complete=0
+				for row in weighted_progress:
+					pct_complete += row["progress"] * row["task_weight"]
+				self.percent_complete = flt(flt(pct_complete), 2)
+
 	def update_costing(self):
 		from_time_sheet = frappe.db.sql("""select
 			sum(costing_amount) as costing_amount,
diff --git a/erpnext/projects/doctype/project_task/project_task.json b/erpnext/projects/doctype/project_task/project_task.json
index a0ef69d..9c469f1 100644
--- a/erpnext/projects/doctype/project_task/project_task.json
+++ b/erpnext/projects/doctype/project_task/project_task.json
@@ -9,6 +9,7 @@
  "doctype": "DocType", 
  "document_type": "Other", 
  "editable_grid": 1, 
+ "engine": "InnoDB", 
  "fields": [
   {
    "allow_on_submit": 0, 
@@ -174,6 +175,32 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "task_weight", 
+   "fieldtype": "Float", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 1, 
+   "label": "Weight", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "section_break_6", 
    "fieldtype": "Section Break", 
    "hidden": 0, 
@@ -258,7 +285,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2016-08-26 02:44:34.737837", 
+ "modified": "2016-10-19 11:16:16.023965", 
  "modified_by": "Administrator", 
  "module": "Projects", 
  "name": "Project Task", 
diff --git a/erpnext/projects/doctype/task/task.json b/erpnext/projects/doctype/task/task.json
index 46c8746..6905e9d 100644
--- a/erpnext/projects/doctype/task/task.json
+++ b/erpnext/projects/doctype/task/task.json
@@ -235,6 +235,32 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "task_weight", 
+   "fieldtype": "Float", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Weight", 
+   "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, 
+   "columns": 0, 
    "fieldname": "column_break_11", 
    "fieldtype": "Column Break", 
    "hidden": 0, 
@@ -294,7 +320,7 @@
    "ignore_xss_filter": 0, 
    "in_filter": 0, 
    "in_list_view": 0, 
-   "label": "Progress", 
+   "label": "% Progress", 
    "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
@@ -854,7 +880,7 @@
  "istable": 0, 
  "max_attachments": 5, 
  "menu_index": 0, 
- "modified": "2016-10-03 15:12:56.078675", 
+ "modified": "2016-10-20 02:59:02.228659", 
  "modified_by": "Administrator", 
  "module": "Projects", 
  "name": "Task", 
diff --git a/erpnext/projects/doctype/task/task.py b/erpnext/projects/doctype/task/task.py
index 34b9ed5..a9b48fc 100644
--- a/erpnext/projects/doctype/task/task.py
+++ b/erpnext/projects/doctype/task/task.py
@@ -28,6 +28,7 @@
 
 	def validate(self):
 		self.validate_dates()
+		self.validate_progress()
 		self.validate_status()
 		self.update_depends_on()
 
@@ -46,6 +47,10 @@
 
 			from frappe.desk.form.assign_to import clear
 			clear(self.doctype, self.name)
+			
+	def validate_progress(self):
+		if self.progress > 100:
+			frappe.throw(_("Progress % for a task cannot be more than 100."))
 
 	def update_depends_on(self):
 		depends_on_tasks = ""
diff --git a/erpnext/schools/doctype/student/student.json b/erpnext/schools/doctype/student/student.json
index c4b8667..d188db3 100644
--- a/erpnext/schools/doctype/student/student.json
+++ b/erpnext/schools/doctype/student/student.json
@@ -1,7 +1,7 @@
 {
  "allow_copy": 0, 
  "allow_import": 1, 
- "allow_rename": 0, 
+ "allow_rename": 1, 
  "autoname": "naming_series:", 
  "beta": 0, 
  "creation": "2015-09-07 13:00:55.938280", 
@@ -10,6 +10,7 @@
  "doctype": "DocType", 
  "document_type": "Document", 
  "editable_grid": 0, 
+ "engine": "InnoDB", 
  "fields": [
   {
    "allow_on_submit": 0, 
@@ -762,7 +763,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2016-10-07 15:36:39.936505", 
+ "modified": "2016-10-26 12:24:10.533118", 
  "modified_by": "Administrator", 
  "module": "Schools", 
  "name": "Student", 
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index e291696..4ddeae9 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -69,6 +69,7 @@
 		self.flags.old_lead = self.lead_name
 		validate_party_accounts(self)
 		self.status = get_party_status(self)
+		self.validate_credit_limit_on_change()
 
 	def on_update(self):
 		self.validate_name_with_customer_group()
@@ -125,6 +126,15 @@
 		if frappe.db.exists("Customer Group", self.name):
 			frappe.throw(_("A Customer Group exists with same name please change the Customer name or rename the Customer Group"), frappe.NameError)
 
+	def validate_credit_limit_on_change(self):
+		if self.get("__islocal") or self.credit_limit == frappe.db.get_value("Customer", self.name, "credit_limit"):
+			return
+
+		for company in frappe.get_all("Company"):
+			outstanding_amt = get_customer_outstanding(self.name, company.name)
+			if flt(self.credit_limit) < outstanding_amt:
+				frappe.throw(_("""New credit limit is less than current outstanding amount for the customer. Credit limit has to be atleast {0}""").format(outstanding_amt))
+
 	def delete_customer_address(self):
 		addresses = frappe.db.sql("""select name, lead from `tabAddress`
 			where customer=%s""", (self.name,))
diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py
index 9e53845..36e4819 100644
--- a/erpnext/selling/doctype/customer/test_customer.py
+++ b/erpnext/selling/doctype/customer/test_customer.py
@@ -8,6 +8,8 @@
 
 from frappe.test_runner import make_test_records
 from erpnext.exceptions import PartyFrozen, PartyDisabled
+from frappe.utils import flt
+from erpnext.selling.doctype.customer.customer import get_credit_limit, get_customer_outstanding
 
 test_ignore = ["Price List"]
 
@@ -111,6 +113,64 @@
 		self.assertEquals("_Test Customer 1 - 1", duplicate_customer.name)
 		self.assertEquals(test_customer_1.customer_name, duplicate_customer.customer_name)
 
+	def get_customer_outstanding_amount(self):
+		outstanding_amt = get_customer_outstanding('_Test Customer', '_Test Company')
+
+		# If outstanding is negative make a transaction to get positive outstanding amount
+		if outstanding_amt > 0.0:
+			return outstanding_amt
+
+		item_qty = int((abs(outstanding_amt) + 200)/100)
+		make_sales_order({'qty':item_qty})
+		return get_customer_outstanding('_Test Customer', '_Test Company')
+
+	def test_customer_credit_limit(self):
+		from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
+		from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
+		from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
+		from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
+
+		outstanding_amt = self.get_customer_outstanding_amount()
+		credit_limit = get_credit_limit('_Test Customer', '_Test Company')
+
+		if outstanding_amt <= 0.0:
+			item_qty = int((abs(outstanding_amt) + 200)/100)
+			make_sales_order({'qty':item_qty})
+
+		if credit_limit == 0.0:
+			frappe.db.set_value("Customer", '_Test Customer', 'credit_limit', outstanding_amt - 50.0)
+
+		# Sales Order
+		so = make_sales_order(do_not_submit=True)
+		self.assertRaises(frappe.ValidationError, so.submit)
+
+		# Delivery Note
+		dn = create_delivery_note(do_not_submit=True)
+		self.assertRaises(frappe.ValidationError, dn.submit)
+
+		# Sales Invoice
+		si = create_sales_invoice(do_not_submit=True)
+		self.assertRaises(frappe.ValidationError, si.submit)
+
+		if credit_limit > outstanding_amt:
+			frappe.db.set_value("Customer", '_Test Customer', 'credit_limit', credit_limit)
+
+		# Makes Sales invoice from Sales Order
+		so.save(ignore_permissions=True)
+		si = make_sales_invoice(so.name)
+		si.save(ignore_permissions=True)
+		self.assertRaises(frappe.ValidationError, make_sales_order)
+
+	def test_customer_credit_limit_on_change(self):
+		from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
+
+		outstanding_amt = self.get_customer_outstanding_amount()
+		credit_limit = get_credit_limit('_Test Customer', '_Test Company')
+
+		customer = frappe.get_doc("Customer", '_Test Customer')
+		customer.credit_limit = flt(outstanding_amt - 100)
+		self.assertRaises(frappe.ValidationError, customer.save)
+
 def get_customer_dict(customer_name):
 	return {
 		 "customer_group": "_Test Customer Group",
@@ -119,4 +179,3 @@
 		 "doctype": "Customer",
 		 "territory": "_Test Territory"
 	}
-
diff --git a/erpnext/setup/doctype/company/company.json b/erpnext/setup/doctype/company/company.json
index 25e0cfb..a003bde 100644
--- a/erpnext/setup/doctype/company/company.json
+++ b/erpnext/setup/doctype/company/company.json
@@ -714,7 +714,7 @@
    "ignore_xss_filter": 0, 
    "in_filter": 0, 
    "in_list_view": 0, 
-   "label": "Deafult Cost Center", 
+   "label": "Default Cost Center", 
    "length": 0, 
    "no_copy": 1, 
    "options": "Cost Center", 
@@ -1385,7 +1385,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2016-06-26 09:08:50.476200", 
+ "modified": "2016-10-26 09:08:50.476200", 
  "modified_by": "Administrator", 
  "module": "Setup", 
  "name": "Company", 
diff --git a/erpnext/setup/setup_wizard/domainify.py b/erpnext/setup/setup_wizard/domainify.py
index 2b87cc0..4d6ea5c 100644
--- a/erpnext/setup/setup_wizard/domainify.py
+++ b/erpnext/setup/setup_wizard/domainify.py
@@ -39,9 +39,6 @@
 			'desktop_icons': ['Item', 'Customer', 'Supplier', 'Lead', 'Sales Order',
 				 'Sales Invoice', 'CRM', 'Selling', 'Buying', 'Stock', 'Accounts', 'HR', 'ToDo'],
 			'remove_roles': ['Manufacturing User', 'Manufacturing Manager', 'Academics User'],
-			'properties': [
-				{'doctype': 'Item', 'fieldname': 'manufacturing', 'property': 'hidden', 'value': 1},
-			],
 			'set_value': [
 				['Stock Settings', None, 'show_barcode_field', 1]
 			],
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index e8fa237..a60e945 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -148,7 +148,7 @@
 						if not d.t_warehouse:
 							frappe.throw(_("Target warehouse is mandatory for row {0}").format(d.idx))
 
-						elif self.pro_doc and cstr(d.t_warehouse) != self.pro_doc.fg_warehouse:
+						elif self.pro_doc and (cstr(d.t_warehouse) != self.pro_doc.fg_warehouse and cstr(d.t_warehouse) != self.pro_doc.scrap_warehouse):
 							frappe.throw(_("Target warehouse in row {0} must be same as Production Order").format(d.idx))
 
 					else:
@@ -238,6 +238,7 @@
 				d.serial_no = transferred_serial_no
 
 	def get_stock_and_rate(self):
+		self.set_production_order_details()
 		self.set_transfer_qty()
 		self.set_actual_qty()
 		self.calculate_rate_and_amount()
@@ -252,6 +253,7 @@
 	def set_basic_rate(self, force=False, update_finished_item_rate=True):
 		"""get stock and incoming rate on posting date"""
 		raw_material_cost = 0.0
+		scrap_material_cost = 0.0
 		fg_basic_rate = 0.0
 
 		for d in self.get('items'):
@@ -276,16 +278,28 @@
 				if not d.t_warehouse:
 					raw_material_cost += flt(d.basic_amount)
 
+			# get scrap items basic rate
+			if d.bom_no:
+				if not flt(d.basic_rate) and getattr(self, "pro_doc", frappe._dict()).scrap_warehouse == d.t_warehouse:
+					basic_rate = flt(get_incoming_rate(args), self.precision("basic_rate", d))
+					if basic_rate > 0:
+						d.basic_rate = basic_rate
+					d.basic_amount = flt(flt(d.transfer_qty) * flt(d.basic_rate), d.precision("basic_amount"))
+
+				if getattr(self, "pro_doc", frappe._dict()).scrap_warehouse == d.t_warehouse:
+
+					scrap_material_cost += flt(d.basic_amount)
+
 		number_of_fg_items = len([t.t_warehouse for t in self.get("items") if t.t_warehouse])
 		if (fg_basic_rate == 0.0 and number_of_fg_items == 1) or update_finished_item_rate:
-			self.set_basic_rate_for_finished_goods(raw_material_cost)
+			self.set_basic_rate_for_finished_goods(raw_material_cost, scrap_material_cost)
 
-	def set_basic_rate_for_finished_goods(self, raw_material_cost):
+	def set_basic_rate_for_finished_goods(self, raw_material_cost, scrap_material_cost):
 		if self.purpose in ["Manufacture", "Repack"]:
 			for d in self.get("items"):
-				if d.bom_no or d.t_warehouse:
-					d.basic_rate = flt(raw_material_cost / flt(d.transfer_qty), d.precision("basic_rate"))
-					d.basic_amount = flt(raw_material_cost, d.precision("basic_amount"))
+				if (d.bom_no or d.t_warehouse) and (getattr(self, "pro_doc", frappe._dict()).scrap_warehouse != d.t_warehouse):
+					d.basic_rate = flt((raw_material_cost - scrap_material_cost) / flt(d.transfer_qty), d.precision("basic_rate"))
+					d.basic_amount = flt((raw_material_cost - scrap_material_cost), d.precision("basic_amount"))
 
 	def distribute_additional_costs(self):
 		if self.purpose == "Material Issue":
@@ -302,7 +316,7 @@
 
 	def update_valuation_rate(self):
 		for d in self.get("items"):
-			d.amount = flt(d.basic_amount + flt(d.additional_cost), d.precision("amount"))
+			d.amount = flt(flt(d.basic_amount) + flt(d.additional_cost), d.precision("amount"))
 			d.valuation_rate = flt(
 				flt(d.basic_rate)
 				+ (flt(d.additional_cost) / flt(d.transfer_qty)),
@@ -348,14 +362,14 @@
 
 	def validate_bom(self):
 		for d in self.get('items'):
-			if d.bom_no:
+			if d.bom_no and (d.t_warehouse != getattr(self, "pro_doc", frappe._dict()).scrap_warehouse):
 				validate_bom_no(d.item_code, d.bom_no)
 
 	def validate_finished_goods(self):
 		"""validation: finished good quantity should be same as manufacturing quantity"""
 		items_with_target_warehouse = []
 		for d in self.get('items'):
-			if d.bom_no and flt(d.transfer_qty) != flt(self.fg_completed_qty):
+			if d.bom_no and flt(d.transfer_qty) != flt(self.fg_completed_qty) and (d.t_warehouse != getattr(self, "pro_doc", frappe._dict()).scrap_warehouse):
 				frappe.throw(_("Quantity in row {0} ({1}) must be same as manufactured quantity {2}"). \
 					format(d.idx, d.transfer_qty, self.fg_completed_qty))
 
@@ -520,19 +534,7 @@
 		if not self.posting_date or not self.posting_time:
 			frappe.throw(_("Posting date and posting time is mandatory"))
 
-		if not getattr(self, "pro_doc", None):
-			self.pro_doc = None
-
-		if self.production_order:
-			# common validations
-			if not self.pro_doc:
-				self.pro_doc = frappe.get_doc('Production Order', self.production_order)
-
-			if self.pro_doc:
-				self.bom_no = self.pro_doc.bom_no
-			else:
-				# invalid production order
-				self.production_order = None
+		self.set_production_order_details()
 
 		if self.bom_no:
 			if self.purpose in ["Material Issue", "Material Transfer", "Manufacture", "Repack",
@@ -558,7 +560,15 @@
 							item["from_warehouse"] = self.pro_doc.wip_warehouse
 
 						item["to_warehouse"] = self.to_warehouse if self.purpose=="Subcontract" else ""
+					
 					self.add_to_stock_entry_detail(item_dict)
+
+					scrap_item_dict = self.get_bom_scrap_material(self.fg_completed_qty)
+					for item in scrap_item_dict.values():
+						if self.pro_doc and self.pro_doc.scrap_warehouse:
+							item["to_warehouse"] = self.pro_doc.scrap_warehouse
+					self.add_to_stock_entry_detail(scrap_item_dict, bom_no=self.bom_no)
+					
 			# fetch the serial_no of the first stock entry for the second stock entry
 			if self.production_order and self.purpose == "Manufacture":
 				self.set_serial_nos(self.production_order)
@@ -570,6 +580,21 @@
 		self.set_actual_qty()
 		self.calculate_rate_and_amount()
 
+	def set_production_order_details(self):
+		if not getattr(self, "pro_doc", None):
+			self.pro_doc = None
+
+		if self.production_order:
+			# common validations
+			if not self.pro_doc:
+				self.pro_doc = frappe.get_doc('Production Order', self.production_order)
+
+			if self.pro_doc:
+				self.bom_no = self.pro_doc.bom_no
+			else:
+				# invalid production order
+				self.production_order = None
+
 	def load_items_from_bom(self):
 		if self.production_order:
 			item_code = self.pro_doc.production_item
@@ -608,7 +633,18 @@
 		for item in item_dict.values():
 			item.from_warehouse = self.from_warehouse or item.default_warehouse
 		return item_dict
+	
+	def get_bom_scrap_material(self, qty):
+		from erpnext.manufacturing.doctype.bom.bom import get_bom_items_as_dict
+		
+		# item dict = { item_code: {qty, description, stock_uom} }
+		item_dict = get_bom_items_as_dict(self.bom_no, self.company, qty=qty,
+			fetch_exploded = 0, fetch_scrap_items = 1)
 
+		for item in item_dict.values():
+			item.from_warehouse = ""
+		return item_dict
+	
 	def get_transfered_raw_materials(self):
 		transferred_materials = frappe.db.sql("""
 			select
diff --git a/erpnext/stock/doctype/warehouse/test_records.json b/erpnext/stock/doctype/warehouse/test_records.json
index 4dd9f6b..af3bd23 100644
--- a/erpnext/stock/doctype/warehouse/test_records.json
+++ b/erpnext/stock/doctype/warehouse/test_records.json
@@ -10,6 +10,13 @@
   "company": "_Test Company",
   "create_account_under": "Stock Assets - _TC",
   "doctype": "Warehouse",
+  "warehouse_name": "_Test Scrap Warehouse",
+  "is_group": 0
+ },
+ {
+  "company": "_Test Company",
+  "create_account_under": "Stock Assets - _TC",
+  "doctype": "Warehouse",
   "warehouse_name": "_Test Warehouse",
   "is_group": 0
  },