style: format code with black

This commit is contained in:
Ankush Menat
2022-03-28 18:52:46 +05:30
parent 21e00da3d6
commit 494bd9ef78
1395 changed files with 91704 additions and 62532 deletions

View File

@@ -19,6 +19,7 @@ from erpnext.utilities.product import get_web_item_qty_in_stock
class WebsitePriceListMissingError(frappe.ValidationError):
pass
def set_cart_count(quotation=None):
if cint(frappe.db.get_singles_value("E Commerce Settings", "enabled")):
if not quotation:
@@ -28,6 +29,7 @@ def set_cart_count(quotation=None):
if hasattr(frappe.local, "cookie_manager"):
frappe.local.cookie_manager.set_cookie("cart_count", cart_count)
@frappe.whitelist()
def get_cart_quotation(doc=None):
party = get_party()
@@ -47,38 +49,46 @@ def get_cart_quotation(doc=None):
"shipping_addresses": get_shipping_addresses(party),
"billing_addresses": get_billing_addresses(party),
"shipping_rules": get_applicable_shipping_rules(party),
"cart_settings": frappe.get_cached_doc("E Commerce Settings")
"cart_settings": frappe.get_cached_doc("E Commerce Settings"),
}
@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"
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"
return [
{"name": address.name, "title": address.address_title, "display": address.display}
for address in addresses
if address.address_type == "Billing"
]
@frappe.whitelist()
def place_order():
quotation = _get_cart_quotation()
cart_settings = frappe.db.get_value("E Commerce Settings", None,
["company", "allow_items_not_in_stock"], as_dict=1)
cart_settings = frappe.db.get_value(
"E Commerce Settings", None, ["company", "allow_items_not_in_stock"], as_dict=1
)
quotation.company = cart_settings.company
quotation.flags.ignore_permissions = True
quotation.submit()
if quotation.quotation_to == 'Lead' and quotation.party_name:
if quotation.quotation_to == "Lead" and quotation.party_name:
# company used to create customer accounts
frappe.defaults.set_user_default("company", quotation.company)
@@ -86,17 +96,14 @@ def place_order():
frappe.throw(_("Set Shipping Address or Billing Address"))
from erpnext.selling.doctype.quotation.quotation import _make_sales_order
sales_order = frappe.get_doc(_make_sales_order(quotation.name, ignore_permissions=True))
sales_order.payment_schedule = []
if not cint(cart_settings.allow_items_not_in_stock):
for item in sales_order.get("items"):
item.warehouse = frappe.db.get_value(
"Website Item",
{
"item_code": item.item_code
},
"website_warehouse"
"Website Item", {"item_code": item.item_code}, "website_warehouse"
)
is_stock_item = frappe.db.get_value("Item", item.item_code, "is_stock_item")
@@ -116,6 +123,7 @@ def place_order():
return sales_order.name
@frappe.whitelist()
def request_for_quotation():
quotation = _get_cart_quotation()
@@ -127,6 +135,7 @@ def request_for_quotation():
quotation.submit()
return quotation.name
@frappe.whitelist()
def update_cart(item_code, qty, additional_notes=None, with_items=False):
quotation = _get_cart_quotation()
@@ -143,12 +152,15 @@ def update_cart(item_code, qty, additional_notes=None, with_items=False):
else:
quotation_items = quotation.get("items", {"item_code": item_code})
if not quotation_items:
quotation.append("items", {
"doctype": "Quotation Item",
"item_code": item_code,
"qty": qty,
"additional_notes": additional_notes
})
quotation.append(
"items",
{
"doctype": "Quotation Item",
"item_code": item_code,
"qty": qty,
"additional_notes": additional_notes,
},
)
else:
quotation_items[0].qty = qty
quotation_items[0].additional_notes = additional_notes
@@ -168,73 +180,75 @@ def update_cart(item_code, qty, additional_notes=None, with_items=False):
if cint(with_items):
context = get_cart_quotation(quotation)
return {
"items": frappe.render_template("templates/includes/cart/cart_items.html",
context),
"total": frappe.render_template("templates/includes/cart/cart_items_total.html",
context),
"taxes_and_totals": frappe.render_template("templates/includes/cart/cart_payment_summary.html",
context)
"items": frappe.render_template("templates/includes/cart/cart_items.html", context),
"total": frappe.render_template("templates/includes/cart/cart_items_total.html", context),
"taxes_and_totals": frappe.render_template(
"templates/includes/cart/cart_payment_summary.html", context
),
}
else:
return {
'name': quotation.name
}
return {"name": quotation.name}
@frappe.whitelist()
def get_shopping_cart_menu(context=None):
if not context:
context = get_cart_quotation()
return frappe.render_template('templates/includes/cart/cart_dropdown.html', context)
return frappe.render_template("templates/includes/cart/cart_dropdown.html", context)
@frappe.whitelist()
def add_new_address(doc):
doc = frappe.parse_json(doc)
doc.update({
'doctype': 'Address'
})
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')
lead_doc = frappe.new_doc("Lead")
for fieldname in ("lead_name", "company_name", "email_id", "phone"):
lead_doc.set(fieldname, lead.get(fieldname))
lead_doc.set('lead_owner', '')
lead_doc.set("lead_owner", "")
if not frappe.db.exists('Lead Source', 'Product Inquiry'):
frappe.get_doc({
'doctype': 'Lead Source',
'source_name' : 'Product Inquiry'
}).insert(ignore_permissions=True)
if not frappe.db.exists("Lead Source", "Product Inquiry"):
frappe.get_doc({"doctype": "Lead Source", "source_name": "Product Inquiry"}).insert(
ignore_permissions=True
)
lead_doc.set('source', 'Product Inquiry')
lead_doc.set("source", "Product Inquiry")
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 = frappe.get_doc("Lead", {"email_id": lead["email_id"]})
lead_doc.add_comment('Comment', text='''
lead_doc.add_comment(
"Comment",
text="""
<div>
<h5>{subject}</h5>
<p>{message}</p>
</div>
'''.format(subject=subject, message=message))
""".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')
return frappe.db.get_value("Terms and Conditions", terms_name, "terms")
@frappe.whitelist()
def update_cart_address(address_type, address_name):
@@ -251,31 +265,35 @@ def update_cart_address(address_type, address_name):
quotation.shipping_address_name = address_name
quotation.shipping_address = address_display
quotation.customer_address = quotation.customer_address or address_name
address_doc = next((doc for doc in get_shipping_addresses() if doc["name"] == address_name), None)
address_doc = next(
(doc for doc in get_shipping_addresses() if doc["name"] == address_name), None
)
apply_cart_settings(quotation=quotation)
quotation.flags.ignore_permissions = True
quotation.save()
context = get_cart_quotation(quotation)
context['address'] = address_doc
context["address"] = address_doc
return {
"taxes": frappe.render_template("templates/includes/order/order_taxes.html",
context),
"address": frappe.render_template("templates/includes/cart/address_card.html",
context)
"taxes": frappe.render_template("templates/includes/order/order_taxes.html", context),
"address": frappe.render_template("templates/includes/cart/address_card.html", context),
}
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 \
frappe.db.get_value("E Commerce Settings", None, "territory") or \
get_root_of("Territory")
return (
territory
or frappe.db.get_value("E Commerce Settings", None, "territory")
or get_root_of("Territory")
)
def decorate_quotation_doc(doc):
for d in doc.get("items", []):
@@ -288,50 +306,56 @@ def decorate_quotation_doc(doc):
"Item",
filters={"item_code": item_code},
fieldname=["variant_of", "item_name", "image"],
as_dict=True
as_dict=True,
)[0]
item_code = variant_data.variant_of
fields = fields[1:]
d.web_item_name = variant_data.item_name
if variant_data.image: # get image from variant or template web item
if variant_data.image: # get image from variant or template web item
d.thumbnail = variant_data.image
fields = fields[2:]
d.update(frappe.db.get_value(
"Website Item",
{"item_code": item_code},
fields,
as_dict=True)
)
d.update(frappe.db.get_value("Website Item", {"item_code": item_code}, fields, as_dict=True))
return doc
def _get_cart_quotation(party=None):
'''Return the open Quotation of type "Shopping Cart" or make a new one'''
"""Return the open Quotation of type "Shopping Cart" or make a new one"""
if not party:
party = get_party()
quotation = frappe.get_all("Quotation", fields=["name"], filters=
{"party_name": party.name, "contact_email": frappe.session.user, "order_type": "Shopping Cart", "docstatus": 0},
order_by="modified desc", limit_page_length=1)
quotation = frappe.get_all(
"Quotation",
fields=["name"],
filters={
"party_name": party.name,
"contact_email": frappe.session.user,
"order_type": "Shopping Cart",
"docstatus": 0,
},
order_by="modified desc",
limit_page_length=1,
)
if quotation:
qdoc = frappe.get_doc("Quotation", quotation[0].name)
else:
company = frappe.db.get_value("E Commerce Settings", None, ["company"])
qdoc = frappe.get_doc({
"doctype": "Quotation",
"naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-",
"quotation_to": party.doctype,
"company": company,
"order_type": "Shopping Cart",
"status": "Draft",
"docstatus": 0,
"__islocal": 1,
"party_name": party.name
})
qdoc = frappe.get_doc(
{
"doctype": "Quotation",
"naming_series": get_shopping_cart_settings().quotation_series or "QTN-CART-",
"quotation_to": party.doctype,
"company": company,
"order_type": "Shopping Cart",
"status": "Draft",
"docstatus": 0,
"__islocal": 1,
"party_name": party.name,
}
)
qdoc.contact_person = frappe.db.get_value("Contact", {"email_id": frappe.session.user})
qdoc.contact_email = frappe.session.user
@@ -342,6 +366,7 @@ def _get_cart_quotation(party=None):
return qdoc
def update_party(fullname, company_name=None, mobile_no=None, phone=None):
party = get_party()
@@ -369,6 +394,7 @@ def update_party(fullname, company_name=None, mobile_no=None, phone=None):
qdoc.flags.ignore_permissions = True
qdoc.save()
def apply_cart_settings(party=None, quotation=None):
if not party:
party = get_party()
@@ -385,14 +411,16 @@ def apply_cart_settings(party=None, quotation=None):
_apply_shipping_rule(party, quotation, cart_settings)
def set_price_list_and_rate(quotation, cart_settings):
"""set price list based on billing territory"""
_set_price_list(cart_settings, quotation)
# reset values
quotation.price_list_currency = quotation.currency = \
quotation.plc_conversion_rate = quotation.conversion_rate = None
quotation.price_list_currency = (
quotation.currency
) = quotation.plc_conversion_rate = quotation.conversion_rate = None
for item in quotation.get("items"):
item.price_list_rate = item.discount_percentage = item.rate = item.amount = None
@@ -403,9 +431,11 @@ def set_price_list_and_rate(quotation, cart_settings):
# set it in cookies for using in product page
frappe.local.cookie_manager.set_cookie("selling_price_list", quotation.selling_price_list)
def _set_price_list(cart_settings, quotation=None):
"""Set price list based on customer or shopping cart default"""
from erpnext.accounts.party import get_default_price_list
party_name = quotation.get("party_name") if quotation else get_party().get("name")
selling_price_list = None
@@ -422,23 +452,33 @@ def _set_price_list(cart_settings, quotation=None):
return selling_price_list
def set_taxes(quotation, cart_settings):
"""set taxes based on billing territory"""
from erpnext.accounts.party import set_taxes
customer_group = frappe.db.get_value("Customer", quotation.party_name, "customer_group")
quotation.taxes_and_charges = set_taxes(quotation.party_name, "Customer",
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)
#
# # clear table
quotation.taxes_and_charges = set_taxes(
quotation.party_name,
"Customer",
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,
)
#
# # clear table
quotation.set("taxes", [])
#
# # append taxes
#
# # append taxes
quotation.append_taxes_from_master()
def get_party(user=None):
if not user:
user = frappe.session.user
@@ -447,14 +487,14 @@ def get_party(user=None):
party = None
if contact_name:
contact = frappe.get_doc('Contact', 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
cart_settings = frappe.get_doc("E Commerce Settings")
debtors_account = ''
debtors_account = ""
if cart_settings.enable_checkout:
debtors_account = get_debtors_account(cart_settings)
@@ -468,57 +508,62 @@ def get_party(user=None):
raise frappe.Redirect
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")
})
customer.update(
{
"customer_name": fullname,
"customer_type": "Individual",
"customer_group": get_shopping_cart_settings().default_customer_group,
"territory": get_root_of("Territory"),
}
)
if debtors_account:
customer.update({
"accounts": [{
"company": cart_settings.company,
"account": debtors_account
}]
})
customer.update({"accounts": [{"company": cart_settings.company, "account": debtors_account}]})
customer.flags.ignore_mandatory = True
customer.insert(ignore_permissions=True)
contact = frappe.new_doc("Contact")
contact.update({
"first_name": fullname,
"email_ids": [{"email_id": user, "is_primary": 1}]
})
contact.append('links', dict(link_doctype='Customer', link_name=customer.name))
contact.update({"first_name": fullname, "email_ids": [{"email_id": user, "is_primary": 1}]})
contact.append("links", dict(link_doctype="Customer", link_name=customer.name))
contact.flags.ignore_mandatory = True
contact.insert(ignore_permissions=True)
return customer
def get_debtors_account(cart_settings):
if not cart_settings.payment_gateway_account:
frappe.throw(_("Payment Gateway Account not set"), _("Mandatory"))
payment_gateway_account_currency = \
frappe.get_doc("Payment Gateway Account", cart_settings.payment_gateway_account).currency
payment_gateway_account_currency = frappe.get_doc(
"Payment Gateway Account", cart_settings.payment_gateway_account
).currency
account_name = _("Debtors ({0})").format(payment_gateway_account_currency)
debtors_account_name = get_account_name("Receivable", "Asset", is_group=0,\
account_currency=payment_gateway_account_currency, company=cart_settings.company)
debtors_account_name = get_account_name(
"Receivable",
"Asset",
is_group=0,
account_currency=payment_gateway_account_currency,
company=cart_settings.company,
)
if not debtors_account_name:
debtors_account = frappe.get_doc({
"doctype": "Account",
"account_type": "Receivable",
"root_type": "Asset",
"is_group": 0,
"parent_account": get_account_name(root_type="Asset", is_group=1, company=cart_settings.company),
"account_name": account_name,
"currency": payment_gateway_account_currency
}).insert(ignore_permissions=True)
debtors_account = frappe.get_doc(
{
"doctype": "Account",
"account_type": "Receivable",
"root_type": "Asset",
"is_group": 0,
"parent_account": get_account_name(
root_type="Asset", is_group=1, company=cart_settings.company
),
"account_name": account_name,
"currency": payment_gateway_account_currency,
}
).insert(ignore_permissions=True)
return debtors_account.name
@@ -526,26 +571,31 @@ def get_debtors_account(cart_settings):
return debtors_account_name
def get_address_docs(doctype=None, txt=None, filters=None, limit_start=0, limit_page_length=20,
party=None):
def get_address_docs(
doctype=None, txt=None, filters=None, limit_start=0, limit_page_length=20, party=None
):
if not party:
party = get_party()
if not party:
return []
address_names = frappe.db.get_all('Dynamic Link', fields=('parent'),
filters=dict(parenttype='Address', link_doctype=party.doctype, link_name=party.name))
address_names = frappe.db.get_all(
"Dynamic Link",
fields=("parent"),
filters=dict(parenttype="Address", link_doctype=party.doctype, link_name=party.name),
)
out = []
for a in address_names:
address = frappe.get_doc('Address', a.parent)
address = frappe.get_doc("Address", a.parent)
address.display = get_address_display(address.as_dict())
out.append(address)
return out
@frappe.whitelist()
def apply_shipping_rule(shipping_rule):
quotation = _get_cart_quotation()
@@ -559,6 +609,7 @@ def apply_shipping_rule(shipping_rule):
return get_cart_quotation(quotation)
def _apply_shipping_rule(party=None, quotation=None, cart_settings=None):
if not quotation.shipping_rule:
shipping_rules = get_shipping_rules(quotation, cart_settings)
@@ -573,6 +624,7 @@ def _apply_shipping_rule(party=None, quotation=None, cart_settings=None):
quotation.run_method("apply_shipping_rule")
quotation.run_method("calculate_taxes_and_totals")
def get_applicable_shipping_rules(party=None, quotation=None):
shipping_rules = get_shipping_rules(quotation)
@@ -581,6 +633,7 @@ def get_applicable_shipping_rules(party=None, quotation=None):
# we need this in sorted order as per the position of the rule in the settings page
return [[rule, rule] for rule in shipping_rules]
def get_shipping_rules(quotation=None, cart_settings=None):
if not quotation:
quotation = _get_cart_quotation()
@@ -593,26 +646,24 @@ def get_shipping_rules(quotation=None, cart_settings=None):
sr = frappe.qb.DocType("Shipping Rule")
query = (
frappe.qb.from_(sr_country)
.join(sr).on(sr.name == sr_country.parent)
.join(sr)
.on(sr.name == sr_country.parent)
.select(sr.name)
.distinct()
.where(
(sr_country.country == country)
& (sr.disabled != 1)
)
.where((sr_country.country == country) & (sr.disabled != 1))
)
result = query.run(as_list=True)
shipping_rules = [x[0] for x in result]
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"])
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:
@@ -620,9 +671,11 @@ def get_address_territory(address_name):
return territory
def show_terms(doc):
return doc.tc_name
@frappe.whitelist(allow_guest=True)
def apply_coupon_code(applied_code, applied_referral_sales_partner):
quotation = True
@@ -630,13 +683,14 @@ def apply_coupon_code(applied_code, applied_referral_sales_partner):
if not applied_code:
frappe.throw(_("Please enter a coupon code"))
coupon_list = frappe.get_all('Coupon Code', filters={'coupon_code': applied_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
@@ -644,7 +698,9 @@ def apply_coupon_code(applied_code, applied_referral_sales_partner):
quotation.save()
if applied_referral_sales_partner:
sales_partner_list = frappe.get_all('Sales Partner', filters={'referral_code': 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

View File

@@ -22,16 +22,17 @@ def get_product_info_for_website(item_code, skip_quotation_creation=False):
cart_settings = get_shopping_cart_settings()
if not cart_settings.enabled:
# return settings even if cart is disabled
return frappe._dict({
"product_info": {},
"cart_settings": cart_settings
})
return frappe._dict({"product_info": {}, "cart_settings": cart_settings})
cart_quotation = frappe._dict()
if not skip_quotation_creation:
cart_quotation = _get_cart_quotation()
selling_price_list = cart_quotation.get("selling_price_list") if cart_quotation else _set_price_list(cart_settings, None)
selling_price_list = (
cart_quotation.get("selling_price_list")
if cart_quotation
else _set_price_list(cart_settings, None)
)
price = {}
if cart_settings.show_price:
@@ -40,10 +41,7 @@ def get_product_info_for_website(item_code, skip_quotation_creation=False):
# If not logged in, check if price is hidden for guest.
if not is_guest or not cart_settings.hide_price_for_guest:
price = get_price(
item_code,
selling_price_list,
cart_settings.default_customer_group,
cart_settings.company
item_code, selling_price_list, cart_settings.default_customer_group, cart_settings.company
)
stock_status = None
@@ -59,7 +57,7 @@ def get_product_info_for_website(item_code, skip_quotation_creation=False):
"price": price,
"qty": 0,
"uom": frappe.db.get_value("Item", item_code, "stock_uom"),
"sales_uom": frappe.db.get_value("Item", item_code, "sales_uom")
"sales_uom": frappe.db.get_value("Item", item_code, "sales_uom"),
}
if stock_status:
@@ -67,7 +65,11 @@ def get_product_info_for_website(item_code, skip_quotation_creation=False):
product_info["on_backorder"] = True
else:
product_info["stock_qty"] = stock_status.stock_qty
product_info["in_stock"] = stock_status.in_stock if stock_status.is_stock_item else get_non_stock_item_status(item_code, "website_warehouse")
product_info["in_stock"] = (
stock_status.in_stock
if stock_status.is_stock_item
else get_non_stock_item_status(item_code, "website_warehouse")
)
product_info["show_stock_qty"] = show_quantity_in_website()
if product_info["price"]:
@@ -76,14 +78,14 @@ def get_product_info_for_website(item_code, skip_quotation_creation=False):
if item:
product_info["qty"] = item[0].qty
return frappe._dict({
"product_info": product_info,
"cart_settings": cart_settings
})
return frappe._dict({"product_info": product_info, "cart_settings": cart_settings})
def set_product_info_for_website(item):
"""set product price uom for website"""
product_info = get_product_info_for_website(item.item_code, skip_quotation_creation=True).get("product_info")
product_info = get_product_info_for_website(item.item_code, skip_quotation_creation=True).get(
"product_info"
)
if product_info:
item.update(product_info)

View File

@@ -22,18 +22,19 @@ from erpnext.tests.utils import create_test_contact_and_address
class TestShoppingCart(unittest.TestCase):
"""
Note:
Shopping Cart == Quotation
Note:
Shopping Cart == Quotation
"""
def setUp(self):
frappe.set_user("Administrator")
create_test_contact_and_address()
self.enable_shopping_cart()
if not frappe.db.exists("Website Item", {"item_code": "_Test Item"}):
make_website_item(frappe.get_cached_doc("Item", "_Test Item"))
make_website_item(frappe.get_cached_doc("Item", "_Test Item"))
if not frappe.db.exists("Website Item", {"item_code": "_Test Item 2"}):
make_website_item(frappe.get_cached_doc("Item", "_Test Item 2"))
make_website_item(frappe.get_cached_doc("Item", "_Test Item 2"))
def tearDown(self):
frappe.db.rollback()
@@ -50,8 +51,10 @@ class TestShoppingCart(unittest.TestCase):
# test if lead is created and quotation with new lead is fetched
quotation = _get_cart_quotation()
self.assertEqual(quotation.quotation_to, "Customer")
self.assertEqual(quotation.contact_person,
frappe.db.get_value("Contact", dict(email_id="test_cart_user@example.com")))
self.assertEqual(
quotation.contact_person,
frappe.db.get_value("Contact", dict(email_id="test_cart_user@example.com")),
)
self.assertEqual(quotation.contact_email, frappe.session.user)
return quotation
@@ -65,7 +68,9 @@ class TestShoppingCart(unittest.TestCase):
self.assertEqual(quotation.contact_email, frappe.session.user)
return quotation
self.login_as_customer("test_contact_two_customer@example.com", "_Test Contact 2 For _Test Customer")
self.login_as_customer(
"test_contact_two_customer@example.com", "_Test Contact 2 For _Test Customer"
)
validate_quotation()
self.login_as_customer()
@@ -131,46 +136,55 @@ class TestShoppingCart(unittest.TestCase):
from erpnext.accounts.party import set_taxes
tax_rule_master = set_taxes(quotation.party_name, "Customer",
quotation.transaction_date, quotation.company, customer_group=None, supplier_group=None,
tax_category=quotation.tax_category, billing_address=quotation.customer_address,
shipping_address=quotation.shipping_address_name, use_for_shopping_cart=1)
tax_rule_master = set_taxes(
quotation.party_name,
"Customer",
quotation.transaction_date,
quotation.company,
customer_group=None,
supplier_group=None,
tax_category=quotation.tax_category,
billing_address=quotation.customer_address,
shipping_address=quotation.shipping_address_name,
use_for_shopping_cart=1,
)
self.assertEqual(quotation.taxes_and_charges, tax_rule_master)
self.assertEqual(quotation.total_taxes_and_charges, 1000.0)
self.remove_test_quotation(quotation)
@change_settings("E Commerce Settings",{
"company": "_Test Company",
"enabled": 1,
"default_customer_group": "_Test Customer Group",
"price_list": "_Test Price List India",
"show_price": 1
})
@change_settings(
"E Commerce Settings",
{
"company": "_Test Company",
"enabled": 1,
"default_customer_group": "_Test Customer Group",
"price_list": "_Test Price List India",
"show_price": 1,
},
)
def test_add_item_variant_without_web_item_to_cart(self):
"Test adding Variants having no Website Items in cart via Template Web Item."
from erpnext.controllers.item_variant import create_variant
from erpnext.e_commerce.doctype.website_item.website_item import make_website_item
from erpnext.stock.doctype.item.test_item import make_item
template_item = make_item("Test-Tshirt-Temp", {
"has_variant": 1,
"variant_based_on": "Item Attribute",
"attributes": [
{"attribute": "Test Size"},
{"attribute": "Test Colour"}
]
})
variant = create_variant("Test-Tshirt-Temp", {
"Test Size": "Small", "Test Colour": "Red"
})
template_item = make_item(
"Test-Tshirt-Temp",
{
"has_variant": 1,
"variant_based_on": "Item Attribute",
"attributes": [{"attribute": "Test Size"}, {"attribute": "Test Colour"}],
},
)
variant = create_variant("Test-Tshirt-Temp", {"Test Size": "Small", "Test Colour": "Red"})
variant.save()
make_website_item(template_item) # publish template not variant
make_website_item(template_item) # publish template not variant
update_cart("Test-Tshirt-Temp-S-R", 1)
cart = get_cart_quotation() # test if cart page gets data without errors
cart = get_cart_quotation() # test if cart page gets data without errors
doc = cart.get("doc")
self.assertEqual(doc.get("items")[0].item_name, "Test-Tshirt-Temp-S-R")
@@ -178,16 +192,14 @@ class TestShoppingCart(unittest.TestCase):
# test if items are rendered without error
frappe.render_template("templates/includes/cart/cart_items.html", cart)
@change_settings("E Commerce Settings",{
"save_quotations_as_draft": 1
})
@change_settings("E Commerce Settings", {"save_quotations_as_draft": 1})
def test_cart_without_checkout_and_draft_quotation(self):
"Test impact of 'save_quotations_as_draft' checkbox."
frappe.local.shopping_cart_settings = None
# add item to cart
update_cart("_Test Item", 1)
quote_name = request_for_quotation() # Request for Quote
quote_name = request_for_quotation() # Request for Quote
quote_doctstatus = cint(frappe.db.get_value("Quotation", quote_name, "docstatus"))
self.assertEqual(quote_doctstatus, 0)
@@ -195,7 +207,7 @@ class TestShoppingCart(unittest.TestCase):
frappe.db.set_value("E Commerce Settings", None, "save_quotations_as_draft", 0)
frappe.local.shopping_cart_settings = None
update_cart("_Test Item", 1)
quote_name = request_for_quotation() # Request for Quote
quote_name = request_for_quotation() # Request for Quote
quote_doctstatus = cint(frappe.db.get_value("Quotation", quote_name, "docstatus"))
self.assertEqual(quote_doctstatus, 1)
@@ -219,16 +231,13 @@ class TestShoppingCart(unittest.TestCase):
"contact_email": frappe.session.user,
"selling_price_list": "_Test Price List Rest of the World",
"currency": "USD",
"taxes_and_charges" : "_Test Tax 1 - _TC",
"conversion_rate":1,
"transaction_date" : nowdate(),
"valid_till" : add_months(nowdate(), 1),
"items": [{
"item_code": "_Test Item",
"qty": 1
}],
"taxes_and_charges": "_Test Tax 1 - _TC",
"conversion_rate": 1,
"transaction_date": nowdate(),
"valid_till": add_months(nowdate(), 1),
"items": [{"item_code": "_Test Item", "qty": 1}],
"taxes": frappe.get_doc("Sales Taxes and Charges Template", "_Test Tax 1 - _TC").taxes,
"company": "_Test Company"
"company": "_Test Company",
}
quotation.update(values)
@@ -245,29 +254,36 @@ class TestShoppingCart(unittest.TestCase):
def enable_shopping_cart(self):
settings = frappe.get_doc("E Commerce Settings", "E Commerce Settings")
settings.update({
"enabled": 1,
"company": "_Test Company",
"default_customer_group": "_Test Customer Group",
"quotation_series": "_T-Quotation-",
"price_list": "_Test Price List India"
})
settings.update(
{
"enabled": 1,
"company": "_Test Company",
"default_customer_group": "_Test Customer Group",
"quotation_series": "_T-Quotation-",
"price_list": "_Test Price List India",
}
)
# insert item price
if not frappe.db.get_value("Item Price", {"price_list": "_Test Price List India",
"item_code": "_Test Item"}):
frappe.get_doc({
"doctype": "Item Price",
"price_list": "_Test Price List India",
"item_code": "_Test Item",
"price_list_rate": 10
}).insert()
frappe.get_doc({
"doctype": "Item Price",
"price_list": "_Test Price List India",
"item_code": "_Test Item 2",
"price_list_rate": 20
}).insert()
if not frappe.db.get_value(
"Item Price", {"price_list": "_Test Price List India", "item_code": "_Test Item"}
):
frappe.get_doc(
{
"doctype": "Item Price",
"price_list": "_Test Price List India",
"item_code": "_Test Item",
"price_list_rate": 10,
}
).insert()
frappe.get_doc(
{
"doctype": "Item Price",
"price_list": "_Test Price List India",
"item_code": "_Test Item 2",
"price_list_rate": 20,
}
).insert()
settings.save()
frappe.local.shopping_cart_settings = None
@@ -282,31 +298,49 @@ class TestShoppingCart(unittest.TestCase):
self.create_user_if_not_exists("test_cart_user@example.com")
frappe.set_user("test_cart_user@example.com")
def login_as_customer(self, email="test_contact_customer@example.com", name="_Test Contact For _Test Customer"):
def login_as_customer(
self, email="test_contact_customer@example.com", name="_Test Contact For _Test Customer"
):
self.create_user_if_not_exists(email, name)
frappe.set_user(email)
def clear_existing_quotations(self):
quotations = frappe.get_all("Quotation", filters={
"party_name": get_party().name,
"order_type": "Shopping Cart",
"docstatus": 0
}, order_by="modified desc", pluck="name")
quotations = frappe.get_all(
"Quotation",
filters={"party_name": get_party().name, "order_type": "Shopping Cart", "docstatus": 0},
order_by="modified desc",
pluck="name",
)
for quotation in quotations:
frappe.delete_doc("Quotation", quotation, ignore_permissions=True, force=True)
def create_user_if_not_exists(self, email, first_name = None):
def create_user_if_not_exists(self, email, first_name=None):
if frappe.db.exists("User", email):
return
frappe.get_doc({
"doctype": "User",
"user_type": "Website User",
"email": email,
"send_welcome_email": 0,
"first_name": first_name or email.split("@")[0]
}).insert(ignore_permissions=True)
frappe.get_doc(
{
"doctype": "User",
"user_type": "Website User",
"email": email,
"send_welcome_email": 0,
"first_name": first_name or email.split("@")[0],
}
).insert(ignore_permissions=True)
test_dependencies = ["Sales Taxes and Charges Template", "Price List", "Item Price", "Shipping Rule", "Currency Exchange",
"Customer Group", "Lead", "Customer", "Contact", "Address", "Item", "Tax Rule"]
test_dependencies = [
"Sales Taxes and Charges Template",
"Price List",
"Item Price",
"Shipping Rule",
"Currency Exchange",
"Customer Group",
"Lead",
"Customer",
"Contact",
"Address",
"Item",
"Tax Rule",
]

View File

@@ -6,12 +6,15 @@ from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import i
def show_cart_count():
if (is_cart_enabled() and
frappe.db.get_value("User", frappe.session.user, "user_type") == "Website User"):
if (
is_cart_enabled()
and frappe.db.get_value("User", frappe.session.user, "user_type") == "Website User"
):
return True
return False
def set_cart_count(login_manager):
# since this is run only on hooks login event
# make sure user is already a customer
@@ -28,21 +31,24 @@ def set_cart_count(login_manager):
# cart count is calculated from this quotation's items
set_cart_count()
def clear_cart_count(login_manager):
if show_cart_count():
frappe.local.cookie_manager.delete_cookie("cart_count")
def update_website_context(context):
cart_enabled = is_cart_enabled()
context["shopping_cart_enabled"] = cart_enabled
def is_customer():
if frappe.session.user and frappe.session.user != "Guest":
contact_name = frappe.get_value("Contact", {"email_id": frappe.session.user})
if contact_name:
contact = frappe.get_doc('Contact', contact_name)
contact = frappe.get_doc("Contact", contact_name)
for link in contact.links:
if link.link_doctype == 'Customer':
if link.link_doctype == "Customer":
return True
return False