Merge pull request #6699 from rohitwaghchaure/bom_price_list_issue

[Enhancement] Currency added in the BOM
diff --git a/erpnext/__init__.py b/erpnext/__init__.py
index 9660cb9..ddaa155 100644
--- a/erpnext/__init__.py
+++ b/erpnext/__init__.py
@@ -2,7 +2,7 @@
 from __future__ import unicode_literals
 import frappe
 
-__version__ = '7.1.16'
+__version__ = '7.1.19'
 
 def get_default_company(user=None):
 	'''Get default company for user'''
diff --git a/erpnext/accounts/doctype/account/account_tree.js b/erpnext/accounts/doctype/account/account_tree.js
index e63c797..4cf9a67 100644
--- a/erpnext/accounts/doctype/account/account_tree.js
+++ b/erpnext/accounts/doctype/account/account_tree.js
@@ -27,7 +27,8 @@
 		{fieldtype:'Check', fieldname:'is_group', label:__('Is Group'),
 			description: __('Further accounts can be made under Groups, but entries can be made against non-Groups')},
 		{fieldtype:'Select', fieldname:'root_type', label:__('Root Type'),
-			options: ['Asset', 'Liability', 'Equity', 'Income', 'Expense'].join('\n')},
+			options: ['Asset', 'Liability', 'Equity', 'Income', 'Expense'].join('\n'),
+			depends_on: 'eval:doc.is_group && !doc.parent_account'},
 		{fieldtype:'Select', fieldname:'account_type', label:__('Account Type'),
 			options: ['', 'Bank', 'Cash', 'Stock', 'Tax', 'Chargeable', 'Fixed Asset'].join('\n'),
 			description: __("Optional. This setting will be used to filter in various transactions.")
@@ -79,4 +80,4 @@
 		}
 	],
 	extend_toolbar: true
-}
\ No newline at end of file
+}
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py
index 9d891a5..2f3b101 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.py
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py
@@ -68,7 +68,7 @@
 	def on_cancel(self):
 		from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries
 		from erpnext.hr.doctype.salary_slip.salary_slip import unlink_ref_doc_from_salary_slip
-		unlink_ref_doc_from_payment_entries(self.doctype, self.name)
+		unlink_ref_doc_from_payment_entries(self)
 		unlink_ref_doc_from_salary_slip(self.name)
 		self.make_gl_entries(1)
 		self.update_advance_paid()
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 5c2b62c..14459fa 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -583,7 +583,7 @@
 		if not self.is_return:
 			from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries
 			if frappe.db.get_single_value('Accounts Settings', 'unlink_payment_on_cancellation_of_invoice'):
-				unlink_ref_doc_from_payment_entries(self.doctype, self.name)
+				unlink_ref_doc_from_payment_entries(self)
 
 			self.update_prevdoc_status()
 			self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 4e0a6b4..5dce71f 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -138,7 +138,7 @@
 
 		from erpnext.accounts.utils import unlink_ref_doc_from_payment_entries
 		if frappe.db.get_single_value('Accounts Settings', 'unlink_payment_on_cancellation_of_invoice'):
-			unlink_ref_doc_from_payment_entries(self.doctype, self.name)
+			unlink_ref_doc_from_payment_entries(self)
 
 		if self.is_return:
 			# NOTE status updating bypassed for is_return
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index f3dc7ba..cec01a0 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -401,16 +401,22 @@
 	payment_entry.set_amounts()
 	payment_entry.save(ignore_permissions=True)
 	
-def unlink_ref_doc_from_payment_entries(ref_type, ref_no):
-	remove_ref_doc_link_from_jv(ref_type, ref_no)
-	remove_ref_doc_link_from_pe(ref_type, ref_no)
+def unlink_ref_doc_from_payment_entries(ref_doc):
+	remove_ref_doc_link_from_jv(ref_doc.doctype, ref_doc.name)
+	remove_ref_doc_link_from_pe(ref_doc.doctype, ref_doc.name)
 	
 	frappe.db.sql("""update `tabGL Entry`
 		set against_voucher_type=null, against_voucher=null,
 		modified=%s, modified_by=%s
 		where against_voucher_type=%s and against_voucher=%s
 		and voucher_no != ifnull(against_voucher, '')""",
-		(now(), frappe.session.user, ref_type, ref_no))
+		(now(), frappe.session.user, ref_doc.doctype, ref_doc.name))
+	
+	if ref_doc.doctype in ("Sales Invoice", "Purchase Invoice"):
+		ref_doc.set("advances", [])
+	
+		frappe.db.sql("""delete from `tab{0} Advance` where parent = %s"""
+			.format(ref_doc.doctype), ref_doc.name)
 
 def remove_ref_doc_link_from_jv(ref_type, ref_no):
 	linked_jv = frappe.db.sql_list("""select parent from `tabJournal Entry Account`
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index eec07db..5796a4d 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -565,6 +565,21 @@
 							elif asset.status in ("Scrapped", "Cancelled", "Sold"):
 								frappe.throw(_("Row #{0}: Asset {1} cannot be submitted, it is already {2}")
 									.format(d.idx, d.asset, asset.status))
+									
+	def delink_advance_entries(self, linked_doc_name):
+		total_allocated_amount = 0
+		for adv in self.advances:
+			consider_for_total_advance = True
+			if adv.reference_name == linked_doc_name:
+				frappe.db.sql("""delete from `tab{0} Advance`
+					where name = %s""".format(self.doctype), adv.name)
+				consider_for_total_advance = False
+
+			if consider_for_total_advance:
+				total_allocated_amount += flt(adv.allocated_amount, adv.precision("allocated_amount"))
+
+		frappe.db.set_value(self.doctype, self.name, "total_advance", 
+			total_allocated_amount, update_modified=False)
 
 	def group_similar_items(self):
 		group_item_qty = {}
diff --git a/erpnext/controllers/tests/test_recurring_document.py b/erpnext/controllers/tests/test_recurring_document.py
index 58558c4..d47c5c7 100644
--- a/erpnext/controllers/tests/test_recurring_document.py
+++ b/erpnext/controllers/tests/test_recurring_document.py
@@ -110,7 +110,7 @@
 	no_of_months = ({"Monthly": 1, "Quarterly": 3, "Yearly": 12})[base_doc.recurring_type]
 
 	def _test(i):
-		obj.assertEquals(i+1, frappe.db.sql("""select count(name) from `tab%s`
+		obj.assertEquals(i+1, frappe.db.sql("""select count(*) from `tab%s`
 			where recurring_id=%s and (docstatus=1 or docstatus=0)""" % (base_doc.doctype, '%s'),
 			(base_doc.recurring_id))[0][0])
 
diff --git a/erpnext/docs/user/manual/de/customize-erpnext/custom-field.md b/erpnext/docs/user/manual/de/customize-erpnext/custom-field.md
index c3bec3f..25c40e1 100644
--- a/erpnext/docs/user/manual/de/customize-erpnext/custom-field.md
+++ b/erpnext/docs/user/manual/de/customize-erpnext/custom-field.md
@@ -12,7 +12,7 @@
 
 > Einstellungen > Anpassen > Benutzerdefiniertes Feld > Neu
 
-Sie können ein neues benutzerdefiniertes Feld auch über das [Werkzeug zum Anpassen von Feldern](https://erpnext.com/customize-erpnext/customize-form) einfügen.
+Sie können ein neues benutzerdefiniertes Feld auch über das [Werkzeug zum Anpassen von Feldern]({{docs_base_url}}/user/manual/de/customize-erpnext/customize-form) einfügen.
 
 In einem benutzerdefinierten Formular finden Sie für jedes Feld die Plus(+)-Option. Wenn Sie auf dieses Symbol klicken, wird eine neue Zeile oberhalb dieses Feldes eingefügt. Sie können die Einstellungen für Ihr Feld in der neu eingefügten leeren Zeile eingeben.
 
@@ -50,7 +50,7 @@
 
 ### Optionen einstellen
 
-Wenn Sie ein Verknüpfungsfeld erstellen,dann wird der Name des DocType, mit dem dieses Feld verknüpft werden soll, in das Feld "Optionen" eingefügt. Klicken Sie [hier](https://erpnext.com/kb/customize/creating-custom-link-field) um weitere Informationen darüber zu erhalten, wie man benutzerdefinierte Verknüpfungsfelder erstellt.
+Wenn Sie ein Verknüpfungsfeld erstellen,dann wird der Name des DocType, mit dem dieses Feld verknüpft werden soll, in das Feld "Optionen" eingefügt. Klicken Sie [hier]({{docs_base_url}}/user/manual/en/customize-erpnext/articles/creating-custom-link-field) um weitere Informationen darüber zu erhalten, wie man benutzerdefinierte Verknüpfungsfelder erstellt.
 
 ![Verknüpfung mit einem benutzerdefinierten Feld]({{docs_base_url}}/assets/old_images/erpnext/custom-field-link.png)
 
diff --git a/erpnext/docs/user/manual/de/customize-erpnext/customize-form.md b/erpnext/docs/user/manual/de/customize-erpnext/customize-form.md
index cd83533..dbb9bc5 100644
--- a/erpnext/docs/user/manual/de/customize-erpnext/customize-form.md
+++ b/erpnext/docs/user/manual/de/customize-erpnext/customize-form.md
@@ -71,7 +71,7 @@
     </tr>
     <tr>
       <td>Feldtyp</td>
-      <td>Klicken Sie <a href="https://erpnext.com/kb/customize/field-types">hier</a> um mehr über Feldtypen zu erfahren.</td>
+      <td>Klicken Sie <a href="{{docs_base_url}}/user/manual/en/customize-erpnext/articles/field-types">hier</a> um mehr über Feldtypen zu erfahren.</td>
     </tr>
     <tr>
       <td>Optionen</td>
diff --git a/erpnext/docs/user/manual/en/CRM/setup/sales-person.md b/erpnext/docs/user/manual/en/CRM/setup/sales-person.md
index 114003b..ce5ed93 100644
--- a/erpnext/docs/user/manual/en/CRM/setup/sales-person.md
+++ b/erpnext/docs/user/manual/en/CRM/setup/sales-person.md
@@ -8,7 +8,7 @@
 ####Sales Person in Transactions
 
 You can use this Sales Person in Customer and sales transactions like Sales Order, Delivery Note and Sales Invoice.
-Click [here](https://erpnext.com/kb/selling/managing-sales-persons-in-sales-transactions) to learn more 
+Click [here]({{docs_base_url}}/user/manual/en/selling/articles/sales-persons-in-the-sales-transactions) to learn more 
 about how Sales Persons are used in the transactions of Sales Cycle.
 
-{next}
\ No newline at end of file
+{next}
diff --git a/erpnext/docs/user/manual/en/accounts/payments.md b/erpnext/docs/user/manual/en/accounts/payments.md
index 7e5aab3..881d366 100644
--- a/erpnext/docs/user/manual/en/accounts/payments.md
+++ b/erpnext/docs/user/manual/en/accounts/payments.md
@@ -22,7 +22,7 @@
 
 <img class="screenshot" alt="Making Payment" src="{{docs_base_url}}/assets/img/accounts/payment-entry-9.png">
 
-For more details about payment entry [check here.](https://frappe.github.io/erpnext/user/manual/en/accounts/payment-entry)
+For more details about payment entry [check here.]({{docs_base_url}}/user/manual/en/accounts/payment-entry)
 
 ## Journal Entry
 
@@ -45,4 +45,4 @@
 Save and submit the journal entry to record the payament against the invoice
 <img class="screenshot" alt="Making Payment" src="{{docs_base_url}}/assets/img/accounts/journal-entry.png">
 
-For more details about journal entry [check here.](https://frappe.github.io/erpnext/user/manual/en/accounts/journal-entry)
\ No newline at end of file
+For more details about journal entry [check here.]({{docs_base_url}}/user/manual/en/accounts/journal-entry)
diff --git a/erpnext/docs/user/manual/en/human-resources/articles/employees-loan-management.md b/erpnext/docs/user/manual/en/human-resources/articles/employees-loan-management.md
index fddab31..d5c4d00 100644
--- a/erpnext/docs/user/manual/en/human-resources/articles/employees-loan-management.md
+++ b/erpnext/docs/user/manual/en/human-resources/articles/employees-loan-management.md
@@ -10,7 +10,7 @@
       
 #### 1.1  Employee Loan Account
 
-Create Group as 'Employees Loans' under Current Assets and create employee loan A/C (Ledger) under it. [Check this link for new account creation](https://erpnext.com/kb/setup/managing-tree-structure-masters)
+Create Group as 'Employees Loans' under Current Assets and create employee loan A/C (Ledger) under it. [Check this link for new account creation]({{docs_base_url}}/user/manual/en/setting-up/articles/managing-tree-structure-masters)
 
 ![CoA]({{docs_base_url}}/assets/img/articles/Selection_433.png)
 
@@ -52,4 +52,4 @@
 
 ![Loan Reco]({{docs_base_url}}/assets/img/articles/Selection_439.png)
 
-<!-- markdown -->
\ No newline at end of file
+<!-- markdown -->
diff --git a/erpnext/docs/user/manual/en/selling/setup/sales-person-target-allocation.md b/erpnext/docs/user/manual/en/selling/setup/sales-person-target-allocation.md
index e32c97d..75b7561 100644
--- a/erpnext/docs/user/manual/en/selling/setup/sales-person-target-allocation.md
+++ b/erpnext/docs/user/manual/en/selling/setup/sales-person-target-allocation.md
@@ -80,7 +80,7 @@
 
 ###See Also
 
-1. [Managing Sales Person](https://erpnext.com/selling/selling-setup/sales-person)
-2. [Using Sales Person in transactions](https://erpnext.com/kb/selling/managing-sales-persons-in-sales-transactions)
+1. [Sales Person Target Allocation]({{docs_base_url}}/user/manual/en/selling/setup/sales-person-target-allocation)
+2. [Using Sales Person in transactions]({{docs_base_url}}/user/manual/en/selling/articles/sales-persons-in-the-sales-transactions)
 
 {next}
diff --git a/erpnext/hr/doctype/daily_work_summary/daily_work_summary.py b/erpnext/hr/doctype/daily_work_summary/daily_work_summary.py
index 0e0abc4..7fff5f5 100644
--- a/erpnext/hr/doctype/daily_work_summary/daily_work_summary.py
+++ b/erpnext/hr/doctype/daily_work_summary/daily_work_summary.py
@@ -39,11 +39,14 @@
 
 		replies = frappe.get_all('Communication', fields=['content', 'text_content', 'sender'],
 			filters=dict(reference_doctype=self.doctype, reference_name=self.name,
-				communication_type='Communication', sent_or_received='Received'))
+				communication_type='Communication', sent_or_received='Received'),
+				order_by='creation asc')
 
 		did_not_reply = self.email_sent_to.split()
 
 		for d in replies:
+			d.sender_name = frappe.db.get_value("Employee", {"user_id": d.sender},
+				"employee_name") or d.sender
 			if d.sender in did_not_reply:
 				did_not_reply.remove(d.sender)
 			if d.text_content:
@@ -62,17 +65,17 @@
 
 	def get_summary_template(self):
 		return '''
-<h4>{{ title }}</h4>
+<h3>{{ title }}</h3>
 
 {% for reply in replies %}
-<h5>{{ frappe.db.get_value("Employee", {"user_id": reply.sender}, "employee_name") or reply.sender }}<h5>
+<h4>{{ reply.sender_name }}</h4>
 <p style="padding-bottom: 20px">
 	{{ reply.content }}
 </p>
+<hr>
 {% endfor %}
 
 {% if did_not_reply %}
-<hr>
 <p>{{ did_not_reply_title }}: {{ did_not_reply }}</p>
 {% endif %}
 
diff --git a/erpnext/patches/v4_0/fix_employee_user_id.py b/erpnext/patches/v4_0/fix_employee_user_id.py
index 2a8f76b..6f449f9 100644
--- a/erpnext/patches/v4_0/fix_employee_user_id.py
+++ b/erpnext/patches/v4_0/fix_employee_user_id.py
@@ -18,6 +18,6 @@
 			frappe.db.sql("""update `tabEmployee` set user_id=null
 				where user_id=%s and name!=%s""", (user_id, employee))
 		else:
-			count = frappe.db.sql("""select count(name) from `tabEmployee` where user_id=%s""", user_id)[0][0]
+			count = frappe.db.sql("""select count(*) from `tabEmployee` where user_id=%s""", user_id)[0][0]
 			frappe.db.sql("""update `tabEmployee` set user_id=null
 				where user_id=%s limit %s""", (user_id, count - 1))
diff --git a/erpnext/patches/v7_0/repost_gle_for_pi_with_update_stock.py b/erpnext/patches/v7_0/repost_gle_for_pi_with_update_stock.py
index 2883a8a..9f159be 100644
--- a/erpnext/patches/v7_0/repost_gle_for_pi_with_update_stock.py
+++ b/erpnext/patches/v7_0/repost_gle_for_pi_with_update_stock.py
@@ -9,6 +9,8 @@
 	if not cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
 		return
 
+	frappe.reload_doctype("Purchase Invoice")
+
 	for pi in frappe.db.sql("""select name from `tabPurchase Invoice` 
 		where update_stock=1 and docstatus=1  order by posting_date asc""", as_dict=1):
 		
diff --git a/erpnext/projects/doctype/timesheet/timesheet.json b/erpnext/projects/doctype/timesheet/timesheet.json
index 751b793..d06fbb5 100644
--- a/erpnext/projects/doctype/timesheet/timesheet.json
+++ b/erpnext/projects/doctype/timesheet/timesheet.json
@@ -220,7 +220,7 @@
    "collapsible": 0, 
    "columns": 0, 
    "depends_on": "", 
-   "description": "List of employee which has \"Salary Slip Based on Timesheet\" is enabled in salary structure.", 
+   "description": "", 
    "fieldname": "employee", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -877,7 +877,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-11-07 06:00:12.460318", 
+ "modified": "2016-11-22 01:42:54.204441", 
  "modified_by": "Administrator", 
  "module": "Projects", 
  "name": "Timesheet", 
diff --git a/erpnext/schools/doctype/student_admission/student_admission.py b/erpnext/schools/doctype/student_admission/student_admission.py
index b0c7b55..a849d65 100644
--- a/erpnext/schools/doctype/student_admission/student_admission.py
+++ b/erpnext/schools/doctype/student_admission/student_admission.py
@@ -23,7 +23,7 @@
 		context.parents = [{'name': 'admissions', 'title': _('All Student Admissions') }]
 
 	def get_title(self):
-		return _("Admissions for {0}").format(self.academic_term)
+		return _("Admissions for {0}").format(self.academic_year)
 
 def get_list_context(context):
 	context.title = _("Student Admissions")
diff --git a/erpnext/selling/page/sales_funnel/sales_funnel.py b/erpnext/selling/page/sales_funnel/sales_funnel.py
index 71dc8bd..4d12efd 100644
--- a/erpnext/selling/page/sales_funnel/sales_funnel.py
+++ b/erpnext/selling/page/sales_funnel/sales_funnel.py
@@ -8,7 +8,7 @@
 
 @frappe.whitelist()
 def get_funnel_data(from_date, to_date):
-	active_leads = frappe.db.sql("""select count(name) from `tabLead`
+	active_leads = frappe.db.sql("""select count(*) from `tabLead`
 		where (date(`modified`) between %s and %s)
 		and status != "Do Not Contact" """, (from_date, to_date))[0][0]
 
@@ -16,15 +16,15 @@
 		where (date(`modified`) between %s and %s)
 		and status != "Passive" """, (from_date, to_date))[0][0]
 
-	opportunities = frappe.db.sql("""select count(name) from `tabOpportunity`
+	opportunities = frappe.db.sql("""select count(*) from `tabOpportunity`
 		where (date(`creation`) between %s and %s)
 		and status != "Lost" """, (from_date, to_date))[0][0]
 
-	quotations = frappe.db.sql("""select count(name) from `tabQuotation`
+	quotations = frappe.db.sql("""select count(*) from `tabQuotation`
 		where docstatus = 1 and (date(`creation`) between %s and %s)
 		and status != "Lost" """, (from_date, to_date))[0][0]
 
-	sales_orders = frappe.db.sql("""select count(name) from `tabSales Order`
+	sales_orders = frappe.db.sql("""select count(*) from `tabSales Order`
 		where docstatus = 1 and (date(`creation`) between %s and %s)""", (from_date, to_date))[0][0]
 
 	return [
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 59fdf70..97eca54 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -66,7 +66,8 @@
 			});
 		}
 
-		if(this.frm.fields_dict["packed_items"].grid.get_field('batch_no')) {
+		if(this.frm.fields_dict["packed_items"] &&
+			this.frm.fields_dict["packed_items"].grid.get_field('batch_no')) {
 			this.frm.set_query("batch_no", "packed_items", function(doc, cdt, cdn) {
 				return me.set_query_for_batch(doc, cdt, cdn)
 			});
diff --git a/erpnext/setup/doctype/email_digest/email_digest.py b/erpnext/setup/doctype/email_digest/email_digest.py
index 791e54e..ac999ed 100644
--- a/erpnext/setup/doctype/email_digest/email_digest.py
+++ b/erpnext/setup/doctype/email_digest/email_digest.py
@@ -177,7 +177,7 @@
 		if not user_id:
 			user_id = frappe.session.user
 
-		return frappe.db.sql("""select count(name) from `tabToDo` 
+		return frappe.db.sql("""select count(*) from `tabToDo` 
 			where status='Open' and (owner=%s or assigned_by=%s)""",
 			(user_id, user_id))[0][0]
 
@@ -202,7 +202,7 @@
 	
 	def get_issue_count(self):
 		"""Get count of Issue"""
-		return frappe.db.sql("""select count(name) from `tabIssue`
+		return frappe.db.sql("""select count(*) from `tabIssue`
 			where status in ('Open','Replied') """)[0][0]
 
 	def get_project_list(self, user_id=None):
@@ -221,7 +221,7 @@
 
 	def get_project_count(self):
 		"""Get count of Project"""
-		return frappe.db.sql("""select count(name) from `tabProject`
+		return frappe.db.sql("""select count(*) from `tabProject`
 			where status='Open' and project_type='External'""")[0][0]
 
 	def set_accounting_cards(self, context):
diff --git a/erpnext/setup/doctype/item_group/item_group.py b/erpnext/setup/doctype/item_group/item_group.py
index e638161..12899cd 100644
--- a/erpnext/setup/doctype/item_group/item_group.py
+++ b/erpnext/setup/doctype/item_group/item_group.py
@@ -127,7 +127,7 @@
 
 def get_group_item_count(item_group):
 	child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(item_group)])
-	return frappe.db.sql("""select count(name) from `tabItem`
+	return frappe.db.sql("""select count(*) from `tabItem`
 		where docstatus = 0 and show_in_website = 1
 		and (item_group in (%s)
 			or name in (select parent from `tabWebsite Item Group`
diff --git a/erpnext/setup/doctype/naming_series/naming_series.py b/erpnext/setup/doctype/naming_series/naming_series.py
index 60a1270..67646f6 100644
--- a/erpnext/setup/doctype/naming_series/naming_series.py
+++ b/erpnext/setup/doctype/naming_series/naming_series.py
@@ -14,7 +14,8 @@
 class NamingSeries(Document):
 	def get_transactions(self, arg=None):
 		doctypes = list(set(frappe.db.sql_list("""select parent
-				from `tabDocField` where fieldname='naming_series'""")
+				from `tabDocField` df where fieldname='naming_series' and 
+				exists(select * from `tabDocPerm` dp, `tabRole` role where dp.role = role.name and dp.parent = df.parent and not role.disabled)""")
 			+ frappe.db.sql_list("""select dt from `tabCustom Field`
 				where fieldname='naming_series'""")))
 
diff --git a/erpnext/startup/boot.py b/erpnext/startup/boot.py
index 9b16ad7..97ef329 100644
--- a/erpnext/startup/boot.py
+++ b/erpnext/startup/boot.py
@@ -23,7 +23,7 @@
 			"Notification Control")
 
 		# if no company, show a dialog box to create a new company
-		bootinfo.customer_count = frappe.db.sql("""select count(name) from tabCustomer""")[0][0]
+		bootinfo.customer_count = frappe.db.sql("""select count(*) from tabCustomer""")[0][0]
 
 		if not bootinfo.customer_count:
 			bootinfo.setup_complete = frappe.db.sql("""select name from
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note_dashboard.py b/erpnext/stock/doctype/delivery_note/delivery_note_dashboard.py
index adbfa4d..c296d8c 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note_dashboard.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note_dashboard.py
@@ -2,10 +2,10 @@
 
 def get_data():
 	return {
-		'fieldname': 'delivery_note_no',
+		'fieldname': 'delivery_note',
 		'non_standard_fieldnames': {
-			'Sales Invoice': 'delivery_note',
-			'Packing Slip': 'delivery_note',
+			'Stock Entry': 'delivery_note_no',
+			'Quality Inspection': 'reference_name'
 		},
 		'internal_links': {
 			'Sales Order': ['items', 'against_sales_order'],
diff --git a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
index 9b83cf2..1cb0fcd 100644
--- a/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
+++ b/erpnext/stock/doctype/delivery_note_item/delivery_note_item.json
@@ -1491,7 +1491,7 @@
    "in_standard_filter": 0, 
    "label": "Against Sales Order", 
    "length": 0, 
-   "no_copy": 0, 
+   "no_copy": 1, 
    "options": "Sales Order", 
    "permlevel": 0, 
    "print_hide": 1, 
@@ -1519,7 +1519,7 @@
    "in_standard_filter": 0, 
    "label": "Against Sales Invoice", 
    "length": 0, 
-   "no_copy": 0, 
+   "no_copy": 1, 
    "options": "Sales Invoice", 
    "permlevel": 0, 
    "print_hide": 1, 
@@ -1691,7 +1691,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2016-11-16 16:05:03.945501", 
+ "modified": "2016-11-23 12:33:37.728117", 
  "modified_by": "Administrator", 
  "module": "Stock", 
  "name": "Delivery Note Item", 
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index ced6a64..3d248d1 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -141,7 +141,7 @@
 
 	def make_route(self):
 		if not self.route:
-			return frappe.db.get_value('Item Group', self.item_group, 'route') + '/' + self.scrub(self.item_name)
+			return cstr(frappe.db.get_value('Item Group', self.item_group, 'route')) + '/' + self.scrub(self.item_name)
 
 	def get_parents(self, context):
 		item_group, route = frappe.db.get_value('Item Group', self.item_group, ['name', 'route'])
diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py
index 051334c..3cc79ef 100644
--- a/erpnext/utilities/transaction_base.py
+++ b/erpnext/utilities/transaction_base.py
@@ -125,20 +125,6 @@
 			ret = None
 		
 		return ret
-	
-	def delink_advance_entries(self, linked_doc_name):
-		total_allocated_amount = 0
-		for adv in self.advances:
-			consider_for_total_advance = True
-			if adv.reference_name == linked_doc_name:
-				frappe.db.sql("""delete from `tab{0} Advance`
-					where name = %s""".format(self.doctype), adv.name)
-				consider_for_total_advance = False
-
-			if consider_for_total_advance:
-				total_allocated_amount += flt(adv.allocated_amount, adv.precision("allocated_amount"))
-
-		frappe.db.set_value(self.doctype, self.name, "total_advance", total_allocated_amount, update_modified=False)
 
 def delete_events(ref_type, ref_name):
 	frappe.delete_doc("Event", frappe.db.sql_list("""select name from `tabEvent`