blob: 46094290e21196a9ada12b41705270d86fcfd697 [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
Nabin Hait0feebc12013-06-03 16:45:38 +053022from controllers.status_updater import StatusUpdater
Anand Doshi756dca72013-01-15 18:39:21 +053023
Nabin Hait0feebc12013-06-03 16:45:38 +053024class TransactionBase(StatusUpdater):
Anand Doshiedc5f2e2013-04-26 17:21:49 +053025 def get_default_address_and_contact(self, party_type):
26 """get a dict of default field values of address and contact for a given party type
27 party_type can be one of: customer, supplier"""
28 ret = {}
29
30 # {customer: self.doc.fields.get("customer")}
31 args = {party_type: self.doc.fields.get(party_type)}
32
33 address_text, address_name = self.get_address_text(**args)
34 ret.update({
35 # customer_address
36 (party_type + "_address"): address_name,
37 "address_display": address_text
38 })
39 ret.update(self.get_contact_text(**args))
40 return ret
41
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053042 # Get Customer Default Primary Address - first load
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053043 def get_default_customer_address(self, args=''):
44 address_text, address_name = self.get_address_text(customer=self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053045 self.doc.customer_address = address_name or ''
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053046 self.doc.address_display = address_text or ''
Anand Doshiae30e012012-10-26 15:01:22 +053047 self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
Nabin Hait239e7902012-04-20 10:25:03 +053048
Nabin Hait84d73102012-02-14 15:13:21 +053049 if args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053050 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053051 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053052
53 # Get Customer Default Shipping Address - first load
54 # -----------------------
55 def get_default_customer_shipping_address(self, args=''):
56 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053057 self.doc.customer_address = address_name or ''
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053058 self.doc.address_display = address_text or ''
Anand Doshiae30e012012-10-26 15:01:22 +053059 self.doc.fields.update(self.get_contact_text(customer=self.doc.customer))
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053060
Nabin Hait84d73102012-02-14 15:13:21 +053061 if self.doc.doctype != 'Quotation' and args != 'onload':
Nabin Hait239e7902012-04-20 10:25:03 +053062 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053063 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053064
65 # Get Customer Address
66 # -----------------------
67 def get_customer_address(self, args):
68 args = load_json(args)
69 address_text, address_name = self.get_address_text(address_name=args['address'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053070 ret = {
71 'customer_address' : address_name,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053072 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053073 }
Anand Doshiae30e012012-10-26 15:01:22 +053074
75 ret.update(self.get_contact_text(contact_name=args['contact']))
76
Nabin Hait06c4de82011-08-16 16:38:11 +053077 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053078
79 # Get Address Text
80 # -----------------------
81 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
82 if customer:
83 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
84 elif supplier:
85 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
86 else:
87 cond = 'name="%s"' % address_name
88
89 if is_shipping_address:
Nabin Hait7f599132012-09-26 13:10:37 +053090 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 +053091 else:
Nabin Hait65353652012-03-21 17:07:42 +053092 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)
Anand Doshiedc5f2e2013-04-26 17:21:49 +053093
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053094 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi3e157ec2013-03-01 17:55:37 +053095 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 +053096 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
97 if address_display.startswith('\n'): address_display = address_display[1:]
98
99 address_name = details and details[0]['name'] or ''
100 return address_display, address_name
101
102 # Get Contact Text
103 # -----------------------
104 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
105 if customer:
106 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
107 elif supplier:
108 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
109 else:
110 cond = 'name="%s"' % contact_name
111
Anand Doshi74d1b652012-01-27 12:25:09 +0530112 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 +0530113
114 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +0530115 contact_fields = [('','first_name'),(' ','last_name')]
116 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 +0530117 if contact_display.startswith('\n'): contact_display = contact_display[1:]
118
Anand Doshiae30e012012-10-26 15:01:22 +0530119 return {
120 "contact_display": contact_display,
121 "contact_person": details and details[0]["name"] or "",
122 "contact_email": details and details[0]["email_id"] or "",
123 "contact_mobile": details and details[0]["mobile_no"] or "",
124 "contact_designation": details and details[0]["designation"] or "",
125 "contact_department": details and details[0]["department"] or "",
126 }
127
Anand Doshif2f5c942012-07-18 20:13:52 +0530128 def get_customer_details(self, name):
129 """
130 Get customer details like name, group, territory
131 and other such defaults
132 """
133 customer_details = webnotes.conn.sql("""\
134 select
135 customer_name, customer_group, territory,
136 default_sales_partner, default_commission_rate, default_currency,
137 default_price_list
138 from `tabCustomer`
139 where name = %s and docstatus < 2""", name, as_dict=1)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530140 if customer_details:
Anand Doshif2f5c942012-07-18 20:13:52 +0530141 for f in ['customer_name', 'customer_group', 'territory']:
142 self.doc.fields[f] = customer_details[0][f] or self.doc.fields.get(f)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530143
Anand Doshif2f5c942012-07-18 20:13:52 +0530144 # fields prepended with default in Customer doctype
145 for f in ['sales_partner', 'commission_rate', 'currency']:
146 self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f)
147
148 # optionally fetch default price list from Customer Group
149 self.doc.price_list_name = (customer_details[0]['default_price_list']
150 or webnotes.conn.get_value('Customer Group', self.doc.customer_group,
151 'default_price_list')
152 or self.doc.fields.get('price_list_name'))
153
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530154 # Get Customer Shipping Address
155 # -----------------------
156 def get_shipping_address(self, name):
Nabin Hait7f599132012-09-26 13:10:37 +0530157 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 +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: ','phone')]
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 'shipping_address_name' : details and details[0]['name'] or '',
166 'shipping_address' : address_display
167 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530168 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530169
170 # Get Lead Details
171 # -----------------------
172 def get_lead_details(self, name):
Anand Doshib7af3d02013-03-07 12:17:52 +0530173 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 +0530174
175 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
176 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
177 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
178 if address_display.startswith('\n'): address_display = address_display[1:]
179
180 ret = {
Nabin Hait1e3fc882013-04-02 18:13:20 +0530181 'contact_display' : extract('lead_name'),
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530182 'address_display' : address_display,
183 'territory' : extract('territory'),
184 'contact_mobile' : extract('mobile_no'),
Nabin Hait9ff92362012-03-20 16:44:15 +0530185 'contact_email' : extract('email_id'),
Nabin Hait1e3fc882013-04-02 18:13:20 +0530186 'customer_name' : extract('company_name') or extract('lead_name')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530187 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530188 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530189
190
191 # Get Supplier Default Primary Address - first load
192 # -----------------------
193 def get_default_supplier_address(self, args):
194 args = load_json(args)
195 address_text, address_name = self.get_address_text(supplier=args['supplier'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530196 ret = {
197 'supplier_address' : address_name,
198 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530199 }
Anand Doshiae30e012012-10-26 15:01:22 +0530200 ret.update(self.get_contact_text(supplier=args['supplier']))
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530201 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530202 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530203
204 # Get Supplier Address
205 # -----------------------
206 def get_supplier_address(self, args):
207 args = load_json(args)
208 address_text, address_name = self.get_address_text(address_name=args['address'])
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530209 ret = {
210 'supplier_address' : address_name,
Anand Doshif2f5c942012-07-18 20:13:52 +0530211 'address_display' : address_text,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530212 }
Anand Doshiae30e012012-10-26 15:01:22 +0530213 ret.update(self.get_contact_text(contact_name=args['contact']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530214 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530215
216 # Get Supplier Details
217 # -----------------------
Anand Doshif2f5c942012-07-18 20:13:52 +0530218 def get_supplier_details(self, name):
219 supplier_details = webnotes.conn.sql("""\
220 select supplier_name, default_currency
221 from `tabSupplier`
222 where name = %s and docstatus < 2""", name, as_dict=1)
223 if supplier_details:
224 return {
225 'supplier_name': (supplier_details[0]['supplier_name']
226 or self.doc.fields.get('supplier_name')),
227 'currency': (supplier_details[0]['default_currency']
228 or self.doc.fields.get('currency')),
229 }
230 else:
231 return {}
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530232
233 # Get Sales Person Details of Customer
234 # ------------------------------------
235 def get_sales_person(self, name):
Anand Doshi8ae5ba92012-06-25 20:05:35 +0530236 self.doclist = self.doc.clear_table(self.doclist,'sales_team')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530237 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530238 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 +0530239 ch = addchild(self.doc, 'sales_team', 'Sales Team', self.doclist)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530240 ch.sales_person = d and cstr(d[0]) or ''
241 ch.allocated_percentage = d and flt(d[1]) or 0
242 ch.allocated_amount = d and flt(d[2]) or 0
243 ch.incentives = d and flt(d[3]) or 0
244 ch.idx = idx
245 idx += 1
Nabin Hait41cc3272012-04-30 14:36:18 +0530246
Rushabh Mehta35c017a2012-11-30 10:57:28 +0530247 def load_notification_message(self):
248 dt = self.doc.doctype.lower().replace(" ", "_")
249 if int(webnotes.conn.get_value("Notification Control", None, dt) or 0):
250 self.doc.fields["__notification_message"] = \
251 webnotes.conn.get_value("Notification Control", None, dt + "_message")
252
Rushabh Mehtac4e7b682012-11-26 18:18:10 +0530253 def add_communication_list(self):
254 # remove communications if present
255 self.doclist = webnotes.doclist(self.doclist).get({
256 "doctype": ["!=", "Communcation"]})
257
258 comm_list = webnotes.conn.sql("""select * from tabCommunication
259 where %s=%s order by modified desc limit 20""" \
260 % (self.doc.doctype.replace(" ", "_").lower(), "%s"),
261 self.doc.name, as_dict=1)
262
263 [d.update({"doctype":"Communication"}) for d in comm_list]
264
265 self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \
Anand Doshiee3d5cc2013-03-13 12:58:54 +0530266 for d in comm_list]))
267
268 def validate_posting_time(self):
269 if not self.doc.posting_time:
Anand Doshiacec0222013-03-26 12:33:43 +0530270 self.doc.posting_time = now_datetime().strftime('%H:%M:%S')
271