commonified get_party_details

This commit is contained in:
Rushabh Mehta 2014-02-03 16:14:56 +05:30
parent 347889b233
commit cc008cc109
11 changed files with 126 additions and 161 deletions

View File

@ -79,13 +79,13 @@ erpnext.accounts.PurchaseInvoice = erpnext.buying.BuyingController.extend({
supplier: function() { supplier: function() {
if(this.frm.updating_party_details) if(this.frm.updating_party_details)
return; return;
erpnext.selling.get_party_details(this.frm, erpnext.utils.get_party_details(this.frm,
"erpnext.accounts.party.get_party_details", { "erpnext.accounts.party.get_party_details", {
posting_date: this.frm.doc.posting_date, posting_date: this.frm.doc.posting_date,
company: this.frm.doc.company,
party: this.frm.doc.supplier, party: this.frm.doc.supplier,
party_type: "Supplier", party_type: "Supplier",
account: this.frm.doc.debit_to account: this.frm.doc.debit_to,
price_list: this.frm.doc.buying_price_list,
}) })
}, },

View File

@ -158,13 +158,13 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte
customer: function() { customer: function() {
if(this.frm.updating_party_details) if(this.frm.updating_party_details)
return; return;
erpnext.selling.get_party_details(this.frm, erpnext.utils.get_party_details(this.frm,
"erpnext.accounts.party.get_party_details", { "erpnext.accounts.party.get_party_details", {
posting_date: this.frm.doc.posting_date, posting_date: this.frm.doc.posting_date,
company: this.frm.doc.company,
party: this.frm.doc.customer, party: this.frm.doc.customer,
party_type: "Customer", party_type: "Customer",
account: this.frm.doc.debit_to account: this.frm.doc.debit_to,
price_list: this.frm.doc.selling_price_list,
}) })
}, },

View File

@ -751,7 +751,7 @@ def notify_errors(inv, customer, owner):
message = webnotes.get_template("template/emails/recurring_invoice_failed.html").render({ message = webnotes.get_template("template/emails/recurring_invoice_failed.html").render({
"name": inv, "name": inv,
"customer": customer "customer": customer
}) }))
assign_task_to_owner(inv, "Recurring Invoice Failed", recipients) assign_task_to_owner(inv, "Recurring Invoice Failed", recipients)

View File

@ -5,16 +5,97 @@ from __future__ import unicode_literals
import webnotes import webnotes
from webnotes import _ from webnotes import _
from webnotes.defaults import get_restrictions
from erpnext.utilities.doctype.address.address import get_address_display
from erpnext.utilities.doctype.contact.contact import get_contact_details
@webnotes.whitelist() @webnotes.whitelist()
def get_party_details(party=None, account=None, party_type="Customer", company=None, posting_date=None): def get_party_details(party=None, account=None, party_type="Customer", company=None,
if not webnotes.has_permission(party_type, "read", party): posting_date=None, price_list=None, currency=None):
webnotes.throw("No Permission") out = webnotes._dict(set_account_and_due_date(party, account, party_type, company, posting_date))
party = out[party_type.lower()]
if not webnotes.has_permission(party_type, "read", party):
webnotes.throw("Not Permitted", webnotes.PermissionError)
party_bean = webnotes.bean(party_type, party)
party = party_bean.doc
set_address_and_contact(out, party, party_type)
set_other_values(out, party, party_type)
set_price_list(out, party, price_list)
if not out.get("currency"):
out["currency"] = currency
# sales team
if party_type=="Customer": if party_type=="Customer":
get_party_details = webnotes.get_attr("erpnext.selling.doctype.customer.customer.get_customer_details") out["sales_team"] = [{
"sales_person": d.sales_person,
"sales_designation": d.sales_designation
} for d in party_bean.doclist.get({"doctype":"Sales Team"})]
return out
def set_address_and_contact(out, party, party_type):
out.update({
party_type.lower() + "_address": webnotes.conn.get_value("Address",
{party_type.lower(): party.name, "is_primary_address":1}, "name"),
"contact_person": webnotes.conn.get_value("Contact",
{party_type.lower(): party.name, "is_primary_contact":1}, "name")
})
# address display
out.address_display = get_address_display(out[party_type.lower() + "_address"])
# primary contact details
out.update(get_contact_details(out.contact_person))
def set_other_values(out, party, party_type):
# copy
if party_type=="Customer":
to_copy = ["customer_name", "customer_group", "territory"]
else: else:
get_party_details = webnotes.get_attr("erpnext.buying.doctype.supplier.supplier.get_supplier_details") to_copy = ["supplier_name", "supplier_type"]
for f in to_copy:
out[f] = party.get(f)
# fields prepended with default in Customer doctype
for f in ['currency', 'taxes_and_charges'] \
+ (['sales_partner', 'commission_rate'] if party_type=="Customer" else []):
if party.get("default_" + f):
out[f] = party.get("default_" + f)
def set_price_list(out, party, given_price_list):
# price list
price_list = get_restrictions().get("Price List")
if isinstance(price_list, list):
price_list = None
if not price_list:
price_list = party.default_price_list
if not price_list and party.party_type=="Customer":
price_list = webnotes.conn.get_value("Customer Group",
party.customer_group, "default_price_list")
if not price_list:
price_list = given_price_list
if price_list:
out.price_list_currency = webnotes.conn.get_value("Price List", price_list, "currency")
out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list
def set_account_and_due_date(party, account, party_type, company, posting_date):
if not posting_date:
# not an invoice
return {
party_type.lower(): party
}
if party: if party:
account = get_party_account(company, party, party_type) account = get_party_account(company, party, party_type)
@ -28,7 +109,6 @@ def get_party_details(party=None, account=None, party_type="Customer", company=N
account_fieldname : account, account_fieldname : account,
"due_date": get_due_date(posting_date, party, party_type, account, company) "due_date": get_due_date(posting_date, party, party_type, account, company)
} }
out.update(get_party_details(party))
return out return out
def get_party_account(company, party, party_type): def get_party_account(company, party, party_type):

View File

@ -128,26 +128,3 @@ def get_dashboard_info(supplier):
return out return out
@webnotes.whitelist()
def get_supplier_details(supplier, price_list=None, currency=None):
if not webnotes.has_permission("Supplier", "read", supplier):
webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError)
supplier = webnotes.doc("Supplier", supplier)
out = webnotes._dict({
"supplier_address": webnotes.conn.get_value("Address",
{"supplier": supplier.name, "is_primary_address":1}, "name"),
"contact_person": webnotes.conn.get_value("Contact",
{"supplier":supplier.name, "is_primary_contact":1}, "name")
})
for f in ['currency', 'taxes_and_charges']:
if supplier.fields.get("default_" + f):
out[f] = supplier.fields.get("default_" + f)
out.supplier_name = supplier.supplier_name
out.currency = supplier.default_currency or currency
out.buying_price_list = supplier.default_price_list or price_list
return out

View File

@ -7,7 +7,7 @@ from webnotes import _, msgprint
from webnotes.utils import flt, _round from webnotes.utils import flt, _round
from erpnext.buying.utils import get_item_details from erpnext.buying.utils import get_item_details
from erpnext.setup.utils import get_company_currency from erpnext.setup.utils import get_company_currency
from erpnext.buying.doctype.supplier.supplier import get_supplier_details from erpnext.accounts.party import get_party_details
from erpnext.controllers.stock_controller import StockController from erpnext.controllers.stock_controller import StockController
@ -33,7 +33,7 @@ class BuyingController(StockController):
# set contact and address details for supplier, if they are not mentioned # set contact and address details for supplier, if they are not mentioned
if self.doc.supplier: if self.doc.supplier:
self.doc.update_if_missing(get_supplier_details(self.doc.supplier)) self.doc.update_if_missing(get_party_details(self.doc.supplier, party_type="Supplier"))
self.set_missing_item_details(get_item_details) self.set_missing_item_details(get_item_details)
if self.doc.fields.get("__islocal"): if self.doc.fields.get("__islocal"):

View File

@ -33,9 +33,9 @@ class SellingController(StockController):
self.set_taxes("other_charges", "taxes_and_charges") self.set_taxes("other_charges", "taxes_and_charges")
def set_missing_lead_customer_details(self): def set_missing_lead_customer_details(self):
from erpnext.selling.doctype.customer.customer import get_customer_details from erpnext.accounts.party import get_party_details
if self.doc.customer: if self.doc.customer:
self.doc.update_if_missing(get_customer_details(self.doc.customer)) self.doc.update_if_missing(get_party_details(self.doc.customer))
elif self.doc.lead: elif self.doc.lead:
self.doc.update_if_missing(self.get_lead_defaults()) self.doc.update_if_missing(self.get_lead_defaults())

View File

@ -4,28 +4,25 @@
wn.provide("erpnext.utils"); wn.provide("erpnext.utils");
erpnext.utils.get_party_details = function(frm, method, args) { erpnext.utils.get_party_details = function(frm, method, args) {
if(!method) { if(!method) {
if(frm.doc.customer) { method = "erpnext.accounts.party.get_party_details";
method = "erpnext.selling.doctype.customer.customer.get_customer_details";
var price_list_field = "selling_price_list";
} else {
method = "erpnext.buying.doctype.supplier.supplier.get_supplier_details";
var price_list_field = "buying_price_list";
}
} }
if(!args) { if(!args) {
if(frm.doc.customer) { if(frm.doc.customer) {
args = { args = {
customer: frm.doc.customer, party: frm.doc.customer,
party_type: "Customer",
price_list: frm.doc.selling_price_list price_list: frm.doc.selling_price_list
}; };
} else { } else {
args = { args = {
supplier: frm.doc.supplier, party: frm.doc.supplier,
party_type: "Supplier",
price_list: frm.doc.buying_price_list price_list: frm.doc.buying_price_list
}; };
} }
args.currency = frm.doc.currency;
} }
args.currency = frm.doc.currency;
args.company = frm.doc.company;
wn.call({ wn.call({
method: method, method: method,
args: args, args: args,

View File

@ -163,58 +163,3 @@ def get_dashboard_info(customer):
out["total_unpaid"] = billing[0][1] out["total_unpaid"] = billing[0][1]
return out return out
@webnotes.whitelist()
def get_customer_details(customer, price_list=None, currency=None):
if not webnotes.has_permission("Customer", "read", customer):
webnotes.throw("Not Permitted", webnotes.PermissionError)
out = {}
customer_bean = webnotes.bean("Customer", customer)
customer = customer_bean.doc
out = webnotes._dict({
"customer_address": webnotes.conn.get_value("Address",
{"customer": customer.name, "is_primary_address":1}, "name"),
"contact_person": webnotes.conn.get_value("Contact",
{"customer":customer.name, "is_primary_contact":1}, "name")
})
# address display
out.address_display = get_address_display(out.customer_address)
# primary contact details
out.update(get_contact_details(out.contact_person))
# copy
for f in ['customer_name', 'customer_group', 'territory']:
out[f] = customer.get(f)
# fields prepended with default in Customer doctype
for f in ['sales_partner', 'commission_rate', 'currency', 'price_list', 'taxes_and_charges']:
if customer.get("default_" + f):
out[f] = customer.get("default_" + f)
# price list
from webnotes.defaults import get_defaults_for
out.selling_price_list = get_defaults_for(webnotes.session.user).get(price_list)
if isinstance(out.selling_price_list, list):
out.selling_price_list = None
out.selling_price_list = out.selling_price_list or customer.price_list \
or webnotes.conn.get_value("Customer Group",
customer.customer_group, "default_price_list") or price_list
if out.selling_price_list:
out.price_list_currency = webnotes.conn.get_value("Price List", out.selling_price_list, "currency")
if not out.currency:
out.currency = currency
# sales team
out.sales_team = [{
"sales_person": d.sales_person,
"sales_designation": d.sales_designation
} for d in customer_bean.doclist.get({"doctype":"Sales Team"})]
return out

View File

@ -10,8 +10,8 @@ from webnotes.test_runner import make_test_records
class TestCustomer(unittest.TestCase): class TestCustomer(unittest.TestCase):
def test_get_customer_details(self): def test_party_details(self):
from erpnext.selling.doctype.customer.customer import get_customer_details from erpnext.accounts.party import get_party_details
to_check = { to_check = {
'address_display': '_Test Address Line 1\n_Test City\nIndia\nPhone: +91 0000000000', 'address_display': '_Test Address Line 1\n_Test City\nIndia\nPhone: +91 0000000000',
@ -22,7 +22,7 @@ class TestCustomer(unittest.TestCase):
'contact_department': None, 'contact_department': None,
'contact_email': 'test_contact_customer@example.com', 'contact_email': 'test_contact_customer@example.com',
'contact_mobile': None, 'contact_mobile': None,
'_sales_team': [], 'sales_team': [],
'contact_display': '_Test Contact For _Test Customer', 'contact_display': '_Test Contact For _Test Customer',
'contact_person': '_Test Contact For _Test Customer-_Test Customer', 'contact_person': '_Test Contact For _Test Customer-_Test Customer',
'territory': u'_Test Territory', 'territory': u'_Test Territory',
@ -33,7 +33,7 @@ class TestCustomer(unittest.TestCase):
make_test_records("Address") make_test_records("Address")
make_test_records("Contact") make_test_records("Contact")
details = get_customer_details("_Test Customer") details = get_party_details("_Test Customer")
for key, value in to_check.iteritems(): for key, value in to_check.iteritems():
self.assertEquals(value, details.get(key)) self.assertEquals(value, details.get(key))
@ -50,48 +50,6 @@ class TestCustomer(unittest.TestCase):
webnotes.rename_doc("Customer", "_Test Customer 1 Renamed", "_Test Customer 1") webnotes.rename_doc("Customer", "_Test Customer 1 Renamed", "_Test Customer 1")
def test_merge(self):
make_test_records("Sales Invoice")
# clear transactions for new name
webnotes.conn.sql("""delete from `tabSales Invoice` where customer='_Test Customer 1'""")
# check if they exist
self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer"),
(("_Test Customer",),))
self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer 1"),
(("_Test Customer 1",),))
self.assertEqual(webnotes.conn.exists("Account", "_Test Customer - _TC"),
(("_Test Customer - _TC",),))
self.assertEqual(webnotes.conn.exists("Account", "_Test Customer 1 - _TC"),
(("_Test Customer 1 - _TC",),))
# check if transactions exists
self.assertNotEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice`
where customer='_Test Customer'""", )[0][0], 0)
self.assertNotEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice`
where debit_to='_Test Customer - _TC'""", )[0][0], 0)
webnotes.rename_doc("Customer", "_Test Customer", "_Test Customer 1", merge=True)
# check that no transaction exists for old name
self.assertNotEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice`
where customer='_Test Customer 1'""", )[0][0], 0)
self.assertNotEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice`
where debit_to='_Test Customer 1 - _TC'""", )[0][0], 0)
# check that transactions exist for new name
self.assertEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice`
where customer='_Test Customer'""", )[0][0], 0)
self.assertEquals(webnotes.conn.sql("""select count(*) from `tabSales Invoice`
where debit_to='_Test Customer - _TC'""", )[0][0], 0)
# check that old name doesn't exist
self.assertEqual(webnotes.conn.exists("Customer", "_Test Customer"), ())
self.assertEqual(webnotes.conn.exists("Account", "_Test Customer - _TC"), ())
# create back _Test Customer
webnotes.bean(copy=test_records[0]).insert()
test_ignore = ["Price List"] test_ignore = ["Price List"]
@ -111,5 +69,13 @@ test_records = [
"customer_group": "_Test Customer Group", "customer_group": "_Test Customer Group",
"territory": "_Test Territory", "territory": "_Test Territory",
"company": "_Test Company" "company": "_Test Company"
}],
[{
"doctype": "Customer",
"customer_name": "_Test Customer 2",
"customer_type": "Individual",
"customer_group": "_Test Customer Group",
"territory": "_Test Territory",
"company": "_Test Company"
}] }]
] ]

View File

@ -239,15 +239,15 @@ erpnext.stock.StockEntry = erpnext.stock.StockController.extend({
customer: function() { customer: function() {
return this.frm.call({ return this.frm.call({
method: "erpnext.selling.doctype.customer.customer.get_customer_details", method: "erpnext.accounts.party.get_party_details",
args: { customer: this.frm.doc.customer } args: { party: this.frm.doc.customer, party_type:"Customer" }
}); });
}, },
supplier: function() { supplier: function() {
return this.frm.call({ return this.frm.call({
method: "erpnext.buying.doctype.supplier.supplier.get_supplier_details", method: "erpnext.accounts.party.get_party_details",
args: { supplier: this.frm.doc.supplier } args: { party: this.frm.doc.supplier, party_type:"Supplier" }
}); });
}, },