Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Kanchan Chauhan | 1c1e2e9 | 2016-10-20 14:53:10 +0530 | [diff] [blame] | 2 | import frappe, erpnext |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 3 | import random |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 4 | import datetime |
| 5 | from frappe.utils import random_string, add_days, get_last_day, getdate |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 6 | from erpnext.projects.doctype.timesheet.test_timesheet import make_timesheet |
| 7 | from erpnext.projects.doctype.timesheet.timesheet import make_salary_slip, make_sales_invoice |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 8 | from frappe.utils.make_random import get_random |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 9 | from erpnext.hr.doctype.expense_claim.expense_claim import get_expense_approver, make_bank_entry |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 10 | from erpnext.hr.doctype.leave_application.leave_application import (get_leave_balance_on, |
| 11 | OverlapError, AttendanceAlreadyMarkedError) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 12 | |
| 13 | def work(): |
| 14 | frappe.set_user(frappe.db.get_global('demo_hr_user')) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 15 | year, month = frappe.flags.current_date.strftime("%Y-%m").split("-") |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 16 | mark_attendance() |
| 17 | make_leave_application() |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 18 | |
| 19 | # process payroll |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 20 | if not frappe.db.sql('select name from `tabSalary Slip` where month(adddate(start_date, interval 1 month))=month(curdate())'): |
| 21 | # process payroll for previous month |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 22 | process_payroll = frappe.get_doc("Process Payroll", "Process Payroll") |
| 23 | process_payroll.company = frappe.flags.company |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 24 | process_payroll.payroll_frequency = 'Monthly' |
| 25 | |
| 26 | # select a posting date from the previous month |
| 27 | process_payroll.posting_date = get_last_day(getdate(frappe.flags.current_date) - datetime.timedelta(days=10)) |
Kanchan Chauhan | 1c1e2e9 | 2016-10-20 14:53:10 +0530 | [diff] [blame] | 28 | process_payroll.payment_account = frappe.get_value('Account', {'account_type': 'Cash', 'company': erpnext.get_default_company(),'is_group':0}, "name") |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 29 | |
| 30 | process_payroll.set_start_end_dates() |
| 31 | |
| 32 | # based on frequency |
| 33 | process_payroll.salary_slip_based_on_timesheet = 0 |
| 34 | process_payroll.create_salary_slips() |
| 35 | process_payroll.submit_salary_slips() |
| 36 | process_payroll.make_journal_entry(reference_date=frappe.flags.current_date, |
| 37 | reference_number=random_string(10)) |
| 38 | |
| 39 | process_payroll.salary_slip_based_on_timesheet = 1 |
| 40 | process_payroll.create_salary_slips() |
| 41 | process_payroll.submit_salary_slips() |
| 42 | process_payroll.make_journal_entry(reference_date=frappe.flags.current_date, |
| 43 | reference_number=random_string(10)) |
| 44 | |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 45 | if frappe.db.get_global('demo_hr_user'): |
| 46 | make_timesheet_records() |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 47 | |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 48 | #expense claim |
| 49 | expense_claim = frappe.new_doc("Expense Claim") |
| 50 | expense_claim.extend('expenses', get_expenses()) |
| 51 | expense_claim.employee = get_random("Employee") |
| 52 | expense_claim.company = frappe.flags.company |
| 53 | expense_claim.posting_date = frappe.flags.current_date |
| 54 | expense_claim.exp_approver = filter((lambda x: x[0] != 'Administrator'), get_expense_approver(None, '', None, 0, 20, None))[0][0] |
| 55 | expense_claim.insert() |
| 56 | |
| 57 | rand = random.random() |
| 58 | |
Saurabh | 9cba6e1 | 2016-07-18 16:22:51 +0530 | [diff] [blame] | 59 | if rand < 0.4: |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 60 | expense_claim.approval_status = "Approved" |
| 61 | update_sanctioned_amount(expense_claim) |
| 62 | expense_claim.submit() |
| 63 | |
| 64 | if random.randint(0, 1): |
| 65 | #make journal entry against expense claim |
| 66 | je = frappe.get_doc(make_bank_entry(expense_claim.name)) |
| 67 | je.posting_date = frappe.flags.current_date |
| 68 | je.cheque_no = random_string(10) |
| 69 | je.cheque_date = frappe.flags.current_date |
| 70 | je.flags.ignore_permissions = 1 |
| 71 | je.submit() |
| 72 | |
Saurabh | 9cba6e1 | 2016-07-18 16:22:51 +0530 | [diff] [blame] | 73 | elif rand < 0.2: |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 74 | expense_claim.approval_status = "Rejected" |
| 75 | expense_claim.submit() |
| 76 | |
| 77 | def get_expenses(): |
| 78 | expenses = [] |
Saurabh | 718d835 | 2016-07-18 15:20:47 +0530 | [diff] [blame] | 79 | expese_types = frappe.db.sql("""select ect.name, eca.default_account from `tabExpense Claim Type` ect, |
| 80 | `tabExpense Claim Account` eca where eca.parent=ect.name |
| 81 | and eca.company=%s """, frappe.flags.company,as_dict=1) |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 82 | |
| 83 | for expense_type in expese_types[:random.randint(1,4)]: |
| 84 | claim_amount = random.randint(1,20)*10 |
| 85 | |
| 86 | expenses.append({ |
| 87 | "expense_date": frappe.flags.current_date, |
| 88 | "expense_type": expense_type.name, |
| 89 | "default_account": expense_type.default_account or "Miscellaneous Expenses - WPL", |
| 90 | "claim_amount": claim_amount, |
| 91 | "sanctioned_amount": claim_amount |
| 92 | }) |
| 93 | |
| 94 | return expenses |
| 95 | |
| 96 | def update_sanctioned_amount(expense_claim): |
| 97 | for expense in expense_claim.expenses: |
| 98 | sanctioned_amount = random.randint(1,20)*10 |
| 99 | |
| 100 | if sanctioned_amount < expense.claim_amount: |
| 101 | expense.sanctioned_amount = sanctioned_amount |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 102 | |
| 103 | def get_timesheet_based_salary_slip_employee(): |
Kanchan Chauhan | db197d5 | 2016-08-20 00:30:59 +0530 | [diff] [blame] | 104 | sal_struct = frappe.db.sql(""" |
| 105 | select name from `tabSalary Structure` |
| 106 | where salary_slip_based_on_timesheet = 1 |
| 107 | and docstatus != 2""") |
| 108 | if sal_struct: |
| 109 | employees = frappe.db.sql(""" |
| 110 | select employee from `tabSalary Structure Employee` |
| 111 | where parent IN %(sal_struct)s""", {"sal_struct": sal_struct}, as_dict=True) |
| 112 | return employees |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 113 | |
| 114 | else: |
| 115 | return [] |
| 116 | |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 117 | def make_timesheet_records(): |
| 118 | employees = get_timesheet_based_salary_slip_employee() |
Kanchan Chauhan | db197d5 | 2016-08-20 00:30:59 +0530 | [diff] [blame] | 119 | for e in employees: |
| 120 | ts = make_timesheet(e.employee, simulate = True, billable = 1, activity_type=get_random("Activity Type")) |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 121 | |
| 122 | rand = random.random() |
| 123 | if rand >= 0.3: |
| 124 | make_salary_slip_for_timesheet(ts.name) |
| 125 | |
| 126 | rand = random.random() |
| 127 | if rand >= 0.2: |
| 128 | make_sales_invoice_for_timesheet(ts.name) |
| 129 | |
| 130 | def make_salary_slip_for_timesheet(name): |
| 131 | salary_slip = make_salary_slip(name) |
| 132 | salary_slip.insert() |
| 133 | salary_slip.submit() |
Rohit Waghchaure | 7d439ec | 2016-07-21 14:50:59 +0530 | [diff] [blame] | 134 | frappe.db.commit() |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 135 | |
| 136 | def make_sales_invoice_for_timesheet(name): |
| 137 | sales_invoice = make_sales_invoice(name) |
| 138 | sales_invoice.customer = get_random("Customer") |
| 139 | sales_invoice.append('items', { |
Nabin Hait | 74edfff | 2016-07-21 11:39:46 +0530 | [diff] [blame] | 140 | 'item_code': get_random("Item", {"has_variants": 0, "is_stock_item": 0, "is_fixed_asset": 0}), |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 141 | 'qty': 1, |
| 142 | 'rate': 1000 |
| 143 | }) |
Rohit Waghchaure | 7d439ec | 2016-07-21 14:50:59 +0530 | [diff] [blame] | 144 | sales_invoice.flags.ignore_permissions = 1 |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 145 | sales_invoice.set_missing_values() |
| 146 | sales_invoice.calculate_taxes_and_totals() |
| 147 | sales_invoice.insert() |
Rohit Waghchaure | 7d439ec | 2016-07-21 14:50:59 +0530 | [diff] [blame] | 148 | sales_invoice.submit() |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 149 | frappe.db.commit() |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 150 | |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 151 | def make_leave_application(): |
| 152 | allocated_leaves = frappe.get_all("Leave Allocation", fields=['employee', 'leave_type']) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 153 | |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 154 | for allocated_leave in allocated_leaves: |
| 155 | leave_balance = get_leave_balance_on(allocated_leave.employee, allocated_leave.leave_type, frappe.flags.current_date, |
| 156 | consider_all_leaves_in_the_allocation_period=True) |
| 157 | if leave_balance != 0: |
| 158 | if leave_balance == 1: |
| 159 | to_date = frappe.flags.current_date |
| 160 | else: |
| 161 | to_date = add_days(frappe.flags.current_date, random.randint(0, leave_balance-1)) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 162 | |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 163 | leave_application = frappe.get_doc({ |
| 164 | "doctype": "Leave Application", |
| 165 | "employee": allocated_leave.employee, |
| 166 | "from_date": frappe.flags.current_date, |
| 167 | "to_date": to_date, |
| 168 | "leave_type": allocated_leave.leave_type, |
| 169 | "status": "Approved" |
| 170 | }) |
| 171 | try: |
| 172 | leave_application.insert() |
| 173 | leave_application.submit() |
| 174 | frappe.db.commit() |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 175 | except (OverlapError, AttendanceAlreadyMarkedError): |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 176 | frappe.db.rollback() |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 177 | |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 178 | def mark_attendance(): |
| 179 | att_date = frappe.flags.current_date |
| 180 | for employee in frappe.get_all('Employee', fields=['name'], filters = {'status': 'Active'}): |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 181 | |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 182 | if not frappe.db.get_value("Attendance", {"employee": employee.name, "att_date": att_date}): |
| 183 | attendance = frappe.get_doc({ |
| 184 | "doctype": "Attendance", |
| 185 | "employee": employee.name, |
| 186 | "att_date": att_date |
| 187 | }) |
| 188 | leave = frappe.db.sql("""select name from `tabLeave Application` |
| 189 | where employee = %s and %s between from_date and to_date and status = 'Approved' |
| 190 | and docstatus = 1""", (employee.name, att_date)) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 191 | |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 192 | if leave: |
| 193 | attendance.status = "Absent" |
| 194 | else: |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 195 | attendance.status = "Present" |
Kanchan Chauhan | db0e57c | 2016-07-29 15:59:39 +0530 | [diff] [blame] | 196 | attendance.save() |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 197 | attendance.submit() |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 198 | frappe.db.commit() |