Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 1 | # 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 Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 17 | from __future__ import unicode_literals |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 18 | import webnotes |
Ravi Dey | 5708da5 | 2011-06-28 19:01:02 +0530 | [diff] [blame] | 19 | from webnotes.utils import load_json, cint, cstr, flt, get_defaults |
Rushabh Mehta | 0eb790a | 2012-05-11 15:53:37 +0530 | [diff] [blame] | 20 | from webnotes.model.doc import Document, addchild, getchildren |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 21 | from webnotes.model.doclist import getlist, copy_doclist |
Nabin Hait | 41cc327 | 2012-04-30 14:36:18 +0530 | [diff] [blame] | 22 | from webnotes.model.code import get_obj |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 23 | from webnotes import msgprint |
| 24 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 25 | class TransactionBase: |
| 26 | |
| 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) |
| 31 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer) |
| 32 | self.doc.customer_address = address_name or '' |
| 33 | self.doc.contact_person = contact_name or '' |
| 34 | self.doc.address_display = address_text or '' |
| 35 | self.doc.contact_display = contact_text or '' |
| 36 | self.doc.contact_email = contact_email or '' |
| 37 | self.doc.contact_mobile = contact_mobile or '' |
Nabin Hait | 239e790 | 2012-04-20 10:25:03 +0530 | [diff] [blame] | 38 | |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 39 | if args != 'onload': |
Nabin Hait | 239e790 | 2012-04-20 10:25:03 +0530 | [diff] [blame] | 40 | self.get_customer_details(self.doc.customer) |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 41 | self.get_sales_person(self.doc.customer) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 42 | |
| 43 | # Get Customer Default Shipping Address - first load |
| 44 | # ----------------------- |
| 45 | def get_default_customer_shipping_address(self, args=''): |
| 46 | address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1) |
| 47 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer) |
| 48 | self.doc.customer_address = address_name or '' |
| 49 | self.doc.contact_person = contact_name or '' |
| 50 | self.doc.address_display = address_text or '' |
| 51 | self.doc.contact_display = contact_text or '' |
| 52 | self.doc.contact_email = contact_email or '' |
| 53 | self.doc.contact_mobile = contact_mobile or '' |
| 54 | |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 55 | if self.doc.doctype != 'Quotation' and args != 'onload': |
Nabin Hait | 239e790 | 2012-04-20 10:25:03 +0530 | [diff] [blame] | 56 | self.get_customer_details(self.doc.customer) |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 57 | self.get_sales_person(self.doc.customer) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 58 | |
| 59 | # Get Customer Address |
| 60 | # ----------------------- |
| 61 | def get_customer_address(self, args): |
| 62 | args = load_json(args) |
| 63 | address_text, address_name = self.get_address_text(address_name=args['address']) |
| 64 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact']) |
| 65 | ret = { |
| 66 | 'customer_address' : address_name, |
| 67 | 'contact_person' : contact_name, |
| 68 | 'address_display' : address_text, |
| 69 | 'contact_display' : contact_text, |
| 70 | 'contact_email' : contact_email, |
| 71 | 'contact_mobile' : contact_mobile |
| 72 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 73 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 74 | |
| 75 | # Get Address Text |
| 76 | # ----------------------- |
| 77 | def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None): |
| 78 | if customer: |
| 79 | cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name |
| 80 | elif supplier: |
| 81 | cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name |
| 82 | else: |
| 83 | cond = 'name="%s"' % address_name |
| 84 | |
| 85 | if is_shipping_address: |
Nabin Hait | 6535365 | 2012-03-21 17:07:42 +0530 | [diff] [blame] | 86 | 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 limit 1" % cond, as_dict = 1) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 87 | else: |
Nabin Hait | 6535365 | 2012-03-21 17:07:42 +0530 | [diff] [blame] | 88 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 89 | |
| 90 | extract = lambda x: details and details[0] and details[0].get(x,'') or '' |
Nabin Hait | 6535365 | 2012-03-21 17:07:42 +0530 | [diff] [blame] | 91 | address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone'),('\nFax: ', 'fax')] |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 92 | address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])]) |
| 93 | if address_display.startswith('\n'): address_display = address_display[1:] |
| 94 | |
| 95 | address_name = details and details[0]['name'] or '' |
| 96 | return address_display, address_name |
| 97 | |
| 98 | # Get Contact Text |
| 99 | # ----------------------- |
| 100 | def get_contact_text(self, customer=None, contact_name=None, supplier=None): |
| 101 | if customer: |
| 102 | cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name |
| 103 | elif supplier: |
| 104 | cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name |
| 105 | else: |
| 106 | cond = 'name="%s"' % contact_name |
| 107 | |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 108 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 109 | |
| 110 | extract = lambda x: details and details[0] and details[0].get(x,'') or '' |
Anand Doshi | 97bd366 | 2012-01-17 14:22:09 +0530 | [diff] [blame] | 111 | contact_fields = [('','first_name'),(' ','last_name')] |
| 112 | contact_display = ''.join([a[0]+cstr(extract(a[1])) for a in contact_fields if extract(a[1])]) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 113 | if contact_display.startswith('\n'): contact_display = contact_display[1:] |
| 114 | |
| 115 | contact_name = details and details[0]['name'] or '' |
| 116 | contact_email = details and details[0]['email_id'] or '' |
| 117 | contact_mobile = details and details[0]['mobile_no'] or '' |
| 118 | return contact_display, contact_name, contact_email, contact_mobile |
| 119 | |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 120 | def get_customer_details(self, name): |
| 121 | """ |
| 122 | Get customer details like name, group, territory |
| 123 | and other such defaults |
| 124 | """ |
| 125 | customer_details = webnotes.conn.sql("""\ |
| 126 | select |
| 127 | customer_name, customer_group, territory, |
| 128 | default_sales_partner, default_commission_rate, default_currency, |
| 129 | default_price_list |
| 130 | from `tabCustomer` |
| 131 | where name = %s and docstatus < 2""", name, as_dict=1) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 132 | if customer_details: |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 133 | for f in ['customer_name', 'customer_group', 'territory']: |
| 134 | self.doc.fields[f] = customer_details[0][f] or self.doc.fields.get(f) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 135 | |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 136 | # fields prepended with default in Customer doctype |
| 137 | for f in ['sales_partner', 'commission_rate', 'currency']: |
| 138 | self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f) |
| 139 | |
| 140 | # optionally fetch default price list from Customer Group |
| 141 | self.doc.price_list_name = (customer_details[0]['default_price_list'] |
| 142 | or webnotes.conn.get_value('Customer Group', self.doc.customer_group, |
| 143 | 'default_price_list') |
| 144 | or self.doc.fields.get('price_list_name')) |
| 145 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 146 | # Get Customer Shipping Address |
| 147 | # ----------------------- |
| 148 | def get_shipping_address(self, name): |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 149 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 150 | |
| 151 | extract = lambda x: details and details[0] and details[0].get(x,'') or '' |
| 152 | address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')] |
| 153 | address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])]) |
| 154 | if address_display.startswith('\n'): address_display = address_display[1:] |
| 155 | |
| 156 | ret = { |
| 157 | 'shipping_address_name' : details and details[0]['name'] or '', |
| 158 | 'shipping_address' : address_display |
| 159 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 160 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 161 | |
| 162 | # Get Lead Details |
| 163 | # ----------------------- |
| 164 | def get_lead_details(self, name): |
Nabin Hait | 9ff9236 | 2012-03-20 16:44:15 +0530 | [diff] [blame] | 165 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 166 | |
| 167 | extract = lambda x: details and details[0] and details[0].get(x,'') or '' |
| 168 | address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')] |
| 169 | address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])]) |
| 170 | if address_display.startswith('\n'): address_display = address_display[1:] |
| 171 | |
| 172 | ret = { |
| 173 | 'lead_name' : extract('lead_name'), |
| 174 | 'address_display' : address_display, |
| 175 | 'territory' : extract('territory'), |
| 176 | 'contact_mobile' : extract('mobile_no'), |
Nabin Hait | 9ff9236 | 2012-03-20 16:44:15 +0530 | [diff] [blame] | 177 | 'contact_email' : extract('email_id'), |
| 178 | 'organization' : extract('company_name') |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 179 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 180 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 181 | |
| 182 | |
| 183 | # Get Supplier Default Primary Address - first load |
| 184 | # ----------------------- |
| 185 | def get_default_supplier_address(self, args): |
| 186 | args = load_json(args) |
| 187 | address_text, address_name = self.get_address_text(supplier=args['supplier']) |
| 188 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier']) |
| 189 | ret = { |
| 190 | 'supplier_address' : address_name, |
| 191 | 'address_display' : address_text, |
| 192 | 'contact_person' : contact_name, |
| 193 | 'contact_display' : contact_text, |
| 194 | 'contact_email' : contact_email, |
| 195 | 'contact_mobile' : contact_mobile |
| 196 | } |
| 197 | ret.update(self.get_supplier_details(args['supplier'])) |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 198 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 199 | |
| 200 | # Get Supplier Address |
| 201 | # ----------------------- |
| 202 | def get_supplier_address(self, args): |
| 203 | args = load_json(args) |
| 204 | address_text, address_name = self.get_address_text(address_name=args['address']) |
| 205 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact']) |
| 206 | ret = { |
| 207 | 'supplier_address' : address_name, |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 208 | 'address_display' : address_text, |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 209 | 'contact_person' : contact_name, |
| 210 | 'contact_display' : contact_text, |
| 211 | 'contact_email' : contact_email, |
| 212 | 'contact_mobile' : contact_mobile |
| 213 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 214 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 215 | |
| 216 | # Get Supplier Details |
| 217 | # ----------------------- |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 218 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 232 | |
| 233 | # Get Sales Person Details of Customer |
| 234 | # ------------------------------------ |
| 235 | def get_sales_person(self, name): |
Anand Doshi | 8ae5ba9 | 2012-06-25 20:05:35 +0530 | [diff] [blame] | 236 | self.doclist = self.doc.clear_table(self.doclist,'sales_team') |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 237 | idx = 0 |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 238 | for d in webnotes.conn.sql("select sales_person, allocated_percentage, allocated_amount, incentives from `tabSales Team` where parent = '%s'" % name): |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 239 | ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist) |
| 240 | 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 |
Ravi Dey | 5708da5 | 2011-06-28 19:01:02 +0530 | [diff] [blame] | 246 | |
| 247 | # Get Company Specific Default Currency |
| 248 | # ------------------------------------- |
| 249 | def get_company_currency(self, name): |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 250 | ret = webnotes.conn.sql("select default_currency from tabCompany where name = '%s'" %(name)) |
Ravi Dey | 5708da5 | 2011-06-28 19:01:02 +0530 | [diff] [blame] | 251 | dcc = ret and ret[0][0] or get_defaults()['currency'] |
| 252 | return dcc |
Nabin Hait | 41cc327 | 2012-04-30 14:36:18 +0530 | [diff] [blame] | 253 | |
| 254 | |
| 255 | def get_formatted_message(self, args): |
| 256 | """ get formatted message for auto notification""" |
| 257 | return get_obj('Notification Control').get_formatted_message(args) |