Packing Slip Fix
diff --git a/erpnext/stock/DocType Mapper/Delivery Note-Packing Slip/Delivery Note-Packing Slip.txt b/erpnext/stock/DocType Mapper/Delivery Note-Packing Slip/Delivery Note-Packing Slip.txt
index baf2b74..f59ab73 100644
--- a/erpnext/stock/DocType Mapper/Delivery Note-Packing Slip/Delivery Note-Packing Slip.txt
+++ b/erpnext/stock/DocType Mapper/Delivery Note-Packing Slip/Delivery Note-Packing Slip.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-02-13 11:28:48',
+		'creation': '2012-02-02 11:50:33',
 		'docstatus': 0,
-		'modified': '2012-02-13 11:28:48',
+		'modified': '2012-02-21 16:11:29',
 		'modified_by': u'Administrator',
 		'owner': u'Administrator'
 	},
@@ -34,7 +34,7 @@
 		'from_doctype': u'Delivery Note',
 		'module': u'Stock',
 		'name': '__common__',
-		'ref_doc_submitted': 1,
+		'ref_doc_submitted': 0,
 		'to_doctype': u'Packing Slip'
 	},
 
@@ -77,7 +77,7 @@
 		'from_table': u'Delivery Note',
 		'match_id': 0,
 		'to_table': u'Packing Slip',
-		'validation_logic': u'docstatus=1'
+		'validation_logic': u'docstatus=0'
 	},
 
 	# Table Mapper Detail
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js
index d9f4795..10297dd 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.js
@@ -52,7 +52,7 @@
 		unhide_field(['SMS','Send SMS', 'message', 'customer_mobile_no', 'Repair Delivery Note']);
 	}
 
-	if(doc.docstatus==1) {
+	if(doc.docstatus==0 && !doc.__islocal) {
 		cur_frm.add_custom_button('Make Packing Slip', cur_frm.cscript['Make Packing Slip']);
 	}
 	
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 60aee13..40fdc94 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -309,6 +309,7 @@
 # ON SUBMIT
 # =================================================================================================
 	def on_submit(self):
+		self.validate_packed_qty()
 		set(self.doc, 'message', 'Items against your Order #%s have been delivered. Delivery #%s: ' % (self.doc.po_no, self.doc.name))
 		self.check_qty_in_stock()
 		# Check for Approving Authority
@@ -326,6 +327,28 @@
 		set(self.doc, 'status', 'Submitted')
 
 
+	def validate_packed_qty(self):
+		"""
+			Validate that if packed qty exists, it should be equal to qty
+		"""
+		if not any([d.fields.get('packed_qty') for d in self.doclist]):
+			return
+		packing_error_list = []
+		for d in self.doclist:
+			if d.doctype != 'Delivery Note Detail': continue
+			if d.fields.get('qty') != d.fields.get('packed_qty'):
+				packing_error_list.append([
+					d.fields.get('item_code', ''),
+					d.fields.get('qty', ''),
+					d.fields.get('packed_qty', '')
+				])
+		if packing_error_list:
+			from webnotes.utils import cstr
+			err_msg = "\n".join([("Item: " + d[0] + ", Qty: " + cstr(d[1]) \
+				+ ", Packed: " + cstr(d[2])) for d in packing_error_list])
+			webnotes.msgprint("Packing Error:\n" + err_msg, raise_exception=1)
+
+
 	# *********** Checks whether actual quantity is present in warehouse *************
 	def check_qty_in_stock(self):
 		for d in getlist(self.doclist, 'packing_details'):
diff --git a/erpnext/stock/doctype/packing_slip/packing_slip.js b/erpnext/stock/doctype/packing_slip/packing_slip.js
index 2e728f6..7359ffa 100644
--- a/erpnext/stock/doctype/packing_slip/packing_slip.js
+++ b/erpnext/stock/doctype/packing_slip/packing_slip.js
@@ -1,12 +1,12 @@
 cur_frm.fields_dict['delivery_note'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name FROM `tabDelivery Note` WHERE docstatus=1 AND %(key)s LIKE "%s"';
+	return 'SELECT name FROM `tabDelivery Note` WHERE docstatus=0 AND %(key)s LIKE "%s"';
 }
 
 
 cur_frm.fields_dict['item_details'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
 	return 'SELECT name, description FROM `tabItem` WHERE name IN ( \
-		SELECT item_code FROM `tabDelivery Note Detail` \
-		WHERE parent="'	+ doc.delivery_note + '") AND %(key)s LIKE "%s" LIMIT 50';
+		SELECT item_code FROM `tabDelivery Note Detail` dnd \
+		WHERE parent="'	+ doc.delivery_note + '" AND qty > packed_qty) AND %(key)s LIKE "%s" LIMIT 50';
 }
 
 
@@ -39,8 +39,7 @@
 		if(r.exc) {
 			msgprint(r.exc);
 		} else {
-			refresh_field('item_details');
-			refresh_field('naming_series');
+			refresh_many(['item_details', 'naming_series', 'from_case_no', 'to_case_no'])
 		}
 	});
 }
diff --git a/erpnext/stock/doctype/packing_slip/packing_slip.py b/erpnext/stock/doctype/packing_slip/packing_slip.py
index 33ec2b5..23367c7 100644
--- a/erpnext/stock/doctype/packing_slip/packing_slip.py
+++ b/erpnext/stock/doctype/packing_slip/packing_slip.py
@@ -28,9 +28,10 @@
 			WHERE name=%(delivery_note)s
 			""", self.doc.fields)
 
-		if not(res and res[0][0]==1):
+		if not(res and res[0][0]==0):
 			webnotes.msgprint("""Invalid Delivery Note. Delivery Note should exist 
-				and should be submitted. Please rectify and try again.""", raise_exception=1)
+				and should be in draft state. Please rectify and try again.""", 
+				raise_exception=1)
 
 
 	def validate_case_nos(self):
@@ -46,12 +47,8 @@
 			""", self.doc.fields)
 
 		if res:
-			recommended_case_no = webnotes.conn.sql("""\
-				SELECT MAX(to_case_no) FROM `tabPacking Slip`
-				WHERE delivery_note = %(delivery_note)s AND docstatus=1""", self.doc.fields)
-			
 			webnotes.msgprint("""Case No(s). already in use. Please rectify and try again.
-				Recommended <b>From Case No. = %s</b>""" % (cint(recommended_case_no[0][0]) + 1),
+				Recommended <b>From Case No. = %s</b>""" % self.get_recommended_case_no(),
 				raise_exception=1)
 
 
@@ -75,13 +72,18 @@
 			* Item Quantity dict of current packing slip doc
 			* No. of Cases of this packing slip
 		"""
-		item_codes = ", ".join([('"' + d.item_code + '"') for d in self.doclist])
+		item_codes = ", ".join([('"' + d.item_code + '"') for d in
+			self.doclist])
+		
+		if not item_codes: webnotes.msgprint("No Items to Pack",
+				raise_exception=1)
 
 		res = webnotes.conn.sql("""\
 			SELECT item_code, IFNULL(SUM(qty), 0) as qty, IFNULL(packed_qty, 0) as packed_qty, stock_uom
 			FROM `tabDelivery Note Detail`
 			WHERE parent = "%s" AND item_code IN (%s)
-			GROUP BY item_code""" % (self.doc.delivery_note, item_codes), as_dict=1)
+			GROUP BY item_code""" % (self.doc.delivery_note, item_codes),
+			as_dict=1)
 
 		ps_item_qty = dict([[d.item_code, d.qty] for d in self.doclist])
 
@@ -150,7 +152,23 @@
 		"""
 			Fill empty columns in Packing Slip Detail
 		"""
+		self.doc.from_case_no = self.get_recommended_case_no()
+
 		from webnotes.model.code import get_obj
 		for d in self.doclist:
 			psd_obj = get_obj(doc=d)
 			psd_obj.get_item_details(self.doc.delivery_note)
+
+
+	def get_recommended_case_no(self):
+		"""
+			Returns the next case no. for a new packing slip for a delivery
+			note
+		"""
+		recommended_case_no = webnotes.conn.sql("""\
+			SELECT MAX(to_case_no) FROM `tabPacking Slip`
+			WHERE delivery_note = %(delivery_note)s AND docstatus=1""", self.doc.fields)
+
+		return cint(recommended_case_no[0][0]) + 1
+		
+