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 |
Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +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 |
Nabin Hait | 3cf67a4 | 2015-07-24 13:26:36 +0530 | [diff] [blame] | 6 | import frappe.share |
Nabin Hait | a1c96de | 2014-02-21 14:44:35 +0530 | [diff] [blame] | 7 | from frappe import _ |
Nabin Hait | f904ac5 | 2022-06-10 11:15:22 +0530 | [diff] [blame] | 8 | from frappe.utils import cint, flt, get_time, now_datetime |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 10 | from erpnext.controllers.status_updater import StatusUpdater |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 11 | |
Achilles Rasquinha | 1697a7a | 2018-02-15 11:39:45 +0530 | [diff] [blame] | 12 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 13 | class UOMMustBeIntegerError(frappe.ValidationError): |
| 14 | pass |
| 15 | |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 16 | |
Nabin Hait | b5be7ba | 2014-01-30 18:47:12 +0530 | [diff] [blame] | 17 | class TransactionBase(StatusUpdater): |
Anand Doshi | ee3d5cc | 2013-03-13 12:58:54 +0530 | [diff] [blame] | 18 | def validate_posting_time(self): |
Makarand Bauskar | b0df661 | 2017-05-16 07:59:58 +0530 | [diff] [blame] | 19 | # set Edit Posting Date and Time to 1 while data import |
Sagar Vora | 67fe101 | 2017-06-19 12:12:22 +0530 | [diff] [blame] | 20 | if frappe.flags.in_import and self.posting_date: |
Makarand Bauskar | b0df661 | 2017-05-16 07:59:58 +0530 | [diff] [blame] | 21 | self.set_posting_time = 1 |
| 22 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 23 | if not getattr(self, "set_posting_time", None): |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 24 | now = now_datetime() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 25 | self.posting_date = now.strftime("%Y-%m-%d") |
| 26 | self.posting_time = now.strftime("%H:%M:%S.%f") |
Manas Solanki | c6d5611 | 2018-01-18 11:20:24 +0530 | [diff] [blame] | 27 | elif self.posting_time: |
Faris Ansari | 184491b | 2018-01-15 14:18:53 +0530 | [diff] [blame] | 28 | try: |
| 29 | get_time(self.posting_time) |
| 30 | except ValueError: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 31 | frappe.throw(_("Invalid Posting Time")) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 32 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 33 | def validate_uom_is_integer(self, uom_field, qty_fields): |
Rushabh Mehta | cfb6ccf | 2014-04-03 11:46:52 +0530 | [diff] [blame] | 34 | validate_uom_is_integer(self, uom_field, qty_fields) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 35 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 36 | def validate_with_previous_doc(self, ref): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 37 | self.exclude_fields = ["conversion_factor", "uom"] if self.get("is_return") else [] |
rohitwaghchaure | 0df95fa | 2018-03-01 11:31:33 +0530 | [diff] [blame] | 38 | |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 39 | for key, val in ref.items(): |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 40 | is_child = val.get("is_child_table") |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 41 | ref_doc = {} |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 42 | item_ref_dn = [] |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 43 | for d in self.get_all_children(self.doctype + " Item"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 44 | ref_dn = d.get(val["ref_dn_field"]) |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 45 | if ref_dn: |
| 46 | if is_child: |
| 47 | self.compare_values({key: [ref_dn]}, val["compare_fields"], d) |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 48 | if ref_dn not in item_ref_dn: |
| 49 | item_ref_dn.append(ref_dn) |
Nabin Hait | 5e200e4 | 2013-07-29 15:29:51 +0530 | [diff] [blame] | 50 | elif not val.get("allow_duplicate_prev_row_id"): |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 51 | frappe.throw(_("Duplicate row {0} with same {1}").format(d.idx, key)) |
Nabin Hait | a9ec49e | 2013-07-15 16:33:24 +0530 | [diff] [blame] | 52 | elif ref_dn: |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 53 | ref_doc.setdefault(key, []) |
| 54 | if ref_dn not in ref_doc[key]: |
| 55 | ref_doc[key].append(ref_dn) |
| 56 | if ref_doc: |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 57 | self.compare_values(ref_doc, val["compare_fields"]) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 58 | |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 59 | def compare_values(self, ref_doc, fields, doc=None): |
Rushabh Mehta | 611b513 | 2015-03-19 17:15:45 +0530 | [diff] [blame] | 60 | for reference_doctype, ref_dn_list in ref_doc.items(): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 61 | prev_doc_detail_map = self.get_prev_doc_reference_details(ref_dn_list, reference_doctype, fields) |
Rushabh Mehta | 611b513 | 2015-03-19 17:15:45 +0530 | [diff] [blame] | 62 | for reference_name in ref_dn_list: |
Deepesh Garg | 4c61ee3 | 2023-04-02 09:35:27 +0530 | [diff] [blame] | 63 | prevdoc_values = prev_doc_detail_map.get(reference_name) |
Rushabh Mehta | afacc3d | 2015-12-01 15:53:07 +0530 | [diff] [blame] | 64 | if not prevdoc_values: |
| 65 | frappe.throw(_("Invalid reference {0} {1}").format(reference_doctype, reference_name)) |
| 66 | |
Nabin Hait | 6e68e0e | 2013-07-10 15:33:29 +0530 | [diff] [blame] | 67 | for field, condition in fields: |
rohitwaghchaure | 0df95fa | 2018-03-01 11:31:33 +0530 | [diff] [blame] | 68 | if prevdoc_values[field] is not None and field not in self.exclude_fields: |
Anand Doshi | 63d844b | 2013-07-10 20:55:15 +0530 | [diff] [blame] | 69 | self.validate_value(field, condition, prevdoc_values[field], doc) |
Anand Doshi | 414248b | 2015-08-26 17:56:40 +0530 | [diff] [blame] | 70 | |
Deepesh Garg | 4c61ee3 | 2023-04-02 09:35:27 +0530 | [diff] [blame] | 71 | def get_prev_doc_reference_details(self, reference_names, reference_doctype, fields): |
| 72 | prev_doc_detail_map = {} |
| 73 | details = frappe.get_all( |
| 74 | reference_doctype, |
| 75 | filters={"name": ("in", reference_names)}, |
| 76 | fields=["name"] + [d[0] for d in fields], |
| 77 | ) |
| 78 | |
| 79 | for d in details: |
| 80 | prev_doc_detail_map.setdefault(d.name, d) |
| 81 | |
| 82 | return prev_doc_detail_map |
| 83 | |
Nabin Hait | 28a9e35 | 2015-07-08 13:10:52 +0530 | [diff] [blame] | 84 | def validate_rate_with_reference_doc(self, ref_details): |
Rohit Waghchaure | b4a102d | 2022-09-06 16:22:00 +0530 | [diff] [blame] | 85 | if self.get("is_internal_supplier"): |
| 86 | return |
| 87 | |
Marica | 5f299a0 | 2020-07-06 18:26:56 +0530 | [diff] [blame] | 88 | buying_doctypes = ["Purchase Order", "Purchase Invoice", "Purchase Receipt"] |
Marica | 5f299a0 | 2020-07-06 18:26:56 +0530 | [diff] [blame] | 89 | |
| 90 | if self.doctype in buying_doctypes: |
Deepesh Garg | 4c61ee3 | 2023-04-02 09:35:27 +0530 | [diff] [blame] | 91 | action, role_allowed_to_override = frappe.get_cached_value( |
| 92 | "Buying Settings", "None", ["maintain_same_rate_action", "role_to_override_stop_action"] |
| 93 | ) |
Marica | 5f299a0 | 2020-07-06 18:26:56 +0530 | [diff] [blame] | 94 | else: |
Deepesh Garg | 4c61ee3 | 2023-04-02 09:35:27 +0530 | [diff] [blame] | 95 | action, role_allowed_to_override = frappe.get_cached_value( |
| 96 | "Selling Settings", "None", ["maintain_same_rate_action", "role_to_override_stop_action"] |
| 97 | ) |
Marica | 5f299a0 | 2020-07-06 18:26:56 +0530 | [diff] [blame] | 98 | |
Devin Slauenwhite | 3f6d805 | 2023-11-23 12:12:12 -0500 | [diff] [blame] | 99 | stop_actions = [] |
Nabin Hait | 28a9e35 | 2015-07-08 13:10:52 +0530 | [diff] [blame] | 100 | for ref_dt, ref_dn_field, ref_link_field in ref_details: |
Deepesh Garg | 4c61ee3 | 2023-04-02 09:35:27 +0530 | [diff] [blame] | 101 | reference_names = [d.get(ref_link_field) for d in self.get("items") if d.get(ref_link_field)] |
| 102 | reference_details = self.get_reference_details(reference_names, ref_dt + " Item") |
Nabin Hait | 28a9e35 | 2015-07-08 13:10:52 +0530 | [diff] [blame] | 103 | for d in self.get("items"): |
| 104 | if d.get(ref_link_field): |
Deepesh Garg | 4c61ee3 | 2023-04-02 09:35:27 +0530 | [diff] [blame] | 105 | ref_rate = reference_details.get(d.get(ref_link_field)) |
Anand Doshi | 414248b | 2015-08-26 17:56:40 +0530 | [diff] [blame] | 106 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 107 | if abs(flt(d.rate - ref_rate, d.precision("rate"))) >= 0.01: |
Deepesh Garg | b717420 | 2021-04-14 10:43:45 +0530 | [diff] [blame] | 108 | if action == "Stop": |
Deepesh Garg | b717420 | 2021-04-14 10:43:45 +0530 | [diff] [blame] | 109 | if role_allowed_to_override not in frappe.get_roles(): |
Devin Slauenwhite | 3f6d805 | 2023-11-23 12:12:12 -0500 | [diff] [blame] | 110 | stop_actions.append( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 111 | _("Row #{0}: Rate must be same as {1}: {2} ({3} / {4})").format( |
| 112 | d.idx, ref_dt, d.get(ref_dn_field), d.rate, ref_rate |
| 113 | ) |
| 114 | ) |
Deepesh Garg | b717420 | 2021-04-14 10:43:45 +0530 | [diff] [blame] | 115 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 116 | frappe.msgprint( |
| 117 | _("Row #{0}: Rate must be same as {1}: {2} ({3} / {4})").format( |
| 118 | d.idx, ref_dt, d.get(ref_dn_field), d.rate, ref_rate |
| 119 | ), |
| 120 | title=_("Warning"), |
| 121 | indicator="orange", |
| 122 | ) |
Devin Slauenwhite | 3f6d805 | 2023-11-23 12:12:12 -0500 | [diff] [blame] | 123 | if stop_actions: |
| 124 | frappe.throw(stop_actions, as_list=True) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 125 | |
Deepesh Garg | 4c61ee3 | 2023-04-02 09:35:27 +0530 | [diff] [blame] | 126 | def get_reference_details(self, reference_names, reference_doctype): |
| 127 | return frappe._dict( |
| 128 | frappe.get_all( |
| 129 | reference_doctype, |
| 130 | filters={"name": ("in", reference_names)}, |
| 131 | fields=["name", "rate"], |
| 132 | as_list=1, |
| 133 | ) |
| 134 | ) |
| 135 | |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 136 | def get_link_filters(self, for_doctype): |
Saurabh | b6d6b84 | 2016-06-27 14:48:26 +0530 | [diff] [blame] | 137 | if hasattr(self, "prev_link_mapper") and self.prev_link_mapper.get(for_doctype): |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 138 | fieldname = self.prev_link_mapper[for_doctype]["fieldname"] |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 139 | |
Ankush Menat | 9891780 | 2021-06-11 18:40:22 +0530 | [diff] [blame] | 140 | values = filter(None, tuple(item.as_dict()[fieldname] for item in self.items)) |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 141 | |
| 142 | if values: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 143 | ret = {for_doctype: {"filters": [[for_doctype, "name", "in", values]]}} |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 144 | else: |
| 145 | ret = None |
| 146 | else: |
| 147 | ret = None |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 148 | |
Saurabh | 8f24ecb | 2016-06-16 18:01:58 +0530 | [diff] [blame] | 149 | return ret |
Anand Doshi | f9fc04c | 2015-02-23 22:14:12 +0530 | [diff] [blame] | 150 | |
Sagar Sharma | 6485ac4 | 2021-12-09 17:04:00 +0530 | [diff] [blame] | 151 | def reset_default_field_value(self, default_field: str, child_table: str, child_table_field: str): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 152 | """Reset "Set default X" fields on forms to avoid confusion. |
Sagar Sharma | 6485ac4 | 2021-12-09 17:04:00 +0530 | [diff] [blame] | 153 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 154 | example: |
| 155 | doc = { |
| 156 | "set_from_warehouse": "Warehouse A", |
| 157 | "items": [{"from_warehouse": "warehouse B"}, {"from_warehouse": "warehouse A"}], |
| 158 | } |
| 159 | Since this has dissimilar values in child table, the default field will be erased. |
Sagar Sharma | 6485ac4 | 2021-12-09 17:04:00 +0530 | [diff] [blame] | 160 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 161 | doc.reset_default_field_value("set_from_warehouse", "items", "from_warehouse") |
| 162 | """ |
Sagar Sharma | 6485ac4 | 2021-12-09 17:04:00 +0530 | [diff] [blame] | 163 | child_table_values = set() |
| 164 | |
| 165 | for row in self.get(child_table): |
| 166 | child_table_values.add(row.get(child_table_field)) |
| 167 | |
| 168 | if len(child_table_values) > 1: |
| 169 | self.set(default_field, None) |
Sagar Sharma | 6485ac4 | 2021-12-09 17:04:00 +0530 | [diff] [blame] | 170 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 171 | |
Anand Doshi | 11d3113 | 2013-06-17 12:51:36 +0530 | [diff] [blame] | 172 | def delete_events(ref_type, ref_name): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 173 | events = ( |
| 174 | frappe.db.sql_list( |
| 175 | """ SELECT |
Rohit Waghchaure | 8f62aec | 2019-01-01 13:54:54 +0530 | [diff] [blame] | 176 | distinct `tabEvent`.name |
| 177 | from |
| 178 | `tabEvent`, `tabEvent Participants` |
| 179 | where |
| 180 | `tabEvent`.name = `tabEvent Participants`.parent |
| 181 | and `tabEvent Participants`.reference_doctype = %s |
| 182 | and `tabEvent Participants`.reference_docname = %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 183 | """, |
| 184 | (ref_type, ref_name), |
| 185 | ) |
| 186 | or [] |
| 187 | ) |
Rohit Waghchaure | 8f62aec | 2019-01-01 13:54:54 +0530 | [diff] [blame] | 188 | |
| 189 | if events: |
| 190 | frappe.delete_doc("Event", events, for_reload=True) |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 191 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 192 | |
Nabin Hait | c5fb88c | 2015-02-09 16:46:46 +0530 | [diff] [blame] | 193 | def validate_uom_is_integer(doc, uom_field, qty_fields, child_dt=None): |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 194 | if isinstance(qty_fields, str): |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 195 | qty_fields = [qty_fields] |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 196 | |
Ankush Menat | 9891780 | 2021-06-11 18:40:22 +0530 | [diff] [blame] | 197 | distinct_uoms = list(set(d.get(uom_field) for d in doc.get_all_children())) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 198 | integer_uoms = list( |
| 199 | filter( |
| 200 | lambda uom: frappe.db.get_value("UOM", uom, "must_be_whole_number", cache=True) or None, |
| 201 | distinct_uoms, |
| 202 | ) |
| 203 | ) |
Rushabh Mehta | 2c45899 | 2014-04-15 14:36:12 +0530 | [diff] [blame] | 204 | |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 205 | if not integer_uoms: |
| 206 | return |
| 207 | |
Nabin Hait | c5fb88c | 2015-02-09 16:46:46 +0530 | [diff] [blame] | 208 | for d in doc.get_all_children(parenttype=child_dt): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 209 | if d.get(uom_field) in integer_uoms: |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 210 | for f in qty_fields: |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 211 | qty = d.get(f) |
| 212 | if qty: |
s-aga-r | 6ec7590 | 2023-04-03 14:06:07 +0530 | [diff] [blame] | 213 | if abs(cint(qty) - flt(qty, d.precision(f))) > 0.0000001: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 214 | frappe.throw( |
| 215 | _( |
| 216 | "Row {1}: Quantity ({0}) cannot be a fraction. To allow this, disable '{2}' in UOM {3}." |
| 217 | ).format( |
s-aga-r | 6ec7590 | 2023-04-03 14:06:07 +0530 | [diff] [blame] | 218 | flt(qty, d.precision(f)), |
| 219 | d.idx, |
| 220 | frappe.bold(_("Must be Whole Number")), |
| 221 | frappe.bold(d.get(uom_field)), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 222 | ), |
| 223 | UOMMustBeIntegerError, |
| 224 | ) |