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 |
ruthra kumar | a0fc685 | 2023-08-14 17:38:44 +0530 | [diff] [blame] | 8 | from frappe.utils import comma_or, flt, get_link_to_form, getdate, now, nowdate |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 10 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 11 | class OverAllowanceError(frappe.ValidationError): |
| 12 | pass |
| 13 | |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 14 | |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 15 | def validate_status(status, options): |
| 16 | if status not in options: |
| 17 | frappe.throw(_("Status must be one of {0}").format(comma_or(options))) |
| 18 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 19 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 20 | status_map = { |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 21 | "Lead": [ |
Nabin Hait | f49bbe1 | 2016-12-15 11:51:54 +0530 | [diff] [blame] | 22 | ["Lost Quotation", "has_lost_quotation"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 23 | ["Opportunity", "has_opportunity"], |
Alec Ruiz-Ramon | fcaaa29 | 2016-06-09 14:19:51 -0400 | [diff] [blame] | 24 | ["Quotation", "has_quotation"], |
| 25 | ["Converted", "has_customer"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 26 | ], |
| 27 | "Opportunity": [ |
Valmik Jangla | 2956327 | 2016-03-29 11:13:07 +0530 | [diff] [blame] | 28 | ["Lost", "eval:self.status=='Lost'"], |
Alec Ruiz-Ramon | fcaaa29 | 2016-06-09 14:19:51 -0400 | [diff] [blame] | 29 | ["Lost", "has_lost_quotation"], |
Nabin Hait | 6a5cf67 | 2017-05-30 15:34:20 +0530 | [diff] [blame] | 30 | ["Quotation", "has_active_quotation"], |
| 31 | ["Converted", "has_ordered_quotation"], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 32 | ["Closed", "eval:self.status=='Closed'"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 33 | ], |
| 34 | "Quotation": [ |
Rushabh Mehta | 4ac3094 | 2015-04-13 16:56:03 +0530 | [diff] [blame] | 35 | ["Draft", None], |
Deepesh Garg | ea05746 | 2019-06-26 11:05:51 +0530 | [diff] [blame] | 36 | ["Open", "eval:self.docstatus==1"], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 37 | ["Lost", "eval:self.status=='Lost'"], |
Deepesh Garg | 118c786 | 2022-06-11 21:55:59 +0530 | [diff] [blame] | 38 | ["Partially Ordered", "is_partially_ordered"], |
| 39 | ["Ordered", "is_fully_ordered"], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 40 | ["Cancelled", "eval:self.docstatus==2"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 41 | ], |
| 42 | "Sales Order": [ |
| 43 | ["Draft", None], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 44 | [ |
| 45 | "To Deliver and Bill", |
| 46 | "eval:self.per_delivered < 100 and self.per_billed < 100 and self.docstatus == 1", |
| 47 | ], |
| 48 | [ |
| 49 | "To Bill", |
Gursheen Anand | d69b0d7 | 2023-10-25 13:59:37 +0530 | [diff] [blame] | 50 | "eval:(self.per_delivered >= 100 or self.skip_delivery_note) and self.per_billed < 100 and self.docstatus == 1", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 51 | ], |
| 52 | [ |
| 53 | "To Deliver", |
Gursheen Anand | d69b0d7 | 2023-10-25 13:59:37 +0530 | [diff] [blame] | 54 | "eval:self.per_delivered < 100 and self.per_billed >= 100 and self.docstatus == 1 and not self.skip_delivery_note", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 55 | ], |
| 56 | [ |
David Arnold | 8b21ca2 | 2023-10-14 16:34:18 +0200 | [diff] [blame] | 57 | "To Pay", |
| 58 | "eval:self.advance_payment_status == 'Requested' and self.docstatus == 1", |
| 59 | ], |
| 60 | [ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 61 | "Completed", |
Gursheen Anand | d69b0d7 | 2023-10-25 13:59:37 +0530 | [diff] [blame] | 62 | "eval:(self.per_delivered >= 100 or self.skip_delivery_note) and self.per_billed >= 100 and self.docstatus == 1", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 63 | ], |
Rushabh Mehta | d10ba85 | 2015-10-02 12:42:48 +0530 | [diff] [blame] | 64 | ["Cancelled", "eval:self.docstatus==2"], |
Deepesh Garg | 597ef0a | 2023-01-20 19:44:06 +0530 | [diff] [blame] | 65 | ["Closed", "eval:self.status=='Closed' and self.docstatus != 2"], |
Mangesh-Khairnar | 5eb66ae | 2019-03-01 16:23:27 +0530 | [diff] [blame] | 66 | ["On Hold", "eval:self.status=='On Hold'"], |
Rushabh Mehta | d10ba85 | 2015-10-02 12:42:48 +0530 | [diff] [blame] | 67 | ], |
| 68 | "Purchase Order": [ |
| 69 | ["Draft", None], |
KanchanChauhan | d114c8f | 2019-09-05 15:11:43 +0530 | [diff] [blame] | 70 | ["To Bill", "eval:self.per_received >= 100 and self.per_billed < 100 and self.docstatus == 1"], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 71 | [ |
| 72 | "To Receive", |
| 73 | "eval:self.per_received < 100 and self.per_billed == 100 and self.docstatus == 1", |
| 74 | ], |
| 75 | [ |
David Arnold | 8b21ca2 | 2023-10-14 16:34:18 +0200 | [diff] [blame] | 76 | "To Receive and Bill", |
| 77 | "eval:self.per_received < 100 and self.per_billed < 100 and self.docstatus == 1", |
| 78 | ], |
| 79 | [ |
| 80 | "To Pay", |
| 81 | "eval:self.advance_payment_status == 'Initiated' and self.docstatus == 1", |
| 82 | ], |
| 83 | [ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 84 | "Completed", |
| 85 | "eval:self.per_received >= 100 and self.per_billed == 100 and self.docstatus == 1", |
| 86 | ], |
Saurabh | 6d64fe3 | 2015-10-23 10:40:32 +0530 | [diff] [blame] | 87 | ["Delivered", "eval:self.status=='Delivered'"], |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 88 | ["Cancelled", "eval:self.docstatus==2"], |
Mangesh-Khairnar | 439546c | 2019-03-11 16:40:27 +0530 | [diff] [blame] | 89 | ["On Hold", "eval:self.status=='On Hold'"], |
Deepesh Garg | 597ef0a | 2023-01-20 19:44:06 +0530 | [diff] [blame] | 90 | ["Closed", "eval:self.status=='Closed' and self.docstatus != 2"], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 91 | ], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 92 | "Delivery Note": [ |
| 93 | ["Draft", None], |
Nabin Hait | 5b13b99 | 2015-12-28 13:03:55 +0530 | [diff] [blame] | 94 | ["To Bill", "eval:self.per_billed < 100 and self.docstatus == 1"], |
marination | ba37fe7 | 2020-07-31 20:01:06 +0530 | [diff] [blame] | 95 | ["Return Issued", "eval:self.per_returned == 100 and self.docstatus == 1"], |
Nabin Hait | 5b13b99 | 2015-12-28 13:03:55 +0530 | [diff] [blame] | 96 | ["Completed", "eval:self.per_billed == 100 and self.docstatus == 1"], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 97 | ["Cancelled", "eval:self.docstatus==2"], |
Deepesh Garg | 597ef0a | 2023-01-20 19:44:06 +0530 | [diff] [blame] | 98 | ["Closed", "eval:self.status=='Closed' and self.docstatus != 2"], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 99 | ], |
| 100 | "Purchase Receipt": [ |
| 101 | ["Draft", None], |
Deepesh Garg | a673220 | 2024-01-29 19:56:46 +0530 | [diff] [blame] | 102 | ["To Bill", "eval:self.per_billed == 0 and self.docstatus == 1"], |
| 103 | ["Partly Billed", "eval:self.per_billed > 0 and self.per_billed < 100 and self.docstatus == 1"], |
marination | 66069df | 2020-07-31 15:54:05 +0530 | [diff] [blame] | 104 | ["Return Issued", "eval:self.per_returned == 100 and self.docstatus == 1"], |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 105 | ["Completed", "eval:self.per_billed == 100 and self.docstatus == 1"], |
Rushabh Mehta | 9dd8aab | 2015-05-18 11:18:55 +0530 | [diff] [blame] | 106 | ["Cancelled", "eval:self.docstatus==2"], |
Deepesh Garg | 597ef0a | 2023-01-20 19:44:06 +0530 | [diff] [blame] | 107 | ["Closed", "eval:self.status=='Closed' and self.docstatus != 2"], |
tundebabzy | 99b734b | 2017-06-07 07:32:07 +0100 | [diff] [blame] | 108 | ], |
| 109 | "Material Request": [ |
| 110 | ["Draft", None], |
| 111 | ["Stopped", "eval:self.status == 'Stopped'"], |
| 112 | ["Cancelled", "eval:self.docstatus == 2"], |
| 113 | ["Pending", "eval:self.status != 'Stopped' and self.per_ordered == 0 and self.docstatus == 1"], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 114 | [ |
| 115 | "Ordered", |
| 116 | "eval:self.status != 'Stopped' and self.per_ordered == 100 and self.docstatus == 1 and self.material_request_type == 'Purchase'", |
| 117 | ], |
| 118 | [ |
| 119 | "Transferred", |
| 120 | "eval:self.status != 'Stopped' and self.per_ordered == 100 and self.docstatus == 1 and self.material_request_type == 'Material Transfer'", |
| 121 | ], |
| 122 | [ |
| 123 | "Issued", |
| 124 | "eval:self.status != 'Stopped' and self.per_ordered == 100 and self.docstatus == 1 and self.material_request_type == 'Material Issue'", |
| 125 | ], |
| 126 | [ |
| 127 | "Received", |
| 128 | "eval:self.status != 'Stopped' and self.per_received == 100 and self.docstatus == 1 and self.material_request_type == 'Purchase'", |
| 129 | ], |
| 130 | [ |
| 131 | "Partially Received", |
| 132 | "eval:self.status != 'Stopped' and self.per_received > 0 and self.per_received < 100 and self.docstatus == 1 and self.material_request_type == 'Purchase'", |
| 133 | ], |
| 134 | [ |
| 135 | "Partially Ordered", |
| 136 | "eval:self.status != 'Stopped' and self.per_ordered < 100 and self.per_ordered > 0 and self.docstatus == 1", |
| 137 | ], |
| 138 | [ |
| 139 | "Manufactured", |
| 140 | "eval:self.status != 'Stopped' and self.per_ordered == 100 and self.docstatus == 1 and self.material_request_type == 'Manufacture'", |
| 141 | ], |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 142 | ], |
Saqib | a6f98d4 | 2020-07-23 18:51:26 +0530 | [diff] [blame] | 143 | "POS Opening Entry": [ |
| 144 | ["Draft", None], |
| 145 | ["Open", "eval:self.docstatus == 1 and not self.pos_closing_entry"], |
| 146 | ["Closed", "eval:self.docstatus == 1 and self.pos_closing_entry"], |
| 147 | ["Cancelled", "eval:self.docstatus == 2"], |
Saqib | 675a833 | 2021-01-28 18:42:43 +0530 | [diff] [blame] | 148 | ], |
| 149 | "POS Closing Entry": [ |
| 150 | ["Draft", None], |
| 151 | ["Submitted", "eval:self.docstatus == 1"], |
| 152 | ["Queued", "eval:self.status == 'Queued'"], |
Saqib | 900a8fb | 2021-05-06 17:02:47 +0530 | [diff] [blame] | 153 | ["Failed", "eval:self.status == 'Failed'"], |
Saqib | 675a833 | 2021-01-28 18:42:43 +0530 | [diff] [blame] | 154 | ["Cancelled", "eval:self.docstatus == 2"], |
Ganga Manoj | f2eb8dd | 2021-05-10 14:02:58 +0530 | [diff] [blame] | 155 | ], |
| 156 | "Transaction Deletion Record": [ |
| 157 | ["Draft", None], |
| 158 | ["Completed", "eval:self.docstatus == 1"], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | ], |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 160 | } |
| 161 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 162 | |
Anand Doshi | 424c033 | 2014-04-21 15:06:56 +0530 | [diff] [blame] | 163 | class StatusUpdater(Document): |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 164 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 165 | Updates the status of the calling records |
| 166 | Delivery Note: Update Delivered Qty, Update Percent and Validate over delivery |
| 167 | Sales Invoice: Update Billed Amt, Update Percent and Validate over billing |
| 168 | Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 169 | """ |
| 170 | |
| 171 | def update_prevdoc_status(self): |
| 172 | self.update_qty() |
| 173 | self.validate_qty() |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 174 | |
Saurabh | 70ed6ed | 2015-12-08 14:02:53 +0530 | [diff] [blame] | 175 | def set_status(self, update=False, status=None, update_modified=True): |
Rushabh Mehta | cb067aa | 2014-08-19 16:20:04 +0530 | [diff] [blame] | 176 | if self.is_new(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 177 | if self.get("amended_from"): |
| 178 | self.status = "Draft" |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 179 | return |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 180 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 181 | if self.doctype in status_map: |
Nabin Hait | cabf9c5 | 2014-08-21 16:34:38 +0530 | [diff] [blame] | 182 | _status = self.status |
Rushabh Mehta | d48c239 | 2015-11-04 15:20:50 +0530 | [diff] [blame] | 183 | if status and update: |
| 184 | self.db_set("status", status) |
| 185 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 186 | sl = status_map[self.doctype][:] |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 187 | sl.reverse() |
| 188 | for s in sl: |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 189 | if not s[1]: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 190 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 191 | break |
Rushabh Mehta | 6856d74 | 2013-10-03 18:12:36 +0530 | [diff] [blame] | 192 | elif s[1].startswith("eval:"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 193 | if frappe.safe_eval( |
| 194 | s[1][5:], |
| 195 | None, |
| 196 | { |
| 197 | "self": self.as_dict(), |
| 198 | "getdate": getdate, |
| 199 | "nowdate": nowdate, |
| 200 | "get_value": frappe.db.get_value, |
| 201 | }, |
| 202 | ): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 203 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 204 | break |
| 205 | elif getattr(self, s[1])(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 206 | self.status = s[0] |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 207 | break |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 208 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 209 | if self.status != _status and self.status not in ( |
| 210 | "Cancelled", |
| 211 | "Partially Ordered", |
| 212 | "Ordered", |
| 213 | "Issued", |
| 214 | "Transferred", |
| 215 | ): |
Anand Doshi | 7b34ebe | 2015-01-07 15:41:32 +0530 | [diff] [blame] | 216 | self.add_comment("Label", _(self.status)) |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 217 | |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 218 | if update: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 219 | self.db_set("status", self.status, update_modified=update_modified) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 220 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 221 | def validate_qty(self): |
Rushabh Mehta | a9ba5ef | 2014-12-19 16:20:32 +0530 | [diff] [blame] | 222 | """Validates qty at row level""" |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 223 | self.item_allowance = {} |
| 224 | self.global_qty_allowance = None |
| 225 | self.global_amount_allowance = None |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 226 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 227 | for args in self.status_updater: |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 228 | if "target_ref_field" not in args: |
| 229 | # if target_ref_field is not specified, the programmer does not want to validate qty / amount |
| 230 | continue |
| 231 | |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 232 | # get unique transactions to update |
Rushabh Mehta | f191f85 | 2014-04-02 18:09:34 +0530 | [diff] [blame] | 233 | for d in self.get_all_children(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 234 | if hasattr(d, "qty") and d.qty < 0 and not self.get("is_return"): |
rohitwaghchaure | 12aa426 | 2018-03-12 11:20:30 +0530 | [diff] [blame] | 235 | frappe.throw(_("For an item {0}, quantity must be positive number").format(d.item_code)) |
| 236 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 237 | if hasattr(d, "qty") and d.qty > 0 and self.get("is_return"): |
rohitwaghchaure | 7048925 | 2018-06-11 12:02:14 +0530 | [diff] [blame] | 238 | frappe.throw(_("For an item {0}, quantity must be negative number").format(d.item_code)) |
| 239 | |
ruthra kumar | a0fc685 | 2023-08-14 17:38:44 +0530 | [diff] [blame] | 240 | if not frappe.db.get_single_value("Selling Settings", "allow_negative_rates_for_items"): |
| 241 | if hasattr(d, "item_code") and hasattr(d, "rate") and flt(d.rate) < 0: |
| 242 | frappe.throw( |
| 243 | _( |
| 244 | "For item {0}, rate must be a positive number. To Allow negative rates, enable {1} in {2}" |
| 245 | ).format( |
| 246 | frappe.bold(d.item_code), |
| 247 | frappe.bold(_("`Allow Negative rates for Items`")), |
| 248 | get_link_to_form("Selling Settings", "Selling Settings"), |
| 249 | ), |
| 250 | ) |
Devin Slauenwhite | dedf24b | 2023-08-02 06:56:55 -0400 | [diff] [blame] | 251 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 252 | if d.doctype == args["source_dt"] and d.get(args["join_field"]): |
| 253 | args["name"] = d.get(args["join_field"]) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 254 | |
| 255 | # get all qty where qty > target_field |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 256 | item = frappe.db.sql( |
| 257 | """select item_code, `{target_ref_field}`, |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 258 | `{target_field}`, parenttype, parent from `tab{target_dt}` |
| 259 | where `{target_ref_field}` < `{target_field}` |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 260 | and name=%s and docstatus=1""".format(**args), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 261 | args["name"], |
| 262 | as_dict=1, |
| 263 | ) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 264 | if item: |
| 265 | item = item[0] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 266 | item["idx"] = d.idx |
| 267 | item["target_ref_field"] = args["target_ref_field"].replace("_", " ") |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 268 | |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 269 | # if not item[args['target_ref_field']]: |
| 270 | # msgprint(_("Note: System will not check over-delivery and over-booking for Item {0} as quantity or amount is 0").format(item.item_code)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 271 | if args.get("no_allowance"): |
| 272 | item["reduce_by"] = item[args["target_field"]] - item[args["target_ref_field"]] |
| 273 | if item["reduce_by"] > 0.01: |
Marica | 4e6b3ad | 2020-01-15 19:22:35 +0530 | [diff] [blame] | 274 | self.limits_crossed_error(args, item, "qty") |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 275 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 276 | elif item[args["target_ref_field"]]: |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 277 | self.check_overflow_with_allowance(item, args) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 278 | |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 279 | def check_overflow_with_allowance(self, item, args): |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 280 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 281 | Checks if there is overflow condering a relaxation allowance |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 282 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 283 | qty_or_amount = "qty" if "qty" in args["target_ref_field"] else "amount" |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 284 | |
| 285 | # check if overflow is within allowance |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 286 | ( |
| 287 | allowance, |
| 288 | self.item_allowance, |
| 289 | self.global_qty_allowance, |
| 290 | self.global_amount_allowance, |
| 291 | ) = get_allowance_for( |
| 292 | item["item_code"], |
| 293 | self.item_allowance, |
| 294 | self.global_qty_allowance, |
| 295 | self.global_amount_allowance, |
| 296 | qty_or_amount, |
| 297 | ) |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 298 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 299 | role_allowed_to_over_deliver_receive = frappe.db.get_single_value( |
| 300 | "Stock Settings", "role_allowed_to_over_deliver_receive" |
| 301 | ) |
| 302 | role_allowed_to_over_bill = frappe.db.get_single_value( |
| 303 | "Accounts Settings", "role_allowed_to_over_bill" |
| 304 | ) |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 305 | 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] | 306 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 307 | overflow_percent = ( |
| 308 | (item[args["target_field"]] - item[args["target_ref_field"]]) / item[args["target_ref_field"]] |
| 309 | ) * 100 |
Deepesh Garg | cb718fc | 2021-04-19 13:25:15 +0530 | [diff] [blame] | 310 | |
Ankush Menat | 6ec047c | 2021-10-27 10:30:05 +0530 | [diff] [blame] | 311 | if overflow_percent - allowance > 0.01: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 312 | item["max_allowed"] = flt(item[args["target_ref_field"]] * (100 + allowance) / 100) |
| 313 | item["reduce_by"] = item[args["target_field"]] - item["max_allowed"] |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 314 | |
Ankush Menat | 6ec047c | 2021-10-27 10:30:05 +0530 | [diff] [blame] | 315 | if role not in frappe.get_roles(): |
| 316 | self.limits_crossed_error(args, item, qty_or_amount) |
| 317 | else: |
| 318 | self.warn_about_bypassing_with_role(item, qty_or_amount, role) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 319 | |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 320 | def limits_crossed_error(self, args, item, qty_or_amount): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 321 | """Raise exception for limits crossed""" |
Rohit Waghchaure | b4a102d | 2022-09-06 16:22:00 +0530 | [diff] [blame] | 322 | if ( |
| 323 | self.doctype in ["Sales Invoice", "Delivery Note"] |
| 324 | and qty_or_amount == "amount" |
| 325 | and self.is_internal_customer |
| 326 | ): |
| 327 | return |
| 328 | |
| 329 | elif ( |
| 330 | self.doctype in ["Purchase Invoice", "Purchase Receipt"] |
| 331 | and qty_or_amount == "amount" |
| 332 | and self.is_internal_supplier |
| 333 | ): |
| 334 | return |
| 335 | |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 336 | if qty_or_amount == "qty": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 337 | action_msg = _( |
| 338 | 'To allow over receipt / delivery, update "Over Receipt/Delivery Allowance" in Stock Settings or the Item.' |
| 339 | ) |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 340 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 341 | action_msg = _( |
| 342 | 'To allow over billing, update "Over Billing Allowance" in Accounts Settings or the Item.' |
| 343 | ) |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 344 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 345 | frappe.throw( |
| 346 | _( |
| 347 | "This document is over limit by {0} {1} for item {4}. Are you making another {3} against the same {2}?" |
| 348 | ).format( |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 349 | frappe.bold(_(item["target_ref_field"].title())), |
| 350 | frappe.bold(item["reduce_by"]), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 351 | frappe.bold(_(args.get("target_dt"))), |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 352 | frappe.bold(_(self.doctype)), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 353 | frappe.bold(item.get("item_code")), |
| 354 | ) |
| 355 | + "<br><br>" |
| 356 | + action_msg, |
| 357 | OverAllowanceError, |
| 358 | title=_("Limit Crossed"), |
| 359 | ) |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 360 | |
Ankush Menat | 6ec047c | 2021-10-27 10:30:05 +0530 | [diff] [blame] | 361 | def warn_about_bypassing_with_role(self, item, qty_or_amount, role): |
barredterra | 36997d9 | 2022-12-13 18:59:20 +0100 | [diff] [blame] | 362 | if qty_or_amount == "qty": |
| 363 | msg = _("Over Receipt/Delivery of {0} {1} ignored for item {2} because you have {3} role.") |
| 364 | else: |
| 365 | msg = _("Overbilling of {0} {1} ignored for item {2} because you have {3} role.") |
Ankush Menat | 6ec047c | 2021-10-27 10:30:05 +0530 | [diff] [blame] | 366 | |
barredterra | 36997d9 | 2022-12-13 18:59:20 +0100 | [diff] [blame] | 367 | frappe.msgprint( |
| 368 | msg.format( |
| 369 | _(item["target_ref_field"].title()), |
| 370 | frappe.bold(item["reduce_by"]), |
| 371 | frappe.bold(item.get("item_code")), |
| 372 | role, |
| 373 | ), |
| 374 | indicator="orange", |
| 375 | alert=True, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 376 | ) |
Ankush Menat | 6ec047c | 2021-10-27 10:30:05 +0530 | [diff] [blame] | 377 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 378 | def update_qty(self, update_modified=True): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 379 | """Updates qty or amount at row level |
| 380 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 381 | :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] | 382 | """ |
| 383 | for args in self.status_updater: |
| 384 | # condition to include current record (if submit or no if cancel) |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 385 | if self.docstatus == 1: |
Conor | 74a782d | 2022-06-17 06:31:27 -0500 | [diff] [blame] | 386 | args["cond"] = " or parent='%s'" % self.name.replace('"', '"') |
Nabin Hait | 0feebc1 | 2013-06-03 16:45:38 +0530 | [diff] [blame] | 387 | else: |
Conor | 74a782d | 2022-06-17 06:31:27 -0500 | [diff] [blame] | 388 | args["cond"] = " and parent!='%s'" % self.name.replace('"', '"') |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 389 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 390 | self._update_children(args, update_modified) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 391 | |
marination | 66069df | 2020-07-31 15:54:05 +0530 | [diff] [blame] | 392 | 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] | 393 | self._update_percent_field_in_targets(args, update_modified) |
ankitjavalkarwork | 9aadb0d | 2014-10-07 17:38:46 +0530 | [diff] [blame] | 394 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 395 | def _update_children(self, args, update_modified): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 396 | """Update quantities or amount in child table""" |
| 397 | for d in self.get_all_children(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 398 | if d.doctype != args["source_dt"]: |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 399 | continue |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 400 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 401 | self._update_modified(args, update_modified) |
| 402 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 403 | # updates qty in the child table |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 404 | args["detail_id"] = d.get(args["join_field"]) |
ankitjavalkarwork | 9aadb0d | 2014-10-07 17:38:46 +0530 | [diff] [blame] | 405 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 406 | args["second_source_condition"] = "" |
| 407 | if ( |
| 408 | args.get("second_source_dt") |
| 409 | and args.get("second_source_field") |
| 410 | and args.get("second_join_field") |
| 411 | ): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 412 | if not args.get("second_source_extra_cond"): |
| 413 | args["second_source_extra_cond"] = "" |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 414 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 415 | args["second_source_condition"] = frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 416 | """ select ifnull((select sum({second_source_field}) |
| 417 | from `tab{second_source_dt}` |
| 418 | where `{second_join_field}`='{detail_id}' |
| 419 | and (`tab{second_source_dt}`.docstatus=1) |
| 420 | {second_source_extra_cond}), 0) """.format(**args) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 421 | )[0][0] |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 422 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 423 | if args["detail_id"]: |
| 424 | if not args.get("extra_cond"): |
| 425 | args["extra_cond"] = "" |
Alchez | e52ae4e | 2018-09-06 18:27:06 +0530 | [diff] [blame] | 426 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 427 | args["source_dt_value"] = ( |
| 428 | frappe.db.sql( |
| 429 | """ |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 430 | (select ifnull(sum({source_field}), 0) |
| 431 | from `tab{source_dt}` where `{join_field}`='{detail_id}' |
| 432 | and (docstatus=1 {cond}) {extra_cond}) |
| 433 | """.format(**args) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 434 | )[0][0] |
| 435 | or 0.0 |
| 436 | ) |
Rohit Waghchaure | 3eea3c6 | 2020-11-18 20:17:52 +0530 | [diff] [blame] | 437 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 438 | if args["second_source_condition"]: |
| 439 | args["source_dt_value"] += flt(args["second_source_condition"]) |
Rohit Waghchaure | 3eea3c6 | 2020-11-18 20:17:52 +0530 | [diff] [blame] | 440 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 441 | frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 442 | """update `tab{target_dt}` |
| 443 | set {target_field} = {source_dt_value} {update_modified} |
| 444 | where name='{detail_id}'""".format(**args) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 445 | ) |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 446 | |
| 447 | def _update_percent_field_in_targets(self, args, update_modified=True): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 448 | """Update percent field in parent transaction""" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 449 | if args.get("percent_join_field_parent"): |
marination | 66069df | 2020-07-31 15:54:05 +0530 | [diff] [blame] | 450 | # if reference to target doc where % is to be updated, is |
| 451 | # in source doc's parent form, consider percent_join_field_parent |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 452 | args["name"] = self.get(args["percent_join_field_parent"]) |
marination | 66069df | 2020-07-31 15:54:05 +0530 | [diff] [blame] | 453 | self._update_percent_field(args, update_modified) |
| 454 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 455 | distinct_transactions = set( |
| 456 | d.get(args["percent_join_field"]) for d in self.get_all_children(args["source_dt"]) |
| 457 | ) |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 458 | |
marination | 66069df | 2020-07-31 15:54:05 +0530 | [diff] [blame] | 459 | for name in distinct_transactions: |
| 460 | if name: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 461 | args["name"] = name |
marination | 66069df | 2020-07-31 15:54:05 +0530 | [diff] [blame] | 462 | self._update_percent_field(args, update_modified) |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 463 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 464 | def _update_percent_field(self, args, update_modified=True): |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 465 | """Update percent field in parent transaction""" |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 466 | |
| 467 | self._update_modified(args, update_modified) |
Alchez | e52ae4e | 2018-09-06 18:27:06 +0530 | [diff] [blame] | 468 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 469 | if args.get("target_parent_field"): |
| 470 | frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 471 | """update `tab{target_parent_dt}` |
| 472 | set {target_parent_field} = round( |
Nabin Hait | bdab0ee | 2015-12-30 19:08:11 +0530 | [diff] [blame] | 473 | ifnull((select |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 474 | ifnull(sum(case when abs({target_ref_field}) > abs({target_field}) then abs({target_field}) else abs({target_ref_field}) end), 0) |
| 475 | / sum(abs({target_ref_field})) * 100 |
| 476 | from `tab{target_dt}` where parent='{name}' and parenttype='{target_parent_dt}' having sum(abs({target_ref_field})) > 0), 0), 6) |
| 477 | {update_modified} |
| 478 | where name='{name}'""".format(**args) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 479 | ) |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 480 | |
rohitwaghchaure | 7be942d | 2016-11-16 11:14:33 +0530 | [diff] [blame] | 481 | # update field |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 482 | if args.get("status_field"): |
| 483 | frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 484 | """update `tab{target_parent_dt}` |
| 485 | set {status_field} = (case when {target_parent_field}<0.001 then 'Not {keyword}' |
| 486 | else case when {target_parent_field}>=99.999999 then 'Fully {keyword}' |
| 487 | else 'Partly {keyword}' end end) |
| 488 | where name='{name}'""".format(**args) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 489 | ) |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 490 | |
rohitwaghchaure | 7be942d | 2016-11-16 11:14:33 +0530 | [diff] [blame] | 491 | if update_modified: |
| 492 | target = frappe.get_doc(args["target_parent_dt"], args["name"]) |
| 493 | target.set_status(update=True) |
| 494 | target.notify_update() |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 495 | |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 496 | def _update_modified(self, args, update_modified): |
Sagar Vora | d932cba | 2021-08-17 13:17:09 +0530 | [diff] [blame] | 497 | if not update_modified: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 498 | args["update_modified"] = "" |
Sagar Vora | d932cba | 2021-08-17 13:17:09 +0530 | [diff] [blame] | 499 | return |
| 500 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 501 | args["update_modified"] = ", modified = {}, modified_by = {}".format( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 502 | frappe.db.escape(now()), frappe.db.escape(frappe.session.user) |
Sagar Vora | d932cba | 2021-08-17 13:17:09 +0530 | [diff] [blame] | 503 | ) |
Anand Doshi | 6b71ef5 | 2016-01-06 16:32:06 +0530 | [diff] [blame] | 504 | |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 505 | def update_billing_status_for_zero_amount_refdoc(self, ref_dt): |
Himanshu Mishra | 70029b8 | 2019-05-29 14:19:35 +0530 | [diff] [blame] | 506 | ref_fieldname = frappe.scrub(ref_dt) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 507 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 508 | ref_docs = [item.get(ref_fieldname) for item in (self.get("items") or []) if item.get(ref_fieldname)] |
Himanshu Mishra | 70029b8 | 2019-05-29 14:19:35 +0530 | [diff] [blame] | 509 | if not ref_docs: |
| 510 | return |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 511 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 512 | zero_amount_refdocs = frappe.db.sql_list( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 513 | f""" |
Himanshu Mishra | 70029b8 | 2019-05-29 14:19:35 +0530 | [diff] [blame] | 514 | SELECT |
| 515 | name |
| 516 | from |
| 517 | `tab{ref_dt}` |
| 518 | where |
| 519 | docstatus = 1 |
| 520 | and base_net_total = 0 |
| 521 | and name in %(ref_docs)s |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 522 | """, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 523 | {"ref_docs": ref_docs}, |
| 524 | ) |
Himanshu Mishra | 70029b8 | 2019-05-29 14:19:35 +0530 | [diff] [blame] | 525 | |
| 526 | if zero_amount_refdocs: |
| 527 | self.update_billing_status(zero_amount_refdocs, ref_dt, ref_fieldname) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 528 | |
Rushabh Mehta | d48c239 | 2015-11-04 15:20:50 +0530 | [diff] [blame] | 529 | def update_billing_status(self, zero_amount_refdoc, ref_dt, ref_fieldname): |
Nabin Hait | 39eb7fa | 2014-01-15 17:36:18 +0530 | [diff] [blame] | 530 | for ref_dn in zero_amount_refdoc: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 531 | ref_doc_qty = flt( |
| 532 | frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 533 | """select ifnull(sum(qty), 0) from `tab{} Item` |
| 534 | where parent={}""".format(ref_dt, "%s"), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 535 | (ref_dn), |
| 536 | )[0][0] |
| 537 | ) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 538 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 539 | billed_qty = flt( |
| 540 | frappe.db.sql( |
| 541 | """select ifnull(sum(qty), 0) |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 542 | from `tab{} Item` where {}={} and docstatus=1""".format(self.doctype, ref_fieldname, "%s"), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 543 | (ref_dn), |
| 544 | )[0][0] |
| 545 | ) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 546 | |
Alchez | e52ae4e | 2018-09-06 18:27:06 +0530 | [diff] [blame] | 547 | per_billed = (min(ref_doc_qty, billed_qty) / ref_doc_qty) * 100 |
Rushabh Mehta | fc8e589 | 2016-07-20 12:08:47 +0530 | [diff] [blame] | 548 | |
| 549 | ref_doc = frappe.get_doc(ref_dt, ref_dn) |
| 550 | |
| 551 | ref_doc.db_set("per_billed", per_billed) |
Deepesh Garg | d679692 | 2022-02-06 11:35:23 +0530 | [diff] [blame] | 552 | |
| 553 | # set billling status |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 554 | if hasattr(ref_doc, "billing_status"): |
Deepesh Garg | d679692 | 2022-02-06 11:35:23 +0530 | [diff] [blame] | 555 | if ref_doc.per_billed < 0.001: |
| 556 | ref_doc.db_set("billing_status", "Not Billed") |
| 557 | elif ref_doc.per_billed > 99.999999: |
| 558 | ref_doc.db_set("billing_status", "Fully Billed") |
| 559 | else: |
| 560 | ref_doc.db_set("billing_status", "Partly Billed") |
| 561 | |
Rohit Waghchaure | 770d04e | 2016-10-05 23:02:15 +0530 | [diff] [blame] | 562 | ref_doc.set_status(update=True) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 563 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 564 | |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame] | 565 | @frappe.request_cache |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 566 | def get_allowance_for( |
| 567 | item_code, |
| 568 | item_allowance=None, |
| 569 | global_qty_allowance=None, |
| 570 | global_amount_allowance=None, |
| 571 | qty_or_amount="qty", |
| 572 | ): |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 573 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 574 | Returns the allowance for the item, if not set, returns global allowance |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 575 | """ |
Ankush Menat | 7eac4a2 | 2021-04-19 10:33:39 +0530 | [diff] [blame] | 576 | if item_allowance is None: |
| 577 | item_allowance = {} |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 578 | if qty_or_amount == "qty": |
| 579 | if item_allowance.get(item_code, frappe._dict()).get("qty"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 580 | return ( |
| 581 | item_allowance[item_code].qty, |
| 582 | item_allowance, |
| 583 | global_qty_allowance, |
| 584 | global_amount_allowance, |
| 585 | ) |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 586 | else: |
| 587 | if item_allowance.get(item_code, frappe._dict()).get("amount"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 588 | return ( |
| 589 | item_allowance[item_code].amount, |
| 590 | item_allowance, |
| 591 | global_qty_allowance, |
| 592 | global_amount_allowance, |
| 593 | ) |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 594 | |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame] | 595 | qty_allowance, over_billing_allowance = frappe.get_cached_value( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 596 | "Item", item_code, ["over_delivery_receipt_allowance", "over_billing_allowance"] |
| 597 | ) |
Nabin Hait | 7f0406f | 2014-01-03 17:43:19 +0530 | [diff] [blame] | 598 | |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 599 | if qty_or_amount == "qty" and not qty_allowance: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 600 | if global_qty_allowance is None: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 601 | global_qty_allowance = flt( |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame] | 602 | frappe.get_cached_value("Stock Settings", None, "over_delivery_receipt_allowance") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 603 | ) |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 604 | qty_allowance = global_qty_allowance |
| 605 | elif qty_or_amount == "amount" and not over_billing_allowance: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 606 | if global_amount_allowance is None: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 607 | global_amount_allowance = flt( |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame] | 608 | frappe.get_cached_value("Accounts Settings", None, "over_billing_allowance") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 609 | ) |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 610 | over_billing_allowance = global_amount_allowance |
Anand Doshi | 9fd50bc | 2014-04-09 19:20:01 +0530 | [diff] [blame] | 611 | |
Nabin Hait | 868766d | 2019-07-15 18:02:58 +0530 | [diff] [blame] | 612 | if qty_or_amount == "qty": |
| 613 | allowance = qty_allowance |
| 614 | item_allowance.setdefault(item_code, frappe._dict()).setdefault("qty", qty_allowance) |
| 615 | else: |
| 616 | allowance = over_billing_allowance |
| 617 | item_allowance.setdefault(item_code, frappe._dict()).setdefault("amount", over_billing_allowance) |
| 618 | |
| 619 | return allowance, item_allowance, global_qty_allowance, global_amount_allowance |