Example #1
0
def update_party(fullname, company_name=None, mobile_no=None, phone=None):
	party = get_lead_or_customer()

	if party.doctype == "Lead":
		party.company_name = company_name
		party.lead_name = fullname
		party.mobile_no = mobile_no
		party.phone = phone
	else:
		party.customer_name = company_name or fullname
		party.customer_type == "Company" if company_name else "Individual"
		
		contact_name = frappe.db.get_value("Contact", {"email_id": frappe.session.user,
			"customer": party.name})
		contact = frappe.bean("Contact", contact_name)
		contact.doc.first_name = fullname
		contact.doc.last_name = None
		contact.doc.customer_name = party.customer_name
		contact.doc.mobile_no = mobile_no
		contact.doc.phone = phone
		contact.ignore_permissions = True
		contact.save()
	
	party_bean = frappe.bean(party.fields)
	party_bean.ignore_permissions = True
	party_bean.save()
	
	qbean = _get_cart_quotation(party)
	if not qbean.doc.fields.get("__islocal"):
		qbean.doc.customer_name = company_name or fullname
		qbean.run_method("set_missing_lead_customer_details")
		qbean.ignore_permissions = True
		qbean.save()
Example #2
0
def _get_cart_quotation(party=None):
	if not party:
		party = get_lead_or_customer()
		
	quotation = frappe.db.get_value("Quotation", 
		{party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0})
	
	if quotation:
		qbean = frappe.bean("Quotation", quotation)
	else:
		qbean = frappe.bean({
			"doctype": "Quotation",
			"naming_series": frappe.defaults.get_user_default("shopping_cart_quotation_series") or "QTN-CART-",
			"quotation_to": party.doctype,
			"company": frappe.defaults.get_user_default("company"),
			"order_type": "Shopping Cart",
			"status": "Draft",
			"docstatus": 0,
			"__islocal": 1,
			(party.doctype.lower()): party.name
		})
		
		if party.doctype == "Customer":
			qbean.doc.contact_person = frappe.db.get_value("Contact", {"email_id": frappe.session.user,
				"customer": party.name})
		
		qbean.run_method("onload_post_render")
		apply_cart_settings(party, qbean)
	
	return qbean
Example #3
0
	def test_exchange_rate_exists(self):
		frappe.db.sql("""delete from `tabCurrency Exchange`""")
		
		cart_settings = self.test_price_list_territory_overlap()
		controller = cart_settings.make_controller()
		self.assertRaises(ShoppingCartSetupError, controller.validate_exchange_rates_exist)
		
		from erpnext.setup.doctype.currency_exchange.test_currency_exchange import test_records as \
			currency_exchange_records
		frappe.bean(currency_exchange_records[0]).insert()
		controller.validate_exchange_rates_exist()
def migrate_unit_to_website_group(unit):
	unit = frappe.bean("Unit", unit)
	
	if frappe.conn.get_value("Website Group", cleanup_page_name(unit.doc.name)):
		# already exists!
		return
	
	unit_name = cleanup_page_name(unit.doc.name)
	print unit_name
	
	# create website group
	group = frappe.new_bean("Website Group")
	group.doc.group_name = unit.doc.name
	group.doc.group_title = unit.doc.unit_title
	group.doc.group_type = unit.doc.unit_type
	group.doc.group_description = unit.doc.unit_description
	group.doc.public_read = unit.doc.public_read
	group.doc.public_write = unit.doc.public_write
	group.doc.upvotes = unit.doc.upvotes
	group.doc.replies = unit.doc.replies
	
	if unit.doc.parent_unit:
		parent_docname = cleanup_page_name(unit.doc.parent_unit)
		group.doc.parent_website_sitemap = frappe.conn.get_value("Website Sitemap",
			{"ref_doctype": "Website Group", "docname": parent_docname})
			
		# just making sure if my logic is correct!
		if not group.doc.parent_website_sitemap:
			if parent_docname.endswith("discussion"):
				print "ignoring", unit_name
				return
			
			raise Exception("Website Sitemap Not Found: {}".format(unit.doc.parent_unit))
	
	group.insert()
	
	# add website sitemap permissions
	for d in unit.doclist.get({"doctype": "Unit Profile"}):
		frappe.bean({
			"doctype": "Website Sitemap Permission",
			"website_sitemap": group.doc.page_name,
			"profile": d.profile,
			"read": d.read,
			"write": d.write,
			"admin": d.admin
		}).insert()
	
	# move posts
	frappe.conn.sql("""update `tabPost` set website_group=%s where unit=%s""", (group.doc.name,
		unit.doc.name))
	
	# WARNING - commit here to avoid too many writes error!
	frappe.conn.commit()
Example #5
0
	def enable_shopping_cart(self):
		settings = frappe.bean("Shopping Cart Settings", "Shopping Cart Settings")

		if len(settings.doclist) > 1:
			settings.doc.enabled = 1
		else:
			settings.doc.fields.update({
				"enabled": 1,
				"company": "_Test Company",
				"default_territory": "_Test Territory Rest Of The World",
				"default_customer_group": "_Test Customer Group",
				"quotation_series": "_T-Quotation-"
			})
			settings.doclist.extend([
				# price lists
				{"doctype": "Shopping Cart Price List", "parentfield": "price_lists", 
					"selling_price_list": "_Test Price List India"},
				{"doctype": "Shopping Cart Price List", "parentfield": "price_lists", 
					"selling_price_list": "_Test Price List Rest of the World"},
			
				# tax masters
				{"doctype": "Shopping Cart Taxes and Charges Master", "parentfield": "sales_taxes_and_charges_masters",
					"sales_taxes_and_charges_master": "_Test India Tax Master"},
				{"doctype": "Shopping Cart Taxes and Charges Master", "parentfield": "sales_taxes_and_charges_masters",
					"sales_taxes_and_charges_master": "_Test Sales Taxes and Charges Master - Rest of the World"},
			
				# shipping rules
				{"doctype": "Shopping Cart Shipping Rule", "parentfield": "shipping_rules",
					"shipping_rule": "_Test Shipping Rule - India"}
			])
		
		settings.save()
Example #6
0
	def enable_shopping_cart(self):
		return
		if not frappe.db.get_value("Shopping Cart Settings", None, "enabled"):
			cart_settings = frappe.bean("Shopping Cart Settings")
			cart_settings.ignore_permissions = True
			cart_settings.doc.enabled = 1
			cart_settings.save()
Example #7
0
	def tearDown(self):
		return
		
		cart_settings = frappe.bean("Shopping Cart Settings")
		cart_settings.ignore_permissions = True
		cart_settings.doc.enabled = 0
		cart_settings.save()
Example #8
0
def get_context(context):
	def _get_fields(fieldnames):
		return [frappe._dict(zip(["label", "fieldname", "fieldtype", "options"], 
				[df.label, df.fieldname, df.fieldtype, df.options]))
			for df in frappe.get_doctype("Address", processed=True).get({"fieldname": ["in", fieldnames]})]
	
	docname = doc = None
	title = "New Address"
	if frappe.form_dict.name:
		bean = frappe.bean("Address", frappe.form_dict.name)
		docname = bean.doc.name
		doc = bean.doc.fields
		title = bean.doc.name
	
	return {
		"doc": doc,
		"meta": frappe._dict({
			"left_fields": _get_fields(["address_title", "address_type", "address_line1", "address_line2",
				"city", "state", "pincode", "country"]),
			"right_fields": _get_fields(["email_id", "phone", "fax", "is_primary_address",
				"is_shipping_address"])
		}),
		"docname": docname,
		"title": title
	}
def move_tasks_and_events():
	"""move tasks to tasks unit, events to events unit"""
	unit_tasks = dict(frappe.conn.sql("""select parent_unit, name from `tabUnit`
		where unit_type='Tasks'"""))
	unit_events = dict(frappe.conn.sql("""select parent_unit, name from `tabUnit`
		where unit_type='Events'"""))
	
	for post in frappe.conn.sql_list("""select name from `tabPost` 
		where is_task=1 or is_event=1"""):
		post = frappe.bean("Post", post)
		if post.doc.is_task:
			if not unit_tasks.get(post.doc.unit):
				# create unit of type Tasks
				new_unit = _add_unit(post.doc.unit, "Tasks", 1, 1, unit_type="Tasks")
				unit_tasks[post.doc.unit] = new_unit.doc.name
			
			post.doc.unit = unit_tasks.get(post.doc.unit)
			post.doc.is_event = 0
			
		elif post.doc.is_event:
			if not unit_events.get(post.doc.unit):
				# create unit of type Events
				new_unit = _add_unit(post.doc.unit, "Events", 1, 1, unit_type="Events")
				unit_events[post.doc.unit] = new_unit.doc.name
			
			post.doc.unit = unit_events.get(post.doc.unit)
			post.doc.is_task = 0
		
		print post.doc.name
		post.save()
Example #10
0
def get_transaction_context(doctype, name):
	customer = frappe.conn.get_value("Contact", {"email_id": frappe.session.user}, 
		"customer")
	
	bean = frappe.bean(doctype, name)
	if bean.doc.customer != customer:
		return { "bean": frappe._dict({ "doc": frappe._dict({"name": _("Not Allowed")}) }) }
	else:
		return { "bean": bean }
Example #11
0
def get_party(user):
	customer = frappe.db.get_value("Contact", {"email_id": user}, "customer")
	if customer:
		return frappe.bean("Customer", customer)
	
	lead = frappe.db.get_value("Lead", {"email_id": user})
	if lead:
		return frappe.bean("Lead", lead)
	
	# create a lead
	lead = frappe.new_bean("Lead")
	lead.doc.fields.update({
		"email_id": user,
		"lead_name": get_fullname(user),
		"territory": guess_territory()
	})
	lead.insert(ignore_permissions=True)
	
	return lead
Example #12
0
def get_context(context):
	bean = frappe.bean("Support Ticket", frappe.form_dict.name)
	if bean.doc.raised_by == frappe.session.user:
		ticket_context = {
			"title": bean.doc.name,
			"bean": bean
		}
	else:
		ticket_context = {"title": "Not Allowed"}
		
	return ticket_context
Example #13
0
def add_reply(ticket, message):
	if not message:
		raise frappe.throw(_("Please write something"))
	
	bean = frappe.bean("Support Ticket", ticket)
	if bean.doc.raised_by != frappe.session.user:
		raise frappe.throw(_("You are not allowed to reply to this ticket."), frappe.PermissionError)
	
	from frappe.core.doctype.communication.communication import _make
	_make(content=message, sender=bean.doc.raised_by, subject = bean.doc.subject,
		doctype="Support Ticket", name=bean.doc.name,
		date=today())
Example #14
0
	def create_groups(group_name, group_title, parent_website_sitemap):
		group = frappe.bean({
			"doctype":"Website Group", 
			"group_name": group_name, 
			"group_title": group_title,
			"parent_website_sitemap": parent_website_sitemap, 
			"public_read":1, 
			"public_write":1,
			"group_type":"Forum"
		}).insert()
		
		return frappe.conn.get_value("Website Sitemap", {"ref_doctype":"Website Group", 
			"docname": group.doc.name})
Example #15
0
def save_address(fields, address_fieldname=None):
	party = get_lead_or_customer()
	fields = json.loads(fields)
	
	if fields.get("name"):
		bean = frappe.bean("Address", fields.get("name"))
	else:
		bean = frappe.bean({"doctype": "Address", "__islocal": 1})
	
	bean.doc.fields.update(fields)
	
	party_fieldname = party.doctype.lower()
	bean.doc.fields.update({
		party_fieldname: party.name,
		(party_fieldname + "_name"): party.fields[party_fieldname + "_name"]
	})
	bean.ignore_permissions = True
	bean.save()
	
	if address_fieldname:
		update_cart_address(address_fieldname, bean.doc.name)
	
	return bean.doc.name
Example #16
0
def place_order():
	quotation = _get_cart_quotation()
	controller = quotation.make_controller()
	for fieldname in ["customer_address", "shipping_address_name"]:
		if not quotation.doc.fields.get(fieldname):
			throw(_("Please select a") + " " + _(controller.meta.get_label(fieldname)))
	
	quotation.ignore_permissions = True
	quotation.submit()
	
	from erpnext.selling.doctype.quotation.quotation import _make_sales_order
	sales_order = frappe.bean(_make_sales_order(quotation.doc.name, ignore_permissions=True))
	sales_order.ignore_permissions = True
	sales_order.insert()
	sales_order.submit()
	frappe._response.set_cookie("cart_count", "")
	
	return sales_order.doc.name
Example #17
0
def get_lead_or_customer():
	customer = frappe.db.get_value("Contact", {"email_id": frappe.session.user}, "customer")
	if customer:
		return frappe.doc("Customer", customer)
	
	lead = frappe.db.get_value("Lead", {"email_id": frappe.session.user})
	if lead:
		return frappe.doc("Lead", lead)
	else:
		lead_bean = frappe.bean({
			"doctype": "Lead",
			"email_id": frappe.session.user,
			"lead_name": get_fullname(frappe.session.user),
			"territory": guess_territory(),
			"status": "Open" # TODO: set something better???
		})
		
		if frappe.session.user != "Guest":
			lead_bean.ignore_permissions = True
			lead_bean.insert()
			
		return lead_bean.doc
Example #18
0
def get_quotation(user=None):
	if not user:
		user = frappe.session.user
	if user == "Guest":
		raise frappe.PermissionError
		
	is_shopping_cart_enabled()
	party = get_party(user)
	values = {
		"order_type": "Shopping Cart",
		party.doc.doctype.lower(): party.doc.name,
		"docstatus": 0,
	}

	try:
		quotation = frappe.bean("Quotation", values)
	except frappe.DoesNotExistError:
		quotation = frappe.new_bean("Quotation")
		quotation.doc.fields.update(values)
		quotation.insert(ignore_permissions=True)
		
	return quotation
Example #19
0
	def get_cart_settings(self):
		return frappe.bean({"doctype": "Shopping Cart Settings",
			"company": "_Test Company"})
def validate_cart_settings(bean, method):
	frappe.bean("Shopping Cart Settings", "Shopping Cart Settings").run_method("validate")
Example #21
0
	def test_place_order(self):
		quotation = self.test_update_cart()
		sales_order_name = place_order()
		sales_order = frappe.bean("Sales Order", sales_order_name)
		self.assertEquals(sales_order.doclist.getone({"item_code": "_Test Item"}).prevdoc_docname, quotation.doc.name)
Example #22
0
	def disable_shopping_cart(self):
		settings = frappe.bean("Shopping Cart Settings", "Shopping Cart Settings")
		settings.doc.enabled = 0
		settings.save()