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 | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 19 | from webnotes.utils import load_json, cstr, flt, now_datetime |
Anand Doshi | 0fd99e7 | 2012-11-30 16:38:04 +0530 | [diff] [blame] | 20 | from webnotes.model.doc import addchild |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 21 | |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 22 | from webnotes.model.controller import DocListController |
| 23 | |
| 24 | class TransactionBase(DocListController): |
Anand Doshi | edc5f2e | 2013-04-26 17:21:49 +0530 | [diff] [blame] | 25 | def get_default_address_and_contact(self, party_type): |
| 26 | """get a dict of default field values of address and contact for a given party type |
| 27 | party_type can be one of: customer, supplier""" |
| 28 | ret = {} |
| 29 | |
| 30 | # {customer: self.doc.fields.get("customer")} |
| 31 | args = {party_type: self.doc.fields.get(party_type)} |
| 32 | |
| 33 | address_text, address_name = self.get_address_text(**args) |
| 34 | ret.update({ |
| 35 | # customer_address |
| 36 | (party_type + "_address"): address_name, |
| 37 | "address_display": address_text |
| 38 | }) |
| 39 | ret.update(self.get_contact_text(**args)) |
| 40 | return ret |
| 41 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 42 | # Get Customer Default Primary Address - first load |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 43 | def get_default_customer_address(self, args=''): |
| 44 | address_text, address_name = self.get_address_text(customer=self.doc.customer) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 45 | self.doc.customer_address = address_name or '' |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 46 | self.doc.address_display = address_text or '' |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 47 | self.doc.fields.update(self.get_contact_text(customer=self.doc.customer)) |
Nabin Hait | 239e790 | 2012-04-20 10:25:03 +0530 | [diff] [blame] | 48 | |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 49 | if args != 'onload': |
Nabin Hait | 239e790 | 2012-04-20 10:25:03 +0530 | [diff] [blame] | 50 | self.get_customer_details(self.doc.customer) |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 51 | self.get_sales_person(self.doc.customer) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 52 | |
| 53 | # Get Customer Default Shipping Address - first load |
| 54 | # ----------------------- |
| 55 | def get_default_customer_shipping_address(self, args=''): |
| 56 | 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] | 57 | self.doc.customer_address = address_name or '' |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 58 | self.doc.address_display = address_text or '' |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 59 | self.doc.fields.update(self.get_contact_text(customer=self.doc.customer)) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 60 | |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 61 | if self.doc.doctype != 'Quotation' and args != 'onload': |
Nabin Hait | 239e790 | 2012-04-20 10:25:03 +0530 | [diff] [blame] | 62 | self.get_customer_details(self.doc.customer) |
Nabin Hait | 84d7310 | 2012-02-14 15:13:21 +0530 | [diff] [blame] | 63 | self.get_sales_person(self.doc.customer) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 64 | |
| 65 | # Get Customer Address |
| 66 | # ----------------------- |
| 67 | def get_customer_address(self, args): |
| 68 | args = load_json(args) |
| 69 | address_text, address_name = self.get_address_text(address_name=args['address']) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 70 | ret = { |
| 71 | 'customer_address' : address_name, |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 72 | 'address_display' : address_text, |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 73 | } |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 74 | |
| 75 | ret.update(self.get_contact_text(contact_name=args['contact'])) |
| 76 | |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 77 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 78 | |
| 79 | # Get Address Text |
| 80 | # ----------------------- |
| 81 | def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None): |
| 82 | if customer: |
| 83 | cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name |
| 84 | elif supplier: |
| 85 | cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name |
| 86 | else: |
| 87 | cond = 'name="%s"' % address_name |
| 88 | |
| 89 | if is_shipping_address: |
Nabin Hait | 7f59913 | 2012-09-26 13:10:37 +0530 | [diff] [blame] | 90 | 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] | 91 | else: |
Nabin Hait | 6535365 | 2012-03-21 17:07:42 +0530 | [diff] [blame] | 92 | 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) |
Anand Doshi | edc5f2e | 2013-04-26 17:21:49 +0530 | [diff] [blame] | 93 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 94 | extract = lambda x: details and details[0] and details[0].get(x,'') or '' |
Anand Doshi | 3e157ec | 2013-03-01 17:55:37 +0530 | [diff] [blame] | 95 | address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),('\n','state'),(' ','pincode'),('\n','country'),('\nPhone: ','phone'),('\nFax: ', 'fax')] |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 96 | address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])]) |
| 97 | if address_display.startswith('\n'): address_display = address_display[1:] |
| 98 | |
| 99 | address_name = details and details[0]['name'] or '' |
| 100 | return address_display, address_name |
| 101 | |
| 102 | # Get Contact Text |
| 103 | # ----------------------- |
| 104 | def get_contact_text(self, customer=None, contact_name=None, supplier=None): |
| 105 | if customer: |
| 106 | cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name |
| 107 | elif supplier: |
| 108 | cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name |
| 109 | else: |
| 110 | cond = 'name="%s"' % contact_name |
| 111 | |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 112 | 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] | 113 | |
| 114 | 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] | 115 | contact_fields = [('','first_name'),(' ','last_name')] |
| 116 | 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] | 117 | if contact_display.startswith('\n'): contact_display = contact_display[1:] |
| 118 | |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 119 | return { |
| 120 | "contact_display": contact_display, |
| 121 | "contact_person": details and details[0]["name"] or "", |
| 122 | "contact_email": details and details[0]["email_id"] or "", |
| 123 | "contact_mobile": details and details[0]["mobile_no"] or "", |
| 124 | "contact_designation": details and details[0]["designation"] or "", |
| 125 | "contact_department": details and details[0]["department"] or "", |
| 126 | } |
| 127 | |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 128 | def get_customer_details(self, name): |
| 129 | """ |
| 130 | Get customer details like name, group, territory |
| 131 | and other such defaults |
| 132 | """ |
| 133 | customer_details = webnotes.conn.sql("""\ |
| 134 | select |
| 135 | customer_name, customer_group, territory, |
| 136 | default_sales_partner, default_commission_rate, default_currency, |
| 137 | default_price_list |
| 138 | from `tabCustomer` |
| 139 | where name = %s and docstatus < 2""", name, as_dict=1) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 140 | if customer_details: |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 141 | for f in ['customer_name', 'customer_group', 'territory']: |
| 142 | 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] | 143 | |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 144 | # fields prepended with default in Customer doctype |
| 145 | for f in ['sales_partner', 'commission_rate', 'currency']: |
| 146 | self.doc.fields[f] = customer_details[0]["default_%s" % f] or self.doc.fields.get(f) |
| 147 | |
| 148 | # optionally fetch default price list from Customer Group |
| 149 | self.doc.price_list_name = (customer_details[0]['default_price_list'] |
| 150 | or webnotes.conn.get_value('Customer Group', self.doc.customer_group, |
| 151 | 'default_price_list') |
| 152 | or self.doc.fields.get('price_list_name')) |
| 153 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 154 | # Get Customer Shipping Address |
| 155 | # ----------------------- |
| 156 | def get_shipping_address(self, name): |
Nabin Hait | 7f59913 | 2012-09-26 13:10:37 +0530 | [diff] [blame] | 157 | 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] | 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: ','phone')] |
| 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 | 'shipping_address_name' : details and details[0]['name'] or '', |
| 166 | 'shipping_address' : address_display |
| 167 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 168 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 169 | |
| 170 | # Get Lead Details |
| 171 | # ----------------------- |
| 172 | def get_lead_details(self, name): |
Anand Doshi | b7af3d0 | 2013-03-07 12:17:52 +0530 | [diff] [blame] | 173 | details = webnotes.conn.sql("select name, lead_name, address_line1, address_line2, city, country, state, pincode, territory, phone, 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] | 174 | |
| 175 | extract = lambda x: details and details[0] and details[0].get(x,'') or '' |
| 176 | address_fields = [('','address_line1'),('\n','address_line2'),('\n','city'),(' ','pincode'),('\n','state'),('\n','country'),('\nPhone: ','contact_no')] |
| 177 | address_display = ''.join([a[0]+extract(a[1]) for a in address_fields if extract(a[1])]) |
| 178 | if address_display.startswith('\n'): address_display = address_display[1:] |
| 179 | |
| 180 | ret = { |
Nabin Hait | 1e3fc88 | 2013-04-02 18:13:20 +0530 | [diff] [blame] | 181 | 'contact_display' : extract('lead_name'), |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 182 | 'address_display' : address_display, |
| 183 | 'territory' : extract('territory'), |
| 184 | 'contact_mobile' : extract('mobile_no'), |
Nabin Hait | 9ff9236 | 2012-03-20 16:44:15 +0530 | [diff] [blame] | 185 | 'contact_email' : extract('email_id'), |
Nabin Hait | 1e3fc88 | 2013-04-02 18:13:20 +0530 | [diff] [blame] | 186 | 'customer_name' : extract('company_name') or extract('lead_name') |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 187 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 188 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 189 | |
| 190 | |
| 191 | # Get Supplier Default Primary Address - first load |
| 192 | # ----------------------- |
| 193 | def get_default_supplier_address(self, args): |
| 194 | args = load_json(args) |
| 195 | address_text, address_name = self.get_address_text(supplier=args['supplier']) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 196 | ret = { |
| 197 | 'supplier_address' : address_name, |
| 198 | 'address_display' : address_text, |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 199 | } |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 200 | ret.update(self.get_contact_text(supplier=args['supplier'])) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 201 | ret.update(self.get_supplier_details(args['supplier'])) |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 202 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 203 | |
| 204 | # Get Supplier Address |
| 205 | # ----------------------- |
| 206 | def get_supplier_address(self, args): |
| 207 | args = load_json(args) |
| 208 | address_text, address_name = self.get_address_text(address_name=args['address']) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 209 | ret = { |
| 210 | 'supplier_address' : address_name, |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 211 | 'address_display' : address_text, |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 212 | } |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 213 | ret.update(self.get_contact_text(contact_name=args['contact'])) |
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): |
Anand Doshi | f5d90ab | 2012-12-24 17:02:38 +0530 | [diff] [blame] | 239 | ch = addchild(self.doc, 'sales_team', 'Sales Team', self.doclist) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 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 |
Nabin Hait | 41cc327 | 2012-04-30 14:36:18 +0530 | [diff] [blame] | 246 | |
Rushabh Mehta | 35c017a | 2012-11-30 10:57:28 +0530 | [diff] [blame] | 247 | def load_notification_message(self): |
| 248 | dt = self.doc.doctype.lower().replace(" ", "_") |
| 249 | if int(webnotes.conn.get_value("Notification Control", None, dt) or 0): |
| 250 | self.doc.fields["__notification_message"] = \ |
| 251 | webnotes.conn.get_value("Notification Control", None, dt + "_message") |
| 252 | |
Rushabh Mehta | c4e7b68 | 2012-11-26 18:18:10 +0530 | [diff] [blame] | 253 | def add_communication_list(self): |
| 254 | # remove communications if present |
| 255 | self.doclist = webnotes.doclist(self.doclist).get({ |
| 256 | "doctype": ["!=", "Communcation"]}) |
| 257 | |
| 258 | comm_list = webnotes.conn.sql("""select * from tabCommunication |
| 259 | where %s=%s order by modified desc limit 20""" \ |
| 260 | % (self.doc.doctype.replace(" ", "_").lower(), "%s"), |
| 261 | self.doc.name, as_dict=1) |
| 262 | |
| 263 | [d.update({"doctype":"Communication"}) for d in comm_list] |
| 264 | |
| 265 | self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \ |
Anand Doshi | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 266 | for d in comm_list])) |
| 267 | |
| 268 | def validate_posting_time(self): |
| 269 | if not self.doc.posting_time: |
Anand Doshi | acec022 | 2013-03-26 12:33:43 +0530 | [diff] [blame] | 270 | self.doc.posting_time = now_datetime().strftime('%H:%M:%S') |
| 271 | |