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