blob: 3bc78217008fae22b7353413944a1509a1645bcf [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
Rushabh Mehta0eb790a2012-05-11 15:53:37 +053019from webnotes.model.doc import Document, addchild, getchildren
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053020from 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
Anand Doshif2f5c942012-07-18 20:13:52 +0530119 def get_customer_details(self, name):
120 """
121 Get customer details like name, group, territory
122 and other such defaults
123 """
124 customer_details = webnotes.conn.sql("""\
125 select
126 customer_name, customer_group, territory,
127 default_sales_partner, default_commission_rate, default_currency,
128 default_price_list
129 from `tabCustomer`
130 where name = %s and docstatus < 2""", name, as_dict=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530131 if customer_details:
Anand Doshif2f5c942012-07-18 20:13:52 +0530132 for f in ['customer_name', 'customer_group', 'territory']:
133 self.doc.fields[f] = customer_details[0][f] or self.doc.fields.get(f)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530134
Anand Doshif2f5c942012-07-18 20:13:52 +0530135 # fields prepended with default in Customer doctype
136 for f in ['sales_partner', 'commission_rate', 'currency']:
137 self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f)
138
139 # optionally fetch default price list from Customer Group
140 self.doc.price_list_name = (customer_details[0]['default_price_list']
141 or webnotes.conn.get_value('Customer Group', self.doc.customer_group,
142 'default_price_list')
143 or self.doc.fields.get('price_list_name'))
144
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530145 # Get Customer Shipping Address
146 # -----------------------
147 def get_shipping_address(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530148 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 +0530149
150 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
151 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
152 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
153 if address_display.startswith('\n'): address_display = address_display[1:]
154
155 ret = {
156 'shipping_address_name' : details and details[0]['name'] or '',
157 'shipping_address' : address_display
158 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530159 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530160
161 # Get Lead Details
162 # -----------------------
163 def get_lead_details(self, name):
Nabin Hait9ff92362012-03-20 16:44:15 +0530164 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 +0530165
166 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
167 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
168 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
169 if address_display.startswith('\n'): address_display = address_display[1:]
170
171 ret = {
172 'lead_name' : extract('lead_name'),
173 'address_display' : address_display,
174 'territory' : extract('territory'),
175 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530176 'contact_email' : extract('email_id'),
177 'organization' : extract('company_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530178 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530179 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530180
181
182 # Get Supplier Default Primary Address - first load
183 # -----------------------
184 def get_default_supplier_address(self, args):
185 args = load_json(args)
186 address_text, address_name = self.get_address_text(supplier=args['supplier'])
187 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier'])
188 ret = {
189 'supplier_address' : address_name,
190 'address_display' : address_text,
191 'contact_person' : contact_name,
192 'contact_display' : contact_text,
193 'contact_email' : contact_email,
194 'contact_mobile' : contact_mobile
195 }
196 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530197 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530198
199 # Get Supplier Address
200 # -----------------------
201 def get_supplier_address(self, args):
202 args = load_json(args)
203 address_text, address_name = self.get_address_text(address_name=args['address'])
204 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
205 ret = {
206 'supplier_address' : address_name,
Anand Doshif2f5c942012-07-18 20:13:52 +0530207 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530208 'contact_person' : contact_name,
209 'contact_display' : contact_text,
210 'contact_email' : contact_email,
211 'contact_mobile' : contact_mobile
212 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530213 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530214
215 # Get Supplier Details
216 # -----------------------
Anand Doshif2f5c942012-07-18 20:13:52 +0530217 def get_supplier_details(self, name):
218 supplier_details = webnotes.conn.sql("""\
219 select supplier_name, default_currency
220 from `tabSupplier`
221 where name = %s and docstatus < 2""", name, as_dict=1)
222 if supplier_details:
223 return {
224 'supplier_name': (supplier_details[0]['supplier_name']
225 or self.doc.fields.get('supplier_name')),
226 'currency': (supplier_details[0]['default_currency']
227 or self.doc.fields.get('currency')),
228 }
229 else:
230 return {}
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530231
232 # Get Sales Person Details of Customer
233 # ------------------------------------
234 def get_sales_person(self, name):
Anand Doshi8ae5ba92012-06-25 20:05:35 +0530235 self.doclist = self.doc.clear_table(self.doclist,'sales_team')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530236 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530237 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 +0530238 ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist)
239 ch.sales_person = d and cstr(d[0]) or ''
240 ch.allocated_percentage = d and flt(d[1]) or 0
241 ch.allocated_amount = d and flt(d[2]) or 0
242 ch.incentives = d and flt(d[3]) or 0
243 ch.idx = idx
244 idx += 1
Ravi Dey5708da52011-06-28 19:01:02 +0530245
246 # Get Company Specific Default Currency
247 # -------------------------------------
248 def get_company_currency(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530249 ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name))
Ravi Dey5708da52011-06-28 19:01:02 +0530250 dcc = ret and ret[0][0] or get_defaults()['currency']
251 return dcc
Nabin Hait41cc3272012-04-30 14:36:18 +0530252
253
254 def get_formatted_message(self, args):
255 """ get formatted message for auto notification"""
256 return get_obj('Notification Control').get_formatted_message(args)