brotherton-erpnext/erpnext/demo/user/hr.py

199 lines
7.0 KiB
Python
Raw Normal View History

2016-06-29 13:08:32 +00:00
from __future__ import unicode_literals
import frappe, erpnext
2016-07-13 10:33:05 +00:00
import random
import datetime
from frappe.utils import random_string, add_days, get_last_day, getdate
2016-07-13 10:33:05 +00:00
from erpnext.projects.doctype.timesheet.test_timesheet import make_timesheet
from erpnext.projects.doctype.timesheet.timesheet import make_salary_slip, make_sales_invoice
2016-08-22 07:27:09 +00:00
from frappe.utils.make_random import get_random
2016-07-15 12:58:05 +00:00
from erpnext.hr.doctype.expense_claim.expense_claim import get_expense_approver, make_bank_entry
from erpnext.hr.doctype.leave_application.leave_application import (get_leave_balance_on,
OverlapError, AttendanceAlreadyMarkedError)
2016-06-29 13:08:32 +00:00
def work():
frappe.set_user(frappe.db.get_global('demo_hr_user'))
year, month = frappe.flags.current_date.strftime("%Y-%m").split("-")
mark_attendance()
make_leave_application()
2016-06-29 13:08:32 +00:00
# process payroll
if not frappe.db.sql('select name from `tabSalary Slip` where month(adddate(start_date, interval 1 month))=month(curdate())'):
# process payroll for previous month
2016-06-29 13:08:32 +00:00
process_payroll = frappe.get_doc("Process Payroll", "Process Payroll")
process_payroll.company = frappe.flags.company
process_payroll.payroll_frequency = 'Monthly'
# select a posting date from the previous month
process_payroll.posting_date = get_last_day(getdate(frappe.flags.current_date) - datetime.timedelta(days=10))
process_payroll.payment_account = frappe.get_value('Account', {'account_type': 'Cash', 'company': erpnext.get_default_company(),'is_group':0}, "name")
process_payroll.set_start_end_dates()
# based on frequency
process_payroll.salary_slip_based_on_timesheet = 0
process_payroll.create_salary_slips()
process_payroll.submit_salary_slips()
process_payroll.make_journal_entry(reference_date=frappe.flags.current_date,
reference_number=random_string(10))
process_payroll.salary_slip_based_on_timesheet = 1
process_payroll.create_salary_slips()
process_payroll.submit_salary_slips()
process_payroll.make_journal_entry(reference_date=frappe.flags.current_date,
reference_number=random_string(10))
2016-07-13 10:33:05 +00:00
if frappe.db.get_global('demo_hr_user'):
make_timesheet_records()
2016-07-15 12:58:05 +00:00
#expense claim
expense_claim = frappe.new_doc("Expense Claim")
expense_claim.extend('expenses', get_expenses())
expense_claim.employee = get_random("Employee")
expense_claim.company = frappe.flags.company
expense_claim.posting_date = frappe.flags.current_date
expense_claim.exp_approver = filter((lambda x: x[0] != 'Administrator'), get_expense_approver(None, '', None, 0, 20, None))[0][0]
expense_claim.insert()
rand = random.random()
2016-07-18 10:52:51 +00:00
if rand < 0.4:
2016-07-15 12:58:05 +00:00
expense_claim.approval_status = "Approved"
update_sanctioned_amount(expense_claim)
expense_claim.submit()
if random.randint(0, 1):
#make journal entry against expense claim
je = frappe.get_doc(make_bank_entry(expense_claim.name))
je.posting_date = frappe.flags.current_date
je.cheque_no = random_string(10)
je.cheque_date = frappe.flags.current_date
je.flags.ignore_permissions = 1
je.submit()
2016-07-18 10:52:51 +00:00
elif rand < 0.2:
2016-07-15 12:58:05 +00:00
expense_claim.approval_status = "Rejected"
expense_claim.submit()
def get_expenses():
expenses = []
expese_types = frappe.db.sql("""select ect.name, eca.default_account from `tabExpense Claim Type` ect,
`tabExpense Claim Account` eca where eca.parent=ect.name
and eca.company=%s """, frappe.flags.company,as_dict=1)
2016-07-15 12:58:05 +00:00
for expense_type in expese_types[:random.randint(1,4)]:
claim_amount = random.randint(1,20)*10
expenses.append({
"expense_date": frappe.flags.current_date,
"expense_type": expense_type.name,
"default_account": expense_type.default_account or "Miscellaneous Expenses - WPL",
"claim_amount": claim_amount,
"sanctioned_amount": claim_amount
})
return expenses
def update_sanctioned_amount(expense_claim):
for expense in expense_claim.expenses:
sanctioned_amount = random.randint(1,20)*10
if sanctioned_amount < expense.claim_amount:
expense.sanctioned_amount = sanctioned_amount
2016-07-13 10:33:05 +00:00
def get_timesheet_based_salary_slip_employee():
2016-08-19 19:00:59 +00:00
sal_struct = frappe.db.sql("""
select name from `tabSalary Structure`
where salary_slip_based_on_timesheet = 1
and docstatus != 2""")
if sal_struct:
employees = frappe.db.sql("""
select employee from `tabSalary Structure Employee`
where parent IN %(sal_struct)s""", {"sal_struct": sal_struct}, as_dict=True)
return employees
else:
return []
2016-07-13 10:33:05 +00:00
def make_timesheet_records():
employees = get_timesheet_based_salary_slip_employee()
2016-08-19 19:00:59 +00:00
for e in employees:
ts = make_timesheet(e.employee, simulate = True, billable = 1, activity_type=get_random("Activity Type"))
2016-07-13 10:33:05 +00:00
rand = random.random()
if rand >= 0.3:
make_salary_slip_for_timesheet(ts.name)
rand = random.random()
if rand >= 0.2:
make_sales_invoice_for_timesheet(ts.name)
def make_salary_slip_for_timesheet(name):
salary_slip = make_salary_slip(name)
salary_slip.insert()
salary_slip.submit()
frappe.db.commit()
2016-07-13 10:33:05 +00:00
def make_sales_invoice_for_timesheet(name):
sales_invoice = make_sales_invoice(name)
sales_invoice.customer = get_random("Customer")
sales_invoice.append('items', {
2016-07-21 06:09:46 +00:00
'item_code': get_random("Item", {"has_variants": 0, "is_stock_item": 0, "is_fixed_asset": 0}),
2016-07-13 10:33:05 +00:00
'qty': 1,
'rate': 1000
})
sales_invoice.flags.ignore_permissions = 1
2016-07-13 10:33:05 +00:00
sales_invoice.set_missing_values()
sales_invoice.calculate_taxes_and_totals()
sales_invoice.insert()
sales_invoice.submit()
frappe.db.commit()
def make_leave_application():
allocated_leaves = frappe.get_all("Leave Allocation", fields=['employee', 'leave_type'])
for allocated_leave in allocated_leaves:
leave_balance = get_leave_balance_on(allocated_leave.employee, allocated_leave.leave_type, frappe.flags.current_date,
consider_all_leaves_in_the_allocation_period=True)
if leave_balance != 0:
if leave_balance == 1:
to_date = frappe.flags.current_date
else:
to_date = add_days(frappe.flags.current_date, random.randint(0, leave_balance-1))
leave_application = frappe.get_doc({
"doctype": "Leave Application",
"employee": allocated_leave.employee,
"from_date": frappe.flags.current_date,
"to_date": to_date,
"leave_type": allocated_leave.leave_type,
"status": "Approved"
})
try:
leave_application.insert()
leave_application.submit()
frappe.db.commit()
except (OverlapError, AttendanceAlreadyMarkedError):
frappe.db.rollback()
def mark_attendance():
att_date = frappe.flags.current_date
for employee in frappe.get_all('Employee', fields=['name'], filters = {'status': 'Active'}):
if not frappe.db.get_value("Attendance", {"employee": employee.name, "att_date": att_date}):
attendance = frappe.get_doc({
"doctype": "Attendance",
"employee": employee.name,
"att_date": att_date
})
leave = frappe.db.sql("""select name from `tabLeave Application`
where employee = %s and %s between from_date and to_date and status = 'Approved'
and docstatus = 1""", (employee.name, att_date))
if leave:
attendance.status = "Absent"
else:
2016-08-22 07:27:09 +00:00
attendance.status = "Present"
attendance.save()
attendance.submit()
2016-08-22 07:27:09 +00:00
frappe.db.commit()