blob: 902e193cd77977048edaaad561b5273a8c0936eb [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053017import webnotes
Ravi Dey5708da52011-06-28 19:01:02 +053018from webnotes.utils import load_json, cint, cstr, flt, get_defaults
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053019from webnotes.model.doc import Document, addchild, removechild, getchildren
20from webnotes.model.doclist import getlist, copy_doclist
Nabin Hait41cc3272012-04-30 14:36:18 +053021from webnotes.model.code import get_obj
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053022from webnotes import msgprint
23
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053024class TransactionBase:
25
26 # Get Customer Default Primary Address - first load
27 # -----------------------
28 def get_default_customer_address(self, args=''):
29 address_text, address_name = self.get_address_text(customer=self.doc.customer)
30 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer)
31 self.doc.customer_address = address_name or ''
32 self.doc.contact_person = contact_name or ''
33 self.doc.address_display = address_text or ''
34 self.doc.contact_display = contact_text or ''
35 self.doc.contact_email = contact_email or ''
36 self.doc.contact_mobile = contact_mobile or ''
Nabin Hait239e7902012-04-20 10:25:03 +053037
Nabin Hait84d73102012-02-14 15:13:21 +053038 if args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053039 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053040 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053041
42 # Get Customer Default Shipping Address - first load
43 # -----------------------
44 def get_default_customer_shipping_address(self, args=''):
45 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
46 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer)
47 self.doc.customer_address = address_name or ''
48 self.doc.contact_person = contact_name or ''
49 self.doc.address_display = address_text or ''
50 self.doc.contact_display = contact_text or ''
51 self.doc.contact_email = contact_email or ''
52 self.doc.contact_mobile = contact_mobile or ''
53
Nabin Hait84d73102012-02-14 15:13:21 +053054 if self.doc.doctype != 'Quotation' and args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053055 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053056 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053057
58 # Get Customer Address
59 # -----------------------
60 def get_customer_address(self, args):
61 args = load_json(args)
62 address_text, address_name = self.get_address_text(address_name=args['address'])
63 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
64 ret = {
65 'customer_address' : address_name,
66 'contact_person' : contact_name,
67 'address_display' : address_text,
68 'contact_display' : contact_text,
69 'contact_email' : contact_email,
70 'contact_mobile' : contact_mobile
71 }
Nabin Hait06c4de82011-08-16 16:38:11 +053072 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053073
74 # Get Address Text
75 # -----------------------
76 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
77 if customer:
78 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
79 elif supplier:
80 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
81 else:
82 cond = 'name="%s"' % address_name
83
84 if is_shipping_address:
Nabin Hait65353652012-03-21 17:07:42 +053085 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)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053086 else:
Nabin Hait65353652012-03-21 17:07:42 +053087 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)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053088
89 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Nabin Hait65353652012-03-21 17:07:42 +053090 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone'),('\nFax: ', 'fax')]
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053091 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
92 if address_display.startswith('\n'): address_display = address_display[1:]
93
94 address_name = details and details[0]['name'] or ''
95 return address_display, address_name
96
97 # Get Contact Text
98 # -----------------------
99 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
100 if customer:
101 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
102 elif supplier:
103 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
104 else:
105 cond = 'name="%s"' % contact_name
106
Anand Doshi74d1b652012-01-27 12:25:09 +0530107 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)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530108
109 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +0530110 contact_fields = [('','first_name'),(' ','last_name')]
111 contact_display = ''.join([a[0]+cstr(extract(a[1])) for a in contact_fields if extract(a[1])])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530112 if contact_display.startswith('\n'): contact_display = contact_display[1:]
113
114 contact_name = details and details[0]['name'] or ''
115 contact_email = details and details[0]['email_id'] or ''
116 contact_mobile = details and details[0]['mobile_no'] or ''
117 return contact_display, contact_name, contact_email, contact_mobile
118
119 # Get Customer Details
120 # -----------------------
121 def get_customer_details(self, name):
Nabin Hait239e7902012-04-20 10:25:03 +0530122 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)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530123 if customer_details:
124 self.doc.customer_name = customer_details[0]['customer_name'] or ''
125 self.doc.customer_group = customer_details[0]['customer_group'] or ''
126 self.doc.territory = customer_details[0]['territory'] or ''
127 self.doc.sales_partner = customer_details[0]['default_sales_partner'] or ''
128 self.doc.commission_rate = customer_details[0]['default_commission_rate'] or ''
Nabin Hait239e7902012-04-20 10:25:03 +0530129 def_price_list = customer_details[0]['default_price_list'] or ''
130 if not def_price_list:
131 cg_price_list = webnotes.conn.sql("select default_price_list from `tabCustomer Group` where name = %s", customer_details[0]['customer_group'])
132 def_price_list = cg_price_list and cg_price_list[0][0] or ''
133 self.doc.price_list_name = def_price_list or self.doc.price_list_name
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530134
135 # Get Customer Shipping Address
136 # -----------------------
137 def get_shipping_address(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530138 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)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530139
140 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
141 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
142 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
143 if address_display.startswith('\n'): address_display = address_display[1:]
144
145 ret = {
146 'shipping_address_name' : details and details[0]['name'] or '',
147 'shipping_address' : address_display
148 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530149 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530150
151 # Get Lead Details
152 # -----------------------
153 def get_lead_details(self, name):
Nabin Hait9ff92362012-03-20 16:44:15 +0530154 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)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530155
156 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
157 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
158 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
159 if address_display.startswith('\n'): address_display = address_display[1:]
160
161 ret = {
162 'lead_name' : extract('lead_name'),
163 'address_display' : address_display,
164 'territory' : extract('territory'),
165 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530166 'contact_email' : extract('email_id'),
167 'organization' : extract('company_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530168 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530169 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530170
171
172 # Get Supplier Default Primary Address - first load
173 # -----------------------
174 def get_default_supplier_address(self, args):
175 args = load_json(args)
176 address_text, address_name = self.get_address_text(supplier=args['supplier'])
177 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier'])
178 ret = {
179 'supplier_address' : address_name,
180 'address_display' : address_text,
181 'contact_person' : contact_name,
182 'contact_display' : contact_text,
183 'contact_email' : contact_email,
184 'contact_mobile' : contact_mobile
185 }
186 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530187 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530188
189 # Get Supplier Address
190 # -----------------------
191 def get_supplier_address(self, args):
192 args = load_json(args)
193 address_text, address_name = self.get_address_text(address_name=args['address'])
194 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
195 ret = {
196 'supplier_address' : address_name,
197 'address_display' : address_text,
198 'contact_person' : contact_name,
199 'contact_display' : contact_text,
200 'contact_email' : contact_email,
201 'contact_mobile' : contact_mobile
202 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530203 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530204
205 # Get Supplier Details
206 # -----------------------
207 def get_supplier_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530208 supplier_details = webnotes.conn.sql("select supplier_name from tabSupplier where name = '%s' and docstatus != 2" %(name), as_dict = 1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530209 ret = {
210 'supplier_name' : supplier_details and supplier_details[0]['supplier_name'] or ''
211 }
212 return ret
213
214 # Get Sales Person Details of Customer
215 # ------------------------------------
216 def get_sales_person(self, name):
217 self.doc.clear_table(self.doclist,'sales_team')
218 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530219 for d in webnotes.conn.sql("select sales_person, allocated_percentage, allocated_amount, incentives from `tabSales Team` where parent = '%s'" % name):
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530220 ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist)
221 ch.sales_person = d and cstr(d[0]) or ''
222 ch.allocated_percentage = d and flt(d[1]) or 0
223 ch.allocated_amount = d and flt(d[2]) or 0
224 ch.incentives = d and flt(d[3]) or 0
225 ch.idx = idx
226 idx += 1
Ravi Dey5708da52011-06-28 19:01:02 +0530227
228 # Get Company Specific Default Currency
229 # -------------------------------------
230 def get_company_currency(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530231 ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name))
Ravi Dey5708da52011-06-28 19:01:02 +0530232 dcc = ret and ret[0][0] or get_defaults()['currency']
233 return dcc
Nabin Hait41cc3272012-04-30 14:36:18 +0530234
235
236 def get_formatted_message(self, args):
237 """ get formatted message for auto notification"""
238 return get_obj('Notification Control').get_formatted_message(args)