packing slip related changes and test case for item
diff --git a/erpnext/accounts/doctype/lease_agreement/lease_agreement.js b/erpnext/accounts/doctype/lease_agreement/lease_agreement.js
index 8ffd522..7b90be9 100644
--- a/erpnext/accounts/doctype/lease_agreement/lease_agreement.js
+++ b/erpnext/accounts/doctype/lease_agreement/lease_agreement.js
@@ -75,4 +75,4 @@
 cur_frm.fields_dict.invoice.get_query=function(doc){
 
 	return "SELECT tv.name FROM `tabReceivable Voucher` tv WHERE debit_to='"+doc.account+"' and  tv.%(key)s like '%s' ORDER BY tv.name LIMIT 50"
-}
\ No newline at end of file
+}
diff --git a/erpnext/production/doctype/bill_of_materials/bill_of_materials.py b/erpnext/production/doctype/bill_of_materials/bill_of_materials.py
index 9727a63..9fcfb81 100644
--- a/erpnext/production/doctype/bill_of_materials/bill_of_materials.py
+++ b/erpnext/production/doctype/bill_of_materials/bill_of_materials.py
@@ -501,7 +501,7 @@
     child_flat_bom_items=[]
 #    if item and (item[0]['is_sub_contracted_item'] == 'Yes' or item[0]['is_pro_applicable'] == 'Yes'):
 
-    child_flat_bom_items = sql("select item_code, description, qty_consumed_per_unit, stock_uom, moving_avg_rate, last_purchase_rate, standard_rate, '%s' as parent_bom, bom_mat_no, 'No' as is_pro_applicable from `tabFlat BOM Detail` where parent = '%s' and is_pro_applicable = 'No' and docstatus = 1" % ( d.bom_no, cstr(d.bom_no)))
+    child_flat_bom_items = sql("select fbom.item_code, fbom.description, fbom.qty_consumed_per_unit, fbom.stock_uom, fbom.moving_avg_rate, fbom.last_purchase_rate, fbom.standard_rate, '%s' as parent_bom, fbom.bom_mat_no, 'No' as is_pro_applicable from `tabFlat BOM Detail` fbom,`tabBill Of Materials` bom where fbom.parent=bom.name and fbom.parent = '%s' and fbom.is_pro_applicable = 'No' and bom.docstatus = 1" % ( d.bom_no, cstr(d.bom_no)))
     self.cur_flat_bom_items.append([d.item_code, d.description, flt(d.qty), d.stock_uom, flt(d.moving_avg_rate), flt(d.amount_as_per_mar), flt(d.last_purchase_rate), flt(d.amount_as_per_lpr), flt(d.standard_rate), flt(d.amount_as_per_sr), flt(d.qty_consumed_per_unit), (item[0]['is_sub_contracted_item'] == 'Yes') and d.parent or d.bom_no, d.name, (item[0]['is_sub_contracted_item'] == 'Yes') and 'No' or 'Yes'])
     return child_flat_bom_items
 
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.txt b/erpnext/stock/doctype/delivery_note/delivery_note.txt
index a3897ec..9fe13e4 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.txt
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.txt
@@ -5,7 +5,7 @@
 	{
 		'creation': '2011-04-18 15:58:20',
 		'docstatus': 0,
-		'modified': '2011-09-07 17:36:34',
+		'modified': '2011-09-20 18:50:41',
 		'modified_by': 'Administrator',
 		'owner': 'Administrator'
 	},
@@ -21,7 +21,7 @@
 
 	# These values are common for all DocType
 	{
-		'_last_update': '1314093417',
+		'_last_update': '1315560377',
 		'colour': 'White:FFF',
 		'default_print_format': 'Standard',
 		'doctype': 'DocType',
@@ -37,7 +37,7 @@
 		'show_in_menu': 0,
 		'subject': 'To %(customer_name)s on %(transaction_date)s | %(per_billed)s% billed',
 		'tag_fields': 'billing_status',
-		'version': 450
+		'version': 453
 	},
 
 	# These values are common for all DocFormat
@@ -133,11 +133,6 @@
 		'doctype': 'DocFormat'
 	},
 
-	# DocFormat
-	{
-		'doctype': 'DocFormat'
-	},
-
 	# DocField
 	{
 		'colour': 'White:FFF',
@@ -1390,6 +1385,42 @@
 		'in_filter': 0,
 		'label': 'Print Packing Slip',
 		'permlevel': 0,
-		'print_hide': 0
+		'print_hide': 1
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'shipping_mark',
+		'fieldtype': 'Text',
+		'label': 'Shipping Mark',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'packed_by',
+		'fieldtype': 'Text',
+		'label': 'Packed By',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'packing_checked_by',
+		'fieldtype': 'Text',
+		'label': 'Packing Checked By',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': 'DocField',
+		'fieldname': 'pack_size',
+		'fieldtype': 'Text',
+		'label': 'Pack Size',
+		'permlevel': 0
 	}
 ]
\ No newline at end of file
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index 28f1d12..4dca704 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -63,6 +63,9 @@
 			child.conversion_factor = 1
 			child.save()
 
+	# On delete 1. Delete BIN (if none of the corrosponding transactions present, it gets deleted. if present, rolled back due to exception)
+	def on_trash(self):
+		sql("delete from tabBin where item_code='%s'"%(self.doc.item_code))
 
 	# Check whether Ref Rate is not entered twice for same Price List and Currency
 	def check_ref_rate_detail(self):
@@ -124,7 +127,7 @@
 			self.doc.min_order_qty = 0
 		self.check_non_asset_warehouse()
 
-		if self.doc.is_pro_applicable == 'Yes' and self.doc.is_manufactured_item != 'Yes':
+		if self.doc.is_pro_applicable and self.doc.is_pro_applicable == 'Yes' and self.doc.is_manufactured_item and self.doc.is_manufactured_item != 'Yes':
 			msgprint("If making Production Order is allowed then, it should also allow to make Bill of Materials. Refer Manufacturing section.")
 			raise Exception
 
diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py
new file mode 100644
index 0000000..529c82a
--- /dev/null
+++ b/erpnext/stock/doctype/item/test_item.py
@@ -0,0 +1,62 @@
+import unittest
+import webnotes
+import copy
+
+from webnotes.model.doclist import DocList
+from webnotes.model.doc import Document
+from webnotes.model.code import get_obj
+from webnotes.utils import flt
+
+sql = webnotes.conn.sql
+
+
+class TestItem(unittest.TestCase):
+	def setUp(self):
+		webnotes.conn.begin()
+
+	def tearDown(self):
+		webnotes.conn.rollback()
+		
+	def testInsert(self):
+		d = DocList()
+
+		count_before =  flt(sql("select count(*) from tab"+_doctype)[0][0])
+		if docok:
+			for i in docok:
+				d.doc = i
+				d.children = None
+				d.doc.fields['__islocal']=1
+				d.save(1)
+		count_after = flt(sql("select count(*) from tab"+_doctype)[0][0])
+		self.assertTrue(count_before+len(docok)==count_after)
+	
+	def testFailAssert(self):
+		if docnotok:
+			with self.assertRaises(Exception) as context:
+				d = DocList()
+				d.doc = docnotok[0]
+				d.children = None
+				d.doc.fields['__islocal']=1
+				d.save(1)
+
+# Test Data
+
+tabOK = [
+		{'is_purchase_item': None, 'is_pro_applicable': 'No', 'is_manufactured_item': None, 'description': 'Gel Ink', 'default_warehouse': None, 'item_name': 'Gel Ink', 'item_group': 'Ink', 'item_code': 'GELINK', 'is_sub_contracted_item': None, 'is_stock_item': 'Yes', 'stock_uom': 'Nos', 'docstatus': '0'}, 
+		{'is_purchase_item': None, 'is_pro_applicable': 'No', 'is_manufactured_item': None, 'description': 'Gel Refill', 'default_warehouse': None, 'item_name': 'Gel Refill', 'item_group': 'Refill', 'item_code': 'GELREF', 'is_sub_contracted_item': None, 'is_stock_item': 'Yes', 'stock_uom': 'Nos', 'docstatus': '0'}, 
+		{'is_purchase_item': None, 'is_pro_applicable': 'No', 'is_manufactured_item': None, 'description': 'Gel Pen', 'default_warehouse': None, 'item_name': 'Gel Pen', 'item_group': 'Pen', 'item_code': 'GELPEN', 'is_sub_contracted_item': None, 'is_stock_item': 'Yes', 'stock_uom': 'Nos', 'docstatus': '0'}
+	]
+
+tabNotOK =	[
+			{'is_purchase_item': None, 'is_pro_applicable': None, 'is_manufactured_item': None, 'description': 'F Ink', 'default_warehouse': None, 'item_name': 'F Ink', 'item_group': 'F Ink', 'item_code': None, 'is_sub_contracted_item': None, 'is_stock_item': 'No', 'stock_uom': 'Nos', 'docstatus': '0'}
+		]
+	      
+_doctype = 'Item'
+
+for i in tabOK: i['doctype']=_doctype
+for i in tabNotOK: i['doctype']=_doctype
+
+docok = [Document(fielddata=r) for r in tabOK]
+docnotok = [Document(fielddata=r) for r in tabNotOK]
+
+
diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py
index 024bd1f..b799eaa 100644
--- a/erpnext/stock/doctype/stock_entry/stock_entry.py
+++ b/erpnext/stock/doctype/stock_entry/stock_entry.py
@@ -122,16 +122,18 @@
 				
 	def get_raw_materials(self,pro_obj):
 		# get all items from flat bom except, child items of sub-contracted and sub assembly items and sub assembly items itself.
-		flat_bom_items = sql("select item_code, ifnull(sum(qty_consumed_per_unit), 0) * '%s', description, stock_uom	from `tabFlat BOM Detail` where parent = '%s' and parent_bom = '%s' and is_pro_applicable = 'No' and docstatus < 2 group by item_code" % ((self.doc.process == 'Backflush') and flt(self.doc.fg_completed_qty) or flt(pro_obj.doc.qty), cstr(pro_obj.doc.bom_no), cstr(pro_obj.doc.bom_no)))
-		self.make_items_dict(flat_bom_items)
+#		flat_bom_items = sql("select item_code, ifnull(sum(qty_consumed_per_unit), 0) * '%s', description, stock_uom from `tabFlat BOM Detail` where parent = '%s' and parent_bom = '%s' and is_pro_applicable = 'No' and docstatus < 2 group by item_code" % ((self.doc.process == 'Backflush') and flt(self.doc.fg_completed_qty) or flt(pro_obj.doc.qty), cstr(pro_obj.doc.bom_no), cstr(pro_obj.doc.bom_no)))
+#		self.make_items_dict(flat_bom_items)
+
 		if pro_obj.doc.consider_sa_items == 'Yes':
 			# get all Sub Assembly items only from flat bom
-			fl_bom_sa_items = sql("select item_code, ifnull(sum(qty_consumed_per_unit), 0) * '%s', description, stock_uom from `tabFlat BOM Detail` where parent = '%s' and parent_bom != '%s' and is_pro_applicable = 'Yes' and docstatus < 2 group by item_code" % ((self.doc.process == 'Backflush') and flt(self.doc.fg_completed_qty) or flt(pro_obj.doc.qty), cstr(pro_obj.doc.bom_no), cstr(pro_obj.doc.bom_no)))
+			fl_bom_sa_items = sql("select item_code, ifnull(sum(qty_consumed_per_unit), 0) * '%s', description, stock_uom from `tabFlat BOM Detail` where parent = '%s' and parent_bom = '%s' and is_pro_applicable = 'Yes' and docstatus < 2 group by item_code" % ((self.doc.process == 'Backflush') and flt(self.doc.fg_completed_qty) or flt(pro_obj.doc.qty), cstr(pro_obj.doc.bom_no), cstr(pro_obj.doc.bom_no)))
 			self.make_items_dict(fl_bom_sa_items)
 		
 		if pro_obj.doc.consider_sa_items == 'No':
 			# get all sub assembly childs only from flat bom
-			fl_bom_sa_child_item = sql("select item_code, ifnull(sum(qty_consumed_per_unit), 0) * '%s', description, stock_uom from `tabFlat BOM Detail` where parent = '%s' and parent_bom in (select distinct parent_bom from `tabFlat BOM Detail` where parent = '%s' and parent_bom != '%s' and is_pro_applicable = 'Yes' and docstatus < 2 ) and is_pro_applicable = 'No' and docstatus < 2 group by item_code" % ((self.doc.process == 'Backflush') and flt(self.doc.fg_completed_qty) or flt(pro_obj.doc.qty), cstr(pro_obj.doc.bom_no), cstr(pro_obj.doc.bom_no), cstr(pro_obj.doc.bom_no)))
+			#select item_code,ifnull(sum(qty_consumed_per_unit),0)*'%s' as qty,description,stock_uom from ( select distinct fb.name,fb.description,fb.item_code,fb.qty_consumed_per_unit,fb.stock_uom from `tabFlat BOM Detail` fb,`tabBOM Material` bm where bm.parent=fb.parent_bom and bm.docstatus<2 and fb.is_pro_applicable='Yes' and fb.docstatus<2 and fb.parent='%s' and bm.bom_no is null)a group by item_code,stock_uom
+			fl_bom_sa_child_item = sql("select item_code,ifnull(sum(qty_consumed_per_unit),0)*'%s' as qty,description,stock_uom from ( select distinct fb.name,fb.description,fb.item_code,fb.qty_consumed_per_unit,fb.stock_uom from `tabFlat BOM Detail` fb,`tabBOM Material` bm where bm.parent=fb.parent_bom and bm.docstatus<2 and fb.is_pro_applicable='Yes' and fb.docstatus<2 and fb.parent='%s' and bm.bom_no is null)a group by item_code,stock_uom" % ((self.doc.process == 'Backflush') and flt(self.doc.fg_completed_qty) or flt(pro_obj.doc.qty), cstr(pro_obj.doc.bom_no)))
 			self.make_items_dict(fl_bom_sa_child_item)
 
 	def add_to_stock_entry_detail(self, pro_obj, item_dict, fg_item = 0):
@@ -165,8 +167,9 @@
 		self.get_raw_materials(pro_obj)
 		
 		self.doc.clear_table(self.doclist, 'mtn_details', 1)
-		
+
 		self.add_to_stock_entry_detail(pro_obj, self.item_dict)
+		
 		if self.doc.process == 'Backflush':
 			item_dict = {cstr(pro_obj.doc.production_item) : [self.doc.fg_completed_qty, pro_obj.doc.description, pro_obj.doc.stock_uom]}
 			self.add_to_stock_entry_detail(pro_obj, item_dict, fg_item = 1)