[fix] Validate Party in not present in Party Type (#12910)

* [fix] Validate Party in not present in Party Type

* [fix] Minor Changes

* [fix] Valid Customer and Supplier fetch

* [WIP] Allow and prevent for creating party

* [fix] minor changes

* [fix] Requested Changes

* [fix] Codacy issue

* Update opening_invoice_creation_tool.py
diff --git a/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.json b/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.json
index cde4c99..dca07ff 100644
--- a/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.json
+++ b/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.json
@@ -49,6 +49,66 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "description": "Create missing customer or supplier.", 
+   "fieldname": "create_missing_party", 
+   "fieldtype": "Check", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Create Missing Party", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "column_break_3", 
+   "fieldtype": "Column Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "invoice_type", 
    "fieldtype": "Select", 
    "hidden": 0, 
@@ -145,7 +205,7 @@
  "issingle": 1, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-09-05 01:30:33.235664", 
+ "modified": "2018-02-14 17:59:35.269118", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Opening Invoice Creation Tool", 
diff --git a/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py b/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
index 930375d..753540e 100644
--- a/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
+++ b/erpnext/accounts/doctype/opening_invoice_creation_tool/opening_invoice_creation_tool.py
@@ -73,6 +73,14 @@
 			if not row.temporary_opening_account:
 				row.temporary_opening_account = get_temporary_opening_account(self.company)
 			row.party_type = "Customer" if self.invoice_type == "Sales" else "Supplier"
+			
+			# Allow to create invoice even if no party present in customer or supplier.
+			if not frappe.db.exists(row.party_type, row.party):
+				if self.create_missing_party:
+					self.add_party(row.party_type, row.party)
+				else:
+					frappe.throw(_("{0} {1} does not exist.").format(frappe.bold(row.party_type), frappe.bold(row.party)))
+
 			if not row.item_name:
 				row.item_name = _("Opening Invoice Item")
 			if not row.posting_date:
@@ -107,13 +115,28 @@
 
 		return names
 
+	def add_party(self, party_type, party):
+		party_doc = frappe.new_doc(party_type)
+		if party_type == "Customer":
+			party_doc.customer_name = party
+		else:
+			supplier_type = frappe.db.get_single_value("Buying Settings", "supplier_type")
+			if not supplier_type:
+				frappe.throw(_("Please Set Supplier Type in Buying Settings."))
+
+			party_doc.supplier_name = party
+			party_doc.supplier_type = supplier_type
+
+		party_doc.flags.ignore_mandatory = True
+		party_doc.save(ignore_permissions=True)		
+
 	def get_invoice_dict(self, row=None):
 		def get_item_dict():
 			default_uom = frappe.db.get_single_value("Stock Settings", "stock_uom") or _("Nos")
 			cost_center = frappe.db.get_value("Company", self.company, "cost_center")
 			if not cost_center:
 				frappe.throw(
-					_("Please set the Default Cost Center in {0} company").format(frappe.bold(self.company))
+					_("Please set the Default Cost Center in {0} company.").format(frappe.bold(self.company))
 				)
 			rate = flt(row.outstanding_amount) / flt(row.qty)
 
@@ -163,3 +186,5 @@
 		frappe.throw(_("Please add a Temporary Opening account in Chart of Accounts"))
 
 	return accounts[0].name
+
+