Merge pull request #4175 from anandpdoshi/dynamic-linked-with

[fix] exclude gl entry and stock ledger entry in linked with. Depends on frappe/frappe#1339
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index 21f6578..01e4813 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -6,7 +6,7 @@
 from frappe.model.naming import make_autoname
 from frappe import _, msgprint, throw
 import frappe.defaults
-from frappe.utils import flt
+from frappe.utils import flt, cint, cstr
 from frappe.desk.reportview import build_match_conditions
 from erpnext.utilities.transaction_base import TransactionBase
 from erpnext.utilities.address_and_contact import load_address_and_contact
@@ -23,12 +23,21 @@
 	def autoname(self):
 		cust_master_name = frappe.defaults.get_global_default('cust_master_name')
 		if cust_master_name == 'Customer Name':
-			self.name = self.customer_name
+			self.name = self.get_customer_name()
 		else:
 			if not self.naming_series:
 				frappe.throw(_("Series is mandatory"), frappe.MandatoryError)
 
 			self.name = make_autoname(self.naming_series+'.#####')
+			
+	def get_customer_name(self):
+		if frappe.db.get_value("Customer", self.customer_name):
+			count = frappe.db.sql("""select ifnull(max(SUBSTRING_INDEX(name, ' ', -1)), 0) from tabCustomer
+				 where name like  '%{0} - %'""".format(self.customer_name), as_list=1)[0][0]
+			count = cint(count) + 1
+			return "{0} - {1}".format(self.customer_name, cstr(count))
+		
+		return self.customer_name
 
 	def validate(self):
 		self.flags.is_new_doc = self.is_new()
@@ -225,4 +234,4 @@
 		credit_limit = frappe.db.get_value("Customer Group", customer_group, "credit_limit") or \
 			frappe.db.get_value("Company", company, "credit_limit")
 
-	return credit_limit
+	return credit_limit
\ No newline at end of file
diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py
index f18194f..0bbe534 100644
--- a/erpnext/selling/doctype/customer/test_customer.py
+++ b/erpnext/selling/doctype/customer/test_customer.py
@@ -68,13 +68,41 @@
 		frappe.rename_doc("Customer", "_Test Customer 1 Renamed", "_Test Customer 1")
 
 	def test_freezed_customer(self):
+		make_test_records("Customer")
+		make_test_records("Item")
+		
 		frappe.db.set_value("Customer", "_Test Customer", "is_frozen", 1)
 
 		from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
 
 		so = make_sales_order(do_not_save= True)
+		
 		self.assertRaises(CustomerFrozen, so.save)
 
 		frappe.db.set_value("Customer", "_Test Customer", "is_frozen", 0)
 
 		so.save()
+	
+	def test_duplicate_customer(self):
+		if not frappe.db.get_value("Customer", "_Test Customer 1"):
+			test_customer_1 = frappe.get_doc({
+				 "customer_group": "_Test Customer Group",
+				 "customer_name": "_Test Customer 1",
+				 "customer_type": "Individual",
+				 "doctype": "Customer",
+				 "territory": "_Test Territory"
+			}).insert(ignore_permissions=True)
+		else:
+			test_customer_1 = frappe.get_doc("Customer", "_Test Customer 1")
+
+		duplicate_customer = frappe.get_doc({
+			 "customer_group": "_Test Customer Group",
+			 "customer_name": "_Test Customer 1",
+			 "customer_type": "Individual",
+			 "doctype": "Customer",
+			 "territory": "_Test Territory"
+		}).insert(ignore_permissions=True)
+
+		self.assertEquals("_Test Customer 1", test_customer_1.name)
+		self.assertEquals("_Test Customer 1 - 1", duplicate_customer.name)
+		self.assertEquals(test_customer_1.customer_name, duplicate_customer.customer_name)
\ No newline at end of file
diff --git a/erpnext/stock/doctype/delivery_note/test_delivery_note.py b/erpnext/stock/doctype/delivery_note/test_delivery_note.py
index 58f1c0e..760d63b 100644
--- a/erpnext/stock/doctype/delivery_note/test_delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/test_delivery_note.py
@@ -326,21 +326,19 @@
 
 	def test_delivery_of_bundled_items_to_target_warehouse(self):
 		set_perpetual_inventory()
+		frappe.db.set_value("Item", "_Test Item", "valuation_method", "FIFO")
 
-		create_stock_reconciliation(item_code="_Test Item", target="_Test Warehouse - _TC",
-			qty=50, rate=100)
-		create_stock_reconciliation(item_code="_Test Item Home Desktop 100",
-			target="_Test Warehouse - _TC",
-			qty=50, rate=100)
+		for warehouse in ("_Test Warehouse - _TC", "_Test Warehouse 1 - _TC"):
+			create_stock_reconciliation(item_code="_Test Item", target=warehouse,
+				qty=50, rate=100)
+			create_stock_reconciliation(item_code="_Test Item Home Desktop 100",
+				target=warehouse, qty=50, rate=100)
 
 		opening_qty_test_warehouse_1 = get_qty_after_transaction(warehouse="_Test Warehouse 1 - _TC")
 
 		dn = create_delivery_note(item_code="_Test Product Bundle Item",
 			qty=5, rate=500, target_warehouse="_Test Warehouse 1 - _TC", do_not_submit=True)
 
-		frappe.db.sql("""delete from `tabStock Ledger Entry`
-			where voucher_type=%s and voucher_no=%s""", (dn.doctype, dn.name))
-
 		dn.submit()
 
 		# qty after delivery
@@ -366,8 +364,6 @@
 		gl_entries = get_gl_entries("Delivery Note", dn.name)
 		self.assertTrue(gl_entries)
 
-		print frappe.as_json(gl_entries)
-
 		stock_value_difference = abs(frappe.db.sql("""select sum(stock_value_difference)
 			from `tabStock Ledger Entry` where voucher_type='Delivery Note' and voucher_no=%s
 			and warehouse='_Test Warehouse - _TC'""", dn.name)[0][0])
diff --git a/erpnext/templates/pages/order.html b/erpnext/templates/pages/order.html
index 7376392..046e6f6 100644
--- a/erpnext/templates/pages/order.html
+++ b/erpnext/templates/pages/order.html
@@ -53,8 +53,10 @@
             </div>
             <div class="col-sm-2 col-xs-3 text-right">
                 {{ d.get_formatted("amount") }}
+				<!-- output of get_formatted("rate") is unicode, to replace unicode use 
+					_("text {0}").decode("utf8").format(val) -->
                 <p class="text-muted small">{{
-                    _("Rate: {0}").format(d.get_formatted("rate")) }}</p>
+                    _("Rate: {0}").decode("utf8").format(d.get_formatted("rate")) }}</p>
             </div>
         </div>
         {% endfor %}