2018-04-14 11:34:56 +00:00
|
|
|
# Copyright (c) 2017, Frappe and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-05-21 13:08:18 +00:00
|
|
|
from datetime import datetime
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2019-02-01 06:55:40 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import getdate
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-06-19 13:47:57 +00:00
|
|
|
from erpnext.payroll.doctype.salary_structure_assignment.salary_structure_assignment import (
|
|
|
|
DuplicateAssignment,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
|
|
|
|
2018-04-14 11:34:56 +00:00
|
|
|
|
|
|
|
def execute():
|
2020-12-01 03:41:05 +00:00
|
|
|
frappe.reload_doc('Payroll', 'doctype', 'Salary Structure')
|
|
|
|
frappe.reload_doc("Payroll", "doctype", "Salary Structure Assignment")
|
2018-07-03 07:36:31 +00:00
|
|
|
frappe.db.sql("""
|
|
|
|
delete from `tabSalary Structure Assignment`
|
|
|
|
where salary_structure in (select name from `tabSalary Structure` where is_active='No' or docstatus!=1)
|
|
|
|
""")
|
2018-07-30 05:22:35 +00:00
|
|
|
if frappe.db.table_exists('Salary Structure Employee'):
|
|
|
|
ss_details = frappe.db.sql("""
|
|
|
|
select sse.employee, sse.employee_name, sse.from_date, sse.to_date,
|
|
|
|
sse.base, sse.variable, sse.parent as salary_structure, ss.company
|
|
|
|
from `tabSalary Structure Employee` sse, `tabSalary Structure` ss
|
|
|
|
where ss.name = sse.parent AND ss.is_active='Yes'
|
|
|
|
AND sse.employee in (select name from `tabEmployee` where ifNull(status, '') != 'Left')""", as_dict=1)
|
|
|
|
else:
|
2018-07-31 12:03:03 +00:00
|
|
|
cols = ""
|
|
|
|
if "base" in frappe.db.get_table_columns("Salary Structure"):
|
|
|
|
cols = ", base, variable"
|
|
|
|
|
2018-07-30 05:22:35 +00:00
|
|
|
ss_details = frappe.db.sql("""
|
2018-07-31 12:03:03 +00:00
|
|
|
select name as salary_structure, employee, employee_name, from_date, to_date, company {0}
|
2018-07-30 05:22:35 +00:00
|
|
|
from `tabSalary Structure`
|
|
|
|
where is_active='Yes'
|
2018-07-31 12:03:03 +00:00
|
|
|
AND employee in (select name from `tabEmployee` where ifNull(status, '') != 'Left')
|
|
|
|
""".format(cols), as_dict=1)
|
2019-02-01 06:55:40 +00:00
|
|
|
|
2020-12-01 03:41:05 +00:00
|
|
|
all_companies = frappe.db.get_all("Company", fields=["name", "default_currency"])
|
|
|
|
for d in all_companies:
|
|
|
|
company = d.name
|
|
|
|
company_currency = d.default_currency
|
|
|
|
|
|
|
|
frappe.db.sql("""update `tabSalary Structure` set currency = %s where company=%s""", (company_currency, company))
|
|
|
|
|
2018-07-30 05:22:35 +00:00
|
|
|
for d in ss_details:
|
2018-05-21 13:22:07 +00:00
|
|
|
try:
|
2019-02-01 06:55:40 +00:00
|
|
|
joining_date, relieving_date = frappe.db.get_value("Employee", d.employee,
|
|
|
|
["date_of_joining", "relieving_date"])
|
|
|
|
from_date = d.from_date
|
|
|
|
if joining_date and getdate(from_date) < joining_date:
|
|
|
|
from_date = joining_date
|
|
|
|
elif relieving_date and getdate(from_date) > relieving_date:
|
|
|
|
continue
|
2020-12-01 03:41:05 +00:00
|
|
|
company_currency = frappe.db.get_value('Company', d.company, 'default_currency')
|
2019-02-01 06:55:40 +00:00
|
|
|
|
2018-05-21 13:22:07 +00:00
|
|
|
s = frappe.new_doc("Salary Structure Assignment")
|
|
|
|
s.employee = d.employee
|
|
|
|
s.employee_name = d.employee_name
|
2018-07-30 05:22:35 +00:00
|
|
|
s.salary_structure = d.salary_structure
|
2019-02-01 06:55:40 +00:00
|
|
|
s.from_date = from_date
|
2018-05-21 13:22:07 +00:00
|
|
|
s.to_date = d.to_date if isinstance(d.to_date, datetime) else None
|
2018-07-31 12:03:03 +00:00
|
|
|
s.base = d.get("base")
|
|
|
|
s.variable = d.get("variable")
|
2018-05-21 13:22:07 +00:00
|
|
|
s.company = d.company
|
2020-12-01 03:41:05 +00:00
|
|
|
s.currency = company_currency
|
2018-05-18 10:16:35 +00:00
|
|
|
|
2018-05-21 13:22:07 +00:00
|
|
|
# to migrate the data of the old employees
|
|
|
|
s.flags.old_employee = True
|
|
|
|
s.save()
|
2018-05-29 07:55:28 +00:00
|
|
|
s.submit()
|
2018-05-21 13:22:07 +00:00
|
|
|
except DuplicateAssignment:
|
|
|
|
pass
|
2018-04-14 11:34:56 +00:00
|
|
|
|
|
|
|
frappe.db.sql("update `tabSalary Structure` set docstatus=1")
|