Merge pull request #16569 from nabinhait/salary-allow-on-submit

fix: Allow-on-submit enabled for formula and condition in salary structure
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index f2d5006..0dd716d 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -231,7 +231,7 @@
 					item.expense_account = warehouse_account[item.warehouse]["account"]
 				else:
 					item.expense_account = stock_not_billed_account
-			elif item.is_fixed_asset and d.pr_detail:
+			elif item.is_fixed_asset and item.pr_detail:
 				item.expense_account = asset_received_but_not_billed
 			elif not item.expense_account and for_validate:
 				throw(_("Expense account is mandatory for item {0}").format(item.item_code or item.item_name))
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index e145a35..58e3e87 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -617,14 +617,14 @@
 
 def get_outstanding_invoices(party_type, party, account, condition=None, limit=1000):
 	outstanding_invoices = []
-	precision = frappe.get_precision("Sales Invoice", "outstanding_amount")
+	precision = frappe.get_precision("Sales Invoice", "outstanding_amount") or 2
 
 	if erpnext.get_party_account_type(party_type) == 'Receivable':
 		dr_or_cr = "debit_in_account_currency - credit_in_account_currency"
-		payment_dr_or_cr = "payment_gl_entry.credit_in_account_currency - payment_gl_entry.debit_in_account_currency"
+		payment_dr_or_cr = "credit_in_account_currency - debit_in_account_currency"
 	else:
 		dr_or_cr = "credit_in_account_currency - debit_in_account_currency"
-		payment_dr_or_cr = "payment_gl_entry.debit_in_account_currency - payment_gl_entry.credit_in_account_currency"
+		payment_dr_or_cr = "debit_in_account_currency - credit_in_account_currency"
 
 	invoice = 'Sales Invoice' if erpnext.get_party_account_type(party_type) == 'Receivable' else 'Purchase Invoice'
 	held_invoices = get_held_invoices(party_type, party)
@@ -632,21 +632,9 @@
 
 	invoice_list = frappe.db.sql("""
 		select
-			voucher_no, voucher_type, posting_date, ifnull(sum({dr_or_cr}), 0) as invoice_amount,
-			(
-				select ifnull(sum({payment_dr_or_cr}), 0)
-				from `tabGL Entry` payment_gl_entry
-				where payment_gl_entry.against_voucher_type = invoice_gl_entry.voucher_type
-					and if(invoice_gl_entry.voucher_type='Journal Entry',
-						payment_gl_entry.against_voucher = invoice_gl_entry.voucher_no,
-						payment_gl_entry.against_voucher = invoice_gl_entry.against_voucher)
-					and payment_gl_entry.party_type = invoice_gl_entry.party_type
-					and payment_gl_entry.party = invoice_gl_entry.party
-					and payment_gl_entry.account = invoice_gl_entry.account
-					and {payment_dr_or_cr} > 0
-			) as payment_amount
+			voucher_no, voucher_type, posting_date, ifnull(sum({dr_or_cr}), 0) as invoice_amount
 		from
-			`tabGL Entry` invoice_gl_entry
+			`tabGL Entry`
 		where
 			party_type = %(party_type)s and party = %(party)s
 			and account = %(account)s and {dr_or_cr} > 0
@@ -655,11 +643,9 @@
 					and (against_voucher = '' or against_voucher is null))
 				or (voucher_type not in ('Journal Entry', 'Payment Entry')))
 		group by voucher_type, voucher_no
-		having (invoice_amount - payment_amount) > 0.005
 		order by posting_date, name {limit_cond}""".format(
 			dr_or_cr=dr_or_cr,
 			invoice = invoice,
-			payment_dr_or_cr=payment_dr_or_cr,
 			condition=condition or "",
 			limit_cond = limit_cond
 		), {
@@ -668,25 +654,46 @@
 			"account": account,
 		}, as_dict=True)
 
-	for d in invoice_list:
-		if not d.voucher_type == "Purchase Invoice" or d.voucher_no not in held_invoices:
-			due_date = frappe.db.get_value(
-				d.voucher_type, d.voucher_no, "posting_date" if party_type == "Employee" else "due_date")
+	payment_entries = frappe.db.sql("""
+		select against_voucher_type, against_voucher,
+			ifnull(sum({payment_dr_or_cr}), 0) as payment_amount
+		from `tabGL Entry`
+		where party_type = %(party_type)s and party = %(party)s
+			and account = %(account)s
+			and {payment_dr_or_cr} > 0
+			and against_voucher is not null and against_voucher != ''
+		group by against_voucher_type, against_voucher
+	""".format(payment_dr_or_cr=payment_dr_or_cr), {
+		"party_type": party_type,
+		"party": party,
+		"account": account,
+	}, as_dict=True)
 
-			outstanding_invoices.append(
-				frappe._dict({
-					'voucher_no': d.voucher_no,
-					'voucher_type': d.voucher_type,
-					'posting_date': d.posting_date,
-					'invoice_amount': flt(d.invoice_amount),
-					'payment_amount': flt(d.payment_amount),
-					'outstanding_amount': flt(d.invoice_amount - d.payment_amount, precision),
-					'due_date': due_date
-				})
-			)
+	pe_map = frappe._dict()
+	for d in payment_entries:
+		pe_map.setdefault((d.against_voucher_type, d.against_voucher), d.payment_amount)
+
+	for d in invoice_list:
+		payment_amount = pe_map.get((d.voucher_type, d.voucher_no), 0)
+		outstanding_amount = flt(d.invoice_amount - payment_amount, precision)
+		if outstanding_amount > 0.5 / (10**precision):
+			if not d.voucher_type == "Purchase Invoice" or d.voucher_no not in held_invoices:
+				due_date = frappe.db.get_value(
+					d.voucher_type, d.voucher_no, "posting_date" if party_type == "Employee" else "due_date")
+
+				outstanding_invoices.append(
+					frappe._dict({
+						'voucher_no': d.voucher_no,
+						'voucher_type': d.voucher_type,
+						'posting_date': d.posting_date,
+						'invoice_amount': flt(d.invoice_amount),
+						'payment_amount': payment_amount,
+						'outstanding_amount': outstanding_amount,
+						'due_date': due_date
+					})
+				)
 
 	outstanding_invoices = sorted(outstanding_invoices, key=lambda k: k['due_date'] or getdate(nowdate()))
-
 	return outstanding_invoices
 
 
@@ -859,5 +866,3 @@
 	def generator():
 		return cint(frappe.db.get_value('Accounts Settings', None, 'allow_cost_center_in_entry_of_bs_account'))
 	return frappe.local_cache("get_allow_cost_center_in_entry_of_bs_account", (), generator, regenerate_if_none=True)
-
-
diff --git a/erpnext/manufacturing/doctype/job_card/job_card.py b/erpnext/manufacturing/doctype/job_card/job_card.py
index 5343a28..5ed03be 100644
--- a/erpnext/manufacturing/doctype/job_card/job_card.py
+++ b/erpnext/manufacturing/doctype/job_card/job_card.py
@@ -57,7 +57,7 @@
 					.format(d.idx, d.item_code))
 
 			if self.get('operation') == d.operation:
-				child = self.append('items', {
+				self.append('items', {
 					'item_code': d.item_code,
 					'source_warehouse': d.source_warehouse,
 					'uom': frappe.db.get_value("Item", d.item_code, 'stock_uom'),
@@ -108,6 +108,10 @@
 		if not self.items:
 			self.transferred_qty = self.for_quantity if self.docstatus == 1 else 0
 
+		doc = frappe.get_doc('Work Order', self.get('work_order'))
+		if doc.transfer_material_against == 'Work Order' or doc.skip_transfer:
+			return
+
 		if self.items:
 			self.transferred_qty = frappe.db.get_value('Stock Entry', {
 				'job_card': self.name,
diff --git a/erpnext/templates/pages/non_profit/join-chapter.html b/erpnext/templates/pages/non_profit/join-chapter.html
index 029cd77..89a7d2a 100644
--- a/erpnext/templates/pages/non_profit/join-chapter.html
+++ b/erpnext/templates/pages/non_profit/join-chapter.html
@@ -15,7 +15,7 @@
 		{{ chapter_button() }}
 		<p><a href="">Leave Chapter</a></p>
 	{% else %}
-		{% if frappe.local.request.method=='POST' %}
+		{% if request.method=='POST' %}
 			<p>Welcome to chapter {{ chapter.name }}!</p>
 			{{ chapter_button() }}
 		{% else %}