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