fix: Multiple fixes for travis (#20924)

* fix: Multiple fixes for travis

* fix: Multiple fixes for travis

* fix: Run dashboard patch if table already exists

* fix: Multiple fixes for travis

* fix: deletion of auto-created batch
diff --git a/erpnext/accounts/doctype/gl_entry/gl_entry.py b/erpnext/accounts/doctype/gl_entry/gl_entry.py
index f9e4fd7..14d0531 100644
--- a/erpnext/accounts/doctype/gl_entry/gl_entry.py
+++ b/erpnext/accounts/doctype/gl_entry/gl_entry.py
@@ -233,35 +233,14 @@
 			frappe.throw(_("Outstanding for {0} cannot be less than zero ({1})").format(against_voucher, fmt_money(bal)))
 
 	if against_voucher_type in ["Sales Invoice", "Purchase Invoice", "Fees"]:
-		update_outstanding_amt_in_ref(against_voucher, against_voucher_type, bal)
-
-def update_outstanding_amt_in_ref(against_voucher, against_voucher_type, bal):
-	data = []
-	# Update outstanding amt on against voucher
-	if against_voucher_type == "Fees":
 		ref_doc = frappe.get_doc(against_voucher_type, against_voucher)
-		ref_doc.db_set('outstanding_amount', bal)
-		ref_doc.set_status(update=True)
-		return
-	elif against_voucher_type == "Purchase Invoice":
-		from erpnext.accounts.doctype.purchase_invoice.purchase_invoice import get_status
-		data = frappe.db.get_value(against_voucher_type, against_voucher, 
-			["name as purchase_invoice", "outstanding_amount", 
-			"is_return", "due_date", "docstatus"])
-	elif against_voucher_type == "Sales Invoice":
-		from erpnext.accounts.doctype.sales_invoice.sales_invoice import get_status
-		data = frappe.db.get_value(against_voucher_type, against_voucher, 
-			["name as sales_invoice", "outstanding_amount", "is_discounted", 
-			"is_return", "due_date", "docstatus"])
 
-	precision = frappe.get_precision(against_voucher_type, "outstanding_amount")
-	data = list(data)
-	data.append(precision)
-	status = get_status(data)
-	frappe.db.set_value(against_voucher_type, against_voucher, {
-		'outstanding_amount': bal,
-		'status': status
-	})
+		# Didn't use db_set for optimisation purpose
+		ref_doc.outstanding_amount = bal
+		frappe.db.set_value(against_voucher_type, against_voucher, 'outstanding_amount', bal)
+
+		ref_doc.set_status(update=True)
+
 
 def validate_frozen_account(account, adv_adj=None):
 	frozen_account = frappe.db.get_value("Account", account, "freeze_account")
diff --git a/erpnext/accounts/doctype/gl_entry/test_gl_entry.py b/erpnext/accounts/doctype/gl_entry/test_gl_entry.py
index 01ddd29..b4a547b 100644
--- a/erpnext/accounts/doctype/gl_entry/test_gl_entry.py
+++ b/erpnext/accounts/doctype/gl_entry/test_gl_entry.py
@@ -38,6 +38,7 @@
 			filters={"voucher_type": "Journal Entry", "voucher_no": je.name},
 			order_by="creation"
 		)
+
 		self.assertTrue(all(entry.to_rename == 1 for entry in gl_entries))
 		old_naming_series_current_value = frappe.db.sql("SELECT current from tabSeries where name = %s", naming_series)[0][0]
 
diff --git a/erpnext/accounts/doctype/payment_entry/test_payment_entry.py b/erpnext/accounts/doctype/payment_entry/test_payment_entry.py
index 5303743..a25e0e3 100644
--- a/erpnext/accounts/doctype/payment_entry/test_payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/test_payment_entry.py
@@ -161,15 +161,15 @@
 		pe.insert()
 		pe.submit()
 
-		outstanding_amount = flt(frappe.db.get_value("Sales Invoice", si.name, "outstanding_amount"))
-		self.assertEqual(outstanding_amount, 0)
-		self.assertEqual(si.status, 'Paid')
+		outstanding_amount, status = frappe.db.get_value("Sales Invoice", si.name, ["outstanding_amount", "status"])
+		self.assertEqual(flt(outstanding_amount), 0)
+		self.assertEqual(status, 'Paid')
 
 		pe.cancel()
 
-		outstanding_amount = flt(frappe.db.get_value("Sales Invoice", si.name, "outstanding_amount"))
-		self.assertEqual(outstanding_amount, 100)
-		self.assertEqual(si.status, 'Unpaid')
+		outstanding_amount, status = frappe.db.get_value("Sales Invoice", si.name, ["outstanding_amount", "status"])
+		self.assertEqual(flt(outstanding_amount), 100)
+		self.assertEqual(status, 'Unpaid')
 
 	def test_payment_against_purchase_invoice_to_check_status(self):
 		pi = make_purchase_invoice(supplier="_Test Supplier USD", debit_to="_Test Payable USD - _TC",
@@ -182,15 +182,15 @@
 		pe.insert()
 		pe.submit()
 
-		outstanding_amount = flt(frappe.db.get_value("Purchase Invoice", pi.name, "outstanding_amount"))
-		self.assertEqual(outstanding_amount, 0)
-		self.assertEqual(pi.status, 'Paid')
+		outstanding_amount, status = frappe.db.get_value("Purchase Invoice", pi.name, ["outstanding_amount", "status"])
+		self.assertEqual(flt(outstanding_amount), 0)
+		self.assertEqual(status, 'Paid')
 
 		pe.cancel()
 
-		outstanding_amount = flt(frappe.db.get_value("Purchase Invoice", pi.name, "outstanding_amount"))
-		self.assertEqual(outstanding_amount, 100)
-		self.assertEqual(pi.status, 'Unpaid')
+		outstanding_amount, status = frappe.db.get_value("Purchase Invoice", pi.name, ["outstanding_amount", "status"])
+		self.assertEqual(flt(outstanding_amount), 250)
+		self.assertEqual(status, 'Unpaid')
 
 	def test_payment_entry_against_ec(self):
 
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index cc992ce..87d40fc 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -125,27 +125,6 @@
 			else:
 				self.remarks = _("No Remarks")
 
-	def set_status(self, update=False, status=None, update_modified=True):
-		if self.is_new():
-			if self.get('amended_from'):
-				self.status = 'Draft'
-			return
-
-		if not status:
-			precision = self.precision("outstanding_amount")
-			args = [
-				self.name,
-				self.outstanding_amount,
-				self.is_return,
-				self.due_date,
-				self.docstatus,
-				precision
-			]
-			self.status = get_status(args)
-
-		if update:
-			self.db_set('status', self.status, update_modified = update_modified)
-
 	def set_missing_values(self, for_validate=False):
 		if not self.credit_to:
 			self.credit_to = get_party_account("Supplier", self.supplier, self.company)
@@ -1028,34 +1007,6 @@
 		# calculate totals again after applying TDS
 		self.calculate_taxes_and_totals()
 
-def get_status(*args):
-	purchase_invoice, outstanding_amount, is_return, due_date, docstatus, precision = args[0]
-
-	outstanding_amount = flt(outstanding_amount, precision)
-	due_date = getdate(due_date)
-	now_date = getdate()
-
-	if docstatus == 2:
-		status = "Cancelled"
-	elif docstatus == 1:
-		if outstanding_amount > 0 and due_date < now_date:
-			status = "Overdue"
-		elif outstanding_amount > 0 and due_date >= now_date:
-			status = "Unpaid"
-		#Check if outstanding amount is 0 due to debit note issued against invoice
-		elif outstanding_amount <= 0 and is_return == 0 and frappe.db.get_value('Purchase Invoice', {'is_return': 1, 'return_against': purchase_invoice, 'docstatus': 1}):
-			status = "Debit Note Issued"
-		elif is_return == 1:
-			status = "Return"
-		elif outstanding_amount <=0:
-			status = "Paid"
-		else:
-			status = "Submitted"
-	else:
-		status = "Draft"
-
-	return status
-
 def get_list_context(context=None):
 	from erpnext.controllers.website_list_for_contact import get_list_context
 	list_context = get_list_context(context)
@@ -1116,3 +1067,6 @@
 def make_inter_company_sales_invoice(source_name, target_doc=None):
 	from erpnext.accounts.doctype.sales_invoice.sales_invoice import make_inter_company_transaction
 	return make_inter_company_transaction("Purchase Invoice", source_name, target_doc)
+
+def on_doctype_update():
+	frappe.db.add_index("Purchase Invoice", ["supplier", "is_return", "return_against"])
diff --git a/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json b/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
index 4a7322d..ef90b94 100644
--- a/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
+++ b/erpnext/accounts/doctype/purchase_invoice_item/purchase_invoice_item.json
@@ -777,7 +777,7 @@
  "idx": 1,
  "istable": 1,
  "links": [],
- "modified": "2020-03-05 14:20:17.297284",
+ "modified": "2020-03-11 14:20:17.297284",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Purchase Invoice Item",
diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py
index c0e1285..c49ac29 100755
--- a/erpnext/accounts/doctype/sales_invoice/pos.py
+++ b/erpnext/accounts/doctype/sales_invoice/pos.py
@@ -423,7 +423,10 @@
 				name_list.append(name)
 
 	email_queue = make_email_queue(email_queue_list)
-	pos_profile = json.loads(pos_profile)
+
+	if isinstance(pos_profile, string_types):
+		pos_profile = json.loads(pos_profile)
+
 	customers = get_customers_list(pos_profile)
 	return {
 		'invoice': name_list,
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 7f7938d..ba1ceff 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -1223,18 +1223,38 @@
 				self.status = 'Draft'
 			return
 
+		precision = self.precision("outstanding_amount")
+		outstanding_amount = flt(self.outstanding_amount, precision)
+		due_date = getdate(self.due_date)
+		nowdate = getdate()
+
+		discounting_status = None
+		if self.is_discounted:
+			discountng_status = get_discounting_status(self.name)
+
 		if not status:
-			precision = self.precision("outstanding_amount")
-			args = [
-				self.name,
-				self.outstanding_amount,
-				self.is_discounted,
-				self.is_return,
-				self.due_date,
-				self.docstatus,
-				precision,
-			]
-			self.status = get_status(args)
+			if self.docstatus == 2:
+				status = "Cancelled"
+			elif self.docstatus == 1:
+				if outstanding_amount > 0 and due_date < nowdate and self.is_discounted and discountng_status=='Disbursed':
+					self.status = "Overdue and Discounted"
+				elif outstanding_amount > 0 and due_date < nowdate:
+					self.status = "Overdue"
+				elif outstanding_amount > 0 and due_date >= nowdate and self.is_discounted and discountng_status=='Disbursed':
+					self.status = "Unpaid and Discounted"
+				elif outstanding_amount > 0 and due_date >= nowdate:
+					self.status = "Unpaid"
+				#Check if outstanding amount is 0 due to credit note issued against invoice
+				elif outstanding_amount <= 0 and self.is_return == 0 and frappe.db.get_value('Sales Invoice', {'is_return': 1, 'return_against': self.name, 'docstatus': 1}):
+					self.status = "Credit Note Issued"
+				elif self.is_return == 1:
+					self.status = "Return"
+				elif outstanding_amount<=0:
+					self.status = "Paid"
+				else:
+					self.status = "Submitted"
+			else:
+				self.status = "Draft"
 
 		if update:
 			self.db_set('status', self.status, update_modified = update_modified)
@@ -1259,42 +1279,6 @@
 
 	return status
 
-def get_status(*args):
-	sales_invoice, outstanding_amount, is_discounted, is_return, due_date, docstatus, precision = args[0]
-
-	discounting_status = None
-	if is_discounted:
-		discounting_status = get_discounting_status(sales_invoice)
-
-	outstanding_amount = flt(outstanding_amount, precision)
-	due_date = getdate(due_date)
-	now_date = getdate()
-
-	if docstatus == 2:
-		status = "Cancelled"
-	elif docstatus == 1:
-		if outstanding_amount > 0 and due_date < now_date and is_discounted and discounting_status=='Disbursed':
-			status = "Overdue and Discounted"
-		elif outstanding_amount > 0 and due_date < now_date:
-			status = "Overdue"
-		elif outstanding_amount > 0 and due_date >= now_date and is_discounted and discounting_status=='Disbursed':
-			status = "Unpaid and Discounted"
-		elif outstanding_amount > 0 and due_date >= now_date:
-			status = "Unpaid"
-		#Check if outstanding amount is 0 due to credit note issued against invoice
-		elif outstanding_amount <= 0 and is_return == 0 and frappe.db.get_value('Sales Invoice', {'is_return': 1, 'return_against': sales_invoice, 'docstatus': 1}):
-			status = "Credit Note Issued"
-		elif is_return == 1:
-			status = "Return"
-		elif outstanding_amount <=0:
-			status = "Paid"
-		else:
-			status = "Submitted"
-	else:
-		status = "Draft"
-
-	return status
-
 def validate_inter_company_party(doctype, party, company, inter_company_reference):
 	if not party:
 		return
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index e48e6c9..3d5ce8a 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -786,7 +786,7 @@
 	def test_make_pos_invoice(self):
 		from erpnext.accounts.doctype.sales_invoice.pos import make_invoice
 
-		make_pos_profile()
+		pos_profile = make_pos_profile()
 		pr = make_purchase_receipt(company= "_Test Company with perpetual inventory",supplier_warehouse= "Work In Progress - TCP1", item_code= "_Test FG Item",warehouse= "Stores - TCP1",cost_center= "Main - TCP1")
 		pos = create_sales_invoice(company= "_Test Company with perpetual inventory", debit_to="Debtors - TCP1", item_code= "_Test FG Item", warehouse="Stores - TCP1", income_account = "Sales - TCP1", expense_account = "Cost of Goods Sold - TCP1", cost_center = "Main - TCP1", do_not_save=True)
 
@@ -802,7 +802,7 @@
 			pos.append("taxes", tax)
 
 		invoice_data = [{'09052016142': pos}]
-		si = make_invoice(invoice_data).get('invoice')
+		si = make_invoice(pos_profile, invoice_data).get('invoice')
 		self.assertEqual(si[0], '09052016142')
 
 		sales_invoice = frappe.get_all('Sales Invoice', fields =["*"], filters = {'offline_pos_name': '09052016142', 'docstatus': 1})
@@ -820,7 +820,7 @@
 		if allow_negative_stock:
 			frappe.db.set_value('Stock Settings', None, 'allow_negative_stock', 0)
 
-		make_pos_profile()
+		pos_profile = make_pos_profile()
 		timestamp = cint(time.time())
 
 		item = make_item("_Test POS Item")
@@ -834,7 +834,7 @@
 							{'mode_of_payment': 'Cash', 'account': 'Cash - _TC', 'amount': 330}]
 
 		invoice_data = [{timestamp: pos}]
-		si = make_invoice(invoice_data).get('invoice')
+		si = make_invoice(pos_profile, invoice_data).get('invoice')
 		self.assertEqual(si[0], timestamp)
 
 		sales_invoice = frappe.get_all('Sales Invoice', fields =["*"], filters = {'offline_pos_name': timestamp})
@@ -843,7 +843,7 @@
 		timestamp = cint(time.time())
 		pos["offline_pos_name"] = timestamp
 		invoice_data = [{timestamp: pos}]
-		si1 = make_invoice(invoice_data).get('invoice')
+		si1 = make_invoice(pos_profile, invoice_data).get('invoice')
 		self.assertEqual(si1[0], timestamp)
 
 		sales_invoice1 = frappe.get_all('Sales Invoice', fields =["*"], filters = {'offline_pos_name': timestamp})
diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py
index 6d53530..5ba455c 100644
--- a/erpnext/accounts/general_ledger.py
+++ b/erpnext/accounts/general_ledger.py
@@ -136,12 +136,11 @@
 
 
 def make_entry(args, adv_adj, update_outstanding, from_repost=False):
-	args.update({"doctype": "GL Entry"})
-	gle = frappe.get_doc(args)
+	gle = frappe.new_doc("GL Entry")
+	gle.update(args)
 	gle.flags.ignore_permissions = 1
 	gle.flags.from_repost = from_repost
 	gle.validate()
-	gle.flags.ignore_permissions = True
 	gle.db_insert()
 	gle.run_method("on_update_with_args", adv_adj, update_outstanding, from_repost)
 	gle.flags.ignore_validate = True
diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py
index b465a10..4d0520a 100644
--- a/erpnext/controllers/status_updater.py
+++ b/erpnext/controllers/status_updater.py
@@ -69,6 +69,17 @@
 		["Cancelled", "eval:self.docstatus==2"],
 		["Closed", "eval:self.status=='Closed'"],
 	],
+	"Purchase Invoice": [	
+		["Draft", None],	
+		["Submitted", "eval:self.docstatus==1"],	
+		["Paid", "eval:self.outstanding_amount==0 and self.docstatus==1"],	
+		["Return", "eval:self.is_return==1 and self.docstatus==1"],	
+		["Debit Note Issued",	
+			"eval:self.outstanding_amount <= 0 and self.docstatus==1 and self.is_return==0 and get_value('Purchase Invoice', {'is_return': 1, 'return_against': self.name, 'docstatus': 1})"],	
+		["Unpaid", "eval:self.outstanding_amount > 0 and getdate(self.due_date) >= getdate(nowdate()) and self.docstatus==1"],	
+		["Overdue", "eval:self.outstanding_amount > 0 and getdate(self.due_date) < getdate(nowdate()) and self.docstatus==1"],	
+		["Cancelled", "eval:self.docstatus==2"],	
+	],
 	"Material Request": [
 		["Draft", None],
 		["Stopped", "eval:self.status == 'Stopped'"],
diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py
index d452fe4..f6908c0 100644
--- a/erpnext/controllers/stock_controller.py
+++ b/erpnext/controllers/stock_controller.py
@@ -238,7 +238,7 @@
 		for d in self.items:
 			if not d.batch_no: continue
 
-			serial_nos = [d.name for d in frappe.get_all("Serial No", {'batch_no': d.batch_no})]
+			serial_nos = [sr.name for sr in frappe.get_all("Serial No", {'batch_no': d.batch_no})]
 			if serial_nos:
 				frappe.db.set_value("Serial No", { 'name': ['in', serial_nos] }, "batch_no", None)
 
diff --git a/erpnext/loan_management/doctype/loan/test_loan.py b/erpnext/loan_management/doctype/loan/test_loan.py
index 08c2f22..c2e5685 100644
--- a/erpnext/loan_management/doctype/loan/test_loan.py
+++ b/erpnext/loan_management/doctype/loan/test_loan.py
@@ -294,18 +294,21 @@
 
 		make_loan_disbursement_entry(loan.name, loan.loan_amount)
 
-		frappe.db.sql(""" UPDATE `tabLoan Security Price` SET loan_security_price = %s
-		where loan_security=%s""", (100, 'Test Security 2'))
+		frappe.db.sql(""" UPDATE `tabLoan Security Price` SET loan_security_price = 100
+			where loan_security='Test Security 2'""")
 
 		check_for_ltv_shortfall()
 		loan_security_shortfall = frappe.get_doc("Loan Security Shortfall", {"loan": loan.name})
-
 		self.assertTrue(loan_security_shortfall)
 
 		self.assertEquals(loan_security_shortfall.loan_amount, 1000000.00)
 		self.assertEquals(loan_security_shortfall.security_value, 400000.00)
 		self.assertEquals(loan_security_shortfall.shortfall_amount, 600000.00)
 
+		frappe.db.sql(""" UPDATE `tabLoan Security Price` SET loan_security_price = 250
+			where loan_security='Test Security 2'""")
+
+
 def create_loan_accounts():
 	if not frappe.db.exists("Account", "Loans and Advances (Assets) - _TC"):
 		frappe.get_doc({
@@ -399,7 +402,8 @@
 			"doctype": "Loan Security Type",
 			"loan_security_type": "Stock",
 			"unit_of_measure": "Nos",
-			"haircut": 50.00
+			"haircut": 50.00,
+			"loan_to_value_ratio": 50
 		}).insert(ignore_permissions=True)
 
 def create_loan_security():
diff --git a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py
index a3442e4..b8e6dab 100644
--- a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py
+++ b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py
@@ -130,9 +130,10 @@
 			loan.loan_account, loan.principal_amount + loan.balance_loan_amount, loan.interest_amount,
 			payable_principal = loan.principal_amount , posting_date=posting_date)
 
-	frappe.db.sql("""UPDATE `tabRepayment Schedule`
-		SET is_accrued = 1 where name in (%s)""" #nosec
-		% ", ".join(['%s']*len(accrued_entries)), tuple(accrued_entries))
+	if accrued_entries:
+		frappe.db.sql("""UPDATE `tabRepayment Schedule`
+			SET is_accrued = 1 where name in (%s)""" #nosec
+			% ", ".join(['%s']*len(accrued_entries)), tuple(accrued_entries))
 
 def make_loan_interest_accrual_entry(loan, applicant_type, applicant, interest_income_account, loan_account,
 	pending_principal_amount, interest_amount, payable_principal=None, process_loan_interest=None, posting_date=None):
diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py
index dd4a872..1df5b3b 100644
--- a/erpnext/manufacturing/doctype/work_order/work_order.py
+++ b/erpnext/manufacturing/doctype/work_order/work_order.py
@@ -279,7 +279,7 @@
 			if enable_capacity_planning and job_card_doc:
 				row.planned_start_time = job_card_doc.time_logs[-1].from_time
 				row.planned_end_time = job_card_doc.time_logs[-1].to_time
-
+				print(row.planned_start_time, original_start_time, plan_days)
 				if date_diff(row.planned_start_time, original_start_time) > plan_days:
 					frappe.message_log.pop()
 					frappe.throw(_("Unable to find the time slot in the next {0} days for the operation {1}.")
diff --git a/erpnext/setup/doctype/company/test_company.py b/erpnext/setup/doctype/company/test_company.py
index af30abd..b37cc17 100644
--- a/erpnext/setup/doctype/company/test_company.py
+++ b/erpnext/setup/doctype/company/test_company.py
@@ -9,7 +9,7 @@
 from frappe.utils import random_string
 from erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts import get_charts_for_country
 
-test_ignore = ["Account", "Cost Center", "Payment Terms Template", "Salary Component"]
+test_ignore = ["Account", "Cost Center", "Payment Terms Template", "Salary Component", "Warehouse"]
 test_dependencies = ["Fiscal Year"]
 test_records = frappe.get_test_records('Company')
 
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index 74ae627..f805a67 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -183,12 +183,17 @@
 		# default warehouse, or Stores
 		for default in self.item_defaults or [frappe._dict({'company': frappe.defaults.get_defaults().company})]:
 			default_warehouse = (default.default_warehouse
-					or frappe.db.get_single_value('Stock Settings', 'default_warehouse')
-					or frappe.db.get_value('Warehouse', {'warehouse_name': _('Stores')}))
+					or frappe.db.get_single_value('Stock Settings', 'default_warehouse'))
+			if default_warehouse:
+				warehouse_company = frappe.db.get_value("Warehouse", default_warehouse, "company")
+			
+			if not default_warehouse or warehouse_company != default.company:
+				default_warehouse = frappe.db.get_value('Warehouse',
+					{'warehouse_name': _('Stores'), 'company': default.company})
 
 			if default_warehouse:
 				stock_entry = make_stock_entry(item_code=self.name, target=default_warehouse, qty=self.opening_stock,
-												rate=self.valuation_rate, company=default.company)
+					rate=self.valuation_rate, company=default.company)
 
 				stock_entry.add_comment("Comment", _("Opening Stock"))
 
diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
index d80e8f2..cba7f20 100644
--- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
@@ -53,7 +53,7 @@
 		self.assertFalse(get_gl_entries("Purchase Receipt", pr.name))
 	
 	def test_batched_serial_no_purchase(self):
-		item = frappe.get_doc("Item", { 'item_name': 'Batched Serialized Item' })
+		item = frappe.db.exists("Item", {'item_name': 'Batched Serialized Item'})
 		if not item:
 			item = create_item("Batched Serialized Item")
 			item.has_batch_no = 1
@@ -62,6 +62,8 @@
 			item.batch_number_series = "BS-BATCH-.##"
 			item.serial_no_series = "BS-.####"
 			item.save()
+		else:
+			item = frappe.get_doc("Item", {'item_name': 'Batched Serialized Item'})
 
 		pr = make_purchase_receipt(item_code=item.name, qty=5, rate=500)
 
@@ -302,6 +304,8 @@
 		self.assertEqual(serial_no, frappe.db.get_value("Serial No",
 			{"purchase_document_type": "Purchase Receipt", "purchase_document_no": pr_doc.name}, "name"))
 
+		pr_doc.cancel()
+
 		#check for the auto created serial nos
 		item_code = "Test Auto Created Serial No"
 		if not frappe.db.exists("Item", item_code):
@@ -317,9 +321,9 @@
 		from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
 		from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
 
-		item_code = frappe.db.get_value('Item', {'has_serial_no': 1, 'is_fixed_asset': 0})
+		item_code = frappe.db.get_value('Item', {'has_serial_no': 1, 'is_fixed_asset': 0, "has_batch_no": 0})
 		if not item_code:
-			item = make_item("Test Serial Item 1", dict(has_serial_no=1))
+			item = make_item("Test Serial Item 1", dict(has_serial_no=1, has_batch_no=0))
 			item_code = item.name
 
 		serial_no = random_string(5)
diff --git a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
index a876d16..a8b9c81 100644
--- a/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
+++ b/erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
@@ -832,7 +832,7 @@
  "idx": 1,
  "istable": 1,
  "links": [],
- "modified": "2020-03-05 14:19:48.799370",
+ "modified": "2020-03-11 14:19:48.799370",
  "modified_by": "Administrator",
  "module": "Stock",
  "name": "Purchase Receipt Item",