Rushabh Mehta | 8cfe18e | 2016-07-13 11:29:59 +0530 | [diff] [blame] | 1 | |
| 2 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 3 | # License: GNU General Public License v3. See license.txt |
| 4 | |
| 5 | from __future__ import unicode_literals |
| 6 | |
| 7 | import frappe |
| 8 | import random |
| 9 | from frappe.utils import random_string |
| 10 | from frappe.desk import query_report |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 11 | from erpnext.accounts.doctype.journal_entry.journal_entry import get_payment_entry_against_invoice |
| 12 | from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 13 | from frappe.utils.make_random import get_random |
| 14 | from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request, make_payment_entry |
Rushabh Mehta | 8cfe18e | 2016-07-13 11:29:59 +0530 | [diff] [blame] | 15 | |
| 16 | def work(): |
| 17 | frappe.set_user(frappe.db.get_global('demo_accounts_user')) |
| 18 | |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 19 | if random.random() <= 0.6: |
Rushabh Mehta | 8cfe18e | 2016-07-13 11:29:59 +0530 | [diff] [blame] | 20 | from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice |
| 21 | report = "Ordered Items to be Billed" |
Rushabh Mehta | 92d1b8c | 2016-07-14 15:46:12 +0530 | [diff] [blame] | 22 | for so in list(set([r[0] for r in query_report.run(report)["result"] |
Rushabh Mehta | 8cfe18e | 2016-07-13 11:29:59 +0530 | [diff] [blame] | 23 | if r[0]!="Total"]))[:random.randint(1, 5)]: |
| 24 | si = frappe.get_doc(make_sales_invoice(so)) |
| 25 | si.posting_date = frappe.flags.current_date |
| 26 | for d in si.get("items"): |
| 27 | if not d.income_account: |
| 28 | d.income_account = "Sales - {}".format(frappe.db.get_value('Company', si.company, 'abbr')) |
| 29 | si.insert() |
| 30 | si.submit() |
| 31 | frappe.db.commit() |
| 32 | |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 33 | if random.random() <= 0.6: |
Rushabh Mehta | 8cfe18e | 2016-07-13 11:29:59 +0530 | [diff] [blame] | 34 | from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice |
| 35 | report = "Received Items to be Billed" |
| 36 | for pr in list(set([r[0] for r in query_report.run(report)["result"] |
| 37 | if r[0]!="Total"]))[:random.randint(1, 5)]: |
| 38 | pi = frappe.get_doc(make_purchase_invoice(pr)) |
| 39 | pi.posting_date = frappe.flags.current_date |
| 40 | pi.bill_no = random_string(6) |
| 41 | pi.insert() |
| 42 | pi.submit() |
| 43 | frappe.db.commit() |
| 44 | |
Nabin Hait | 1606626 | 2016-07-21 11:00:28 +0530 | [diff] [blame] | 45 | if random.random() < 0.5: |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 46 | make_payment_entries("Sales Invoice", "Accounts Receivable") |
Rushabh Mehta | 8cfe18e | 2016-07-13 11:29:59 +0530 | [diff] [blame] | 47 | |
Nabin Hait | 1606626 | 2016-07-21 11:00:28 +0530 | [diff] [blame] | 48 | if random.random() < 0.5: |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 49 | make_payment_entries("Purchase Invoice", "Accounts Payable") |
Rushabh Mehta | 8cfe18e | 2016-07-13 11:29:59 +0530 | [diff] [blame] | 50 | |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 51 | if random.random() < 0.1: |
| 52 | #make payment request against sales invoice |
| 53 | sales_invoice_name = get_random("Sales Invoice", filters={"docstatus": 1}) |
| 54 | if sales_invoice_name: |
| 55 | si = frappe.get_doc("Sales Invoice", sales_invoice_name) |
| 56 | if si.outstanding_amount > 0: |
| 57 | payment_request = make_payment_request(dt="Sales Invoice", dn=si.name, recipient_id=si.contact_email, |
| 58 | submit_doc=True, mute_email=True, use_dummy_message=True) |
| 59 | |
Saurabh | 6e0a00b | 2016-07-22 15:17:17 +0530 | [diff] [blame] | 60 | payment_entry = frappe.get_doc(make_payment_entry(payment_request.name)) |
| 61 | payment_entry.posting_date = frappe.flags.current_date |
| 62 | payment_entry.submit() |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 63 | |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 64 | def make_payment_entries(ref_doctype, report): |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 65 | outstanding_invoices = list(set([r[3] for r in query_report.run(report, |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 66 | {"report_date": frappe.flags.current_date })["result"] if r[2]==ref_doctype])) |
Saurabh | 6e0a00b | 2016-07-22 15:17:17 +0530 | [diff] [blame] | 67 | |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 68 | # make Payment Entry |
Nabin Hait | 1606626 | 2016-07-21 11:00:28 +0530 | [diff] [blame] | 69 | for inv in outstanding_invoices[:random.randint(1, 2)]: |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 70 | pe = get_payment_entry(ref_doctype, inv) |
| 71 | pe.posting_date = frappe.flags.current_date |
| 72 | pe.reference_no = random_string(6) |
| 73 | pe.reference_date = frappe.flags.current_date |
| 74 | pe.insert() |
| 75 | pe.submit() |
Nabin Hait | 1606626 | 2016-07-21 11:00:28 +0530 | [diff] [blame] | 76 | frappe.db.commit() |
| 77 | outstanding_invoices.remove(inv) |
| 78 | |
| 79 | # make payment via JV |
| 80 | for inv in outstanding_invoices[:1]: |
| 81 | jv = frappe.get_doc(get_payment_entry_against_invoice(ref_doctype, inv)) |
| 82 | jv.posting_date = frappe.flags.current_date |
| 83 | jv.cheque_no = random_string(6) |
| 84 | jv.cheque_date = frappe.flags.current_date |
| 85 | jv.insert() |
| 86 | jv.submit() |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 87 | frappe.db.commit() |