[fix] [patch] [minor] sle for deleted stock entry detail
diff --git a/patches/august_2013/p06_fix_sle_against_stock_entry.py b/patches/august_2013/p06_fix_sle_against_stock_entry.py
index 4413eea..dbc2c94 100644
--- a/patches/august_2013/p06_fix_sle_against_stock_entry.py
+++ b/patches/august_2013/p06_fix_sle_against_stock_entry.py
@@ -1,23 +1,119 @@
 import webnotes
+
+cancelled = []
+uncancelled = []
+
 def execute():
 	from stock.stock_ledger import update_entries_after
-	
-	stock_entries = webnotes.conn.sql("""select name from `tabStock Entry` 
-		where docstatus < 2  and modified >= '2013-08-15' 
-		and ifnull(production_order, '') != '' and ifnull(bom_no, '') != ''""")
-		
+
+	stock_entries = webnotes.conn.sql("""select * from `tabStock Entry` 
+		where docstatus >= 1 and date(modified) >= "2013-08-16" and date(modified) <= "2013-08-21" 
+		and ifnull(production_order, '') != '' and ifnull(bom_no, '') != '' 
+		order by modified desc, name desc""", as_dict=True)
+
 	for entry in stock_entries:
-		webnotes.conn.sql("""update `tabStock Ledger Entry` set is_cancelled = 'Yes' 
-			where voucher_type = 'Stock Entry' and voucher_no = %s""", entry[0])
+		if not webnotes.conn.sql("""select name from `tabStock Entry Detail` 
+			where parent=%s""", entry.name):
+				res = webnotes.conn.sql("""select * from `tabStock Ledger Entry`
+					where voucher_type='Stock Entry' and voucher_no=%s
+					and is_cancelled='No'""", entry.name, as_dict=True)
+				if res:
+					print entry
+					make_stock_entry_detail(entry, res)
+				
+	if cancelled or uncancelled:
+		send_email()
 			
-		item_warehouse = webnotes.conn.sql("""select distinct item_code, warehouse 
-			from `tabStock Ledger Entry` 
-			where voucher_type = 'Stock Entry' and voucher_no = %s""", entry[0], as_dict=1)
-			
-		for d in item_warehouse:
-			update_entries_after({
-				"item_code": d.item_code,
-				"warehouse": d.warehouse,
-				"posting_date": "2013-08-15",
-				"posting_date": "01:00"
-			})
\ No newline at end of file
+def make_stock_entry_detail(entry, res):
+	global cancelled, uncancelled
+	
+	fg_item = webnotes.conn.get_value("Production Order", entry.production_order,
+		"production_item")
+	voucher_detail_entries_map = {}
+	for sle in res:
+		voucher_detail_entries_map.setdefault(sle.voucher_detail_no, []).append(sle)
+	
+	for i, voucher_detail_no in enumerate(sorted(voucher_detail_entries_map.keys())):
+		sl_entries = voucher_detail_entries_map[voucher_detail_no]
+		# create stock entry details back from stock ledger entries
+		stock_entry_detail = webnotes.doc({
+			"doctype": "Stock Entry Detail",
+			"parentfield": "mtn_details",
+			"parenttype": "Stock Entry",
+			"parent": entry.name,
+			"__islocal": 1,
+			"idx": i+1,
+			"docstatus": 1,
+			"owner": entry.owner,
+			"name": voucher_detail_no,
+			"transfer_qty": abs(sl_entries[0].actual_qty),
+			"qty": abs(sl_entries[0].actual_qty),
+			"stock_uom": sl_entries[0].stock_uom,
+			"uom": sl_entries[0].stock_uom,
+			"conversion_factor": 1,
+			"item_code": sl_entries[0].item_code,
+			"description": webnotes.conn.get_value("Item", sl_entries[0].item_code,
+				"description"),
+			"incoming_rate": sl_entries[0].incoming_rate,
+			"batch_no": sl_entries[0].batch_no,
+			"serial_no": sl_entries[0].serial_no
+		})
+		
+		if sl_entries[0].item_code == fg_item:
+			stock_entry_detail.bom_no = entry.bom_no
+		
+		for sle in sl_entries:
+			if sle.actual_qty < 0:
+				stock_entry_detail.s_warehouse = sle.warehouse
+			else:
+				stock_entry_detail.t_warehouse = sle.warehouse
+				
+		stock_entry_detail.save()
+		
+	if entry.docstatus == 2:
+		webnotes.conn.set_value("Stock Entry", entry.name, "docstatus", 1)
+		
+		# call for cancelled ones
+		se = webnotes.bean("Stock Entry", entry.name)
+		controller = se.make_controller()
+		controller.update_production_order(1)
+		
+		res = webnotes.conn.sql("""select name from `tabStock Entry`
+			where amended_from=%s""", entry.name)
+		if res:
+			cancelled.append(res[0][0])
+			if res[0][0] in uncancelled:
+				uncancelled.remove(res[0][0])
+				
+			webnotes.bean("Stock Entry", res[0][0]).cancel()
+
+		uncancelled.append(se.doc.name)
+		
+def send_email():
+	from webnotes.utils.email_lib import sendmail_to_system_managers
+	global cancelled, uncancelled
+	uncancelled = "we have undone the cancellation of the following Stock Entries through a patch:\n" + \
+		"\n".join(uncancelled) if uncancelled else ""
+	cancelled = "and cancelled the following Stock Entries:\n" + "\n".join(cancelled) \
+		if cancelled else ""
+
+	subject = "[ERPNext] [Important] Cancellation undone for some Stock Entries"
+	content = """Dear user, 
+
+An error got introduced into the code that cleared the item table in Stock Entry associated to a Production Order.
+
+Hence, 
+%s
+
+%s
+
+You will have to edit them again.
+
+Sorry for the inconvenience this has caused.
+
+Regards,
+Team ERPNext.""" % (uncancelled, cancelled)
+
+	print subject, content
+
+	# sendmail_to_system_managers(subject, content)
\ No newline at end of file