blob: 052d4560ef1c97034b28a0da37c4b4fab405f345 [file] [log] [blame]
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +05301
2# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
3# License: GNU General Public License v3. See license.txt
4
5from __future__ import unicode_literals
6
7import frappe
8import random
9from frappe.utils import random_string
10from frappe.desk import query_report
Nabin Hait26cad302016-07-21 10:28:54 +053011from erpnext.accounts.doctype.journal_entry.journal_entry import get_payment_entry_against_invoice
12from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
Saurabh90c0b7f2016-07-22 14:15:03 +053013from frappe.utils.make_random import get_random
14from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request, make_payment_entry
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053015
16def work():
17 frappe.set_user(frappe.db.get_global('demo_accounts_user'))
18
Nabin Hait26cad302016-07-21 10:28:54 +053019 if random.random() <= 0.6:
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053020 from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
21 report = "Ordered Items to be Billed"
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +053022 for so in list(set([r[0] for r in query_report.run(report)["result"]
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053023 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 Hait26cad302016-07-21 10:28:54 +053033 if random.random() <= 0.6:
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053034 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 Hait16066262016-07-21 11:00:28 +053045 if random.random() < 0.5:
Nabin Hait26cad302016-07-21 10:28:54 +053046 make_payment_entries("Sales Invoice", "Accounts Receivable")
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053047
Nabin Hait16066262016-07-21 11:00:28 +053048 if random.random() < 0.5:
Nabin Hait26cad302016-07-21 10:28:54 +053049 make_payment_entries("Purchase Invoice", "Accounts Payable")
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053050
Saurabh90c0b7f2016-07-22 14:15:03 +053051 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
Saurabh6e0a00b2016-07-22 15:17:17 +053060 payment_entry = frappe.get_doc(make_payment_entry(payment_request.name))
61 payment_entry.posting_date = frappe.flags.current_date
62 payment_entry.submit()
Saurabh90c0b7f2016-07-22 14:15:03 +053063
Nabin Hait26cad302016-07-21 10:28:54 +053064def make_payment_entries(ref_doctype, report):
Saurabh90c0b7f2016-07-22 14:15:03 +053065 outstanding_invoices = list(set([r[3] for r in query_report.run(report,
Nabin Hait26cad302016-07-21 10:28:54 +053066 {"report_date": frappe.flags.current_date })["result"] if r[2]==ref_doctype]))
Saurabh6e0a00b2016-07-22 15:17:17 +053067
Nabin Hait26cad302016-07-21 10:28:54 +053068 # make Payment Entry
Nabin Hait16066262016-07-21 11:00:28 +053069 for inv in outstanding_invoices[:random.randint(1, 2)]:
Nabin Hait26cad302016-07-21 10:28:54 +053070 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 Hait16066262016-07-21 11:00:28 +053076 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()
Saurabh90c0b7f2016-07-22 14:15:03 +053087 frappe.db.commit()