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 | |
| 24 | # lost quotations / inquiries |
| 25 | if random.random() < 0.3: |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 26 | for i in range(random.randint(1,3)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 27 | quotation = get_random('Quotation', doc=True) |
| 28 | if quotation and quotation.status == 'Submitted': |
| 29 | quotation.declare_order_lost('Did not ask') |
| 30 | |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 31 | for i in range(random.randint(1,3)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 32 | opportunity = get_random('Opportunity', doc=True) |
| 33 | if opportunity and opportunity.status in ('Open', 'Replied'): |
| 34 | opportunity.declare_enquiry_lost('Did not ask') |
| 35 | |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 36 | for i in range(random.randint(1,3)): |
Zlash65 | d080a17 | 2018-10-01 10:49:06 +0530 | [diff] [blame] | 37 | if random.random() < 0.6: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 38 | make_sales_order() |
| 39 | |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 40 | if random.random() < 0.5: |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 41 | #make payment request against Sales Order |
| 42 | sales_order_name = get_random("Sales Order", filters={"docstatus": 1}) |
Zlash65 | d080a17 | 2018-10-01 10:49:06 +0530 | [diff] [blame] | 43 | 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) |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 49 | |
Zlash65 | d080a17 | 2018-10-01 10:49:06 +0530 | [diff] [blame] | 50 | 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 |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 55 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 56 | def make_opportunity(domain): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 57 | b = frappe.get_doc({ |
| 58 | "doctype": "Opportunity", |
| 59 | "enquiry_from": "Customer", |
| 60 | "customer": get_random("Customer"), |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 61 | "opportunity_type": "Sales", |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 62 | "with_items": 1, |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 63 | "transaction_date": frappe.flags.current_date, |
| 64 | }) |
| 65 | |
| 66 | add_random_children(b, "items", rows=4, randomize = { |
| 67 | "qty": (1, 5), |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 68 | "item_code": ("Item", {"has_variants": 0, "is_fixed_asset": 0, "domain": domain}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 69 | }, unique="item_code") |
| 70 | |
| 71 | b.insert() |
| 72 | frappe.db.commit() |
| 73 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 74 | def make_quotation(domain): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 75 | # get open opportunites |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 76 | opportunity = get_random("Opportunity", {"status": "Open", "with_items": 1}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 77 | |
| 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 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 91 | company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency") |
| 92 | 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] | 93 | if company_currency == party_account_currency: |
| 94 | exchange_rate = 1 |
| 95 | else: |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 96 | 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] | 97 | |
| 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), |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 111 | "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0, "domain": domain}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 112 | }, unique="item_code") |
| 113 | |
| 114 | qtn.insert() |
| 115 | frappe.db.commit() |
| 116 | qtn.submit() |
| 117 | frappe.db.commit() |
| 118 | |
| 119 | def make_sales_order(): |
| 120 | q = get_random("Quotation", {"status": "Submitted"}) |
| 121 | if q: |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 122 | from erpnext.selling.doctype.quotation.quotation import make_sales_order as mso |
| 123 | so = frappe.get_doc(mso(q)) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 124 | so.transaction_date = frappe.flags.current_date |
Nabin Hait | 495ef67 | 2017-07-31 11:25:51 +0530 | [diff] [blame] | 125 | so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 126 | so.insert() |
| 127 | frappe.db.commit() |
| 128 | so.submit() |
| 129 | frappe.db.commit() |