From 8d39fd9790472917a96a3dd3fa1f8695ecfc5626 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 16 Jan 2017 13:06:07 +0530 Subject: [PATCH] [fix] address queries + tests --- .../doctype/purchase_common/purchase_common.js | 2 +- erpnext/buying/doctype/supplier/supplier.js | 2 +- erpnext/crm/doctype/opportunity/opportunity.js | 4 ++-- .../maintenance_schedule.js | 2 +- .../maintenance_visit/maintenance_visit.js | 2 +- erpnext/public/js/queries.js | 18 +++++++++--------- erpnext/selling/doctype/customer/customer.js | 2 +- erpnext/selling/doctype/quotation/quotation.js | 8 ++------ erpnext/selling/sales_common.js | 2 +- .../doctype/sales_partner/sales_partner.js | 2 +- erpnext/shopping_cart/cart.py | 16 +++++++++------- .../doctype/warranty_claim/warranty_claim.js | 2 +- erpnext/utilities/address_and_contact.py | 2 +- 13 files changed, 31 insertions(+), 33 deletions(-) diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.js b/erpnext/buying/doctype/purchase_common/purchase_common.js index 921c3b57ea..720a1dc208 100644 --- a/erpnext/buying/doctype/purchase_common/purchase_common.js +++ b/erpnext/buying/doctype/purchase_common/purchase_common.js @@ -75,7 +75,7 @@ erpnext.buying.BuyingController = erpnext.TransactionController.extend({ }, refresh: function(doc) { - frappe.contact_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'}; + frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'supplier', doctype: 'Supplier'}; this.frm.toggle_display("supplier_name", (this.frm.doc.supplier_name && this.frm.doc.supplier_name!==this.frm.doc.supplier)); diff --git a/erpnext/buying/doctype/supplier/supplier.js b/erpnext/buying/doctype/supplier/supplier.js index 9808718446..ce6f4511dd 100644 --- a/erpnext/buying/doctype/supplier/supplier.js +++ b/erpnext/buying/doctype/supplier/supplier.js @@ -16,7 +16,7 @@ frappe.ui.form.on("Supplier", { }); }, refresh: function(frm) { - frappe.contact_link = {doc: frm.doc, fieldname: 'name', doctype: 'Supplier'} + frappe.dynamic_link = {doc: frm.doc, fieldname: 'name', doctype: 'Supplier'} if(frappe.defaults.get_default("supp_master_name")!="Naming Series") { frm.toggle_display("naming_series", false); diff --git a/erpnext/crm/doctype/opportunity/opportunity.js b/erpnext/crm/doctype/opportunity/opportunity.js index d7d412a1e1..32171b0601 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.js +++ b/erpnext/crm/doctype/opportunity/opportunity.js @@ -52,9 +52,9 @@ frappe.ui.form.on("Opportunity", { set_contact_link: function(frm) { if(frm.doc.customer) { - frappe.contact_link = {doc: frm.doc, fieldname: 'customer', doctype: 'Customer'} + frappe.dynamic_link = {doc: frm.doc, fieldname: 'customer', doctype: 'Customer'} } else if(frm.doc.lead) { - frappe.contact_link = {doc: frm.doc, fieldname: 'lead', doctype: 'Lead'} + frappe.dynamic_link = {doc: frm.doc, fieldname: 'lead', doctype: 'Lead'} } }, diff --git a/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js b/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js index b5d1ad320f..9639e7faa2 100644 --- a/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js +++ b/erpnext/maintenance/doctype/maintenance_schedule/maintenance_schedule.js @@ -23,7 +23,7 @@ frappe.ui.form.on('Maintenance Schedule', { // TODO commonify this code erpnext.maintenance.MaintenanceSchedule = frappe.ui.form.Controller.extend({ refresh: function() { - frappe.contact_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} + frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} var me = this; diff --git a/erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js b/erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js index 1a3c35cc06..62cdf865e0 100644 --- a/erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js +++ b/erpnext/maintenance/doctype/maintenance_visit/maintenance_visit.js @@ -25,7 +25,7 @@ frappe.ui.form.on('Maintenance Visit', { // TODO commonify this code erpnext.maintenance.MaintenanceVisit = frappe.ui.form.Controller.extend({ refresh: function() { - frappe.contact_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} + frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} if (this.frm.doc.docstatus===0) { cur_frm.add_custom_button(__('Maintenance Schedule'), diff --git a/erpnext/public/js/queries.js b/erpnext/public/js/queries.js index 8236ce7ae6..1141f99623 100644 --- a/erpnext/public/js/queries.js +++ b/erpnext/public/js/queries.js @@ -43,28 +43,28 @@ $.extend(erpnext.queries, { }, contact_query: function(doc) { - if(frappe.contact_link) { - if(!doc[frappe.contact_link.fieldname]) { + if(frappe.dynamic_link) { + if(!doc[frappe.dynamic_link.fieldname]) { frappe.throw(__("Please set {0}", __(frappe.meta.get_label(doc.doctype, - frappe.contact_link.fieldname, doc.name)))); + frappe.dynamic_link.fieldname, doc.name)))); } return { query: 'frappe.email.doctype.contact.contact.contact_query', - filters: { link_doctype: frappe.contact_link.doctype, link_name: doc[frappe.contact_link.fieldname] } }; + filters: { link_doctype: frappe.dynamic_link.doctype, link_name: doc[frappe.dynamic_link.fieldname] } }; } }, address_query: function(doc) { - if(frappe.contact_link) { - if(!doc[frappe.contact_link.fieldname]) { + if(frappe.dynamic_link) { + if(!doc[frappe.dynamic_link.fieldname]) { frappe.throw(__("Please set {0}", __(frappe.meta.get_label(doc.doctype, - frappe.contact_link.fieldname, doc.name)))); + frappe.dynamic_link.fieldname, doc.name)))); } return { - query: 'frappe.email.doctype.address.address_query', - filters: { link_doctype: frappe.contact_link.doctype, link_name: doc[frappe.contact_link.fieldname] } }; + query: 'frappe.geo.doctype.address.address.address_query', + filters: { link_doctype: frappe.dynamic_link.doctype, link_name: doc[frappe.dynamic_link.fieldname] } }; } }, diff --git a/erpnext/selling/doctype/customer/customer.js b/erpnext/selling/doctype/customer/customer.js index 975604deb4..747b31e13d 100644 --- a/erpnext/selling/doctype/customer/customer.js +++ b/erpnext/selling/doctype/customer/customer.js @@ -32,7 +32,7 @@ frappe.ui.form.on("Customer", { erpnext.toggle_naming_series(); } - frappe.contact_link = {doc: frm.doc, fieldname: 'name', doctype: 'Customer'} + frappe.dynamic_link = {doc: frm.doc, fieldname: 'name', doctype: 'Customer'} frm.toggle_display(['address_html','contact_html'], !frm.doc.__islocal); diff --git a/erpnext/selling/doctype/quotation/quotation.js b/erpnext/selling/doctype/quotation/quotation.js index ab1a24a80a..e06f96307f 100644 --- a/erpnext/selling/doctype/quotation/quotation.js +++ b/erpnext/selling/doctype/quotation/quotation.js @@ -67,12 +67,8 @@ erpnext.selling.QuotationController = erpnext.selling.SellingController.extend({ this.frm.toggle_reqd("customer", this.frm.doc.quotation_to == "Customer"); // to overwrite the customer_filter trigger from queries.js - $.each(["customer_address", "shipping_address_name"], - function(i, opts) { - me.frm.set_query(opts, me.frm.doc.quotation_to==="Lead" - ? erpnext.queries["lead_filter"] : erpnext.queries["customer_filter"]); - } - ); + this.frm.set_query('customer_address', erpnext.queries.address_query); + this.frm.set_query('shipping_address_name', erpnext.queries.address_query); }, tc_name: function() { diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js index 96ce79acb0..9d9511e7cb 100644 --- a/erpnext/selling/sales_common.js +++ b/erpnext/selling/sales_common.js @@ -106,7 +106,7 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({ refresh: function() { this._super(); - frappe.contact_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} + frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} this.frm.toggle_display("customer_name", (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer)); diff --git a/erpnext/setup/doctype/sales_partner/sales_partner.js b/erpnext/setup/doctype/sales_partner/sales_partner.js index 653163075b..df8d23f7af 100644 --- a/erpnext/setup/doctype/sales_partner/sales_partner.js +++ b/erpnext/setup/doctype/sales_partner/sales_partner.js @@ -3,7 +3,7 @@ frappe.ui.form.on('Sales Partner', { refresh: function(frm) { - frappe.contact_link = {doc: frm.doc, fieldname: 'name', doctype: 'Sales Person'} + frappe.dynamic_link = {doc: frm.doc, fieldname: 'name', doctype: 'Sales Person'} if(doc.__islocal){ hide_field(['address_html', 'contact_html']); diff --git a/erpnext/shopping_cart/cart.py b/erpnext/shopping_cart/cart.py index e52ef8105d..5ca0315ccc 100644 --- a/erpnext/shopping_cart/cart.py +++ b/erpnext/shopping_cart/cart.py @@ -375,15 +375,17 @@ def get_address_docs(doctype=None, txt=None, filters=None, limit_start=0, limit_ if not party: return [] - address_docs = frappe.db.sql("""select * from `tabAddress` - where `{0}`=%s order by name limit {1}, {2}""".format(party.doctype.lower(), - limit_start, limit_page_length), party.name, - as_dict=True, update={"doctype": "Address"}) + address_names = frappe.db.get_all('Dyanamic Link', fields=('parent'), + filters=dict(parenttype='Address', link_doctype=party.doctype, link_name=party.name)) - for address in address_docs: - address.display = get_address_display(address) + out = [] - return address_docs + for address_name in address_names: + address = frappe.get_doc('Address', address_name) + address.display = get_address_display(address.as_dict()) + out.append(address) + + return out @frappe.whitelist() def apply_shipping_rule(shipping_rule): diff --git a/erpnext/support/doctype/warranty_claim/warranty_claim.js b/erpnext/support/doctype/warranty_claim/warranty_claim.js index abd207b9e4..2369a8ac01 100644 --- a/erpnext/support/doctype/warranty_claim/warranty_claim.js +++ b/erpnext/support/doctype/warranty_claim/warranty_claim.js @@ -21,7 +21,7 @@ frappe.ui.form.on("Warranty Claim", { erpnext.support.WarrantyClaim = frappe.ui.form.Controller.extend({ refresh: function() { - frappe.contact_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} + frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} if(!cur_frm.doc.__islocal && (cur_frm.doc.status=='Open' || cur_frm.doc.status == 'Work In Progress')) { diff --git a/erpnext/utilities/address_and_contact.py b/erpnext/utilities/address_and_contact.py index eaa71f9b1f..f3435aeed4 100644 --- a/erpnext/utilities/address_and_contact.py +++ b/erpnext/utilities/address_and_contact.py @@ -122,7 +122,7 @@ def get_permitted_and_not_permitted_links(doctype): def delete_contact_and_address(doctype, name): for parenttype in ('Contact', 'Address'): items = frappe.db.sql("""select parent from `tabDynamic Link` - where parenttype=%s and link_type=%s and link_name=%s""", + where parenttype=%s and link_doctype=%s and link_name=%s""", (parenttype, doctype, name)) for name in items: