brotherton-erpnext/erpnext/patches/v12_0/generate_leave_ledger_entries.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.2 KiB
Python
Raw Normal View History

2019-05-26 20:17:16 +05:30
# Copyright (c) 2018, Frappe and Contributors
# License: GNU General Public License v3. See license.txt
2019-05-26 20:17:16 +05:30
import frappe
2019-08-20 13:02:40 +05:30
from frappe.utils import getdate, today
2019-05-26 20:17:16 +05:30
2019-05-26 20:17:16 +05:30
def execute():
"""Generates leave ledger entries for leave allocation/application/encashment
for last allocation"""
frappe.reload_doc("HR", "doctype", "Leave Ledger Entry")
frappe.reload_doc("HR", "doctype", "Leave Encashment")
2019-10-04 16:24:53 +05:30
frappe.reload_doc("HR", "doctype", "Leave Type")
2019-05-26 20:17:16 +05:30
if not frappe.get_meta("Leave Allocation").has_field("unused_leaves"):
frappe.reload_doc("HR", "doctype", "Leave Allocation")
update_leave_allocation_fieldname()
generate_allocation_ledger_entries()
generate_application_leave_ledger_entries()
generate_encashment_leave_ledger_entries()
generate_expiry_allocation_ledger_entries()
2019-05-26 20:17:16 +05:30
2022-03-28 18:52:46 +05:30
def update_leave_allocation_fieldname():
"""maps data from old field to the new field"""
frappe.db.sql(
"""
UPDATE `tabLeave Allocation`
SET `unused_leaves` = `carry_forwarded_leaves`
"""
)
2022-03-28 18:52:46 +05:30
def generate_allocation_ledger_entries():
"""fix ledger entries for missing leave allocation transaction"""
allocation_list = get_allocation_records()
2019-05-26 20:17:16 +05:30
for allocation in allocation_list:
if not frappe.db.exists(
"Leave Ledger Entry",
{"transaction_type": "Leave Allocation", "transaction_name": allocation.name},
):
2020-09-07 11:53:50 +05:30
allocation_obj = frappe.get_doc("Leave Allocation", allocation)
allocation_obj.create_leave_ledger_entry()
2019-05-26 20:17:16 +05:30
2022-03-28 18:52:46 +05:30
def generate_application_leave_ledger_entries():
"""fix ledger entries for missing leave application transaction"""
leave_applications = get_leaves_application_records()
2019-05-26 20:17:16 +05:30
for application in leave_applications:
if not frappe.db.exists(
"Leave Ledger Entry",
{"transaction_type": "Leave Application", "transaction_name": application.name},
):
2020-09-07 11:53:50 +05:30
frappe.get_doc("Leave Application", application.name).create_leave_ledger_entry()
2019-05-26 20:17:16 +05:30
2022-03-28 18:52:46 +05:30
def generate_encashment_leave_ledger_entries():
"""fix ledger entries for missing leave encashment transaction"""
leave_encashments = get_leave_encashment_records()
for encashment in leave_encashments:
if not frappe.db.exists(
"Leave Ledger Entry",
{"transaction_type": "Leave Encashment", "transaction_name": encashment.name},
):
frappe.get_doc("Leave Encashment", encashment).create_leave_ledger_entry()
2022-03-28 18:52:46 +05:30
def generate_expiry_allocation_ledger_entries():
"""fix ledger entries for missing leave allocation transaction"""
2019-07-31 19:31:26 +05:30
from erpnext.hr.doctype.leave_ledger_entry.leave_ledger_entry import expire_allocation
2022-03-28 18:52:46 +05:30
allocation_list = get_allocation_records()
2019-05-26 20:17:16 +05:30
for allocation in allocation_list:
if not frappe.db.exists(
"Leave Ledger Entry",
{"transaction_type": "Leave Allocation", "transaction_name": allocation.name, "is_expired": 1},
):
2020-09-07 11:53:50 +05:30
allocation_obj = frappe.get_doc("Leave Allocation", allocation)
2019-08-20 13:02:40 +05:30
if allocation_obj.to_date <= getdate(today()):
expire_allocation(allocation_obj)
2019-05-26 20:17:16 +05:30
2022-03-28 18:52:46 +05:30
2019-05-26 20:17:16 +05:30
def get_allocation_records():
2020-09-07 11:53:50 +05:30
return frappe.get_all(
"Leave Allocation", filters={"docstatus": 1}, fields=["name"], order_by="to_date ASC"
)
2022-03-28 18:52:46 +05:30
2019-05-26 20:17:16 +05:30
def get_leaves_application_records():
2020-09-07 11:53:50 +05:30
return frappe.get_all("Leave Application", filters={"docstatus": 1}, fields=["name"])
2022-03-28 18:52:46 +05:30
2019-05-26 20:17:16 +05:30
def get_leave_encashment_records():
2020-09-07 11:53:50 +05:30
return frappe.get_all("Leave Encashment", filters={"docstatus": 1}, fields=["name"])