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