Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe 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 | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 6 | from frappe.utils import flt, comma_or |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 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 | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 10 | def validate_status(status, options): |
| 11 | if status not in options: |
| 12 | frappe.throw(_("Status must be one of {0}").format(comma_or(options))) |
| 13 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 14 | status_map = { |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 15 | "Lead": [ |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 16 | ["Converted", "has_customer"], |
| 17 | ["Opportunity", "has_opportunity"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 18 | ], |
| 19 | "Opportunity": [ |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 20 | ["Lost", "eval:self.status=='Lost'"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 21 | ["Quotation", "has_quotation"], |
Rushabh Mehta | fd0f1cb | 2015-04-13 16:21:58 +0530 | [diff] [blame] | 22 | ["Converted", "has_ordered_quotation"] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 23 | ], |
| 24 | "Quotation": [ |
Rushabh Mehta | 4ac3094 | 2015-04-13 16:56:03 +0530 | [diff] [blame] | 25 | ["Draft", None], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 26 | ["Submitted", "eval:self.docstatus==1"], |
| 27 | ["Lost", "eval:self.status=='Lost'"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 28 | ["Ordered", "has_sales_order"], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 29 | ["Cancelled", "eval:self.docstatus==2"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 30 | ], |
| 31 | "Sales Order": [ |
| 32 | ["Draft", None], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 33 | ["Submitted", "eval:self.docstatus==1"], |
| 34 | ["Stopped", "eval:self.status=='Stopped'"], |
| 35 | ["Cancelled", "eval:self.docstatus==2"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 36 | ], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 37 | "Delivery Note": [ |
| 38 | ["Draft", None], |
| 39 | ["Submitted", "eval:self.docstatus==1"], |
| 40 | ["Cancelled", "eval:self.docstatus==2"], |
| 41 | ], |
| 42 | "Purchase Receipt": [ |
| 43 | ["Draft", None], |
| 44 | ["Submitted", "eval:self.docstatus==1"], |
| 45 | ["Cancelled", "eval:self.docstatus==2"], |
| 46 | ] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 47 | } |
| 48 | |
Anand Doshi | 424c033 | 2014-04-21 15:06:56 +0530 | [diff] [blame] | 49 | class StatusUpdater(Document): |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 50 | """ |
| 51 | Updates the status of the calling records |
| 52 | Delivery Note: Update Delivered Qty, Update Percent and Validate over delivery |
| 53 | Sales Invoice: Update Billed Amt, Update Percent and Validate over billing |
| 54 | Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation |
| 55 | """ |
| 56 | |
| 57 | def update_prevdoc_status(self): |
| 58 | self.update_qty() |
| 59 | self.validate_qty() |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 60 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 61 | def set_status(self, update=False): |
Rushabh Mehta | cb067aa | 2014-08-19 16:20:04 +0530 | [diff] [blame] | 62 | if self.is_new(): |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 63 | return |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 64 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 65 | if self.doctype in status_map: |
Nabin Hait | cabf9c5 | 2014-08-21 16:34:38 +0530 | [diff] [blame] | 66 | _status = self.status |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 67 | sl = status_map[self.doctype][:] |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 68 | sl.reverse() |
| 69 | for s in sl: |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 70 | if not s[1]: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 71 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 72 | break |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 73 | elif s[1].startswith("eval:"): |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 74 | if eval(s[1][5:]): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 75 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 76 | break |
| 77 | elif getattr(self, s[1])(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 78 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 79 | break |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 80 | |
Rushabh Mehta | c8b406d | 2015-06-02 12:17:14 +0530 | [diff] [blame] | 81 | if self.status != _status and self.status not in ("Submitted", "Cancelled"): |
Anand Doshi | 7b34ebe | 2015-01-07 15:41:32 +0530 | [diff] [blame] | 82 | self.add_comment("Label", _(self.status)) |
Rushabh Mehta | cb067aa | 2014-08-19 16:20:04 +0530 | [diff] [blame] | 83 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 84 | if update: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 85 | frappe.db.set_value(self.doctype, self.name, "status", self.status) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 86 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 87 | def validate_qty(self): |
Rushabh Mehta | a9ba5ef | 2014-12-19 16:20:32 +0530 | [diff] [blame] | 88 | """Validates qty at row level""" |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 89 | self.tolerance = {} |
| 90 | self.global_tolerance = None |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 91 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 92 | for args in self.status_updater: |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 93 | if "target_ref_field" not in args: |
| 94 | # if target_ref_field is not specified, the programmer does not want to validate qty / amount |
| 95 | continue |
| 96 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 97 | # get unique transactions to update |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 98 | for d in self.get_all_children(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 99 | if d.doctype == args['source_dt'] and d.get(args["join_field"]): |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 100 | args['name'] = d.get(args['join_field']) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 101 | |
| 102 | # get all qty where qty > target_field |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 103 | item = frappe.db.sql("""select item_code, `{target_ref_field}`, |
| 104 | `{target_field}`, parenttype, parent from `tab{target_dt}` |
| 105 | where `{target_ref_field}` < `{target_field}` |
| 106 | and name=%s and docstatus=1""".format(**args), |
Nabin Hait | 4d713ac | 2014-03-03 15:51:13 +0530 | [diff] [blame] | 107 | args['name'], as_dict=1) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 108 | if item: |
| 109 | item = item[0] |
| 110 | item['idx'] = d.idx |
| 111 | item['target_ref_field'] = args['target_ref_field'].replace('_', ' ') |
| 112 | |
| 113 | if not item[args['target_ref_field']]: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 114 | 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] | 115 | elif args.get('no_tolerance'): |
nabinhait | d5fb5d9 | 2014-07-09 17:36:38 +0530 | [diff] [blame] | 116 | item['reduce_by'] = item[args['target_field']] - item[args['target_ref_field']] |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 117 | if item['reduce_by'] > .01: |
nabinhait | d5fb5d9 | 2014-07-09 17:36:38 +0530 | [diff] [blame] | 118 | msgprint(_("Allowance for over-{0} crossed for Item {1}") |
| 119 | .format(args["overflow_type"], item.item_code)) |
| 120 | throw(_("{0} must be reduced by {1} or you should increase overflow tolerance") |
| 121 | .format(_(item.target_ref_field.title()), item["reduce_by"])) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 122 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 123 | else: |
| 124 | self.check_overflow_with_tolerance(item, args) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 125 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 126 | def check_overflow_with_tolerance(self, item, args): |
| 127 | """ |
| 128 | Checks if there is overflow condering a relaxation tolerance |
| 129 | """ |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 130 | # check if overflow is within tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 131 | tolerance, self.tolerance, self.global_tolerance = get_tolerance_for(item['item_code'], |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 132 | self.tolerance, self.global_tolerance) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 133 | |
| 134 | overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) / |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 135 | item[args['target_ref_field']]) * 100 |
nabinhait | 711e8be | 2014-07-19 17:00:15 +0530 | [diff] [blame] | 136 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 137 | if overflow_percent - tolerance > 0.01: |
| 138 | item['max_allowed'] = flt(item[args['target_ref_field']] * (100+tolerance)/100) |
| 139 | item['reduce_by'] = item[args['target_field']] - item['max_allowed'] |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 140 | |
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"])) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 145 | |
| 146 | def update_qty(self, change_modified=True): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 147 | """Updates qty or amount at row level |
| 148 | |
| 149 | :param change_modified: If true, updates `modified` and `modified_by` for target parent doc |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 150 | """ |
| 151 | for args in self.status_updater: |
| 152 | # condition to include current record (if submit or no if cancel) |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 153 | if self.docstatus == 1: |
| 154 | args['cond'] = ' or parent="%s"' % self.name.replace('"', '\"') |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 155 | else: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 156 | args['cond'] = ' and parent!="%s"' % self.name.replace('"', '\"') |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 157 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 158 | args['set_modified'] = '' |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 159 | if change_modified: |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 160 | args['set_modified'] = ', modified = now(), modified_by = "{0}"'\ |
| 161 | .format(frappe.db.escape(frappe.session.user)) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 162 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 163 | self._update_children(args) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 164 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 165 | if "percent_join_field" in args: |
| 166 | self._update_percent_field(args) |
ankitjavalkarwork | 9aadb0d | 2014-10-07 17:38:46 +0530 | [diff] [blame] | 167 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 168 | def _update_children(self, args): |
| 169 | """Update quantities or amount in child table""" |
| 170 | for d in self.get_all_children(): |
| 171 | if d.doctype != args['source_dt']: |
| 172 | continue |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 173 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 174 | # updates qty in the child table |
| 175 | args['detail_id'] = d.get(args['join_field']) |
ankitjavalkarwork | 9aadb0d | 2014-10-07 17:38:46 +0530 | [diff] [blame] | 176 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 177 | args['second_source_condition'] = "" |
| 178 | if args.get('second_source_dt') and args.get('second_source_field') \ |
| 179 | and args.get('second_join_field'): |
| 180 | if not args.get("second_source_extra_cond"): |
| 181 | args["second_source_extra_cond"] = "" |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 182 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 183 | args['second_source_condition'] = """ + ifnull((select sum(%(second_source_field)s) |
| 184 | from `tab%(second_source_dt)s` |
| 185 | where `%(second_join_field)s`="%(detail_id)s" |
| 186 | and (`tab%(second_source_dt)s`.docstatus=1) %(second_source_extra_cond)s), 0) """ % args |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 187 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 188 | if args['detail_id']: |
| 189 | if not args.get("extra_cond"): args["extra_cond"] = "" |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 190 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 191 | frappe.db.sql("""update `tab%(target_dt)s` |
| 192 | set %(target_field)s = (select sum(%(source_field)s) |
| 193 | from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s" |
| 194 | and (docstatus=1 %(cond)s) %(extra_cond)s) %(second_source_condition)s |
| 195 | where name='%(detail_id)s'""" % args) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 196 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 197 | def _update_percent_field(self, args): |
| 198 | """Update percent field in parent transaction""" |
| 199 | unique_transactions = set([d.get(args['percent_join_field']) for d in self.get_all_children(args['source_dt'])]) |
| 200 | |
| 201 | for name in unique_transactions: |
| 202 | if not name: |
| 203 | continue |
| 204 | |
| 205 | args['name'] = name |
| 206 | |
| 207 | # update percent complete in the parent table |
| 208 | if args.get('target_parent_field'): |
| 209 | frappe.db.sql("""update `tab%(target_parent_dt)s` |
Anand Doshi | d970b00 | 2015-09-25 17:57:20 +0530 | [diff] [blame] | 210 | set %(target_parent_field)s = round((select sum(if(%(target_ref_field)s > |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 211 | ifnull(%(target_field)s, 0), %(target_field)s, |
| 212 | %(target_ref_field)s))/sum(%(target_ref_field)s)*100 |
Anand Doshi | d970b00 | 2015-09-25 17:57:20 +0530 | [diff] [blame] | 213 | from `tab%(target_dt)s` where parent="%(name)s"), 2) %(set_modified)s |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 214 | where name='%(name)s'""" % args) |
| 215 | |
| 216 | # update field |
| 217 | if args.get('status_field'): |
| 218 | frappe.db.sql("""update `tab%(target_parent_dt)s` |
| 219 | set %(status_field)s = if(ifnull(%(target_parent_field)s,0)<0.001, |
| 220 | 'Not %(keyword)s', if(%(target_parent_field)s>=99.99, |
| 221 | 'Fully %(keyword)s', 'Partly %(keyword)s')) |
| 222 | where name='%(name)s'""" % args) |
| 223 | |
| 224 | if args.get("set_modified"): |
Rushabh Mehta | bc23e5a | 2015-09-10 10:30:41 +0530 | [diff] [blame] | 225 | frappe.get_doc(args["target_parent_dt"], name).notify_update() |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 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 | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 231 | where docstatus=1 and base_net_total = 0""" % ref_dt) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 232 | |
Nabin Hait | 4b8185d | 2014-12-25 18:19:39 +0530 | [diff] [blame] | 233 | for item in self.get("items"): |
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 |