mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-08 23:52:57 +00:00
[wip] shopping cart shipping rule, price list cleanup
This commit is contained in:
@@ -28,6 +28,8 @@ def get_cart_quotation(doc=None):
|
||||
doc = quotation
|
||||
set_cart_count(quotation)
|
||||
|
||||
print get_applicable_shipping_rules(party)
|
||||
|
||||
return {
|
||||
"doc": decorate_quotation_doc(doc),
|
||||
"addresses": [{"name": address.name, "display": address.display}
|
||||
@@ -63,7 +65,7 @@ def place_order():
|
||||
return sales_order.name
|
||||
|
||||
@frappe.whitelist()
|
||||
def update_cart(item_code, qty, with_doc):
|
||||
def update_cart(item_code, qty, with_items=False):
|
||||
quotation = _get_cart_quotation()
|
||||
|
||||
qty = flt(qty)
|
||||
@@ -95,8 +97,15 @@ def update_cart(item_code, qty, with_doc):
|
||||
|
||||
set_cart_count(quotation)
|
||||
|
||||
if with_doc:
|
||||
return get_cart_quotation(quotation)
|
||||
|
||||
if with_items:
|
||||
context = get_cart_quotation(quotation)
|
||||
return {
|
||||
"items": frappe.render_template("templates/includes/cart/cart_items.html",
|
||||
context),
|
||||
"taxes": frappe.render_template("templates/includes/order/order_taxes.html",
|
||||
context),
|
||||
}
|
||||
else:
|
||||
return quotation.name
|
||||
|
||||
|
||||
@@ -8,8 +8,7 @@ import frappe
|
||||
from frappe import _, msgprint
|
||||
from frappe.utils import comma_and
|
||||
from frappe.model.document import Document
|
||||
from frappe.utils.nestedset import get_ancestors_of, get_root_of
|
||||
from erpnext.utilities.doctype.address.address import get_territory_from_address
|
||||
from frappe.utils.nestedset import get_root_of
|
||||
|
||||
class ShoppingCartSetupError(frappe.ValidationError): pass
|
||||
|
||||
@@ -19,57 +18,8 @@ class ShoppingCartSettings(Document):
|
||||
|
||||
def validate(self):
|
||||
if self.enabled:
|
||||
self.validate_price_lists()
|
||||
self.validate_tax_masters()
|
||||
self.validate_exchange_rates_exist()
|
||||
self.validate_tax_rule()
|
||||
|
||||
def validate_overlapping_territories(self, parentfield, fieldname):
|
||||
# for displaying message
|
||||
doctype = self.meta.get_field(parentfield).options
|
||||
|
||||
# specify atleast one entry in the table
|
||||
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():
|
||||
if len(names) > 1:
|
||||
frappe.throw(_("{0} {1} has a common territory {2}").format(_(doctype), comma_and(names), territory), ShoppingCartSetupError)
|
||||
|
||||
return territory_name_map
|
||||
|
||||
def validate_price_lists(self):
|
||||
self.validate_overlapping_territories("price_lists", "selling_price_list")
|
||||
|
||||
# validate that a Shopping Cart Price List exists for the default territory as a catch all!
|
||||
price_list_for_default_territory = self.get_name_from_territory(self.default_territory, "price_lists",
|
||||
"selling_price_list")
|
||||
|
||||
if not price_list_for_default_territory:
|
||||
msgprint(_("Please specify a Price List which is valid for Territory") +
|
||||
": " + self.default_territory, raise_exception=ShoppingCartSetupError)
|
||||
|
||||
def get_territory_name_map(self, parentfield, fieldname):
|
||||
territory_name_map = {}
|
||||
|
||||
# entries in table
|
||||
names = [doc.get(fieldname) for doc in self.get(parentfield)]
|
||||
if names:
|
||||
# for condition in territory check
|
||||
parenttype = frappe.get_meta(self.meta.get_options(parentfield)).get_options(fieldname)
|
||||
# to validate territory overlap
|
||||
# make a map of territory: [list of names]
|
||||
# if list against each territory has more than one element, raise exception
|
||||
territory_name = frappe.db.sql("""select `territory`, `parent`
|
||||
from `tabApplicable Territory`
|
||||
where `parenttype`=%s and `parent` in (%s)""" %
|
||||
("%s", ", ".join(["%s"]*len(names))), tuple([parenttype] + names))
|
||||
|
||||
for territory, name in territory_name:
|
||||
territory_name_map.setdefault(territory, []).append(name)
|
||||
|
||||
if len(territory_name_map[territory]) > 1:
|
||||
territory_name_map[territory].sort(key=lambda val: names.index(val))
|
||||
return territory_name_map
|
||||
|
||||
def validate_exchange_rates_exist(self):
|
||||
"""check if exchange rates exist for all Price List currencies (to company's currency)"""
|
||||
@@ -102,20 +52,6 @@ class ShoppingCartSettings(Document):
|
||||
msgprint(_("Missing Currency Exchange Rates for {0}").format(comma_and(missing)),
|
||||
raise_exception=ShoppingCartSetupError)
|
||||
|
||||
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:
|
||||
territory_ancestry = self.get_territory_ancestry(territory)
|
||||
for ancestor in territory_ancestry:
|
||||
if territory_name_map.get(ancestor):
|
||||
name = territory_name_map.get(ancestor)
|
||||
break
|
||||
|
||||
return name
|
||||
|
||||
def get_price_list(self, billing_territory):
|
||||
price_list = self.get_name_from_territory(billing_territory, "price_lists", "selling_price_list")
|
||||
if not (price_list and price_list[0]):
|
||||
@@ -123,11 +59,11 @@ class ShoppingCartSettings(Document):
|
||||
"price_lists", "selling_price_list")
|
||||
|
||||
return price_list and price_list[0] or None
|
||||
|
||||
|
||||
def validate_tax_rule(self):
|
||||
if not frappe.db.get_value("Tax Rule", {"use_for_shopping_cart" : 1}, "name"):
|
||||
frappe.throw(frappe._("Set Tax Rule for shopping cart"), ShoppingCartSetupError)
|
||||
|
||||
|
||||
def get_tax_master(self, billing_territory):
|
||||
tax_master = self.get_name_from_territory(billing_territory, "sales_taxes_and_charges_masters",
|
||||
"sales_taxes_and_charges_master")
|
||||
@@ -136,15 +72,6 @@ class ShoppingCartSettings(Document):
|
||||
def get_shipping_rules(self, shipping_territory):
|
||||
return self.get_name_from_territory(shipping_territory, "shipping_rules", "shipping_rule")
|
||||
|
||||
def get_territory_ancestry(self, territory):
|
||||
if not hasattr(self, "_territory_ancestry"):
|
||||
self._territory_ancestry = {}
|
||||
|
||||
if not self._territory_ancestry.get(territory):
|
||||
self._territory_ancestry[territory] = get_ancestors_of("Territory", territory)
|
||||
|
||||
return self._territory_ancestry[territory]
|
||||
|
||||
def validate_cart_settings(doc, method):
|
||||
frappe.get_doc("Shopping Cart Settings", "Shopping Cart Settings").run_method("validate")
|
||||
|
||||
@@ -163,4 +90,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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user