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 | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 19 | from webnotes import msgprint, _ |
Anand Doshi | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 20 | from webnotes.utils import load_json, cstr, flt, now_datetime |
Anand Doshi | 0fd99e7 | 2012-11-30 16:38:04 +0530 | [diff] [blame] | 21 | from webnotes.model.doc import addchild |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 22 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 23 | from controllers.status_updater import StatusUpdater |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 24 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 25 | class TransactionBase(StatusUpdater): |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 26 | def get_default_address_and_contact(self, party_field, party_name=None): |
Anand Doshi | edc5f2e | 2013-04-26 17:21:49 +0530 | [diff] [blame] | 27 | """get a dict of default field values of address and contact for a given party type |
| 28 | party_type can be one of: customer, supplier""" |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 29 | if not party_name: |
| 30 | party_name = self.doc.fields.get(party_field) |
Anand Doshi | edc5f2e | 2013-04-26 17:21:49 +0530 | [diff] [blame] | 31 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 32 | return get_default_address_and_contact(party_field, party_name, |
| 33 | fetch_shipping_address=True if self.meta.get_field("shipping_address_name") else False) |
| 34 | |
| 35 | def get_customer_defaults(self): |
| 36 | out = self.get_default_address_and_contact("customer") |
| 37 | |
| 38 | customer = webnotes.doc("Customer", self.doc.customer) |
| 39 | for f in ['customer_name', 'customer_group', 'territory']: |
| 40 | out[f] = customer.fields.get(f) |
Anand Doshi | edc5f2e | 2013-04-26 17:21:49 +0530 | [diff] [blame] | 41 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 42 | # fields prepended with default in Customer doctype |
| 43 | for f in ['sales_partner', 'commission_rate', 'currency', 'price_list']: |
| 44 | out[f] = customer.fields.get("default_" + f) |
| 45 | |
| 46 | return out |
| 47 | |
| 48 | def set_customer_defaults(self): |
| 49 | """ |
| 50 | For a customer: |
| 51 | 1. Sets default address and contact |
| 52 | 2. Sets values like Territory, Customer Group, etc. |
| 53 | 3. Clears existing Sales Team and fetches the one mentioned in Customer |
| 54 | """ |
| 55 | customer_defaults = self.get_customer_defaults() |
| 56 | |
| 57 | # hack! TODO - add shipping_address_field in Delivery Note |
| 58 | if self.doc.doctype == "Delivery Note": |
| 59 | customer_defaults["customer_address"] = customer_defaults["shipping_address_name"] |
| 60 | customer_defaults["address_display"] = customer_defaults["shipping_address"] |
| 61 | |
| 62 | customer_defaults["price_list"] = customer_defaults["price_list"] or \ |
| 63 | webnotes.conn.get_value("Customer Group", self.doc.customer_group, "default_price_list") or \ |
| 64 | self.doc.price_list |
| 65 | |
| 66 | self.doc.fields.update(customer_defaults) |
| 67 | |
| 68 | if self.meta.get_field("sales_team"): |
| 69 | self.set_sales_team_for_customer() |
| 70 | |
| 71 | def set_sales_team_for_customer(self): |
| 72 | from webnotes.model import default_fields |
| 73 | |
| 74 | # clear table |
| 75 | self.doclist = self.doc.clear_table(self.doclist, "sales_team") |
| 76 | |
| 77 | sales_team = webnotes.conn.sql("""select * from `tabSales Team` |
| 78 | where parenttype="Customer" and parent=%s""", self.doc.customer, as_dict=True) |
| 79 | for i, sales_person in enumerate(sales_team): |
| 80 | # remove default fields |
| 81 | for fieldname in default_fields: |
| 82 | if fieldname in sales_person: |
| 83 | del sales_person[fieldname] |
| 84 | |
| 85 | sales_person.update({ |
| 86 | "doctype": "Sales Team", |
| 87 | "parentfield": "sales_team", |
| 88 | "idx": i+1 |
| 89 | }) |
| 90 | |
| 91 | # add child |
| 92 | self.doclist.append(sales_person) |
Anand Doshi | edc5f2e | 2013-04-26 17:21:49 +0530 | [diff] [blame] | 93 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 94 | def get_lead_defaults(self): |
| 95 | out = self.get_default_address_and_contact("lead") |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 96 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 97 | lead = webnotes.conn.get_value("Lead", self.doc.lead, |
| 98 | ["territory", "company_name", "lead_name"], as_dict=True) or {} |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 99 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 100 | out["territory"] = lead.get("territory") |
| 101 | out["customer_name"] = lead.get("company_name") or lead.get("lead_name") |
| 102 | |
| 103 | return out |
| 104 | |
| 105 | def set_lead_defaults(self): |
| 106 | self.doc.fields.update(self.get_lead_defaults()) |
| 107 | |
| 108 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 109 | # Get Customer Address |
| 110 | # ----------------------- |
| 111 | def get_customer_address(self, args): |
| 112 | args = load_json(args) |
| 113 | address_text, address_name = self.get_address_text(address_name=args['address']) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 114 | ret = { |
| 115 | 'customer_address' : address_name, |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 116 | 'address_display' : address_text, |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 117 | } |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 118 | |
| 119 | ret.update(self.get_contact_text(contact_name=args['contact'])) |
| 120 | |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 121 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 122 | |
| 123 | # Get Address Text |
| 124 | # ----------------------- |
| 125 | def get_address_text(self, customer=None, address_name=None, supplier=None, is_shipping_address=None): |
| 126 | if customer: |
| 127 | cond = customer and 'customer="%s"' % customer or 'name="%s"' % address_name |
| 128 | elif supplier: |
| 129 | cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % address_name |
| 130 | else: |
| 131 | cond = 'name="%s"' % address_name |
| 132 | |
| 133 | if is_shipping_address: |
Nabin Hait | 7f59913 | 2012-09-26 13:10:37 +0530 | [diff] [blame] | 134 | 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] | 135 | else: |
Nabin Hait | 6535365 | 2012-03-21 17:07:42 +0530 | [diff] [blame] | 136 | 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 | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 137 | |
| 138 | address_display = "" |
| 139 | |
| 140 | if details: |
| 141 | address_display = get_address_display(details[0]) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 142 | |
| 143 | address_name = details and details[0]['name'] or '' |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 144 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 145 | return address_display, address_name |
| 146 | |
| 147 | # Get Contact Text |
| 148 | # ----------------------- |
| 149 | def get_contact_text(self, customer=None, contact_name=None, supplier=None): |
| 150 | if customer: |
| 151 | cond = customer and 'customer="%s"' % customer or 'name="%s"' % contact_name |
| 152 | elif supplier: |
| 153 | cond = supplier and 'supplier="%s"' % supplier or 'name="%s"' % contact_name |
| 154 | else: |
| 155 | cond = 'name="%s"' % contact_name |
| 156 | |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 157 | 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] | 158 | |
| 159 | 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] | 160 | contact_fields = [('','first_name'),(' ','last_name')] |
| 161 | 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] | 162 | if contact_display.startswith('\n'): contact_display = contact_display[1:] |
| 163 | |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 164 | return { |
| 165 | "contact_display": contact_display, |
| 166 | "contact_person": details and details[0]["name"] or "", |
| 167 | "contact_email": details and details[0]["email_id"] or "", |
| 168 | "contact_mobile": details and details[0]["mobile_no"] or "", |
| 169 | "contact_designation": details and details[0]["designation"] or "", |
| 170 | "contact_department": details and details[0]["department"] or "", |
| 171 | } |
| 172 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 173 | # TODO deprecate this - used only in sales_order.js |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 174 | def get_shipping_address(self, name): |
Nabin Hait | 7f59913 | 2012-09-26 13:10:37 +0530 | [diff] [blame] | 175 | 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] | 176 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 177 | address_display = "" |
| 178 | if details: |
| 179 | address_display = get_address_display(details[0]) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 180 | |
| 181 | ret = { |
| 182 | 'shipping_address_name' : details and details[0]['name'] or '', |
| 183 | 'shipping_address' : address_display |
| 184 | } |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 185 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 186 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 187 | # Get Supplier Default Primary Address - first load |
| 188 | # ----------------------- |
| 189 | def get_default_supplier_address(self, args): |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 190 | if isinstance(args, basestring): |
| 191 | args = load_json(args) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 192 | address_text, address_name = self.get_address_text(supplier=args['supplier']) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 193 | ret = { |
| 194 | 'supplier_address' : address_name, |
| 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(supplier=args['supplier'])) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 198 | ret.update(self.get_supplier_details(args['supplier'])) |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 199 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 200 | |
| 201 | # Get Supplier Address |
| 202 | # ----------------------- |
| 203 | def get_supplier_address(self, args): |
| 204 | args = load_json(args) |
| 205 | address_text, address_name = self.get_address_text(address_name=args['address']) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 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 | } |
Anand Doshi | ae30e01 | 2012-10-26 15:01:22 +0530 | [diff] [blame] | 210 | ret.update(self.get_contact_text(contact_name=args['contact'])) |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 211 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 212 | |
| 213 | # Get Supplier Details |
| 214 | # ----------------------- |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 215 | def get_supplier_details(self, name): |
| 216 | supplier_details = webnotes.conn.sql("""\ |
| 217 | select supplier_name, default_currency |
| 218 | from `tabSupplier` |
| 219 | where name = %s and docstatus < 2""", name, as_dict=1) |
| 220 | if supplier_details: |
| 221 | return { |
| 222 | 'supplier_name': (supplier_details[0]['supplier_name'] |
| 223 | or self.doc.fields.get('supplier_name')), |
| 224 | 'currency': (supplier_details[0]['default_currency'] |
| 225 | or self.doc.fields.get('currency')), |
| 226 | } |
| 227 | else: |
| 228 | return {} |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 229 | |
| 230 | # Get Sales Person Details of Customer |
| 231 | # ------------------------------------ |
| 232 | def get_sales_person(self, name): |
Anand Doshi | 8ae5ba9 | 2012-06-25 20:05:35 +0530 | [diff] [blame] | 233 | self.doclist = self.doc.clear_table(self.doclist,'sales_team') |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 234 | idx = 0 |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 235 | 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] | 236 | ch = addchild(self.doc, 'sales_team', 'Sales Team', self.doclist) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 237 | ch.sales_person = d and cstr(d[0]) or '' |
| 238 | ch.allocated_percentage = d and flt(d[1]) or 0 |
| 239 | ch.allocated_amount = d and flt(d[2]) or 0 |
| 240 | ch.incentives = d and flt(d[3]) or 0 |
| 241 | ch.idx = idx |
| 242 | idx += 1 |
Nabin Hait | 41cc327 | 2012-04-30 14:36:18 +0530 | [diff] [blame] | 243 | |
Rushabh Mehta | 35c017a | 2012-11-30 10:57:28 +0530 | [diff] [blame] | 244 | def load_notification_message(self): |
| 245 | dt = self.doc.doctype.lower().replace(" ", "_") |
| 246 | if int(webnotes.conn.get_value("Notification Control", None, dt) or 0): |
| 247 | self.doc.fields["__notification_message"] = \ |
| 248 | webnotes.conn.get_value("Notification Control", None, dt + "_message") |
| 249 | |
Rushabh Mehta | c4e7b68 | 2012-11-26 18:18:10 +0530 | [diff] [blame] | 250 | def add_communication_list(self): |
| 251 | # remove communications if present |
| 252 | self.doclist = webnotes.doclist(self.doclist).get({ |
| 253 | "doctype": ["!=", "Communcation"]}) |
| 254 | |
| 255 | comm_list = webnotes.conn.sql("""select * from tabCommunication |
| 256 | where %s=%s order by modified desc limit 20""" \ |
| 257 | % (self.doc.doctype.replace(" ", "_").lower(), "%s"), |
Anand Doshi | 060d924 | 2013-06-12 17:40:36 +0530 | [diff] [blame] | 258 | self.doc.name, as_dict=1, update={"doctype":"Communication"}) |
Rushabh Mehta | c4e7b68 | 2012-11-26 18:18:10 +0530 | [diff] [blame] | 259 | |
| 260 | self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \ |
Anand Doshi | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 261 | for d in comm_list])) |
| 262 | |
| 263 | def validate_posting_time(self): |
| 264 | if not self.doc.posting_time: |
Anand Doshi | acec022 | 2013-03-26 12:33:43 +0530 | [diff] [blame] | 265 | self.doc.posting_time = now_datetime().strftime('%H:%M:%S') |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 266 | |
Anand Doshi | 670199b | 2013-06-10 15:38:01 +0530 | [diff] [blame] | 267 | def add_calendar_event(self, opts, force=False): |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 268 | if self.doc.contact_by != cstr(self._prev.contact_by) or \ |
Anand Doshi | 670199b | 2013-06-10 15:38:01 +0530 | [diff] [blame] | 269 | self.doc.contact_date != cstr(self._prev.contact_date) or force: |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 270 | |
| 271 | self.delete_events() |
| 272 | self._add_calendar_event(opts) |
| 273 | |
| 274 | def delete_events(self): |
| 275 | webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` |
| 276 | where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name))) |
| 277 | |
| 278 | def _add_calendar_event(self, opts): |
| 279 | opts = webnotes._dict(opts) |
| 280 | |
| 281 | if self.doc.contact_date: |
| 282 | event_doclist = [{ |
| 283 | "doctype": "Event", |
| 284 | "owner": opts.owner or self.doc.owner, |
| 285 | "subject": opts.subject, |
| 286 | "description": opts.description, |
| 287 | "starts_on": self.doc.contact_date + " 10:00:00", |
| 288 | "event_type": "Private", |
| 289 | "ref_type": self.doc.doctype, |
| 290 | "ref_name": self.doc.name |
| 291 | }] |
| 292 | |
| 293 | if webnotes.conn.exists("Profile", self.doc.contact_by): |
| 294 | event_doclist.append({ |
| 295 | "doctype": "Event User", |
| 296 | "parentfield": "event_individuals", |
| 297 | "person": self.doc.contact_by |
| 298 | }) |
| 299 | |
| 300 | webnotes.bean(event_doclist).insert() |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 301 | |
| 302 | def validate_with_previous_doc(self, source_dt, ref): |
| 303 | for key, val in ref.items(): |
| 304 | ref_doc = {} |
| 305 | for d in self.doclist.get({"doctype": source_dt}): |
Nabin Hait | 253de85 | 2013-07-09 11:36:00 +0530 | [diff] [blame] | 306 | if d.fields.get(val["ref_dn_field"]): |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 307 | ref_doc.setdefault(key, d.fields[val["ref_dn_field"]]) |
| 308 | |
| 309 | if val.get("is_child_table"): |
| 310 | self.compare_values(ref_doc, val["compare_fields"], d) |
| 311 | else: |
| 312 | self.compare_values(ref_doc, val["compare_fields"]) |
| 313 | |
| 314 | def compare_values(self, ref_doc, fields, doc=None): |
| 315 | for ref_doctype, ref_docname in ref_doc.items(): |
| 316 | prevdoc_values = webnotes.conn.get_value(ref_doctype, ref_docname, |
| 317 | [d[0] for d in fields], as_dict=1) |
| 318 | |
| 319 | for field, condition in fields: |
| 320 | self.validate_value(field, condition, prevdoc_values[field], doc) |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 321 | |
| 322 | def get_default_address_and_contact(party_field, party_name, fetch_shipping_address=False): |
| 323 | out = {} |
| 324 | |
| 325 | # get addresses |
| 326 | billing_address = get_address_dict(party_field, party_name) |
| 327 | if billing_address: |
| 328 | out[party_field + "_address"] = billing_address["name"] |
| 329 | out["address_display"] = get_address_display(billing_address) |
| 330 | else: |
| 331 | out[party_field + "_address"] = out["address_display"] = None |
| 332 | |
| 333 | if fetch_shipping_address: |
| 334 | shipping_address = get_address_dict(party_field, party_name, is_shipping_address=True) |
| 335 | if shipping_address: |
| 336 | out["shipping_address_name"] = shipping_address["name"] |
| 337 | out["shipping_address"] = get_address_display(shipping_address) |
| 338 | else: |
| 339 | out["shipping_address_name"] = out["shipping_address"] = None |
| 340 | |
| 341 | # get contact |
| 342 | if party_field == "lead": |
| 343 | out["customer_address"] = out.get("lead_address") |
| 344 | out.update(map_lead_fields(party_name)) |
| 345 | else: |
| 346 | out.update(map_contact_fields(party_field, party_name)) |
| 347 | |
| 348 | return out |
| 349 | |
| 350 | def get_address_dict(party_field, party_name, is_shipping_address=None): |
| 351 | order_by = "is_shipping_address desc, is_primary_address desc, name asc" if \ |
| 352 | is_shipping_address else "is_primary_address desc, name asc" |
| 353 | |
| 354 | address = webnotes.conn.sql("""select * from `tabAddress` where `%s`=%s order by %s |
| 355 | limit 1""" % (party_field, "%s", order_by), party_name, as_dict=True, |
| 356 | update={"doctype": "Address"}) |
| 357 | |
| 358 | return address[0] if address else None |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 359 | |
| 360 | def get_address_display(address_dict): |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 361 | def _prepare_for_display(a_dict, sequence): |
| 362 | display = "" |
| 363 | for separator, fieldname in sequence: |
| 364 | if a_dict.get(fieldname): |
| 365 | display += separator + a_dict.get(fieldname) |
| 366 | |
| 367 | return display.strip() |
| 368 | |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 369 | meta = webnotes.get_doctype("Address") |
| 370 | address_sequence = (("", "address_line1"), ("\n", "address_line2"), ("\n", "city"), |
| 371 | ("\n", "state"), ("\n" + meta.get_label("pincode") + ": ", "pincode"), ("\n", "country"), |
| 372 | ("\n" + meta.get_label("phone") + ": ", "phone"), ("\n" + meta.get_label("fax") + ": ", "fax")) |
| 373 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 374 | return _prepare_for_display(address_dict, address_sequence) |
| 375 | |
| 376 | def map_lead_fields(party_name): |
| 377 | out = {} |
| 378 | for fieldname in ["contact_display", "contact_email", "contact_mobile", "contact_phone"]: |
| 379 | out[fieldname] = None |
| 380 | |
| 381 | lead = webnotes.conn.sql("""select * from `tabLead` where name=%s""", party_name, as_dict=True) |
| 382 | if lead: |
| 383 | lead = lead[0] |
| 384 | out.update({ |
| 385 | "contact_display": lead.get("lead_name"), |
| 386 | "contact_email": lead.get("email_id"), |
| 387 | "contact_mobile": lead.get("mobile_no"), |
| 388 | "contact_phone": lead.get("phone"), |
| 389 | }) |
| 390 | |
| 391 | return out |
| 392 | |
| 393 | def map_contact_fields(party_field, party_name): |
| 394 | out = {} |
| 395 | for fieldname in ["contact_person", "contact_display", "contact_email", |
| 396 | "contact_mobile", "contact_phone", "contact_designation", "contact_department"]: |
| 397 | out[fieldname] = None |
| 398 | |
| 399 | contact = webnotes.conn.sql("""select * from `tabContact` where `%s`=%s |
| 400 | order by is_primary_contact desc, name asc limit 1""" % (party_field, "%s"), |
| 401 | (party_name,), as_dict=True) |
| 402 | if contact: |
| 403 | contact = contact[0] |
| 404 | out.update({ |
| 405 | "contact_person": contact.get("name"), |
| 406 | "contact_display": " ".join(filter(None, |
| 407 | [contact.get("first_name"), contact.get("last_name")])), |
| 408 | "contact_email": contact.get("email_id"), |
| 409 | "contact_mobile": contact.get("mobile_no"), |
| 410 | "contact_phone": contact.get("phone"), |
| 411 | "contact_designation": contact.get("designation"), |
| 412 | "contact_department": contact.get("department") |
| 413 | }) |
| 414 | |
| 415 | return out |
| 416 | |
| 417 | def get_address_territory(address_doc): |
| 418 | territory = None |
| 419 | for fieldname in ("city", "state", "country"): |
| 420 | value = address_doc.fields.get(fieldname) |
| 421 | if value: |
| 422 | territory = webnotes.conn.get_value("Territory", value.strip()) |
| 423 | if territory: |
| 424 | break |
| 425 | |
| 426 | return territory |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 427 | |
| 428 | def validate_conversion_rate(currency, conversion_rate, conversion_rate_label, company): |
| 429 | """common validation for currency and price list currency""" |
| 430 | if conversion_rate == 0: |
| 431 | msgprint(conversion_rate_label + _(' cannot be 0'), raise_exception=True) |
| 432 | |
| 433 | company_currency = webnotes.conn.get_value("Company", company, "default_currency") |
| 434 | |
| 435 | # parenthesis for 'OR' are necessary as we want it to evaluate as |
| 436 | # mandatory valid condition and (1st optional valid condition |
| 437 | # or 2nd optional valid condition) |
| 438 | valid_conversion_rate = (conversion_rate and |
| 439 | ((currency == company_currency and conversion_rate == 1.00) |
| 440 | or (currency != company_currency and conversion_rate != 1.00))) |
| 441 | |
| 442 | if not valid_conversion_rate: |
| 443 | msgprint(_('Please enter valid ') + conversion_rate_label + (': ') |
| 444 | + ("1 %s = [?] %s" % (currency, company_currency)), |
| 445 | raise_exception=True) |
| 446 | |
| 447 | def validate_item_fetch(args, item): |
| 448 | from stock.utils import validate_end_of_life |
| 449 | validate_end_of_life(item.name, item.end_of_life) |
| 450 | |
| 451 | # validate company |
| 452 | if not args.company: |
| 453 | msgprint(_("Please specify Company"), raise_exception=True) |
| 454 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 455 | def validate_currency(args, item, meta=None): |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 456 | from webnotes.model.meta import get_field_precision |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 457 | if not meta: |
| 458 | meta = webnotes.get_doctype(args.doctype) |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 459 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 460 | # validate conversion rate |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 461 | if meta.get_field("currency"): |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 462 | validate_conversion_rate(args.currency, args.conversion_rate, |
| 463 | meta.get_label("conversion_rate"), args.company) |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 464 | |
| 465 | # round it |
| 466 | args.conversion_rate = flt(args.conversion_rate, |
Anand Doshi | e961af4 | 2013-05-31 14:30:46 +0530 | [diff] [blame] | 467 | get_field_precision(meta.get_field("conversion_rate"), |
| 468 | webnotes._dict({"fields": args}))) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 469 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 470 | # validate price list conversion rate |
| 471 | if meta.get_field("price_list_currency") and args.price_list_name and \ |
| 472 | args.price_list_currency: |
| 473 | validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 474 | meta.get_label("plc_conversion_rate"), args.company) |
| 475 | |
| 476 | # round it |
| 477 | args.plc_conversion_rate = flt(args.plc_conversion_rate, |
Anand Doshi | e961af4 | 2013-05-31 14:30:46 +0530 | [diff] [blame] | 478 | get_field_precision(meta.get_field("plc_conversion_rate"), |
| 479 | webnotes._dict({"fields": args}))) |
Anand Doshi | 097ac35 | 2013-06-10 15:38:31 +0530 | [diff] [blame] | 480 | |
Anand Doshi | 11d3113 | 2013-06-17 12:51:36 +0530 | [diff] [blame] | 481 | def delete_events(ref_type, ref_name): |
| 482 | webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` |
Anand Doshi | b5fd788 | 2013-06-17 12:55:05 +0530 | [diff] [blame] | 483 | where ref_type=%s and ref_name=%s""", (ref_type, ref_name)), for_reload=True) |