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