[enhancement] bring taxes from tax rules and test cases
This commit is contained in:
parent
957e7a37be
commit
8f7317175d
@ -115,7 +115,7 @@ def get_tax_template(posting_date, args):
|
||||
conditions.append("ifnull({0}, '') in ('', '{1}')".format(key, frappe.db.escape(cstr(value))))
|
||||
|
||||
matching = frappe.db.sql("""select * from `tabTax Rule`
|
||||
where {0}""".format(" and ".join(conditions)), as_dict = True, debug=True)
|
||||
where {0}""".format(" and ".join(conditions)), as_dict = True)
|
||||
|
||||
if not matching:
|
||||
return None
|
||||
|
@ -301,7 +301,6 @@ def set_taxes(party, party_type, posting_date, company, customer_group=None, sup
|
||||
args.update({"tax_type": "Purchase"})
|
||||
|
||||
if use_for_shopping_cart:
|
||||
print "use_for_shopping_cart", use_for_shopping_cart
|
||||
args.update({"use_for_shopping_cart": use_for_shopping_cart})
|
||||
|
||||
return get_tax_template(posting_date, args)
|
@ -35,6 +35,7 @@ def get_quotation(user=None):
|
||||
quotation = frappe.new_doc("Quotation")
|
||||
quotation.update(values)
|
||||
if party.doctype == "Customer":
|
||||
print "create quoation for cutomer"
|
||||
quotation.contact_person = frappe.db.get_value("Contact", {"customer": party.name, "email_id": user})
|
||||
quotation.insert(ignore_permissions=True)
|
||||
|
||||
|
@ -267,9 +267,11 @@ def _set_price_list(quotation, cart_settings, billing_territory):
|
||||
def set_taxes(quotation, cart_settings, billing_territory):
|
||||
"""set taxes based on billing territory"""
|
||||
from erpnext.accounts.party import set_taxes
|
||||
|
||||
|
||||
customer_group = frappe.db.get_value("Customer", quotation.customer, "customer_group")
|
||||
|
||||
quotation.taxes_and_charges = set_taxes(quotation.customer, "Customer", \
|
||||
quotation.transaction_date, quotation.company, None, None, \
|
||||
quotation.transaction_date, quotation.company, customer_group, None, \
|
||||
quotation.customer_address, quotation.shipping_address_name, 1)
|
||||
#
|
||||
# # clear table
|
||||
@ -361,7 +363,6 @@ def get_shipping_rules(party=None, quotation=None, cart_settings=None):
|
||||
shipping_territory = get_address_territory(quotation.shipping_address_name) or \
|
||||
party.territory
|
||||
|
||||
|
||||
shipping_rules = cart_settings.get_shipping_rules(shipping_territory)
|
||||
|
||||
return shipping_rules
|
||||
|
@ -200,6 +200,27 @@
|
||||
"set_only_once": 0,
|
||||
"unique": 0
|
||||
},
|
||||
{
|
||||
"allow_on_submit": 0,
|
||||
"bold": 0,
|
||||
"collapsible": 0,
|
||||
"fieldname": "column_break_10",
|
||||
"fieldtype": "Column Break",
|
||||
"hidden": 0,
|
||||
"ignore_user_permissions": 0,
|
||||
"in_filter": 0,
|
||||
"in_list_view": 0,
|
||||
"no_copy": 0,
|
||||
"permlevel": 0,
|
||||
"precision": "",
|
||||
"print_hide": 0,
|
||||
"read_only": 0,
|
||||
"report_hide": 0,
|
||||
"reqd": 0,
|
||||
"search_index": 0,
|
||||
"set_only_once": 0,
|
||||
"unique": 0
|
||||
},
|
||||
{
|
||||
"allow_on_submit": 0,
|
||||
"bold": 0,
|
||||
@ -232,7 +253,7 @@
|
||||
"is_submittable": 0,
|
||||
"issingle": 1,
|
||||
"istable": 0,
|
||||
"modified": "2015-09-10 19:05:17.386135",
|
||||
"modified": "2015-09-11 19:03:54.750937",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Shopping Cart",
|
||||
"name": "Shopping Cart Settings",
|
||||
|
@ -28,7 +28,7 @@ class ShoppingCartSettings(Document):
|
||||
doctype = self.meta.get_field(parentfield).options
|
||||
|
||||
# specify atleast one entry in the table
|
||||
# self.validate_table_has_rows(parentfield, raise_exception=ShoppingCartSetupError)
|
||||
self.validate_table_has_rows(parentfield, raise_exception=ShoppingCartSetupError)
|
||||
|
||||
territory_name_map = self.get_territory_name_map(parentfield, fieldname)
|
||||
for territory, names in territory_name_map.items():
|
||||
@ -105,7 +105,6 @@ class ShoppingCartSettings(Document):
|
||||
def get_name_from_territory(self, territory, parentfield, fieldname):
|
||||
name = None
|
||||
territory_name_map = self.get_territory_name_map(parentfield, fieldname)
|
||||
|
||||
if territory_name_map.get(territory):
|
||||
name = territory_name_map.get(territory)
|
||||
else:
|
||||
@ -164,55 +163,4 @@ def get_default_territory():
|
||||
def check_shopping_cart_enabled():
|
||||
if not get_shopping_cart_settings().enabled:
|
||||
frappe.throw(_("You need to enable Shopping Cart"), ShoppingCartSetupError)
|
||||
|
||||
def apply_shopping_cart_settings(quotation, method):
|
||||
"""Called via a validate hook on Quotation"""
|
||||
|
||||
"""no hooks call added yet. same functionality is in cart.py, which will get triggered by cart.js"""
|
||||
|
||||
from erpnext.shopping_cart import get_party
|
||||
if quotation.order_type != "Shopping Cart":
|
||||
return
|
||||
|
||||
quotation.billing_territory = (get_territory_from_address(quotation.customer_address)
|
||||
or get_party(quotation.contact_email).territory or get_default_territory())
|
||||
quotation.shipping_territory = (get_territory_from_address(quotation.shipping_address_name)
|
||||
or get_party(quotation.contact_email).territory or get_default_territory())
|
||||
|
||||
set_price_list(quotation)
|
||||
set_taxes_and_charges(quotation)
|
||||
quotation.calculate_taxes_and_totals()
|
||||
set_shipping_rule(quotation)
|
||||
|
||||
def set_price_list(quotation):
|
||||
previous_selling_price_list = quotation.selling_price_list
|
||||
quotation.selling_price_list = get_shopping_cart_settings().get_price_list(quotation.billing_territory)
|
||||
|
||||
if not quotation.selling_price_list:
|
||||
quotation.selling_price_list = get_shopping_cart_settings().get_price_list(get_default_territory())
|
||||
|
||||
if previous_selling_price_list != quotation.selling_price_list:
|
||||
quotation.price_list_currency = quotation.currency = quotation.plc_conversion_rate = quotation.conversion_rate = None
|
||||
for d in quotation.get("items"):
|
||||
d.price_list_rate = d.discount_percentage = d.rate = d.amount = None
|
||||
|
||||
quotation.set_price_list_and_item_details()
|
||||
|
||||
def set_taxes_and_charges(quotation):
|
||||
previous_taxes_and_charges = quotation.taxes_and_charges
|
||||
quotation.taxes_and_charges = get_shopping_cart_settings().get_tax_master(quotation.billing_territory)
|
||||
|
||||
if previous_taxes_and_charges != quotation.taxes_and_charges:
|
||||
quotation.set_other_charges()
|
||||
|
||||
def set_shipping_rule(quotation):
|
||||
shipping_rules = get_shopping_cart_settings().get_shipping_rules(quotation.shipping_territory)
|
||||
if not shipping_rules:
|
||||
quotation.remove_shipping_charge()
|
||||
return
|
||||
|
||||
if quotation.shipping_rule not in shipping_rules:
|
||||
quotation.remove_shipping_charge()
|
||||
quotation.shipping_rule = shipping_rules[0]
|
||||
|
||||
quotation.apply_shipping_rule()
|
||||
|
||||
|
@ -62,6 +62,5 @@ class TestShoppingCartSettings(unittest.TestCase):
|
||||
cart_settings = self.get_cart_settings()
|
||||
cart_settings.enabled = 1
|
||||
if not frappe.db.get_value("Tax Rule", {"use_for_shopping_cart": 1}, "name"):
|
||||
print "here"
|
||||
self.assertRaises(ShoppingCartSetupError, cart_settings.validate_tax_rule)
|
||||
|
||||
|
@ -4,7 +4,8 @@
|
||||
from __future__ import unicode_literals
|
||||
import unittest
|
||||
import frappe
|
||||
from erpnext.shopping_cart import get_quotation, set_item_in_cart
|
||||
from erpnext.shopping_cart import get_quotation, set_item_in_cart, get_party
|
||||
import random
|
||||
|
||||
class TestShoppingCart(unittest.TestCase):
|
||||
"""
|
||||
@ -110,42 +111,42 @@ class TestShoppingCart(unittest.TestCase):
|
||||
self.assertEquals(quotation.net_total, 0)
|
||||
self.assertEquals(len(quotation.get("items")), 0)
|
||||
|
||||
def test_taxe_rule(self):
|
||||
frappe.set_user("Administrator")
|
||||
|
||||
def test_taxe_rule(self):
|
||||
self.create_tax_rule()
|
||||
quotation = self.test_get_cart_customer()
|
||||
set_item_in_cart("_Test Item", 1)
|
||||
|
||||
self.login_as_customer()
|
||||
quotation = self.create_quotation()
|
||||
|
||||
from erpnext.accounts.party import set_taxes
|
||||
|
||||
|
||||
tax_rule_master = set_taxes(quotation.customer, "Customer", \
|
||||
quotation.transaction_date, quotation.company, None, None, \
|
||||
quotation.customer_address, quotation.shipping_address_name, 1)
|
||||
|
||||
|
||||
self.assertEquals(quotation.taxes_and_charges, tax_rule_master)
|
||||
self.assertEquals(quotation.total_taxes_and_charges, "1000")
|
||||
self.assertEquals(quotation.total_taxes_and_charges, 1000.0)
|
||||
|
||||
def create_tax_rule(self):
|
||||
for tax_rule_setting in [{"priority": 1, "use_for_shopping_cart": 1}, {"priority": 2, "use_for_shopping_cart": 0}]:
|
||||
tax_template = self.get_tax_template(tax_rule_setting['priority']).name
|
||||
print tax_template
|
||||
tax_rule = frappe.get_doc({
|
||||
"doctype": "Tax Rule",
|
||||
"tax_type" : "Sales",
|
||||
"sales_tax_template": tax_template,
|
||||
"use_for_shopping_cart": tax_rule_setting["use_for_shopping_cart"],
|
||||
"billing_city": "_Test City",
|
||||
"billing_country": "India",
|
||||
"shipping_city": "_Test City",
|
||||
"shipping_country": "India",
|
||||
"priority": tax_rule_setting['priority']
|
||||
"doctype": "Tax Rule",
|
||||
"tax_type" : "Sales",
|
||||
"sales_tax_template": tax_template,
|
||||
"use_for_shopping_cart": tax_rule_setting["use_for_shopping_cart"],
|
||||
"billing_city": "_Test City",
|
||||
"billing_country": "India",
|
||||
"shipping_city": "_Test City",
|
||||
"shipping_country": "India",
|
||||
"priority": tax_rule_setting['priority'],
|
||||
"company": frappe.get_value("Global Defaults", "Global Defaults", "default_company")
|
||||
}).insert()
|
||||
|
||||
def get_tax_template(self, priority):
|
||||
return frappe.get_doc({
|
||||
"doctype" : "Sales Taxes and Charges Template",
|
||||
"title": "_Test Tax %s"%priority,
|
||||
"company": "_Test Company",
|
||||
"company": frappe.get_value("Global Defaults", "Global Defaults", "default_company"),
|
||||
"taxes":[{
|
||||
"charge_type": "Actual",
|
||||
"account_head": "Sales Expenses - _TC",
|
||||
@ -157,6 +158,34 @@ class TestShoppingCart(unittest.TestCase):
|
||||
"territory" : "All Territories"
|
||||
}]
|
||||
}).insert()
|
||||
|
||||
def create_quotation(self):
|
||||
quotation = frappe.new_doc("Quotation")
|
||||
|
||||
values = {
|
||||
"doctype": "Quotation",
|
||||
"quotation_to": "Customer",
|
||||
"order_type": "Shopping Cart",
|
||||
"customer": get_party(frappe.session.user).name,
|
||||
"docstatus": 0,
|
||||
"contact_email": frappe.session.user,
|
||||
"selling_price_list": "_Test Price List Rest of the World",
|
||||
"currency": "USD",
|
||||
"taxes_and_charges" : "_Test Tax 1",
|
||||
"items": [{
|
||||
"item_code": "_Test Item",
|
||||
"qty": 1
|
||||
}],
|
||||
"taxes": frappe.get_doc("Sales Taxes and Charges Template", "_Test Tax 1").taxes
|
||||
}
|
||||
|
||||
quotation.update(values)
|
||||
|
||||
quotation.insert(ignore_permissions=True)
|
||||
|
||||
frappe.reload_doc("selling", "Quotation", quotation.name)
|
||||
|
||||
return quotation
|
||||
|
||||
# helper functions
|
||||
def enable_shopping_cart(self):
|
||||
|
Loading…
Reference in New Issue
Block a user