Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 3 | import random |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 4 | from frappe.utils import random_string |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 5 | from erpnext.projects.doctype.timesheet.test_timesheet import make_timesheet |
| 6 | from erpnext.projects.doctype.timesheet.timesheet import make_salary_slip, make_sales_invoice |
| 7 | from frappe.utils.make_random import how_many, get_random |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 8 | from erpnext.hr.doctype.expense_claim.expense_claim import get_expense_approver, make_bank_entry |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 9 | |
| 10 | def work(): |
| 11 | frappe.set_user(frappe.db.get_global('demo_hr_user')) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 12 | year, month = frappe.flags.current_date.strftime("%Y-%m").split("-") |
| 13 | |
| 14 | # process payroll |
| 15 | if not frappe.db.get_value("Salary Slip", {"month": month, "fiscal_year": year}): |
| 16 | process_payroll = frappe.get_doc("Process Payroll", "Process Payroll") |
| 17 | process_payroll.company = frappe.flags.company |
| 18 | process_payroll.month = month |
| 19 | process_payroll.fiscal_year = year |
| 20 | process_payroll.create_sal_slip() |
| 21 | process_payroll.submit_salary_slip() |
| 22 | r = process_payroll.make_journal_entry(frappe.get_value('Account', |
| 23 | {'account_name': 'Salary'})) |
| 24 | |
| 25 | journal_entry = frappe.get_doc(r) |
| 26 | journal_entry.cheque_no = random_string(10) |
| 27 | journal_entry.cheque_date = frappe.flags.current_date |
| 28 | journal_entry.posting_date = frappe.flags.current_date |
| 29 | journal_entry.insert() |
| 30 | journal_entry.submit() |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 31 | |
| 32 | if frappe.db.get_global('demo_hr_user'): |
| 33 | make_timesheet_records() |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 34 | |
| 35 | #expense claim |
| 36 | expense_claim = frappe.new_doc("Expense Claim") |
| 37 | expense_claim.extend('expenses', get_expenses()) |
| 38 | expense_claim.employee = get_random("Employee") |
| 39 | expense_claim.company = frappe.flags.company |
| 40 | expense_claim.posting_date = frappe.flags.current_date |
| 41 | expense_claim.exp_approver = filter((lambda x: x[0] != 'Administrator'), get_expense_approver(None, '', None, 0, 20, None))[0][0] |
| 42 | expense_claim.insert() |
| 43 | |
| 44 | rand = random.random() |
| 45 | |
Saurabh | 9cba6e1 | 2016-07-18 16:22:51 +0530 | [diff] [blame] | 46 | if rand < 0.4: |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 47 | expense_claim.approval_status = "Approved" |
| 48 | update_sanctioned_amount(expense_claim) |
| 49 | expense_claim.submit() |
| 50 | |
| 51 | if random.randint(0, 1): |
| 52 | #make journal entry against expense claim |
| 53 | je = frappe.get_doc(make_bank_entry(expense_claim.name)) |
| 54 | je.posting_date = frappe.flags.current_date |
| 55 | je.cheque_no = random_string(10) |
| 56 | je.cheque_date = frappe.flags.current_date |
| 57 | je.flags.ignore_permissions = 1 |
| 58 | je.submit() |
| 59 | |
Saurabh | 9cba6e1 | 2016-07-18 16:22:51 +0530 | [diff] [blame] | 60 | elif rand < 0.2: |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 61 | expense_claim.approval_status = "Rejected" |
| 62 | expense_claim.submit() |
| 63 | |
| 64 | def get_expenses(): |
| 65 | expenses = [] |
Saurabh | 718d835 | 2016-07-18 15:20:47 +0530 | [diff] [blame] | 66 | expese_types = frappe.db.sql("""select ect.name, eca.default_account from `tabExpense Claim Type` ect, |
| 67 | `tabExpense Claim Account` eca where eca.parent=ect.name |
| 68 | and eca.company=%s """, frappe.flags.company,as_dict=1) |
Saurabh | f589c82 | 2016-07-15 18:28:05 +0530 | [diff] [blame] | 69 | |
| 70 | for expense_type in expese_types[:random.randint(1,4)]: |
| 71 | claim_amount = random.randint(1,20)*10 |
| 72 | |
| 73 | expenses.append({ |
| 74 | "expense_date": frappe.flags.current_date, |
| 75 | "expense_type": expense_type.name, |
| 76 | "default_account": expense_type.default_account or "Miscellaneous Expenses - WPL", |
| 77 | "claim_amount": claim_amount, |
| 78 | "sanctioned_amount": claim_amount |
| 79 | }) |
| 80 | |
| 81 | return expenses |
| 82 | |
| 83 | def update_sanctioned_amount(expense_claim): |
| 84 | for expense in expense_claim.expenses: |
| 85 | sanctioned_amount = random.randint(1,20)*10 |
| 86 | |
| 87 | if sanctioned_amount < expense.claim_amount: |
| 88 | expense.sanctioned_amount = sanctioned_amount |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 89 | |
| 90 | def get_timesheet_based_salary_slip_employee(): |
| 91 | return frappe.get_all('Salary Structure', fields = ["distinct employee as name"], |
| 92 | filters = {'salary_slip_based_on_timesheet': 1}) |
| 93 | |
| 94 | def make_timesheet_records(): |
| 95 | employees = get_timesheet_based_salary_slip_employee() |
| 96 | for employee in employees: |
| 97 | ts = make_timesheet(employee.name, simulate = True, billable = 1, activity_type=get_random("Activity Type")) |
| 98 | |
| 99 | rand = random.random() |
| 100 | if rand >= 0.3: |
| 101 | make_salary_slip_for_timesheet(ts.name) |
| 102 | |
| 103 | rand = random.random() |
| 104 | if rand >= 0.2: |
| 105 | make_sales_invoice_for_timesheet(ts.name) |
| 106 | |
| 107 | def make_salary_slip_for_timesheet(name): |
| 108 | salary_slip = make_salary_slip(name) |
| 109 | salary_slip.insert() |
| 110 | salary_slip.submit() |
Rohit Waghchaure | 7d439ec | 2016-07-21 14:50:59 +0530 | [diff] [blame] | 111 | frappe.db.commit() |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 112 | |
| 113 | def make_sales_invoice_for_timesheet(name): |
| 114 | sales_invoice = make_sales_invoice(name) |
| 115 | sales_invoice.customer = get_random("Customer") |
| 116 | sales_invoice.append('items', { |
Nabin Hait | 74edfff | 2016-07-21 11:39:46 +0530 | [diff] [blame] | 117 | 'item_code': get_random("Item", {"has_variants": 0, "is_stock_item": 0, "is_fixed_asset": 0}), |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 118 | 'qty': 1, |
| 119 | 'rate': 1000 |
| 120 | }) |
Rohit Waghchaure | 7d439ec | 2016-07-21 14:50:59 +0530 | [diff] [blame] | 121 | sales_invoice.flags.ignore_permissions = 1 |
Rohit Waghchaure | 8002d47 | 2016-07-13 16:03:05 +0530 | [diff] [blame] | 122 | sales_invoice.set_missing_values() |
| 123 | sales_invoice.calculate_taxes_and_totals() |
| 124 | sales_invoice.insert() |
Rohit Waghchaure | 7d439ec | 2016-07-21 14:50:59 +0530 | [diff] [blame] | 125 | sales_invoice.submit() |
| 126 | frappe.db.commit() |