blob: 6c6a7d4e9377fc8cda981ed9984056fc7993736f [file] [log] [blame]
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05301from __future__ import unicode_literals
Kanchan Chauhan1c1e2e92016-10-20 14:53:10 +05302import frappe, erpnext
Rohit Waghchaure8002d472016-07-13 16:03:05 +05303import random
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +05304import datetime
5from frappe.utils import random_string, add_days, get_last_day, getdate
Rohit Waghchaure8002d472016-07-13 16:03:05 +05306from erpnext.projects.doctype.timesheet.test_timesheet import make_timesheet
7from erpnext.projects.doctype.timesheet.timesheet import make_salary_slip, make_sales_invoice
Neil Trini Lasrado06724592016-08-22 12:57:09 +05308from frappe.utils.make_random import get_random
rohitwaghchaureea092a72017-02-01 12:02:08 +05309from erpnext.hr.doctype.expense_claim.test_expense_claim import get_payable_account
Shreya Shah093e7e62018-02-16 14:49:39 +053010from erpnext.hr.doctype.expense_claim.expense_claim import make_bank_entry
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053011from erpnext.hr.doctype.leave_application.leave_application import (get_leave_balance_on,
12 OverlapError, AttendanceAlreadyMarkedError)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053013
14def work():
15 frappe.set_user(frappe.db.get_global('demo_hr_user'))
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053016 year, month = frappe.flags.current_date.strftime("%Y-%m").split("-")
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +053017 mark_attendance()
18 make_leave_application()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053019
Shreyaf8e7bc72017-12-01 10:42:12 +053020 # payroll entry
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053021 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
Shreyaf8e7bc72017-12-01 10:42:12 +053023 payroll_entry = frappe.new_doc("Payroll Entry")
24 payroll_entry.company = frappe.flags.company
25 payroll_entry.payroll_frequency = 'Monthly'
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053026
27 # select a posting date from the previous month
Shreyaf8e7bc72017-12-01 10:42:12 +053028 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 Mehtae9d9b8e2016-12-15 11:27:35 +053030
Shreyaf8e7bc72017-12-01 10:42:12 +053031 payroll_entry.set_start_end_dates()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053032
33 # based on frequency
Shreyaf8e7bc72017-12-01 10:42:12 +053034 payroll_entry.salary_slip_based_on_timesheet = 0
35 payroll_entry.create_salary_slips()
36 payroll_entry.submit_salary_slips()
Shreya9240eaa2018-03-30 12:19:11 +053037 payroll_entry.make_accrual_jv_entry()
Shreyaf8e7bc72017-12-01 10:42:12 +053038 # payroll_entry.make_journal_entry(reference_date=frappe.flags.current_date,
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053039 # reference_number=random_string(10))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053040
Shreyaf8e7bc72017-12-01 10:42:12 +053041 payroll_entry.salary_slip_based_on_timesheet = 1
42 payroll_entry.create_salary_slips()
43 payroll_entry.submit_salary_slips()
Shreya9240eaa2018-03-30 12:19:11 +053044 payroll_entry.make_accrual_jv_entry()
Shreyaf8e7bc72017-12-01 10:42:12 +053045 # payroll_entry.make_journal_entry(reference_date=frappe.flags.current_date,
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053046 # reference_number=random_string(10))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053047
Rohit Waghchaure8002d472016-07-13 16:03:05 +053048 if frappe.db.get_global('demo_hr_user'):
49 make_timesheet_records()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053050
Saurabhf589c822016-07-15 18:28:05 +053051 #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
rohitwaghchaureea092a72017-02-01 12:02:08 +053056 expense_claim.payable_account = get_payable_account(expense_claim.company)
Saurabhf589c822016-07-15 18:28:05 +053057 expense_claim.posting_date = frappe.flags.current_date
Saurabhf589c822016-07-15 18:28:05 +053058 expense_claim.insert()
59
60 rand = random.random()
61
Saurabh9cba6e12016-07-18 16:22:51 +053062 if rand < 0.4:
Saurabhf589c822016-07-15 18:28:05 +053063 update_sanctioned_amount(expense_claim)
64 expense_claim.submit()
65
66 if random.randint(0, 1):
67 #make journal entry against expense claim
Nabin Hait9641d5b2017-08-01 17:33:08 +053068 je = frappe.get_doc(make_bank_entry("Expense Claim", expense_claim.name))
Saurabhf589c822016-07-15 18:28:05 +053069 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
Saurabhf589c822016-07-15 18:28:05 +053075def get_expenses():
76 expenses = []
Saurabh718d8352016-07-18 15:20:47 +053077 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)
Saurabhf589c822016-07-15 18:28:05 +053080
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
94def 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 Waghchaure8002d472016-07-13 16:03:05 +0530100
101def get_timesheet_based_salary_slip_employee():
Kanchan Chauhandb197d52016-08-20 00:30:59 +0530102 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("""
108 select employee from `tabSalary Structure Employee`
109 where parent IN %(sal_struct)s""", {"sal_struct": sal_struct}, as_dict=True)
110 return employees
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530111
112 else:
113 return []
114
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530115def make_timesheet_records():
116 employees = get_timesheet_based_salary_slip_employee()
Kanchan Chauhandb197d52016-08-20 00:30:59 +0530117 for e in employees:
118 ts = make_timesheet(e.employee, simulate = True, billable = 1, activity_type=get_random("Activity Type"))
Rushabh Mehta397e5082017-01-21 16:57:24 +0530119 frappe.db.commit()
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530120
121 rand = random.random()
122 if rand >= 0.3:
123 make_salary_slip_for_timesheet(ts.name)
124
125 rand = random.random()
126 if rand >= 0.2:
127 make_sales_invoice_for_timesheet(ts.name)
128
129def make_salary_slip_for_timesheet(name):
130 salary_slip = make_salary_slip(name)
131 salary_slip.insert()
132 salary_slip.submit()
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530133 frappe.db.commit()
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530134
135def make_sales_invoice_for_timesheet(name):
136 sales_invoice = make_sales_invoice(name)
137 sales_invoice.customer = get_random("Customer")
138 sales_invoice.append('items', {
Rushabh Mehta397e5082017-01-21 16:57:24 +0530139 'item_code': get_random("Item", {"has_variants": 0, "is_stock_item": 0,
140 "is_fixed_asset": 0}),
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530141 'qty': 1,
142 'rate': 1000
143 })
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530144 sales_invoice.flags.ignore_permissions = 1
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530145 sales_invoice.set_missing_values()
146 sales_invoice.calculate_taxes_and_totals()
147 sales_invoice.insert()
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530148 sales_invoice.submit()
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530149 frappe.db.commit()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530150
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530151def make_leave_application():
152 allocated_leaves = frappe.get_all("Leave Allocation", fields=['employee', 'leave_type'])
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530153
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530154 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 Mehtae9d9b8e2016-12-15 11:27:35 +0530162
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530163 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,
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530169 })
170 try:
171 leave_application.insert()
172 leave_application.submit()
173 frappe.db.commit()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530174 except (OverlapError, AttendanceAlreadyMarkedError):
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530175 frappe.db.rollback()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530176
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530177def mark_attendance():
Kanchan Chauhan7933eb62017-01-12 17:24:53 +0530178 attendance_date = frappe.flags.current_date
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530179 for employee in frappe.get_all('Employee', fields=['name'], filters = {'status': 'Active'}):
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530180
Kanchan Chauhan7933eb62017-01-12 17:24:53 +0530181 if not frappe.db.get_value("Attendance", {"employee": employee.name, "attendance_date": attendance_date}):
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530182 attendance = frappe.get_doc({
183 "doctype": "Attendance",
184 "employee": employee.name,
Kanchan Chauhan7933eb62017-01-12 17:24:53 +0530185 "attendance_date": attendance_date
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530186 })
Shreya Shahd9a585b2018-02-12 16:02:57 +0530187
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530188 leave = frappe.db.sql("""select name from `tabLeave Application`
Shreya Shahd9a585b2018-02-12 16:02:57 +0530189 where employee = %s and %s between from_date and to_date
Kanchan Chauhan7933eb62017-01-12 17:24:53 +0530190 and docstatus = 1""", (employee.name, attendance_date))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530191
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530192 if leave:
193 attendance.status = "Absent"
194 else:
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530195 attendance.status = "Present"
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530196 attendance.save()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530197 attendance.submit()
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530198 frappe.db.commit()