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