Merge pull request #1753 from rmehta/develop

Address Template
diff --git a/erpnext/config/setup.py b/erpnext/config/setup.py
index 66b44e2..8db9ef2 100644
--- a/erpnext/config/setup.py
+++ b/erpnext/config/setup.py
@@ -32,6 +32,11 @@
 				},
 				{
 					"type": "doctype",
+					"name": "Address Template",
+					"description": _("Country wise default Address Templates")
+				},
+				{
+					"type": "doctype",
 					"name": "Terms and Conditions",
 					"description": _("Standard contract terms for Sales or Purchase.")
 				},
diff --git a/erpnext/hr/doctype/salary_manager/salary_manager.json b/erpnext/hr/doctype/salary_manager/salary_manager.json
index 39a1d9c..e430f3b 100644
--- a/erpnext/hr/doctype/salary_manager/salary_manager.json
+++ b/erpnext/hr/doctype/salary_manager/salary_manager.json
@@ -11,7 +11,7 @@
    "fieldname": "document_description", 
    "fieldtype": "HTML", 
    "label": "Document Description", 
-   "options": "<div class=\"field_description\">You can generate multiple salary slips based on the selected criteria, submit and mail those to the employee directly from here</div>", 
+   "options": "<div class=\"alert alert-info\">You can generate multiple salary slips based on the selected criteria, submit and mail those to the employee directly from here</div>", 
    "permlevel": 0
   }, 
   {
@@ -144,7 +144,7 @@
  "icon": "icon-cog", 
  "idx": 1, 
  "issingle": 1, 
- "modified": "2014-05-09 02:16:45.165977", 
+ "modified": "2014-06-04 06:46:39.437061", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Salary Manager", 
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index b5e66d4..a5a7ae4 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -45,3 +45,4 @@
 execute:import frappe.defaults;frappe.defaults.clear_default("price_list_currency")
 erpnext.patches.v4_0.update_account_root_type
 execute:frappe.delete_doc("Report", "Purchase In Transit")
+erpnext.patches.v4_0.new_address_template
diff --git a/erpnext/patches/v4_0/new_address_template.py b/erpnext/patches/v4_0/new_address_template.py
new file mode 100644
index 0000000..fc0c957
--- /dev/null
+++ b/erpnext/patches/v4_0/new_address_template.py
@@ -0,0 +1,9 @@
+import frappe
+
+def execute():
+	d = frappe.new_doc("Address Template")
+	d.update({"country":frappe.db.get_default("country")})
+	try:
+		d.insert()
+	except Exception:
+		pass
diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py
index 19bcce8..e2273bc 100644
--- a/erpnext/selling/doctype/customer/test_customer.py
+++ b/erpnext/selling/doctype/customer/test_customer.py
@@ -9,44 +9,43 @@
 from frappe.test_runner import make_test_records
 
 test_ignore = ["Price List"]
-			
+
 test_records = frappe.get_test_records('Customer')
 
 class TestCustomer(unittest.TestCase):
 	def test_party_details(self):
 		from erpnext.accounts.party import get_party_details
-		
+
 		to_check = {
-			'address_display': '_Test Address Line 1\n_Test City\nIndia\nPhone: +91 0000000000', 
-			'selling_price_list': None, 
-			'customer_group': '_Test Customer Group', 
-			'contact_designation': None, 
-			'customer_address': '_Test Address-Office', 
-			'contact_department': None, 
-			'contact_email': 'test_contact_customer@example.com', 
-			'contact_mobile': None, 
-			'sales_team': [], 
-			'contact_display': '_Test Contact For _Test Customer', 
-			'contact_person': '_Test Contact For _Test Customer-_Test Customer', 
-			'territory': u'_Test Territory', 
-			'contact_phone': '+91 0000000000', 
+			'selling_price_list': None,
+			'customer_group': '_Test Customer Group',
+			'contact_designation': None,
+			'customer_address': '_Test Address-Office',
+			'contact_department': None,
+			'contact_email': 'test_contact_customer@example.com',
+			'contact_mobile': None,
+			'sales_team': [],
+			'contact_display': '_Test Contact For _Test Customer',
+			'contact_person': '_Test Contact For _Test Customer-_Test Customer',
+			'territory': u'_Test Territory',
+			'contact_phone': '+91 0000000000',
 			'customer_name': '_Test Customer'
 		}
-		
+
 		make_test_records("Address")
 		make_test_records("Contact")
-						
+
 		details = get_party_details("_Test Customer")
-		
+
 		for key, value in to_check.iteritems():
 			self.assertEquals(value, details.get(key))
-		
+
 	def test_rename(self):
 		frappe.rename_doc("Customer", "_Test Customer 1", "_Test Customer 1 Renamed")
 
 		self.assertTrue(frappe.db.exists("Customer", "_Test Customer 1 Renamed"))
 		self.assertFalse(frappe.db.exists("Customer", "_Test Customer 1"))
-		
+
 		frappe.rename_doc("Customer", "_Test Customer 1 Renamed", "_Test Customer 1")
-		
+
 
diff --git a/erpnext/setup/page/setup_wizard/install_fixtures.py b/erpnext/setup/page/setup_wizard/install_fixtures.py
index 90ef1f4..4bdf15e 100644
--- a/erpnext/setup/page/setup_wizard/install_fixtures.py
+++ b/erpnext/setup/page/setup_wizard/install_fixtures.py
@@ -10,6 +10,9 @@
 def install(country=None):
 	records = [
 
+		# address template
+		{'doctype':"Address Template", "country": country},
+
 		# item group
 		{'doctype': 'Item Group', 'item_group_name': _('All Item Groups'),
 			'is_group': 'Yes', 'parent_item_group': ''},
@@ -189,7 +192,8 @@
 
 	from frappe.modules import scrub
 	for r in records:
-		doc = frappe.get_doc(r)
+		doc = frappe.new_doc(r.get("doctype"))
+		doc.update(r)
 
 		# ignore mandatory for root
 		parent_link_field = ("parent_" + scrub(doc.doctype))
diff --git a/erpnext/utilities/doctype/address/address.py b/erpnext/utilities/doctype/address/address.py
index b00b40d..01b9d9a 100644
--- a/erpnext/utilities/doctype/address/address.py
+++ b/erpnext/utilities/doctype/address/address.py
@@ -10,7 +10,6 @@
 from frappe.model.document import Document
 
 class Address(Document):
-
 	def autoname(self):
 		if not self.address_title:
 			self.address_title = self.customer \
@@ -56,22 +55,16 @@
 	if not isinstance(address_dict, dict):
 		address_dict = frappe.db.get_value("Address", address_dict, "*", as_dict=True) or {}
 
-	meta = frappe.get_meta("Address")
-	sequence = (("", "address_line1"),
-		("\n", "address_line2"),
-		("\n", "city"),
-		("\n", "state"),
-		("\n" + meta.get_label("pincode") + ": ", "pincode"),
-		("\n", "country"),
-		("\n" + meta.get_label("phone") + ": ", "phone"),
-		("\n" + meta.get_label("fax") + ": ", "fax"))
+	template = frappe.db.get_value("Address Template", \
+		{"country": address_dict.get("country")}, "template")
+	if not template:
+		template = frappe.db.get_value("Address Template", \
+			{"is_default": 1}, "template")
 
-	display = ""
-	for separator, fieldname in sequence:
-		if address_dict.get(fieldname):
-			display += separator + address_dict.get(fieldname)
+	if not template:
+		frappe.throw(_("No default Address Template found. Please create a new one"))
 
-	return display.strip()
+	return frappe.render_template(template, address_dict)
 
 def get_territory_from_address(address):
 	"""Tries to match city, state and country of address to existing territory"""
@@ -88,3 +81,6 @@
 			break
 
 	return territory
+
+
+
diff --git a/erpnext/utilities/doctype/address/test_address.py b/erpnext/utilities/doctype/address/test_address.py
index 815449a..1e36a44 100644
--- a/erpnext/utilities/doctype/address/test_address.py
+++ b/erpnext/utilities/doctype/address/test_address.py
@@ -1,6 +1,18 @@
 # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
+from __future__ import unicode_literals
 
 import frappe
-test_records = frappe.get_test_records('Address')
\ No newline at end of file
+test_records = frappe.get_test_records('Address')
+
+import unittest
+import frappe
+
+from erpnext.utilities.doctype.address.address import get_address_display
+
+class TestAddress(unittest.TestCase):
+	def test_template_works(self):
+		address = frappe.get_list("Address")[0].name
+		display = get_address_display(frappe.get_doc("Address", address).as_dict())
+		self.assertTrue(display)
diff --git a/erpnext/utilities/doctype/address_template/__init__.py b/erpnext/utilities/doctype/address_template/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/utilities/doctype/address_template/__init__.py
diff --git a/erpnext/utilities/doctype/address_template/address_template.json b/erpnext/utilities/doctype/address_template/address_template.json
new file mode 100644
index 0000000..378474e
--- /dev/null
+++ b/erpnext/utilities/doctype/address_template/address_template.json
@@ -0,0 +1,57 @@
+{
+ "autoname": "field:country", 
+ "creation": "2014-06-05 02:22:36.029850", 
+ "docstatus": 0, 
+ "doctype": "DocType", 
+ "document_type": "Master", 
+ "fields": [
+  {
+   "fieldname": "country", 
+   "fieldtype": "Link", 
+   "in_list_view": 1, 
+   "label": "Country", 
+   "options": "Country", 
+   "permlevel": 0, 
+   "reqd": 0, 
+   "search_index": 1
+  }, 
+  {
+   "description": "This format is used if country specific format is not found", 
+   "fieldname": "is_default", 
+   "fieldtype": "Check", 
+   "in_list_view": 1, 
+   "label": "Is Default", 
+   "permlevel": 0
+  }, 
+  {
+   "default": "{% if address_line2 %}{{ address_line2 }}<br>{% endif -%}\n{{ city }}<br>\n{% if state %}{{ state }}<br>{% endif -%}\n{% if pincode %} PIN:  {{ pincode }}<br>{% endif -%}\n{{ country }}<br>\n{% if phone %}Phone: {{ phone }}<br>{% endif -%}\n{% if fax %}Fax: {{ fax }}<br>{% endif -%}\n{% if email_id %}Email: {{ email_id }}<br>{% endif -%}\n", 
+   "description": "<h4>Default Template</h4>\n<p>Uses <a href=\"http://jinja.pocoo.org/docs/templates/\">Jinja Templating</a> and all the fields of Address (including Custom Fields if any) will be available</p>\n<pre><code>{{ address_line1 }}&lt;br&gt;\n{% if address_line2 %}{{ address_line2 }}&lt;br&gt;{% endif -%}\n{{ city }}&lt;br&gt;\n{% if state %}{{ state }}&lt;br&gt;{% endif -%}\n{% if pincode %} PIN:  {{ pincode }}&lt;br&gt;{% endif -%}\n{{ country }}&lt;br&gt;\n{% if phone %}Phone: {{ phone }}&lt;br&gt;{% endif -%}\n{% if fax %}Fax: {{ fax }}&lt;br&gt;{% endif -%}\n{% if email_id %}Email: {{ email_id }}&lt;br&gt;{% endif -%}\n</code></pre>", 
+   "fieldname": "template", 
+   "fieldtype": "Code", 
+   "label": "Template", 
+   "permlevel": 0
+  }
+ ], 
+ "icon": "icon-map-marker", 
+ "modified": "2014-06-05 06:14:13.200689", 
+ "modified_by": "Administrator", 
+ "module": "Utilities", 
+ "name": "Address Template", 
+ "name_case": "", 
+ "owner": "Administrator", 
+ "permissions": [
+  {
+   "create": 1, 
+   "delete": 1, 
+   "export": 1, 
+   "permlevel": 0, 
+   "read": 1, 
+   "report": 1, 
+   "role": "System Manager", 
+   "set_user_permissions": 1, 
+   "write": 1
+  }
+ ], 
+ "sort_field": "modified", 
+ "sort_order": "DESC"
+}
\ No newline at end of file
diff --git a/erpnext/utilities/doctype/address_template/address_template.py b/erpnext/utilities/doctype/address_template/address_template.py
new file mode 100644
index 0000000..39b8d21
--- /dev/null
+++ b/erpnext/utilities/doctype/address_template/address_template.py
@@ -0,0 +1,24 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe.model.document import Document
+from frappe import _
+
+class AddressTemplate(Document):
+	def validate(self):
+		defaults = frappe.db.get_values("Address Template",
+			{"is_default":1, "name":("!=", self.name)})
+		if not self.is_default:
+			if not defaults:
+				self.is_default = 1
+				frappe.msgprint(_("Setting this Address Template as default as there is no other default"))
+		else:
+			if defaults:
+				for d in defaults:
+					frappe.db.set_value("Address Template", d, "is_default", 0)
+
+	def on_trash(self):
+		if self.is_default:
+			frappe.throw(_("Default Address Template cannot be deleted"))
diff --git a/erpnext/utilities/doctype/address_template/test_address_template.py b/erpnext/utilities/doctype/address_template/test_address_template.py
new file mode 100644
index 0000000..953c852
--- /dev/null
+++ b/erpnext/utilities/doctype/address_template/test_address_template.py
@@ -0,0 +1,22 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: See license.txt
+
+from __future__ import unicode_literals
+
+import frappe
+test_records = frappe.get_test_records('Address Template')
+
+import unittest
+import frappe
+
+class TestAddressTemplate(unittest.TestCase):
+	def test_default_is_unset(self):
+		a = frappe.get_doc("Address Template", "India")
+		a.is_default = 1
+		a.save()
+
+		b = frappe.get_doc("Address Template", "Brazil")
+		b.is_default = 1
+		b.save()
+
+		self.assertEqual(frappe.db.get_value("Address Template", "India", "is_default"), 0)
diff --git a/erpnext/utilities/doctype/address_template/test_records.json b/erpnext/utilities/doctype/address_template/test_records.json
new file mode 100644
index 0000000..412c9e7
--- /dev/null
+++ b/erpnext/utilities/doctype/address_template/test_records.json
@@ -0,0 +1,13 @@
+[
+ {
+  "country": "India",
+  "is_default": 1,
+  "template": "{{ address_title }}<br>\n{{ address_line1 }}<br>\n{% if address_line2 %}{{ address_line2 }}<br>{% endif %}\n{{ city }}<br>\n{% if state %}{{ state }}<br>{% endif %}\n{% if pincode %} PIN / ZIP:  {{ pincode }}<br>{% endif %}\n{{ country }}<br>\n{% if phone %}Phone: {{ phone }}<br>{% endif %}\n{% if fax %}Fax: {{ fax }}<br>{% endif %}\n{% if email_id %}Email: {{ email_id }}<br>{% endif %}\n"
+ },
+ {
+  "country": "Brazil",
+  "is_default": 0,
+  "template": "{{ address_title }}<br>\n{{ address_line1 }}<br>\n{% if address_line2 %}{{ address_line2 }}<br>{% endif %}\n{{ city }}<br>\n{% if state %}{{ state }}<br>{% endif %}\n{% if pincode %} PIN / ZIP:  {{ pincode }}<br>{% endif %}\n{{ country }}<br>\n{% if phone %}Phone: {{ phone }}<br>{% endif %}\n{% if fax %}Fax: {{ fax }}<br>{% endif %}\n{% if email_id %}Email: {{ email_id }}<br>{% endif %}\n"
+ }
+]
+