From d0b0c87f4034261d3431e83eb0fdab24b50e2289 Mon Sep 17 00:00:00 2001 From: Jamsheer Date: Fri, 11 May 2018 11:33:07 +0530 Subject: [PATCH] get additional salary component in salary slip --- .../additional_salary_component.py | 80 +++++++++++++++---- erpnext/hr/doctype/salary_slip/salary_slip.py | 7 ++ 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/erpnext/hr/doctype/additional_salary_component/additional_salary_component.py b/erpnext/hr/doctype/additional_salary_component/additional_salary_component.py index 0591082b4a..216dea0403 100644 --- a/erpnext/hr/doctype/additional_salary_component/additional_salary_component.py +++ b/erpnext/hr/doctype/additional_salary_component/additional_salary_component.py @@ -5,22 +5,72 @@ from __future__ import unicode_literals import frappe from frappe.model.document import Document -from frappe.utils import formatdate, format_datetime, getdate, get_datetime, nowdate -from erpnext.hr.utils import validate_dates +from frappe import _ +from frappe.utils import getdate, date_diff class AdditionalSalaryComponent(Document): def validate(self): - # self.validate_dates() - validate_dates(self, self.from_date, self.to_date) + self.validate_dates() + if self.amount <= 0: + frappe.throw(_("Amount should be greater than zero.")) - # def validate_dates(self): - # date_of_joining, relieving_date = frappe.db.get_value("Employee", self.employee, - # ["date_of_joining", "relieving_date"]) - # if getdate(self.from_date) > getdate(to_date): - # frappe.throw(_("To date can not be less than from date")) - # elif getdate(self.from_date) > getdate(nowdate()): - # frappe.throw(_("Future dates not allowed")) - # elif date_of_joining and getdate(self.from_date) < getdate(date_of_joining): - # frappe.throw(_("From date can not be less than employee's joining date")) - # elif relieving_date and getdate(to_date) > getdate(relieving_date): - # frappe.throw(_("To date can not greater than employee's relieving date")) + def validate_dates(self): + date_of_joining, relieving_date = frappe.db.get_value("Employee", self.employee, + ["date_of_joining", "relieving_date"]) + if getdate(self.from_date) > getdate(self.to_date): + frappe.throw(_("To date can not be less than from date")) + elif date_of_joining and getdate(self.from_date) < getdate(date_of_joining): + frappe.throw(_("From date can not be less than employee's joining date")) + elif relieving_date and getdate(self.to_date) > getdate(relieving_date): + frappe.throw(_("To date can not greater than employee's relieving date")) + + def get_amount(self, sal_start_date, sal_end_date): + start_date = getdate(sal_start_date) + end_date = getdate(sal_end_date) + total_days = date_diff(getdate(self.to_date), getdate(self.from_date)) + 1 + amount_per_day = self.amount / total_days + if getdate(sal_start_date) <= getdate(self.from_date): + start_date = getdate(self.from_date) + if getdate(sal_end_date) > getdate(self.to_date): + end_date = getdate(self.end_date) + no_of_days = date_diff(getdate(end_date), getdate(start_date)) + return amount_per_day * no_of_days + + + + +@frappe.whitelist() +def get_additional_salary_component(employee, start_date, end_date): + additional_components = frappe.db.sql(""" + select name from `tabAdditional Salary Component` + where employee=%(employee)s + and docstatus = 1 + and ( + (%(from_date)s between from_date and to_date) + or (%(to_date)s between from_date and to_date) + or (from_date between %(from_date)s and %(to_date)s) + )""", { + 'employee': employee, + 'from_date': start_date, + 'to_date': end_date + }) + + if additional_components: + additional_components_array = [] + for additional_component in additional_components: + struct_row = {} + additional_components_dict = {} + additional_component_obj = frappe.get_doc("Additional Salary Component", additional_component[0]) + amount = additional_component_obj.get_amount(start_date, end_date) + salary_component = frappe.get_doc("Salary Component", additional_component_obj.salary_component) + struct_row['depends_on_lwp'] = salary_component.depends_on_lwp + struct_row['salary_component'] = salary_component.name + struct_row['abbr'] = salary_component.salary_component_abbr + struct_row['do_not_include_in_total'] = salary_component.do_not_include_in_total + additional_components_dict['amount'] = amount + additional_components_dict['struct_row'] = struct_row + additional_components_array.append(additional_components_dict) + + if len(additional_components_array) > 0: + return additional_components_array + return False diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.py b/erpnext/hr/doctype/salary_slip/salary_slip.py index 99de580a3f..984a78c75f 100644 --- a/erpnext/hr/doctype/salary_slip/salary_slip.py +++ b/erpnext/hr/doctype/salary_slip/salary_slip.py @@ -12,6 +12,7 @@ from erpnext.hr.doctype.payroll_entry.payroll_entry import get_start_end_dates from erpnext.hr.doctype.employee.employee import get_holiday_list_for_employee from erpnext.utilities.transaction_base import TransactionBase from frappe.utils.background_jobs import enqueue +from erpnext.hr.doctype.additional_salary_component.additional_salary_component import get_additional_salary_component class SalarySlip(TransactionBase): def autoname(self): @@ -58,6 +59,12 @@ class SalarySlip(TransactionBase): if amount and struct_row.statistical_component == 0: self.update_component_row(struct_row, amount, key) + additional_components = get_additional_salary_component(self.employee, self.start_date, self.end_date) + if additional_components: + for additional_component in additional_components: + additional_component = frappe._dict(additional_component) + self.update_component_row(frappe._dict(additional_component.struct_row), additional_component.amount, "earnings") + def update_component_row(self, struct_row, amount, key): component_row = None for d in self.get(key):