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