blob: fbeeff477c0f51ad6a081be3e12ea6761a0869a2 [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
4from __future__ import unicode_literals
5
6import frappe, random
Saurabh90c0b7f2016-07-22 14:15:03 +05307from frappe.utils import flt
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05308from frappe.utils.make_random import add_random_children, get_random
9from erpnext.setup.utils import get_exchange_rate
10from erpnext.accounts.party import get_party_account_currency
Saurabh90c0b7f2016-07-22 14:15:03 +053011from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request, make_payment_entry
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053012
13def work():
14 frappe.set_user(frappe.db.get_global('demo_sales_user_2'))
Zlash65dcf74012018-09-27 18:51:18 +053015
16 for i in range(random.randint(1,7)):
17 if random.random() < 0.5:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053018 make_opportunity()
19
Zlash65dcf74012018-09-27 18:51:18 +053020 for i in range(random.randint(1,3)):
21 if random.random() < 0.5:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053022 make_quotation()
23
24 # lost quotations / inquiries
25 if random.random() < 0.3:
Achilles Rasquinha96698c92018-02-28 16:12:51 +053026 for i in range(random.randint(1,3)):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053027 quotation = get_random('Quotation', doc=True)
28 if quotation and quotation.status == 'Submitted':
29 quotation.declare_order_lost('Did not ask')
30
Achilles Rasquinha96698c92018-02-28 16:12:51 +053031 for i in range(random.randint(1,3)):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053032 opportunity = get_random('Opportunity', doc=True)
33 if opportunity and opportunity.status in ('Open', 'Replied'):
34 opportunity.declare_enquiry_lost('Did not ask')
35
Zlash65dcf74012018-09-27 18:51:18 +053036 for i in range(random.randint(1,3)):
Zlash65d080a172018-10-01 10:49:06 +053037 if random.random() < 0.6:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053038 make_sales_order()
39
Zlash65dcf74012018-09-27 18:51:18 +053040 if random.random() < 0.5:
Saurabh90c0b7f2016-07-22 14:15:03 +053041 #make payment request against Sales Order
42 sales_order_name = get_random("Sales Order", filters={"docstatus": 1})
Zlash65d080a172018-10-01 10:49:06 +053043 try:
44 if sales_order_name:
45 so = frappe.get_doc("Sales Order", sales_order_name)
46 if flt(so.per_billed) != 100:
47 payment_request = make_payment_request(dt="Sales Order", dn=so.name, recipient_id=so.contact_email,
48 submit_doc=True, mute_email=True, use_dummy_message=True)
Saurabh90c0b7f2016-07-22 14:15:03 +053049
Zlash65d080a172018-10-01 10:49:06 +053050 payment_entry = frappe.get_doc(make_payment_entry(payment_request.name))
51 payment_entry.posting_date = frappe.flags.current_date
52 payment_entry.submit()
53 except Exception:
54 pass
Saurabh90c0b7f2016-07-22 14:15:03 +053055
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053056def make_opportunity():
57 b = frappe.get_doc({
58 "doctype": "Opportunity",
59 "enquiry_from": "Customer",
60 "customer": get_random("Customer"),
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053061 "opportunity_type": "Sales",
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053062 "with_items": 1,
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053063 "transaction_date": frappe.flags.current_date,
64 })
65
66 add_random_children(b, "items", rows=4, randomize = {
67 "qty": (1, 5),
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053068 "item_code": ("Item", {"has_variants": 0, "is_fixed_asset": 0})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053069 }, unique="item_code")
70
71 b.insert()
72 frappe.db.commit()
73
74def make_quotation():
75 # get open opportunites
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053076 opportunity = get_random("Opportunity", {"status": "Open", "with_items": 1})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053077
78 if opportunity:
79 from erpnext.crm.doctype.opportunity.opportunity import make_quotation
80 qtn = frappe.get_doc(make_quotation(opportunity))
81 qtn.insert()
82 frappe.db.commit()
83 qtn.submit()
84 frappe.db.commit()
85 else:
86 # make new directly
87
88 # get customer, currency and exchange_rate
89 customer = get_random("Customer")
90
Rushabh Mehta708e47a2018-08-08 16:37:31 +053091 company_currency = frappe.get_cached_value('Company', "Wind Power LLC", "default_currency")
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053092 party_account_currency = get_party_account_currency("Customer", customer, "Wind Power LLC")
93 if company_currency == party_account_currency:
94 exchange_rate = 1
95 else:
Shreya3f778522018-05-15 16:59:20 +053096 exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_selling")
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053097
98 qtn = frappe.get_doc({
99 "creation": frappe.flags.current_date,
100 "doctype": "Quotation",
101 "quotation_to": "Customer",
102 "customer": customer,
103 "currency": party_account_currency or company_currency,
104 "conversion_rate": exchange_rate,
105 "order_type": "Sales",
106 "transaction_date": frappe.flags.current_date,
107 })
108
109 add_random_children(qtn, "items", rows=3, randomize = {
110 "qty": (1, 5),
Nabin Hait26cad302016-07-21 10:28:54 +0530111 "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530112 }, unique="item_code")
113
114 qtn.insert()
115 frappe.db.commit()
116 qtn.submit()
117 frappe.db.commit()
118
119def make_sales_order():
120 q = get_random("Quotation", {"status": "Submitted"})
121 if q:
Zlash65dcf74012018-09-27 18:51:18 +0530122 from erpnext.selling.doctype.quotation.quotation import make_sales_order as mso
123 so = frappe.get_doc(mso(q))
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530124 so.transaction_date = frappe.flags.current_date
Nabin Hait495ef672017-07-31 11:25:51 +0530125 so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530126 so.insert()
127 frappe.db.commit()
128 so.submit()
129 frappe.db.commit()