Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 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 |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 5 | import webnotes, json |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 6 | from webnotes import msgprint, _ |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 7 | from webnotes.utils import 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 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 10 | from erpnext.controllers.status_updater import StatusUpdater |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 11 | |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 12 | |
| 13 | class TransactionBase(StatusUpdater): |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 14 | def set_address_fields(self): |
| 15 | party_type, party_name = self.get_party_type_and_name() |
| 16 | |
| 17 | if party_type in ("Customer", "Lead"): |
| 18 | if self.doc.customer_address: |
| 19 | self.doc.address_display = get_address_display(self.doc.customer_address) |
| 20 | |
| 21 | if self.doc.shipping_address_name: |
| 22 | self.doc.shipping_address = get_address_display(self.doc.shipping_address_name) |
| 23 | |
| 24 | elif self.doc.supplier_address: |
| 25 | self.doc.address_display = get_address_display(self.doc.supplier_address) |
| 26 | |
| 27 | def set_contact_fields(self): |
| 28 | party_type, party_name = self.get_party_type_and_name() |
| 29 | |
| 30 | if party_type == "Lead": |
| 31 | contact_dict = map_lead_contact_details(party_name) |
| 32 | else: |
| 33 | contact_dict = map_party_contact_details(self.doc.contact_person, party_type, party_name) |
| 34 | |
| 35 | for fieldname, value in contact_dict.items(): |
| 36 | if self.meta.get_field(fieldname): |
| 37 | self.doc.fields[fieldname] = value |
| 38 | |
| 39 | def get_party_type_and_name(self): |
| 40 | if not hasattr(self, "_party_type_and_name"): |
| 41 | for party_type in ("Lead", "Customer", "Supplier"): |
| 42 | party_field = party_type.lower() |
| 43 | if self.meta.get_field(party_field) and self.doc.fields.get(party_field): |
| 44 | self._party_type_and_name = (party_type, self.doc.fields.get(party_field)) |
| 45 | break |
| 46 | |
| 47 | return self._party_type_and_name |
Anand Doshi | f2045fb | 2013-07-25 17:46:02 +0530 | [diff] [blame] | 48 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 49 | def get_lead_defaults(self): |
| 50 | out = self.get_default_address_and_contact("lead") |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 51 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 52 | lead = webnotes.conn.get_value("Lead", self.doc.lead, |
| 53 | ["territory", "company_name", "lead_name"], as_dict=True) or {} |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 54 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 55 | out["territory"] = lead.get("territory") |
| 56 | out["customer_name"] = lead.get("company_name") or lead.get("lead_name") |
| 57 | |
| 58 | return out |
| 59 | |
| 60 | def set_lead_defaults(self): |
| 61 | self.doc.fields.update(self.get_lead_defaults()) |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 62 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 63 | # TODO deprecate this - used only in sales_order.js |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 64 | def get_shipping_address(self, name): |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 65 | shipping_address = get_default_address("customer", name, is_shipping_address=True) |
| 66 | return { |
| 67 | 'shipping_address_name' : shipping_address, |
| 68 | 'shipping_address' : get_address_display(shipping_address) if shipping_address else None |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 69 | } |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 70 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 71 | # Get Supplier Default Primary Address - first load |
| 72 | # ----------------------- |
| 73 | def get_default_supplier_address(self, args): |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 74 | if isinstance(args, basestring): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 75 | args = json.loads(args) |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 76 | |
| 77 | address_name = get_default_address("supplier", args["supplier"]) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 78 | ret = { |
| 79 | 'supplier_address' : address_name, |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 80 | 'address_display' : get_address_display(address_name), |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 81 | } |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 82 | ret.update(map_party_contact_details(None, "supplier", args["supplier"])) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 83 | ret.update(self.get_supplier_details(args['supplier'])) |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 84 | return ret |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 85 | |
| 86 | # Get Supplier Address |
| 87 | # ----------------------- |
| 88 | def get_supplier_address(self, args): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 89 | args = json.loads(args) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 90 | ret = { |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 91 | 'supplier_address' : args['address'], |
| 92 | 'address_display' : get_address_display(args["address"]), |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 93 | } |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 94 | ret.update(map_party_contact_details(contact_name=args['contact'])) |
Nabin Hait | 06c4de8 | 2011-08-16 16:38:11 +0530 | [diff] [blame] | 95 | return ret |
Nabin Hait | deff578 | 2013-07-24 11:30:35 +0530 | [diff] [blame] | 96 | |
| 97 | def set_supplier_address(self, args): |
| 98 | self.doc.fields.update(self.get_supplier_address(args)) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 99 | |
| 100 | # Get Supplier Details |
| 101 | # ----------------------- |
Anand Doshi | f2f5c94 | 2012-07-18 20:13:52 +0530 | [diff] [blame] | 102 | def get_supplier_details(self, name): |
| 103 | supplier_details = webnotes.conn.sql("""\ |
| 104 | select supplier_name, default_currency |
| 105 | from `tabSupplier` |
| 106 | where name = %s and docstatus < 2""", name, as_dict=1) |
| 107 | if supplier_details: |
| 108 | return { |
| 109 | 'supplier_name': (supplier_details[0]['supplier_name'] |
| 110 | or self.doc.fields.get('supplier_name')), |
| 111 | 'currency': (supplier_details[0]['default_currency'] |
| 112 | or self.doc.fields.get('currency')), |
| 113 | } |
| 114 | else: |
| 115 | return {} |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 116 | |
| 117 | # Get Sales Person Details of Customer |
| 118 | # ------------------------------------ |
| 119 | def get_sales_person(self, name): |
Anand Doshi | 8ae5ba9 | 2012-06-25 20:05:35 +0530 | [diff] [blame] | 120 | self.doclist = self.doc.clear_table(self.doclist,'sales_team') |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 121 | idx = 0 |
Anand Doshi | 74d1b65 | 2012-01-27 12:25:09 +0530 | [diff] [blame] | 122 | 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] | 123 | ch = addchild(self.doc, 'sales_team', 'Sales Team', self.doclist) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 124 | ch.sales_person = d and cstr(d[0]) or '' |
| 125 | ch.allocated_percentage = d and flt(d[1]) or 0 |
| 126 | ch.allocated_amount = d and flt(d[2]) or 0 |
| 127 | ch.incentives = d and flt(d[3]) or 0 |
| 128 | ch.idx = idx |
| 129 | idx += 1 |
Nabin Hait | 41cc327 | 2012-04-30 14:36:18 +0530 | [diff] [blame] | 130 | |
Rushabh Mehta | 35c017a | 2012-11-30 10:57:28 +0530 | [diff] [blame] | 131 | def load_notification_message(self): |
| 132 | dt = self.doc.doctype.lower().replace(" ", "_") |
| 133 | if int(webnotes.conn.get_value("Notification Control", None, dt) or 0): |
| 134 | self.doc.fields["__notification_message"] = \ |
| 135 | webnotes.conn.get_value("Notification Control", None, dt + "_message") |
Rushabh Mehta | 96690eb | 2013-09-02 17:04:27 +0530 | [diff] [blame] | 136 | |
Anand Doshi | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 137 | def validate_posting_time(self): |
| 138 | if not self.doc.posting_time: |
Anand Doshi | acec022 | 2013-03-26 12:33:43 +0530 | [diff] [blame] | 139 | self.doc.posting_time = now_datetime().strftime('%H:%M:%S') |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 140 | |
Anand Doshi | 670199b | 2013-06-10 15:38:01 +0530 | [diff] [blame] | 141 | def add_calendar_event(self, opts, force=False): |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 142 | if self.doc.contact_by != cstr(self._prev.contact_by) or \ |
Anand Doshi | 670199b | 2013-06-10 15:38:01 +0530 | [diff] [blame] | 143 | self.doc.contact_date != cstr(self._prev.contact_date) or force: |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 144 | |
| 145 | self.delete_events() |
| 146 | self._add_calendar_event(opts) |
| 147 | |
| 148 | def delete_events(self): |
| 149 | webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` |
Nabin Hait | ad6ce4e | 2013-09-19 11:02:24 +0530 | [diff] [blame] | 150 | where ref_type=%s and ref_name=%s""", (self.doc.doctype, self.doc.name)), |
| 151 | ignore_permissions=True) |
Anand Doshi | e53a81d | 2013-06-10 15:15:40 +0530 | [diff] [blame] | 152 | |
| 153 | def _add_calendar_event(self, opts): |
| 154 | opts = webnotes._dict(opts) |
| 155 | |
| 156 | if self.doc.contact_date: |
| 157 | event_doclist = [{ |
| 158 | "doctype": "Event", |
| 159 | "owner": opts.owner or self.doc.owner, |
| 160 | "subject": opts.subject, |
| 161 | "description": opts.description, |
| 162 | "starts_on": self.doc.contact_date + " 10:00:00", |
| 163 | "event_type": "Private", |
| 164 | "ref_type": self.doc.doctype, |
| 165 | "ref_name": self.doc.name |
| 166 | }] |
| 167 | |
| 168 | if webnotes.conn.exists("Profile", self.doc.contact_by): |
| 169 | event_doclist.append({ |
| 170 | "doctype": "Event User", |
| 171 | "parentfield": "event_individuals", |
| 172 | "person": self.doc.contact_by |
| 173 | }) |
| 174 | |
| 175 | webnotes.bean(event_doclist).insert() |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 176 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 177 | def validate_uom_is_integer(self, uom_field, qty_fields): |
| 178 | validate_uom_is_integer(self.doclist, uom_field, qty_fields) |
| 179 | |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 180 | def validate_with_previous_doc(self, source_dt, ref): |
| 181 | for key, val in ref.items(): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 182 | is_child = val.get("is_child_table") |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 183 | ref_doc = {} |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 184 | item_ref_dn = [] |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 185 | for d in self.doclist.get({"doctype": source_dt}): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 186 | ref_dn = d.fields.get(val["ref_dn_field"]) |
| 187 | if ref_dn: |
| 188 | if is_child: |
| 189 | self.compare_values({key: [ref_dn]}, val["compare_fields"], d) |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 190 | if ref_dn not in item_ref_dn: |
| 191 | item_ref_dn.append(ref_dn) |
Nabin Hait | 5e200e4 | 2013-07-29 15:29:51 +0530 | [diff] [blame] | 192 | elif not val.get("allow_duplicate_prev_row_id"): |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 193 | webnotes.msgprint(_("Row ") + cstr(d.idx + 1) + |
| 194 | _(": Duplicate row from same ") + key, raise_exception=1) |
| 195 | elif ref_dn: |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 196 | ref_doc.setdefault(key, []) |
| 197 | if ref_dn not in ref_doc[key]: |
| 198 | ref_doc[key].append(ref_dn) |
| 199 | if ref_doc: |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 200 | self.compare_values(ref_doc, val["compare_fields"]) |
| 201 | |
| 202 | def compare_values(self, ref_doc, fields, doc=None): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 203 | for ref_doctype, ref_dn_list in ref_doc.items(): |
| 204 | for ref_docname in ref_dn_list: |
| 205 | prevdoc_values = webnotes.conn.get_value(ref_doctype, ref_docname, |
| 206 | [d[0] for d in fields], as_dict=1) |
| 207 | |
| 208 | for field, condition in fields: |
Anand Doshi | 63d844b | 2013-07-10 20:55:15 +0530 | [diff] [blame] | 209 | if prevdoc_values[field] is not None: |
| 210 | self.validate_value(field, condition, prevdoc_values[field], doc) |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 211 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 212 | def get_default_address(party_field, party_name, is_shipping_address=False): |
| 213 | if is_shipping_address: |
| 214 | order_by = "is_shipping_address desc, is_primary_address desc, name asc" |
| 215 | else: |
| 216 | order_by = "is_primary_address desc, name asc" |
| 217 | |
| 218 | address = webnotes.conn.sql("""select name from `tabAddress` where `%s`=%s order by %s |
| 219 | limit 1""" % (party_field, "%s", order_by), party_name) |
| 220 | |
| 221 | return address[0][0] if address else None |
| 222 | |
| 223 | def get_default_contact(party_field, party_name): |
| 224 | contact = webnotes.conn.sql("""select name from `tabContact` where `%s`=%s |
| 225 | order by is_primary_contact desc, name asc limit 1""" % (party_field, "%s"), |
| 226 | (party_name,)) |
| 227 | |
| 228 | return contact[0][0] if contact else None |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 229 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 230 | def map_lead_contact_details(party_name): |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 231 | out = {} |
| 232 | for fieldname in ["contact_display", "contact_email", "contact_mobile", "contact_phone"]: |
| 233 | out[fieldname] = None |
| 234 | |
| 235 | lead = webnotes.conn.sql("""select * from `tabLead` where name=%s""", party_name, as_dict=True) |
| 236 | if lead: |
| 237 | lead = lead[0] |
| 238 | out.update({ |
| 239 | "contact_display": lead.get("lead_name"), |
| 240 | "contact_email": lead.get("email_id"), |
| 241 | "contact_mobile": lead.get("mobile_no"), |
| 242 | "contact_phone": lead.get("phone"), |
| 243 | }) |
| 244 | |
| 245 | return out |
| 246 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 247 | 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] | 248 | out = {} |
| 249 | for fieldname in ["contact_person", "contact_display", "contact_email", |
| 250 | "contact_mobile", "contact_phone", "contact_designation", "contact_department"]: |
| 251 | out[fieldname] = None |
Nabin Hait | deff578 | 2013-07-24 11:30:35 +0530 | [diff] [blame] | 252 | |
Anand Doshi | ba8ea7d | 2013-07-24 12:08:42 +0530 | [diff] [blame] | 253 | if not contact_name and party_field: |
| 254 | contact_name = get_default_contact(party_field, party_name) |
| 255 | |
Nabin Hait | 02a3d7a | 2013-07-24 16:33:30 +0530 | [diff] [blame] | 256 | if contact_name: |
| 257 | contact = webnotes.conn.sql("""select * from `tabContact` where name=%s""", |
| 258 | contact_name, as_dict=True) |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 259 | |
Nabin Hait | 02a3d7a | 2013-07-24 16:33:30 +0530 | [diff] [blame] | 260 | if contact: |
| 261 | contact = contact[0] |
| 262 | out.update({ |
| 263 | "contact_person": contact.get("name"), |
| 264 | "contact_display": " ".join(filter(None, |
| 265 | [contact.get("first_name"), contact.get("last_name")])), |
| 266 | "contact_email": contact.get("email_id"), |
| 267 | "contact_mobile": contact.get("mobile_no"), |
| 268 | "contact_phone": contact.get("phone"), |
| 269 | "contact_designation": contact.get("designation"), |
| 270 | "contact_department": contact.get("department") |
| 271 | }) |
Anand Doshi | ba8ea7d | 2013-07-24 12:08:42 +0530 | [diff] [blame] | 272 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 273 | return out |
| 274 | |
| 275 | def get_address_territory(address_doc): |
| 276 | territory = None |
| 277 | for fieldname in ("city", "state", "country"): |
| 278 | value = address_doc.fields.get(fieldname) |
| 279 | if value: |
| 280 | territory = webnotes.conn.get_value("Territory", value.strip()) |
| 281 | if territory: |
| 282 | break |
| 283 | |
| 284 | return territory |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 285 | |
| 286 | def validate_conversion_rate(currency, conversion_rate, conversion_rate_label, company): |
| 287 | """common validation for currency and price list currency""" |
Akhilesh Darjee | 10dce34 | 2013-09-25 11:09:33 +0530 | [diff] [blame] | 288 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 289 | company_currency = webnotes.conn.get_value("Company", company, "default_currency") |
Akhilesh Darjee | e0b3282 | 2013-11-12 19:27:16 +0530 | [diff] [blame] | 290 | |
Akhilesh Darjee | 1797cfe | 2013-10-11 13:34:10 +0530 | [diff] [blame] | 291 | if not conversion_rate: |
Rushabh Mehta | 2b08985 | 2013-12-19 18:27:48 +0530 | [diff] [blame] | 292 | msgprint(_('%(conversion_rate_label)s is mandatory. Maybe Currency Exchange record is not created for %(from_currency)s to %(to_currency)s') % { |
Akhilesh Darjee | e0b3282 | 2013-11-12 19:27:16 +0530 | [diff] [blame] | 293 | "conversion_rate_label": conversion_rate_label, |
| 294 | "from_currency": currency, |
| 295 | "to_currency": company_currency |
| 296 | }, raise_exception=True) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 297 | |
| 298 | def validate_item_fetch(args, item): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 299 | from erpnext.stock.utils import validate_end_of_life |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 300 | validate_end_of_life(item.name, item.end_of_life) |
| 301 | |
| 302 | # validate company |
| 303 | if not args.company: |
| 304 | msgprint(_("Please specify Company"), raise_exception=True) |
| 305 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 306 | def validate_currency(args, item, meta=None): |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 307 | from webnotes.model.meta import get_field_precision |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 308 | if not meta: |
| 309 | meta = webnotes.get_doctype(args.doctype) |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 310 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 311 | # validate conversion rate |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 312 | if meta.get_field("currency"): |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 313 | validate_conversion_rate(args.currency, args.conversion_rate, |
| 314 | meta.get_label("conversion_rate"), args.company) |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 315 | |
| 316 | # round it |
| 317 | args.conversion_rate = flt(args.conversion_rate, |
Anand Doshi | e961af4 | 2013-05-31 14:30:46 +0530 | [diff] [blame] | 318 | get_field_precision(meta.get_field("conversion_rate"), |
| 319 | webnotes._dict({"fields": args}))) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 320 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 321 | # validate price list conversion rate |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 322 | if meta.get_field("price_list_currency") and (args.selling_price_list or args.buying_price_list) \ |
| 323 | and args.price_list_currency: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 324 | validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 325 | meta.get_label("plc_conversion_rate"), args.company) |
| 326 | |
| 327 | # round it |
| 328 | args.plc_conversion_rate = flt(args.plc_conversion_rate, |
Anand Doshi | e961af4 | 2013-05-31 14:30:46 +0530 | [diff] [blame] | 329 | get_field_precision(meta.get_field("plc_conversion_rate"), |
| 330 | webnotes._dict({"fields": args}))) |
Anand Doshi | 097ac35 | 2013-06-10 15:38:31 +0530 | [diff] [blame] | 331 | |
Anand Doshi | 11d3113 | 2013-06-17 12:51:36 +0530 | [diff] [blame] | 332 | def delete_events(ref_type, ref_name): |
| 333 | webnotes.delete_doc("Event", webnotes.conn.sql_list("""select name from `tabEvent` |
Anand Doshi | b5fd788 | 2013-06-17 12:55:05 +0530 | [diff] [blame] | 334 | 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] | 335 | |
Rushabh Mehta | c4f5e4f | 2013-07-26 11:21:45 +0530 | [diff] [blame] | 336 | class UOMMustBeIntegerError(webnotes.ValidationError): pass |
| 337 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 338 | def validate_uom_is_integer(doclist, uom_field, qty_fields): |
| 339 | if isinstance(qty_fields, basestring): |
| 340 | qty_fields = [qty_fields] |
| 341 | |
| 342 | integer_uoms = filter(lambda uom: webnotes.conn.get_value("UOM", uom, |
| 343 | "must_be_whole_number") or None, doclist.get_distinct_values(uom_field)) |
| 344 | |
| 345 | if not integer_uoms: |
| 346 | return |
| 347 | |
| 348 | for d in doclist: |
| 349 | if d.fields.get(uom_field) in integer_uoms: |
| 350 | for f in qty_fields: |
| 351 | if d.fields.get(f): |
| 352 | if cint(d.fields[f])!=d.fields[f]: |
| 353 | webnotes.msgprint(_("For UOM") + " '" + d.fields[uom_field] \ |
| 354 | + "': " + _("Quantity cannot be a fraction.") \ |
| 355 | + " " + _("In Row") + ": " + str(d.idx), |
Rushabh Mehta | c4f5e4f | 2013-07-26 11:21:45 +0530 | [diff] [blame] | 356 | raise_exception=UOMMustBeIntegerError) |