blob: ef6e4c42cd8235992eb5e1623b776ae69c851363 [file] [log] [blame]
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05304
Chillar Anand915b3432021-09-02 16:44:59 +05305import random
6
7import frappe
Saurabh90c0b7f2016-07-22 14:15:03 +05308from frappe.utils import flt
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05309from frappe.utils.make_random import add_random_children, get_random
Chillar Anand915b3432021-09-02 16:44:59 +053010
11import erpnext
12from erpnext.accounts.doctype.payment_request.payment_request import (
13 make_payment_entry,
14 make_payment_request,
15)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053016from erpnext.accounts.party import get_party_account_currency
Chillar Anand915b3432021-09-02 16:44:59 +053017from erpnext.setup.utils import get_exchange_rate
18
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053019
Zlash65fd9c4512018-10-10 14:27:07 +053020def work(domain="Manufacturing"):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053021 frappe.set_user(frappe.db.get_global('demo_sales_user_2'))
Zlash65dcf74012018-09-27 18:51:18 +053022
23 for i in range(random.randint(1,7)):
24 if random.random() < 0.5:
Zlash65fd9c4512018-10-10 14:27:07 +053025 make_opportunity(domain)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053026
Zlash65dcf74012018-09-27 18:51:18 +053027 for i in range(random.randint(1,3)):
28 if random.random() < 0.5:
Zlash65fd9c4512018-10-10 14:27:07 +053029 make_quotation(domain)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053030
Chinmay Paib5038962019-04-27 01:39:50 +053031 try:
32 lost_reason = frappe.get_doc({
33 "doctype": "Opportunity Lost Reason",
34 "lost_reason": "Did not ask"
35 })
36 lost_reason.save(ignore_permissions=True)
37 except frappe.exceptions.DuplicateEntryError:
38 pass
39
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053040 # lost quotations / inquiries
41 if random.random() < 0.3:
Achilles Rasquinha96698c92018-02-28 16:12:51 +053042 for i in range(random.randint(1,3)):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053043 quotation = get_random('Quotation', doc=True)
44 if quotation and quotation.status == 'Submitted':
Chinmay Paib5038962019-04-27 01:39:50 +053045 quotation.declare_order_lost([{'lost_reason': 'Did not ask'}])
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053046
Achilles Rasquinha96698c92018-02-28 16:12:51 +053047 for i in range(random.randint(1,3)):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053048 opportunity = get_random('Opportunity', doc=True)
49 if opportunity and opportunity.status in ('Open', 'Replied'):
Chinmay Paib5038962019-04-27 01:39:50 +053050 opportunity.declare_enquiry_lost([{'lost_reason': 'Did not ask'}])
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053051
Zlash65dcf74012018-09-27 18:51:18 +053052 for i in range(random.randint(1,3)):
Zlash65d080a172018-10-01 10:49:06 +053053 if random.random() < 0.6:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053054 make_sales_order()
55
Zlash65dcf74012018-09-27 18:51:18 +053056 if random.random() < 0.5:
Saurabh90c0b7f2016-07-22 14:15:03 +053057 #make payment request against Sales Order
58 sales_order_name = get_random("Sales Order", filters={"docstatus": 1})
Zlash65d080a172018-10-01 10:49:06 +053059 try:
60 if sales_order_name:
61 so = frappe.get_doc("Sales Order", sales_order_name)
62 if flt(so.per_billed) != 100:
63 payment_request = make_payment_request(dt="Sales Order", dn=so.name, recipient_id=so.contact_email,
64 submit_doc=True, mute_email=True, use_dummy_message=True)
Saurabh90c0b7f2016-07-22 14:15:03 +053065
Zlash65d080a172018-10-01 10:49:06 +053066 payment_entry = frappe.get_doc(make_payment_entry(payment_request.name))
67 payment_entry.posting_date = frappe.flags.current_date
68 payment_entry.submit()
69 except Exception:
70 pass
Saurabh90c0b7f2016-07-22 14:15:03 +053071
Zlash65fd9c4512018-10-10 14:27:07 +053072def make_opportunity(domain):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053073 b = frappe.get_doc({
74 "doctype": "Opportunity",
Nabin Hait34c551d2019-07-03 10:34:31 +053075 "opportunity_from": "Customer",
deepeshgarg0073db65ef2019-10-15 19:16:45 +053076 "party_name": frappe.get_value("Customer", get_random("Customer"), 'name'),
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053077 "opportunity_type": "Sales",
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053078 "with_items": 1,
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053079 "transaction_date": frappe.flags.current_date,
80 })
81
82 add_random_children(b, "items", rows=4, randomize = {
83 "qty": (1, 5),
Zlash65fd9c4512018-10-10 14:27:07 +053084 "item_code": ("Item", {"has_variants": 0, "is_fixed_asset": 0, "domain": domain})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053085 }, unique="item_code")
86
87 b.insert()
88 frappe.db.commit()
89
Zlash65fd9c4512018-10-10 14:27:07 +053090def make_quotation(domain):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053091 # get open opportunites
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053092 opportunity = get_random("Opportunity", {"status": "Open", "with_items": 1})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053093
94 if opportunity:
95 from erpnext.crm.doctype.opportunity.opportunity import make_quotation
96 qtn = frappe.get_doc(make_quotation(opportunity))
97 qtn.insert()
98 frappe.db.commit()
99 qtn.submit()
100 frappe.db.commit()
101 else:
102 # make new directly
103
104 # get customer, currency and exchange_rate
105 customer = get_random("Customer")
106
Zlash65fd9c4512018-10-10 14:27:07 +0530107 company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency")
108 party_account_currency = get_party_account_currency("Customer", customer, erpnext.get_default_company())
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530109 if company_currency == party_account_currency:
110 exchange_rate = 1
111 else:
Shreya3f778522018-05-15 16:59:20 +0530112 exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_selling")
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530113
114 qtn = frappe.get_doc({
115 "creation": frappe.flags.current_date,
116 "doctype": "Quotation",
117 "quotation_to": "Customer",
Nabin Hait34c551d2019-07-03 10:34:31 +0530118 "party_name": customer,
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530119 "currency": party_account_currency or company_currency,
120 "conversion_rate": exchange_rate,
121 "order_type": "Sales",
122 "transaction_date": frappe.flags.current_date,
123 })
124
125 add_random_children(qtn, "items", rows=3, randomize = {
126 "qty": (1, 5),
Zlash65fd9c4512018-10-10 14:27:07 +0530127 "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0, "domain": domain})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530128 }, unique="item_code")
129
130 qtn.insert()
131 frappe.db.commit()
132 qtn.submit()
133 frappe.db.commit()
134
135def make_sales_order():
136 q = get_random("Quotation", {"status": "Submitted"})
137 if q:
Zlash65dcf74012018-09-27 18:51:18 +0530138 from erpnext.selling.doctype.quotation.quotation import make_sales_order as mso
139 so = frappe.get_doc(mso(q))
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530140 so.transaction_date = frappe.flags.current_date
Nabin Hait495ef672017-07-31 11:25:51 +0530141 so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530142 so.insert()
143 frappe.db.commit()
144 so.submit()
145 frappe.db.commit()