Merge pull request #30602 from deepeshgarg007/deferred_revenue_default_account_fixes

fix: Deferred Revenue/Expense Account validation
diff --git a/erpnext/accounts/deferred_revenue.py b/erpnext/accounts/deferred_revenue.py
index 0611f88..a8776fa 100644
--- a/erpnext/accounts/deferred_revenue.py
+++ b/erpnext/accounts/deferred_revenue.py
@@ -386,7 +386,6 @@
 				doc,
 				credit_account,
 				debit_account,
-				against,
 				amount,
 				base_amount,
 				end_date,
@@ -570,7 +569,6 @@
 	doc,
 	credit_account,
 	debit_account,
-	against,
 	amount,
 	base_amount,
 	posting_date,
@@ -591,6 +589,7 @@
 	journal_entry.voucher_type = (
 		"Deferred Revenue" if doc.doctype == "Sales Invoice" else "Deferred Expense"
 	)
+	journal_entry.process_deferred_accounting = deferred_process
 
 	debit_entry = {
 		"account": credit_account,
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.json b/erpnext/accounts/doctype/journal_entry/journal_entry.json
index 335fd35..4493c72 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.json
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.json
@@ -3,7 +3,7 @@
  "allow_auto_repeat": 1,
  "allow_import": 1,
  "autoname": "naming_series:",
- "creation": "2013-03-25 10:53:52",
+ "creation": "2022-01-25 10:29:58.717206",
  "doctype": "DocType",
  "document_type": "Document",
  "engine": "InnoDB",
@@ -13,6 +13,7 @@
   "voucher_type",
   "naming_series",
   "finance_book",
+  "process_deferred_accounting",
   "reversal_of",
   "tax_withholding_category",
   "column_break1",
@@ -524,13 +525,20 @@
    "label": "Reversal Of",
    "options": "Journal Entry",
    "read_only": 1
+  },
+  {
+   "fieldname": "process_deferred_accounting",
+   "fieldtype": "Link",
+   "label": "Process Deferred Accounting",
+   "options": "Process Deferred Accounting",
+   "read_only": 1
   }
  ],
  "icon": "fa fa-file-text",
  "idx": 176,
  "is_submittable": 1,
  "links": [],
- "modified": "2022-01-04 13:39:36.485954",
+ "modified": "2022-04-06 17:18:46.865259",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Journal Entry",
@@ -578,6 +586,7 @@
  "search_fields": "voucher_type,posting_date, due_date, cheque_no",
  "sort_field": "modified",
  "sort_order": "DESC",
+ "states": [],
  "title_field": "title",
  "track_changes": 1
 }
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/process_deferred_accounting/process_deferred_accounting.py b/erpnext/accounts/doctype/process_deferred_accounting/process_deferred_accounting.py
index 08a7f41..8ec726b 100644
--- a/erpnext/accounts/doctype/process_deferred_accounting/process_deferred_accounting.py
+++ b/erpnext/accounts/doctype/process_deferred_accounting/process_deferred_accounting.py
@@ -11,7 +11,7 @@
 	convert_deferred_expense_to_expense,
 	convert_deferred_revenue_to_income,
 )
-from erpnext.accounts.general_ledger import make_reverse_gl_entries
+from erpnext.accounts.general_ledger import make_gl_entries
 
 
 class ProcessDeferredAccounting(Document):
@@ -34,4 +34,4 @@
 			filters={"against_voucher_type": self.doctype, "against_voucher": self.name},
 		)
 
-		make_reverse_gl_entries(gl_entries=gl_entries)
+		make_gl_entries(gl_entries=gl_entries, cancel=1)
diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
index 6c38a7e..7781fe3 100644
--- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -2240,6 +2240,14 @@
 
 		check_gl_entries(self, si.name, expected_gle, "2019-01-30")
 
+	def test_deferred_revenue_missing_account(self):
+		si = create_sales_invoice(posting_date="2019-01-10", do_not_submit=True)
+		si.items[0].enable_deferred_revenue = 1
+		si.items[0].service_start_date = "2019-01-10"
+		si.items[0].service_end_date = "2019-03-15"
+
+		self.assertRaises(frappe.ValidationError, si.save)
+
 	def test_fixed_deferred_revenue(self):
 		deferred_account = create_account(
 			account_name="Deferred Revenue",
@@ -3104,7 +3112,7 @@
 
 		acc_settings = frappe.get_single("Accounts Settings")
 		acc_settings.book_deferred_entries_via_journal_entry = 0
-		acc_settings.submit_journal_entriessubmit_journal_entries = 0
+		acc_settings.submit_journal_entries = 0
 		acc_settings.save()
 
 		frappe.db.set_value("Accounts Settings", None, "acc_frozen_upto", None)
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 8a9318e..7cbd2bd 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -180,6 +180,7 @@
 			else:
 				self.validate_deferred_start_and_end_date()
 
+			self.validate_deferred_income_expense_account()
 			self.set_inter_company_account()
 
 		if self.doctype == "Purchase Invoice":
@@ -208,6 +209,27 @@
 				(self.doctype, self.name),
 			)
 
+	def validate_deferred_income_expense_account(self):
+		field_map = {
+			"Sales Invoice": "deferred_revenue_account",
+			"Purchase Invoice": "deferred_expense_account",
+		}
+
+		for item in self.get("items"):
+			if item.get("enable_deferred_revenue") or item.get("enable_deferred_expense"):
+				if not item.get(field_map.get(self.doctype)):
+					default_deferred_account = frappe.db.get_value(
+						"Company", self.company, "default_" + field_map.get(self.doctype)
+					)
+					if not default_deferred_account:
+						frappe.throw(
+							_(
+								"Row #{0}: Please update deferred revenue/expense account in item row or default account in company master"
+							).format(item.idx)
+						)
+					else:
+						item.set(field_map.get(self.doctype), default_deferred_account)
+
 	def validate_deferred_start_and_end_date(self):
 		for d in self.items:
 			if d.get("enable_deferred_revenue") or d.get("enable_deferred_expense"):