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