Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: payment entry against employee advance #635

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions hrms/hr/doctype/employee_advance/test_employee_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest

import frappe
from frappe.tests.utils import FrappeTestCase
from frappe.utils import flt, nowdate

import erpnext
Expand All @@ -24,15 +25,15 @@
from hrms.payroll.doctype.salary_structure.test_salary_structure import make_salary_structure


class TestEmployeeAdvance(unittest.TestCase):
class TestEmployeeAdvance(FrappeTestCase):
def setUp(self):
frappe.db.delete("Employee Advance")

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

journal_entry = make_payment_entry(advance)
journal_entry = make_journal_entry_for_advance(advance)
journal_entry.submit()

advance.reload()
Expand All @@ -41,22 +42,22 @@ def test_paid_amount_and_status(self):
self.assertEqual(advance.status, "Paid")

# try making over payment
journal_entry1 = make_payment_entry(advance)
journal_entry1 = make_journal_entry_for_advance(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()
journal_entry = make_journal_entry_for_advance(advance)
journal_entry.submit()

advance.reload()

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

pe.cancel()
journal_entry.cancel()
advance.reload()

self.assertEqual(advance.paid_amount, 0)
Expand All @@ -74,8 +75,8 @@ def test_claimed_status(self):
)

advance = make_employee_advance(claim.employee)
pe = make_payment_entry(advance)
pe.submit()
journal_entry = make_journal_entry_for_advance(advance)
journal_entry.submit()

claim = get_advances_for_claim(claim, advance.name)
claim.save()
Expand Down Expand Up @@ -103,8 +104,8 @@ def test_partly_claimed_and_returned_status(self):
)

advance = make_employee_advance(claim.employee)
pe = make_payment_entry(advance)
pe.submit()
journal_entry = make_journal_entry_for_advance(advance)
journal_entry.submit()

# PARTLY CLAIMED AND RETURNED status check
# 500 Claimed, 500 Returned
Expand All @@ -113,8 +114,8 @@ def test_partly_claimed_and_returned_status(self):
)

advance = make_employee_advance(claim.employee)
pe = make_payment_entry(advance)
pe.submit()
journal_entry = make_journal_entry_for_advance(advance)
journal_entry.submit()

claim = get_advances_for_claim(claim, advance.name, amount=500)
claim.save()
Expand Down Expand Up @@ -162,8 +163,8 @@ def test_partly_claimed_and_returned_status(self):
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})
pe = make_payment_entry(advance)
pe.submit()
journal_entry = make_journal_entry_for_advance(advance)
journal_entry.submit()

args = {"type": "Deduction"}
create_salary_component("Advance Salary - Deduction", **args)
Expand Down Expand Up @@ -201,11 +202,27 @@ def test_repay_unclaimed_amount_from_salary(self):
self.assertEqual(advance.return_amount, 700)
self.assertEqual(advance.status, "Paid")

def tearDown(self):
frappe.db.rollback()
def test_payment_entry_against_advance(self):
employee_name = make_employee("_T@employee.advance")
advance = make_employee_advance(employee_name)

pe = make_payment_entry(advance, 700)
advance.reload()
self.assertEqual(advance.status, "Unpaid")
self.assertEqual(advance.paid_amount, 700)

pe = make_payment_entry(advance, 300)
advance.reload()
self.assertEqual(advance.status, "Paid")
self.assertEqual(advance.paid_amount, 1000)

pe.cancel()
advance.reload()
self.assertEqual(advance.status, "Unpaid")
self.assertEqual(advance.paid_amount, 700)


def make_payment_entry(advance):
def make_journal_entry_for_advance(advance):
journal_entry = frappe.get_doc(make_bank_entry("Employee Advance", advance.name))
journal_entry.cheque_no = "123123"
journal_entry.cheque_date = nowdate()
Expand All @@ -214,6 +231,18 @@ def make_payment_entry(advance):
return journal_entry


def make_payment_entry(advance, amount):
from hrms.overrides.employee_payment_entry import get_payment_entry_for_employee

payment_entry = get_payment_entry_for_employee(advance.doctype, advance.name)
payment_entry.reference_no = "1"
payment_entry.reference_date = nowdate()
payment_entry.references[0].allocated_amount = amount
payment_entry.submit()

return payment_entry


def make_employee_advance(employee_name, args=None):
doc = frappe.new_doc("Employee Advance")
doc.employee = employee_name
Expand Down
52 changes: 16 additions & 36 deletions hrms/hr/doctype/expense_claim/test_expense_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_expense_claim_status_as_payment_from_payment_entry(self):
payable_account, 300, 200, company_name, "Travel Expenses - _TC3"
)

pe = _make_payment_entry(expense_claim)
pe = make_payment_entry(expense_claim, 200)

expense_claim.load_from_db()
self.assertEqual(expense_claim.status, "Paid")
Expand Down Expand Up @@ -175,7 +175,7 @@ def test_expense_claim_against_fully_paid_advances(self):
from hrms.hr.doctype.employee_advance.test_employee_advance import (
get_advances_for_claim,
make_employee_advance,
make_payment_entry,
make_journal_entry_for_advance,
)

frappe.db.delete("Employee Advance")
Expand All @@ -186,7 +186,7 @@ def test_expense_claim_against_fully_paid_advances(self):
)

advance = make_employee_advance(claim.employee)
pe = make_payment_entry(advance)
pe = make_journal_entry_for_advance(advance)
pe.submit()

# claim for already paid out advances
Expand All @@ -201,7 +201,7 @@ def test_advance_amount_allocation_against_claim_with_taxes(self):
from hrms.hr.doctype.employee_advance.test_employee_advance import (
get_advances_for_claim,
make_employee_advance,
make_payment_entry,
make_journal_entry_for_advance,
)

frappe.db.delete("Employee Advance")
Expand All @@ -220,7 +220,7 @@ def test_advance_amount_allocation_against_claim_with_taxes(self):
claim.save()

advance = make_employee_advance(claim.employee)
pe = make_payment_entry(advance)
pe = make_journal_entry_for_advance(advance)
pe.submit()

# claim for already paid out advances
Expand All @@ -235,9 +235,7 @@ def test_expense_claim_partially_paid_via_advance(self):
from hrms.hr.doctype.employee_advance.test_employee_advance import (
get_advances_for_claim,
make_employee_advance,
)
from hrms.hr.doctype.employee_advance.test_employee_advance import (
make_payment_entry as make_advance_payment,
make_journal_entry_for_advance,
)

frappe.db.delete("Employee Advance")
Expand All @@ -249,7 +247,7 @@ def test_expense_claim_partially_paid_via_advance(self):

# link advance for partial amount
advance = make_employee_advance(claim.employee, {"advance_amount": 500})
pe = make_advance_payment(advance)
pe = make_journal_entry_for_advance(advance)
pe.submit()

claim = get_advances_for_claim(claim, advance.name)
Expand All @@ -260,7 +258,7 @@ def test_expense_claim_partially_paid_via_advance(self):
self.assertEqual(claim.status, "Unpaid")

# reimburse remaning amount
make_payment_entry(claim, payable_account, 500)
make_payment_entry(claim, 500)
claim.reload()

self.assertEqual(claim.total_amount_reimbursed, 500)
Expand Down Expand Up @@ -371,23 +369,23 @@ def test_multiple_payment_entries_against_expense(self):
expense_claim.submit()

# Payment entry 1: paying 500
make_payment_entry(expense_claim, payable_account, 500)
make_payment_entry(expense_claim, 500)
outstanding_amount, total_amount_reimbursed = get_outstanding_and_total_reimbursed_amounts(
expense_claim
)
self.assertEqual(outstanding_amount, 5000)
self.assertEqual(total_amount_reimbursed, 500)

# Payment entry 1: paying 2000
make_payment_entry(expense_claim, payable_account, 2000)
make_payment_entry(expense_claim, 2000)
outstanding_amount, total_amount_reimbursed = get_outstanding_and_total_reimbursed_amounts(
expense_claim
)
self.assertEqual(outstanding_amount, 3000)
self.assertEqual(total_amount_reimbursed, 2500)

# Payment entry 1: paying 3000
make_payment_entry(expense_claim, payable_account, 3000)
make_payment_entry(expense_claim, 3000)
outstanding_amount, total_amount_reimbursed = get_outstanding_and_total_reimbursed_amounts(
expense_claim
)
Expand Down Expand Up @@ -559,20 +557,19 @@ def get_outstanding_and_total_reimbursed_amounts(expense_claim):
return outstanding_amount, total_amount_reimbursed


def make_payment_entry(expense_claim, payable_account, amt):
def make_payment_entry(expense_claim, amount):
from hrms.overrides.employee_payment_entry import get_payment_entry_for_employee

pe = get_payment_entry_for_employee(
"Expense Claim", expense_claim.name, bank_account="_Test Bank USD - _TC", bank_amount=amt
)
pe = get_payment_entry_for_employee("Expense Claim", expense_claim.name)
pe.reference_no = "1"
pe.reference_date = nowdate()
pe.source_exchange_rate = 1
pe.paid_to = payable_account
pe.references[0].allocated_amount = amt
pe.references[0].allocated_amount = amount
pe.insert()
pe.submit()

return pe


def make_journal_entry(expense_claim, do_not_submit=False):
je_dict = make_bank_entry("Expense Claim", expense_claim.name)
Expand All @@ -587,23 +584,6 @@ def make_journal_entry(expense_claim, do_not_submit=False):
return je


def _make_payment_entry(expense_claim):
outstanding_amount, total_amount_reimbursed = get_outstanding_and_total_reimbursed_amounts(
expense_claim
)
pe = get_payment_entry(
"Expense Claim", expense_claim.name, party_type="Employee", party_amount=outstanding_amount
)
pe.reference_no = "Conrad Oct 2022"
pe.reference_date = nowdate()
pe.paid_amount = expense_claim.total_sanctioned_amount
pe.received_amount = expense_claim.total_sanctioned_amount
pe.insert()
pe.submit()

return pe


def create_payment_reconciliation(company, employee, payable_account):
pr = frappe.new_doc("Payment Reconciliation")
pr.company = company
Expand Down
Loading