chore: fix logic for purchase invoice and some typos
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index eea7f4d..44e3e89 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -811,8 +811,8 @@
 		else:
 
 			# Use source/target exchange rate, so no difference amount is calculated.
-			# then update exchange gain/loss amount in refernece table
-			# if there is an amount, submit a JE for that
+			# then update exchange gain/loss amount in reference table
+			# if there is an exchange gain/loss amount in reference table, submit a JE for that
 
 			exchange_rate = 1
 			if self.payment_type == "Receive":
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index ee7dfb7..e60719c 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -972,7 +972,7 @@
 		"""
 		Make Exchange Gain/Loss journal for Invoices and Payments
 		"""
-		# Cancelling is existing exchange gain/loss journals is handled in on_cancel event
+		# Cancelling existing exchange gain/loss journals is handled in on_cancel event in accounts/utils.py
 		if self.docstatus == 1:
 			if self.get("doctype") == "Payment Entry":
 				gain_loss_to_book = [x for x in self.references if x.exchange_gain_loss != 0]
@@ -1027,6 +1027,10 @@
 							"Account", party_account, "account_currency"
 						)
 						dr_or_cr = "debit" if d.exchange_gain_loss > 0 else "credit"
+
+						if d.reference_doctype == "Purchase Invoice":
+							dr_or_cr = "debit" if dr_or_cr == "credit" else "credit"
+
 						reverse_dr_or_cr = "debit" if dr_or_cr == "credit" else "credit"
 
 						gain_loss_account = frappe.get_cached_value(
@@ -1080,7 +1084,6 @@
 
 						journal_entry.save()
 						journal_entry.submit()
-				# frappe.throw("stopping...")
 
 	def update_against_document_in_jv(self):
 		"""