Merge branch 'master' of github.com:webnotes/erpnext
diff --git a/manufacturing/doctype/production_order/production_order.py b/manufacturing/doctype/production_order/production_order.py
index fa9b325..2f31180 100644
--- a/manufacturing/doctype/production_order/production_order.py
+++ b/manufacturing/doctype/production_order/production_order.py
@@ -10,6 +10,8 @@
 
 sql = webnotes.conn.sql
 
+class OverProductionError(webnotes.ValidationError): pass
+
 class DocType:
 	def __init__(self, doc, doclist=[]):
 		self.doc = doc
@@ -71,8 +73,7 @@
 				cstr(self.doc.production_item) + _(" against sales order") + ": " + 
 				cstr(self.doc.sales_order) + _(" will be ") + cstr(total_qty) + ", " + 
 				_("which is greater than sales order qty ") + "(" + cstr(so_qty) + ")" + 
-				_("Please reduce qty."), raise_exception=1)
-
+				_("Please reduce qty."), raise_exception=OverProductionError)
 
 	def stop_unstop(self, status):
 		""" Called from client side on Stop/Unstop event"""
@@ -95,6 +96,8 @@
 
 
 	def on_submit(self):
+		if not self.doc.wip_warehouse:
+			webnotes.throw(_("WIP Warehouse required before Submit"))
 		webnotes.conn.set(self.doc,'status', 'Submitted')
 		self.update_planned_qty(self.doc.qty)
 		
diff --git a/manufacturing/doctype/production_order/production_order.txt b/manufacturing/doctype/production_order/production_order.txt
index c546692..d7c6558 100644
--- a/manufacturing/doctype/production_order/production_order.txt
+++ b/manufacturing/doctype/production_order/production_order.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:16", 
   "docstatus": 0, 
-  "modified": "2013-07-25 16:38:37", 
+  "modified": "2013-08-08 12:21:25", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -152,7 +152,7 @@
   "label": "For Warehouse", 
   "options": "Warehouse", 
   "read_only": 0, 
-  "reqd": 1
+  "reqd": 0
  }, 
  {
   "doctype": "DocField", 
@@ -165,7 +165,7 @@
   "fieldtype": "Link", 
   "label": "Work-in-Progress Warehouse", 
   "options": "Warehouse", 
-  "reqd": 1
+  "reqd": 0
  }, 
  {
   "doctype": "DocField", 
diff --git a/manufacturing/doctype/production_plan_item/production_plan_item.txt b/manufacturing/doctype/production_plan_item/production_plan_item.txt
index ff14658..7bcaaca 100644
--- a/manufacturing/doctype/production_plan_item/production_plan_item.txt
+++ b/manufacturing/doctype/production_plan_item/production_plan_item.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-02-22 01:27:49", 
   "docstatus": 0, 
-  "modified": "2013-08-07 14:44:20", 
+  "modified": "2013-08-08 12:12:27", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -88,6 +88,15 @@
   "width": "100px"
  }, 
  {
+  "description": "Reserved Warehouse in Sales Order / Finished Goods Warehouse", 
+  "doctype": "DocField", 
+  "fieldname": "warehouse", 
+  "fieldtype": "Link", 
+  "label": "Warehouse", 
+  "options": "Warehouse", 
+  "reqd": 0
+ }, 
+ {
   "doctype": "DocField", 
   "fieldname": "stock_uom", 
   "fieldtype": "Link", 
diff --git a/manufacturing/doctype/production_planning_tool/production_planning_tool.py b/manufacturing/doctype/production_planning_tool/production_planning_tool.py
index 7a92437..ad3eff1 100644
--- a/manufacturing/doctype/production_planning_tool/production_planning_tool.py
+++ b/manufacturing/doctype/production_planning_tool/production_planning_tool.py
@@ -7,7 +7,7 @@
 from webnotes.model.doc import addchild, Document
 from webnotes.model.bean import getlist
 from webnotes.model.code import get_obj
-from webnotes import msgprint
+from webnotes import msgprint, _
 
 sql = webnotes.conn.sql
 
@@ -70,12 +70,12 @@
 				and so.docstatus = 1 and so.status != "Stopped"
 				and so.company = %s
 				and ifnull(so_item.qty, 0) > ifnull(so_item.delivered_qty, 0) %s
-				and (exists (select * from `tabItem` item where item.name=so_item.item_code
+				and (exists (select name from `tabItem` item where item.name=so_item.item_code
 					and (ifnull(item.is_pro_applicable, 'No') = 'Yes' 
 						or ifnull(item.is_sub_contracted_item, 'No') = 'Yes') %s)
-					or exists (select * from `tabDelivery Note Packing Item` dnpi
+					or exists (select name from `tabDelivery Note Packing Item` dnpi
 						where dnpi.parent = so.name and dnpi.parent_item = so_item.item_code
-							and exists (select * from `tabItem` item where item.name=dnpi.item_code
+							and exists (select name from `tabItem` item where item.name=dnpi.item_code
 								and (ifnull(item.is_pro_applicable, 'No') = 'Yes' 
 									or ifnull(item.is_sub_contracted_item, 'No') = 'Yes') %s)))
 			""" % ('%s', so_filter, item_filter, item_filter), self.doc.company, as_dict=1)
@@ -107,7 +107,7 @@
 		if not so_list:
 			msgprint("Please enter sales order in the above table", raise_exception=1)
 			
-		items = sql("""select distinct parent, item_code,
+		items = sql("""select distinct parent, item_code, reserved_warehouse,
 			(qty - ifnull(delivered_qty, 0)) as pending_qty
 			from `tabSales Order Item` so_item
 			where parent in (%s) and docstatus = 1 and ifnull(qty, 0) > ifnull(delivered_qty, 0)
@@ -116,7 +116,7 @@
 					or ifnull(item.is_sub_contracted_item, 'No') = 'Yes'))""" % \
 			(", ".join(["%s"] * len(so_list))), tuple(so_list), as_dict=1)
 		
-		dnpi_items = sql("""select distinct dnpi.parent, dnpi.item_code,
+		dnpi_items = sql("""select distinct dnpi.parent, dnpi.item_code, dnpi.warehouse as reserved_warhouse,
 			(((so_item.qty - ifnull(so_item.delivered_qty, 0)) * dnpi.qty) / so_item.qty) 
 				as pending_qty
 			from `tabSales Order Item` so_item, `tabDelivery Note Packing Item` dnpi
@@ -139,6 +139,7 @@
 				from tabItem where name=%s""", p['item_code'])
 			pi = addchild(self.doc, 'pp_details', 'Production Plan Item', self.doclist)
 			pi.sales_order				= p['parent']
+			pi.warehouse				= p['reserved_warehouse']
 			pi.item_code				= p['item_code']
 			pi.description				= item_details and item_details[0][0] or ''
 			pi.stock_uom				= item_details and item_details[0][1] or ''
@@ -190,34 +191,38 @@
 		item_dict, bom_dict = {}, {}
 		for d in self.doclist.get({"parentfield": "pp_details"}):			
 			bom_dict[d.bom_no] = bom_dict.get(d.bom_no, 0) + flt(d.planned_qty)
-			item_dict[(d.item_code, d.sales_order)] = {
-				"qty" 				: flt(item_dict.get((d.item_code, d.sales_order), \
-					{}).get("qty")) + flt(d.planned_qty),
+			item_dict[(d.item_code, d.sales_order, d.warehouse)] = {
+				"production_item"	: d.item_code,
+				"sales_order"		: d.sales_order,
+				"qty" 				: flt(item_dict.get((d.item_code, d.sales_order, d.warehouse),
+										{}).get("qty")) + flt(d.planned_qty),
 				"bom_no"			: d.bom_no,
 				"description"		: d.description,
 				"stock_uom"			: d.stock_uom,
 				"company"			: self.doc.company,
 				"wip_warehouse"		: "",
-				"fg_warehouse"		: "",
+				"fg_warehouse"		: d.warehouse,
 				"status"			: "Draft",
 			}
 		return bom_dict, item_dict
 		
 	def create_production_order(self, items):
 		"""Create production order. Called from Production Planning Tool"""
+		from manufacturing.doctype.production_order.production_order import OverProductionError
 
 		pro_list = []
-		for item_so in items:
-			pro_doc = Document('Production Order')
-			pro_doc.production_item = item_so[0]
-			pro_doc.sales_order = item_so[1]
-			for key in items[item_so]:
-				pro_doc.fields[key] = items[item_so][key]
+		for key in items:
+			pro = webnotes.new_bean("Production Order")
+			pro.doc.fields.update(items[key])
 			
-			pro_doc.save(new = 1)
-			
-			get_obj("Production Order", pro_doc.name).validate_production_order_against_so()
-			pro_list.append(pro_doc.name)
+			webnotes.mute_messages = True
+			try:
+				pro.insert()
+				pro_list.append(pro.doc.name)
+			except OverProductionError, e:
+				pass
+				
+			webnotes.mute_messages = False
 			
 		return pro_list
 
diff --git a/manufacturing/doctype/production_planning_tool/production_planning_tool.txt b/manufacturing/doctype/production_planning_tool/production_planning_tool.txt
index 482946b..c3a1d8f 100644
--- a/manufacturing/doctype/production_planning_tool/production_planning_tool.txt
+++ b/manufacturing/doctype/production_planning_tool/production_planning_tool.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-21 12:03:47", 
   "docstatus": 0, 
-  "modified": "2013-08-07 17:21:19", 
+  "modified": "2013-08-08 12:01:02", 
   "modified_by": "Administrator", 
   "owner": "jai@webnotestech.com"
  }, 
diff --git a/utilities/make_demo.py b/utilities/make_demo.py
index 025c83a..7ed9531 100644
--- a/utilities/make_demo.py
+++ b/utilities/make_demo.py
@@ -70,10 +70,15 @@
 	ppt.doc.purchase_request_for_warehouse = "Stores - WP"
 	ppt.run_method("get_open_sales_orders")
 	ppt.run_method("get_items_from_so")
-	ppt.run_method("get_items_from_so")
 	ppt.run_method("raise_production_order")
 	ppt.run_method("raise_purchase_request")
-
+	
+	# submit production orders
+	for pro in webnotes.conn.get_values("Production Order", {"docstatus": 0}):
+		b = webnotes.bean("Production Order", pro[0])
+		b.doc.wip_warehouse = "Work in Progress - WP"
+		b.submit()
+	
 def make_quotation(current_date):
 	b = webnotes.bean([{
 		"creation": current_date,