blob: 86fb52630bd54e02efe57a8483418bc398726e21 [file] [log] [blame]
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05301from __future__ import unicode_literals
2import frappe
Rohit Waghchaure8002d472016-07-13 16:03:05 +05303import random
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05304from frappe.utils import random_string
Rohit Waghchaure8002d472016-07-13 16:03:05 +05305from erpnext.projects.doctype.timesheet.test_timesheet import make_timesheet
6from erpnext.projects.doctype.timesheet.timesheet import make_salary_slip, make_sales_invoice
7from frappe.utils.make_random import how_many, get_random
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05308
9def work():
10 frappe.set_user(frappe.db.get_global('demo_hr_user'))
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053011 year, month = frappe.flags.current_date.strftime("%Y-%m").split("-")
12
13 # process payroll
14 if not frappe.db.get_value("Salary Slip", {"month": month, "fiscal_year": year}):
15 process_payroll = frappe.get_doc("Process Payroll", "Process Payroll")
16 process_payroll.company = frappe.flags.company
17 process_payroll.month = month
18 process_payroll.fiscal_year = year
19 process_payroll.create_sal_slip()
20 process_payroll.submit_salary_slip()
21 r = process_payroll.make_journal_entry(frappe.get_value('Account',
22 {'account_name': 'Salary'}))
23
24 journal_entry = frappe.get_doc(r)
25 journal_entry.cheque_no = random_string(10)
26 journal_entry.cheque_date = frappe.flags.current_date
27 journal_entry.posting_date = frappe.flags.current_date
28 journal_entry.insert()
29 journal_entry.submit()
Rohit Waghchaure8002d472016-07-13 16:03:05 +053030
31 if frappe.db.get_global('demo_hr_user'):
32 make_timesheet_records()
33
34def get_timesheet_based_salary_slip_employee():
35 return frappe.get_all('Salary Structure', fields = ["distinct employee as name"],
36 filters = {'salary_slip_based_on_timesheet': 1})
37
38def make_timesheet_records():
39 employees = get_timesheet_based_salary_slip_employee()
40 for employee in employees:
41 ts = make_timesheet(employee.name, simulate = True, billable = 1, activity_type=get_random("Activity Type"))
42
43 rand = random.random()
44 if rand >= 0.3:
45 make_salary_slip_for_timesheet(ts.name)
46
47 rand = random.random()
48 if rand >= 0.2:
49 make_sales_invoice_for_timesheet(ts.name)
50
51def make_salary_slip_for_timesheet(name):
52 salary_slip = make_salary_slip(name)
53 salary_slip.insert()
54 salary_slip.submit()
55
56def make_sales_invoice_for_timesheet(name):
57 sales_invoice = make_sales_invoice(name)
58 sales_invoice.customer = get_random("Customer")
59 sales_invoice.append('items', {
60 'item_code': get_random_item(),
61 'qty': 1,
62 'rate': 1000
63 })
64 sales_invoice.set_missing_values()
65 sales_invoice.calculate_taxes_and_totals()
66 sales_invoice.insert()
67 sales_invoice.submit()
68
69def get_random_item():
70 return frappe.db.sql_list(""" select name from `tabItem` where
71 has_variants = 0 order by rand() limit 1""")[0]