blob: 2a248244850df85779a1418fa2f5750ce9683b19 [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
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +05307import random
Chillar Anand915b3432021-09-02 16:44:59 +05308
9import frappe
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053010from frappe.desk import query_report
Chillar Anand915b3432021-09-02 16:44:59 +053011from frappe.utils import random_string
12from frappe.utils.make_random import get_random
13
14import erpnext
Nabin Hait26cad302016-07-21 10:28:54 +053015from erpnext.accounts.doctype.journal_entry.journal_entry import get_payment_entry_against_invoice
16from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
Chillar Anand915b3432021-09-02 16:44:59 +053017from erpnext.accounts.doctype.payment_request.payment_request import (
18 make_payment_entry,
19 make_payment_request,
20)
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +053021from erpnext.demo.user.sales import make_sales_order
22from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
23from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053024
Chillar Anand915b3432021-09-02 16:44:59 +053025
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053026def work():
27 frappe.set_user(frappe.db.get_global('demo_accounts_user'))
28
Nabin Hait26cad302016-07-21 10:28:54 +053029 if random.random() <= 0.6:
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053030 report = "Ordered Items to be Billed"
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +053031 for so in list(set([r[0] for r in query_report.run(report)["result"]
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053032 if r[0]!="Total"]))[:random.randint(1, 5)]:
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053033 try:
34 si = frappe.get_doc(make_sales_invoice(so))
35 si.posting_date = frappe.flags.current_date
36 for d in si.get("items"):
37 if not d.income_account:
Rushabh Mehta708e47a2018-08-08 16:37:31 +053038 d.income_account = "Sales - {}".format(frappe.get_cached_value('Company', si.company, 'abbr'))
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053039 si.insert()
40 si.submit()
41 frappe.db.commit()
42 except frappe.ValidationError:
43 pass
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053044
Nabin Hait26cad302016-07-21 10:28:54 +053045 if random.random() <= 0.6:
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053046 report = "Received Items to be Billed"
47 for pr in list(set([r[0] for r in query_report.run(report)["result"]
48 if r[0]!="Total"]))[:random.randint(1, 5)]:
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053049 try:
50 pi = frappe.get_doc(make_purchase_invoice(pr))
51 pi.posting_date = frappe.flags.current_date
52 pi.bill_no = random_string(6)
53 pi.insert()
54 pi.submit()
55 frappe.db.commit()
56 except frappe.ValidationError:
57 pass
58
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053059
Nabin Hait16066262016-07-21 11:00:28 +053060 if random.random() < 0.5:
Nabin Hait26cad302016-07-21 10:28:54 +053061 make_payment_entries("Sales Invoice", "Accounts Receivable")
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053062
Nabin Hait16066262016-07-21 11:00:28 +053063 if random.random() < 0.5:
Nabin Hait26cad302016-07-21 10:28:54 +053064 make_payment_entries("Purchase Invoice", "Accounts Payable")
Rushabh Mehta8cfe18e2016-07-13 11:29:59 +053065
Zlash65dcf74012018-09-27 18:51:18 +053066 if random.random() < 0.4:
Saurabh90c0b7f2016-07-22 14:15:03 +053067 #make payment request against sales invoice
68 sales_invoice_name = get_random("Sales Invoice", filters={"docstatus": 1})
69 if sales_invoice_name:
70 si = frappe.get_doc("Sales Invoice", sales_invoice_name)
71 if si.outstanding_amount > 0:
72 payment_request = make_payment_request(dt="Sales Invoice", dn=si.name, recipient_id=si.contact_email,
73 submit_doc=True, mute_email=True, use_dummy_message=True)
74
Saurabh6e0a00b2016-07-22 15:17:17 +053075 payment_entry = frappe.get_doc(make_payment_entry(payment_request.name))
76 payment_entry.posting_date = frappe.flags.current_date
77 payment_entry.submit()
Saurabh90c0b7f2016-07-22 14:15:03 +053078
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +053079 make_pos_invoice()
80
Nabin Hait26cad302016-07-21 10:28:54 +053081def make_payment_entries(ref_doctype, report):
deepeshgarg0073db65ef2019-10-15 19:16:45 +053082
83 outstanding_invoices = frappe.get_all(ref_doctype, fields=["name"],
84 filters={
85 "company": erpnext.get_default_company(),
86 "outstanding_amount": (">", 0.0)
87 })
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053088
Nabin Hait26cad302016-07-21 10:28:54 +053089 # make Payment Entry
Nabin Hait16066262016-07-21 11:00:28 +053090 for inv in outstanding_invoices[:random.randint(1, 2)]:
deepeshgarg0073db65ef2019-10-15 19:16:45 +053091 pe = get_payment_entry(ref_doctype, inv.name)
Nabin Hait26cad302016-07-21 10:28:54 +053092 pe.posting_date = frappe.flags.current_date
93 pe.reference_no = random_string(6)
94 pe.reference_date = frappe.flags.current_date
95 pe.insert()
96 pe.submit()
Nabin Hait16066262016-07-21 11:00:28 +053097 frappe.db.commit()
98 outstanding_invoices.remove(inv)
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +053099
Nabin Hait16066262016-07-21 11:00:28 +0530100 # make payment via JV
101 for inv in outstanding_invoices[:1]:
deepeshgarg0073db65ef2019-10-15 19:16:45 +0530102 jv = frappe.get_doc(get_payment_entry_against_invoice(ref_doctype, inv.name))
Nabin Hait16066262016-07-21 11:00:28 +0530103 jv.posting_date = frappe.flags.current_date
104 jv.cheque_no = random_string(6)
105 jv.cheque_date = frappe.flags.current_date
106 jv.insert()
107 jv.submit()
Saurabh90c0b7f2016-07-22 14:15:03 +0530108 frappe.db.commit()
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530109
110def make_pos_invoice():
111 make_sales_order()
112
113 for data in frappe.get_all('Sales Order', fields=["name"],
114 filters = [["per_billed", "<", "100"]]):
115 si = frappe.get_doc(make_sales_invoice(data.name))
116 si.is_pos =1
117 si.posting_date = frappe.flags.current_date
118 for d in si.get("items"):
119 if not d.income_account:
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530120 d.income_account = "Sales - {}".format(frappe.get_cached_value('Company', si.company, 'abbr'))
Rohit Waghchaure7d439ec2016-07-21 14:50:59 +0530121 si.set_missing_values()
122 make_payment_entries_for_pos_invoice(si)
123 si.insert()
124 si.submit()
125
126def make_payment_entries_for_pos_invoice(si):
127 for data in si.payments:
128 data.amount = si.outstanding_amount
129 return