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 |
Rohit Waghchaure | 2f1db57 | 2016-11-08 12:39:33 +0530 | [diff] [blame] | 6 | from frappe.utils import flt, comma_or, nowdate, getdate |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 7 | from frappe import _ |
Anand Doshi | 424c033 | 2014-04-21 15:06:56 +0530 | [diff] [blame] | 8 | from frappe.model.document import Document |
Rushabh Mehta | fc8e589 | 2016-07-20 12:08:47 +0530 | [diff] [blame] | 9 | from erpnext.accounts.party_status import notify_status |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 11 | def validate_status(status, options): |
| 12 | if status not in options: |
| 13 | frappe.throw(_("Status must be one of {0}").format(comma_or(options))) |
| 14 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 15 | status_map = { |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 16 | "Lead": [ |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 17 | ["Converted", "has_customer"], |
| 18 | ["Opportunity", "has_opportunity"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 19 | ], |
| 20 | "Opportunity": [ |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 21 | ["Quotation", "has_quotation"], |
Valmik Jangla | 2956327 | 2016-03-29 11:13:07 +0530 | [diff] [blame] | 22 | ["Converted", "has_ordered_quotation"], |
| 23 | ["Lost", "eval:self.status=='Lost'"], |
| 24 | ["Closed", "eval:self.status=='Closed'"] |
| 25 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 26 | ], |
| 27 | "Quotation": [ |
Rushabh Mehta | 4ac3094 | 2015-04-13 16:56:03 +0530 | [diff] [blame] | 28 | ["Draft", None], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 29 | ["Submitted", "eval:self.docstatus==1"], |
| 30 | ["Lost", "eval:self.status=='Lost'"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 31 | ["Ordered", "has_sales_order"], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 32 | ["Cancelled", "eval:self.docstatus==2"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 33 | ], |
| 34 | "Sales Order": [ |
| 35 | ["Draft", None], |
Rushabh Mehta | d10ba85 | 2015-10-02 12:42:48 +0530 | [diff] [blame] | 36 | ["To Deliver and Bill", "eval:self.per_delivered < 100 and self.per_billed < 100 and self.docstatus == 1"], |
| 37 | ["To Bill", "eval:self.per_delivered == 100 and self.per_billed < 100 and self.docstatus == 1"], |
| 38 | ["To Deliver", "eval:self.per_delivered < 100 and self.per_billed == 100 and self.docstatus == 1"], |
| 39 | ["Completed", "eval:self.per_delivered == 100 and self.per_billed == 100 and self.docstatus == 1"], |
Rushabh Mehta | 86125b2 | 2015-10-13 11:48:08 +0530 | [diff] [blame] | 40 | ["Completed", "eval:self.order_type == 'Maintenance' and self.per_billed == 100 and self.docstatus == 1"], |
Rushabh Mehta | d10ba85 | 2015-10-02 12:42:48 +0530 | [diff] [blame] | 41 | ["Cancelled", "eval:self.docstatus==2"], |
Saurabh | c6dbe70 | 2015-10-19 14:17:52 +0530 | [diff] [blame] | 42 | ["Closed", "eval:self.status=='Closed'"], |
Rushabh Mehta | d10ba85 | 2015-10-02 12:42:48 +0530 | [diff] [blame] | 43 | ], |
Rohit Waghchaure | 2f1db57 | 2016-11-08 12:39:33 +0530 | [diff] [blame] | 44 | "Sales Invoice": [ |
| 45 | ["Draft", None], |
| 46 | ["Submitted", "eval:self.docstatus==1"], |
Rohit Waghchaure | 2285f90 | 2016-11-09 14:16:58 +0530 | [diff] [blame] | 47 | ["Return", "eval:self.is_return==1 and self.docstatus==1"], |
| 48 | ["Credit Note Issued", "eval:self.outstanding_amount < 0 and self.docstatus==1"], |
| 49 | ["Paid", "eval:self.outstanding_amount==0 and self.docstatus==1 and self.is_return==0"], |
Rohit Waghchaure | 2f1db57 | 2016-11-08 12:39:33 +0530 | [diff] [blame] | 50 | ["Unpaid", "eval:self.outstanding_amount > 0 and getdate(self.due_date) >= getdate(nowdate()) and self.docstatus==1"], |
| 51 | ["Overdue", "eval:self.outstanding_amount > 0 and getdate(self.due_date) < getdate(nowdate()) and self.docstatus==1"], |
| 52 | ["Cancelled", "eval:self.docstatus==2"], |
| 53 | ], |
| 54 | "Purchase Invoice": [ |
| 55 | ["Draft", None], |
| 56 | ["Submitted", "eval:self.docstatus==1"], |
Rohit Waghchaure | 2285f90 | 2016-11-09 14:16:58 +0530 | [diff] [blame] | 57 | ["Return", "eval:self.is_return==1 and self.docstatus==1"], |
| 58 | ["Debit Note Issued", "eval:self.outstanding_amount < 0 and self.docstatus==1"], |
| 59 | ["Paid", "eval:self.outstanding_amount==0 and self.docstatus==1 and self.is_return==0"], |
Rohit Waghchaure | 2f1db57 | 2016-11-08 12:39:33 +0530 | [diff] [blame] | 60 | ["Unpaid", "eval:self.outstanding_amount > 0 and getdate(self.due_date) >= getdate(nowdate()) and self.docstatus==1"], |
| 61 | ["Overdue", "eval:self.outstanding_amount > 0 and getdate(self.due_date) < getdate(nowdate()) and self.docstatus==1"], |
| 62 | ["Cancelled", "eval:self.docstatus==2"], |
| 63 | ], |
Rushabh Mehta | d10ba85 | 2015-10-02 12:42:48 +0530 | [diff] [blame] | 64 | "Purchase Order": [ |
| 65 | ["Draft", None], |
| 66 | ["To Receive and Bill", "eval:self.per_received < 100 and self.per_billed < 100 and self.docstatus == 1"], |
| 67 | ["To Bill", "eval:self.per_received == 100 and self.per_billed < 100 and self.docstatus == 1"], |
| 68 | ["To Receive", "eval:self.per_received < 100 and self.per_billed == 100 and self.docstatus == 1"], |
| 69 | ["Completed", "eval:self.per_received == 100 and self.per_billed == 100 and self.docstatus == 1"], |
Saurabh | 6d64fe3 | 2015-10-23 10:40:32 +0530 | [diff] [blame] | 70 | ["Delivered", "eval:self.status=='Delivered'"], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 71 | ["Cancelled", "eval:self.docstatus==2"], |
Saurabh | 98b2875 | 2015-10-19 15:16:17 +0530 | [diff] [blame] | 72 | ["Closed", "eval:self.status=='Closed'"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 73 | ], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 74 | "Delivery Note": [ |
| 75 | ["Draft", None], |
Nabin Hait | 5b13b99 | 2015-12-28 13:03:55 +0530 | [diff] [blame] | 76 | ["To Bill", "eval:self.per_billed < 100 and self.docstatus == 1"], |
| 77 | ["Completed", "eval:self.per_billed == 100 and self.docstatus == 1"], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 78 | ["Cancelled", "eval:self.docstatus==2"], |
Saurabh | a8a91cc | 2015-11-02 12:57:08 +0530 | [diff] [blame] | 79 | ["Closed", "eval:self.status=='Closed'"], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 80 | ], |
| 81 | "Purchase Receipt": [ |
| 82 | ["Draft", None], |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 83 | ["To Bill", "eval:self.per_billed < 100 and self.docstatus == 1"], |
| 84 | ["Completed", "eval:self.per_billed == 100 and self.docstatus == 1"], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 85 | ["Cancelled", "eval:self.docstatus==2"], |
Saurabh | a8a91cc | 2015-11-02 12:57:08 +0530 | [diff] [blame] | 86 | ["Closed", "eval:self.status=='Closed'"], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 87 | ] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Anand Doshi | 424c033 | 2014-04-21 15:06:56 +0530 | [diff] [blame] | 90 | class StatusUpdater(Document): |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 91 | """ |
| 92 | Updates the status of the calling records |
| 93 | Delivery Note: Update Delivered Qty, Update Percent and Validate over delivery |
| 94 | Sales Invoice: Update Billed Amt, Update Percent and Validate over billing |
| 95 | Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation |
| 96 | """ |
| 97 | |
| 98 | def update_prevdoc_status(self): |
| 99 | self.update_qty() |
| 100 | self.validate_qty() |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 101 | |
Saurabh | 70ed6ed | 2015-12-08 14:02:53 +0530 | [diff] [blame] | 102 | def set_status(self, update=False, status=None, update_modified=True): |
Rushabh Mehta | cb067aa | 2014-08-19 16:20:04 +0530 | [diff] [blame] | 103 | if self.is_new(): |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 104 | return |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 105 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 106 | if self.doctype in status_map: |
Nabin Hait | cabf9c5 | 2014-08-21 16:34:38 +0530 | [diff] [blame] | 107 | _status = self.status |
Rushabh Mehta | d48c239 | 2015-11-04 15:20:50 +0530 | [diff] [blame] | 108 | |
| 109 | if status and update: |
| 110 | self.db_set("status", status) |
| 111 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 112 | sl = status_map[self.doctype][:] |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 113 | sl.reverse() |
| 114 | for s in sl: |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 115 | if not s[1]: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 116 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 117 | break |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 118 | elif s[1].startswith("eval:"): |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 119 | if eval(s[1][5:]): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 120 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 121 | break |
| 122 | elif getattr(self, s[1])(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 123 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 124 | break |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 125 | |
Rushabh Mehta | c8b406d | 2015-06-02 12:17:14 +0530 | [diff] [blame] | 126 | if self.status != _status and self.status not in ("Submitted", "Cancelled"): |
Anand Doshi | 7b34ebe | 2015-01-07 15:41:32 +0530 | [diff] [blame] | 127 | self.add_comment("Label", _(self.status)) |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 128 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 129 | if update: |
Rushabh Mehta | fc8e589 | 2016-07-20 12:08:47 +0530 | [diff] [blame] | 130 | self.db_set('status', self.status, update_modified = update_modified) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 131 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 132 | def validate_qty(self): |
Rushabh Mehta | a9ba5ef | 2014-12-19 16:20:32 +0530 | [diff] [blame] | 133 | """Validates qty at row level""" |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 134 | self.tolerance = {} |
| 135 | self.global_tolerance = None |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 136 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 137 | for args in self.status_updater: |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 138 | if "target_ref_field" not in args: |
| 139 | # if target_ref_field is not specified, the programmer does not want to validate qty / amount |
| 140 | continue |
| 141 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 142 | # get unique transactions to update |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 143 | for d in self.get_all_children(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 144 | if d.doctype == args['source_dt'] and d.get(args["join_field"]): |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 145 | args['name'] = d.get(args['join_field']) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 146 | |
| 147 | # get all qty where qty > target_field |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 148 | item = frappe.db.sql("""select item_code, `{target_ref_field}`, |
| 149 | `{target_field}`, parenttype, parent from `tab{target_dt}` |
| 150 | where `{target_ref_field}` < `{target_field}` |
| 151 | and name=%s and docstatus=1""".format(**args), |
Nabin Hait | 4d713ac | 2014-03-03 15:51:13 +0530 | [diff] [blame] | 152 | args['name'], as_dict=1) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 153 | if item: |
| 154 | item = item[0] |
| 155 | item['idx'] = d.idx |
| 156 | item['target_ref_field'] = args['target_ref_field'].replace('_', ' ') |
| 157 | |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 158 | # if not item[args['target_ref_field']]: |
| 159 | # msgprint(_("Note: System will not check over-delivery and over-booking for Item {0} as quantity or amount is 0").format(item.item_code)) |
| 160 | if args.get('no_tolerance'): |
nabinhait | d5fb5d9 | 2014-07-09 17:36:38 +0530 | [diff] [blame] | 161 | item['reduce_by'] = item[args['target_field']] - item[args['target_ref_field']] |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 162 | if item['reduce_by'] > .01: |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 163 | self.limits_crossed_error(args, item) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 164 | |
Nabin Hait | 47a3f63 | 2016-08-26 11:39:23 +0530 | [diff] [blame] | 165 | elif item[args['target_ref_field']]: |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 166 | self.check_overflow_with_tolerance(item, args) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 167 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 168 | def check_overflow_with_tolerance(self, item, args): |
| 169 | """ |
| 170 | Checks if there is overflow condering a relaxation tolerance |
| 171 | """ |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 172 | # check if overflow is within tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 173 | tolerance, self.tolerance, self.global_tolerance = get_tolerance_for(item['item_code'], |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 174 | self.tolerance, self.global_tolerance) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 175 | overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) / |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 176 | item[args['target_ref_field']]) * 100 |
nabinhait | 711e8be | 2014-07-19 17:00:15 +0530 | [diff] [blame] | 177 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 178 | if overflow_percent - tolerance > 0.01: |
| 179 | item['max_allowed'] = flt(item[args['target_ref_field']] * (100+tolerance)/100) |
| 180 | item['reduce_by'] = item[args['target_field']] - item['max_allowed'] |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 181 | |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 182 | self.limits_crossed_error(args, item) |
| 183 | |
| 184 | def limits_crossed_error(self, args, item): |
| 185 | '''Raise exception for limits crossed''' |
| 186 | frappe.throw(_('This document is over limit by {0} {1} for item {4}. Are you making another {3} against the same {2}?') |
| 187 | .format( |
| 188 | frappe.bold(_(item["target_ref_field"].title())), |
| 189 | frappe.bold(item["reduce_by"]), |
| 190 | frappe.bold(_(args.get('target_dt'))), |
| 191 | frappe.bold(_(self.doctype)), |
| 192 | frappe.bold(item.get('item_code')) |
| 193 | ) + '<br><br>' + |
| 194 | _('To allow over-billing or over-ordering, update "Allowance" in Stock Settings or the Item.'), |
| 195 | title = _('Limit Crossed')) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 196 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 197 | def update_qty(self, update_modified=True): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 198 | """Updates qty or amount at row level |
| 199 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 200 | :param update_modified: If true, updates `modified` and `modified_by` for target parent doc |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 201 | """ |
| 202 | for args in self.status_updater: |
| 203 | # condition to include current record (if submit or no if cancel) |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 204 | if self.docstatus == 1: |
| 205 | args['cond'] = ' or parent="%s"' % self.name.replace('"', '\"') |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 206 | else: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 207 | args['cond'] = ' and parent!="%s"' % self.name.replace('"', '\"') |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 208 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 209 | self._update_children(args, update_modified) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 210 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 211 | if "percent_join_field" in args: |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 212 | self._update_percent_field_in_targets(args, update_modified) |
ankitjavalkarwork | 9aadb0d | 2014-10-07 17:38:46 +0530 | [diff] [blame] | 213 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 214 | def _update_children(self, args, update_modified): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 215 | """Update quantities or amount in child table""" |
| 216 | for d in self.get_all_children(): |
| 217 | if d.doctype != args['source_dt']: |
| 218 | continue |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 219 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 220 | self._update_modified(args, update_modified) |
| 221 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 222 | # updates qty in the child table |
| 223 | args['detail_id'] = d.get(args['join_field']) |
ankitjavalkarwork | 9aadb0d | 2014-10-07 17:38:46 +0530 | [diff] [blame] | 224 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 225 | args['second_source_condition'] = "" |
| 226 | if args.get('second_source_dt') and args.get('second_source_field') \ |
| 227 | and args.get('second_join_field'): |
| 228 | if not args.get("second_source_extra_cond"): |
| 229 | args["second_source_extra_cond"] = "" |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 230 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 231 | args['second_source_condition'] = """ + ifnull((select sum(%(second_source_field)s) |
| 232 | from `tab%(second_source_dt)s` |
| 233 | where `%(second_join_field)s`="%(detail_id)s" |
| 234 | 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] | 235 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 236 | if args['detail_id']: |
| 237 | if not args.get("extra_cond"): args["extra_cond"] = "" |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 238 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 239 | frappe.db.sql("""update `tab%(target_dt)s` |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 240 | set %(target_field)s = ( |
| 241 | (select ifnull(sum(%(source_field)s), 0) |
| 242 | from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s" |
| 243 | and (docstatus=1 %(cond)s) %(extra_cond)s) |
| 244 | %(second_source_condition)s |
| 245 | ) |
| 246 | %(update_modified)s |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 247 | where name='%(detail_id)s'""" % args) |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 248 | |
| 249 | def _update_percent_field_in_targets(self, args, update_modified=True): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 250 | """Update percent field in parent transaction""" |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 251 | distinct_transactions = set([d.get(args['percent_join_field']) |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 252 | for d in self.get_all_children(args['source_dt'])]) |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 253 | |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 254 | for name in distinct_transactions: |
| 255 | if name: |
| 256 | args['name'] = name |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 257 | self._update_percent_field(args, update_modified) |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 258 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 259 | def _update_percent_field(self, args, update_modified=True): |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 260 | """Update percent field in parent transaction""" |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 261 | |
| 262 | self._update_modified(args, update_modified) |
| 263 | |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 264 | if args.get('target_parent_field'): |
| 265 | frappe.db.sql("""update `tab%(target_parent_dt)s` |
| 266 | set %(target_parent_field)s = round( |
| 267 | ifnull((select |
RobertSchouten | db33ebb | 2016-09-12 14:54:46 +0800 | [diff] [blame] | 268 | ifnull(sum(if(%(target_ref_field)s > %(target_field)s, abs(%(target_field)s), abs(%(target_ref_field)s))), 0) |
| 269 | / sum(abs(%(target_ref_field)s)) * 100 |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 270 | from `tab%(target_dt)s` where parent="%(name)s"), 0), 2) |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 271 | %(update_modified)s |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 272 | where name='%(name)s'""" % args) |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 273 | |
rohitwaghchaure | 7be942d | 2016-11-16 11:14:33 +0530 | [diff] [blame] | 274 | # update field |
| 275 | if args.get('status_field'): |
| 276 | frappe.db.sql("""update `tab%(target_parent_dt)s` |
| 277 | set %(status_field)s = if(%(target_parent_field)s<0.001, |
| 278 | 'Not %(keyword)s', if(%(target_parent_field)s>=99.99, |
| 279 | 'Fully %(keyword)s', 'Partly %(keyword)s')) |
| 280 | where name='%(name)s'""" % args) |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 281 | |
rohitwaghchaure | 7be942d | 2016-11-16 11:14:33 +0530 | [diff] [blame] | 282 | if update_modified: |
| 283 | target = frappe.get_doc(args["target_parent_dt"], args["name"]) |
| 284 | target.set_status(update=True) |
| 285 | target.notify_update() |
| 286 | notify_status(target) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 287 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 288 | def _update_modified(self, args, update_modified): |
| 289 | args['update_modified'] = '' |
| 290 | if update_modified: |
| 291 | args['update_modified'] = ', modified = now(), modified_by = "{0}"'\ |
| 292 | .format(frappe.db.escape(frappe.session.user)) |
| 293 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 294 | def update_billing_status_for_zero_amount_refdoc(self, ref_dt): |
| 295 | ref_fieldname = ref_dt.lower().replace(" ", "_") |
| 296 | zero_amount_refdoc = [] |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 297 | 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] | 298 | where docstatus=1 and base_net_total = 0""" % ref_dt) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 299 | |
Nabin Hait | 4b8185d | 2014-12-25 18:19:39 +0530 | [diff] [blame] | 300 | for item in self.get("items"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 301 | if item.get(ref_fieldname) \ |
| 302 | and item.get(ref_fieldname) in all_zero_amount_refdoc \ |
| 303 | and item.get(ref_fieldname) not in zero_amount_refdoc: |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 304 | zero_amount_refdoc.append(item.get(ref_fieldname)) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 305 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 306 | if zero_amount_refdoc: |
Rushabh Mehta | d48c239 | 2015-11-04 15:20:50 +0530 | [diff] [blame] | 307 | self.update_billing_status(zero_amount_refdoc, ref_dt, ref_fieldname) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 308 | |
Rushabh Mehta | d48c239 | 2015-11-04 15:20:50 +0530 | [diff] [blame] | 309 | def update_billing_status(self, zero_amount_refdoc, ref_dt, ref_fieldname): |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 310 | for ref_dn in zero_amount_refdoc: |
Anand Doshi | ebae726 | 2015-11-25 15:51:01 +0530 | [diff] [blame] | 311 | ref_doc_qty = flt(frappe.db.sql("""select ifnull(sum(qty), 0) from `tab%s Item` |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 312 | where parent=%s""" % (ref_dt, '%s'), (ref_dn))[0][0]) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 313 | |
Anand Doshi | ebae726 | 2015-11-25 15:51:01 +0530 | [diff] [blame] | 314 | billed_qty = flt(frappe.db.sql("""select ifnull(sum(qty), 0) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 315 | from `tab%s Item` where %s=%s and docstatus=1""" % |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 316 | (self.doctype, ref_fieldname, '%s'), (ref_dn))[0][0]) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 317 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 318 | per_billed = ((ref_doc_qty if billed_qty > ref_doc_qty else billed_qty)\ |
| 319 | / ref_doc_qty)*100 |
Rushabh Mehta | fc8e589 | 2016-07-20 12:08:47 +0530 | [diff] [blame] | 320 | |
| 321 | ref_doc = frappe.get_doc(ref_dt, ref_dn) |
| 322 | |
| 323 | ref_doc.db_set("per_billed", per_billed) |
Rohit Waghchaure | 770d04e | 2016-10-05 23:02:15 +0530 | [diff] [blame] | 324 | ref_doc.set_status(update=True) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 325 | |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 326 | def get_tolerance_for(item_code, item_tolerance={}, global_tolerance=None): |
| 327 | """ |
| 328 | Returns the tolerance for the item, if not set, returns global tolerance |
| 329 | """ |
| 330 | if item_tolerance.get(item_code): |
| 331 | return item_tolerance[item_code], item_tolerance, global_tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 332 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 333 | tolerance = flt(frappe.db.get_value('Item',item_code,'tolerance') or 0) |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 334 | |
| 335 | if not tolerance: |
| 336 | if global_tolerance == None: |
nabinhait | b1c5738 | 2014-07-19 16:53:45 +0530 | [diff] [blame] | 337 | global_tolerance = flt(frappe.db.get_value('Stock Settings', None, 'tolerance')) |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 338 | tolerance = global_tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 339 | |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 340 | item_tolerance[item_code] = tolerance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 341 | return tolerance, item_tolerance, global_tolerance |