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 |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 6 | from frappe.utils import flt |
| 7 | from frappe import msgprint, _, throw |
Anand Doshi | 424c033 | 2014-04-21 15:06:56 +0530 | [diff] [blame] | 8 | from frappe.model.document import Document |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 10 | status_map = { |
| 11 | "Contact": [ |
| 12 | ["Replied", "communication_sent"], |
| 13 | ["Open", "communication_received"] |
| 14 | ], |
| 15 | "Job Applicant": [ |
| 16 | ["Replied", "communication_sent"], |
| 17 | ["Open", "communication_received"] |
| 18 | ], |
| 19 | "Lead": [ |
| 20 | ["Replied", "communication_sent"], |
| 21 | ["Converted", "has_customer"], |
| 22 | ["Opportunity", "has_opportunity"], |
| 23 | ["Open", "communication_received"], |
| 24 | ], |
| 25 | "Opportunity": [ |
| 26 | ["Draft", None], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 27 | ["Submitted", "eval:self.docstatus==1"], |
| 28 | ["Lost", "eval:self.status=='Lost'"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 29 | ["Quotation", "has_quotation"], |
| 30 | ["Replied", "communication_sent"], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 31 | ["Cancelled", "eval:self.docstatus==2"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 32 | ["Open", "communication_received"], |
| 33 | ], |
| 34 | "Quotation": [ |
| 35 | ["Draft", None], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 36 | ["Submitted", "eval:self.docstatus==1"], |
| 37 | ["Lost", "eval:self.status=='Lost'"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 38 | ["Ordered", "has_sales_order"], |
| 39 | ["Replied", "communication_sent"], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 40 | ["Cancelled", "eval:self.docstatus==2"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 41 | ["Open", "communication_received"], |
| 42 | ], |
| 43 | "Sales Order": [ |
| 44 | ["Draft", None], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 45 | ["Submitted", "eval:self.docstatus==1"], |
| 46 | ["Stopped", "eval:self.status=='Stopped'"], |
| 47 | ["Cancelled", "eval:self.docstatus==2"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 48 | ], |
| 49 | "Support Ticket": [ |
| 50 | ["Replied", "communication_sent"], |
| 51 | ["Open", "communication_received"] |
| 52 | ], |
| 53 | } |
| 54 | |
Anand Doshi | 424c033 | 2014-04-21 15:06:56 +0530 | [diff] [blame] | 55 | class StatusUpdater(Document): |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 56 | """ |
| 57 | Updates the status of the calling records |
| 58 | Delivery Note: Update Delivered Qty, Update Percent and Validate over delivery |
| 59 | Sales Invoice: Update Billed Amt, Update Percent and Validate over billing |
| 60 | Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation |
| 61 | """ |
| 62 | |
| 63 | def update_prevdoc_status(self): |
| 64 | self.update_qty() |
| 65 | self.validate_qty() |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 66 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 67 | def set_status(self, update=False): |
Rushabh Mehta | cb067aa | 2014-08-19 16:20:04 +0530 | [diff] [blame] | 68 | if self.is_new(): |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 69 | return |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 70 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 71 | if self.doctype in status_map: |
Nabin Hait | cabf9c5 | 2014-08-21 16:34:38 +0530 | [diff] [blame] | 72 | _status = self.status |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 73 | sl = status_map[self.doctype][:] |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 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]: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 77 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 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:]): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 81 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 82 | break |
| 83 | elif getattr(self, s[1])(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 84 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 85 | break |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 86 | |
Rushabh Mehta | cb067aa | 2014-08-19 16:20:04 +0530 | [diff] [blame] | 87 | if self.status != _status: |
| 88 | self.add_comment("Label", self.status) |
| 89 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 90 | if update: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 91 | frappe.db.set_value(self.doctype, self.name, "status", self.status) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 92 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 93 | def on_communication(self): |
Nabin Hait | 08dd6af | 2014-05-26 15:06:42 +0530 | [diff] [blame] | 94 | if not self.get("communications"): return |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 95 | self.communication_set = True |
Nabin Hait | 85786f3 | 2014-05-19 19:31:40 +0530 | [diff] [blame] | 96 | self.get("communications").sort(key=lambda d: d.creation) |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 97 | self.set_status(update=True) |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 98 | del self.communication_set |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 99 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 100 | def communication_received(self): |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 101 | if getattr(self, "communication_set", False): |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 102 | last_comm = self.get("communications") |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 103 | if last_comm: |
| 104 | return last_comm[-1].sent_or_received == "Received" |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 105 | |
| 106 | def communication_sent(self): |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 107 | if getattr(self, "communication_set", False): |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 108 | last_comm = self.get("communications") |
Rushabh Mehta | acc876e | 2013-10-04 13:33:24 +0530 | [diff] [blame] | 109 | if last_comm: |
| 110 | return last_comm[-1].sent_or_received == "Sent" |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 111 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 112 | def validate_qty(self): |
| 113 | """ |
| 114 | Validates qty at row level |
| 115 | """ |
| 116 | self.tolerance = {} |
| 117 | self.global_tolerance = None |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 119 | for args in self.status_updater: |
| 120 | # get unique transactions to update |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 121 | for d in self.get_all_children(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 122 | if d.doctype == args['source_dt'] and d.get(args["join_field"]): |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 123 | args['name'] = d.get(args['join_field']) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 124 | |
| 125 | # get all qty where qty > target_field |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 126 | item = frappe.db.sql("""select item_code, `{target_ref_field}`, |
| 127 | `{target_field}`, parenttype, parent from `tab{target_dt}` |
| 128 | where `{target_ref_field}` < `{target_field}` |
| 129 | and name=%s and docstatus=1""".format(**args), |
Nabin Hait | 4d713ac | 2014-03-03 15:51:13 +0530 | [diff] [blame] | 130 | args['name'], as_dict=1) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 131 | if item: |
| 132 | item = item[0] |
| 133 | item['idx'] = d.idx |
| 134 | item['target_ref_field'] = args['target_ref_field'].replace('_', ' ') |
| 135 | |
| 136 | if not item[args['target_ref_field']]: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 137 | msgprint(_("Note: System will not check over-delivery and over-booking for Item {0} as quantity or amount is 0").format(item.item_code)) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 138 | elif args.get('no_tolerance'): |
nabinhait | d5fb5d9 | 2014-07-09 17:36:38 +0530 | [diff] [blame] | 139 | item['reduce_by'] = item[args['target_field']] - item[args['target_ref_field']] |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 140 | if item['reduce_by'] > .01: |
nabinhait | d5fb5d9 | 2014-07-09 17:36:38 +0530 | [diff] [blame] | 141 | msgprint(_("Allowance for over-{0} crossed for Item {1}") |
| 142 | .format(args["overflow_type"], item.item_code)) |
| 143 | throw(_("{0} must be reduced by {1} or you should increase overflow tolerance") |
| 144 | .format(_(item.target_ref_field.title()), item["reduce_by"])) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 145 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 146 | else: |
| 147 | self.check_overflow_with_tolerance(item, args) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 148 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 149 | def check_overflow_with_tolerance(self, item, args): |
| 150 | """ |
| 151 | Checks if there is overflow condering a relaxation tolerance |
| 152 | """ |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 153 | # check if overflow is within tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 154 | tolerance, self.tolerance, self.global_tolerance = get_tolerance_for(item['item_code'], |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 155 | self.tolerance, self.global_tolerance) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 156 | |
| 157 | overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) / |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 158 | item[args['target_ref_field']]) * 100 |
nabinhait | 711e8be | 2014-07-19 17:00:15 +0530 | [diff] [blame] | 159 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 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'] |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 163 | |
nabinhait | d5fb5d9 | 2014-07-09 17:36:38 +0530 | [diff] [blame] | 164 | msgprint(_("Allowance for over-{0} crossed for Item {1}.") |
| 165 | .format(args["overflow_type"], item["item_code"])) |
| 166 | throw(_("{0} must be reduced by {1} or you should increase overflow tolerance") |
| 167 | .format(_(item["target_ref_field"].title()), item["reduce_by"])) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 168 | |
| 169 | def update_qty(self, change_modified=True): |
| 170 | """ |
| 171 | Updates qty at row level |
| 172 | """ |
| 173 | for args in self.status_updater: |
| 174 | # condition to include current record (if submit or no if cancel) |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 175 | if self.docstatus == 1: |
| 176 | args['cond'] = ' or parent="%s"' % self.name.replace('"', '\"') |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 177 | else: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 178 | args['cond'] = ' and parent!="%s"' % self.name.replace('"', '\"') |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 179 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 180 | args['modified_cond'] = '' |
| 181 | if change_modified: |
| 182 | args['modified_cond'] = ', modified = now()' |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 183 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 184 | # update quantities in child table |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 185 | for d in self.get_all_children(): |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 186 | if d.doctype == args['source_dt']: |
| 187 | # updates qty in the child table |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 188 | args['detail_id'] = d.get(args['join_field']) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 189 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 190 | args['second_source_condition'] = "" |
| 191 | if args.get('second_source_dt') and args.get('second_source_field') \ |
| 192 | and args.get('second_join_field'): |
Rushabh Mehta | c53efc0 | 2014-07-01 17:51:34 +0530 | [diff] [blame] | 193 | args['second_source_condition'] = """ + ifnull((select sum(%(second_source_field)s) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 194 | from `tab%(second_source_dt)s` |
| 195 | where `%(second_join_field)s`="%(detail_id)s" |
Rushabh Mehta | c53efc0 | 2014-07-01 17:51:34 +0530 | [diff] [blame] | 196 | and (docstatus=1)), 0)""" % args |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 197 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 198 | if args['detail_id']: |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 199 | frappe.db.sql("""update `tab%(target_dt)s` |
| 200 | set %(target_field)s = (select sum(%(source_field)s) |
| 201 | from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s" |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 202 | and (docstatus=1 %(cond)s)) %(second_source_condition)s |
| 203 | where name='%(detail_id)s'""" % args) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 204 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 205 | # get unique transactions to update |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 206 | for name in set([d.get(args['percent_join_field']) for d in self.get_all_children(args['source_dt'])]): |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 207 | if name: |
| 208 | args['name'] = name |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 209 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 210 | # update percent complete in the parent table |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 211 | frappe.db.sql("""update `tab%(target_parent_dt)s` |
| 212 | set %(target_parent_field)s = (select sum(if(%(target_ref_field)s > |
| 213 | ifnull(%(target_field)s, 0), %(target_field)s, |
| 214 | %(target_ref_field)s))/sum(%(target_ref_field)s)*100 |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 215 | from `tab%(target_dt)s` where parent="%(name)s") %(modified_cond)s |
| 216 | where name='%(name)s'""" % args) |
| 217 | |
| 218 | # update field |
| 219 | if args.get('status_field'): |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 220 | frappe.db.sql("""update `tab%(target_parent_dt)s` |
| 221 | set %(status_field)s = if(ifnull(%(target_parent_field)s,0)<0.001, |
| 222 | 'Not %(keyword)s', if(%(target_parent_field)s>=99.99, |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 223 | 'Fully %(keyword)s', 'Partly %(keyword)s')) |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 224 | where name='%(name)s'""" % args) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 225 | |
| 226 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 227 | def update_billing_status_for_zero_amount_refdoc(self, ref_dt): |
| 228 | ref_fieldname = ref_dt.lower().replace(" ", "_") |
| 229 | zero_amount_refdoc = [] |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 230 | all_zero_amount_refdoc = frappe.db.sql_list("""select name from `tab%s` |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 231 | where docstatus=1 and net_total = 0""" % ref_dt) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 232 | |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 233 | for item in self.get("entries"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 234 | if item.get(ref_fieldname) \ |
| 235 | and item.get(ref_fieldname) in all_zero_amount_refdoc \ |
| 236 | and item.get(ref_fieldname) not in zero_amount_refdoc: |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 237 | zero_amount_refdoc.append(item.get(ref_fieldname)) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 238 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 239 | if zero_amount_refdoc: |
| 240 | self.update_biling_status(zero_amount_refdoc, ref_dt, ref_fieldname) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 241 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 242 | def update_biling_status(self, zero_amount_refdoc, ref_dt, ref_fieldname): |
| 243 | for ref_dn in zero_amount_refdoc: |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 244 | ref_doc_qty = flt(frappe.db.sql("""select sum(ifnull(qty, 0)) from `tab%s Item` |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 245 | where parent=%s""" % (ref_dt, '%s'), (ref_dn))[0][0]) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 246 | |
| 247 | billed_qty = flt(frappe.db.sql("""select sum(ifnull(qty, 0)) |
| 248 | from `tab%s Item` where %s=%s and docstatus=1""" % |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 249 | (self.doctype, ref_fieldname, '%s'), (ref_dn))[0][0]) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 250 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 251 | per_billed = ((ref_doc_qty if billed_qty > ref_doc_qty else billed_qty)\ |
| 252 | / ref_doc_qty)*100 |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 253 | frappe.db.set_value(ref_dt, ref_dn, "per_billed", per_billed) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 254 | |
Rushabh Mehta | 0a0f249 | 2014-03-31 17:27:06 +0530 | [diff] [blame] | 255 | if frappe.get_meta(ref_dt).get_field("billing_status"): |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 256 | if per_billed < 0.001: billing_status = "Not Billed" |
| 257 | elif per_billed >= 99.99: billing_status = "Fully Billed" |
| 258 | else: billing_status = "Partly Billed" |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 259 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 260 | frappe.db.set_value(ref_dt, ref_dn, "billing_status", billing_status) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 261 | |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 262 | def get_tolerance_for(item_code, item_tolerance={}, global_tolerance=None): |
| 263 | """ |
| 264 | Returns the tolerance for the item, if not set, returns global tolerance |
| 265 | """ |
| 266 | if item_tolerance.get(item_code): |
| 267 | return item_tolerance[item_code], item_tolerance, global_tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 268 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 269 | tolerance = flt(frappe.db.get_value('Item',item_code,'tolerance') or 0) |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 270 | |
| 271 | if not tolerance: |
| 272 | if global_tolerance == None: |
nabinhait | b1c5738 | 2014-07-19 16:53:45 +0530 | [diff] [blame] | 273 | global_tolerance = flt(frappe.db.get_value('Stock Settings', None, 'tolerance')) |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 274 | tolerance = global_tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 275 | |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 276 | item_tolerance[item_code] = tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 277 | return tolerance, item_tolerance, global_tolerance |