blob: 0e10eeacb076e90b9fd3155c5b8e70afe30650c2 [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
Anand Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053018import webnotes
Ravi Dey5708da52011-06-28 19:01:02 +053019from webnotes.utils import load_json, cint, cstr, flt, get_defaults
Rushabh Mehta0eb790a2012-05-11 15:53:37 +053020from webnotes.model.doc import Document, addchild, getchildren
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053021from webnotes.model.doclist import getlist, copy_doclist
Nabin Hait41cc3272012-04-30 14:36:18 +053022from webnotes.model.code import get_obj
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053023from webnotes import msgprint
24
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053025class TransactionBase:
26
27 # Get Customer Default Primary Address - first load
28 # -----------------------
29 def get_default_customer_address(self, args=''):
30 address_text, address_name = self.get_address_text(customer=self.doc.customer)
31 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer)
32 self.doc.customer_address = address_name or ''
33 self.doc.contact_person = contact_name or ''
34 self.doc.address_display = address_text or ''
35 self.doc.contact_display = contact_text or ''
36 self.doc.contact_email = contact_email or ''
37 self.doc.contact_mobile = contact_mobile or ''
Nabin Hait239e7902012-04-20 10:25:03 +053038
Nabin Hait84d73102012-02-14 15:13:21 +053039 if args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053040 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053041 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053042
43 # Get Customer Default Shipping Address - first load
44 # -----------------------
45 def get_default_customer_shipping_address(self, args=''):
46 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
47 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer)
48 self.doc.customer_address = address_name or ''
49 self.doc.contact_person = contact_name or ''
50 self.doc.address_display = address_text or ''
51 self.doc.contact_display = contact_text or ''
52 self.doc.contact_email = contact_email or ''
53 self.doc.contact_mobile = contact_mobile or ''
54
Nabin Hait84d73102012-02-14 15:13:21 +053055 if self.doc.doctype != 'Quotation' and args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053056 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053057 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053058
59 # Get Customer Address
60 # -----------------------
61 def get_customer_address(self, args):
62 args = load_json(args)
63 address_text, address_name = self.get_address_text(address_name=args['address'])
64 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
65 ret = {
66 'customer_address' : address_name,
67 'contact_person' : contact_name,
68 'address_display' : address_text,
69 'contact_display' : contact_text,
70 'contact_email' : contact_email,
71 'contact_mobile' : contact_mobile
72 }
Nabin Hait06c4de82011-08-16 16:38:11 +053073 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053074
75 # Get Address Text
76 # -----------------------
77 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
78 if customer:
79 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
80 elif supplier:
81 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
82 else:
83 cond = 'name="%s"' % address_name
84
85 if is_shipping_address:
Nabin Hait65353652012-03-21 17:07:42 +053086 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 +053087 else:
Nabin Hait65353652012-03-21 17:07:42 +053088 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 +053089
90 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Nabin Hait65353652012-03-21 17:07:42 +053091 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 +053092 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
93 if address_display.startswith('\n'): address_display = address_display[1:]
94
95 address_name = details and details[0]['name'] or ''
96 return address_display, address_name
97
98 # Get Contact Text
99 # -----------------------
100 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
101 if customer:
102 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
103 elif supplier:
104 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
105 else:
106 cond = 'name="%s"' % contact_name
107
Anand Doshi74d1b652012-01-27 12:25:09 +0530108 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 +0530109
110 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +0530111 contact_fields = [('','first_name'),(' ','last_name')]
112 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 +0530113 if contact_display.startswith('\n'): contact_display = contact_display[1:]
114
115 contact_name = details and details[0]['name'] or ''
116 contact_email = details and details[0]['email_id'] or ''
117 contact_mobile = details and details[0]['mobile_no'] or ''
118 return contact_display, contact_name, contact_email, contact_mobile
119
Anand Doshif2f5c942012-07-18 20:13:52 +0530120 def get_customer_details(self, name):
121 """
122 Get customer details like name, group, territory
123 and other such defaults
124 """
125 customer_details = webnotes.conn.sql("""\
126 select
127 customer_name, customer_group, territory,
128 default_sales_partner, default_commission_rate, default_currency,
129 default_price_list
130 from `tabCustomer`
131 where name = %s and docstatus < 2""", name, as_dict=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530132 if customer_details:
Anand Doshif2f5c942012-07-18 20:13:52 +0530133 for f in ['customer_name', 'customer_group', 'territory']:
134 self.doc.fields[f] = customer_details[0][f] or self.doc.fields.get(f)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530135
Anand Doshif2f5c942012-07-18 20:13:52 +0530136 # fields prepended with default in Customer doctype
137 for f in ['sales_partner', 'commission_rate', 'currency']:
138 self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f)
139
140 # optionally fetch default price list from Customer Group
141 self.doc.price_list_name = (customer_details[0]['default_price_list']
142 or webnotes.conn.get_value('Customer Group', self.doc.customer_group,
143 'default_price_list')
144 or self.doc.fields.get('price_list_name'))
145
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530146 # Get Customer Shipping Address
147 # -----------------------
148 def get_shipping_address(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530149 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 +0530150
151 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
152 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
153 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
154 if address_display.startswith('\n'): address_display = address_display[1:]
155
156 ret = {
157 'shipping_address_name' : details and details[0]['name'] or '',
158 'shipping_address' : address_display
159 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530160 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530161
162 # Get Lead Details
163 # -----------------------
164 def get_lead_details(self, name):
Nabin Hait9ff92362012-03-20 16:44:15 +0530165 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 +0530166
167 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
168 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
169 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
170 if address_display.startswith('\n'): address_display = address_display[1:]
171
172 ret = {
173 'lead_name' : extract('lead_name'),
174 'address_display' : address_display,
175 'territory' : extract('territory'),
176 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530177 'contact_email' : extract('email_id'),
178 'organization' : extract('company_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530179 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530180 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530181
182
183 # Get Supplier Default Primary Address - first load
184 # -----------------------
185 def get_default_supplier_address(self, args):
186 args = load_json(args)
187 address_text, address_name = self.get_address_text(supplier=args['supplier'])
188 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier'])
189 ret = {
190 'supplier_address' : address_name,
191 'address_display' : address_text,
192 'contact_person' : contact_name,
193 'contact_display' : contact_text,
194 'contact_email' : contact_email,
195 'contact_mobile' : contact_mobile
196 }
197 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530198 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530199
200 # Get Supplier Address
201 # -----------------------
202 def get_supplier_address(self, args):
203 args = load_json(args)
204 address_text, address_name = self.get_address_text(address_name=args['address'])
205 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
206 ret = {
207 'supplier_address' : address_name,
Anand Doshif2f5c942012-07-18 20:13:52 +0530208 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530209 'contact_person' : contact_name,
210 'contact_display' : contact_text,
211 'contact_email' : contact_email,
212 'contact_mobile' : contact_mobile
213 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530214 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530215
216 # Get Supplier Details
217 # -----------------------
Anand Doshif2f5c942012-07-18 20:13:52 +0530218 def get_supplier_details(self, name):
219 supplier_details = webnotes.conn.sql("""\
220 select supplier_name, default_currency
221 from `tabSupplier`
222 where name = %s and docstatus < 2""", name, as_dict=1)
223 if supplier_details:
224 return {
225 'supplier_name': (supplier_details[0]['supplier_name']
226 or self.doc.fields.get('supplier_name')),
227 'currency': (supplier_details[0]['default_currency']
228 or self.doc.fields.get('currency')),
229 }
230 else:
231 return {}
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530232
233 # Get Sales Person Details of Customer
234 # ------------------------------------
235 def get_sales_person(self, name):
Anand Doshi8ae5ba92012-06-25 20:05:35 +0530236 self.doclist = self.doc.clear_table(self.doclist,'sales_team')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530237 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530238 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 +0530239 ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist)
240 ch.sales_person = d and cstr(d[0]) or ''
241 ch.allocated_percentage = d and flt(d[1]) or 0
242 ch.allocated_amount = d and flt(d[2]) or 0
243 ch.incentives = d and flt(d[3]) or 0
244 ch.idx = idx
245 idx += 1
Ravi Dey5708da52011-06-28 19:01:02 +0530246
247 # Get Company Specific Default Currency
248 # -------------------------------------
249 def get_company_currency(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530250 ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name))
Ravi Dey5708da52011-06-28 19:01:02 +0530251 dcc = ret and ret[0][0] or get_defaults()['currency']
252 return dcc
Nabin Hait41cc3272012-04-30 14:36:18 +0530253
254
255 def get_formatted_message(self, args):
256 """ get formatted message for auto notification"""
257 return get_obj('Notification Control').get_formatted_message(args)