2012-02-23 07:05:32 +00:00
# ERPNext - web based ERP (http://erpnext.com)
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2011-06-08 09:07:15 +00:00
import webnotes
2011-06-28 13:31:02 +00:00
from webnotes . utils import load_json , cint , cstr , flt , get_defaults
2011-06-08 09:07:15 +00:00
from webnotes . model . doc import Document , addchild , removechild , getchildren
from webnotes . model . doclist import getlist , copy_doclist
2012-04-30 09:06:18 +00:00
from webnotes . model . code import get_obj
2011-06-08 09:07:15 +00:00
from webnotes import msgprint
class TransactionBase :
# 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 )
contact_text , contact_name , contact_email , contact_mobile = self . get_contact_text ( customer = self . doc . customer )
self . doc . customer_address = address_name or ' '
self . doc . contact_person = contact_name or ' '
self . doc . address_display = address_text or ' '
self . doc . contact_display = contact_text or ' '
self . doc . contact_email = contact_email or ' '
self . doc . contact_mobile = contact_mobile or ' '
2012-04-20 04:55:03 +00:00
2012-02-14 09:43:21 +00:00
if args != ' onload ' :
2012-04-20 04:55:03 +00:00
self . get_customer_details ( self . doc . customer )
2012-02-14 09:43:21 +00:00
self . get_sales_person ( self . doc . customer )
2011-06-08 09:07:15 +00:00
# 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 )
contact_text , contact_name , contact_email , contact_mobile = self . get_contact_text ( customer = self . doc . customer )
self . doc . customer_address = address_name or ' '
self . doc . contact_person = contact_name or ' '
self . doc . address_display = address_text or ' '
self . doc . contact_display = contact_text or ' '
self . doc . contact_email = contact_email or ' '
self . doc . contact_mobile = contact_mobile or ' '
2012-02-14 09:43:21 +00:00
if self . doc . doctype != ' Quotation ' and args != ' onload ' :
2012-04-20 04:55:03 +00:00
self . get_customer_details ( self . doc . customer )
2012-02-14 09:43:21 +00:00
self . get_sales_person ( self . doc . customer )
2011-06-08 09:07:15 +00:00
# Get Customer Address
# -----------------------
def get_customer_address ( self , args ) :
args = load_json ( args )
address_text , address_name = self . get_address_text ( address_name = args [ ' address ' ] )
contact_text , contact_name , contact_email , contact_mobile = self . get_contact_text ( contact_name = args [ ' contact ' ] )
ret = {
' customer_address ' : address_name ,
' contact_person ' : contact_name ,
' address_display ' : address_text ,
' contact_display ' : contact_text ,
' contact_email ' : contact_email ,
' contact_mobile ' : contact_mobile
}
2011-08-16 11:08:11 +00:00
return ret
2011-06-08 09:07:15 +00:00
# Get Address Text
# -----------------------
def get_address_text ( self , customer = None , address_name = None , supplier = None , is_shipping_address = None ) :
if customer :
cond = customer and ' customer= " %s " ' % customer or ' name= " %s " ' % address_name
elif supplier :
cond = supplier and ' supplier= " %s " ' % supplier or ' name= " %s " ' % address_name
else :
cond = ' name= " %s " ' % address_name
if is_shipping_address :
2012-03-21 11:37:42 +00:00
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 limit 1 " % cond , as_dict = 1 )
2011-06-08 09:07:15 +00:00
else :
2012-03-21 11:37:42 +00:00
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 )
2011-06-08 09:07:15 +00:00
extract = lambda x : details and details [ 0 ] and details [ 0 ] . get ( x , ' ' ) or ' '
2012-03-21 11:37:42 +00:00
address_fields = [ ( ' ' , ' address_line1 ' ) , ( ' \n ' , ' address_line2 ' ) , ( ' \n ' , ' city ' ) , ( ' ' , ' pincode ' ) , ( ' \n ' , ' state ' ) , ( ' \n ' , ' country ' ) , ( ' \n Phone: ' , ' phone ' ) , ( ' \n Fax: ' , ' fax ' ) ]
2011-06-08 09:07:15 +00:00
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_name = details and details [ 0 ] [ ' name ' ] or ' '
return address_display , address_name
# Get Contact Text
# -----------------------
def get_contact_text ( self , customer = None , contact_name = None , supplier = None ) :
if customer :
cond = customer and ' customer= " %s " ' % customer or ' name= " %s " ' % contact_name
elif supplier :
cond = supplier and ' supplier= " %s " ' % supplier or ' name= " %s " ' % contact_name
else :
cond = ' name= " %s " ' % contact_name
2012-01-27 06:55:09 +00:00
details = webnotes . conn . sql ( " select name, first_name, last_name, email_id, phone, mobile_no, department, designation from `tabContact` where %s and docstatus != 2 order by is_primary_contact desc limit 1 " % cond , as_dict = 1 )
2011-06-08 09:07:15 +00:00
extract = lambda x : details and details [ 0 ] and details [ 0 ] . get ( x , ' ' ) or ' '
2012-01-17 08:52:09 +00:00
contact_fields = [ ( ' ' , ' first_name ' ) , ( ' ' , ' last_name ' ) ]
contact_display = ' ' . join ( [ a [ 0 ] + cstr ( extract ( a [ 1 ] ) ) for a in contact_fields if extract ( a [ 1 ] ) ] )
2011-06-08 09:07:15 +00:00
if contact_display . startswith ( ' \n ' ) : contact_display = contact_display [ 1 : ]
contact_name = details and details [ 0 ] [ ' name ' ] or ' '
contact_email = details and details [ 0 ] [ ' email_id ' ] or ' '
contact_mobile = details and details [ 0 ] [ ' mobile_no ' ] or ' '
return contact_display , contact_name , contact_email , contact_mobile
# Get Customer Details
# -----------------------
def get_customer_details ( self , name ) :
2012-04-20 04:55:03 +00:00
customer_details = webnotes . conn . sql ( " select customer_name, customer_group, territory, default_sales_partner, default_commission_rate, default_price_list from tabCustomer where name = ' %s ' and docstatus != 2 " % ( name ) , as_dict = 1 )
2011-06-08 09:07:15 +00:00
if customer_details :
self . doc . customer_name = customer_details [ 0 ] [ ' customer_name ' ] or ' '
self . doc . customer_group = customer_details [ 0 ] [ ' customer_group ' ] or ' '
self . doc . territory = customer_details [ 0 ] [ ' territory ' ] or ' '
self . doc . sales_partner = customer_details [ 0 ] [ ' default_sales_partner ' ] or ' '
self . doc . commission_rate = customer_details [ 0 ] [ ' default_commission_rate ' ] or ' '
2012-04-20 04:55:03 +00:00
def_price_list = customer_details [ 0 ] [ ' default_price_list ' ] or ' '
if not def_price_list :
cg_price_list = webnotes . conn . sql ( " select default_price_list from `tabCustomer Group` where name = %s " , customer_details [ 0 ] [ ' customer_group ' ] )
def_price_list = cg_price_list and cg_price_list [ 0 ] [ 0 ] or ' '
self . doc . price_list_name = def_price_list or self . doc . price_list_name
2011-06-08 09:07:15 +00:00
# Get Customer Shipping Address
# -----------------------
def get_shipping_address ( self , name ) :
2012-01-27 06:55:09 +00:00
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 limit 1 " % ( name ) , as_dict = 1 )
2011-06-08 09:07:15 +00:00
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 ' ) , ( ' \n Phone: ' , ' 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 : ]
ret = {
' shipping_address_name ' : details and details [ 0 ] [ ' name ' ] or ' ' ,
' shipping_address ' : address_display
}
2011-08-16 11:08:11 +00:00
return ret
2011-06-08 09:07:15 +00:00
# Get Lead Details
# -----------------------
def get_lead_details ( self , name ) :
2012-03-20 11:14:15 +00:00
details = webnotes . conn . sql ( " select name, lead_name, address_line1, address_line2, city, country, state, pincode, territory, contact_no, mobile_no, email_id, company_name from `tabLead` where name = ' %s ' " % ( name ) , as_dict = 1 )
2011-06-08 09:07:15 +00:00
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 ' ) , ( ' \n Phone: ' , ' contact_no ' ) ]
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 : ]
ret = {
' lead_name ' : extract ( ' lead_name ' ) ,
' address_display ' : address_display ,
' territory ' : extract ( ' territory ' ) ,
' contact_mobile ' : extract ( ' mobile_no ' ) ,
2012-03-20 11:14:15 +00:00
' contact_email ' : extract ( ' email_id ' ) ,
' organization ' : extract ( ' company_name ' )
2011-06-08 09:07:15 +00:00
}
2011-08-16 11:08:11 +00:00
return ret
2011-06-08 09:07:15 +00:00
# Get Supplier Default Primary Address - first load
# -----------------------
def get_default_supplier_address ( self , args ) :
args = load_json ( args )
address_text , address_name = self . get_address_text ( supplier = args [ ' supplier ' ] )
contact_text , contact_name , contact_email , contact_mobile = self . get_contact_text ( supplier = args [ ' supplier ' ] )
ret = {
' supplier_address ' : address_name ,
' address_display ' : address_text ,
' contact_person ' : contact_name ,
' contact_display ' : contact_text ,
' contact_email ' : contact_email ,
' contact_mobile ' : contact_mobile
}
ret . update ( self . get_supplier_details ( args [ ' supplier ' ] ) )
2011-08-16 11:08:11 +00:00
return ret
2011-06-08 09:07:15 +00:00
# Get Supplier Address
# -----------------------
def get_supplier_address ( self , args ) :
args = load_json ( args )
address_text , address_name = self . get_address_text ( address_name = args [ ' address ' ] )
contact_text , contact_name , contact_email , contact_mobile = self . get_contact_text ( contact_name = args [ ' contact ' ] )
ret = {
' supplier_address ' : address_name ,
' address_display ' : address_text ,
' contact_person ' : contact_name ,
' contact_display ' : contact_text ,
' contact_email ' : contact_email ,
' contact_mobile ' : contact_mobile
}
2011-08-16 11:08:11 +00:00
return ret
2011-06-08 09:07:15 +00:00
# Get Supplier Details
# -----------------------
def get_supplier_details ( self , name ) :
2012-01-27 06:55:09 +00:00
supplier_details = webnotes . conn . sql ( " select supplier_name from tabSupplier where name = ' %s ' and docstatus != 2 " % ( name ) , as_dict = 1 )
2011-06-08 09:07:15 +00:00
ret = {
' supplier_name ' : supplier_details and supplier_details [ 0 ] [ ' supplier_name ' ] or ' '
}
return ret
# Get Sales Person Details of Customer
# ------------------------------------
def get_sales_person ( self , name ) :
self . doc . clear_table ( self . doclist , ' sales_team ' )
idx = 0
2012-01-27 06:55:09 +00:00
for d in webnotes . conn . sql ( " select sales_person, allocated_percentage, allocated_amount, incentives from `tabSales Team` where parent = ' %s ' " % name ) :
2011-06-08 09:07:15 +00:00
ch = addchild ( self . doc , ' sales_team ' , ' Sales Team ' , 1 , 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
2011-06-28 13:31:02 +00:00
# Get Company Specific Default Currency
# -------------------------------------
def get_company_currency ( self , name ) :
2012-01-27 06:55:09 +00:00
ret = webnotes . conn . sql ( " select default_currency from tabCompany where name = ' %s ' " % ( name ) )
2011-06-28 13:31:02 +00:00
dcc = ret and ret [ 0 ] [ 0 ] or get_defaults ( ) [ ' currency ' ]
return dcc
2012-04-30 09:06:18 +00:00
def get_formatted_message ( self , args ) :
""" get formatted message for auto notification """
return get_obj ( ' Notification Control ' ) . get_formatted_message ( args )