blob: 273a3f92f3bb46eb42d81f897038ba6c52cf807d [file] [log] [blame]
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +05304
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +05305import random
Chillar Anand915b3432021-09-02 16:44:59 +05306
7import frappe
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +05308from frappe.desk import query_report
Chillar Anand915b3432021-09-02 16:44:59 +05309from frappe.utils import random_string
10from frappe.utils.make_random import get_random
11
12import erpnext
Nabin Hait26cad302016-07-21 10:28:54 +053013from erpnext.accounts.doctype.journal_entry.journal_entry import get_payment_entry_against_invoice
14from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
Chillar Anand915b3432021-09-02 16:44:59 +053015from erpnext.accounts.doctype.payment_request.payment_request import (
16 make_payment_entry,
17 make_payment_request,
18)
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +053019from erpnext.demo.user.sales import make_sales_order
20from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
21from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053022
Chillar Anand915b3432021-09-02 16:44:59 +053023
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053024def work():
25 frappe.set_user(frappe.db.get_global('demo_accounts_user'))
26
Nabin Hait26cad302016-07-21 10:28:54 +053027 if random.random() <= 0.6:
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053028 report = "Ordered Items to be Billed"
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +053029 for so in list(set([r[0] for r in query_report.run(report)["result"]
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053030 if r[0]!="Total"]))[:random.randint(1, 5)]:
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053031 try:
32 si = frappe.get_doc(make_sales_invoice(so))
33 si.posting_date = frappe.flags.current_date
34 for d in si.get("items"):
35 if not d.income_account:
Rushabh Mehta708e47a2018-08-08 16:37:31 +053036 d.income_account = "Sales - {}".format(frappe.get_cached_value('Company', si.company, 'abbr'))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053037 si.insert()
38 si.submit()
39 frappe.db.commit()
40 except frappe.ValidationError:
41 pass
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053042
Nabin Hait26cad302016-07-21 10:28:54 +053043 if random.random() <= 0.6:
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053044 report = "Received Items to be Billed"
45 for pr in list(set([r[0] for r in query_report.run(report)["result"]
46 if r[0]!="Total"]))[:random.randint(1, 5)]:
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053047 try:
48 pi = frappe.get_doc(make_purchase_invoice(pr))
49 pi.posting_date = frappe.flags.current_date
50 pi.bill_no = random_string(6)
51 pi.insert()
52 pi.submit()
53 frappe.db.commit()
54 except frappe.ValidationError:
55 pass
56
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053057
Nabin Hait16066262016-07-21 11:00:28 +053058 if random.random() < 0.5:
Nabin Hait26cad302016-07-21 10:28:54 +053059 make_payment_entries("Sales Invoice", "Accounts Receivable")
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053060
Nabin Hait16066262016-07-21 11:00:28 +053061 if random.random() < 0.5:
Nabin Hait26cad302016-07-21 10:28:54 +053062 make_payment_entries("Purchase Invoice", "Accounts Payable")
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053063
Zlash65dcf74012018-09-27 18:51:18 +053064 if random.random() < 0.4:
Saurabh90c0b7f2016-07-22 14:15:03 +053065 #make payment request against sales invoice
66 sales_invoice_name = get_random("Sales Invoice", filters={"docstatus": 1})
67 if sales_invoice_name:
68 si = frappe.get_doc("Sales Invoice", sales_invoice_name)
69 if si.outstanding_amount > 0:
70 payment_request = make_payment_request(dt="Sales Invoice", dn=si.name, recipient_id=si.contact_email,
71 submit_doc=True, mute_email=True, use_dummy_message=True)
72
Saurabh6e0a00b2016-07-22 15:17:17 +053073 payment_entry = frappe.get_doc(make_payment_entry(payment_request.name))
74 payment_entry.posting_date = frappe.flags.current_date
75 payment_entry.submit()
Saurabh90c0b7f2016-07-22 14:15:03 +053076
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +053077 make_pos_invoice()
78
Nabin Hait26cad302016-07-21 10:28:54 +053079def make_payment_entries(ref_doctype, report):
deepeshgarg0073db65ef2019-10-15 19:16:45 +053080
81 outstanding_invoices = frappe.get_all(ref_doctype, fields=["name"],
82 filters={
83 "company": erpnext.get_default_company(),
84 "outstanding_amount": (">", 0.0)
85 })
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053086
Nabin Hait26cad302016-07-21 10:28:54 +053087 # make Payment Entry
Nabin Hait16066262016-07-21 11:00:28 +053088 for inv in outstanding_invoices[:random.randint(1, 2)]:
deepeshgarg0073db65ef2019-10-15 19:16:45 +053089 pe = get_payment_entry(ref_doctype, inv.name)
Nabin Hait26cad302016-07-21 10:28:54 +053090 pe.posting_date = frappe.flags.current_date
91 pe.reference_no = random_string(6)
92 pe.reference_date = frappe.flags.current_date
93 pe.insert()
94 pe.submit()
Nabin Hait16066262016-07-21 11:00:28 +053095 frappe.db.commit()
96 outstanding_invoices.remove(inv)
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +053097
Nabin Hait16066262016-07-21 11:00:28 +053098 # make payment via JV
99 for inv in outstanding_invoices[:1]:
deepeshgarg0073db65ef2019-10-15 19:16:45 +0530100 jv = frappe.get_doc(get_payment_entry_against_invoice(ref_doctype, inv.name))
Nabin Hait16066262016-07-21 11:00:28 +0530101 jv.posting_date = frappe.flags.current_date
102 jv.cheque_no = random_string(6)
103 jv.cheque_date = frappe.flags.current_date
104 jv.insert()
105 jv.submit()
Saurabh90c0b7f2016-07-22 14:15:03 +0530106 frappe.db.commit()
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530107
108def make_pos_invoice():
109 make_sales_order()
110
111 for data in frappe.get_all('Sales Order', fields=["name"],
112 filters = [["per_billed", "<", "100"]]):
113 si = frappe.get_doc(make_sales_invoice(data.name))
114 si.is_pos =1
115 si.posting_date = frappe.flags.current_date
116 for d in si.get("items"):
117 if not d.income_account:
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530118 d.income_account = "Sales - {}".format(frappe.get_cached_value('Company', si.company, 'abbr'))
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530119 si.set_missing_values()
120 make_payment_entries_for_pos_invoice(si)
121 si.insert()
122 si.submit()
123
124def make_payment_entries_for_pos_invoice(si):
125 for data in si.payments:
126 data.amount = si.outstanding_amount
127 return