fix: generate ledger entries for all leave transactions
This commit is contained in:
parent
4e1b60d401
commit
87adaed933
@ -3,84 +3,70 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe
|
import frappe
|
||||||
|
from frappe.utils import getdate
|
||||||
|
|
||||||
def execute():
|
def execute():
|
||||||
""" Generates leave ledger entries for leave allocation/application/encashment
|
""" Generates leave ledger entries for leave allocation/application/encashment
|
||||||
for last allocation """
|
for last allocation """
|
||||||
frappe.reload_doc("HR","doctype", "Leave Ledger Entry")
|
frappe.reload_doc("HR", "doctype", "Leave Ledger Entry")
|
||||||
frappe.reload_doc("HR","doctype", "Leave Encashment")
|
frappe.reload_doc("HR", "doctype", "Leave Encashment")
|
||||||
if frappe.db.a_row_exists("Leave Ledger Entry"):
|
if frappe.db.a_row_exists("Leave Ledger Entry"):
|
||||||
return
|
return
|
||||||
|
|
||||||
allocation_list = get_allocation_records()
|
generate_allocation_ledger_entries()
|
||||||
generate_allocation_ledger_entries(allocation_list)
|
generate_application_leave_ledger_entries()
|
||||||
generate_application_leave_ledger_entries(allocation_list)
|
generate_encashment_leave_ledger_entries()
|
||||||
generate_encashment_leave_ledger_entries(allocation_list)
|
|
||||||
|
|
||||||
def generate_allocation_ledger_entries(allocation_list):
|
def generate_allocation_ledger_entries():
|
||||||
''' fix ledger entries for missing leave allocation transaction '''
|
''' fix ledger entries for missing leave allocation transaction '''
|
||||||
from erpnext.hr.doctype.leave_allocation.leave_allocation import LeaveAllocation
|
allocation_list = get_allocation_records()
|
||||||
|
|
||||||
for allocation in allocation_list:
|
for allocation in allocation_list:
|
||||||
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Allocation', 'transaction_name': allocation.name}):
|
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Allocation', 'transaction_name': allocation.name}):
|
||||||
allocation.update(dict(doctype="Leave Allocation"))
|
allocation.update(dict(doctype="Leave Allocation"))
|
||||||
leave_allocation = LeaveAllocation(allocation)
|
allocation_obj = frappe.get_doc(allocation)
|
||||||
leave_allocation.create_leave_ledger_entry()
|
allocation_obj.create_leave_ledger_entry()
|
||||||
|
if allocation.to_date <= getdate():
|
||||||
|
allocation_obj.expire_allocation()
|
||||||
|
|
||||||
def generate_application_leave_ledger_entries(allocation_list):
|
|
||||||
|
def generate_application_leave_ledger_entries():
|
||||||
''' fix ledger entries for missing leave application transaction '''
|
''' fix ledger entries for missing leave application transaction '''
|
||||||
from erpnext.hr.doctype.leave_application.leave_application import LeaveApplication
|
leave_applications = get_leaves_application_records()
|
||||||
|
|
||||||
leave_applications = get_leaves_application_records(allocation_list)
|
for application in leave_applications:
|
||||||
|
|
||||||
for record in leave_applications:
|
|
||||||
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Application', 'transaction_name': record.name}):
|
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Application', 'transaction_name': record.name}):
|
||||||
record.update(dict(doctype="Leave Application"))
|
application.update(dict(doctype="Leave Application"))
|
||||||
leave_application = LeaveApplication(record)
|
frappe.get_doc(application).create_leave_ledger_entry()
|
||||||
leave_application.create_leave_ledger_entry()
|
|
||||||
|
|
||||||
def generate_encashment_leave_ledger_entries(allocation_list):
|
def generate_encashment_leave_ledger_entries():
|
||||||
''' fix ledger entries for missing leave encashment transaction '''
|
''' fix ledger entries for missing leave encashment transaction '''
|
||||||
from erpnext.hr.doctype.leave_encashment.leave_encashment import LeaveEncashment
|
leave_encashments = get_leave_encashment_records()
|
||||||
|
|
||||||
leave_encashments = get_leave_encashment_records(allocation_list)
|
for encashment in leave_encashments:
|
||||||
|
|
||||||
for record in leave_encashments:
|
|
||||||
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Encashment', 'transaction_name': record.name}):
|
if not frappe.db.exists("Leave Ledger Entry", {'transaction_type': 'Leave Encashment', 'transaction_name': record.name}):
|
||||||
record.update(dict(doctype="Leave Encashment"))
|
encashment.update(dict(doctype="Leave Encashment"))
|
||||||
leave_encashment = LeaveEncashment(record)
|
frappe.get_doc(encashment).create_leave_ledger_entry()
|
||||||
leave_encashment.create_leave_ledger_entry()
|
|
||||||
|
|
||||||
def get_allocation_records():
|
def get_allocation_records():
|
||||||
return frappe.db.sql("""
|
return frappe.db.sql("""
|
||||||
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`
|
|
||||||
)
|
|
||||||
SELECT
|
SELECT
|
||||||
*
|
DISTINCT name,
|
||||||
FROM
|
employee,
|
||||||
`allocation_values`
|
leave_type,
|
||||||
|
new_leaves_allocated,
|
||||||
|
carry_forwarded_leaves,
|
||||||
|
from_date,
|
||||||
|
to_date,
|
||||||
|
carry_forward
|
||||||
|
FROM `tabLeave Allocation`
|
||||||
WHERE
|
WHERE
|
||||||
allocation=1
|
docstatus=1
|
||||||
|
ORDER BY to_date ASC
|
||||||
""", as_dict=1)
|
""", as_dict=1)
|
||||||
|
|
||||||
def get_leaves_application_records(allocation_list):
|
def get_leaves_application_records():
|
||||||
leave_applications = []
|
return frappe.db.sql("""
|
||||||
for allocation in allocation_list:
|
|
||||||
leave_applications += frappe.db.sql("""
|
|
||||||
SELECT
|
SELECT
|
||||||
DISTINCT name,
|
DISTINCT name,
|
||||||
employee,
|
employee,
|
||||||
@ -90,16 +76,11 @@ def get_leaves_application_records(allocation_list):
|
|||||||
to_date
|
to_date
|
||||||
FROM `tabLeave Application`
|
FROM `tabLeave Application`
|
||||||
WHERE
|
WHERE
|
||||||
from_date >= %s
|
docstatus=1
|
||||||
AND leave_type = %s
|
""", as_dict=1)
|
||||||
AND employee = %s
|
|
||||||
""", (allocation.from_date, allocation.leave_type, allocation.employee), as_dict=1)
|
|
||||||
return leave_applications
|
|
||||||
|
|
||||||
def get_leave_encashment_records(allocation_list):
|
def get_leave_encashment_records():
|
||||||
leave_encashments = []
|
return frappe.db.sql("""
|
||||||
for allocation in allocation_list:
|
|
||||||
leave_encashments += frappe.db.sql("""
|
|
||||||
SELECT
|
SELECT
|
||||||
DISTINCT name,
|
DISTINCT name,
|
||||||
employee,
|
employee,
|
||||||
@ -108,8 +89,5 @@ def get_leave_encashment_records(allocation_list):
|
|||||||
encashment_date
|
encashment_date
|
||||||
FROM `tabLeave Encashment`
|
FROM `tabLeave Encashment`
|
||||||
WHERE
|
WHERE
|
||||||
leave_type = %s
|
AND docstatus=1
|
||||||
AND employee = %s
|
""", as_dict=1)
|
||||||
AND encashment_date >= %s
|
|
||||||
""", (allocation.leave_type, allocation.employee, allocation.from_date), as_dict=1)
|
|
||||||
return leave_encashments
|
|
||||||
Loading…
x
Reference in New Issue
Block a user