blob: 2dc8c6a2d7ebaca920e3f434ece0d632c8b2c49b [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 Doshiee3d5cc2013-03-13 12:58:54 +053019from webnotes.utils import load_json, cstr, flt, now_datetime
Anand Doshi0fd99e72012-11-30 16:38:04 +053020from webnotes.model.doc import addchild
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053021
Anand Doshi756dca72013-01-15 18:39:21 +053022from webnotes.model.controller import DocListController
23
24class TransactionBase(DocListController):
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053025 # Get Customer Default Primary Address - first load
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053026 def get_default_customer_address(self, args=''):
27 address_text, address_name = self.get_address_text(customer=self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053028 self.doc.customer_address = address_name or ''
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053029 self.doc.address_display = address_text or ''
Anand Doshiae30e012012-10-26 15:01:22 +053030 self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
Nabin Hait239e7902012-04-20 10:25:03 +053031
Nabin Hait84d73102012-02-14 15:13:21 +053032 if args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053033 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053034 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053035
36 # Get Customer Default Shipping Address - first load
37 # -----------------------
38 def get_default_customer_shipping_address(self, args=''):
39 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053040 self.doc.customer_address = address_name or ''
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053041 self.doc.address_display = address_text or ''
Anand Doshiae30e012012-10-26 15:01:22 +053042 self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053043
Nabin Hait84d73102012-02-14 15:13:21 +053044 if self.doc.doctype != 'Quotation' and args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053045 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053046 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053047
48 # Get Customer Address
49 # -----------------------
50 def get_customer_address(self, args):
51 args = load_json(args)
52 address_text, address_name = self.get_address_text(address_name=args['address'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053053 ret = {
54 'customer_address' : address_name,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053055 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053056 }
Anand Doshiae30e012012-10-26 15:01:22 +053057
58 ret.update(self.get_contact_text(contact_name=args['contact']))
59
Nabin Hait06c4de82011-08-16 16:38:11 +053060 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053061
62 # Get Address Text
63 # -----------------------
64 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
65 if customer:
66 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
67 elif supplier:
68 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
69 else:
70 cond = 'name="%s"' % address_name
71
72 if is_shipping_address:
Nabin Hait7f599132012-09-26 13:10:37 +053073 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 +053074 else:
Nabin Hait65353652012-03-21 17:07:42 +053075 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 +053076
77 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi3e157ec2013-03-01 17:55:37 +053078 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),('\n','state'),(' ','pincode'),('\n','country'),('\nPhone: ','phone'),('\nFax: ', 'fax')]
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053079 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
80 if address_display.startswith('\n'): address_display = address_display[1:]
81
82 address_name = details and details[0]['name'] or ''
83 return address_display, address_name
84
85 # Get Contact Text
86 # -----------------------
87 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
88 if customer:
89 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
90 elif supplier:
91 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
92 else:
93 cond = 'name="%s"' % contact_name
94
Anand Doshi74d1b652012-01-27 12:25:09 +053095 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 +053096
97 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +053098 contact_fields = [('','first_name'),(' ','last_name')]
99 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 +0530100 if contact_display.startswith('\n'): contact_display = contact_display[1:]
101
Anand Doshiae30e012012-10-26 15:01:22 +0530102 return {
103 "contact_display": contact_display,
104 "contact_person": details and details[0]["name"] or "",
105 "contact_email": details and details[0]["email_id"] or "",
106 "contact_mobile": details and details[0]["mobile_no"] or "",
107 "contact_designation": details and details[0]["designation"] or "",
108 "contact_department": details and details[0]["department"] or "",
109 }
110
Anand Doshif2f5c942012-07-18 20:13:52 +0530111 def get_customer_details(self, name):
112 """
113 Get customer details like name, group, territory
114 and other such defaults
115 """
116 customer_details = webnotes.conn.sql("""\
117 select
118 customer_name, customer_group, territory,
119 default_sales_partner, default_commission_rate, default_currency,
120 default_price_list
121 from `tabCustomer`
122 where name = %s and docstatus < 2""", name, as_dict=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530123 if customer_details:
Anand Doshif2f5c942012-07-18 20:13:52 +0530124 for f in ['customer_name', 'customer_group', 'territory']:
125 self.doc.fields[f] = customer_details[0][f] or self.doc.fields.get(f)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530126
Anand Doshif2f5c942012-07-18 20:13:52 +0530127 # fields prepended with default in Customer doctype
128 for f in ['sales_partner', 'commission_rate', 'currency']:
129 self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f)
130
131 # optionally fetch default price list from Customer Group
132 self.doc.price_list_name = (customer_details[0]['default_price_list']
133 or webnotes.conn.get_value('Customer Group', self.doc.customer_group,
134 'default_price_list')
135 or self.doc.fields.get('price_list_name'))
136
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530137 # Get Customer Shipping Address
138 # -----------------------
139 def get_shipping_address(self, name):
Nabin Hait7f599132012-09-26 13:10:37 +0530140 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 +0530141
142 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
143 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
144 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
145 if address_display.startswith('\n'): address_display = address_display[1:]
146
147 ret = {
148 'shipping_address_name' : details and details[0]['name'] or '',
149 'shipping_address' : address_display
150 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530151 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530152
153 # Get Lead Details
154 # -----------------------
155 def get_lead_details(self, name):
Anand Doshib7af3d02013-03-07 12:17:52 +0530156 details = webnotes.conn.sql("select name, lead_name, address_line1, address_line2, city, country, state, pincode, territory, phone, mobile_no, email_id, company_name from `tabLead` where name = '%s'" %(name), as_dict = 1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530157
158 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
159 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
160 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
161 if address_display.startswith('\n'): address_display = address_display[1:]
162
163 ret = {
164 'lead_name' : extract('lead_name'),
165 'address_display' : address_display,
166 'territory' : extract('territory'),
167 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530168 'contact_email' : extract('email_id'),
169 'organization' : extract('company_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530170 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530171 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530172
173
174 # Get Supplier Default Primary Address - first load
175 # -----------------------
176 def get_default_supplier_address(self, args):
177 args = load_json(args)
178 address_text, address_name = self.get_address_text(supplier=args['supplier'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530179 ret = {
180 'supplier_address' : address_name,
181 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530182 }
Anand Doshiae30e012012-10-26 15:01:22 +0530183 ret.update(self.get_contact_text(supplier=args['supplier']))
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530184 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530185 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530186
187 # Get Supplier Address
188 # -----------------------
189 def get_supplier_address(self, args):
190 args = load_json(args)
191 address_text, address_name = self.get_address_text(address_name=args['address'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530192 ret = {
193 'supplier_address' : address_name,
Anand Doshif2f5c942012-07-18 20:13:52 +0530194 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530195 }
Anand Doshiae30e012012-10-26 15:01:22 +0530196 ret.update(self.get_contact_text(contact_name=args['contact']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530197 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530198
199 # Get Supplier Details
200 # -----------------------
Anand Doshif2f5c942012-07-18 20:13:52 +0530201 def get_supplier_details(self, name):
202 supplier_details = webnotes.conn.sql("""\
203 select supplier_name, default_currency
204 from `tabSupplier`
205 where name = %s and docstatus < 2""", name, as_dict=1)
206 if supplier_details:
207 return {
208 'supplier_name': (supplier_details[0]['supplier_name']
209 or self.doc.fields.get('supplier_name')),
210 'currency': (supplier_details[0]['default_currency']
211 or self.doc.fields.get('currency')),
212 }
213 else:
214 return {}
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530215
216 # Get Sales Person Details of Customer
217 # ------------------------------------
218 def get_sales_person(self, name):
Anand Doshi8ae5ba92012-06-25 20:05:35 +0530219 self.doclist = self.doc.clear_table(self.doclist,'sales_team')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530220 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530221 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 +0530222 ch = addchild(self.doc, 'sales_team', 'Sales Team', self.doclist)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530223 ch.sales_person = d and cstr(d[0]) or ''
224 ch.allocated_percentage = d and flt(d[1]) or 0
225 ch.allocated_amount = d and flt(d[2]) or 0
226 ch.incentives = d and flt(d[3]) or 0
227 ch.idx = idx
228 idx += 1
Nabin Hait41cc3272012-04-30 14:36:18 +0530229
Rushabh Mehta35c017a2012-11-30 10:57:28 +0530230 def load_notification_message(self):
231 dt = self.doc.doctype.lower().replace(" ", "_")
232 if int(webnotes.conn.get_value("Notification Control", None, dt) or 0):
233 self.doc.fields["__notification_message"] = \
234 webnotes.conn.get_value("Notification Control", None, dt + "_message")
235
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530236 def add_communication_list(self):
237 # remove communications if present
238 self.doclist = webnotes.doclist(self.doclist).get({
239 "doctype": ["!=", "Communcation"]})
240
241 comm_list = webnotes.conn.sql("""select * from tabCommunication
242 where %s=%s order by modified desc limit 20""" \
243 % (self.doc.doctype.replace(" ", "_").lower(), "%s"),
244 self.doc.name, as_dict=1)
245
246 [d.update({"doctype":"Communication"}) for d in comm_list]
247
248 self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \
Anand Doshiee3d5cc2013-03-13 12:58:54 +0530249 for d in comm_list]))
250
251 def validate_posting_time(self):
252 if not self.doc.posting_time:
253 self.doc.posting_time = now_datetime().strftime('%H:%M:%S')