brotherton-erpnext/erpnext/patches/v7_0/rename_salary_components.py

150 lines
4.8 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2016-07-02 06:53:59 +00:00
import frappe
from frappe.model.utils.rename_field import update_property_setters
def execute():
if not frappe.db.exists("DocType", "Salary Structure Earning"):
return
frappe.reload_doc("Payroll", "doctype", "salary_detail")
frappe.reload_doc("Payroll", "doctype", "salary_component")
2016-07-02 06:53:59 +00:00
standard_cols = ["name", "creation", "modified", "owner", "modified_by", "parent", "parenttype", "parentfield", "idx"]
2016-07-02 06:53:59 +00:00
dt_cols = {
"Salary Structure Deduction": ["d_type", "d_modified_amt", "depend_on_lwp"],
"Salary Structure Earning": ["e_type", "modified_value", "depend_on_lwp"],
"Salary Slip Earning": ["e_type", "e_modified_amount", "e_depends_on_lwp", "e_amount"],
"Salary Slip Deduction": ["d_type", "d_modified_amount", "d_depends_on_lwp", "d_amount"],
}
2016-07-02 06:53:59 +00:00
earning_type_exists = True if "earning_type" in frappe.db.get_table_columns("Salary Slip Earning") else False
e_type_exists = True if "e_type" in frappe.db.get_table_columns("Salary Slip Earning") else False
2016-07-02 06:53:59 +00:00
if e_type_exists and earning_type_exists:
frappe.db.sql("""update `tabSalary Slip Earning`
2016-07-02 06:53:59 +00:00
set e_type = earning_type, e_modified_amount = earning_amount
where e_type is null and earning_type is not null""")
frappe.db.sql("""update `tabSalary Structure Earning` set e_type = earning_type
2016-07-02 06:53:59 +00:00
where e_type is null and earning_type is not null""")
frappe.db.sql("""update `tabSalary Slip Deduction` set
2016-07-02 06:53:59 +00:00
d_type = deduction_type, d_modified_amount = deduction_amount
where d_type is null and deduction_type is not null""")
frappe.db.sql("""update `tabSalary Structure Deduction` set d_type = deduction_type
2016-07-02 06:53:59 +00:00
where d_type is null and deduction_type is not null""")
2016-07-02 06:53:59 +00:00
if earning_type_exists and not e_type_exists:
for val in dt_cols.values():
if val[0] == "e_type":
val[0] = "earning_type"
2016-07-02 06:53:59 +00:00
if val[0] == "d_type":
val[0] = "deduction_type"
2016-07-02 06:53:59 +00:00
if val[1] == "e_modified_amount":
val[1] ="earning_amount"
2016-07-02 06:53:59 +00:00
if val[1] == "d_modified_amount":
val[1] ="deduction_amount"
target_cols = standard_cols + ["salary_component", "amount", "depends_on_payment_days", "default_amount"]
target_cols = "`" + "`, `".join(target_cols) + "`"
for doctype, cols in dt_cols.items():
2016-07-02 06:53:59 +00:00
source_cols = "`" + "`, `".join(standard_cols + cols) + "`"
if len(cols) == 3:
source_cols += ", 0"
2016-07-02 06:53:59 +00:00
frappe.db.sql("""INSERT INTO `tabSalary Detail` ({0}) SELECT {1} FROM `tab{2}`"""
.format(target_cols, source_cols, doctype))
2016-07-02 06:53:59 +00:00
dt_cols_de = {
"Deduction Type": ["deduction_name", "description"],
"Earning Type": ["earning_name", "description"],
}
2016-07-06 07:41:51 +00:00
standard_cols_de = standard_cols
2016-07-02 06:53:59 +00:00
target_cols = standard_cols_de + ["salary_component", "description"]
target_cols = "`" + "`, `".join(target_cols) + "`"
for doctype, cols in dt_cols_de.items():
2016-07-02 06:53:59 +00:00
source_cols = "`" + "`, `".join(standard_cols_de + cols) + "`"
2016-10-24 12:17:02 +00:00
try:
frappe.db.sql("""INSERT INTO `tabSalary Component` ({0}) SELECT {1} FROM `tab{2}`"""
.format(target_cols, source_cols, doctype))
except Exception as e:
2016-10-24 12:17:02 +00:00
if e.args[0]==1062:
pass
2016-07-02 06:53:59 +00:00
update_customizations()
for doctype in ["Salary Structure Deduction", "Salary Structure Earning", "Salary Slip Earning",
2016-07-02 06:53:59 +00:00
"Salary Slip Deduction", "Deduction Type", "Earning Type"] :
frappe.delete_doc("DocType", doctype)
def update_customizations():
dt_cols = {
"Salary Structure Deduction": {
"d_type": "salary_component",
"deduction_type": "salary_component",
2016-07-02 06:53:59 +00:00
"d_modified_amt": "amount",
"depend_on_lwp": "depends_on_payment_days"
2016-07-02 06:53:59 +00:00
},
"Salary Structure Earning": {
"e_type": "salary_component",
"earning_type": "salary_component",
2016-07-02 06:53:59 +00:00
"modified_value": "amount",
"depend_on_lwp": "depends_on_payment_days"
2016-07-02 06:53:59 +00:00
},
"Salary Slip Earning": {
"e_type": "salary_component",
"earning_type": "salary_component",
2016-07-02 06:53:59 +00:00
"e_modified_amount": "amount",
2016-07-06 07:03:54 +00:00
"e_amount" : "default_amount",
"e_depends_on_lwp": "depends_on_payment_days"
2016-07-02 06:53:59 +00:00
},
"Salary Slip Deduction": {
"d_type": "salary_component",
"deduction_type": "salary_component",
2016-07-02 06:53:59 +00:00
"d_modified_amount": "amount",
2016-07-06 07:03:54 +00:00
"d_amount" : "default_amount",
"d_depends_on_lwp": "depends_on_payment_days"
2016-07-02 06:53:59 +00:00
}
}
2016-07-02 06:53:59 +00:00
update_property_setters_and_custom_fields("Salary Detail", dt_cols)
2016-07-02 06:53:59 +00:00
dt_cols = {
"Earning Type": {
"earning_name": "salary_component"
},
"Deduction Type": {
"deduction_name": "salary_component"
}
}
2016-07-02 06:53:59 +00:00
update_property_setters_and_custom_fields("Salary Component", dt_cols)
2016-07-02 06:53:59 +00:00
def update_property_setters_and_custom_fields(new_dt, dt_cols):
for doctype, cols in dt_cols.items():
frappe.db.sql("update `tabProperty Setter` set doc_type = %s where doc_type=%s", (new_dt, doctype))
frappe.db.sql("update `tabCustom Field` set dt = %s where dt=%s", (new_dt, doctype))
2016-07-02 06:53:59 +00:00
for old_fieldname, new_fieldname in cols.items():
2016-07-06 07:41:51 +00:00
update_property_setters(new_dt, old_fieldname, new_fieldname)