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