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 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 17 | import webnotes |
Ravi Dey | 5708da5 | 2011-06-28 19:01:02 +0530 | [diff] [blame] | 18 | from webnotes.utils import load_json, cint, cstr, flt, get_defaults |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 19 | from webnotes.model.doc import Document, addchild, removechild, getchildren |
| 20 | from webnotes.model.doclist import getlist, copy_doclist |
| 21 | from webnotes import msgprint |
| 22 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 23 | class TransactionBase: |
| 24 | |
| 25 | # Get Customer Default Primary Address - first load |
| 26 | # ----------------------- |
| 27 | def get_default_customer_address(self, args=''): |
| 28 | address_text, address_name = self.get_address_text(customer=self.doc.customer) |
| 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 Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 38 | if args != 'onload': |
| 39 | self.get_sales_person(self.doc.customer) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 40 | |
| 41 | # Get Customer Default Shipping Address - first load |
| 42 | # ----------------------- |
| 43 | def get_default_customer_shipping_address(self, args=''): |
| 44 | address_text, address_name = self.get_address_text(customer=self.doc.customer,is_shipping_address=1) |
| 45 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(customer=self.doc.customer) |
| 46 | self.doc.customer_address = address_name or '' |
| 47 | self.doc.contact_person = contact_name or '' |
| 48 | self.doc.address_display = address_text or '' |
| 49 | self.doc.contact_display = contact_text or '' |
| 50 | self.doc.contact_email = contact_email or '' |
| 51 | self.doc.contact_mobile = contact_mobile or '' |
| 52 | |
| 53 | self.get_customer_details(self.doc.customer) |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 54 | if self.doc.doctype != 'Quotation' and args != 'onload': |
| 55 | self.get_sales_person(self.doc.customer) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 56 | |
| 57 | # Get Customer Address |
| 58 | # ----------------------- |
| 59 | def get_customer_address(self, args): |
| 60 | args = load_json(args) |
| 61 | address_text, address_name = self.get_address_text(address_name=args['address']) |
| 62 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact']) |
| 63 | ret = { |
| 64 | 'customer_address' : address_name, |
| 65 | 'contact_person' : contact_name, |
| 66 | 'address_display' : address_text, |
| 67 | 'contact_display' : contact_text, |
| 68 | 'contact_email' : contact_email, |
| 69 | 'contact_mobile' : contact_mobile |
| 70 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 71 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 72 | |
| 73 | # Get Address Text |
| 74 | # ----------------------- |
| 75 | def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None): |
| 76 | if customer: |
| 77 | cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name |
| 78 | elif supplier: |
| 79 | cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name |
| 80 | else: |
| 81 | cond = 'name="%s"' % address_name |
| 82 | |
| 83 | if is_shipping_address: |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 84 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 85 | else: |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 86 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 87 | |
| 88 | extract = lambda x: details and details[0] and details[0].get(x,'') or '' |
| 89 | address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','phone')] |
| 90 | address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])]) |
| 91 | if address_display.startswith('\n'): address_display = address_display[1:] |
| 92 | |
| 93 | address_name = details and details[0]['name'] or '' |
| 94 | return address_display, address_name |
| 95 | |
| 96 | # Get Contact Text |
| 97 | # ----------------------- |
| 98 | def get_contact_text(self, customer=None, contact_name=None, supplier=None): |
| 99 | if customer: |
| 100 | cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name |
| 101 | elif supplier: |
| 102 | cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name |
| 103 | else: |
| 104 | cond = 'name="%s"' % contact_name |
| 105 | |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 106 | 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] | 107 | |
| 108 | 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] | 109 | contact_fields = [('','first_name'),(' ','last_name')] |
| 110 | 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] | 111 | if contact_display.startswith('\n'): contact_display = contact_display[1:] |
| 112 | |
| 113 | contact_name = details and details[0]['name'] or '' |
| 114 | contact_email = details and details[0]['email_id'] or '' |
| 115 | contact_mobile = details and details[0]['mobile_no'] or '' |
| 116 | return contact_display, contact_name, contact_email, contact_mobile |
| 117 | |
| 118 | # Get Customer Details |
| 119 | # ----------------------- |
| 120 | def get_customer_details(self, name): |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 121 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 122 | if customer_details: |
| 123 | self.doc.customer_name = customer_details[0]['customer_name'] or '' |
| 124 | self.doc.customer_group = customer_details[0]['customer_group'] or '' |
| 125 | self.doc.territory = customer_details[0]['territory'] or '' |
| 126 | self.doc.sales_partner = customer_details[0]['default_sales_partner'] or '' |
| 127 | self.doc.commission_rate = customer_details[0]['default_commission_rate'] or '' |
| 128 | |
| 129 | # Get Customer Shipping Address |
| 130 | # ----------------------- |
| 131 | def get_shipping_address(self, name): |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 132 | 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] | 133 | |
| 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: ','phone')] |
| 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 | 'shipping_address_name' : details and details[0]['name'] or '', |
| 141 | 'shipping_address' : address_display |
| 142 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 143 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 144 | |
| 145 | # Get Lead Details |
| 146 | # ----------------------- |
| 147 | def get_lead_details(self, name): |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 148 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 149 | |
| 150 | extract = lambda x: details and details[0] and details[0].get(x,'') or '' |
| 151 | address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')] |
| 152 | address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])]) |
| 153 | if address_display.startswith('\n'): address_display = address_display[1:] |
| 154 | |
| 155 | ret = { |
| 156 | 'lead_name' : extract('lead_name'), |
| 157 | 'address_display' : address_display, |
| 158 | 'territory' : extract('territory'), |
| 159 | 'contact_mobile' : extract('mobile_no'), |
| 160 | 'contact_email' : extract('email_id') |
| 161 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 162 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 163 | |
| 164 | |
| 165 | # Get Supplier Default Primary Address - first load |
| 166 | # ----------------------- |
| 167 | def get_default_supplier_address(self, args): |
| 168 | args = load_json(args) |
| 169 | address_text, address_name = self.get_address_text(supplier=args['supplier']) |
| 170 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(supplier=args['supplier']) |
| 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 | } |
| 179 | ret.update(self.get_supplier_details(args['supplier'])) |
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 | # Get Supplier Address |
| 183 | # ----------------------- |
| 184 | def get_supplier_address(self, args): |
| 185 | args = load_json(args) |
| 186 | address_text, address_name = self.get_address_text(address_name=args['address']) |
| 187 | contact_text, contact_name, contact_email, contact_mobile = self.get_contact_text(contact_name=args['contact']) |
| 188 | ret = { |
| 189 | 'supplier_address' : address_name, |
| 190 | 'address_display' : address_text, |
| 191 | 'contact_person' : contact_name, |
| 192 | 'contact_display' : contact_text, |
| 193 | 'contact_email' : contact_email, |
| 194 | 'contact_mobile' : contact_mobile |
| 195 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 196 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 197 | |
| 198 | # Get Supplier Details |
| 199 | # ----------------------- |
| 200 | def get_supplier_details(self, name): |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 201 | supplier_details = webnotes.conn.sql("select supplier_name from tabSupplier where name = '%s' and docstatus != 2" %(name), as_dict = 1) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 202 | ret = { |
| 203 | 'supplier_name' : supplier_details and supplier_details[0]['supplier_name'] or '' |
| 204 | } |
| 205 | return ret |
| 206 | |
| 207 | # Get Sales Person Details of Customer |
| 208 | # ------------------------------------ |
| 209 | def get_sales_person(self, name): |
| 210 | self.doc.clear_table(self.doclist,'sales_team') |
| 211 | idx = 0 |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 212 | 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] | 213 | ch = addchild(self.doc, 'sales_team', 'Sales Team', 1, self.doclist) |
| 214 | ch.sales_person = d and cstr(d[0]) or '' |
| 215 | ch.allocated_percentage = d and flt(d[1]) or 0 |
| 216 | ch.allocated_amount = d and flt(d[2]) or 0 |
| 217 | ch.incentives = d and flt(d[3]) or 0 |
| 218 | ch.idx = idx |
| 219 | idx += 1 |
Ravi Dey | 5708da5 | 2011-06-28 19:01:02 +0530 | [diff] [blame] | 220 | |
| 221 | # Get Company Specific Default Currency |
| 222 | # ------------------------------------- |
| 223 | def get_company_currency(self, name): |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 224 | 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] | 225 | dcc = ret and ret[0][0] or get_defaults()['currency'] |
| 226 | return dcc |