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 |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 154 | tolerance, self.tolerance, self.global_tolerance = get_tolerance_for(item['item_code'], |
| 155 | self.tolerance, self.global_tolerance) |
| 156 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 157 | overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) / |
| 158 | item[args['target_ref_field']]) * 100 |
| 159 | |
| 160 | if overflow_percent - tolerance > 0.01: |
| 161 | item['max_allowed'] = flt(item[args['target_ref_field']] * (100+tolerance)/100) |
| 162 | item['reduce_by'] = item[args['target_field']] - item['max_allowed'] |
| 163 | |
| 164 | msgprint(""" |
| 165 | Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item %(item_code)s</b> \ |
| 166 | against <b>%(parenttype)s %(parent)s</b> is <b>%(max_allowed)s</b>. |
| 167 | |
| 168 | If you want to increase your overflow tolerance, please increase tolerance %% in \ |
| 169 | Global Defaults or Item master. |
| 170 | |
| 171 | Or, you must reduce the %(target_ref_field)s by %(reduce_by)s |
| 172 | |
| 173 | Also, please check if the order item has already been billed in the Sales Order""" % |
| 174 | item, raise_exception=1) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 175 | |
| 176 | |
| 177 | def update_qty(self, change_modified=True): |
| 178 | """ |
| 179 | Updates qty at row level |
| 180 | """ |
| 181 | for args in self.status_updater: |
| 182 | # condition to include current record (if submit or no if cancel) |
| 183 | if self.doc.docstatus == 1: |
| 184 | args['cond'] = ' or parent="%s"' % self.doc.name |
| 185 | else: |
| 186 | args['cond'] = ' and parent!="%s"' % self.doc.name |
| 187 | |
| 188 | args['modified_cond'] = '' |
| 189 | if change_modified: |
| 190 | args['modified_cond'] = ', modified = now()' |
| 191 | |
| 192 | # update quantities in child table |
| 193 | for d in self.doclist: |
| 194 | if d.doctype == args['source_dt']: |
| 195 | # updates qty in the child table |
| 196 | args['detail_id'] = d.fields.get(args['join_field']) |
| 197 | |
| 198 | args['second_source_condition'] = "" |
| 199 | if args.get('second_source_dt') and args.get('second_source_field') \ |
| 200 | and args.get('second_join_field'): |
| 201 | args['second_source_condition'] = """ + (select sum(%(second_source_field)s) |
| 202 | from `tab%(second_source_dt)s` |
| 203 | where `%(second_join_field)s`="%(detail_id)s" |
| 204 | and (docstatus=1))""" % args |
| 205 | |
| 206 | if args['detail_id']: |
| 207 | webnotes.conn.sql("""update `tab%(target_dt)s` |
| 208 | set %(target_field)s = (select sum(%(source_field)s) |
| 209 | from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s" |
| 210 | and (docstatus=1 %(cond)s)) %(second_source_condition)s |
| 211 | where name='%(detail_id)s'""" % args) |
| 212 | |
| 213 | # get unique transactions to update |
| 214 | for name in set([d.fields.get(args['percent_join_field']) for d in self.doclist |
| 215 | if d.doctype == args['source_dt']]): |
| 216 | if name: |
| 217 | args['name'] = name |
| 218 | |
| 219 | # update percent complete in the parent table |
| 220 | webnotes.conn.sql("""update `tab%(target_parent_dt)s` |
| 221 | set %(target_parent_field)s = (select sum(if(%(target_ref_field)s > |
| 222 | ifnull(%(target_field)s, 0), %(target_field)s, |
| 223 | %(target_ref_field)s))/sum(%(target_ref_field)s)*100 |
| 224 | from `tab%(target_dt)s` where parent="%(name)s") %(modified_cond)s |
| 225 | where name='%(name)s'""" % args) |
| 226 | |
| 227 | # update field |
| 228 | if args.get('status_field'): |
| 229 | webnotes.conn.sql("""update `tab%(target_parent_dt)s` |
| 230 | set %(status_field)s = if(ifnull(%(target_parent_field)s,0)<0.001, |
| 231 | 'Not %(keyword)s', if(%(target_parent_field)s>=99.99, |
| 232 | 'Fully %(keyword)s', 'Partly %(keyword)s')) |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 233 | where name='%(name)s'""" % args) |
| 234 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 235 | |
| 236 | def update_billing_status_for_zero_amount_refdoc(self, ref_dt): |
| 237 | ref_fieldname = ref_dt.lower().replace(" ", "_") |
| 238 | zero_amount_refdoc = [] |
| 239 | all_zero_amount_refdoc = webnotes.conn.sql_list("""select name from `tab%s` |
| 240 | where docstatus=1 and net_total = 0""" % ref_dt) |
| 241 | |
| 242 | for item in self.doclist.get({"parentfield": "entries"}): |
| 243 | if item.fields.get(ref_fieldname) \ |
| 244 | and item.fields.get(ref_fieldname) in all_zero_amount_refdoc \ |
| 245 | and item.fields.get(ref_fieldname) not in zero_amount_refdoc: |
| 246 | zero_amount_refdoc.append(item.fields[ref_fieldname]) |
| 247 | |
| 248 | if zero_amount_refdoc: |
| 249 | self.update_biling_status(zero_amount_refdoc, ref_dt, ref_fieldname) |
| 250 | |
| 251 | def update_biling_status(self, zero_amount_refdoc, ref_dt, ref_fieldname): |
| 252 | for ref_dn in zero_amount_refdoc: |
| 253 | ref_doc_qty = flt(webnotes.conn.sql("""select sum(ifnull(qty, 0)) from `tab%s Item` |
| 254 | where parent=%s""" % (ref_dt, '%s'), (ref_dn))[0][0]) |
| 255 | |
| 256 | billed_qty = flt(webnotes.conn.sql("""select sum(ifnull(qty, 0)) |
| 257 | from `tab%s Item` where %s=%s and docstatus=1""" % |
| 258 | (self.doc.doctype, ref_fieldname, '%s'), (ref_dn))[0][0]) |
| 259 | |
| 260 | per_billed = ((ref_doc_qty if billed_qty > ref_doc_qty else billed_qty)\ |
| 261 | / ref_doc_qty)*100 |
| 262 | webnotes.conn.set_value(ref_dt, ref_dn, "per_billed", per_billed) |
| 263 | |
| 264 | from webnotes.model.meta import has_field |
| 265 | if has_field(ref_dt, "billing_status"): |
| 266 | if per_billed < 0.001: billing_status = "Not Billed" |
| 267 | elif per_billed >= 99.99: billing_status = "Fully Billed" |
| 268 | else: billing_status = "Partly Billed" |
| 269 | |
| 270 | webnotes.conn.set_value(ref_dt, ref_dn, "billing_status", billing_status) |
| 271 | |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 272 | def get_tolerance_for(item_code, item_tolerance={}, global_tolerance=None): |
| 273 | """ |
| 274 | Returns the tolerance for the item, if not set, returns global tolerance |
| 275 | """ |
| 276 | if item_tolerance.get(item_code): |
| 277 | return item_tolerance[item_code], item_tolerance, global_tolerance |
| 278 | |
| 279 | tolerance = flt(webnotes.conn.get_value('Item',item_code,'tolerance') or 0) |
| 280 | |
| 281 | if not tolerance: |
| 282 | if global_tolerance == None: |
| 283 | global_tolerance = flt(webnotes.conn.get_value('Global Defaults', None, |
| 284 | 'tolerance')) |
| 285 | tolerance = global_tolerance |
| 286 | |
| 287 | item_tolerance[item_code] = tolerance |
| 288 | return tolerance, item_tolerance, global_tolerance |