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 |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
| 6 | from webnotes.utils import flt, cstr |
| 7 | from webnotes import msgprint |
| 8 | |
| 9 | from webnotes.model.controller import DocListController |
| 10 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 11 | status_map = { |
| 12 | "Contact": [ |
| 13 | ["Replied", "communication_sent"], |
| 14 | ["Open", "communication_received"] |
| 15 | ], |
| 16 | "Job Applicant": [ |
| 17 | ["Replied", "communication_sent"], |
| 18 | ["Open", "communication_received"] |
| 19 | ], |
| 20 | "Lead": [ |
| 21 | ["Replied", "communication_sent"], |
| 22 | ["Converted", "has_customer"], |
| 23 | ["Opportunity", "has_opportunity"], |
| 24 | ["Open", "communication_received"], |
| 25 | ], |
| 26 | "Opportunity": [ |
| 27 | ["Draft", None], |
| 28 | ["Submitted", "eval:self.doc.docstatus==1"], |
| 29 | ["Lost", "eval:self.doc.status=='Lost'"], |
| 30 | ["Quotation", "has_quotation"], |
| 31 | ["Replied", "communication_sent"], |
| 32 | ["Cancelled", "eval:self.doc.docstatus==2"], |
| 33 | ["Open", "communication_received"], |
| 34 | ], |
| 35 | "Quotation": [ |
| 36 | ["Draft", None], |
| 37 | ["Submitted", "eval:self.doc.docstatus==1"], |
| 38 | ["Lost", "eval:self.doc.status=='Lost'"], |
| 39 | ["Ordered", "has_sales_order"], |
| 40 | ["Replied", "communication_sent"], |
| 41 | ["Cancelled", "eval:self.doc.docstatus==2"], |
| 42 | ["Open", "communication_received"], |
| 43 | ], |
| 44 | "Sales Order": [ |
| 45 | ["Draft", None], |
| 46 | ["Submitted", "eval:self.doc.docstatus==1"], |
| 47 | ["Stopped", "eval:self.doc.status=='Stopped'"], |
| 48 | ["Cancelled", "eval:self.doc.docstatus==2"], |
| 49 | ], |
| 50 | "Support Ticket": [ |
| 51 | ["Replied", "communication_sent"], |
| 52 | ["Open", "communication_received"] |
| 53 | ], |
| 54 | } |
| 55 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 56 | class StatusUpdater(DocListController): |
| 57 | """ |
| 58 | Updates the status of the calling records |
| 59 | Delivery Note: Update Delivered Qty, Update Percent and Validate over delivery |
| 60 | Sales Invoice: Update Billed Amt, Update Percent and Validate over billing |
| 61 | Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation |
| 62 | """ |
| 63 | |
| 64 | def update_prevdoc_status(self): |
| 65 | self.update_qty() |
| 66 | self.validate_qty() |
| 67 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 68 | def set_status(self, update=False): |
| 69 | if self.doc.get("__islocal"): |
| 70 | return |
| 71 | |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 72 | if self.doc.doctype in status_map: |
| 73 | sl = status_map[self.doc.doctype][:] |
| 74 | sl.reverse() |
| 75 | for s in sl: |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 76 | if not s[1]: |
| 77 | self.doc.status = s[0] |
| 78 | break |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 79 | elif s[1].startswith("eval:"): |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 80 | if eval(s[1][5:]): |
| 81 | self.doc.status = s[0] |
| 82 | break |
| 83 | elif getattr(self, s[1])(): |
| 84 | self.doc.status = s[0] |
| 85 | break |
| 86 | |
| 87 | if update: |
| 88 | webnotes.conn.set_value(self.doc.doctype, self.doc.name, "status", self.doc.status) |
| 89 | |
| 90 | def on_communication(self): |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 91 | self.communication_set = True |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 92 | self.set_status(update=True) |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 93 | del self.communication_set |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 94 | |
| 95 | def communication_received(self): |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 96 | if getattr(self, "communication_set", False): |
| 97 | last_comm = self.doclist.get({"doctype":"Communication"}) |
| 98 | if last_comm: |
| 99 | return last_comm[-1].sent_or_received == "Received" |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 100 | |
| 101 | def communication_sent(self): |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 102 | if getattr(self, "communication_set", False): |
| 103 | last_comm = self.doclist.get({"doctype":"Communication"}) |
| 104 | if last_comm: |
| 105 | return last_comm[-1].sent_or_received == "Sent" |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 106 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 107 | def validate_qty(self): |
| 108 | """ |
| 109 | Validates qty at row level |
| 110 | """ |
| 111 | self.tolerance = {} |
| 112 | self.global_tolerance = None |
| 113 | |
| 114 | for args in self.status_updater: |
| 115 | # get unique transactions to update |
| 116 | for d in self.doclist: |
| 117 | if d.doctype == args['source_dt'] and d.fields.get(args["join_field"]): |
| 118 | args['name'] = d.fields[args['join_field']] |
| 119 | |
| 120 | # get all qty where qty > target_field |
| 121 | item = webnotes.conn.sql("""select item_code, `%(target_ref_field)s`, |
| 122 | `%(target_field)s`, parenttype, parent from `tab%(target_dt)s` |
| 123 | where `%(target_ref_field)s` < `%(target_field)s` |
| 124 | and name="%(name)s" and docstatus=1""" % args, as_dict=1) |
| 125 | if item: |
| 126 | item = item[0] |
| 127 | item['idx'] = d.idx |
| 128 | item['target_ref_field'] = args['target_ref_field'].replace('_', ' ') |
| 129 | |
| 130 | if not item[args['target_ref_field']]: |
| 131 | msgprint("""As %(target_ref_field)s for item: %(item_code)s in \ |
| 132 | %(parenttype)s: %(parent)s is zero, system will not check \ |
| 133 | over-delivery or over-billed""" % item) |
| 134 | elif args.get('no_tolerance'): |
| 135 | item['reduce_by'] = item[args['target_field']] - \ |
| 136 | item[args['target_ref_field']] |
| 137 | if item['reduce_by'] > .01: |
| 138 | msgprint(""" |
| 139 | Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item \ |
| 140 | %(item_code)s</b> against <b>%(parenttype)s %(parent)s</b> \ |
| 141 | is <b>""" % item + cstr(item[args['target_ref_field']]) + |
| 142 | """</b>.<br>You must reduce the %(target_ref_field)s by \ |
| 143 | %(reduce_by)s""" % item, raise_exception=1) |
| 144 | |
| 145 | else: |
| 146 | self.check_overflow_with_tolerance(item, args) |
| 147 | |
| 148 | def check_overflow_with_tolerance(self, item, args): |
| 149 | """ |
| 150 | Checks if there is overflow condering a relaxation tolerance |
| 151 | """ |
| 152 | |
| 153 | # check if overflow is within tolerance |
| 154 | tolerance = self.get_tolerance_for(item['item_code']) |
| 155 | overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) / |
| 156 | item[args['target_ref_field']]) * 100 |
| 157 | |
| 158 | if overflow_percent - tolerance > 0.01: |
| 159 | item['max_allowed'] = flt(item[args['target_ref_field']] * (100+tolerance)/100) |
| 160 | item['reduce_by'] = item[args['target_field']] - item['max_allowed'] |
| 161 | |
| 162 | msgprint(""" |
| 163 | Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item %(item_code)s</b> \ |
| 164 | against <b>%(parenttype)s %(parent)s</b> is <b>%(max_allowed)s</b>. |
| 165 | |
| 166 | If you want to increase your overflow tolerance, please increase tolerance %% in \ |
| 167 | Global Defaults or Item master. |
| 168 | |
| 169 | Or, you must reduce the %(target_ref_field)s by %(reduce_by)s |
| 170 | |
| 171 | Also, please check if the order item has already been billed in the Sales Order""" % |
| 172 | item, raise_exception=1) |
| 173 | |
| 174 | def get_tolerance_for(self, item_code): |
| 175 | """ |
| 176 | Returns the tolerance for the item, if not set, returns global tolerance |
| 177 | """ |
| 178 | if self.tolerance.get(item_code): return self.tolerance[item_code] |
| 179 | |
| 180 | tolerance = flt(webnotes.conn.get_value('Item',item_code,'tolerance') or 0) |
| 181 | |
| 182 | if not tolerance: |
| 183 | if self.global_tolerance == None: |
| 184 | self.global_tolerance = flt(webnotes.conn.get_value('Global Defaults', None, |
| 185 | 'tolerance')) |
| 186 | tolerance = self.global_tolerance |
| 187 | |
| 188 | self.tolerance[item_code] = tolerance |
| 189 | return tolerance |
| 190 | |
| 191 | |
| 192 | def update_qty(self, change_modified=True): |
| 193 | """ |
| 194 | Updates qty at row level |
| 195 | """ |
| 196 | for args in self.status_updater: |
| 197 | # condition to include current record (if submit or no if cancel) |
| 198 | if self.doc.docstatus == 1: |
| 199 | args['cond'] = ' or parent="%s"' % self.doc.name |
| 200 | else: |
| 201 | args['cond'] = ' and parent!="%s"' % self.doc.name |
| 202 | |
| 203 | args['modified_cond'] = '' |
| 204 | if change_modified: |
| 205 | args['modified_cond'] = ', modified = now()' |
| 206 | |
| 207 | # update quantities in child table |
| 208 | for d in self.doclist: |
| 209 | if d.doctype == args['source_dt']: |
| 210 | # updates qty in the child table |
| 211 | args['detail_id'] = d.fields.get(args['join_field']) |
| 212 | |
| 213 | args['second_source_condition'] = "" |
| 214 | if args.get('second_source_dt') and args.get('second_source_field') \ |
| 215 | and args.get('second_join_field'): |
| 216 | args['second_source_condition'] = """ + (select sum(%(second_source_field)s) |
| 217 | from `tab%(second_source_dt)s` |
| 218 | where `%(second_join_field)s`="%(detail_id)s" |
| 219 | and (docstatus=1))""" % args |
| 220 | |
| 221 | if args['detail_id']: |
| 222 | webnotes.conn.sql("""update `tab%(target_dt)s` |
| 223 | set %(target_field)s = (select sum(%(source_field)s) |
| 224 | from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s" |
| 225 | and (docstatus=1 %(cond)s)) %(second_source_condition)s |
| 226 | where name='%(detail_id)s'""" % args) |
| 227 | |
| 228 | # get unique transactions to update |
| 229 | for name in set([d.fields.get(args['percent_join_field']) for d in self.doclist |
| 230 | if d.doctype == args['source_dt']]): |
| 231 | if name: |
| 232 | args['name'] = name |
| 233 | |
| 234 | # update percent complete in the parent table |
| 235 | webnotes.conn.sql("""update `tab%(target_parent_dt)s` |
| 236 | set %(target_parent_field)s = (select sum(if(%(target_ref_field)s > |
| 237 | ifnull(%(target_field)s, 0), %(target_field)s, |
| 238 | %(target_ref_field)s))/sum(%(target_ref_field)s)*100 |
| 239 | from `tab%(target_dt)s` where parent="%(name)s") %(modified_cond)s |
| 240 | where name='%(name)s'""" % args) |
| 241 | |
| 242 | # update field |
| 243 | if args.get('status_field'): |
| 244 | webnotes.conn.sql("""update `tab%(target_parent_dt)s` |
| 245 | set %(status_field)s = if(ifnull(%(target_parent_field)s,0)<0.001, |
| 246 | 'Not %(keyword)s', if(%(target_parent_field)s>=99.99, |
| 247 | 'Fully %(keyword)s', 'Partly %(keyword)s')) |
| 248 | where name='%(name)s'""" % args) |