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 |
| 7 | from frappe.utils.make_random import add_random_children, get_random |
| 8 | from erpnext.setup.utils import get_exchange_rate |
| 9 | from erpnext.accounts.party import get_party_account_currency |
| 10 | |
| 11 | def work(): |
| 12 | frappe.set_user(frappe.db.get_global('demo_sales_user_2')) |
| 13 | if random.random() < 0.5: |
| 14 | for i in xrange(random.randint(1,7)): |
| 15 | make_opportunity() |
| 16 | |
| 17 | if random.random() < 0.5: |
| 18 | for i in xrange(random.randint(1,3)): |
| 19 | make_quotation() |
| 20 | |
| 21 | # lost quotations / inquiries |
| 22 | if random.random() < 0.3: |
| 23 | for i in xrange(random.randint(1,3)): |
| 24 | quotation = get_random('Quotation', doc=True) |
| 25 | if quotation and quotation.status == 'Submitted': |
| 26 | quotation.declare_order_lost('Did not ask') |
| 27 | |
| 28 | for i in xrange(random.randint(1,3)): |
| 29 | opportunity = get_random('Opportunity', doc=True) |
| 30 | if opportunity and opportunity.status in ('Open', 'Replied'): |
| 31 | opportunity.declare_enquiry_lost('Did not ask') |
| 32 | |
| 33 | if random.random() < 0.3: |
| 34 | for i in xrange(random.randint(1,3)): |
| 35 | make_sales_order() |
| 36 | |
| 37 | def make_opportunity(): |
| 38 | b = frappe.get_doc({ |
| 39 | "doctype": "Opportunity", |
| 40 | "enquiry_from": "Customer", |
| 41 | "customer": get_random("Customer"), |
| 42 | "enquiry_type": "Sales", |
| 43 | "transaction_date": frappe.flags.current_date, |
| 44 | }) |
| 45 | |
| 46 | add_random_children(b, "items", rows=4, randomize = { |
| 47 | "qty": (1, 5), |
| 48 | "item_code": ("Item", {"has_variants": "0"}) |
| 49 | }, unique="item_code") |
| 50 | |
| 51 | b.insert() |
| 52 | frappe.db.commit() |
| 53 | |
| 54 | def make_quotation(): |
| 55 | # get open opportunites |
| 56 | opportunity = get_random("Opportunity", {"status": "Open"}) |
| 57 | |
| 58 | if opportunity: |
| 59 | from erpnext.crm.doctype.opportunity.opportunity import make_quotation |
| 60 | qtn = frappe.get_doc(make_quotation(opportunity)) |
| 61 | qtn.insert() |
| 62 | frappe.db.commit() |
| 63 | qtn.submit() |
| 64 | frappe.db.commit() |
| 65 | else: |
| 66 | # make new directly |
| 67 | |
| 68 | # get customer, currency and exchange_rate |
| 69 | customer = get_random("Customer") |
| 70 | |
| 71 | company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency") |
| 72 | party_account_currency = get_party_account_currency("Customer", customer, "Wind Power LLC") |
| 73 | if company_currency == party_account_currency: |
| 74 | exchange_rate = 1 |
| 75 | else: |
| 76 | exchange_rate = get_exchange_rate(party_account_currency, company_currency) |
| 77 | |
| 78 | qtn = frappe.get_doc({ |
| 79 | "creation": frappe.flags.current_date, |
| 80 | "doctype": "Quotation", |
| 81 | "quotation_to": "Customer", |
| 82 | "customer": customer, |
| 83 | "currency": party_account_currency or company_currency, |
| 84 | "conversion_rate": exchange_rate, |
| 85 | "order_type": "Sales", |
| 86 | "transaction_date": frappe.flags.current_date, |
| 87 | }) |
| 88 | |
| 89 | add_random_children(qtn, "items", rows=3, randomize = { |
| 90 | "qty": (1, 5), |
| 91 | "item_code": ("Item", {"has_variants": "0"}) |
| 92 | }, unique="item_code") |
| 93 | |
| 94 | qtn.insert() |
| 95 | frappe.db.commit() |
| 96 | qtn.submit() |
| 97 | frappe.db.commit() |
| 98 | |
| 99 | def make_sales_order(): |
| 100 | q = get_random("Quotation", {"status": "Submitted"}) |
| 101 | if q: |
| 102 | from erpnext.selling.doctype.quotation.quotation import make_sales_order |
| 103 | so = frappe.get_doc(make_sales_order(q)) |
| 104 | so.transaction_date = frappe.flags.current_date |
| 105 | so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10) |
| 106 | so.insert() |
| 107 | frappe.db.commit() |
| 108 | so.submit() |
| 109 | frappe.db.commit() |