Skip to content

Commit

Permalink
feat: link bank entry in salary withholdings
Browse files Browse the repository at this point in the history
(cherry picked from commit a489dae)
  • Loading branch information
ruchamahabal authored and mergify[bot] committed Jul 23, 2024
1 parent 4a3f912 commit 387bcd7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 37 deletions.
26 changes: 4 additions & 22 deletions hrms/payroll/doctype/payroll_entry/payroll_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ frappe.ui.form.on("Payroll Entry", {
}).addClass("btn-primary");
} else if (!r.message.has_bank_entries_for_withheld_salaries) {
frm.add_custom_button(__("Release Withheld Salaries"), function () {
frm.trigger("make_bank_entry_for_withheld_salaries");
make_bank_entry(frm, (for_withheld_salaries = 1));
}).addClass("btn-primary");
}
});
Expand Down Expand Up @@ -388,25 +388,6 @@ frappe.ui.form.on("Payroll Entry", {
}
},

make_bank_entry_for_withheld_salaries: function (frm) {
frappe
.call({
method: "run_doc_method",
args: {
method: "make_bank_entry_for_withheld_salaries",
dt: "Payroll Entry",
dn: frm.doc.name,
},
freeze: true,
freeze_message: __("Creating Bank Entries") + "...",
})
.then(() => {
frappe.set_route("List", "Journal Entry", {
"Journal Entry Account.reference_name": frm.doc.name,
});
});
},

clear_employee_table: function (frm) {
frm.clear_table("employees");
frm.refresh();
Expand Down Expand Up @@ -437,15 +418,16 @@ const submit_salary_slip = function (frm) {
);
};

let make_bank_entry = function (frm) {
var doc = frm.doc;
let make_bank_entry = function (frm, for_withheld_salaries = 0) {
const doc = frm.doc;
if (doc.payment_account) {
return frappe.call({
method: "run_doc_method",
args: {
method: "make_bank_entry",
dt: "Payroll Entry",
dn: frm.doc.name,
args: { for_withheld_salaries: for_withheld_salaries },
},
callback: function () {
frappe.set_route("List", "Journal Entry", {
Expand Down
29 changes: 19 additions & 10 deletions hrms/payroll/doctype/payroll_entry/payroll_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
)
from erpnext.accounts.utils import get_fiscal_year

from hrms.payroll.doctype.salary_withholding.salary_withholding import link_bank_entry_in_salary_withholdings


class PayrollEntry(Document):
def onload(self):
Expand Down Expand Up @@ -598,7 +600,7 @@ def make_journal_entry(
user_remark="",
submitted_salary_slips: list | None = None,
submit_journal_entry=False,
):
) -> str:
multi_currency = 0
if len(currencies) > 1:
multi_currency = 1
Expand All @@ -622,7 +624,7 @@ def make_journal_entry(
journal_entry.submit()

if submitted_salary_slips:
self.update_salary_slip_status(submitted_salary_slips, jv_name=journal_entry.name)
self.set_journal_entry_in_salary_slips(submitted_salary_slips, jv_name=journal_entry.name)

except Exception as e:
if type(e) in (str, list, tuple):
Expand All @@ -631,6 +633,8 @@ def make_journal_entry(
self.log_error("Journal Entry creation against Salary Slip failed")
raise

return journal_entry

def get_payable_amount_for_earnings_and_deductions(
self,
accounts,
Expand Down Expand Up @@ -915,11 +919,11 @@ def make_bank_entry(self, for_withheld_salaries=False):
salary_slip_total -= salary_detail.amount

if salary_slip_total > 0:
self.set_accounting_entries_for_bank_entry(salary_slip_total, "salary")
remark = "withheld salaries" if for_withheld_salaries else "salaries"
bank_entry = self.set_accounting_entries_for_bank_entry(salary_slip_total, remark)

@frappe.whitelist()
def make_bank_entry_for_withheld_salaries(self):
self.make_bank_entry(for_withheld_salaries=True)
if for_withheld_salaries:
link_bank_entry_in_salary_withholdings(salary_slips, bank_entry.name)

def get_salary_slip_details(self, for_withheld_salaries=False):
SalarySlip = frappe.qb.DocType("Salary Slip")
Expand All @@ -933,6 +937,7 @@ def get_salary_slip_details(self, for_withheld_salaries=False):
SalarySlip.name,
SalarySlip.employee,
SalarySlip.salary_structure,
SalarySlip.salary_withholding_cycle,
SalaryDetail.salary_component,
SalaryDetail.amount,
SalaryDetail.parentfield,
Expand Down Expand Up @@ -1024,16 +1029,16 @@ def set_accounting_entries_for_bank_entry(self, je_payment_amount, user_remark):
)
)

self.make_journal_entry(
return self.make_journal_entry(
accounts,
currencies,
voucher_type="Bank Entry",
user_remark=_("Payment of {0} from {1} to {2}").format(
user_remark, self.start_date, self.end_date
_(user_remark), self.start_date, self.end_date
),
)

def update_salary_slip_status(self, submitted_salary_slips, jv_name=None):
def set_journal_entry_in_salary_slips(self, submitted_salary_slips, jv_name=None):
SalarySlip = frappe.qb.DocType("Salary Slip")
(
frappe.qb.update(SalarySlip)
Expand Down Expand Up @@ -1598,7 +1603,11 @@ def get_salary_withholdings(
frappe.qb.from_(Withholding)
.join(WithholdingCycle)
.on(WithholdingCycle.parent == Withholding.name)
.select(Withholding.employee, Withholding.name)
.select(
Withholding.employee,
Withholding.name.as_("salary_withholding"),
WithholdingCycle.name.as_("salary_withholding_cycle"),
)
.where(
(WithholdingCycle.from_date == start_date)
& (WithholdingCycle.to_date == end_date)
Expand Down
7 changes: 4 additions & 3 deletions hrms/payroll/doctype/salary_slip/salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ def validate(self):
)

def check_salary_withholding(self):
salary_withholding = get_salary_withholdings(self.start_date, self.end_date, self.employee)
if salary_withholding:
self.salary_withholding = salary_withholding[0].name
withholding = get_salary_withholdings(self.start_date, self.end_date, self.employee)
if withholding:
self.salary_withholding = withholding[0].salary_withholding
self.salary_withholding_cycle = withholding[0].salary_withholding_cycle
else:
self.salary_withholding = None

Expand Down
11 changes: 11 additions & 0 deletions hrms/payroll/doctype/salary_withholding/salary_withholding.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,14 @@ def get_payroll_frequency(employee: str, posting_date: str) -> str | None:
)

return frappe.db.get_value("Salary Structure", salary_structure, "payroll_frequency")


def link_bank_entry_in_salary_withholdings(salary_slips: list[dict], bank_entry: str):
WithholdingCycle = frappe.qb.DocType("Salary Withholding Cycle")
(
frappe.qb.update(WithholdingCycle)
.set(WithholdingCycle.journal_entry, bank_entry)
.where(
WithholdingCycle.name.isin([salary_slip.salary_withholding_cycle for salary_slip in salary_slips])
)
).run()
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"field_order": [
"from_date",
"to_date",
"is_salary_released"
"is_salary_released",
"journal_entry"
],
"fields": [
{
Expand Down Expand Up @@ -36,12 +37,20 @@
"label": "Is Salary Released",
"no_copy": 1,
"read_only": 1
},
{
"fieldname": "journal_entry",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Journal Entry",
"options": "Journal Entry",
"read_only": 1
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-07-01 21:49:15.389192",
"modified": "2024-07-18 12:43:39.315699",
"modified_by": "Administrator",
"module": "Payroll",
"name": "Salary Withholding Cycle",
Expand Down
8 changes: 8 additions & 0 deletions hrms/public/js/erpnext/journal_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
// For license information, please see license.txt

frappe.ui.form.on("Journal Entry", {
setup(frm) {
frm.ignore_doctypes_on_cancel_all.push("Salary Withholding");
if (frm.doc.voucher_type === "Bank Entry") {
// since salary withholding is linked to salary slip, nested links are also pulled for cancellation
frm.ignore_doctypes_on_cancel_all.push("Salary Slip");
}
},

refresh(frm) {
frm.set_query("reference_name", "accounts", function (frm, cdt, cdn) {
let jvd = frappe.get_doc(cdt, cdn);
Expand Down

0 comments on commit 387bcd7

Please sign in to comment.