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 | |
| 6 | import frappe, random |
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 | |
| 13 | def work(): |
| 14 | frappe.set_user(frappe.db.get_global('demo_sales_user_2')) |
| 15 | if random.random() < 0.5: |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 16 | for i in range(random.randint(1,7)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 17 | make_opportunity() |
| 18 | |
| 19 | if random.random() < 0.5: |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 20 | for i in range(random.randint(1,3)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 21 | make_quotation() |
| 22 | |
| 23 | # lost quotations / inquiries |
| 24 | if random.random() < 0.3: |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 25 | for i in range(random.randint(1,3)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 26 | quotation = get_random('Quotation', doc=True) |
| 27 | if quotation and quotation.status == 'Submitted': |
| 28 | quotation.declare_order_lost('Did not ask') |
| 29 | |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 30 | for i in range(random.randint(1,3)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 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: |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 36 | for i in range(random.randint(1,3)): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 37 | make_sales_order() |
| 38 | |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 39 | 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) |
Saurabh | 6e0a00b | 2016-07-22 15:17:17 +0530 | [diff] [blame] | 44 | if flt(so.per_billed) != 100: |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 45 | 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 | |
Saurabh | 6e0a00b | 2016-07-22 15:17:17 +0530 | [diff] [blame] | 48 | payment_entry = frappe.get_doc(make_payment_entry(payment_request.name)) |
| 49 | payment_entry.posting_date = frappe.flags.current_date |
| 50 | payment_entry.submit() |
Saurabh | 90c0b7f | 2016-07-22 14:15:03 +0530 | [diff] [blame] | 51 | |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 52 | def make_opportunity(): |
| 53 | b = frappe.get_doc({ |
| 54 | "doctype": "Opportunity", |
| 55 | "enquiry_from": "Customer", |
| 56 | "customer": get_random("Customer"), |
Rushabh Mehta | a5ebebd | 2017-11-16 17:03:52 +0530 | [diff] [blame] | 57 | "opportunity_type": "Sales", |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 58 | "with_items": 1, |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 59 | "transaction_date": frappe.flags.current_date, |
| 60 | }) |
| 61 | |
| 62 | add_random_children(b, "items", rows=4, randomize = { |
| 63 | "qty": (1, 5), |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 64 | "item_code": ("Item", {"has_variants": 0, "is_fixed_asset": 0}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 65 | }, unique="item_code") |
| 66 | |
| 67 | b.insert() |
| 68 | frappe.db.commit() |
| 69 | |
| 70 | def make_quotation(): |
| 71 | # get open opportunites |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 72 | opportunity = get_random("Opportunity", {"status": "Open", "with_items": 1}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 73 | |
| 74 | if opportunity: |
| 75 | from erpnext.crm.doctype.opportunity.opportunity import make_quotation |
| 76 | qtn = frappe.get_doc(make_quotation(opportunity)) |
| 77 | qtn.insert() |
| 78 | frappe.db.commit() |
| 79 | qtn.submit() |
| 80 | frappe.db.commit() |
| 81 | else: |
| 82 | # make new directly |
| 83 | |
| 84 | # get customer, currency and exchange_rate |
| 85 | customer = get_random("Customer") |
| 86 | |
| 87 | company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency") |
| 88 | party_account_currency = get_party_account_currency("Customer", customer, "Wind Power LLC") |
| 89 | if company_currency == party_account_currency: |
| 90 | exchange_rate = 1 |
| 91 | else: |
| 92 | exchange_rate = get_exchange_rate(party_account_currency, company_currency) |
| 93 | |
| 94 | qtn = frappe.get_doc({ |
| 95 | "creation": frappe.flags.current_date, |
| 96 | "doctype": "Quotation", |
| 97 | "quotation_to": "Customer", |
| 98 | "customer": customer, |
| 99 | "currency": party_account_currency or company_currency, |
| 100 | "conversion_rate": exchange_rate, |
| 101 | "order_type": "Sales", |
| 102 | "transaction_date": frappe.flags.current_date, |
| 103 | }) |
| 104 | |
| 105 | add_random_children(qtn, "items", rows=3, randomize = { |
| 106 | "qty": (1, 5), |
Nabin Hait | 26cad30 | 2016-07-21 10:28:54 +0530 | [diff] [blame] | 107 | "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0}) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 108 | }, unique="item_code") |
| 109 | |
| 110 | qtn.insert() |
| 111 | frappe.db.commit() |
| 112 | qtn.submit() |
| 113 | frappe.db.commit() |
| 114 | |
| 115 | def make_sales_order(): |
| 116 | q = get_random("Quotation", {"status": "Submitted"}) |
| 117 | if q: |
| 118 | from erpnext.selling.doctype.quotation.quotation import make_sales_order |
| 119 | so = frappe.get_doc(make_sales_order(q)) |
| 120 | so.transaction_date = frappe.flags.current_date |
Nabin Hait | 495ef67 | 2017-07-31 11:25:51 +0530 | [diff] [blame] | 121 | so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 122 | so.insert() |
| 123 | frappe.db.commit() |
| 124 | so.submit() |
| 125 | frappe.db.commit() |