def get_data(filters, leave_types):
	user = frappe.session.user
	allocation_records_based_on_to_date = get_leave_allocation_records(filters.to_date)
	allocation_records_based_on_from_date = get_leave_allocation_records(filters.from_date)

	active_employees = frappe.get_all("Employee", 
		filters = { "status": "Active", "company": filters.company}, 
		fields = ["name", "employee_name", "department", "user_id"])
	
	data = []
	for employee in active_employees:
		leave_approvers = [l.leave_approver for l in frappe.db.sql("""select leave_approver from `tabEmployee Leave Approver` where parent = %s""",
							(employee.name),as_dict=True)]
		if (len(leave_approvers) and user in leave_approvers) or (user in ["Administrator", employee.user_id]) or ("HR Manager" in frappe.get_roles(user)):
			row = [employee.name, employee.employee_name, employee.department]

			for leave_type in leave_types:
				# leaves taken
				leaves_taken = get_approved_leaves_for_period(employee.name, leave_type,
					filters.from_date, filters.to_date)

				# opening balance
				opening = get_leave_balance_on(employee.name, leave_type, filters.from_date,
					allocation_records_based_on_from_date.get(employee.name, frappe._dict()))

				# closing balance
				closing = get_leave_balance_on(employee.name, leave_type, filters.to_date,
					allocation_records_based_on_to_date.get(employee.name, frappe._dict()))

				row += [opening, leaves_taken, closing]
			
			data.append(row)
		
	return data
Example #2
0
def get_data(filters, leave_types):
	user = frappe.session.user
	allocation_records_based_on_to_date = get_leave_allocation_records(filters.to_date)
	allocation_records_based_on_from_date = get_leave_allocation_records(filters.from_date)

	active_employees = frappe.get_all("Employee", 
		filters = { "status": "Active", "company": filters.company}, 
		fields = ["name", "employee_name", "department", "user_id"])
	
	data = []
	for employee in active_employees:
		leave_approvers = get_approvers(employee.department)
		if (len(leave_approvers) and user in leave_approvers) or (user in ["Administrator", employee.user_id]) or ("HR Manager" in frappe.get_roles(user)):
			row = [employee.name, employee.employee_name, employee.department]

			for leave_type in leave_types:
				# leaves taken
				leaves_taken = get_approved_leaves_for_period(employee.name, leave_type,
					filters.from_date, filters.to_date)

				# opening balance
				opening = get_leave_balance_on(employee.name, leave_type, filters.from_date,
					allocation_records_based_on_from_date.get(employee.name, frappe._dict()))

				# closing balance
				closing = get_leave_balance_on(employee.name, leave_type, filters.to_date,
					allocation_records_based_on_to_date.get(employee.name, frappe._dict()))

				row += [opening, leaves_taken, closing]

			data.append(row)
		
	return data
Example #3
0
def get_data(filters, leave_types):
    dates = frappe.db.sql(
        '''select
									year_start_date, year_end_date
							from 
								`tabFiscal Year`
							where name = %s''', filters.fiscal_year)
    start_date = dates[0][0]
    end_date = dates[0][1]
    allocation_records_based_on_to_date_prev_year = get_leave_allocation_records(
        frappe.utils.add_days(start_date, -1))
    allocation_records_based_on_to_date = get_leave_allocation_records(
        end_date)
    allocation_records_based_on_from_date = get_leave_allocation_records(
        start_date)

    active_employees = frappe.get_all(
        "Employee",
        filters={
            "status": "Active",
            "company": filters.company
        },
        fields=["name", "employee_name", "department", "user_id"])

    data = []
    for employee in active_employees:
        row = [employee.name, employee.employee_name, employee.department]
        for leave_type in leave_types:
            # leaves taken
            leaves_taken = get_approved_leaves_for_period(
                employee.name, leave_type, start_date, end_date)
            #prev_year_opening
            prev_year_end_date = frappe.utils.add_days(start_date, -1)
            prev_year_opening = get_leave_balance_on(
                employee.name, leave_type, prev_year_end_date,
                allocation_records_based_on_to_date_prev_year.get(
                    employee.name, frappe._dict()))
            # opening balance
            opening = get_total_allocated_leaves(employee.name, leave_type,
                                                 end_date)
            added_leave_bal = 0
            if (opening - prev_year_opening) > 0:
                added_leave_bal = opening - prev_year_opening
            # closing balance
            closing = get_leave_balance_on(
                employee.name, leave_type, end_date,
                allocation_records_based_on_to_date.get(
                    employee.name, frappe._dict()))

            row += [prev_year_opening, added_leave_bal, leaves_taken, closing]

        data.append(row)

    return data
def get_data(filters, leave_types):
	user = frappe.session.user
	allocation_records_based_on_to_date = get_leave_allocation_records(filters.to_date)
	allocation_records_based_on_from_date = get_leave_allocation_records(filters.from_date)

	active_employees = frappe.get_all("Employee", 
		filters = { "status": "Active", "company": filters.company}, 
		fields = ["name", "employee_name", "department", "user_id"])
	
	data = []
	for employee in active_employees:
		leave_approvers = get_approvers(employee.department)
		if (len(leave_approvers) and user in leave_approvers) or (user in ["Administrator", employee.user_id]) or ("HR Manager" in frappe.get_roles(user)):
			row = [employee.name, employee.employee_name, employee.department]

			for leave_type in leave_types:
				# leaves taken
				leaves_taken = get_approved_leaves_for_period(employee.name, leave_type,
					filters.from_date, filters.to_date)

				# opening balance
				opening = 0
				closing = 0

				conditions = (" and leave_type= '"+str(leave_type)+"' and employee='%s' " % employee.name) if employee else ""

				leave_allocation_records = frappe.db.sql("""select employee, leave_type, total_leaves_allocated, total_leaves_encashed, from_date, to_date from `tabLeave Allocation` where from_date >= %s and to_date <= %s and docstatus=1 {0}""".format(conditions), (filters.from_date, filters.to_date), as_dict=1)
				allocated_leaves = frappe._dict()
				for d in leave_allocation_records:

					opening = d.total_leaves_allocated
					# allocated_leaves.setdefault(d.employee, frappe._dict()).setdefault(d.leave_type, frappe._dict({
					# 	"from_date": d.from_date,
					# 	"to_date": d.to_date,
					# 	"total_leaves_allocated": d.total_leaves_allocated,
					# 	"total_leaves_encashed":d.total_leaves_encashed
					# }))

					# opening = get_leave_balance_on(employee.name, leave_type, filters.from_date,allocation_records_based_on_from_date.get(employee.name, frappe._dict()))

				# closing balance
				closing = float(opening) - float(leaves_taken)
				# closing = get_leave_balance_on(employee.name, leave_type, filters.to_date,
				# 	allocation_records_based_on_to_date.get(employee.name, frappe._dict()))

				row += [opening, leaves_taken, closing]

			data.append(row)
		
	return data
Example #5
0
    def get_record(self, filters):
        allocation_records_based_on_to_date = get_leave_allocation_records(
            filters.get('to_date'))
        data, active_employees = [], frappe.get_all(
            "Employee",
            filters={"status": "Active"},
            fields=["name", "employee_name", "department"])
        for employee in active_employees:
            row = [
                employee.name, employee.employee_name, employee.department,
                self.get_days_spent(employee.name)
            ]
            # if leave type is selected in the filter
            # leaves taken
            leaves_taken = get_approved_leaves_for_period(
                employee.name, filters.get('leave_type'),
                filters.get('from_date'), filters.get('to_date'))
            # closing balance
            closing = get_leave_balance_on(
                employee.name, filters.get('leave_type'),
                filters.get('to_date'),
                allocation_records_based_on_to_date.get(
                    employee.name, frappe._dict()))

            row += [leaves_taken, closing]
            data.append(row)

        print(data)
def get_data(filters, leave_types):

	allocation_records_based_on_to_date = get_leave_allocation_records(filters.to_date)

	active_employees = frappe.get_all("Employee", 
		filters = { "status": "Active", "company": filters.company}, 
		fields = ["name", "employee_name", "department"])
	
	data = []
	for employee in active_employees:
		row = [employee.name, employee.employee_name, employee.department]

		for leave_type in leave_types:	
			# leaves taken
			leaves_taken = get_approved_leaves_for_period(employee.name, leave_type, 
				filters.from_date, filters.to_date)
	
			# closing balance
			closing = get_leave_balance_on(employee.name, leave_type, filters.to_date, 
				allocation_records_based_on_to_date.get(employee.name, frappe._dict()))

			row += [leaves_taken, closing]
			
		data.append(row)
		
	return data
def get_data(filters, leave_types):

    allocation_records_based_on_to_date = get_leave_allocation_records(
        filters.to_date)

    active_employees = frappe.get_all(
        "Employee",
        filters={
            "status": "Active",
            "company": filters.company
        },
        fields=["name", "employee_name", "department"])

    data = []
    for employee in active_employees:
        row = [employee.name, employee.employee_name, employee.department]

        for leave_type in leave_types:
            # leaves taken
            leaves_taken = get_approved_leaves_for_period(
                employee.name, leave_type, filters.from_date, filters.to_date)

            # closing balance
            closing = get_leave_balance_on(
                employee.name, leave_type, filters.to_date,
                allocation_records_based_on_to_date.get(
                    employee.name, frappe._dict()))

            row += [leaves_taken, closing]

        data.append(row)

    return data
def salaries(conditions, filters, leaves):
    data = []
    hours = {}
    ss = frappe.db.sql(
        """select * from `tabEmployee` where docstatus <2 %s order by name """
        % conditions,
        filters,
        as_dict=1)
    for add in ss:
        for leave_type in leaves:

            # leaves taken
            leaves_taken = get_approved_leaves_for_period(
                add.name, leave_type.name, filters.from_date, filters.to_date)
            allocation_records = get_leave_allocation_records(
                filters.to_date, add.name).get(add.name, frappe._dict())
            allocation = allocation_records.get(leave_type.name,
                                                frappe._dict())

            # closing balance
            closing = flt(
                allocation.total_leaves_allocated) - flt(leaves_taken)

            if closing <= 0:
                row = [
                    add.name, add.employee_name, add.department,
                    add.designation, leave_type.name
                ]
                if allocation.total_leaves_allocated > 0:
                    data.append(row)

    return data
def calculate_casual_leave(doc, method):
    user = frappe.session.user
    start_date = doc.start_date
    end_date = doc.end_date
    allocation_records_based_on_to_date = get_leave_allocation_records(
        end_date)
    employee_name = doc.employee_name

    active_employees = frappe.get_all(
        "Employee", fields=["name", "employee_name", "department", "user_id"])
    a = 0.0
    b = 0.0
    t = 0.0
    leaves_taken = 0.0

    for employee in active_employees:
        leave_approvers = [
            l.leave_approver for l in frappe.db.sql(
                """select leave_approver from `tabEmployee Leave Approver` where parent = %s""",
                (employee.name),
                as_dict=True)
        ]

        if (len(leave_approvers) and user in leave_approvers) or (user in [
                "Administrator", employee.user_id
        ]) or ("HR Manager" in frappe.get_roles(user)):

            # leaves taken
            leaves_taken = get_approved_leaves_for_period(
                employee.name, "Casual Leave", start_date, end_date)
            a = a + leaves_taken
    conditions = (" and employee_name='%s'" % employee_name)
    allocation_records = frappe.db.sql("""
        select total_leaves_allocated,from_date,to_date
        from `tabLeave Allocation`
        where leave_type="Casual Leave" and docstatus=1 {0}""".format(
        conditions),
                                       as_list=1)

    salary_slip_list = frappe.db.sql("""
        select start_date,end_date,pending_leaves,casual_leaves,sick_leaves
        from `tabSalary Slip`
        where docstatus=1 {0}""".format(conditions),
                                     as_list=1)

    if len(salary_slip_list) != 0:
        length = len(salary_slip_list)
        t = frappe.utils.data.flt(salary_slip_list[length - 1][3], precision=1)
        doc.casual_leaves = t - a

    if len(salary_slip_list) == 0:
        if allocation_records[0][
                1] <= start_date and start_date <= allocation_records[0][2]:
            doc.casual_leaves = allocation_records[0][0] - a
Example #10
0
def get_data(filters, leave_types):

    allocation_records_based_on_to_date = get_leave_allocation_records(
        filters.to_date)

    active_employees = frappe.get_all(
        "Employee",
        filters={
            "status": "Active",
            "company": filters.company
        },
        fields=["name", "employee_name", "department"],
        order_by="employee_name asc")

    data = []
    for employee in active_employees:
        row = [employee.name, employee.employee_name, employee.department]

        for leave_type in leave_types:
            matched_leave_type = leave_type.lower().replace(' ', '_')
            if filters.get(matched_leave_type):

                # leaves taken
                leaves_taken = get_approved_leaves_for_period(
                    employee.name, leave_type, filters.from_date,
                    filters.to_date)

                # closing balance
                closing = get_leave_balance_on(
                    employee.name, leave_type, filters.to_date,
                    allocation_records_based_on_to_date.get(
                        employee.name, frappe._dict()))

                # annual leave allocated
                allocation_records = allocation_records_based_on_to_date.get(
                    employee.name, frappe._dict())
                allocation = allocation_records.get(leave_type, frappe._dict())
                total_leaves_allocated = flt(allocation.total_leaves_allocated)
                new_leaves_allocated = flt(allocation.new_leaves_allocated)

                row += [
                    new_leaves_allocated, total_leaves_allocated, leaves_taken,
                    closing
                ]

        data.append(row)

    return data
Example #11
0
def get_leave_balance_on(employee,
                         leave_type,
                         date,
                         allocation_records=None,
                         consider_all_leaves_in_the_allocation_period=False):
    if allocation_records == None:
        allocation_records = get_leave_allocation_records(date, employee).get(
            employee, frappe._dict())

    allocation = allocation_records.get(leave_type, frappe._dict())

    if consider_all_leaves_in_the_allocation_period:
        date = allocation.to_date
    leaves_taken = get_approved_leaves_for_period(employee, leave_type,
                                                  allocation.from_date, date)

    return flt(allocation.total_leaves_allocated) - flt(leaves_taken)
Example #12
0
	def test_get_leave_allocation_records(self):
		employee = get_employee()
		leave_type = create_leave_type(
			leave_type_name="_Test_CF_leave_expiry",
			is_carry_forward=1,
			expire_carry_forwarded_leaves_after_days=90,
		)
		leave_type.insert()

		leave_alloc = create_carry_forwarded_allocation(employee, leave_type)
		details = get_leave_allocation_records(employee.name, getdate(), leave_type.name)
		expected_data = {
			"from_date": getdate(leave_alloc.from_date),
			"to_date": getdate(leave_alloc.to_date),
			"total_leaves_allocated": 30.0,
			"unused_leaves": 15.0,
			"new_leaves_allocated": 15.0,
			"leave_type": leave_type.name,
		}
		self.assertEqual(details.get(leave_type.name), expected_data)
Example #13
0
def get_leave_details(employee, date):
    '''Override white listed function get_leave_details for calculate leaves details
	Because after adding hourly leave funcionality leave balanace, leave used and pending leaves qouta calculted in decimals and 
	this add almost 10+ digits after decimal'''
    allocation_records = get_leave_allocation_records(date, employee).get(
        employee, frappe._dict())
    leave_allocation = {}
    for d in allocation_records:
        allocation = allocation_records.get(d, frappe._dict())
        date = allocation.to_date
        leaves_taken = round(
            get_leaves_for_period(employee,
                                  d,
                                  allocation.from_date,
                                  date,
                                  status="Approved"), 2)
        leaves_pending = round(
            get_leaves_for_period(employee,
                                  d,
                                  allocation.from_date,
                                  date,
                                  status="Open"), 2)
        remaining_leaves = round(
            allocation.total_leaves_allocated - leaves_taken - leaves_pending,
            2)
        leave_allocation[d] = {
            "total_leaves": allocation.total_leaves_allocated,
            "leaves_taken": leaves_taken,
            "pending_leaves": leaves_pending,
            "remaining_leaves": remaining_leaves
        }

    ret = {
        'leave_allocation': leave_allocation,
        'leave_approver': get_leave_approver(employee)
    }

    return ret
def get_data(filters, leave_types):

	allocation_records_based_on_to_date = get_leave_allocation_records(filters.to_date)

	active_employees = frappe.get_all("Employee", 
		filters = { "status": "Active", "company": filters.company}, 
		fields = ["name", "employee_name", "department"],
		order_by = "employee_name asc")
	
	data = []
	for employee in active_employees:
		row = [employee.name, employee.employee_name, employee.department]

		for leave_type in leave_types:	
			matched_leave_type = leave_type.lower().replace(' ','_')
			if filters.get(matched_leave_type):

				# leaves taken
				leaves_taken = get_approved_leaves_for_period(employee.name, leave_type, 
					filters.from_date, filters.to_date)
	
				# closing balance
				closing = get_leave_balance_on(employee.name, leave_type, filters.to_date, 
					allocation_records_based_on_to_date.get(employee.name, frappe._dict()))
			
				# annual leave allocated
				allocation_records = allocation_records_based_on_to_date.get(employee.name, frappe._dict())
				allocation = allocation_records.get(leave_type, frappe._dict())
				total_leaves_allocated = flt(allocation.total_leaves_allocated)
				new_leaves_allocated = flt(allocation.new_leaves_allocated)
				
				row += [new_leaves_allocated, total_leaves_allocated, leaves_taken, closing]
			
		data.append(row)
		
	return data
def get_data(filters, leave_types):
    user = frappe.session.user
    allocation_records_based_on_to_date = get_leave_allocation_records(
        filters.to_date)
    allocation_records_based_on_from_date = get_leave_allocation_records(
        filters.from_date)
    hr_manager = frappe.get_doc("User", frappe.session.user).get(
        "roles", {"role": "HR Manager"})
    if hr_manager:
        emp_filters = {
            "status": "Active",
            "company": filters.company,
            "department": filters.department
        }
    else:
        emp_filters = {
            "status": "Active",
            "company": filters.company,
            "department": filters.department,
            "user_id": frappe.session.user
        }

    active_employees = frappe.get_all(
        "Employee",
        filters=emp_filters,
        fields=["name", "employee_name", "department", "user_id"],
        order_by="name desc")

    data = []
    for employee in active_employees:
        leave_approvers = [
            l.leave_approver for l in frappe.db.sql(
                """select leave_approver from `tabEmployee Leave Approver` where parent = %s""",
                (employee.name),
                as_dict=True)
        ]
        if (len(leave_approvers) and user in leave_approvers) or (user in [
                "Administrator", employee.user_id
        ]) or ("HR Manager" in frappe.get_roles(user)):
            row = [employee.name, employee.employee_name, employee.department]

            for leave_type in leave_types:
                # leaves taken
                leaves_taken = get_approved_leaves_for_period(
                    employee.name, leave_type, filters.from_date,
                    filters.to_date)

                # opening balance
                opening = get_leave_balance_on(
                    employee.name, leave_type, filters.from_date,
                    allocation_records_based_on_from_date.get(
                        employee.name, frappe._dict()))

                # closing balance
                closing = get_leave_balance_on(
                    employee.name, leave_type, filters.to_date,
                    allocation_records_based_on_to_date.get(
                        employee.name, frappe._dict()))

                row += [opening, leaves_taken, closing]

            data.append(row)

    return data