[fix] Payment Days and holidays considering joining and relieving dates

This commit is contained in:
Nabin Hait 2015-12-03 10:44:21 +05:30
parent 2e1c8f27c4
commit c4d9554a05

View File

@ -20,19 +20,20 @@ class SalarySlip(TransactionBase):
def get_emp_and_leave_details(self): def get_emp_and_leave_details(self):
if self.employee: if self.employee:
self.get_leave_details() joining_date, relieving_date = frappe.db.get_value("Employee", self.employee,
struct = self.check_sal_struct() ["date_of_joining", "relieving_date"])
self.get_leave_details(joining_date, relieving_date)
struct = self.check_sal_struct(joining_date, relieving_date)
if struct: if struct:
self.set("earnings", []) self.set("earnings", [])
self.set("deduction", []) self.set("deduction", [])
self.pull_sal_struct(struct) self.pull_sal_struct(struct)
def check_sal_struct(self): def check_sal_struct(self, joining_date, relieving_date):
m = get_month_details(self.fiscal_year, self.month) m = get_month_details(self.fiscal_year, self.month)
joining_date, relieving_date = frappe.db.get_value("Employee", self.employee,
["date_of_joining", "relieving_date"])
struct = frappe.db.sql("""select name from `tabSalary Structure` struct = frappe.db.sql("""select name from `tabSalary Structure`
where employee=%s and is_active = 'Yes' where employee=%s and is_active = 'Yes'
and (from_date <= %s or from_date <= %s) and (from_date <= %s or from_date <= %s)
@ -51,20 +52,23 @@ class SalarySlip(TransactionBase):
make_salary_slip(struct, self) make_salary_slip(struct, self)
def pull_emp_details(self): def pull_emp_details(self):
emp = frappe.db.get_value("Employee", self.employee, emp = frappe.db.get_value("Employee", self.employee, ["bank_name", "bank_ac_no"], as_dict=1)
["bank_name", "bank_ac_no"], as_dict=1)
if emp: if emp:
self.bank_name = emp.bank_name self.bank_name = emp.bank_name
self.bank_account_no = emp.bank_ac_no self.bank_account_no = emp.bank_ac_no
def get_leave_details(self, lwp=None): def get_leave_details(self, joining_date=None, relieving_date=None, lwp=None):
if not self.fiscal_year: if not self.fiscal_year:
self.fiscal_year = frappe.db.get_default("fiscal_year") self.fiscal_year = frappe.db.get_default("fiscal_year")
if not self.month: if not self.month:
self.month = "%02d" % getdate(nowdate()).month self.month = "%02d" % getdate(nowdate()).month
if not joining_date:
joining_date, relieving_date = frappe.db.get_value("Employee", self.employee,
["date_of_joining", "relieving_date"])
m = get_month_details(self.fiscal_year, self.month) m = get_month_details(self.fiscal_year, self.month)
holidays = self.get_holidays_for_employee(m) holidays = self.get_holidays_for_employee(m, joining_date, relieving_date)
if not cint(frappe.db.get_value("HR Settings", "HR Settings", if not cint(frappe.db.get_value("HR Settings", "HR Settings",
"include_holidays_in_total_working_days")): "include_holidays_in_total_working_days")):
@ -76,44 +80,48 @@ class SalarySlip(TransactionBase):
lwp = self.calculate_lwp(holidays, m) lwp = self.calculate_lwp(holidays, m)
self.total_days_in_month = m['month_days'] self.total_days_in_month = m['month_days']
self.leave_without_pay = lwp self.leave_without_pay = lwp
payment_days = flt(self.get_payment_days(m)) - flt(lwp) payment_days = flt(self.get_payment_days(m, joining_date, relieving_date)) - flt(lwp)
self.payment_days = payment_days > 0 and payment_days or 0 self.payment_days = payment_days > 0 and payment_days or 0
def get_payment_days(self, m, joining_date, relieving_date):
def get_payment_days(self, m):
payment_days = m['month_days'] payment_days = m['month_days']
emp = frappe.db.sql("select date_of_joining, relieving_date from `tabEmployee` \ if relieving_date:
where name = %s", self.employee, as_dict=1)[0] if getdate(relieving_date) > m['month_start_date'] \
and getdate(relieving_date) < m['month_end_date']:
payment_days = getdate(relieving_date).day
elif getdate(relieving_date) < m['month_start_date']:
frappe.throw(_("Employee relieved on {0} must be set as 'Left'").format(relieving_date))
if emp['relieving_date']: if joining_date:
if getdate(emp['relieving_date']) > m['month_start_date'] and \ if getdate(joining_date) > m['month_start_date'] and \
getdate(emp['relieving_date']) < m['month_end_date']: getdate(joining_date) < m['month_end_date']:
payment_days = getdate(emp['relieving_date']).day payment_days = payment_days - getdate(joining_date).day + 1
elif getdate(emp['relieving_date']) < m['month_start_date']: elif getdate(joining_date) > m['month_end_date']:
frappe.throw(_("Employee relieved on {0} must be set as 'Left'").format(emp["relieving_date"]))
if emp['date_of_joining']:
if getdate(emp['date_of_joining']) > m['month_start_date'] and \
getdate(emp['date_of_joining']) < m['month_end_date']:
payment_days = payment_days - getdate(emp['date_of_joining']).day + 1
elif getdate(emp['date_of_joining']) > m['month_end_date']:
payment_days = 0 payment_days = 0
return payment_days return payment_days
def get_holidays_for_employee(self, m): def get_holidays_for_employee(self, month, joining_date, relieving_date):
start_date = month['month_start_date'] \
if joining_date < month['month_start_date'] else joining_date
end_date = relieving_date \
if (relieving_date and relieving_date < month['month_end_date']) else month['month_end_date']
holidays = frappe.db.sql("""select t1.holiday_date holidays = frappe.db.sql("""select t1.holiday_date
from `tabHoliday` t1, tabEmployee t2 from `tabHoliday` t1, tabEmployee t2
where t1.parent = t2.holiday_list and t2.name = %s where t1.parent = t2.holiday_list and t2.name = %s
and t1.holiday_date between %s and %s""", and t1.holiday_date between %s and %s""",
(self.employee, m['month_start_date'], m['month_end_date'])) (self.employee, start_date, end_date))
if not holidays: if not holidays:
holidays = frappe.db.sql("""select t1.holiday_date holidays = frappe.db.sql("""select t1.holiday_date
from `tabHoliday` t1, `tabHoliday List` t2 from `tabHoliday` t1, `tabHoliday List` t2
where t1.parent = t2.name and t2.is_default = 1 where t1.parent = t2.name and t2.is_default = 1
and t2.fiscal_year = %s and t2.fiscal_year = %s
and t1.holiday_date between %s and %s""", (self.fiscal_year, and t1.holiday_date between %s and %s""",
m['month_start_date'], m['month_end_date'])) (self.fiscal_year, start_date, end_date))
holidays = [cstr(i[0]) for i in holidays] holidays = [cstr(i[0]) for i in holidays]
return holidays return holidays
@ -148,9 +156,8 @@ class SalarySlip(TransactionBase):
from frappe.utils import money_in_words from frappe.utils import money_in_words
self.check_existing() self.check_existing()
if not (len(self.get("earnings")) or if not (len(self.get("earnings")) or len(self.get("deductions"))):
len(self.get("deductions"))): self.get_emp_and_leave_details()
self.get_emp_and_leave_details()
else: else:
self.get_leave_details(self.leave_without_pay) self.get_leave_details(self.leave_without_pay)