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