Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 6 | import frappe, random, erpnext |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 7 | from frappe.utils import flt |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 8 | from frappe.utils.make_random import add_random_children, get_random |
| 9 | from erpnext.setup.utils import get_exchange_rate |
| 10 | from erpnext.accounts.party import get_party_account_currency |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 11 | from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request, make_payment_entry |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 12 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 13 | def work(domain="Manufacturing"): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 14 | frappe.set_user(frappe.db.get_global('demo_sales_user_2')) |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 15 | |
| 16 | for i in range(random.randint(1,7)): |
| 17 | if random.random() < 0.5: |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 18 | make_opportunity(domain) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 19 | |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 20 | for i in range(random.randint(1,3)): |
| 21 | if random.random() < 0.5: |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 22 | make_quotation(domain) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 23 | |
Chinmay Pai | b503896 | 2019-04-27 01:39:50 +0530 | [diff] [blame] | 24 | try: |
| 25 | lost_reason = frappe.get_doc({ |
| 26 | "doctype": "Opportunity Lost Reason", |
| 27 | "lost_reason": "Did not ask" |
| 28 | }) |
| 29 | lost_reason.save(ignore_permissions=True) |
| 30 | except frappe.exceptions.DuplicateEntryError: |
| 31 | pass |
| 32 | |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 33 | # lost quotations / inquiries |
| 34 | if random.random() < 0.3: |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 35 | for i in range(random.randint(1,3)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 36 | quotation = get_random('Quotation', doc=True) |
| 37 | if quotation and quotation.status == 'Submitted': |
Chinmay Pai | b503896 | 2019-04-27 01:39:50 +0530 | [diff] [blame] | 38 | quotation.declare_order_lost([{'lost_reason': 'Did not ask'}]) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 39 | |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 40 | for i in range(random.randint(1,3)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 41 | opportunity = get_random('Opportunity', doc=True) |
| 42 | if opportunity and opportunity.status in ('Open', 'Replied'): |
Chinmay Pai | b503896 | 2019-04-27 01:39:50 +0530 | [diff] [blame] | 43 | opportunity.declare_enquiry_lost([{'lost_reason': 'Did not ask'}]) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 44 | |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 45 | for i in range(random.randint(1,3)): |
Zlash65 | d080a17 | 2018-10-01 10:49:06 +0530 | [diff] [blame] | 46 | if random.random() < 0.6: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 47 | make_sales_order() |
| 48 | |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 49 | if random.random() < 0.5: |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 50 | #make payment request against Sales Order |
| 51 | sales_order_name = get_random("Sales Order", filters={"docstatus": 1}) |
Zlash65 | d080a17 | 2018-10-01 10:49:06 +0530 | [diff] [blame] | 52 | try: |
| 53 | if sales_order_name: |
| 54 | so = frappe.get_doc("Sales Order", sales_order_name) |
| 55 | if flt(so.per_billed) != 100: |
| 56 | payment_request = make_payment_request(dt="Sales Order", dn=so.name, recipient_id=so.contact_email, |
| 57 | submit_doc=True, mute_email=True, use_dummy_message=True) |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 58 | |
Zlash65 | d080a17 | 2018-10-01 10:49:06 +0530 | [diff] [blame] | 59 | payment_entry = frappe.get_doc(make_payment_entry(payment_request.name)) |
| 60 | payment_entry.posting_date = frappe.flags.current_date |
| 61 | payment_entry.submit() |
| 62 | except Exception: |
| 63 | pass |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 64 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 65 | def make_opportunity(domain): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 66 | b = frappe.get_doc({ |
| 67 | "doctype": "Opportunity", |
| 68 | "enquiry_from": "Customer", |
| 69 | "customer": get_random("Customer"), |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 70 | "opportunity_type": "Sales", |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 71 | "with_items": 1, |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 72 | "transaction_date": frappe.flags.current_date, |
| 73 | }) |
| 74 | |
| 75 | add_random_children(b, "items", rows=4, randomize = { |
| 76 | "qty": (1, 5), |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 77 | "item_code": ("Item", {"has_variants": 0, "is_fixed_asset": 0, "domain": domain}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 78 | }, unique="item_code") |
| 79 | |
| 80 | b.insert() |
| 81 | frappe.db.commit() |
| 82 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 83 | def make_quotation(domain): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 84 | # get open opportunites |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 85 | opportunity = get_random("Opportunity", {"status": "Open", "with_items": 1}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 86 | |
| 87 | if opportunity: |
| 88 | from erpnext.crm.doctype.opportunity.opportunity import make_quotation |
| 89 | qtn = frappe.get_doc(make_quotation(opportunity)) |
| 90 | qtn.insert() |
| 91 | frappe.db.commit() |
| 92 | qtn.submit() |
| 93 | frappe.db.commit() |
| 94 | else: |
| 95 | # make new directly |
| 96 | |
| 97 | # get customer, currency and exchange_rate |
| 98 | customer = get_random("Customer") |
| 99 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 100 | company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency") |
| 101 | party_account_currency = get_party_account_currency("Customer", customer, erpnext.get_default_company()) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 102 | if company_currency == party_account_currency: |
| 103 | exchange_rate = 1 |
| 104 | else: |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 105 | exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_selling") |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 106 | |
| 107 | qtn = frappe.get_doc({ |
| 108 | "creation": frappe.flags.current_date, |
| 109 | "doctype": "Quotation", |
| 110 | "quotation_to": "Customer", |
| 111 | "customer": customer, |
| 112 | "currency": party_account_currency or company_currency, |
| 113 | "conversion_rate": exchange_rate, |
| 114 | "order_type": "Sales", |
| 115 | "transaction_date": frappe.flags.current_date, |
| 116 | }) |
| 117 | |
| 118 | add_random_children(qtn, "items", rows=3, randomize = { |
| 119 | "qty": (1, 5), |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 120 | "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0, "domain": domain}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 121 | }, unique="item_code") |
| 122 | |
| 123 | qtn.insert() |
| 124 | frappe.db.commit() |
| 125 | qtn.submit() |
| 126 | frappe.db.commit() |
| 127 | |
| 128 | def make_sales_order(): |
| 129 | q = get_random("Quotation", {"status": "Submitted"}) |
| 130 | if q: |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 131 | from erpnext.selling.doctype.quotation.quotation import make_sales_order as mso |
| 132 | so = frappe.get_doc(mso(q)) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 133 | so.transaction_date = frappe.flags.current_date |
Nabin Hait | 495ef67 | 2017-07-31 11:25:51 +0530 | [diff] [blame] | 134 | so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 135 | so.insert() |
| 136 | frappe.db.commit() |
| 137 | so.submit() |
| 138 | frappe.db.commit() |