blob: 10df14334fcdf7d1e9b59a77b2f145e8eaef5905 [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'))
15 if random.random() < 0.5:
16 for i in xrange(random.randint(1,7)):
17 make_opportunity()
18
19 if random.random() < 0.5:
20 for i in xrange(random.randint(1,3)):
21 make_quotation()
22
23 # lost quotations / inquiries
24 if random.random() < 0.3:
25 for i in xrange(random.randint(1,3)):
26 quotation = get_random('Quotation', doc=True)
27 if quotation and quotation.status == 'Submitted':
28 quotation.declare_order_lost('Did not ask')
29
30 for i in xrange(random.randint(1,3)):
31 opportunity = get_random('Opportunity', doc=True)
32 if opportunity and opportunity.status in ('Open', 'Replied'):
33 opportunity.declare_enquiry_lost('Did not ask')
34
35 if random.random() < 0.3:
36 for i in xrange(random.randint(1,3)):
37 make_sales_order()
38
Saurabh90c0b7f2016-07-22 14:15:03 +053039 if random.random() < 0.1:
40 #make payment request against Sales Order
41 sales_order_name = get_random("Sales Order", filters={"docstatus": 1})
42 if sales_order_name:
43 so = frappe.get_doc("Sales Order", sales_order_name)
Saurabh6e0a00b2016-07-22 15:17:17 +053044 if flt(so.per_billed) != 100:
Saurabh90c0b7f2016-07-22 14:15:03 +053045 payment_request = make_payment_request(dt="Sales Order", dn=so.name, recipient_id=so.contact_email,
46 submit_doc=True, mute_email=True, use_dummy_message=True)
47
Saurabh6e0a00b2016-07-22 15:17:17 +053048 payment_entry = frappe.get_doc(make_payment_entry(payment_request.name))
49 payment_entry.posting_date = frappe.flags.current_date
50 payment_entry.submit()
Saurabh90c0b7f2016-07-22 14:15:03 +053051
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053052def make_opportunity():
53 b = frappe.get_doc({
54 "doctype": "Opportunity",
55 "enquiry_from": "Customer",
56 "customer": get_random("Customer"),
57 "enquiry_type": "Sales",
58 "transaction_date": frappe.flags.current_date,
59 })
60
61 add_random_children(b, "items", rows=4, randomize = {
62 "qty": (1, 5),
Nabin Hait26cad302016-07-21 10:28:54 +053063 "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053064 }, unique="item_code")
65
66 b.insert()
67 frappe.db.commit()
68
69def make_quotation():
70 # get open opportunites
71 opportunity = get_random("Opportunity", {"status": "Open"})
72
73 if opportunity:
74 from erpnext.crm.doctype.opportunity.opportunity import make_quotation
75 qtn = frappe.get_doc(make_quotation(opportunity))
76 qtn.insert()
77 frappe.db.commit()
78 qtn.submit()
79 frappe.db.commit()
80 else:
81 # make new directly
82
83 # get customer, currency and exchange_rate
84 customer = get_random("Customer")
85
86 company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency")
87 party_account_currency = get_party_account_currency("Customer", customer, "Wind Power LLC")
88 if company_currency == party_account_currency:
89 exchange_rate = 1
90 else:
91 exchange_rate = get_exchange_rate(party_account_currency, company_currency)
92
93 qtn = frappe.get_doc({
94 "creation": frappe.flags.current_date,
95 "doctype": "Quotation",
96 "quotation_to": "Customer",
97 "customer": customer,
98 "currency": party_account_currency or company_currency,
99 "conversion_rate": exchange_rate,
100 "order_type": "Sales",
101 "transaction_date": frappe.flags.current_date,
102 })
103
104 add_random_children(qtn, "items", rows=3, randomize = {
105 "qty": (1, 5),
Nabin Hait26cad302016-07-21 10:28:54 +0530106 "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530107 }, unique="item_code")
108
109 qtn.insert()
110 frappe.db.commit()
111 qtn.submit()
112 frappe.db.commit()
113
114def make_sales_order():
115 q = get_random("Quotation", {"status": "Submitted"})
116 if q:
117 from erpnext.selling.doctype.quotation.quotation import make_sales_order
118 so = frappe.get_doc(make_sales_order(q))
119 so.transaction_date = frappe.flags.current_date
120 so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10)
121 so.insert()
122 frappe.db.commit()
123 so.submit()
124 frappe.db.commit()