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
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import unittest
|
|
|
|
import frappe
|
2017-07-26 12:47:11 +00:00
|
|
|
from frappe.utils import nowdate, add_months
|
2016-04-04 13:19:26 +00:00
|
|
|
from erpnext.shopping_cart.cart import _get_cart_quotation, update_cart, get_party
|
2017-01-17 12:27:19 +00:00
|
|
|
from erpnext.tests.utils import create_test_contact_and_address
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2017-09-25 11:10:02 +00:00
|
|
|
|
2018-01-15 08:31:05 +00:00
|
|
|
# test_dependencies = ['Payment Terms Template']
|
2017-09-25 11:10:02 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
class TestShoppingCart(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Note:
|
|
|
|
Shopping Cart == Quotation
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
frappe.set_user("Administrator")
|
2017-01-17 16:45:17 +00:00
|
|
|
create_test_contact_and_address()
|
2014-10-21 10:46:30 +00:00
|
|
|
self.enable_shopping_cart()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
frappe.set_user("Administrator")
|
|
|
|
self.disable_shopping_cart()
|
|
|
|
|
|
|
|
def test_get_cart_new_user(self):
|
|
|
|
self.login_as_new_user()
|
|
|
|
|
|
|
|
# test if lead is created and quotation with new lead is fetched
|
2015-09-23 07:16:59 +00:00
|
|
|
quotation = _get_cart_quotation()
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(quotation.quotation_to, "Customer")
|
|
|
|
self.assertEqual(quotation.contact_person,
|
2017-01-17 12:27:19 +00:00
|
|
|
frappe.db.get_value("Contact", dict(email_id="test_cart_user@example.com")))
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(quotation.lead, None)
|
|
|
|
self.assertEqual(quotation.contact_email, frappe.session.user)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
return quotation
|
|
|
|
|
|
|
|
def test_get_cart_customer(self):
|
|
|
|
self.login_as_customer()
|
|
|
|
|
|
|
|
# test if quotation with customer is fetched
|
2015-09-23 07:16:59 +00:00
|
|
|
quotation = _get_cart_quotation()
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(quotation.quotation_to, "Customer")
|
|
|
|
self.assertEqual(quotation.customer, "_Test Customer")
|
|
|
|
self.assertEqual(quotation.lead, None)
|
|
|
|
self.assertEqual(quotation.contact_email, frappe.session.user)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
return quotation
|
|
|
|
|
|
|
|
def test_add_to_cart(self):
|
2015-09-23 07:16:59 +00:00
|
|
|
self.login_as_customer()
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-03-13 02:44:31 +00:00
|
|
|
# remove from cart
|
|
|
|
self.remove_all_items_from_cart()
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
# add first item
|
2015-09-23 07:16:59 +00:00
|
|
|
update_cart("_Test Item", 1)
|
2015-09-23 10:13:09 +00:00
|
|
|
|
2015-09-23 07:16:59 +00:00
|
|
|
quotation = self.test_get_cart_customer()
|
2015-09-23 10:13:09 +00:00
|
|
|
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(quotation.get("items")[0].item_code, "_Test Item")
|
|
|
|
self.assertEqual(quotation.get("items")[0].qty, 1)
|
|
|
|
self.assertEqual(quotation.get("items")[0].amount, 10)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
# add second item
|
2015-09-23 07:16:59 +00:00
|
|
|
update_cart("_Test Item 2", 1)
|
|
|
|
quotation = self.test_get_cart_customer()
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(quotation.get("items")[1].item_code, "_Test Item 2")
|
|
|
|
self.assertEqual(quotation.get("items")[1].qty, 1)
|
|
|
|
self.assertEqual(quotation.get("items")[1].amount, 20)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(len(quotation.get("items")), 2)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
def test_update_cart(self):
|
|
|
|
# first, add to cart
|
|
|
|
self.test_add_to_cart()
|
|
|
|
|
|
|
|
# update first item
|
2015-09-23 07:16:59 +00:00
|
|
|
update_cart("_Test Item", 5)
|
|
|
|
quotation = self.test_get_cart_customer()
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(quotation.get("items")[0].item_code, "_Test Item")
|
|
|
|
self.assertEqual(quotation.get("items")[0].qty, 5)
|
|
|
|
self.assertEqual(quotation.get("items")[0].amount, 50)
|
|
|
|
self.assertEqual(quotation.net_total, 70)
|
|
|
|
self.assertEqual(len(quotation.get("items")), 2)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
def test_remove_from_cart(self):
|
|
|
|
# first, add to cart
|
|
|
|
self.test_add_to_cart()
|
|
|
|
|
|
|
|
# remove first item
|
2015-09-23 07:16:59 +00:00
|
|
|
update_cart("_Test Item", 0)
|
|
|
|
quotation = self.test_get_cart_customer()
|
2015-09-23 10:13:09 +00:00
|
|
|
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(quotation.get("items")[0].item_code, "_Test Item 2")
|
|
|
|
self.assertEqual(quotation.get("items")[0].qty, 1)
|
|
|
|
self.assertEqual(quotation.get("items")[0].amount, 20)
|
|
|
|
self.assertEqual(quotation.net_total, 20)
|
|
|
|
self.assertEqual(len(quotation.get("items")), 1)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-17 12:59:44 +00:00
|
|
|
def test_tax_rule(self):
|
2015-09-14 05:43:01 +00:00
|
|
|
self.login_as_customer()
|
|
|
|
quotation = self.create_quotation()
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2015-09-11 10:14:06 +00:00
|
|
|
from erpnext.accounts.party import set_taxes
|
2017-01-16 06:49:07 +00:00
|
|
|
|
2019-01-06 13:14:18 +00:00
|
|
|
tax_rule_master = set_taxes(quotation.customer, "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)
|
2018-02-28 15:43:51 +00:00
|
|
|
self.assertEqual(quotation.taxes_and_charges, tax_rule_master)
|
|
|
|
self.assertEqual(quotation.total_taxes_and_charges, 1000.0)
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2015-09-14 11:29:10 +00:00
|
|
|
self.remove_test_quotation(quotation)
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2015-09-14 05:43:01 +00:00
|
|
|
def create_quotation(self):
|
|
|
|
quotation = frappe.new_doc("Quotation")
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2015-09-14 05:43:01 +00:00
|
|
|
values = {
|
|
|
|
"doctype": "Quotation",
|
|
|
|
"quotation_to": "Customer",
|
|
|
|
"order_type": "Shopping Cart",
|
2016-04-04 13:19:26 +00:00
|
|
|
"customer": get_party(frappe.session.user).name,
|
2015-09-14 05:43:01 +00:00
|
|
|
"docstatus": 0,
|
|
|
|
"contact_email": frappe.session.user,
|
|
|
|
"selling_price_list": "_Test Price List Rest of the World",
|
|
|
|
"currency": "USD",
|
2018-01-05 07:12:02 +00:00
|
|
|
"taxes_and_charges" : "_Test Tax 1 - _TC",
|
2017-11-17 09:01:09 +00:00
|
|
|
"conversion_rate":1,
|
2017-07-26 12:47:11 +00:00
|
|
|
"transaction_date" : nowdate(),
|
|
|
|
"valid_till" : add_months(nowdate(), 1),
|
2015-09-14 05:43:01 +00:00
|
|
|
"items": [{
|
|
|
|
"item_code": "_Test Item",
|
|
|
|
"qty": 1
|
|
|
|
}],
|
2018-01-05 07:12:02 +00:00
|
|
|
"taxes": frappe.get_doc("Sales Taxes and Charges Template", "_Test Tax 1 - _TC").taxes,
|
2015-09-14 11:29:10 +00:00
|
|
|
"company": "_Test Company"
|
2015-09-14 05:43:01 +00:00
|
|
|
}
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2015-09-14 05:43:01 +00:00
|
|
|
quotation.update(values)
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2015-09-14 05:43:01 +00:00
|
|
|
quotation.insert(ignore_permissions=True)
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2015-09-14 05:43:01 +00:00
|
|
|
return quotation
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2015-09-14 11:29:10 +00:00
|
|
|
def remove_test_quotation(self, quotation):
|
|
|
|
frappe.set_user("Administrator")
|
|
|
|
quotation.delete()
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
# helper functions
|
|
|
|
def enable_shopping_cart(self):
|
|
|
|
settings = frappe.get_doc("Shopping Cart Settings", "Shopping Cart Settings")
|
|
|
|
|
2015-09-23 10:13:09 +00:00
|
|
|
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()
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
settings.save()
|
|
|
|
frappe.local.shopping_cart_settings = None
|
|
|
|
|
|
|
|
def disable_shopping_cart(self):
|
|
|
|
settings = frappe.get_doc("Shopping Cart Settings", "Shopping Cart Settings")
|
|
|
|
settings.enabled = 0
|
|
|
|
settings.save()
|
|
|
|
frappe.local.shopping_cart_settings = None
|
|
|
|
|
|
|
|
def login_as_new_user(self):
|
2016-01-19 13:08:08 +00:00
|
|
|
self.create_user_if_not_exists("test_cart_user@example.com")
|
2014-10-21 10:46:30 +00:00
|
|
|
frappe.set_user("test_cart_user@example.com")
|
|
|
|
|
|
|
|
def login_as_customer(self):
|
2016-03-31 09:32:11 +00:00
|
|
|
self.create_user_if_not_exists("test_contact_customer@example.com",
|
|
|
|
"_Test Contact For _Test Customer")
|
2014-10-21 10:46:30 +00:00
|
|
|
frappe.set_user("test_contact_customer@example.com")
|
|
|
|
|
2015-03-13 02:44:31 +00:00
|
|
|
def remove_all_items_from_cart(self):
|
2015-09-23 07:16:59 +00:00
|
|
|
quotation = _get_cart_quotation()
|
2016-04-01 10:37:34 +00:00
|
|
|
quotation.flags.ignore_permissions=True
|
2016-03-31 11:25:21 +00:00
|
|
|
quotation.delete()
|
2015-09-17 12:59:44 +00:00
|
|
|
|
2016-03-31 09:32:11 +00:00
|
|
|
def create_user_if_not_exists(self, email, first_name = None):
|
2016-01-19 13:08:08 +00:00
|
|
|
if frappe.db.exists("User", email):
|
|
|
|
return
|
|
|
|
|
|
|
|
frappe.get_doc({
|
|
|
|
"doctype": "User",
|
|
|
|
"user_type": "Website User",
|
|
|
|
"email": email,
|
|
|
|
"send_welcome_email": 0,
|
2016-03-31 09:32:11 +00:00
|
|
|
"first_name": first_name or email.split("@")[0]
|
2016-01-19 13:08:08 +00:00
|
|
|
}).insert(ignore_permissions=True)
|
|
|
|
|
2015-05-06 13:15:47 +00:00
|
|
|
test_dependencies = ["Sales Taxes and Charges Template", "Price List", "Item Price", "Shipping Rule", "Currency Exchange",
|
2015-09-14 11:29:10 +00:00
|
|
|
"Customer Group", "Lead", "Customer", "Contact", "Address", "Item", "Tax Rule"]
|