blob: 95494ee3242b907392b61363a0005fb5e21f5e35 [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
Chillar Anand915b3432021-09-02 16:44:59 +05306import random
7
8import frappe
Saurabh90c0b7f2016-07-22 14:15:03 +05309from frappe.utils import flt
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053010from frappe.utils.make_random import add_random_children, get_random
Chillar Anand915b3432021-09-02 16:44:59 +053011
12import erpnext
13from erpnext.accounts.doctype.payment_request.payment_request import (
14 make_payment_entry,
15 make_payment_request,
16)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053017from erpnext.accounts.party import get_party_account_currency
Chillar Anand915b3432021-09-02 16:44:59 +053018from erpnext.setup.utils import get_exchange_rate
19
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053020
Zlash65fd9c4512018-10-10 14:27:07 +053021def work(domain="Manufacturing"):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053022 frappe.set_user(frappe.db.get_global('demo_sales_user_2'))
Zlash65dcf74012018-09-27 18:51:18 +053023
24 for i in range(random.randint(1,7)):
25 if random.random() < 0.5:
Zlash65fd9c4512018-10-10 14:27:07 +053026 make_opportunity(domain)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053027
Zlash65dcf74012018-09-27 18:51:18 +053028 for i in range(random.randint(1,3)):
29 if random.random() < 0.5:
Zlash65fd9c4512018-10-10 14:27:07 +053030 make_quotation(domain)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053031
Chinmay Paib5038962019-04-27 01:39:50 +053032 try:
33 lost_reason = frappe.get_doc({
34 "doctype": "Opportunity Lost Reason",
35 "lost_reason": "Did not ask"
36 })
37 lost_reason.save(ignore_permissions=True)
38 except frappe.exceptions.DuplicateEntryError:
39 pass
40
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053041 # lost quotations / inquiries
42 if random.random() < 0.3:
Achilles Rasquinha96698c92018-02-28 16:12:51 +053043 for i in range(random.randint(1,3)):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053044 quotation = get_random('Quotation', doc=True)
45 if quotation and quotation.status == 'Submitted':
Chinmay Paib5038962019-04-27 01:39:50 +053046 quotation.declare_order_lost([{'lost_reason': 'Did not ask'}])
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053047
Achilles Rasquinha96698c92018-02-28 16:12:51 +053048 for i in range(random.randint(1,3)):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053049 opportunity = get_random('Opportunity', doc=True)
50 if opportunity and opportunity.status in ('Open', 'Replied'):
Chinmay Paib5038962019-04-27 01:39:50 +053051 opportunity.declare_enquiry_lost([{'lost_reason': 'Did not ask'}])
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053052
Zlash65dcf74012018-09-27 18:51:18 +053053 for i in range(random.randint(1,3)):
Zlash65d080a172018-10-01 10:49:06 +053054 if random.random() < 0.6:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053055 make_sales_order()
56
Zlash65dcf74012018-09-27 18:51:18 +053057 if random.random() < 0.5:
Saurabh90c0b7f2016-07-22 14:15:03 +053058 #make payment request against Sales Order
59 sales_order_name = get_random("Sales Order", filters={"docstatus": 1})
Zlash65d080a172018-10-01 10:49:06 +053060 try:
61 if sales_order_name:
62 so = frappe.get_doc("Sales Order", sales_order_name)
63 if flt(so.per_billed) != 100:
64 payment_request = make_payment_request(dt="Sales Order", dn=so.name, recipient_id=so.contact_email,
65 submit_doc=True, mute_email=True, use_dummy_message=True)
Saurabh90c0b7f2016-07-22 14:15:03 +053066
Zlash65d080a172018-10-01 10:49:06 +053067 payment_entry = frappe.get_doc(make_payment_entry(payment_request.name))
68 payment_entry.posting_date = frappe.flags.current_date
69 payment_entry.submit()
70 except Exception:
71 pass
Saurabh90c0b7f2016-07-22 14:15:03 +053072
Zlash65fd9c4512018-10-10 14:27:07 +053073def make_opportunity(domain):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053074 b = frappe.get_doc({
75 "doctype": "Opportunity",
Nabin Hait34c551d2019-07-03 10:34:31 +053076 "opportunity_from": "Customer",
deepeshgarg0073db65ef2019-10-15 19:16:45 +053077 "party_name": frappe.get_value("Customer", get_random("Customer"), 'name'),
Rushabh Mehtaa5ebebd2017-11-16 17:03:52 +053078 "opportunity_type": "Sales",
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053079 "with_items": 1,
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053080 "transaction_date": frappe.flags.current_date,
81 })
82
83 add_random_children(b, "items", rows=4, randomize = {
84 "qty": (1, 5),
Zlash65fd9c4512018-10-10 14:27:07 +053085 "item_code": ("Item", {"has_variants": 0, "is_fixed_asset": 0, "domain": domain})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053086 }, unique="item_code")
87
88 b.insert()
89 frappe.db.commit()
90
Zlash65fd9c4512018-10-10 14:27:07 +053091def make_quotation(domain):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053092 # get open opportunites
Rushabh Mehtae9d9b8e2016-12-15 11:27:35 +053093 opportunity = get_random("Opportunity", {"status": "Open", "with_items": 1})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053094
95 if opportunity:
96 from erpnext.crm.doctype.opportunity.opportunity import make_quotation
97 qtn = frappe.get_doc(make_quotation(opportunity))
98 qtn.insert()
99 frappe.db.commit()
100 qtn.submit()
101 frappe.db.commit()
102 else:
103 # make new directly
104
105 # get customer, currency and exchange_rate
106 customer = get_random("Customer")
107
Zlash65fd9c4512018-10-10 14:27:07 +0530108 company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency")
109 party_account_currency = get_party_account_currency("Customer", customer, erpnext.get_default_company())
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530110 if company_currency == party_account_currency:
111 exchange_rate = 1
112 else:
Shreya3f778522018-05-15 16:59:20 +0530113 exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_selling")
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530114
115 qtn = frappe.get_doc({
116 "creation": frappe.flags.current_date,
117 "doctype": "Quotation",
118 "quotation_to": "Customer",
Nabin Hait34c551d2019-07-03 10:34:31 +0530119 "party_name": customer,
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530120 "currency": party_account_currency or company_currency,
121 "conversion_rate": exchange_rate,
122 "order_type": "Sales",
123 "transaction_date": frappe.flags.current_date,
124 })
125
126 add_random_children(qtn, "items", rows=3, randomize = {
127 "qty": (1, 5),
Zlash65fd9c4512018-10-10 14:27:07 +0530128 "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0, "domain": domain})
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530129 }, unique="item_code")
130
131 qtn.insert()
132 frappe.db.commit()
133 qtn.submit()
134 frappe.db.commit()
135
136def make_sales_order():
137 q = get_random("Quotation", {"status": "Submitted"})
138 if q:
Zlash65dcf74012018-09-27 18:51:18 +0530139 from erpnext.selling.doctype.quotation.quotation import make_sales_order as mso
140 so = frappe.get_doc(mso(q))
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530141 so.transaction_date = frappe.flags.current_date
Nabin Hait495ef672017-07-31 11:25:51 +0530142 so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530143 so.insert()
144 frappe.db.commit()
145 so.submit()
146 frappe.db.commit()