blob: 3f47237501ab21ce0dcb8f0db68359d417e40f01 [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
Anand Doshi0fd99e72012-11-30 16:38:04 +053019from webnotes.utils import load_json, cstr, flt, get_defaults
20from webnotes.model.doc import addchild
21from webnotes.model.wrapper import copy_doclist
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053022
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)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053029 self.doc.customer_address = address_name or ''
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053030 self.doc.address_display = address_text or ''
Anand Doshiae30e012012-10-26 15:01:22 +053031 self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
Nabin Hait239e7902012-04-20 10:25:03 +053032
Nabin Hait84d73102012-02-14 15:13:21 +053033 if args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053034 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053035 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053036
37 # Get Customer Default Shipping Address - first load
38 # -----------------------
39 def get_default_customer_shipping_address(self, args=''):
40 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053041 self.doc.customer_address = address_name or ''
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053042 self.doc.address_display = address_text or ''
Anand Doshiae30e012012-10-26 15:01:22 +053043 self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053044
Nabin Hait84d73102012-02-14 15:13:21 +053045 if self.doc.doctype != 'Quotation' and args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053046 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053047 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053048
49 # Get Customer Address
50 # -----------------------
51 def get_customer_address(self, args):
52 args = load_json(args)
53 address_text, address_name = self.get_address_text(address_name=args['address'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053054 ret = {
55 'customer_address' : address_name,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053056 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053057 }
Anand Doshiae30e012012-10-26 15:01:22 +053058
59 ret.update(self.get_contact_text(contact_name=args['contact']))
60
Nabin Hait06c4de82011-08-16 16:38:11 +053061 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053062
63 # Get Address Text
64 # -----------------------
65 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
66 if customer:
67 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
68 elif supplier:
69 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
70 else:
71 cond = 'name="%s"' % address_name
72
73 if is_shipping_address:
Nabin Hait7f599132012-09-26 13:10:37 +053074 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, is_primary_address desc limit 1" % cond, as_dict = 1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053075 else:
Nabin Hait65353652012-03-21 17:07:42 +053076 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 +053077
78 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Nabin Hait65353652012-03-21 17:07:42 +053079 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 +053080 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
81 if address_display.startswith('\n'): address_display = address_display[1:]
82
83 address_name = details and details[0]['name'] or ''
84 return address_display, address_name
85
86 # Get Contact Text
87 # -----------------------
88 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
89 if customer:
90 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
91 elif supplier:
92 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
93 else:
94 cond = 'name="%s"' % contact_name
95
Anand Doshi74d1b652012-01-27 12:25:09 +053096 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 +053097
98 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +053099 contact_fields = [('','first_name'),(' ','last_name')]
100 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 +0530101 if contact_display.startswith('\n'): contact_display = contact_display[1:]
102
Anand Doshiae30e012012-10-26 15:01:22 +0530103 return {
104 "contact_display": contact_display,
105 "contact_person": details and details[0]["name"] or "",
106 "contact_email": details and details[0]["email_id"] or "",
107 "contact_mobile": details and details[0]["mobile_no"] or "",
108 "contact_designation": details and details[0]["designation"] or "",
109 "contact_department": details and details[0]["department"] or "",
110 }
111
Anand Doshif2f5c942012-07-18 20:13:52 +0530112 def get_customer_details(self, name):
113 """
114 Get customer details like name, group, territory
115 and other such defaults
116 """
117 customer_details = webnotes.conn.sql("""\
118 select
119 customer_name, customer_group, territory,
120 default_sales_partner, default_commission_rate, default_currency,
121 default_price_list
122 from `tabCustomer`
123 where name = %s and docstatus < 2""", name, as_dict=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530124 if customer_details:
Anand Doshif2f5c942012-07-18 20:13:52 +0530125 for f in ['customer_name', 'customer_group', 'territory']:
126 self.doc.fields[f] = customer_details[0][f] or self.doc.fields.get(f)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530127
Anand Doshif2f5c942012-07-18 20:13:52 +0530128 # fields prepended with default in Customer doctype
129 for f in ['sales_partner', 'commission_rate', 'currency']:
130 self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f)
131
132 # optionally fetch default price list from Customer Group
133 self.doc.price_list_name = (customer_details[0]['default_price_list']
134 or webnotes.conn.get_value('Customer Group', self.doc.customer_group,
135 'default_price_list')
136 or self.doc.fields.get('price_list_name'))
137
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530138 # Get Customer Shipping Address
139 # -----------------------
140 def get_shipping_address(self, name):
Nabin Hait7f599132012-09-26 13:10:37 +0530141 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, is_primary_address desc limit 1" %(name), as_dict = 1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530142
143 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
144 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
145 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
146 if address_display.startswith('\n'): address_display = address_display[1:]
147
148 ret = {
149 'shipping_address_name' : details and details[0]['name'] or '',
150 'shipping_address' : address_display
151 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530152 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530153
154 # Get Lead Details
155 # -----------------------
156 def get_lead_details(self, name):
Nabin Hait9ff92362012-03-20 16:44:15 +0530157 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 +0530158
159 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
160 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
161 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
162 if address_display.startswith('\n'): address_display = address_display[1:]
163
164 ret = {
165 'lead_name' : extract('lead_name'),
166 'address_display' : address_display,
167 'territory' : extract('territory'),
168 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530169 'contact_email' : extract('email_id'),
170 'organization' : extract('company_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530171 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530172 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530173
174
175 # Get Supplier Default Primary Address - first load
176 # -----------------------
177 def get_default_supplier_address(self, args):
178 args = load_json(args)
179 address_text, address_name = self.get_address_text(supplier=args['supplier'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530180 ret = {
181 'supplier_address' : address_name,
182 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530183 }
Anand Doshiae30e012012-10-26 15:01:22 +0530184 ret.update(self.get_contact_text(supplier=args['supplier']))
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530185 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'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530193 ret = {
194 'supplier_address' : address_name,
Anand Doshif2f5c942012-07-18 20:13:52 +0530195 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530196 }
Anand Doshiae30e012012-10-26 15:01:22 +0530197 ret.update(self.get_contact_text(contact_name=args['contact']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530198 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530199
200 # Get Supplier Details
201 # -----------------------
Anand Doshif2f5c942012-07-18 20:13:52 +0530202 def get_supplier_details(self, name):
203 supplier_details = webnotes.conn.sql("""\
204 select supplier_name, default_currency
205 from `tabSupplier`
206 where name = %s and docstatus < 2""", name, as_dict=1)
207 if supplier_details:
208 return {
209 'supplier_name': (supplier_details[0]['supplier_name']
210 or self.doc.fields.get('supplier_name')),
211 'currency': (supplier_details[0]['default_currency']
212 or self.doc.fields.get('currency')),
213 }
214 else:
215 return {}
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530216
217 # Get Sales Person Details of Customer
218 # ------------------------------------
219 def get_sales_person(self, name):
Anand Doshi8ae5ba92012-06-25 20:05:35 +0530220 self.doclist = self.doc.clear_table(self.doclist,'sales_team')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530221 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530222 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 +0530223 ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist)
224 ch.sales_person = d and cstr(d[0]) or ''
225 ch.allocated_percentage = d and flt(d[1]) or 0
226 ch.allocated_amount = d and flt(d[2]) or 0
227 ch.incentives = d and flt(d[3]) or 0
228 ch.idx = idx
229 idx += 1
Ravi Dey5708da52011-06-28 19:01:02 +0530230
231 # Get Company Specific Default Currency
232 # -------------------------------------
233 def get_company_currency(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530234 ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name))
Ravi Dey5708da52011-06-28 19:01:02 +0530235 dcc = ret and ret[0][0] or get_defaults()['currency']
236 return dcc
Nabin Hait41cc3272012-04-30 14:36:18 +0530237
238
Rushabh Mehta35c017a2012-11-30 10:57:28 +0530239 def load_notification_message(self):
240 dt = self.doc.doctype.lower().replace(" ", "_")
241 if int(webnotes.conn.get_value("Notification Control", None, dt) or 0):
242 self.doc.fields["__notification_message"] = \
243 webnotes.conn.get_value("Notification Control", None, dt + "_message")
244
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530245 def add_communication_list(self):
246 # remove communications if present
247 self.doclist = webnotes.doclist(self.doclist).get({
248 "doctype": ["!=", "Communcation"]})
249
250 comm_list = webnotes.conn.sql("""select * from tabCommunication
251 where %s=%s order by modified desc limit 20""" \
252 % (self.doc.doctype.replace(" ", "_").lower(), "%s"),
253 self.doc.name, as_dict=1)
254
255 [d.update({"doctype":"Communication"}) for d in comm_list]
256
257 self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \
258 for d in comm_list]))