Skip to content

Commit

Permalink
fix: Employee Advance paid amount not updated on PE cancellation (#28572
Browse files Browse the repository at this point in the history
)

* fix: employee advance paid amount not updated on PE cancellation

* fix: convert raw sql queries to qb

* test: Employee Advance Paid Amount on PE cancellation

* chore: disable no copy for sanctioned amount in Expense Claim
  • Loading branch information
ruchamahabal authored Nov 29, 2021
1 parent c7701ac commit baf41fd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
47 changes: 29 additions & 18 deletions erpnext/hr/doctype/employee_advance/employee_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.query_builder.functions import Sum
from frappe.utils import flt, nowdate

import erpnext
Expand Down Expand Up @@ -41,24 +42,34 @@ def set_status(self):
self.status = "Cancelled"

def set_total_advance_paid(self):
paid_amount = frappe.db.sql("""
select ifnull(sum(debit), 0) as paid_amount
from `tabGL Entry`
where against_voucher_type = 'Employee Advance'
and against_voucher = %s
and party_type = 'Employee'
and party = %s
""", (self.name, self.employee), as_dict=1)[0].paid_amount

return_amount = frappe.db.sql("""
select ifnull(sum(credit), 0) as return_amount
from `tabGL Entry`
where against_voucher_type = 'Employee Advance'
and voucher_type != 'Expense Claim'
and against_voucher = %s
and party_type = 'Employee'
and party = %s
""", (self.name, self.employee), as_dict=1)[0].return_amount
gle = frappe.qb.DocType("GL Entry")

paid_amount = (
frappe.qb.from_(gle)
.select(Sum(gle.debit).as_("paid_amount"))
.where(
(gle.against_voucher_type == 'Employee Advance')
& (gle.against_voucher == self.name)
& (gle.party_type == 'Employee')
& (gle.party == self.employee)
& (gle.docstatus == 1)
& (gle.is_cancelled == 0)
)
).run(as_dict=True)[0].paid_amount or 0

return_amount = (
frappe.qb.from_(gle)
.select(Sum(gle.credit).as_("return_amount"))
.where(
(gle.against_voucher_type == 'Employee Advance')
& (gle.voucher_type != 'Expense Claim')
& (gle.against_voucher == self.name)
& (gle.party_type == 'Employee')
& (gle.party == self.employee)
& (gle.docstatus == 1)
& (gle.is_cancelled == 0)
)
).run(as_dict=True)[0].return_amount or 0

if paid_amount != 0:
paid_amount = flt(paid_amount) / flt(self.exchange_rate)
Expand Down
18 changes: 18 additions & 0 deletions erpnext/hr/doctype/employee_advance/test_employee_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ def test_paid_amount_and_status(self):
journal_entry1 = make_payment_entry(advance)
self.assertRaises(EmployeeAdvanceOverPayment, journal_entry1.submit)

def test_paid_amount_on_pe_cancellation(self):
employee_name = make_employee("_T@employe.advance")
advance = make_employee_advance(employee_name)

pe = make_payment_entry(advance)
pe.submit()

advance.reload()

self.assertEqual(advance.paid_amount, 1000)
self.assertEqual(advance.status, "Paid")

pe.cancel()
advance.reload()

self.assertEqual(advance.paid_amount, 0)
self.assertEqual(advance.status, "Unpaid")

def test_repay_unclaimed_amount_from_salary(self):
employee_name = make_employee("_T@employe.advance")
advance = make_employee_advance(employee_name, {"repay_unclaimed_amount_from_salary": 1})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
"fieldtype": "Currency",
"in_list_view": 1,
"label": "Sanctioned Amount",
"no_copy": 1,
"oldfieldname": "sanctioned_amount",
"oldfieldtype": "Currency",
"options": "Company:company:default_currency",
Expand All @@ -120,7 +119,7 @@
"idx": 1,
"istable": 1,
"links": [],
"modified": "2020-09-18 17:26:09.703215",
"modified": "2021-11-26 14:23:45.539922",
"modified_by": "Administrator",
"module": "HR",
"name": "Expense Claim Detail",
Expand Down

0 comments on commit baf41fd

Please sign in to comment.