# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals

import frappe, random
from frappe.utils.make_random import add_random_children, get_random
from erpnext.setup.utils import get_exchange_rate
from erpnext.accounts.party import get_party_account_currency

def work():
	frappe.set_user(frappe.db.get_global('demo_sales_user_2'))
	if random.random() < 0.5:
		for i in xrange(random.randint(1,7)):
			make_opportunity()

	if random.random() < 0.5:
		for i in xrange(random.randint(1,3)):
			make_quotation()

	# lost quotations / inquiries
	if random.random() < 0.3:
		for i in xrange(random.randint(1,3)):
			quotation = get_random('Quotation', doc=True)
			if quotation and quotation.status == 'Submitted':
				quotation.declare_order_lost('Did not ask')

		for i in xrange(random.randint(1,3)):
			opportunity = get_random('Opportunity', doc=True)
			if opportunity and opportunity.status in ('Open', 'Replied'):
				opportunity.declare_enquiry_lost('Did not ask')

	if random.random() < 0.3:
		for i in xrange(random.randint(1,3)):
			make_sales_order()

def make_opportunity():
	b = frappe.get_doc({
		"doctype": "Opportunity",
		"enquiry_from": "Customer",
		"customer": get_random("Customer"),
		"enquiry_type": "Sales",
		"transaction_date": frappe.flags.current_date,
	})

	add_random_children(b, "items", rows=4, randomize = {
		"qty": (1, 5),
		"item_code": ("Item", {"has_variants": "0"})
	}, unique="item_code")

	b.insert()
	frappe.db.commit()

def make_quotation():
	# get open opportunites
	opportunity = get_random("Opportunity", {"status": "Open"})

	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.db.get_value("Company", "Wind Power LLC", "default_currency")
		party_account_currency = get_party_account_currency("Customer", customer, "Wind Power LLC")
		if company_currency == party_account_currency:
			exchange_rate = 1
		else:
			exchange_rate = get_exchange_rate(party_account_currency, company_currency)

		qtn = frappe.get_doc({
			"creation": frappe.flags.current_date,
			"doctype": "Quotation",
			"quotation_to": "Customer",
			"customer": customer,
			"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"})
		}, 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:
		from erpnext.selling.doctype.quotation.quotation import make_sales_order
		so = frappe.get_doc(make_sales_order(q))
		so.transaction_date = frappe.flags.current_date
		so.delivery_date = frappe.utils.add_days(frappe.flags.current_date, 10)
		so.insert()
		frappe.db.commit()
		so.submit()
		frappe.db.commit()
