blob: db2d167dd01f4c04f023856582585820f38c775c [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())'):
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053022 # based on frequency
Zlash65796bffb2018-09-26 12:27:09 +053023 payroll_entry = get_payroll_entry()
Shreyaf8e7bc72017-12-01 10:42:12 +053024 payroll_entry.salary_slip_based_on_timesheet = 0
Zlash65796bffb2018-09-26 12:27:09 +053025 payroll_entry.save()
Shreyaf8e7bc72017-12-01 10:42:12 +053026 payroll_entry.create_salary_slips()
27 payroll_entry.submit_salary_slips()
Shreya9240eaa2018-03-30 12:19:11 +053028 payroll_entry.make_accrual_jv_entry()
Zlash65796bffb2018-09-26 12:27:09 +053029 payroll_entry.submit()
Shreyaf8e7bc72017-12-01 10:42:12 +053030 # payroll_entry.make_journal_entry(reference_date=frappe.flags.current_date,
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053031 # reference_number=random_string(10))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053032
Zlash65796bffb2018-09-26 12:27:09 +053033 # based on timesheet
34 payroll_entry = get_payroll_entry()
Shreyaf8e7bc72017-12-01 10:42:12 +053035 payroll_entry.salary_slip_based_on_timesheet = 1
Zlash65796bffb2018-09-26 12:27:09 +053036 payroll_entry.save()
Shreyaf8e7bc72017-12-01 10:42:12 +053037 payroll_entry.create_salary_slips()
38 payroll_entry.submit_salary_slips()
Shreya9240eaa2018-03-30 12:19:11 +053039 payroll_entry.make_accrual_jv_entry()
Zlash65796bffb2018-09-26 12:27:09 +053040 payroll_entry.submit()
Shreyaf8e7bc72017-12-01 10:42:12 +053041 # payroll_entry.make_journal_entry(reference_date=frappe.flags.current_date,
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053042 # reference_number=random_string(10))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053043
Rohit Waghchaure8002d472016-07-13 16:03:05 +053044 if frappe.db.get_global('demo_hr_user'):
45 make_timesheet_records()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053046
Saurabhf589c822016-07-15 18:28:05 +053047 #expense claim
48 expense_claim = frappe.new_doc("Expense Claim")
49 expense_claim.extend('expenses', get_expenses())
50 expense_claim.employee = get_random("Employee")
51 expense_claim.company = frappe.flags.company
rohitwaghchaureea092a72017-02-01 12:02:08 +053052 expense_claim.payable_account = get_payable_account(expense_claim.company)
Saurabhf589c822016-07-15 18:28:05 +053053 expense_claim.posting_date = frappe.flags.current_date
Saurabhf589c822016-07-15 18:28:05 +053054 expense_claim.insert()
55
56 rand = random.random()
57
Saurabh9cba6e12016-07-18 16:22:51 +053058 if rand < 0.4:
Saurabhf589c822016-07-15 18:28:05 +053059 update_sanctioned_amount(expense_claim)
60 expense_claim.submit()
61
62 if random.randint(0, 1):
63 #make journal entry against expense claim
Nabin Hait9641d5b2017-08-01 17:33:08 +053064 je = frappe.get_doc(make_bank_entry("Expense Claim", expense_claim.name))
Saurabhf589c822016-07-15 18:28:05 +053065 je.posting_date = frappe.flags.current_date
66 je.cheque_no = random_string(10)
67 je.cheque_date = frappe.flags.current_date
68 je.flags.ignore_permissions = 1
69 je.submit()
70
Zlash65796bffb2018-09-26 12:27:09 +053071def get_payroll_entry():
72 # process payroll for previous month
73 payroll_entry = frappe.new_doc("Payroll Entry")
74 payroll_entry.company = frappe.flags.company
75 payroll_entry.payroll_frequency = 'Monthly'
76
77 # select a posting date from the previous month
78 payroll_entry.posting_date = get_last_day(getdate(frappe.flags.current_date) - datetime.timedelta(days=10))
79 payroll_entry.payment_account = frappe.get_value('Account', {'account_type': 'Cash', 'company': erpnext.get_default_company(),'is_group':0}, "name")
80
81 payroll_entry.set_start_end_dates()
82 return payroll_entry
83
Saurabhf589c822016-07-15 18:28:05 +053084def get_expenses():
85 expenses = []
Saurabh718d8352016-07-18 15:20:47 +053086 expese_types = frappe.db.sql("""select ect.name, eca.default_account from `tabExpense Claim Type` ect,
87 `tabExpense Claim Account` eca where eca.parent=ect.name
88 and eca.company=%s """, frappe.flags.company,as_dict=1)
Saurabhf589c822016-07-15 18:28:05 +053089
90 for expense_type in expese_types[:random.randint(1,4)]:
91 claim_amount = random.randint(1,20)*10
92
93 expenses.append({
94 "expense_date": frappe.flags.current_date,
95 "expense_type": expense_type.name,
96 "default_account": expense_type.default_account or "Miscellaneous Expenses - WPL",
97 "claim_amount": claim_amount,
98 "sanctioned_amount": claim_amount
99 })
100
101 return expenses
102
103def update_sanctioned_amount(expense_claim):
104 for expense in expense_claim.expenses:
105 sanctioned_amount = random.randint(1,20)*10
106
107 if sanctioned_amount < expense.claim_amount:
108 expense.sanctioned_amount = sanctioned_amount
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530109
110def get_timesheet_based_salary_slip_employee():
Kanchan Chauhandb197d52016-08-20 00:30:59 +0530111 sal_struct = frappe.db.sql("""
112 select name from `tabSalary Structure`
113 where salary_slip_based_on_timesheet = 1
114 and docstatus != 2""")
115 if sal_struct:
116 employees = frappe.db.sql("""
Nabin Hait2f82bce2018-04-26 16:32:17 +0530117 select employee from `tabSalary Structure Assignment`
118 where salary_structure IN %(sal_struct)s""", {"sal_struct": sal_struct}, as_dict=True)
Kanchan Chauhandb197d52016-08-20 00:30:59 +0530119 return employees
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530120 else:
121 return []
122
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530123def make_timesheet_records():
124 employees = get_timesheet_based_salary_slip_employee()
Kanchan Chauhandb197d52016-08-20 00:30:59 +0530125 for e in employees:
Zlash65796bffb2018-09-26 12:27:09 +0530126 ts = make_timesheet(e.employee, simulate = True, billable = 1, activity_type=get_random("Activity Type"), company=frappe.flags.company)
Rushabh Mehta397e5082017-01-21 16:57:24 +0530127 frappe.db.commit()
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530128
129 rand = random.random()
130 if rand >= 0.3:
131 make_salary_slip_for_timesheet(ts.name)
132
133 rand = random.random()
134 if rand >= 0.2:
135 make_sales_invoice_for_timesheet(ts.name)
136
137def make_salary_slip_for_timesheet(name):
138 salary_slip = make_salary_slip(name)
139 salary_slip.insert()
140 salary_slip.submit()
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530141 frappe.db.commit()
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530142
143def make_sales_invoice_for_timesheet(name):
144 sales_invoice = make_sales_invoice(name)
145 sales_invoice.customer = get_random("Customer")
146 sales_invoice.append('items', {
Rushabh Mehta397e5082017-01-21 16:57:24 +0530147 'item_code': get_random("Item", {"has_variants": 0, "is_stock_item": 0,
148 "is_fixed_asset": 0}),
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530149 'qty': 1,
150 'rate': 1000
151 })
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530152 sales_invoice.flags.ignore_permissions = 1
Rohit Waghchaure8002d472016-07-13 16:03:05 +0530153 sales_invoice.set_missing_values()
154 sales_invoice.calculate_taxes_and_totals()
155 sales_invoice.insert()
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530156 sales_invoice.submit()
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530157 frappe.db.commit()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530158
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530159def make_leave_application():
160 allocated_leaves = frappe.get_all("Leave Allocation", fields=['employee', 'leave_type'])
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530161
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530162 for allocated_leave in allocated_leaves:
163 leave_balance = get_leave_balance_on(allocated_leave.employee, allocated_leave.leave_type, frappe.flags.current_date,
164 consider_all_leaves_in_the_allocation_period=True)
165 if leave_balance != 0:
166 if leave_balance == 1:
167 to_date = frappe.flags.current_date
168 else:
169 to_date = add_days(frappe.flags.current_date, random.randint(0, leave_balance-1))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530170
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530171 leave_application = frappe.get_doc({
172 "doctype": "Leave Application",
173 "employee": allocated_leave.employee,
174 "from_date": frappe.flags.current_date,
175 "to_date": to_date,
176 "leave_type": allocated_leave.leave_type,
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530177 })
178 try:
179 leave_application.insert()
180 leave_application.submit()
181 frappe.db.commit()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530182 except (OverlapError, AttendanceAlreadyMarkedError):
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530183 frappe.db.rollback()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530184
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530185def mark_attendance():
Kanchan Chauhan7933eb62017-01-12 17:24:53 +0530186 attendance_date = frappe.flags.current_date
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530187 for employee in frappe.get_all('Employee', fields=['name'], filters = {'status': 'Active'}):
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530188
Kanchan Chauhan7933eb62017-01-12 17:24:53 +0530189 if not frappe.db.get_value("Attendance", {"employee": employee.name, "attendance_date": attendance_date}):
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530190 attendance = frappe.get_doc({
191 "doctype": "Attendance",
192 "employee": employee.name,
Kanchan Chauhan7933eb62017-01-12 17:24:53 +0530193 "attendance_date": attendance_date
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530194 })
Shreya Shahd9a585b2018-02-12 16:02:57 +0530195
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530196 leave = frappe.db.sql("""select name from `tabLeave Application`
Shreya Shahd9a585b2018-02-12 16:02:57 +0530197 where employee = %s and %s between from_date and to_date
Kanchan Chauhan7933eb62017-01-12 17:24:53 +0530198 and docstatus = 1""", (employee.name, attendance_date))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530199
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530200 if leave:
201 attendance.status = "Absent"
202 else:
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530203 attendance.status = "Present"
Kanchan Chauhandb0e57c2016-07-29 15:59:39 +0530204 attendance.save()
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +0530205 attendance.submit()
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530206 frappe.db.commit()