Merge branch 'master' into staging-fixes
diff --git a/erpnext/__init__.py b/erpnext/__init__.py
index 4197d54..b9b5347 100644
--- a/erpnext/__init__.py
+++ b/erpnext/__init__.py
@@ -5,7 +5,7 @@
 from erpnext.hooks import regional_overrides
 from frappe.utils import getdate
 
-__version__ = '10.1.76'
+__version__ = '10.1.77'
 
 def get_default_company(user=None):
 	'''Get default company for user'''
diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.py b/erpnext/accounts/doctype/gl_entry/gl_entry.py
index e6fe6ca..810b6f7 100644
--- a/erpnext/accounts/doctype/gl_entry/gl_entry.py
+++ b/erpnext/accounts/doctype/gl_entry/gl_entry.py
@@ -18,15 +18,14 @@
 		self.flags.ignore_submit_comment = True
 		self.check_mandatory()
 		self.validate_and_set_fiscal_year()
+		self.pl_must_have_cost_center()
+		self.validate_cost_center()
 
 		if not self.flags.from_repost:
-			self.pl_must_have_cost_center()
 			self.check_pl_account()
-			self.validate_cost_center()
 			self.validate_party()
 			self.validate_currency()
 
-
 	def on_update_with_args(self, adv_adj, update_outstanding = 'Yes', from_repost=False):
 		if not from_repost:
 			self.validate_account_details(adv_adj)
diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py
index ea600e4..78aeee7 100644
--- a/erpnext/regional/india/utils.py
+++ b/erpnext/regional/india/utils.py
@@ -11,22 +11,49 @@
 	if not hasattr(doc, 'gstin'):
 		return
 
-	if doc.gstin:
-		doc.gstin = doc.gstin.upper()
-		if doc.gstin not in ["NA", "na"]:
-			p = re.compile("[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9A-Za-z]{1}[Z]{1}[0-9a-zA-Z]{1}")
-			if not p.match(doc.gstin):
-				frappe.throw(_("Invalid GSTIN or Enter NA for Unregistered"))
+	doc.gstin = doc.gstin.upper().strip()
+	if not doc.gstin or doc.gstin == 'NA':
+		return
+
+	if len(doc.gstin) != 15:
+		frappe.throw(_("Invalid GSTIN! A GSTIN must have 15 characters."))
+
+	p = re.compile("^[0-9]{2}[A-Z]{4}[0-9A-Z]{1}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}[1-9A-Z]{1}[0-9A-Z]{1}$")
+	if not p.match(doc.gstin):
+		frappe.throw(_("Invalid GSTIN! The input you've entered doesn't match the format of GSTIN."))
+
+	validate_gstin_check_digit(doc.gstin)
 
 	if not doc.gst_state:
-		if doc.state in states:
-			doc.gst_state = doc.state
+		if not doc.state:
+			return
+		state = doc.state.lower()
+		states_lowercase = {s.lower():s for s in states}
+		if state in states_lowercase:
+			doc.gst_state = states_lowercase[state]
+		else:
+			return
 
-	if doc.gst_state:
-		doc.gst_state_number = state_numbers[doc.gst_state]
-		if doc.gstin and doc.gstin != "NA" and doc.gst_state_number != doc.gstin[:2]:
-			frappe.throw(_("First 2 digits of GSTIN should match with State number {0}")
-				.format(doc.gst_state_number))
+	doc.gst_state_number = state_numbers[doc.gst_state]
+	if doc.gst_state_number != doc.gstin[:2]:
+		frappe.throw(_("Invalid GSTIN! First 2 digits of GSTIN should match with State number {0}.")
+			.format(doc.gst_state_number))
+
+def validate_gstin_check_digit(gstin):
+	''' Function to validate the check digit of the GSTIN.'''
+	factor = 1
+	total = 0
+	code_point_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+	mod = len(code_point_chars)
+	input_chars = gstin[:-1]
+	for char in input_chars:
+		digit = factor * code_point_chars.find(char)
+		digit = (digit // mod) + (digit % mod)
+		total += digit
+		factor = 2 if factor == 1 else 1
+	if gstin[-1] != code_point_chars[((mod - (total % mod)) % mod)]:
+		frappe.throw(_("Invalid GSTIN! The check digit validation has failed. " +
+			"Please ensure you've typed the GSTIN correctly."))
 
 def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
 	if frappe.get_meta(item_doctype).has_field('gst_hsn_code'):
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index 0da8272..2970d7a 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -177,6 +177,11 @@
 				frappe.throw(_("""New credit limit is less than current outstanding amount for the customer. Credit limit has to be atleast {0}""").format(outstanding_amt))
 
 	def on_trash(self):
+		if self.customer_primary_contact:
+			frappe.db.sql("""update `tabCustomer`
+				set customer_primary_contact=null, mobile_no=null, email_id=null
+				where name=%s""", self.name)
+
 		delete_contact_and_address('Customer', self.name)
 		if self.lead_name:
 			frappe.db.sql("update `tabLead` set status='Interested' where name=%s", self.lead_name)
diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py
index cc2fc59..fa4f835 100644
--- a/erpnext/selling/doctype/customer/test_customer.py
+++ b/erpnext/selling/doctype/customer/test_customer.py
@@ -98,6 +98,15 @@
 
 		so.save()
 
+	def test_delete_customer_contact(self):
+		customer = frappe.get_doc(
+			get_customer_dict('_Test Customer for delete')).insert(ignore_permissions=True)
+
+		customer.mobile_no = "8989889890"
+		customer.save()
+		self.assertTrue(customer.customer_primary_contact)
+		frappe.delete_doc('Customer', customer.name)
+
 	def test_disabled_customer(self):
 		make_test_records("Item")
 
diff --git a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
index 212bb51..bc991b4 100644
--- a/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
+++ b/erpnext/stock/doctype/stock_reconciliation/test_stock_reconciliation.py
@@ -10,7 +10,9 @@
 from erpnext.accounts.utils import get_stock_and_account_difference
 from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import set_perpetual_inventory
 from erpnext.stock.stock_ledger import get_previous_sle, update_entries_after
-from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import EmptyStockReconciliationItemsError
+from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import EmptyStockReconciliationItemsError, get_items
+from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse
+from erpnext.stock.doctype.item.test_item import make_item
 
 class TestStockReconciliation(unittest.TestCase):
 	def setUp(self):
@@ -79,6 +81,17 @@
 
 			set_perpetual_inventory(0)
 
+	def test_get_items(self):
+		create_warehouse("_Test Warehouse Group 1", {"is_group": 1})
+		create_warehouse("_Test Warehouse Ledger 1", {"is_group": 0, "parent_warehouse": "_Test Warehouse Group 1 - _TC"})
+		make_item("_Test Stock Reco Item", {"default_warehouse": "_Test Warehouse Ledger 1 - _TC",
+			"is_stock_item": 1, "opening_stock": 100, "valuation_rate": 100})
+
+		items = get_items("_Test Warehouse Group 1 - _TC", nowdate(), nowtime())
+
+		self.assertEqual(["_Test Stock Reco Item", "_Test Warehouse Ledger 1 - _TC", 100],
+			[items[0]["item_code"], items[0]["warehouse"], items[0]["qty"]])
+
 	def insert_existing_sle(self):
 		from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
 
diff --git a/erpnext/stock/doctype/warehouse/test_warehouse.py b/erpnext/stock/doctype/warehouse/test_warehouse.py
index 006945e..b033f86 100644
--- a/erpnext/stock/doctype/warehouse/test_warehouse.py
+++ b/erpnext/stock/doctype/warehouse/test_warehouse.py
@@ -90,7 +90,7 @@
 		self.assertTrue(frappe.db.get_value("Warehouse",
 			filters={"account": "Test Warehouse for Merging 2 - _TC"}))
 
-def create_warehouse(warehouse_name):
+def create_warehouse(warehouse_name, properties=None):
 	if not frappe.db.exists("Warehouse", warehouse_name + " - _TC"):
 		w = frappe.new_doc("Warehouse")
 		w.warehouse_name = warehouse_name
@@ -98,11 +98,13 @@
 		w.company = "_Test Company"
 		make_account_for_warehouse(warehouse_name, w)
 		w.account = warehouse_name + " - _TC"
+		if properties:
+			w.update(properties)
 		w.save()
 
 def make_account_for_warehouse(warehouse_name, warehouse_obj):
 	if not frappe.db.exists("Account", warehouse_name + " - _TC"):
-		parent_account = frappe.db.get_value('Account', 
+		parent_account = frappe.db.get_value('Account',
 			{'company': warehouse_obj.company, 'is_group':1, 'account_type': 'Stock'},'name')
 		account = create_account(account_name=warehouse_name, \
 				account_type="Stock", parent_account= parent_account, company=warehouse_obj.company)
\ No newline at end of file