From b5be7bab9b95d4b8e0bec6429f99b8726e5b3603 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 30 Jan 2014 18:47:12 +0530 Subject: [PATCH] Stock Return: fetch customer and supplier details --- .../stock/doctype/stock_entry/stock_entry.js | 74 ++++++++------ .../stock/doctype/stock_entry/stock_entry.py | 64 +++--------- erpnext/utilities/transaction_base.py | 97 +------------------ 3 files changed, 59 insertions(+), 176 deletions(-) diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js index 6bd9564845..5dbcef8a0e 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.js +++ b/erpnext/stock/doctype/stock_entry/stock_entry.js @@ -93,9 +93,8 @@ erpnext.stock.StockEntry = erpnext.stock.StockController.extend({ if(cint(wn.defaults.get_default("auto_accounting_for_stock"))) { var account_for = "stock_adjustment_account"; - if (this.frm.doc.purpose == "Sales Return") - account_for = "stock_in_hand_account"; - else if (this.frm.doc.purpose == "Purchase Return") + + if (this.frm.doc.purpose == "Purchase Return") account_for = "stock_received_but_not_billed"; return this.frm.call({ @@ -236,7 +235,50 @@ erpnext.stock.StockEntry = erpnext.stock.StockController.extend({ mtn_details_on_form_rendered: function(doc, grid_row) { erpnext.setup_serial_no(grid_row) + }, + + customer: function() { + return this.frm.call({ + method: "erpnext.selling.doctype.customer.customer.get_customer_details", + args: { customer: this.frm.doc.customer } + }); + }, + + supplier: function() { + return this.frm.call({ + method: "erpnext.buying.doctype.supplier.supplier.get_supplier_details", + args: { supplier: this.frm.doc.supplier } + }); + }, + + delivery_note_no: function() { + this.get_party_details({ + ref_dt: "Delivery Note", + ref_dn: this.frm.doc.delivery_note_no + }) + }, + + sales_invoice_no: function() { + this.get_party_details({ + ref_dt: "Sales Invoice", + ref_dn: this.frm.doc.sales_invoice_no + }) + }, + + purchase_receipt_no: function() { + this.get_party_details({ + ref_dt: "Purchase Receipt", + ref_dn: this.frm.doc.purchase_receipt_no + }) + }, + + get_party_details: function(args) { + return this.frm.call({ + method: "erpnext.stock.doctype.stock_entry.stock_entry.get_party_details", + args: args, + }) } + }); cur_frm.script_manager.make(erpnext.stock.StockEntry); @@ -265,32 +307,6 @@ cur_frm.cscript.toggle_related_fields = function(doc) { } } -cur_frm.cscript.delivery_note_no = function(doc, cdt, cdn) { - if(doc.delivery_note_no) - return get_server_fields('get_cust_values', '', '', doc, cdt, cdn, 1); -} - -cur_frm.cscript.sales_invoice_no = function(doc, cdt, cdn) { - if(doc.sales_invoice_no) - return get_server_fields('get_cust_values', '', '', doc, cdt, cdn, 1); -} - -cur_frm.cscript.customer = function(doc, cdt, cdn) { - if(doc.customer) - return get_server_fields('get_cust_addr', '', '', doc, cdt, cdn, 1); -} - -cur_frm.cscript.purchase_receipt_no = function(doc, cdt, cdn) { - if(doc.purchase_receipt_no) - return get_server_fields('get_supp_values', '', '', doc, cdt, cdn, 1); -} - -cur_frm.cscript.supplier = function(doc, cdt, cdn) { - if(doc.supplier) - return get_server_fields('get_supp_addr', '', '', doc, cdt, cdn, 1); - -} - cur_frm.fields_dict['production_order'].get_query = function(doc) { return{ filters:[ diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 5c760f4aa0..161a3ad391 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -590,56 +590,6 @@ class DocType(StockController): # increment idx by 1 idx += 1 return idx - - def get_cust_values(self): - """fetches customer details""" - if self.doc.delivery_note_no: - doctype = "Delivery Note" - name = self.doc.delivery_note_no - else: - doctype = "Sales Invoice" - name = self.doc.sales_invoice_no - - result = webnotes.conn.sql("""select customer, customer_name, - address_display as customer_address - from `tab%s` where name=%s""" % (doctype, "%s"), (name,), as_dict=1) - - return result and result[0] or {} - - def get_cust_addr(self): - from erpnext.utilities.transaction_base import get_default_address, get_address_display - res = webnotes.conn.sql("select customer_name from `tabCustomer` where name = '%s'"%self.doc.customer) - address_display = None - customer_address = get_default_address("customer", self.doc.customer) - if customer_address: - address_display = get_address_display(customer_address) - ret = { - 'customer_name' : res and res[0][0] or '', - 'customer_address' : address_display} - - return ret - - def get_supp_values(self): - result = webnotes.conn.sql("""select supplier, supplier_name, - address_display as supplier_address - from `tabPurchase Receipt` where name=%s""", (self.doc.purchase_receipt_no,), - as_dict=1) - - return result and result[0] or {} - - def get_supp_addr(self): - from erpnext.utilities.transaction_base import get_default_address, get_address_display - res = webnotes.conn.sql("""select supplier_name from `tabSupplier` - where name=%s""", self.doc.supplier) - address_display = None - supplier_address = get_default_address("customer", self.doc.customer) - if supplier_address: - address_display = get_address_display(supplier_address) - - ret = { - 'supplier_name' : res and res[0][0] or '', - 'supplier_address' : address_display } - return ret def validate_with_material_request(self): for item in self.doclist.get({"parentfield": "mtn_details"}): @@ -653,6 +603,17 @@ class DocType(StockController): + _("Material Request") + (" - %s" % item.material_request), raise_exception=webnotes.MappingMismatchError) +@webnotes.whitelist() +def get_party_details(ref_dt, ref_dn): + if ref_dt in ["Delivery Note", "Sales Invoice"]: + res = webnotes.conn.get_value(ref_dt, ref_dn, + ["customer", "customer_name", "address_display as customer_address"], as_dict=1) + else: + res = webnotes.conn.get_value(ref_dt, ref_dn, + ["supplier", "supplier_name", "address_display as supplier_address"], as_dict=1) + print ref_dt, ref_dn, res + return res or {} + @webnotes.whitelist() def get_production_order_details(production_order): result = webnotes.conn.sql("""select bom_no, @@ -965,5 +926,4 @@ def make_return_jv_from_purchase_receipt(se, ref): result = [parent] + [{"account": account} for account in children] - return result - + return result \ No newline at end of file diff --git a/erpnext/utilities/transaction_base.py b/erpnext/utilities/transaction_base.py index 02416291bd..098a4bd8e9 100644 --- a/erpnext/utilities/transaction_base.py +++ b/erpnext/utilities/transaction_base.py @@ -2,28 +2,14 @@ # License: GNU General Public License v3. See license.txt from __future__ import unicode_literals -import webnotes, json +import webnotes from webnotes import msgprint, _ from webnotes.utils import cstr, flt, now_datetime, cint -from webnotes.model.doc import addchild from erpnext.controllers.status_updater import StatusUpdater -class TransactionBase(StatusUpdater): - def set_address_fields(self): - party_type, party_name = self.get_party_type_and_name() - - if party_type in ("Customer", "Lead"): - if self.doc.customer_address: - self.doc.address_display = get_address_display(self.doc.customer_address) - - if self.doc.shipping_address_name: - self.doc.shipping_address = get_address_display(self.doc.shipping_address_name) - - elif self.doc.supplier_address: - self.doc.address_display = get_address_display(self.doc.supplier_address) - +class TransactionBase(StatusUpdater): def set_contact_fields(self): party_type, party_name = self.get_party_type_and_name() @@ -59,74 +45,6 @@ class TransactionBase(StatusUpdater): def set_lead_defaults(self): self.doc.fields.update(self.get_lead_defaults()) - - # TODO deprecate this - used only in sales_order.js - def get_shipping_address(self, name): - shipping_address = get_default_address("customer", name, is_shipping_address=True) - return { - 'shipping_address_name' : shipping_address, - 'shipping_address' : get_address_display(shipping_address) if shipping_address else None - } - - # Get Supplier Default Primary Address - first load - # ----------------------- - def get_default_supplier_address(self, args): - if isinstance(args, basestring): - args = json.loads(args) - - address_name = get_default_address("supplier", args["supplier"]) - ret = { - 'supplier_address' : address_name, - 'address_display' : get_address_display(address_name), - } - ret.update(map_party_contact_details(None, "supplier", args["supplier"])) - ret.update(self.get_supplier_details(args['supplier'])) - return ret - - # Get Supplier Address - # ----------------------- - def get_supplier_address(self, args): - args = json.loads(args) - ret = { - 'supplier_address' : args['address'], - 'address_display' : get_address_display(args["address"]), - } - ret.update(map_party_contact_details(contact_name=args['contact'])) - return ret - - def set_supplier_address(self, args): - self.doc.fields.update(self.get_supplier_address(args)) - - # Get Supplier Details - # ----------------------- - def get_supplier_details(self, name): - supplier_details = webnotes.conn.sql("""\ - select supplier_name, default_currency - from `tabSupplier` - where name = %s and docstatus < 2""", name, as_dict=1) - if supplier_details: - return { - 'supplier_name': (supplier_details[0]['supplier_name'] - or self.doc.fields.get('supplier_name')), - 'currency': (supplier_details[0]['default_currency'] - or self.doc.fields.get('currency')), - } - else: - return {} - - # Get Sales Person Details of Customer - # ------------------------------------ - def get_sales_person(self, name): - self.doclist = self.doc.clear_table(self.doclist,'sales_team') - idx = 0 - for d in webnotes.conn.sql("select sales_person, allocated_percentage, allocated_amount, incentives from `tabSales Team` where parent = '%s'" % name): - ch = addchild(self.doc, 'sales_team', 'Sales Team', self.doclist) - ch.sales_person = d and cstr(d[0]) or '' - ch.allocated_percentage = d and flt(d[1]) or 0 - ch.allocated_amount = d and flt(d[2]) or 0 - ch.incentives = d and flt(d[3]) or 0 - ch.idx = idx - idx += 1 def load_notification_message(self): dt = self.doc.doctype.lower().replace(" ", "_") @@ -208,17 +126,6 @@ class TransactionBase(StatusUpdater): for field, condition in fields: if prevdoc_values[field] is not None: self.validate_value(field, condition, prevdoc_values[field], doc) - -def get_default_address(party_field, party_name, is_shipping_address=False): - if is_shipping_address: - order_by = "is_shipping_address desc, is_primary_address desc, name asc" - else: - order_by = "is_primary_address desc, name asc" - - address = webnotes.conn.sql("""select name from `tabAddress` where `%s`=%s order by %s - limit 1""" % (party_field, "%s", order_by), party_name) - - return address[0][0] if address else None def get_default_contact(party_field, party_name): contact = webnotes.conn.sql("""select name from `tabContact` where `%s`=%s