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

115 lines
4.4 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
from __future__ import unicode_literals
import frappe
def execute():
""" Generates leave ledger entries for leave allocation/application/encashment
for last allocation """
frappe.reload_doc("HR","doctype", "Leave Ledger Entry")
2019-05-30 22:22:15 +05:30
frappe.reload_doc("HR","doctype", "Leave Encashment")
if frappe.db.a_row_exists("Leave Ledger Entry"):
2019-05-26 20:17:16 +05:30
return
allocation_list = get_allocation_records()
generate_allocation_ledger_entries(allocation_list)
generate_application_leave_ledger_entries(allocation_list)
generate_encashment_leave_ledger_entries(allocation_list)
def generate_allocation_ledger_entries(allocation_list):
''' fix ledger entries for missing leave allocation transaction '''
from erpnext.hr.doctype.leave_allocation.leave_allocation import LeaveAllocation
for allocation in allocation_list:
2019-05-29 15:54:14 +05:30
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Allocation', 'transaction_name': allocation.name}):
2019-05-29 19:12:19 +05:30
allocation.update(dict(doctype="Leave Allocation"))
2019-05-29 15:54:14 +05:30
leave_allocation = LeaveAllocation(allocation)
leave_allocation.create_leave_ledger_entry()
2019-05-26 20:17:16 +05:30
def generate_application_leave_ledger_entries(allocation_list):
''' fix ledger entries for missing leave application transaction '''
from erpnext.hr.doctype.leave_application.leave_application import LeaveApplication
leave_applications = get_leaves_application_records(allocation_list)
for record in leave_applications:
2019-05-29 15:54:14 +05:30
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Application', 'transaction_name': record.name}):
2019-05-29 19:12:19 +05:30
record.update(dict(doctype="Leave Application"))
2019-05-29 15:54:14 +05:30
leave_application = LeaveApplication(record)
leave_application.create_leave_ledger_entry()
2019-05-26 20:17:16 +05:30
def generate_encashment_leave_ledger_entries(allocation_list):
''' fix ledger entries for missing leave encashment transaction '''
from erpnext.hr.doctype.leave_encashment.leave_encashment import LeaveEncashment
leave_encashments = get_leave_encashment_records(allocation_list)
for record in leave_encashments:
2019-05-29 15:54:14 +05:30
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Encashment', 'transaction_name': record.name}):
2019-05-29 19:12:19 +05:30
record.update(dict(doctype="Leave Encashment"))
2019-05-29 15:54:14 +05:30
leave_encashment = LeaveEncashment(record)
leave_encashment.create_leave_ledger_entry()
2019-05-26 20:17:16 +05:30
def get_allocation_records():
return frappe.db.sql("""
2019-05-28 13:24:15 +05:30
WITH allocation_values AS (
SELECT
DISTINCT name,
employee,
leave_type,
new_leaves_allocated,
carry_forwarded_leaves,
from_date,
to_date,
carry_forward,
RANK() OVER(
PARTITION BY employee, leave_type
ORDER BY to_date DESC
) as allocation
FROM `tabLeave Allocation`
)
2019-05-26 20:17:16 +05:30
SELECT
2019-05-28 13:24:15 +05:30
*
FROM
`allocation_values`
2019-05-26 20:17:16 +05:30
WHERE
allocation=1
""", as_dict=1)
def get_leaves_application_records(allocation_list):
leave_applications = []
for allocation in allocation_list:
2019-05-28 13:24:15 +05:30
leave_applications += frappe.db.sql("""
2019-05-26 20:17:16 +05:30
SELECT
DISTINCT name,
2019-05-26 20:17:16 +05:30
employee,
leave_type,
total_leave_days,
from_date,
to_date
FROM `tabLeave Application`
WHERE
from_date >= %s
2019-05-28 13:24:15 +05:30
AND leave_type = %s
AND employee = %s
2019-05-29 19:12:19 +05:30
""", (allocation.from_date, allocation.leave_type, allocation.employee), as_dict=1)
2019-05-26 20:17:16 +05:30
return leave_applications
def get_leave_encashment_records(allocation_list):
leave_encashments = []
for allocation in allocation_list:
2019-05-28 13:24:15 +05:30
leave_encashments += frappe.db.sql("""
2019-05-26 20:17:16 +05:30
SELECT
DISTINCT name,
employee,
leave_type,
encashable_days,
encashment_date
2019-05-26 20:17:16 +05:30
FROM `tabLeave Encashment`
WHERE
leave_type = %s
2019-05-28 13:24:15 +05:30
AND employee = %s
AND encashment_date >= %s
2019-05-29 19:12:19 +05:30
""", (allocation.leave_type, allocation.employee, allocation.from_date), as_dict=1)
2019-05-26 20:17:16 +05:30
return leave_encashments