blob: eecbb068549a4367b0a0b5dc760119c1d53787d0 [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
21from webnotes import msgprint
22
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053023class TransactionBase:
24
25 # Get Customer Default Primary Address - first load
26 # -----------------------
27 def get_default_customer_address(self, args=''):
28 address_text, address_name = self.get_address_text(customer=self.doc.customer)
29 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer)
30 self.doc.customer_address = address_name or ''
31 self.doc.contact_person = contact_name or ''
32 self.doc.address_display = address_text or ''
33 self.doc.contact_display = contact_text or ''
34 self.doc.contact_email = contact_email or ''
35 self.doc.contact_mobile = contact_mobile or ''
Nabin Hait239e7902012-04-20 10:25:03 +053036
Nabin Hait84d73102012-02-14 15:13:21 +053037 if args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053038 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053039 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053040
41 # Get Customer Default Shipping Address - first load
42 # -----------------------
43 def get_default_customer_shipping_address(self, args=''):
44 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
45 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer)
46 self.doc.customer_address = address_name or ''
47 self.doc.contact_person = contact_name or ''
48 self.doc.address_display = address_text or ''
49 self.doc.contact_display = contact_text or ''
50 self.doc.contact_email = contact_email or ''
51 self.doc.contact_mobile = contact_mobile or ''
52
Nabin Hait84d73102012-02-14 15:13:21 +053053 if self.doc.doctype != 'Quotation' and args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053054 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053055 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053056
57 # Get Customer Address
58 # -----------------------
59 def get_customer_address(self, args):
60 args = load_json(args)
61 address_text, address_name = self.get_address_text(address_name=args['address'])
62 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
63 ret = {
64 'customer_address' : address_name,
65 'contact_person' : contact_name,
66 'address_display' : address_text,
67 'contact_display' : contact_text,
68 'contact_email' : contact_email,
69 'contact_mobile' : contact_mobile
70 }
Nabin Hait06c4de82011-08-16 16:38:11 +053071 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053072
73 # Get Address Text
74 # -----------------------
75 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
76 if customer:
77 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
78 elif supplier:
79 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
80 else:
81 cond = 'name="%s"' % address_name
82
83 if is_shipping_address:
Nabin Hait65353652012-03-21 17:07:42 +053084 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 +053085 else:
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_primary_address desc limit 1" % cond, as_dict = 1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053087
88 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Nabin Hait65353652012-03-21 17:07:42 +053089 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 +053090 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
91 if address_display.startswith('\n'): address_display = address_display[1:]
92
93 address_name = details and details[0]['name'] or ''
94 return address_display, address_name
95
96 # Get Contact Text
97 # -----------------------
98 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
99 if customer:
100 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
101 elif supplier:
102 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
103 else:
104 cond = 'name="%s"' % contact_name
105
Anand Doshi74d1b652012-01-27 12:25:09 +0530106 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 +0530107
108 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +0530109 contact_fields = [('','first_name'),(' ','last_name')]
110 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 +0530111 if contact_display.startswith('\n'): contact_display = contact_display[1:]
112
113 contact_name = details and details[0]['name'] or ''
114 contact_email = details and details[0]['email_id'] or ''
115 contact_mobile = details and details[0]['mobile_no'] or ''
116 return contact_display, contact_name, contact_email, contact_mobile
117
118 # Get Customer Details
119 # -----------------------
120 def get_customer_details(self, name):
Nabin Hait239e7902012-04-20 10:25:03 +0530121 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 +0530122 if customer_details:
123 self.doc.customer_name = customer_details[0]['customer_name'] or ''
124 self.doc.customer_group = customer_details[0]['customer_group'] or ''
125 self.doc.territory = customer_details[0]['territory'] or ''
126 self.doc.sales_partner = customer_details[0]['default_sales_partner'] or ''
127 self.doc.commission_rate = customer_details[0]['default_commission_rate'] or ''
Nabin Hait239e7902012-04-20 10:25:03 +0530128 def_price_list = customer_details[0]['default_price_list'] or ''
129 if not def_price_list:
130 cg_price_list = webnotes.conn.sql("select default_price_list from `tabCustomer Group` where name = %s", customer_details[0]['customer_group'])
131 def_price_list = cg_price_list and cg_price_list[0][0] or ''
132 self.doc.price_list_name = def_price_list or self.doc.price_list_name
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530133
134 # Get Customer Shipping Address
135 # -----------------------
136 def get_shipping_address(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530137 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 +0530138
139 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
140 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
141 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
142 if address_display.startswith('\n'): address_display = address_display[1:]
143
144 ret = {
145 'shipping_address_name' : details and details[0]['name'] or '',
146 'shipping_address' : address_display
147 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530148 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530149
150 # Get Lead Details
151 # -----------------------
152 def get_lead_details(self, name):
Nabin Hait9ff92362012-03-20 16:44:15 +0530153 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 +0530154
155 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
156 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
157 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
158 if address_display.startswith('\n'): address_display = address_display[1:]
159
160 ret = {
161 'lead_name' : extract('lead_name'),
162 'address_display' : address_display,
163 'territory' : extract('territory'),
164 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530165 'contact_email' : extract('email_id'),
166 'organization' : extract('company_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530167 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530168 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530169
170
171 # Get Supplier Default Primary Address - first load
172 # -----------------------
173 def get_default_supplier_address(self, args):
174 args = load_json(args)
175 address_text, address_name = self.get_address_text(supplier=args['supplier'])
176 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier'])
177 ret = {
178 'supplier_address' : address_name,
179 'address_display' : address_text,
180 'contact_person' : contact_name,
181 'contact_display' : contact_text,
182 'contact_email' : contact_email,
183 'contact_mobile' : contact_mobile
184 }
185 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530186 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530187
188 # Get Supplier Address
189 # -----------------------
190 def get_supplier_address(self, args):
191 args = load_json(args)
192 address_text, address_name = self.get_address_text(address_name=args['address'])
193 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
194 ret = {
195 'supplier_address' : address_name,
196 'address_display' : address_text,
197 'contact_person' : contact_name,
198 'contact_display' : contact_text,
199 'contact_email' : contact_email,
200 'contact_mobile' : contact_mobile
201 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530202 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530203
204 # Get Supplier Details
205 # -----------------------
206 def get_supplier_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530207 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 +0530208 ret = {
209 'supplier_name' : supplier_details and supplier_details[0]['supplier_name'] or ''
210 }
211 return ret
212
213 # Get Sales Person Details of Customer
214 # ------------------------------------
215 def get_sales_person(self, name):
216 self.doc.clear_table(self.doclist,'sales_team')
217 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530218 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 +0530219 ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist)
220 ch.sales_person = d and cstr(d[0]) or ''
221 ch.allocated_percentage = d and flt(d[1]) or 0
222 ch.allocated_amount = d and flt(d[2]) or 0
223 ch.incentives = d and flt(d[3]) or 0
224 ch.idx = idx
225 idx += 1
Ravi Dey5708da52011-06-28 19:01:02 +0530226
227 # Get Company Specific Default Currency
228 # -------------------------------------
229 def get_company_currency(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530230 ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name))
Ravi Dey5708da52011-06-28 19:01:02 +0530231 dcc = ret and ret[0][0] or get_defaults()['currency']
232 return dcc