blob: 79790361710f052559393e19a534f1b86360c3a3 [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)
22 self.get_sales_person(self.doc.customer)
23
24 # Get Customer Default Shipping Address - first load
25 # -----------------------
26 def get_default_customer_shipping_address(self, args=''):
27 address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1)
28 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer)
29 self.doc.customer_address = address_name or ''
30 self.doc.contact_person = contact_name or ''
31 self.doc.address_display = address_text or ''
32 self.doc.contact_display = contact_text or ''
33 self.doc.contact_email = contact_email or ''
34 self.doc.contact_mobile = contact_mobile or ''
35
36 self.get_customer_details(self.doc.customer)
37 if self.doc.doctype != 'Quotation':
38 self.get_sales_person(self.doc.customer)
39
40 # Get Customer Address
41 # -----------------------
42 def get_customer_address(self, args):
43 args = load_json(args)
44 address_text, address_name = self.get_address_text(address_name=args['address'])
45 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
46 ret = {
47 'customer_address' : address_name,
48 'contact_person' : contact_name,
49 'address_display' : address_text,
50 'contact_display' : contact_text,
51 'contact_email' : contact_email,
52 'contact_mobile' : contact_mobile
53 }
Nabin Hait06c4de82011-08-16 16:38:11 +053054 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053055
56 # Get Address Text
57 # -----------------------
58 def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None):
59 if customer:
60 cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name
61 elif supplier:
62 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name
63 else:
64 cond = 'name="%s"' % address_name
65
66 if is_shipping_address:
Anand Doshi74d1b652012-01-27 12:25:09 +053067 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 +053068 else:
Anand Doshi74d1b652012-01-27 12:25:09 +053069 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 +053070
71 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
72 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
73 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
74 if address_display.startswith('\n'): address_display = address_display[1:]
75
76 address_name = details and details[0]['name'] or ''
77 return address_display, address_name
78
79 # Get Contact Text
80 # -----------------------
81 def get_contact_text(self, customer=None, contact_name=None, supplier=None):
82 if customer:
83 cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name
84 elif supplier:
85 cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name
86 else:
87 cond = 'name="%s"' % contact_name
88
Anand Doshi74d1b652012-01-27 12:25:09 +053089 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 +053090
91 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
Anand Doshi97bd3662012-01-17 14:22:09 +053092 contact_fields = [('','first_name'),(' ','last_name')]
93 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 +053094 if contact_display.startswith('\n'): contact_display = contact_display[1:]
95
96 contact_name = details and details[0]['name'] or ''
97 contact_email = details and details[0]['email_id'] or ''
98 contact_mobile = details and details[0]['mobile_no'] or ''
99 return contact_display, contact_name, contact_email, contact_mobile
100
101 # Get Customer Details
102 # -----------------------
103 def get_customer_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530104 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 +0530105 if customer_details:
106 self.doc.customer_name = customer_details[0]['customer_name'] or ''
107 self.doc.customer_group = customer_details[0]['customer_group'] or ''
108 self.doc.territory = customer_details[0]['territory'] or ''
109 self.doc.sales_partner = customer_details[0]['default_sales_partner'] or ''
110 self.doc.commission_rate = customer_details[0]['default_commission_rate'] or ''
111
112 # Get Customer Shipping Address
113 # -----------------------
114 def get_shipping_address(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530115 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 +0530116
117 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
118 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')]
119 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
120 if address_display.startswith('\n'): address_display = address_display[1:]
121
122 ret = {
123 'shipping_address_name' : details and details[0]['name'] or '',
124 'shipping_address' : address_display
125 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530126 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530127
128 # Get Lead Details
129 # -----------------------
130 def get_lead_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530131 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 +0530132
133 extract = lambda x: details and details[0] and details[0].get(x,'') or ''
134 address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')]
135 address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])])
136 if address_display.startswith('\n'): address_display = address_display[1:]
137
138 ret = {
139 'lead_name' : extract('lead_name'),
140 'address_display' : address_display,
141 'territory' : extract('territory'),
142 'contact_mobile' : extract('mobile_no'),
143 'contact_email' : extract('email_id')
144 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530145 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530146
147
148 # Get Supplier Default Primary Address - first load
149 # -----------------------
150 def get_default_supplier_address(self, args):
151 args = load_json(args)
152 address_text, address_name = self.get_address_text(supplier=args['supplier'])
153 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier'])
154 ret = {
155 'supplier_address' : address_name,
156 'address_display' : address_text,
157 'contact_person' : contact_name,
158 'contact_display' : contact_text,
159 'contact_email' : contact_email,
160 'contact_mobile' : contact_mobile
161 }
162 ret.update(self.get_supplier_details(args['supplier']))
Nabin Hait06c4de82011-08-16 16:38:11 +0530163 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530164
165 # Get Supplier Address
166 # -----------------------
167 def get_supplier_address(self, args):
168 args = load_json(args)
169 address_text, address_name = self.get_address_text(address_name=args['address'])
170 contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact'])
171 ret = {
172 'supplier_address' : address_name,
173 'address_display' : address_text,
174 'contact_person' : contact_name,
175 'contact_display' : contact_text,
176 'contact_email' : contact_email,
177 'contact_mobile' : contact_mobile
178 }
Nabin Hait06c4de82011-08-16 16:38:11 +0530179 return ret
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530180
181 # Get Supplier Details
182 # -----------------------
183 def get_supplier_details(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530184 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 +0530185 ret = {
186 'supplier_name' : supplier_details and supplier_details[0]['supplier_name'] or ''
187 }
188 return ret
189
190 # Get Sales Person Details of Customer
191 # ------------------------------------
192 def get_sales_person(self, name):
193 self.doc.clear_table(self.doclist,'sales_team')
194 idx = 0
Anand Doshi74d1b652012-01-27 12:25:09 +0530195 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 +0530196 ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist)
197 ch.sales_person = d and cstr(d[0]) or ''
198 ch.allocated_percentage = d and flt(d[1]) or 0
199 ch.allocated_amount = d and flt(d[2]) or 0
200 ch.incentives = d and flt(d[3]) or 0
201 ch.idx = idx
202 idx += 1
Ravi Dey5708da52011-06-28 19:01:02 +0530203
204 # Get Company Specific Default Currency
205 # -------------------------------------
206 def get_company_currency(self, name):
Anand Doshi74d1b652012-01-27 12:25:09 +0530207 ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name))
Ravi Dey5708da52011-06-28 19:01:02 +0530208 dcc = ret and ret[0][0] or get_defaults()['currency']
209 return dcc