blob: 7cb7479cec725c7aa13c3933034fdf512d971318 [file] [log] [blame]
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05301import webnotes
Ravi Dey5708da52011-06-28 19:01:02 +05302from webnotes.utils import load_json, cint, cstr, flt, get_defaults
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05303from webnotes.model.doc import Document, addchild, removechild, getchildren
4from webnotes.model.doclist import getlist, copy_doclist
5from webnotes import msgprint
6
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05307class TransactionBase:
8
9 # Get Customer Default Primary Address - first load
10 # -----------------------
11 def get_default_customer_address(self, args=''):
12 address_text, address_name = self.get_address_text(customer=self.doc.customer)
13 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer)
14 self.doc.customer_address = address_name or ''
15 self.doc.contact_person = contact_name or ''
16 self.doc.address_display = address_text or ''
17 self.doc.contact_display = contact_text or ''
18 self.doc.contact_email = contact_email or ''
19 self.doc.contact_mobile = contact_mobile or ''
20
21 self.get_customer_details(self.doc.customer)
Nabin Hait84d73102012-02-14 15:13:21 +053022 if args != 'onload':
23 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053024
25 # Get Customer Default Shipping Address - first load
26 # -----------------------
27 def get_default_customer_shipping_address(self, args=''):
28 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
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 self.doc.doctype != 'Quotation' and args != 'onload':
39 self.get_sales_person(self.doc.customer)
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053040
41 # Get Customer Address
42 # -----------------------
43 def get_customer_address(self, args):
44 args = load_json(args)
45 address_text, address_name = self.get_address_text(address_name=args['address'])
46 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
47 ret = {
48 'customer_address' : address_name,
49 'contact_person' : contact_name,
50 'address_display' : address_text,
51 'contact_display' : contact_text,
52 'contact_email' : contact_email,
53 'contact_mobile' : contact_mobile
54 }
Nabin Hait06c4de82011-08-16 16:38:11 +053055 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053056
57 # Get Address Text
58 # -----------------------
59 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
60 if customer:
61 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
62 elif supplier:
63 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
64 else:
65 cond = 'name="%s"' % address_name
66
67 if is_shipping_address:
Anand Doshi74d1b652012-01-27 12:25:09 +053068 details = webnotes.conn.sql("select name, address_line1, address_line2, city, country, pincode, state, phone 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 +053069 else:
Anand Doshi74d1b652012-01-27 12:25:09 +053070 details = webnotes.conn.sql("select name, address_line1, address_line2, city, country, pincode, state, phone 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 +053071
72 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
73 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
74 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
75 if address_display.startswith('\n'): address_display = address_display[1:]
76
77 address_name = details and details[0]['name'] or ''
78 return address_display, address_name
79
80 # Get Contact Text
81 # -----------------------
82 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
83 if customer:
84 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
85 elif supplier:
86 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
87 else:
88 cond = 'name="%s"' % contact_name
89
Anand Doshi74d1b652012-01-27 12:25:09 +053090 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 +053091
92 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +053093 contact_fields = [('','first_name'),(' ','last_name')]
94 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 +053095 if contact_display.startswith('\n'): contact_display = contact_display[1:]
96
97 contact_name = details and details[0]['name'] or ''
98 contact_email = details and details[0]['email_id'] or ''
99 contact_mobile = details and details[0]['mobile_no'] or ''
100 return contact_display, contact_name, contact_email, contact_mobile
101
102 # Get Customer Details
103 # -----------------------
104 def get_customer_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530105 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 +0530106 if customer_details:
107 self.doc.customer_name = customer_details[0]['customer_name'] or ''
108 self.doc.customer_group = customer_details[0]['customer_group'] or ''
109 self.doc.territory = customer_details[0]['territory'] or ''
110 self.doc.sales_partner = customer_details[0]['default_sales_partner'] or ''
111 self.doc.commission_rate = customer_details[0]['default_commission_rate'] or ''
112
113 # Get Customer Shipping Address
114 # -----------------------
115 def get_shipping_address(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530116 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 +0530117
118 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
119 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
120 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
121 if address_display.startswith('\n'): address_display = address_display[1:]
122
123 ret = {
124 'shipping_address_name' : details and details[0]['name'] or '',
125 'shipping_address' : address_display
126 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530127 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530128
129 # Get Lead Details
130 # -----------------------
131 def get_lead_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530132 details = webnotes.conn.sql("select name, lead_name, address_line1, address_line2, city, country, state, pincode, territory, contact_no, mobile_no, email_id from `tabLead` where name = '%s'" %(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: ','contact_no')]
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 'lead_name' : extract('lead_name'),
141 'address_display' : address_display,
142 'territory' : extract('territory'),
143 'contact_mobile' : extract('mobile_no'),
144 'contact_email' : extract('email_id')
145 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530146 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530147
148
149 # Get Supplier Default Primary Address - first load
150 # -----------------------
151 def get_default_supplier_address(self, args):
152 args = load_json(args)
153 address_text, address_name = self.get_address_text(supplier=args['supplier'])
154 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier'])
155 ret = {
156 'supplier_address' : address_name,
157 'address_display' : address_text,
158 'contact_person' : contact_name,
159 'contact_display' : contact_text,
160 'contact_email' : contact_email,
161 'contact_mobile' : contact_mobile
162 }
163 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530164 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530165
166 # Get Supplier Address
167 # -----------------------
168 def get_supplier_address(self, args):
169 args = load_json(args)
170 address_text, address_name = self.get_address_text(address_name=args['address'])
171 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
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 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530180 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530181
182 # Get Supplier Details
183 # -----------------------
184 def get_supplier_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530185 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 +0530186 ret = {
187 'supplier_name' : supplier_details and supplier_details[0]['supplier_name'] or ''
188 }
189 return ret
190
191 # Get Sales Person Details of Customer
192 # ------------------------------------
193 def get_sales_person(self, name):
194 self.doc.clear_table(self.doclist,'sales_team')
195 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530196 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 +0530197 ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist)
198 ch.sales_person = d and cstr(d[0]) or ''
199 ch.allocated_percentage = d and flt(d[1]) or 0
200 ch.allocated_amount = d and flt(d[2]) or 0
201 ch.incentives = d and flt(d[3]) or 0
202 ch.idx = idx
203 idx += 1
Ravi Dey5708da52011-06-28 19:01:02 +0530204
205 # Get Company Specific Default Currency
206 # -------------------------------------
207 def get_company_currency(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530208 ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name))
Ravi Dey5708da52011-06-28 19:01:02 +0530209 dcc = ret and ret[0][0] or get_defaults()['currency']
210 return dcc