blob: 850204152c455173f01816a6ea53e7228389ef27 [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
Anand Doshi756dca72013-01-15 18:39:21 +053023from webnotes.model.controller import DocListController
24
25class TransactionBase(DocListController):
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053026
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)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053031 self.doc.customer_address = address_name or ''
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053032 self.doc.address_display = address_text or ''
Anand Doshiae30e012012-10-26 15:01:22 +053033 self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
Nabin Hait239e7902012-04-20 10:25:03 +053034
Nabin Hait84d73102012-02-14 15:13:21 +053035 if args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053036 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053037 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053038
39 # Get Customer Default Shipping Address - first load
40 # -----------------------
41 def get_default_customer_shipping_address(self, args=''):
42 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053043 self.doc.customer_address = address_name or ''
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053044 self.doc.address_display = address_text or ''
Anand Doshiae30e012012-10-26 15:01:22 +053045 self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053046
Nabin Hait84d73102012-02-14 15:13:21 +053047 if self.doc.doctype != 'Quotation' and args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053048 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053049 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053050
51 # Get Customer Address
52 # -----------------------
53 def get_customer_address(self, args):
54 args = load_json(args)
55 address_text, address_name = self.get_address_text(address_name=args['address'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053056 ret = {
57 'customer_address' : address_name,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053058 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053059 }
Anand Doshiae30e012012-10-26 15:01:22 +053060
61 ret.update(self.get_contact_text(contact_name=args['contact']))
62
Nabin Hait06c4de82011-08-16 16:38:11 +053063 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053064
65 # Get Address Text
66 # -----------------------
67 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
68 if customer:
69 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
70 elif supplier:
71 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
72 else:
73 cond = 'name="%s"' % address_name
74
75 if is_shipping_address:
Nabin Hait7f599132012-09-26 13:10:37 +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_shipping_address desc, is_primary_address desc limit 1" % cond, as_dict = 1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053077 else:
Nabin Hait65353652012-03-21 17:07:42 +053078 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 +053079
80 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Nabin Hait65353652012-03-21 17:07:42 +053081 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 +053082 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
83 if address_display.startswith('\n'): address_display = address_display[1:]
84
85 address_name = details and details[0]['name'] or ''
86 return address_display, address_name
87
88 # Get Contact Text
89 # -----------------------
90 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
91 if customer:
92 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
93 elif supplier:
94 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
95 else:
96 cond = 'name="%s"' % contact_name
97
Anand Doshi74d1b652012-01-27 12:25:09 +053098 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 +053099
100 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +0530101 contact_fields = [('','first_name'),(' ','last_name')]
102 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 +0530103 if contact_display.startswith('\n'): contact_display = contact_display[1:]
104
Anand Doshiae30e012012-10-26 15:01:22 +0530105 return {
106 "contact_display": contact_display,
107 "contact_person": details and details[0]["name"] or "",
108 "contact_email": details and details[0]["email_id"] or "",
109 "contact_mobile": details and details[0]["mobile_no"] or "",
110 "contact_designation": details and details[0]["designation"] or "",
111 "contact_department": details and details[0]["department"] or "",
112 }
113
Anand Doshif2f5c942012-07-18 20:13:52 +0530114 def get_customer_details(self, name):
115 """
116 Get customer details like name, group, territory
117 and other such defaults
118 """
119 customer_details = webnotes.conn.sql("""\
120 select
121 customer_name, customer_group, territory,
122 default_sales_partner, default_commission_rate, default_currency,
123 default_price_list
124 from `tabCustomer`
125 where name = %s and docstatus < 2""", name, as_dict=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530126 if customer_details:
Anand Doshif2f5c942012-07-18 20:13:52 +0530127 for f in ['customer_name', 'customer_group', 'territory']:
128 self.doc.fields[f] = customer_details[0][f] or self.doc.fields.get(f)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530129
Anand Doshif2f5c942012-07-18 20:13:52 +0530130 # fields prepended with default in Customer doctype
131 for f in ['sales_partner', 'commission_rate', 'currency']:
132 self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f)
133
134 # optionally fetch default price list from Customer Group
135 self.doc.price_list_name = (customer_details[0]['default_price_list']
136 or webnotes.conn.get_value('Customer Group', self.doc.customer_group,
137 'default_price_list')
138 or self.doc.fields.get('price_list_name'))
139
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530140 # Get Customer Shipping Address
141 # -----------------------
142 def get_shipping_address(self, name):
Nabin Hait7f599132012-09-26 13:10:37 +0530143 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 +0530144
145 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
146 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
147 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
148 if address_display.startswith('\n'): address_display = address_display[1:]
149
150 ret = {
151 'shipping_address_name' : details and details[0]['name'] or '',
152 'shipping_address' : address_display
153 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530154 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530155
156 # Get Lead Details
157 # -----------------------
158 def get_lead_details(self, name):
Nabin Hait9ff92362012-03-20 16:44:15 +0530159 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 +0530160
161 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
162 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
163 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
164 if address_display.startswith('\n'): address_display = address_display[1:]
165
166 ret = {
167 'lead_name' : extract('lead_name'),
168 'address_display' : address_display,
169 'territory' : extract('territory'),
170 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530171 'contact_email' : extract('email_id'),
172 'organization' : extract('company_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530173 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530174 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530175
176
177 # Get Supplier Default Primary Address - first load
178 # -----------------------
179 def get_default_supplier_address(self, args):
180 args = load_json(args)
181 address_text, address_name = self.get_address_text(supplier=args['supplier'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530182 ret = {
183 'supplier_address' : address_name,
184 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530185 }
Anand Doshiae30e012012-10-26 15:01:22 +0530186 ret.update(self.get_contact_text(supplier=args['supplier']))
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530187 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530188 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530189
190 # Get Supplier Address
191 # -----------------------
192 def get_supplier_address(self, args):
193 args = load_json(args)
194 address_text, address_name = self.get_address_text(address_name=args['address'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530195 ret = {
196 'supplier_address' : address_name,
Anand Doshif2f5c942012-07-18 20:13:52 +0530197 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530198 }
Anand Doshiae30e012012-10-26 15:01:22 +0530199 ret.update(self.get_contact_text(contact_name=args['contact']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530200 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530201
202 # Get Supplier Details
203 # -----------------------
Anand Doshif2f5c942012-07-18 20:13:52 +0530204 def get_supplier_details(self, name):
205 supplier_details = webnotes.conn.sql("""\
206 select supplier_name, default_currency
207 from `tabSupplier`
208 where name = %s and docstatus < 2""", name, as_dict=1)
209 if supplier_details:
210 return {
211 'supplier_name': (supplier_details[0]['supplier_name']
212 or self.doc.fields.get('supplier_name')),
213 'currency': (supplier_details[0]['default_currency']
214 or self.doc.fields.get('currency')),
215 }
216 else:
217 return {}
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530218
219 # Get Sales Person Details of Customer
220 # ------------------------------------
221 def get_sales_person(self, name):
Anand Doshi8ae5ba92012-06-25 20:05:35 +0530222 self.doclist = self.doc.clear_table(self.doclist,'sales_team')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530223 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530224 for d in webnotes.conn.sql("select sales_person, allocated_percentage, allocated_amount, incentives from `tabSales Team` where parent = '%s'" % name):
Anand Doshif5d90ab2012-12-24 17:02:38 +0530225 ch = addchild(self.doc, 'sales_team', 'Sales Team', self.doclist)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530226 ch.sales_person = d and cstr(d[0]) or ''
227 ch.allocated_percentage = d and flt(d[1]) or 0
228 ch.allocated_amount = d and flt(d[2]) or 0
229 ch.incentives = d and flt(d[3]) or 0
230 ch.idx = idx
231 idx += 1
Nabin Hait41cc3272012-04-30 14:36:18 +0530232
Rushabh Mehta35c017a2012-11-30 10:57:28 +0530233 def load_notification_message(self):
234 dt = self.doc.doctype.lower().replace(" ", "_")
235 if int(webnotes.conn.get_value("Notification Control", None, dt) or 0):
236 self.doc.fields["__notification_message"] = \
237 webnotes.conn.get_value("Notification Control", None, dt + "_message")
238
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530239 def add_communication_list(self):
240 # remove communications if present
241 self.doclist = webnotes.doclist(self.doclist).get({
242 "doctype": ["!=", "Communcation"]})
243
244 comm_list = webnotes.conn.sql("""select * from tabCommunication
245 where %s=%s order by modified desc limit 20""" \
246 % (self.doc.doctype.replace(" ", "_").lower(), "%s"),
247 self.doc.name, as_dict=1)
248
249 [d.update({"doctype":"Communication"}) for d in comm_list]
250
251 self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \
252 for d in comm_list]))