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