mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-03 04:09:11 +00:00
[webshop] shopping cart settings, update price list, taxes and shipping rule on change of address, apply defaults on creation of fresh quotation
This commit is contained in:
@@ -23,46 +23,89 @@ from webnotes.model.doc import addchild
|
||||
from controllers.status_updater import StatusUpdater
|
||||
|
||||
class TransactionBase(StatusUpdater):
|
||||
def get_default_address_and_contact(self, party_type):
|
||||
def get_default_address_and_contact(self, party_field, party_name=None):
|
||||
"""get a dict of default field values of address and contact for a given party type
|
||||
party_type can be one of: customer, supplier"""
|
||||
ret = {}
|
||||
if not party_name:
|
||||
party_name = self.doc.fields.get(party_field)
|
||||
|
||||
# {customer: self.doc.fields.get("customer")}
|
||||
args = {party_type: self.doc.fields.get(party_type)}
|
||||
return get_default_address_and_contact(party_field, party_name,
|
||||
fetch_shipping_address=True if self.meta.get_field("shipping_address_name") else False)
|
||||
|
||||
def get_customer_defaults(self):
|
||||
out = self.get_default_address_and_contact("customer")
|
||||
|
||||
customer = webnotes.doc("Customer", self.doc.customer)
|
||||
for f in ['customer_name', 'customer_group', 'territory']:
|
||||
out[f] = customer.fields.get(f)
|
||||
|
||||
address_text, address_name = self.get_address_text(**args)
|
||||
ret.update({
|
||||
# customer_address
|
||||
(party_type + "_address"): address_name,
|
||||
"address_display": address_text
|
||||
})
|
||||
ret.update(self.get_contact_text(**args))
|
||||
return ret
|
||||
# fields prepended with default in Customer doctype
|
||||
for f in ['sales_partner', 'commission_rate', 'currency', 'price_list']:
|
||||
out[f] = customer.fields.get("default_" + f)
|
||||
|
||||
return out
|
||||
|
||||
def set_customer_defaults(self):
|
||||
"""
|
||||
For a customer:
|
||||
1. Sets default address and contact
|
||||
2. Sets values like Territory, Customer Group, etc.
|
||||
3. Clears existing Sales Team and fetches the one mentioned in Customer
|
||||
"""
|
||||
customer_defaults = self.get_customer_defaults()
|
||||
|
||||
# hack! TODO - add shipping_address_field in Delivery Note
|
||||
if self.doc.doctype == "Delivery Note":
|
||||
customer_defaults["customer_address"] = customer_defaults["shipping_address_name"]
|
||||
customer_defaults["address_display"] = customer_defaults["shipping_address"]
|
||||
|
||||
customer_defaults["price_list"] = customer_defaults["price_list"] or \
|
||||
webnotes.conn.get_value("Customer Group", self.doc.customer_group, "default_price_list") or \
|
||||
self.doc.price_list
|
||||
|
||||
self.doc.fields.update(customer_defaults)
|
||||
|
||||
if self.meta.get_field("sales_team"):
|
||||
self.set_sales_team_for_customer()
|
||||
|
||||
def set_sales_team_for_customer(self):
|
||||
from webnotes.model import default_fields
|
||||
|
||||
# clear table
|
||||
self.doclist = self.doc.clear_table(self.doclist, "sales_team")
|
||||
|
||||
sales_team = webnotes.conn.sql("""select * from `tabSales Team`
|
||||
where parenttype="Customer" and parent=%s""", self.doc.customer, as_dict=True)
|
||||
for i, sales_person in enumerate(sales_team):
|
||||
# remove default fields
|
||||
for fieldname in default_fields:
|
||||
if fieldname in sales_person:
|
||||
del sales_person[fieldname]
|
||||
|
||||
sales_person.update({
|
||||
"doctype": "Sales Team",
|
||||
"parentfield": "sales_team",
|
||||
"idx": i+1
|
||||
})
|
||||
|
||||
# add child
|
||||
self.doclist.append(sales_person)
|
||||
|
||||
# Get Customer Default Primary Address - first load
|
||||
def get_default_customer_address(self, args=''):
|
||||
address_text, address_name = self.get_address_text(customer=self.doc.customer)
|
||||
self.doc.customer_address = address_name or ''
|
||||
self.doc.address_display = address_text or ''
|
||||
self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
|
||||
|
||||
if args != 'onload':
|
||||
self.get_customer_details(self.doc.customer)
|
||||
self.get_sales_person(self.doc.customer)
|
||||
def get_lead_defaults(self):
|
||||
out = self.get_default_address_and_contact("lead")
|
||||
|
||||
# Get Customer Default Shipping Address - first load
|
||||
# -----------------------
|
||||
def get_default_customer_shipping_address(self, args=''):
|
||||
address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
|
||||
self.doc.customer_address = address_name or ''
|
||||
self.doc.address_display = address_text or ''
|
||||
self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
|
||||
|
||||
if self.doc.doctype != 'Quotation' and args != 'onload':
|
||||
self.get_customer_details(self.doc.customer)
|
||||
self.get_sales_person(self.doc.customer)
|
||||
lead = webnotes.conn.get_value("Lead", self.doc.lead,
|
||||
["territory", "company_name", "lead_name"], as_dict=True) or {}
|
||||
|
||||
out["territory"] = lead.get("territory")
|
||||
out["customer_name"] = lead.get("company_name") or lead.get("lead_name")
|
||||
|
||||
return out
|
||||
|
||||
def set_lead_defaults(self):
|
||||
self.doc.fields.update(self.get_lead_defaults())
|
||||
|
||||
|
||||
# Get Customer Address
|
||||
# -----------------------
|
||||
def get_customer_address(self, args):
|
||||
@@ -91,13 +134,14 @@ class TransactionBase(StatusUpdater):
|
||||
details = webnotes.conn.sql("select name, address_line1, address_line2, city, country, pincode, state, phone, fax from `tabAddress` where %s and docstatus != 2 order by is_shipping_address desc, is_primary_address desc limit 1" % cond, as_dict = 1)
|
||||
else:
|
||||
details = webnotes.conn.sql("select name, address_line1, address_line2, city, country, pincode, state, phone, fax from `tabAddress` where %s and docstatus != 2 order by is_primary_address desc limit 1" % cond, as_dict = 1)
|
||||
|
||||
extract = lambda x: details and details[0] and details[0].get(x,'') or ''
|
||||
address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),('\n','state'),(' ','pincode'),('\n','country'),('\nPhone: ','phone'),('\nFax: ', 'fax')]
|
||||
address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
|
||||
if address_display.startswith('\n'): address_display = address_display[1:]
|
||||
|
||||
address_display = ""
|
||||
|
||||
if details:
|
||||
address_display = get_address_display(details[0])
|
||||
|
||||
address_name = details and details[0]['name'] or ''
|
||||
|
||||
return address_display, address_name
|
||||
|
||||
# Get Contact Text
|
||||
@@ -126,41 +170,13 @@ class TransactionBase(StatusUpdater):
|
||||
"contact_department": details and details[0]["department"] or "",
|
||||
}
|
||||
|
||||
def get_customer_details(self, name):
|
||||
"""
|
||||
Get customer details like name, group, territory
|
||||
and other such defaults
|
||||
"""
|
||||
customer_details = webnotes.conn.sql("""\
|
||||
select
|
||||
customer_name, customer_group, territory,
|
||||
default_sales_partner, default_commission_rate, default_currency,
|
||||
default_price_list
|
||||
from `tabCustomer`
|
||||
where name = %s and docstatus < 2""", name, as_dict=1)
|
||||
if customer_details:
|
||||
for f in ['customer_name', 'customer_group', 'territory']:
|
||||
self.doc.fields[f] = customer_details[0][f] or self.doc.fields.get(f)
|
||||
|
||||
# fields prepended with default in Customer doctype
|
||||
for f in ['sales_partner', 'commission_rate', 'currency']:
|
||||
self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f)
|
||||
|
||||
# optionally fetch default price list from Customer Group
|
||||
self.doc.price_list_name = (customer_details[0]['default_price_list']
|
||||
or webnotes.conn.get_value('Customer Group', self.doc.customer_group,
|
||||
'default_price_list')
|
||||
or self.doc.fields.get('price_list_name'))
|
||||
|
||||
# Get Customer Shipping Address
|
||||
# -----------------------
|
||||
# TODO deprecate this - used only in sales_order.js
|
||||
def get_shipping_address(self, name):
|
||||
details = webnotes.conn.sql("select name, address_line1, address_line2, city, country, pincode, state, phone from `tabAddress` where customer = '%s' and docstatus != 2 order by is_shipping_address desc, is_primary_address desc limit 1" %(name), as_dict = 1)
|
||||
|
||||
extract = lambda x: details and details[0] and details[0].get(x,'') or ''
|
||||
address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
|
||||
address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
|
||||
if address_display.startswith('\n'): address_display = address_display[1:]
|
||||
address_display = ""
|
||||
if details:
|
||||
address_display = get_address_display(details[0])
|
||||
|
||||
ret = {
|
||||
'shipping_address_name' : details and details[0]['name'] or '',
|
||||
@@ -168,36 +184,6 @@ class TransactionBase(StatusUpdater):
|
||||
}
|
||||
return ret
|
||||
|
||||
# Get Lead Details
|
||||
# -----------------------
|
||||
def get_lead_details(self, name):
|
||||
details = webnotes.conn.sql("""select name, lead_name, address_line1, address_line2, city, country, state, pincode
|
||||
from `tabAddress` where lead=%s""", name, as_dict=True)
|
||||
lead = webnotes.conn.get_value("Lead", name,
|
||||
["territory", "phone", "mobile_no", "email_id", "company_name", "lead_name"], as_dict=True) or {}
|
||||
|
||||
address_display = ""
|
||||
if details:
|
||||
details = details[0]
|
||||
for separator, fieldname in (('','address_line1'), ('\n','address_line2'), ('\n','city'),
|
||||
(' ','pincode'), ('\n','state'), ('\n','country'), ('\nPhone: ', 'phone')):
|
||||
if details.get(fieldname):
|
||||
address_display += separator + details.get(fieldname)
|
||||
|
||||
if address_display.startswith('\n'):
|
||||
address_display = address_display[1:]
|
||||
|
||||
ret = {
|
||||
'contact_display' : lead.get('lead_name'),
|
||||
'address_display' : address_display,
|
||||
'territory' : lead.get('territory'),
|
||||
'contact_mobile' : lead.get('mobile_no'),
|
||||
'contact_email' : lead.get('email_id'),
|
||||
'customer_name' : lead.get('company_name') or lead.get('lead_name')
|
||||
}
|
||||
return ret
|
||||
|
||||
|
||||
# Get Supplier Default Primary Address - first load
|
||||
# -----------------------
|
||||
def get_default_supplier_address(self, args):
|
||||
@@ -312,20 +298,112 @@ class TransactionBase(StatusUpdater):
|
||||
})
|
||||
|
||||
webnotes.bean(event_doclist).insert()
|
||||
|
||||
|
||||
def get_default_address_and_contact(party_field, party_name, fetch_shipping_address=False):
|
||||
out = {}
|
||||
|
||||
# get addresses
|
||||
billing_address = get_address_dict(party_field, party_name)
|
||||
if billing_address:
|
||||
out[party_field + "_address"] = billing_address["name"]
|
||||
out["address_display"] = get_address_display(billing_address)
|
||||
else:
|
||||
out[party_field + "_address"] = out["address_display"] = None
|
||||
|
||||
if fetch_shipping_address:
|
||||
shipping_address = get_address_dict(party_field, party_name, is_shipping_address=True)
|
||||
if shipping_address:
|
||||
out["shipping_address_name"] = shipping_address["name"]
|
||||
out["shipping_address"] = get_address_display(shipping_address)
|
||||
else:
|
||||
out["shipping_address_name"] = out["shipping_address"] = None
|
||||
|
||||
# get contact
|
||||
if party_field == "lead":
|
||||
out["customer_address"] = out.get("lead_address")
|
||||
out.update(map_lead_fields(party_name))
|
||||
else:
|
||||
out.update(map_contact_fields(party_field, party_name))
|
||||
|
||||
return out
|
||||
|
||||
def get_address_dict(party_field, party_name, is_shipping_address=None):
|
||||
order_by = "is_shipping_address desc, is_primary_address desc, name asc" if \
|
||||
is_shipping_address else "is_primary_address desc, name asc"
|
||||
|
||||
address = webnotes.conn.sql("""select * from `tabAddress` where `%s`=%s order by %s
|
||||
limit 1""" % (party_field, "%s", order_by), party_name, as_dict=True,
|
||||
update={"doctype": "Address"})
|
||||
|
||||
return address[0] if address else None
|
||||
|
||||
def get_address_display(address_dict):
|
||||
def _prepare_for_display(a_dict, sequence):
|
||||
display = ""
|
||||
for separator, fieldname in sequence:
|
||||
if a_dict.get(fieldname):
|
||||
display += separator + a_dict.get(fieldname)
|
||||
|
||||
return display.strip()
|
||||
|
||||
meta = webnotes.get_doctype("Address")
|
||||
address_sequence = (("", "address_line1"), ("\n", "address_line2"), ("\n", "city"),
|
||||
("\n", "state"), ("\n" + meta.get_label("pincode") + ": ", "pincode"), ("\n", "country"),
|
||||
("\n" + meta.get_label("phone") + ": ", "phone"), ("\n" + meta.get_label("fax") + ": ", "fax"))
|
||||
|
||||
address_display = ""
|
||||
for separator, fieldname in address_sequence:
|
||||
if address_dict.get(fieldname):
|
||||
address_display += separator + address_dict.get(fieldname)
|
||||
|
||||
return address_display
|
||||
return _prepare_for_display(address_dict, address_sequence)
|
||||
|
||||
def map_lead_fields(party_name):
|
||||
out = {}
|
||||
for fieldname in ["contact_display", "contact_email", "contact_mobile", "contact_phone"]:
|
||||
out[fieldname] = None
|
||||
|
||||
lead = webnotes.conn.sql("""select * from `tabLead` where name=%s""", party_name, as_dict=True)
|
||||
if lead:
|
||||
lead = lead[0]
|
||||
out.update({
|
||||
"contact_display": lead.get("lead_name"),
|
||||
"contact_email": lead.get("email_id"),
|
||||
"contact_mobile": lead.get("mobile_no"),
|
||||
"contact_phone": lead.get("phone"),
|
||||
})
|
||||
|
||||
return out
|
||||
|
||||
def map_contact_fields(party_field, party_name):
|
||||
out = {}
|
||||
for fieldname in ["contact_person", "contact_display", "contact_email",
|
||||
"contact_mobile", "contact_phone", "contact_designation", "contact_department"]:
|
||||
out[fieldname] = None
|
||||
|
||||
contact = webnotes.conn.sql("""select * from `tabContact` where `%s`=%s
|
||||
order by is_primary_contact desc, name asc limit 1""" % (party_field, "%s"),
|
||||
(party_name,), as_dict=True)
|
||||
if contact:
|
||||
contact = contact[0]
|
||||
out.update({
|
||||
"contact_person": contact.get("name"),
|
||||
"contact_display": " ".join(filter(None,
|
||||
[contact.get("first_name"), contact.get("last_name")])),
|
||||
"contact_email": contact.get("email_id"),
|
||||
"contact_mobile": contact.get("mobile_no"),
|
||||
"contact_phone": contact.get("phone"),
|
||||
"contact_designation": contact.get("designation"),
|
||||
"contact_department": contact.get("department")
|
||||
})
|
||||
|
||||
return out
|
||||
|
||||
def get_address_territory(address_doc):
|
||||
territory = None
|
||||
for fieldname in ("city", "state", "country"):
|
||||
value = address_doc.fields.get(fieldname)
|
||||
if value:
|
||||
territory = webnotes.conn.get_value("Territory", value.strip())
|
||||
if territory:
|
||||
break
|
||||
|
||||
return territory
|
||||
|
||||
def validate_conversion_rate(currency, conversion_rate, conversion_rate_label, company):
|
||||
"""common validation for currency and price list currency"""
|
||||
|
||||
Reference in New Issue
Block a user