brotherton-erpnext/erpnext/demo/user/sales.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

147 lines
4.5 KiB
Python
Raw Normal View History

2016-06-29 13:08:32 +00:00
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import random
import frappe
2016-07-22 08:45:03 +00:00
from frappe.utils import flt
2016-06-29 13:08:32 +00:00
from frappe.utils.make_random import add_random_children, get_random
2016-06-29 13:08:32 +00:00
import erpnext
from erpnext.accounts.doctype.payment_request.payment_request import (
make_payment_entry,
2016-07-22 08:45:03 +00:00
make_payment_request,
)
2016-06-29 13:08:32 +00:00
from erpnext.accounts.party import get_party_account_currency
2016-07-22 08:45:03 +00:00
from erpnext.setup.utils import get_exchange_rate
2016-06-29 13:08:32 +00:00
def work(domain="Manufacturing"):
2016-06-29 13:08:32 +00:00
frappe.set_user(frappe.db.get_global('demo_sales_user_2'))
2018-09-27 13:21:18 +00:00
for i in range(random.randint(1,7)):
if random.random() < 0.5:
make_opportunity(domain)
2016-06-29 13:08:32 +00:00
2018-09-27 13:21:18 +00:00
for i in range(random.randint(1,3)):
if random.random() < 0.5:
make_quotation(domain)
2016-06-29 13:08:32 +00:00
try:
lost_reason = frappe.get_doc({
"doctype": "Opportunity Lost Reason",
"lost_reason": "Did not ask"
})
lost_reason.save(ignore_permissions=True)
except frappe.exceptions.DuplicateEntryError:
pass
2016-06-29 13:08:32 +00:00
# lost quotations / inquiries
if random.random() < 0.3:
for i in range(random.randint(1,3)):
2016-06-29 13:08:32 +00:00
quotation = get_random('Quotation', doc=True)
if quotation and quotation.status == 'Submitted':
quotation.declare_order_lost([{'lost_reason': 'Did not ask'}])
2016-06-29 13:08:32 +00:00
for i in range(random.randint(1,3)):
2016-06-29 13:08:32 +00:00
opportunity = get_random('Opportunity', doc=True)
if opportunity and opportunity.status in ('Open', 'Replied'):
opportunity.declare_enquiry_lost([{'lost_reason': 'Did not ask'}])
2016-06-29 13:08:32 +00:00
2018-09-27 13:21:18 +00:00
for i in range(random.randint(1,3)):
if random.random() < 0.6:
2016-06-29 13:08:32 +00:00
make_sales_order()
2018-09-27 13:21:18 +00:00
if random.random() < 0.5:
2016-07-22 08:45:03 +00:00
#make payment request against Sales Order
sales_order_name = get_random("Sales Order", filters={"docstatus": 1})
try:
if sales_order_name:
so = frappe.get_doc("Sales Order", sales_order_name)
if flt(so.per_billed) != 100:
payment_request = make_payment_request(dt="Sales Order", dn=so.name, recipient_id=so.contact_email,
submit_doc=True, mute_email=True, use_dummy_message=True)
payment_entry = frappe.get_doc(make_payment_entry(payment_request.name))
payment_entry.posting_date = frappe.flags.current_date
payment_entry.submit()
except Exception:
pass
2016-07-22 08:45:03 +00:00
def make_opportunity(domain):
2016-06-29 13:08:32 +00:00
b = frappe.get_doc({
"doctype": "Opportunity",
"opportunity_from": "Customer",
2019-10-15 13:46:45 +00:00
"party_name": frappe.get_value("Customer", get_random("Customer"), 'name'),
"opportunity_type": "Sales",
"with_items": 1,
2016-06-29 13:08:32 +00:00
"transaction_date": frappe.flags.current_date,
})
add_random_children(b, "items", rows=4, randomize = {
"qty": (1, 5),
"item_code": ("Item", {"has_variants": 0, "is_fixed_asset": 0, "domain": domain})
2016-06-29 13:08:32 +00:00
}, unique="item_code")
b.insert()
frappe.db.commit()
def make_quotation(domain):
2016-06-29 13:08:32 +00:00
# get open opportunites
opportunity = get_random("Opportunity", {"status": "Open", "with_items": 1})
2016-06-29 13:08:32 +00:00
if opportunity:
from erpnext.crm.doctype.opportunity.opportunity import make_quotation
qtn = frappe.get_doc(make_quotation(opportunity))
qtn.insert()
frappe.db.commit()
qtn.submit()
frappe.db.commit()
else:
# make new directly
# get customer, currency and exchange_rate
customer = get_random("Customer")
company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency")
party_account_currency = get_party_account_currency("Customer", customer, erpnext.get_default_company())
2016-06-29 13:08:32 +00:00
if company_currency == party_account_currency:
exchange_rate = 1
else:
exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_selling")
2016-06-29 13:08:32 +00:00
qtn = frappe.get_doc({
"creation": frappe.flags.current_date,
"doctype": "Quotation",
"quotation_to": "Customer",
"party_name": customer,
2016-06-29 13:08:32 +00:00
"currency": party_account_currency or company_currency,
"conversion_rate": exchange_rate,
"order_type": "Sales",
"transaction_date": frappe.flags.current_date,
})
add_random_children(qtn, "items", rows=3, randomize = {
"qty": (1, 5),
"item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0, "domain": domain})
2016-06-29 13:08:32 +00:00
}, unique="item_code")
qtn.insert()
frappe.db.commit()
qtn.submit()
frappe.db.commit()
def make_sales_order():
q = get_random("Quotation", {"status": "Submitted"})
if q:
2018-09-27 13:21:18 +00:00
from erpnext.selling.doctype.quotation.quotation import make_sales_order as mso
so = frappe.get_doc(mso(q))
2016-06-29 13:08:32 +00:00
so.transaction_date = frappe.flags.current_date
so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10)
2016-06-29 13:08:32 +00:00
so.insert()
frappe.db.commit()
so.submit()
frappe.db.commit()