Skip to content

Commit

Permalink
fix: employee advance return through multiple additional salaries (#2…
Browse files Browse the repository at this point in the history
…7438)

* fix: employee advance return through multiple additional salaries

* test: test repay unclaimed amount from salary

* fix: sorting in imports
  • Loading branch information
ruchamahabal committed Sep 14, 2021
1 parent c7ceb37 commit b98740b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion erpnext/hr/doctype/employee_advance/employee_advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ frappe.ui.form.on('Employee Advance', {
frm.trigger('make_return_entry');
}, __('Create'));
} else if (frm.doc.repay_unclaimed_amount_from_salary == 1 && frappe.model.can_create("Additional Salary")) {
frm.add_custom_button(__("Deduction from salary"), function() {
frm.add_custom_button(__("Deduction from Salary"), function() {
frm.events.make_deduction_via_additional_salary(frm);
}, __('Create'));
}
Expand Down
5 changes: 3 additions & 2 deletions erpnext/hr/doctype/employee_advance/employee_advance.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
"default": "0",
"fieldname": "repay_unclaimed_amount_from_salary",
"fieldtype": "Check",
"label": "Repay unclaimed amount from salary"
"label": "Repay Unclaimed Amount from Salary"
},
{
"depends_on": "eval:cur_frm.doc.employee",
Expand Down Expand Up @@ -200,10 +200,11 @@
],
"is_submittable": 1,
"links": [],
"modified": "2021-03-31 22:31:53.746659",
"modified": "2021-09-11 18:38:38.617478",
"modified_by": "Administrator",
"module": "HR",
"name": "Employee Advance",
"naming_rule": "By \"Naming Series\" field",
"owner": "Administrator",
"permissions": [
{
Expand Down
5 changes: 4 additions & 1 deletion erpnext/hr/doctype/employee_advance/employee_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ def get_paying_amount_paying_exchange_rate(payment_account, doc):
@frappe.whitelist()
def create_return_through_additional_salary(doc):
import json
doc = frappe._dict(json.loads(doc))

if isinstance(doc, str):
doc = frappe._dict(json.loads(doc))

additional_salary = frappe.new_doc('Additional Salary')
additional_salary.employee = doc.employee
additional_salary.currency = doc.currency
Expand Down
49 changes: 48 additions & 1 deletion erpnext/hr/doctype/employee_advance/test_employee_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
from erpnext.hr.doctype.employee.test_employee import make_employee
from erpnext.hr.doctype.employee_advance.employee_advance import (
EmployeeAdvanceOverPayment,
create_return_through_additional_salary,
make_bank_entry,
)
from erpnext.payroll.doctype.salary_component.test_salary_component import create_salary_component
from erpnext.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure


class TestEmployeeAdvance(unittest.TestCase):
Expand All @@ -33,6 +36,46 @@ def test_paid_amount_and_status(self):
journal_entry1 = make_payment_entry(advance)
self.assertRaises(EmployeeAdvanceOverPayment, journal_entry1.submit)

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})

args = {"type": "Deduction"}
create_salary_component("Advance Salary - Deduction", **args)
make_salary_structure("Test Additional Salary for Advance Return", "Monthly", employee=employee_name)

# additional salary for 700 first
advance.reload()
additional_salary = create_return_through_additional_salary(advance)
additional_salary.salary_component = "Advance Salary - Deduction"
additional_salary.payroll_date = nowdate()
additional_salary.amount = 700
additional_salary.insert()
additional_salary.submit()

advance.reload()
self.assertEqual(advance.return_amount, 700)

# additional salary for remaining 300
additional_salary = create_return_through_additional_salary(advance)
additional_salary.salary_component = "Advance Salary - Deduction"
additional_salary.payroll_date = nowdate()
additional_salary.amount = 300
additional_salary.insert()
additional_salary.submit()

advance.reload()
self.assertEqual(advance.return_amount, 1000)

# update advance return amount on additional salary cancellation
additional_salary.cancel()
advance.reload()
self.assertEqual(advance.return_amount, 700)

def tearDown(self):
frappe.db.rollback()


def make_payment_entry(advance):
journal_entry = frappe.get_doc(make_bank_entry("Employee Advance", advance.name))
journal_entry.cheque_no = "123123"
Expand All @@ -41,7 +84,7 @@ def make_payment_entry(advance):

return journal_entry

def make_employee_advance(employee_name):
def make_employee_advance(employee_name, args=None):
doc = frappe.new_doc("Employee Advance")
doc.employee = employee_name
doc.company = "_Test company"
Expand All @@ -51,6 +94,10 @@ def make_employee_advance(employee_name):
doc.advance_amount = 1000
doc.posting_date = nowdate()
doc.advance_account = "_Test Employee Advance - _TC"

if args:
doc.update(args)

doc.insert()
doc.submit()

Expand Down
16 changes: 13 additions & 3 deletions erpnext/payroll/doctype/additional_salary/additional_salary.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@

class AdditionalSalary(Document):
def on_submit(self):
if self.ref_doctype == "Employee Advance" and self.ref_docname:
frappe.db.set_value("Employee Advance", self.ref_docname, "return_amount", self.amount)

self.update_return_amount_in_employee_advance()
self.update_employee_referral()

def on_cancel(self):
self.update_return_amount_in_employee_advance()
self.update_employee_referral(cancel=True)

def validate(self):
Expand Down Expand Up @@ -98,6 +97,17 @@ def validate_employee_referral(self):
frappe.throw(_("Additional Salary for referral bonus can only be created against Employee Referral with status {0}").format(
frappe.bold("Accepted")))

def update_return_amount_in_employee_advance(self):
if self.ref_doctype == "Employee Advance" and self.ref_docname:
return_amount = frappe.db.get_value("Employee Advance", self.ref_docname, "return_amount")

if self.docstatus == 2:
return_amount -= self.amount
else:
return_amount += self.amount

frappe.db.set_value("Employee Advance", self.ref_docname, "return_amount", return_amount)

def update_employee_referral(self, cancel=False):
if self.ref_doctype == "Employee Referral":
status = "Unpaid" if cancel else "Paid"
Expand Down

0 comments on commit b98740b

Please sign in to comment.