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