fix: Expense Claim conditions for Paid status
- status doesn't change to Paid if the entire required amount is already covered via linked advances
- status doesn't change to Paid if the claim is partially paid via advances
- patch to update such uncancelled claims to Paid
diff --git a/erpnext/hr/doctype/expense_claim/expense_claim.py b/erpnext/hr/doctype/expense_claim/expense_claim.py
index fe04efb..9bb31b3 100644
--- a/erpnext/hr/doctype/expense_claim/expense_claim.py
+++ b/erpnext/hr/doctype/expense_claim/expense_claim.py
@@ -42,10 +42,16 @@
"2": "Cancelled"
}[cstr(self.docstatus or 0)]
- paid_amount = flt(self.total_amount_reimbursed) + flt(self.total_advance_amount)
precision = self.precision("grand_total")
- if (self.is_paid or (flt(self.total_sanctioned_amount) > 0 and self.docstatus == 1
- and flt(self.grand_total, precision) == flt(paid_amount, precision))) and self.approval_status == 'Approved':
+
+ if (
+ # set as paid
+ self.is_paid
+ # grand total is reimbursed
+ or (flt(self.total_sanctioned_amount) > 0 and self.docstatus == 1 and flt(self.grand_total, precision) == flt(self.total_amount_reimbursed, precision))
+ # grand total (to be paid) is 0 since linked advances already cover the claimed amount
+ or (flt(self.grand_total, precision) == 0)
+ ) and self.approval_status == "Approved":
status = "Paid"
elif flt(self.total_sanctioned_amount) > 0 and self.docstatus == 1 and self.approval_status == 'Approved':
status = "Unpaid"
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index dc1d692..028834a 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -362,3 +362,4 @@
erpnext.patches.v13_0.add_cost_center_in_loans
erpnext.patches.v13_0.set_return_against_in_pos_invoice_references
erpnext.patches.v13_0.remove_unknown_links_to_prod_plan_items # 24-03-2022
+erpnext.patches.v13_0.update_expense_claim_status_for_paid_advances
diff --git a/erpnext/patches/v13_0/update_expense_claim_status_for_paid_advances.py b/erpnext/patches/v13_0/update_expense_claim_status_for_paid_advances.py
new file mode 100644
index 0000000..206d032
--- /dev/null
+++ b/erpnext/patches/v13_0/update_expense_claim_status_for_paid_advances.py
@@ -0,0 +1,21 @@
+import frappe
+
+
+def execute():
+ """
+ Update Expense Claim status to Paid if:
+ - the entire required amount is already covered via linked advances
+ - the claim is partially paid via advances and the rest is reimbursed
+ """
+
+ ExpenseClaim = frappe.qb.DocType('Expense Claim')
+
+ (frappe.qb
+ .update(ExpenseClaim)
+ .set(ExpenseClaim.status, 'Paid')
+ .where(
+ ((ExpenseClaim.grand_total == 0) | (ExpenseClaim.grand_total == ExpenseClaim.total_amount_reimbursed))
+ & (ExpenseClaim.approval_status == 'Approved')
+ & (ExpenseClaim.docstatus != 2)
+ )
+ ).run()