From cc24d28ddc44251a5ddf1b75d0c988578cb82a3a Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Tue, 8 Dec 2015 12:34:01 +0530 Subject: [PATCH] [fix] address creation for new user --- erpnext/hooks.py | 3 +++ erpnext/shopping_cart/cart.py | 12 ++++++++++++ erpnext/templates/includes/address_row.html | 3 +-- erpnext/utilities/doctype/address/address.py | 17 +++++++++++------ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 734aa597e2..22a093dc77 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -111,6 +111,9 @@ doc_events = { "Price List": { "on_update": "erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings.validate_cart_settings" }, + "Address": { + "validate": "erpnext.shopping_cart.cart.set_customer_in_address" + } } scheduler_events = { diff --git a/erpnext/shopping_cart/cart.py b/erpnext/shopping_cart/cart.py index c116bdc993..561a50cd72 100644 --- a/erpnext/shopping_cart/cart.py +++ b/erpnext/shopping_cart/cart.py @@ -280,6 +280,7 @@ def get_customer(user=None): user = frappe.session.user customer = frappe.db.get_value("Contact", {"email_id": user}, "customer") + if customer: return frappe.get_doc("Customer", customer) @@ -320,6 +321,17 @@ def get_address_docs(doctype=None, txt=None, filters=None, limit_start=0, limit_ return address_docs +def set_customer_in_address(doc, method=None): + if doc.flags.linked: + return + + doc.check_if_linked() + + if not doc.flags.linked and (frappe.db.get_value("User", frappe.session.user, "user_type") == "Website User"): + # creates a customer if one does not exist + get_customer() + doc.link_address() + @frappe.whitelist() def apply_shipping_rule(shipping_rule): quotation = _get_cart_quotation() diff --git a/erpnext/templates/includes/address_row.html b/erpnext/templates/includes/address_row.html index 717ca7500e..f6ec819bd5 100644 --- a/erpnext/templates/includes/address_row.html +++ b/erpnext/templates/includes/address_row.html @@ -3,7 +3,6 @@

{{ doc.address_title }}

{{ frappe.get_doc(doc).get_display() }} - - +

diff --git a/erpnext/utilities/doctype/address/address.py b/erpnext/utilities/doctype/address/address.py index a4828914c3..da529fb5b3 100644 --- a/erpnext/utilities/doctype/address/address.py +++ b/erpnext/utilities/doctype/address/address.py @@ -10,6 +10,9 @@ from frappe.utils import cstr from frappe.model.document import Document class Address(Document): + def __setup__(self): + self.flags.linked = False + def autoname(self): if not self.address_title: self.address_title = self.customer \ @@ -42,13 +45,10 @@ class Address(Document): def link_address(self): """Link address based on owner""" - linked = False - for fieldname in self.link_fields: - if self.get(fieldname): - linked = True - break + if not self.flags.linked: + self.check_if_linked() - if not linked: + if not self.flags.linked: contact = frappe.db.get_value("Contact", {"email_id": self.owner}, ("name", "customer", "supplier"), as_dict = True) if contact: @@ -57,6 +57,11 @@ class Address(Document): self.lead = frappe.db.get_value("Lead", {"email_id": self.owner}) + def check_if_linked(self): + for fieldname in self.link_fields: + if self.get(fieldname): + self.flags.linked = True + break def validate_shipping_address(self): """Validate that there can only be one shipping address for particular customer, supplier"""