2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-10-21 10:46:30 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
import frappe
|
|
|
|
import frappe.defaults
|
2016-03-23 06:29:24 +00:00
|
|
|
from frappe import _, throw
|
2017-06-13 09:56:35 +00:00
|
|
|
from frappe.contacts.doctype.address.address import get_address_display
|
2015-07-13 10:53:42 +00:00
|
|
|
from frappe.contacts.doctype.contact.contact import get_contact_name
|
|
|
|
from frappe.utils import cint, cstr, flt, get_fullname
|
2014-10-21 10:46:30 +00:00
|
|
|
from frappe.utils.nestedset import get_root_of
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-03-18 09:35:50 +00:00
|
|
|
from erpnext.accounts.utils import get_account_name
|
2022-01-31 19:09:14 +00:00
|
|
|
from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import (
|
|
|
|
get_shopping_cart_settings,
|
|
|
|
)
|
2021-04-12 19:09:26 +00:00
|
|
|
from erpnext.utilities.product import get_web_item_qty_in_stock
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2017-09-25 11:10:02 +00:00
|
|
|
|
|
|
|
class WebsitePriceListMissingError(frappe.ValidationError):
|
|
|
|
pass
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
def set_cart_count(quotation=None):
|
2021-02-16 13:15:36 +00:00
|
|
|
if cint(frappe.db.get_singles_value("E Commerce Settings", "enabled")):
|
2015-06-01 11:45:42 +00:00
|
|
|
if not quotation:
|
|
|
|
quotation = _get_cart_quotation()
|
2021-12-31 14:58:00 +00:00
|
|
|
cart_count = cstr(cint(quotation.get("total_qty")))
|
2015-09-23 07:16:59 +00:00
|
|
|
|
|
|
|
if hasattr(frappe.local, "cookie_manager"):
|
|
|
|
frappe.local.cookie_manager.set_cookie("cart_count", cart_count)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_cart_quotation(doc=None):
|
2016-04-04 13:19:26 +00:00
|
|
|
party = get_party()
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
if not doc:
|
|
|
|
quotation = _get_cart_quotation(party)
|
|
|
|
doc = quotation
|
|
|
|
set_cart_count(quotation)
|
|
|
|
|
2017-03-22 10:44:43 +00:00
|
|
|
addresses = get_address_docs(party=party)
|
|
|
|
|
2017-11-20 05:52:18 +00:00
|
|
|
if not doc.customer_address and addresses:
|
2020-03-12 09:00:28 +00:00
|
|
|
update_cart_address("billing", addresses[0].name)
|
2022-01-21 09:03:30 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
return {
|
|
|
|
"doc": decorate_quotation_doc(doc),
|
2021-01-20 12:22:54 +00:00
|
|
|
"shipping_addresses": get_shipping_addresses(party),
|
|
|
|
"billing_addresses": get_billing_addresses(party),
|
2019-03-19 06:18:32 +00:00
|
|
|
"shipping_rules": get_applicable_shipping_rules(party),
|
2021-02-16 13:15:36 +00:00
|
|
|
"cart_settings": frappe.get_cached_doc("E Commerce Settings")
|
2014-10-21 10:46:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 12:22:54 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_shipping_addresses(party=None):
|
|
|
|
if not party:
|
|
|
|
party = get_party()
|
|
|
|
addresses = get_address_docs(party=party)
|
|
|
|
return [{"name": address.name, "title": address.address_title, "display": address.display}
|
|
|
|
for address in addresses if address.address_type == "Shipping"
|
|
|
|
]
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_billing_addresses(party=None):
|
|
|
|
if not party:
|
|
|
|
party = get_party()
|
|
|
|
addresses = get_address_docs(party=party)
|
|
|
|
return [{"name": address.name, "title": address.address_title, "display": address.display}
|
|
|
|
for address in addresses if address.address_type == "Billing"
|
|
|
|
]
|
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def place_order():
|
|
|
|
quotation = _get_cart_quotation()
|
2021-02-16 13:15:36 +00:00
|
|
|
cart_settings = frappe.db.get_value("E Commerce Settings", None,
|
2019-07-03 05:04:31 +00:00
|
|
|
["company", "allow_items_not_in_stock"], as_dict=1)
|
|
|
|
quotation.company = cart_settings.company
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-02-10 09:11:27 +00:00
|
|
|
quotation.flags.ignore_permissions = True
|
2014-10-21 10:46:30 +00:00
|
|
|
quotation.submit()
|
|
|
|
|
2019-07-12 09:21:01 +00:00
|
|
|
if quotation.quotation_to == 'Lead' and quotation.party_name:
|
2014-10-21 10:46:30 +00:00
|
|
|
# company used to create customer accounts
|
|
|
|
frappe.defaults.set_user_default("company", quotation.company)
|
|
|
|
|
2020-03-12 09:00:28 +00:00
|
|
|
if not (quotation.shipping_address_name or quotation.customer_address):
|
2020-03-12 10:34:22 +00:00
|
|
|
frappe.throw(_("Set Shipping Address or Billing Address"))
|
2020-03-12 09:00:28 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
from erpnext.selling.doctype.quotation.quotation import _make_sales_order
|
|
|
|
sales_order = frappe.get_doc(_make_sales_order(quotation.name, ignore_permissions=True))
|
2019-11-28 12:57:17 +00:00
|
|
|
sales_order.payment_schedule = []
|
2018-02-05 06:55:02 +00:00
|
|
|
|
2019-10-25 06:46:11 +00:00
|
|
|
if not cint(cart_settings.allow_items_not_in_stock):
|
2019-07-03 05:04:31 +00:00
|
|
|
for item in sales_order.get("items"):
|
2021-05-27 13:23:11 +00:00
|
|
|
item.warehouse = frappe.db.get_value(
|
|
|
|
"Website Item",
|
|
|
|
{
|
|
|
|
"item_code": item.item_code
|
|
|
|
},
|
|
|
|
"website_warehouse"
|
|
|
|
)
|
|
|
|
is_stock_item = frappe.db.get_value("Item", item.item_code, "is_stock_item")
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if is_stock_item:
|
2021-04-12 19:09:26 +00:00
|
|
|
item_stock = get_web_item_qty_in_stock(item.item_code, "website_warehouse")
|
2020-06-15 18:50:57 +00:00
|
|
|
if not cint(item_stock.in_stock):
|
2021-09-15 08:30:14 +00:00
|
|
|
throw(_("{0} Not in Stock").format(item.item_code))
|
2019-07-03 05:04:31 +00:00
|
|
|
if item.qty > item_stock.stock_qty[0][0]:
|
2020-06-15 18:50:57 +00:00
|
|
|
throw(_("Only {0} in Stock for item {1}").format(item_stock.stock_qty[0][0], item.item_code))
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-02-10 09:11:27 +00:00
|
|
|
sales_order.flags.ignore_permissions = True
|
2014-10-21 10:46:30 +00:00
|
|
|
sales_order.insert()
|
|
|
|
sales_order.submit()
|
2015-09-23 07:16:59 +00:00
|
|
|
|
|
|
|
if hasattr(frappe.local, "cookie_manager"):
|
|
|
|
frappe.local.cookie_manager.delete_cookie("cart_count")
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
return sales_order.name
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
2019-03-19 06:18:32 +00:00
|
|
|
def request_for_quotation():
|
|
|
|
quotation = _get_cart_quotation()
|
|
|
|
quotation.flags.ignore_permissions = True
|
2022-03-10 08:24:00 +00:00
|
|
|
|
|
|
|
if get_shopping_cart_settings().save_quotations_as_draft:
|
|
|
|
quotation.save()
|
|
|
|
else:
|
|
|
|
quotation.submit()
|
2019-03-19 06:18:32 +00:00
|
|
|
return quotation.name
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def update_cart(item_code, qty, additional_notes=None, with_items=False):
|
2014-10-21 10:46:30 +00:00
|
|
|
quotation = _get_cart_quotation()
|
|
|
|
|
2017-06-20 07:21:32 +00:00
|
|
|
empty_card = False
|
2014-10-21 10:46:30 +00:00
|
|
|
qty = flt(qty)
|
|
|
|
if qty == 0:
|
2017-06-20 07:21:32 +00:00
|
|
|
quotation_items = quotation.get("items", {"item_code": ["!=", item_code]})
|
|
|
|
if quotation_items:
|
|
|
|
quotation.set("items", quotation_items)
|
|
|
|
else:
|
|
|
|
empty_card = True
|
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
else:
|
2014-12-25 10:31:55 +00:00
|
|
|
quotation_items = quotation.get("items", {"item_code": item_code})
|
2014-10-21 10:46:30 +00:00
|
|
|
if not quotation_items:
|
2014-12-25 10:31:55 +00:00
|
|
|
quotation.append("items", {
|
2014-10-21 10:46:30 +00:00
|
|
|
"doctype": "Quotation Item",
|
|
|
|
"item_code": item_code,
|
2019-03-19 06:18:32 +00:00
|
|
|
"qty": qty,
|
|
|
|
"additional_notes": additional_notes
|
2014-10-21 10:46:30 +00:00
|
|
|
})
|
|
|
|
else:
|
2021-03-14 11:58:49 +00:00
|
|
|
quotation_items[0].qty = qty
|
2019-03-19 06:18:32 +00:00
|
|
|
quotation_items[0].additional_notes = additional_notes
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
apply_cart_settings(quotation=quotation)
|
|
|
|
|
2015-09-23 10:13:09 +00:00
|
|
|
quotation.flags.ignore_permissions = True
|
2017-09-25 11:10:02 +00:00
|
|
|
quotation.payment_schedule = []
|
2017-06-20 07:21:32 +00:00
|
|
|
if not empty_card:
|
|
|
|
quotation.save()
|
|
|
|
else:
|
|
|
|
quotation.delete()
|
|
|
|
quotation = None
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
set_cart_count(quotation)
|
|
|
|
|
2016-05-02 06:13:44 +00:00
|
|
|
if cint(with_items):
|
2021-03-11 15:54:47 +00:00
|
|
|
context = get_cart_quotation(quotation)
|
2015-09-17 10:58:30 +00:00
|
|
|
return {
|
|
|
|
"items": frappe.render_template("templates/includes/cart/cart_items.html",
|
|
|
|
context),
|
2021-09-15 08:30:14 +00:00
|
|
|
"total": frappe.render_template("templates/includes/cart/cart_items_total.html",
|
2015-09-17 10:58:30 +00:00
|
|
|
context),
|
2021-09-15 08:30:14 +00:00
|
|
|
"taxes_and_totals": frappe.render_template("templates/includes/cart/cart_payment_summary.html",
|
|
|
|
context)
|
2015-09-17 10:58:30 +00:00
|
|
|
}
|
2014-10-21 10:46:30 +00:00
|
|
|
else:
|
2016-05-02 06:13:44 +00:00
|
|
|
return {
|
2021-03-11 15:54:47 +00:00
|
|
|
'name': quotation.name
|
2016-05-02 06:13:44 +00:00
|
|
|
}
|
2016-05-16 06:06:08 +00:00
|
|
|
|
2016-05-02 06:13:44 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_shopping_cart_menu(context=None):
|
|
|
|
if not context:
|
|
|
|
context = get_cart_quotation()
|
2016-05-16 06:06:08 +00:00
|
|
|
|
2016-05-02 06:13:44 +00:00
|
|
|
return frappe.render_template('templates/includes/cart/cart_dropdown.html', context)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2019-03-19 06:18:32 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def add_new_address(doc):
|
|
|
|
doc = frappe.parse_json(doc)
|
|
|
|
doc.update({
|
|
|
|
'doctype': 'Address'
|
|
|
|
})
|
|
|
|
address = frappe.get_doc(doc)
|
|
|
|
address.save(ignore_permissions=True)
|
|
|
|
|
|
|
|
return address
|
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def create_lead_for_item_inquiry(lead, subject, message):
|
|
|
|
lead = frappe.parse_json(lead)
|
|
|
|
lead_doc = frappe.new_doc('Lead')
|
2021-11-09 10:19:32 +00:00
|
|
|
for fieldname in ("lead_name", "company_name", "email_id", "phone"):
|
|
|
|
lead_doc.set(fieldname, lead.get(fieldname))
|
|
|
|
|
2019-03-19 06:18:32 +00:00
|
|
|
lead_doc.set('lead_owner', '')
|
|
|
|
|
2021-01-28 12:02:57 +00:00
|
|
|
if not frappe.db.exists('Lead Source', 'Product Inquiry'):
|
|
|
|
frappe.get_doc({
|
|
|
|
'doctype': 'Lead Source',
|
|
|
|
'source_name' : 'Product Inquiry'
|
|
|
|
}).insert(ignore_permissions=True)
|
2021-11-09 10:19:32 +00:00
|
|
|
|
2021-01-28 12:02:57 +00:00
|
|
|
lead_doc.set('source', 'Product Inquiry')
|
|
|
|
|
2019-03-19 06:18:32 +00:00
|
|
|
try:
|
|
|
|
lead_doc.save(ignore_permissions=True)
|
|
|
|
except frappe.exceptions.DuplicateEntryError:
|
|
|
|
frappe.clear_messages()
|
|
|
|
lead_doc = frappe.get_doc('Lead', {'email_id': lead['email_id']})
|
|
|
|
|
|
|
|
lead_doc.add_comment('Comment', text='''
|
|
|
|
<div>
|
|
|
|
<h5>{subject}</h5>
|
|
|
|
<p>{message}</p>
|
|
|
|
</div>
|
|
|
|
'''.format(subject=subject, message=message))
|
|
|
|
|
|
|
|
return lead_doc
|
|
|
|
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_terms_and_conditions(terms_name):
|
|
|
|
return frappe.db.get_value('Terms and Conditions', terms_name, 'terms')
|
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
@frappe.whitelist()
|
2020-03-12 09:00:28 +00:00
|
|
|
def update_cart_address(address_type, address_name):
|
2014-10-21 10:46:30 +00:00
|
|
|
quotation = _get_cart_quotation()
|
2021-01-20 12:22:54 +00:00
|
|
|
address_doc = frappe.get_doc("Address", address_name).as_dict()
|
|
|
|
address_display = get_address_display(address_doc)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2020-03-12 09:00:28 +00:00
|
|
|
if address_type.lower() == "billing":
|
2014-10-21 10:46:30 +00:00
|
|
|
quotation.customer_address = address_name
|
|
|
|
quotation.address_display = address_display
|
2021-04-16 15:45:50 +00:00
|
|
|
quotation.shipping_address_name = quotation.shipping_address_name or address_name
|
2021-01-20 12:22:54 +00:00
|
|
|
address_doc = next((doc for doc in get_billing_addresses() if doc["name"] == address_name), None)
|
2020-03-12 09:00:28 +00:00
|
|
|
elif address_type.lower() == "shipping":
|
|
|
|
quotation.shipping_address_name = address_name
|
|
|
|
quotation.shipping_address = address_display
|
2021-04-16 15:45:50 +00:00
|
|
|
quotation.customer_address = quotation.customer_address or address_name
|
2021-01-20 12:22:54 +00:00
|
|
|
address_doc = next((doc for doc in get_shipping_addresses() if doc["name"] == address_name), None)
|
2014-10-21 10:46:30 +00:00
|
|
|
apply_cart_settings(quotation=quotation)
|
|
|
|
|
2015-02-10 09:11:27 +00:00
|
|
|
quotation.flags.ignore_permissions = True
|
2014-10-21 10:46:30 +00:00
|
|
|
quotation.save()
|
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
context = get_cart_quotation(quotation)
|
2021-01-20 12:22:54 +00:00
|
|
|
context['address'] = address_doc
|
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
return {
|
|
|
|
"taxes": frappe.render_template("templates/includes/order/order_taxes.html",
|
|
|
|
context),
|
2021-01-20 12:22:54 +00:00
|
|
|
"address": frappe.render_template("templates/includes/cart/address_card.html",
|
|
|
|
context)
|
|
|
|
}
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
def guess_territory():
|
|
|
|
territory = None
|
|
|
|
geoip_country = frappe.session.get("session_country")
|
|
|
|
if geoip_country:
|
|
|
|
territory = frappe.db.get_value("Territory", geoip_country)
|
|
|
|
|
|
|
|
return territory or \
|
2021-02-16 13:15:36 +00:00
|
|
|
frappe.db.get_value("E Commerce Settings", None, "territory") or \
|
2014-10-21 10:46:30 +00:00
|
|
|
get_root_of("Territory")
|
|
|
|
|
2015-09-11 13:19:59 +00:00
|
|
|
def decorate_quotation_doc(doc):
|
2014-12-25 10:31:55 +00:00
|
|
|
for d in doc.get("items", []):
|
2022-01-20 14:04:36 +00:00
|
|
|
item_code = d.item_code
|
|
|
|
fields = ["web_item_name", "thumbnail", "website_image", "description", "route"]
|
|
|
|
|
|
|
|
# Variant Item
|
|
|
|
if not frappe.db.exists("Website Item", {"item_code": item_code}):
|
|
|
|
variant_data = frappe.db.get_values(
|
|
|
|
"Item",
|
|
|
|
filters={"item_code": item_code},
|
2022-01-21 09:03:30 +00:00
|
|
|
fieldname=["variant_of", "item_name", "image"],
|
2022-01-20 14:04:36 +00:00
|
|
|
as_dict=True
|
|
|
|
)[0]
|
|
|
|
item_code = variant_data.variant_of
|
|
|
|
fields = fields[1:]
|
2022-01-21 10:28:29 +00:00
|
|
|
d.web_item_name = variant_data.item_name
|
2022-01-21 09:03:30 +00:00
|
|
|
|
|
|
|
if variant_data.image: # get image from variant or template web item
|
|
|
|
d.thumbnail = variant_data.image
|
|
|
|
fields = fields[2:]
|
2022-01-20 14:04:36 +00:00
|
|
|
|
2021-07-15 14:39:02 +00:00
|
|
|
d.update(frappe.db.get_value(
|
|
|
|
"Website Item",
|
2022-01-20 14:04:36 +00:00
|
|
|
{"item_code": item_code},
|
|
|
|
fields,
|
2021-07-15 14:39:02 +00:00
|
|
|
as_dict=True)
|
|
|
|
)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
return doc
|
|
|
|
|
2017-09-25 11:10:02 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
def _get_cart_quotation(party=None):
|
2017-01-18 08:44:20 +00:00
|
|
|
'''Return the open Quotation of type "Shopping Cart" or make a new one'''
|
2014-10-21 10:46:30 +00:00
|
|
|
if not party:
|
2016-04-04 13:19:26 +00:00
|
|
|
party = get_party()
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-23 10:13:09 +00:00
|
|
|
quotation = frappe.get_all("Quotation", fields=["name"], filters=
|
2022-02-09 15:53:24 +00:00
|
|
|
{"party_name": party.name, "contact_email": frappe.session.user, "order_type": "Shopping Cart", "docstatus": 0},
|
2015-09-23 10:13:09 +00:00
|
|
|
order_by="modified desc", limit_page_length=1)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
if quotation:
|
2015-09-23 10:13:09 +00:00
|
|
|
qdoc = frappe.get_doc("Quotation", quotation[0].name)
|
2014-10-21 10:46:30 +00:00
|
|
|
else:
|
2021-02-16 13:15:36 +00:00
|
|
|
company = frappe.db.get_value("E Commerce Settings", None, ["company"])
|
2014-10-21 10:46:30 +00:00
|
|
|
qdoc = frappe.get_doc({
|
|
|
|
"doctype": "Quotation",
|
2015-07-13 10:53:42 +00:00
|
|
|
"naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-",
|
2014-10-21 10:46:30 +00:00
|
|
|
"quotation_to": party.doctype,
|
2019-07-03 13:52:49 +00:00
|
|
|
"company": company,
|
2014-10-21 10:46:30 +00:00
|
|
|
"order_type": "Shopping Cart",
|
|
|
|
"status": "Draft",
|
|
|
|
"docstatus": 0,
|
|
|
|
"__islocal": 1,
|
2019-07-03 05:04:31 +00:00
|
|
|
"party_name": party.name
|
2014-10-21 10:46:30 +00:00
|
|
|
})
|
|
|
|
|
2017-01-16 06:49:07 +00:00
|
|
|
qdoc.contact_person = frappe.db.get_value("Contact", {"email_id": frappe.session.user})
|
2015-09-23 07:16:59 +00:00
|
|
|
qdoc.contact_email = frappe.session.user
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-02-10 09:11:27 +00:00
|
|
|
qdoc.flags.ignore_permissions = True
|
2014-10-21 10:46:30 +00:00
|
|
|
qdoc.run_method("set_missing_values")
|
|
|
|
apply_cart_settings(party, qdoc)
|
|
|
|
|
|
|
|
return qdoc
|
|
|
|
|
|
|
|
def update_party(fullname, company_name=None, mobile_no=None, phone=None):
|
2016-04-04 13:19:26 +00:00
|
|
|
party = get_party()
|
2015-09-23 07:16:59 +00:00
|
|
|
|
|
|
|
party.customer_name = company_name or fullname
|
2021-08-19 10:13:34 +00:00
|
|
|
party.customer_type = "Company" if company_name else "Individual"
|
2015-09-23 07:16:59 +00:00
|
|
|
|
2017-01-16 06:49:07 +00:00
|
|
|
contact_name = frappe.db.get_value("Contact", {"email_id": frappe.session.user})
|
2015-09-23 07:16:59 +00:00
|
|
|
contact = frappe.get_doc("Contact", contact_name)
|
|
|
|
contact.first_name = fullname
|
|
|
|
contact.last_name = None
|
|
|
|
contact.customer_name = party.customer_name
|
|
|
|
contact.mobile_no = mobile_no
|
|
|
|
contact.phone = phone
|
|
|
|
contact.flags.ignore_permissions = True
|
|
|
|
contact.save()
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
party_doc = frappe.get_doc(party.as_dict())
|
2015-02-10 09:11:27 +00:00
|
|
|
party_doc.flags.ignore_permissions = True
|
2014-10-21 10:46:30 +00:00
|
|
|
party_doc.save()
|
|
|
|
|
|
|
|
qdoc = _get_cart_quotation(party)
|
|
|
|
if not qdoc.get("__islocal"):
|
|
|
|
qdoc.customer_name = company_name or fullname
|
|
|
|
qdoc.run_method("set_missing_lead_customer_details")
|
2015-02-10 09:11:27 +00:00
|
|
|
qdoc.flags.ignore_permissions = True
|
2014-10-21 10:46:30 +00:00
|
|
|
qdoc.save()
|
|
|
|
|
|
|
|
def apply_cart_settings(party=None, quotation=None):
|
|
|
|
if not party:
|
2016-04-04 13:19:26 +00:00
|
|
|
party = get_party()
|
2014-10-21 10:46:30 +00:00
|
|
|
if not quotation:
|
|
|
|
quotation = _get_cart_quotation(party)
|
|
|
|
|
2021-02-16 13:15:36 +00:00
|
|
|
cart_settings = frappe.get_doc("E Commerce Settings")
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
set_price_list_and_rate(quotation, cart_settings)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
quotation.run_method("calculate_taxes_and_totals")
|
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
set_taxes(quotation, cart_settings)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
_apply_shipping_rule(party, quotation, cart_settings)
|
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
def set_price_list_and_rate(quotation, cart_settings):
|
2014-10-21 10:46:30 +00:00
|
|
|
"""set price list based on billing territory"""
|
2015-07-13 10:53:42 +00:00
|
|
|
|
2020-05-25 12:56:01 +00:00
|
|
|
_set_price_list(cart_settings, quotation)
|
2015-07-13 10:53:42 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
# reset values
|
|
|
|
quotation.price_list_currency = quotation.currency = \
|
|
|
|
quotation.plc_conversion_rate = quotation.conversion_rate = None
|
2014-12-25 10:31:55 +00:00
|
|
|
for item in quotation.get("items"):
|
2014-10-21 10:46:30 +00:00
|
|
|
item.price_list_rate = item.discount_percentage = item.rate = item.amount = None
|
|
|
|
|
|
|
|
# refetch values
|
|
|
|
quotation.run_method("set_price_list_and_item_details")
|
|
|
|
|
2015-09-23 07:16:59 +00:00
|
|
|
if hasattr(frappe.local, "cookie_manager"):
|
|
|
|
# set it in cookies for using in product page
|
|
|
|
frappe.local.cookie_manager.set_cookie("selling_price_list", quotation.selling_price_list)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2020-05-25 12:56:01 +00:00
|
|
|
def _set_price_list(cart_settings, quotation=None):
|
2015-09-17 12:59:44 +00:00
|
|
|
"""Set price list based on customer or shopping cart default"""
|
2019-09-05 06:49:50 +00:00
|
|
|
from erpnext.accounts.party import get_default_price_list
|
2020-06-10 12:52:59 +00:00
|
|
|
party_name = quotation.get("party_name") if quotation else get_party().get("name")
|
2015-07-13 10:53:42 +00:00
|
|
|
selling_price_list = None
|
|
|
|
|
2020-06-10 12:52:59 +00:00
|
|
|
# check if default customer price list exists
|
2020-12-01 03:44:57 +00:00
|
|
|
if party_name and frappe.db.exists("Customer", party_name):
|
2020-06-10 12:52:59 +00:00
|
|
|
selling_price_list = get_default_price_list(frappe.get_doc("Customer", party_name))
|
|
|
|
|
|
|
|
# check default price list in shopping cart
|
2015-07-13 10:53:42 +00:00
|
|
|
if not selling_price_list:
|
2015-09-17 12:59:44 +00:00
|
|
|
selling_price_list = cart_settings.price_list
|
2015-07-13 10:53:42 +00:00
|
|
|
|
2020-05-25 12:56:01 +00:00
|
|
|
if quotation:
|
|
|
|
quotation.selling_price_list = selling_price_list
|
|
|
|
|
|
|
|
return selling_price_list
|
2015-07-13 10:53:42 +00:00
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
def set_taxes(quotation, cart_settings):
|
2014-10-21 10:46:30 +00:00
|
|
|
"""set taxes based on billing territory"""
|
2015-09-11 05:44:07 +00:00
|
|
|
from erpnext.accounts.party import set_taxes
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
customer_group = frappe.db.get_value("Customer", quotation.party_name, "customer_group")
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
quotation.taxes_and_charges = set_taxes(quotation.party_name, "Customer",
|
2019-01-06 13:14:18 +00:00
|
|
|
quotation.transaction_date, quotation.company, customer_group=customer_group, supplier_group=None,
|
|
|
|
tax_category=quotation.tax_category, billing_address=quotation.customer_address,
|
|
|
|
shipping_address=quotation.shipping_address_name, use_for_shopping_cart=1)
|
2015-09-11 05:44:07 +00:00
|
|
|
#
|
|
|
|
# # clear table
|
2014-12-25 10:31:55 +00:00
|
|
|
quotation.set("taxes", [])
|
2015-09-11 05:44:07 +00:00
|
|
|
#
|
|
|
|
# # append taxes
|
2015-05-12 09:37:02 +00:00
|
|
|
quotation.append_taxes_from_master()
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2016-04-04 13:19:26 +00:00
|
|
|
def get_party(user=None):
|
2015-09-23 07:16:59 +00:00
|
|
|
if not user:
|
|
|
|
user = frappe.session.user
|
|
|
|
|
2019-11-06 12:33:03 +00:00
|
|
|
contact_name = get_contact_name(user)
|
2017-01-16 06:49:07 +00:00
|
|
|
party = None
|
|
|
|
|
|
|
|
if contact_name:
|
|
|
|
contact = frappe.get_doc('Contact', contact_name)
|
|
|
|
if contact.links:
|
|
|
|
party_doctype = contact.links[0].link_doctype
|
|
|
|
party = contact.links[0].link_name
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2021-02-16 13:15:36 +00:00
|
|
|
cart_settings = frappe.get_doc("E Commerce Settings")
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2016-02-22 09:48:40 +00:00
|
|
|
debtors_account = ''
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2016-02-22 09:48:40 +00:00
|
|
|
if cart_settings.enable_checkout:
|
|
|
|
debtors_account = get_debtors_account(cart_settings)
|
2016-04-04 13:19:26 +00:00
|
|
|
|
|
|
|
if party:
|
|
|
|
return frappe.get_doc(party_doctype, party)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
else:
|
2016-05-16 06:06:08 +00:00
|
|
|
if not cart_settings.enabled:
|
2017-05-18 07:11:19 +00:00
|
|
|
frappe.local.flags.redirect_location = "/contact"
|
|
|
|
raise frappe.Redirect
|
2015-09-23 07:16:59 +00:00
|
|
|
customer = frappe.new_doc("Customer")
|
|
|
|
fullname = get_fullname(user)
|
|
|
|
customer.update({
|
|
|
|
"customer_name": fullname,
|
|
|
|
"customer_type": "Individual",
|
|
|
|
"customer_group": get_shopping_cart_settings().default_customer_group,
|
|
|
|
"territory": get_root_of("Territory")
|
2014-10-21 10:46:30 +00:00
|
|
|
})
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2016-02-22 09:48:40 +00:00
|
|
|
if debtors_account:
|
|
|
|
customer.update({
|
|
|
|
"accounts": [{
|
|
|
|
"company": cart_settings.company,
|
2016-03-18 09:35:50 +00:00
|
|
|
"account": debtors_account
|
2016-02-22 09:48:40 +00:00
|
|
|
}]
|
|
|
|
})
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2015-10-20 13:27:27 +00:00
|
|
|
customer.flags.ignore_mandatory = True
|
2015-09-23 07:16:59 +00:00
|
|
|
customer.insert(ignore_permissions=True)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-23 07:16:59 +00:00
|
|
|
contact = frappe.new_doc("Contact")
|
|
|
|
contact.update({
|
|
|
|
"first_name": fullname,
|
2019-11-06 12:33:03 +00:00
|
|
|
"email_ids": [{"email_id": user, "is_primary": 1}]
|
2015-09-23 07:16:59 +00:00
|
|
|
})
|
2017-01-16 06:49:07 +00:00
|
|
|
contact.append('links', dict(link_doctype='Customer', link_name=customer.name))
|
2015-10-20 13:27:27 +00:00
|
|
|
contact.flags.ignore_mandatory = True
|
2015-09-23 07:16:59 +00:00
|
|
|
contact.insert(ignore_permissions=True)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-23 07:16:59 +00:00
|
|
|
return customer
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2016-02-22 09:48:40 +00:00
|
|
|
def get_debtors_account(cart_settings):
|
2021-02-11 13:36:10 +00:00
|
|
|
if not cart_settings.payment_gateway_account:
|
|
|
|
frappe.throw(_("Payment Gateway Account not set"), _("Mandatory"))
|
|
|
|
|
2016-02-22 09:48:40 +00:00
|
|
|
payment_gateway_account_currency = \
|
|
|
|
frappe.get_doc("Payment Gateway Account", cart_settings.payment_gateway_account).currency
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2020-01-29 09:36:18 +00:00
|
|
|
account_name = _("Debtors ({0})").format(payment_gateway_account_currency)
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2016-03-18 09:35:50 +00:00
|
|
|
debtors_account_name = get_account_name("Receivable", "Asset", is_group=0,\
|
|
|
|
account_currency=payment_gateway_account_currency, company=cart_settings.company)
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2016-03-18 09:35:50 +00:00
|
|
|
if not debtors_account_name:
|
2016-02-22 09:48:40 +00:00
|
|
|
debtors_account = frappe.get_doc({
|
|
|
|
"doctype": "Account",
|
|
|
|
"account_type": "Receivable",
|
|
|
|
"root_type": "Asset",
|
|
|
|
"is_group": 0,
|
2016-03-18 09:35:50 +00:00
|
|
|
"parent_account": get_account_name(root_type="Asset", is_group=1, company=cart_settings.company),
|
2016-02-22 09:48:40 +00:00
|
|
|
"account_name": account_name,
|
2016-04-04 13:19:26 +00:00
|
|
|
"currency": payment_gateway_account_currency
|
2016-02-22 09:48:40 +00:00
|
|
|
}).insert(ignore_permissions=True)
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2016-03-18 09:35:50 +00:00
|
|
|
return debtors_account.name
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2016-02-22 09:48:40 +00:00
|
|
|
else:
|
2016-03-18 09:35:50 +00:00
|
|
|
return debtors_account_name
|
2016-04-04 13:19:26 +00:00
|
|
|
|
2016-02-22 09:48:40 +00:00
|
|
|
|
2017-01-17 16:45:17 +00:00
|
|
|
def get_address_docs(doctype=None, txt=None, filters=None, limit_start=0, limit_page_length=20,
|
|
|
|
party=None):
|
2014-10-21 10:46:30 +00:00
|
|
|
if not party:
|
2016-04-04 13:19:26 +00:00
|
|
|
party = get_party()
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2016-05-16 06:06:08 +00:00
|
|
|
if not party:
|
|
|
|
return []
|
|
|
|
|
2017-01-17 06:36:31 +00:00
|
|
|
address_names = frappe.db.get_all('Dynamic Link', fields=('parent'),
|
2017-01-16 07:36:07 +00:00
|
|
|
filters=dict(parenttype='Address', link_doctype=party.doctype, link_name=party.name))
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2017-01-16 07:36:07 +00:00
|
|
|
out = []
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2017-01-17 16:45:17 +00:00
|
|
|
for a in address_names:
|
|
|
|
address = frappe.get_doc('Address', a.parent)
|
2017-01-16 07:36:07 +00:00
|
|
|
address.display = get_address_display(address.as_dict())
|
|
|
|
out.append(address)
|
|
|
|
|
|
|
|
return out
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def apply_shipping_rule(shipping_rule):
|
|
|
|
quotation = _get_cart_quotation()
|
|
|
|
|
|
|
|
quotation.shipping_rule = shipping_rule
|
|
|
|
|
|
|
|
apply_cart_settings(quotation=quotation)
|
|
|
|
|
2015-02-10 09:11:27 +00:00
|
|
|
quotation.flags.ignore_permissions = True
|
2014-10-21 10:46:30 +00:00
|
|
|
quotation.save()
|
|
|
|
|
|
|
|
return get_cart_quotation(quotation)
|
|
|
|
|
|
|
|
def _apply_shipping_rule(party=None, quotation=None, cart_settings=None):
|
2015-09-17 12:59:44 +00:00
|
|
|
if not quotation.shipping_rule:
|
|
|
|
shipping_rules = get_shipping_rules(quotation, cart_settings)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
if not shipping_rules:
|
|
|
|
return
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
elif quotation.shipping_rule not in shipping_rules:
|
|
|
|
quotation.shipping_rule = shipping_rules[0]
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
if quotation.shipping_rule:
|
|
|
|
quotation.run_method("apply_shipping_rule")
|
|
|
|
quotation.run_method("calculate_taxes_and_totals")
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
def get_applicable_shipping_rules(party=None, quotation=None):
|
2015-09-17 12:59:44 +00:00
|
|
|
shipping_rules = get_shipping_rules(quotation)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
if shipping_rules:
|
|
|
|
rule_label_map = frappe.db.get_values("Shipping Rule", shipping_rules, "label")
|
|
|
|
# we need this in sorted order as per the position of the rule in the settings page
|
2019-11-07 07:08:05 +00:00
|
|
|
return [[rule, rule] for rule in shipping_rules]
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
def get_shipping_rules(quotation=None, cart_settings=None):
|
2014-10-21 10:46:30 +00:00
|
|
|
if not quotation:
|
|
|
|
quotation = _get_cart_quotation()
|
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
shipping_rules = []
|
|
|
|
if quotation.shipping_address_name:
|
|
|
|
country = frappe.db.get_value("Address", quotation.shipping_address_name, "country")
|
|
|
|
if country:
|
2022-02-02 07:28:31 +00:00
|
|
|
sr_country = frappe.qb.DocType("Shipping Rule Country")
|
|
|
|
sr = frappe.qb.DocType("Shipping Rule")
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(sr_country)
|
|
|
|
.join(sr).on(sr.name == sr_country.parent)
|
|
|
|
.select(sr.name)
|
|
|
|
.distinct()
|
|
|
|
.where(
|
|
|
|
(sr_country.country == country)
|
|
|
|
& (sr.disabled != 1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
result = query.run(as_list=True)
|
|
|
|
shipping_rules = [x[0] for x in result]
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
return shipping_rules
|
|
|
|
|
|
|
|
def get_address_territory(address_name):
|
|
|
|
"""Tries to match city, state and country of address to existing territory"""
|
|
|
|
territory = None
|
|
|
|
|
|
|
|
if address_name:
|
|
|
|
address_fields = frappe.db.get_value("Address", address_name,
|
|
|
|
["city", "state", "country"])
|
|
|
|
for value in address_fields:
|
|
|
|
territory = frappe.db.get_value("Territory", value)
|
|
|
|
if territory:
|
|
|
|
break
|
|
|
|
|
|
|
|
return territory
|
2016-03-16 12:31:22 +00:00
|
|
|
|
|
|
|
def show_terms(doc):
|
2016-04-04 13:19:26 +00:00
|
|
|
return doc.tc_name
|
2019-08-12 08:09:25 +00:00
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
2020-05-15 06:40:34 +00:00
|
|
|
def apply_coupon_code(applied_code, applied_referral_sales_partner):
|
2019-08-12 08:09:25 +00:00
|
|
|
quotation = True
|
2020-05-15 06:40:34 +00:00
|
|
|
|
|
|
|
if not applied_code:
|
|
|
|
frappe.throw(_("Please enter a coupon code"))
|
|
|
|
|
|
|
|
coupon_list = frappe.get_all('Coupon Code', filters={'coupon_code': applied_code})
|
|
|
|
if not coupon_list:
|
|
|
|
frappe.throw(_("Please enter a valid coupon code"))
|
|
|
|
|
|
|
|
coupon_name = coupon_list[0].name
|
|
|
|
|
|
|
|
from erpnext.accounts.doctype.pricing_rule.utils import validate_coupon_code
|
|
|
|
validate_coupon_code(coupon_name)
|
|
|
|
quotation = _get_cart_quotation()
|
|
|
|
quotation.coupon_code = coupon_name
|
|
|
|
quotation.flags.ignore_permissions = True
|
|
|
|
quotation.save()
|
|
|
|
|
|
|
|
if applied_referral_sales_partner:
|
|
|
|
sales_partner_list = frappe.get_all('Sales Partner', filters={'referral_code': applied_referral_sales_partner})
|
|
|
|
if sales_partner_list:
|
|
|
|
sales_partner_name = sales_partner_list[0].name
|
|
|
|
quotation.referral_sales_partner = sales_partner_name
|
2019-08-12 08:09:25 +00:00
|
|
|
quotation.flags.ignore_permissions = True
|
|
|
|
quotation.save()
|
2020-05-15 06:40:34 +00:00
|
|
|
|
2019-11-07 07:08:05 +00:00
|
|
|
return quotation
|