blob: 032b52faa63fda05bed61ca34c0cfa84ed1cf1b9 [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 ''
36
37 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053038 if args != 'onload':
39 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
53 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053054 if self.doc.doctype != 'Quotation' and args != 'onload':
55 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):
Anand Doshi74d1b652012-01-27 12:25:09 +0530121 customer_details = webnotes.conn.sql("select customer_name, customer_group, territory, default_sales_partner, default_commission_rate 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 ''
128
129 # Get Customer Shipping Address
130 # -----------------------
131 def get_shipping_address(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530132 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 +0530133
134 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
135 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
136 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
137 if address_display.startswith('\n'): address_display = address_display[1:]
138
139 ret = {
140 'shipping_address_name' : details and details[0]['name'] or '',
141 'shipping_address' : address_display
142 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530143 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530144
145 # Get Lead Details
146 # -----------------------
147 def get_lead_details(self, name):
Nabin Hait9ff92362012-03-20 16:44:15 +0530148 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 +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: ','contact_no')]
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 'lead_name' : extract('lead_name'),
157 'address_display' : address_display,
158 'territory' : extract('territory'),
159 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530160 'contact_email' : extract('email_id'),
161 'organization' : extract('company_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530162 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530163 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530164
165
166 # Get Supplier Default Primary Address - first load
167 # -----------------------
168 def get_default_supplier_address(self, args):
169 args = load_json(args)
170 address_text, address_name = self.get_address_text(supplier=args['supplier'])
171 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier'])
172 ret = {
173 'supplier_address' : address_name,
174 'address_display' : address_text,
175 'contact_person' : contact_name,
176 'contact_display' : contact_text,
177 'contact_email' : contact_email,
178 'contact_mobile' : contact_mobile
179 }
180 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530181 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530182
183 # Get Supplier Address
184 # -----------------------
185 def get_supplier_address(self, args):
186 args = load_json(args)
187 address_text, address_name = self.get_address_text(address_name=args['address'])
188 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
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 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530197 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530198
199 # Get Supplier Details
200 # -----------------------
201 def get_supplier_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530202 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 +0530203 ret = {
204 'supplier_name' : supplier_details and supplier_details[0]['supplier_name'] or ''
205 }
206 return ret
207
208 # Get Sales Person Details of Customer
209 # ------------------------------------
210 def get_sales_person(self, name):
211 self.doc.clear_table(self.doclist,'sales_team')
212 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530213 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 +0530214 ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist)
215 ch.sales_person = d and cstr(d[0]) or ''
216 ch.allocated_percentage = d and flt(d[1]) or 0
217 ch.allocated_amount = d and flt(d[2]) or 0
218 ch.incentives = d and flt(d[3]) or 0
219 ch.idx = idx
220 idx += 1
Ravi Dey5708da52011-06-28 19:01:02 +0530221
222 # Get Company Specific Default Currency
223 # -------------------------------------
224 def get_company_currency(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530225 ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name))
Ravi Dey5708da52011-06-28 19:01:02 +0530226 dcc = ret and ret[0][0] or get_defaults()['currency']
227 return dcc