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 | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 3 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 4 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | import json |
| 6 | |
| 7 | import frappe |
| 8 | from frappe import _ |
s-aga-r | 9a37ac6 | 2023-04-21 13:28:14 +0530 | [diff] [blame] | 9 | from frappe.query_builder.functions import CombineDatetime, IfNull, Sum |
Rohit Waghchaure | d80ca52 | 2024-02-07 21:56:21 +0530 | [diff] [blame] | 10 | from frappe.utils import cstr, flt, get_link_to_form, get_time, getdate, nowdate, nowtime |
Achilles Rasquinha | 56b2e12 | 2018-02-13 14:42:40 +0530 | [diff] [blame] | 11 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 12 | import erpnext |
Rohit Waghchaure | 0165012 | 2024-02-06 13:31:36 +0530 | [diff] [blame] | 13 | from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import ( |
| 14 | get_available_serial_nos, |
| 15 | ) |
s-aga-r | e782a05 | 2023-04-25 13:54:36 +0530 | [diff] [blame] | 16 | from erpnext.stock.doctype.warehouse.warehouse import get_child_warehouses |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 17 | from erpnext.stock.serial_batch_bundle import BatchNoValuation, SerialNoValuation |
Ankush Menat | 61c5ad4 | 2022-01-15 18:06:50 +0530 | [diff] [blame] | 18 | from erpnext.stock.valuation import FIFOValuation, LIFOValuation |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 19 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 20 | BarcodeScanResult = dict[str, str | None] |
Devin Slauenwhite | b88e850 | 2022-10-20 06:26:07 -0400 | [diff] [blame] | 21 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 22 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 23 | class InvalidWarehouseCompany(frappe.ValidationError): |
| 24 | pass |
| 25 | |
| 26 | |
| 27 | class PendingRepostingError(frappe.ValidationError): |
| 28 | pass |
| 29 | |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 30 | |
Shreya Shah | e0a47ae | 2018-08-28 13:46:22 +0530 | [diff] [blame] | 31 | def get_stock_value_from_bin(warehouse=None, item_code=None): |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 32 | values = {} |
| 33 | conditions = "" |
| 34 | if warehouse: |
rohitwaghchaure | f1fab87 | 2019-09-05 14:47:43 +0530 | [diff] [blame] | 35 | conditions += """ and `tabBin`.warehouse in ( |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 36 | select w2.name from `tabWarehouse` w1 |
| 37 | join `tabWarehouse` w2 on |
| 38 | w1.name = %(warehouse)s |
| 39 | and w2.lft between w1.lft and w1.rgt |
| 40 | ) """ |
| 41 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 42 | values["warehouse"] = warehouse |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 43 | |
| 44 | if item_code: |
rohitwaghchaure | f1fab87 | 2019-09-05 14:47:43 +0530 | [diff] [blame] | 45 | conditions += " and `tabBin`.item_code = %(item_code)s" |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 46 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 47 | values["item_code"] = item_code |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 48 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | query = ( |
| 50 | """select sum(stock_value) from `tabBin`, `tabItem` where 1 = 1 |
| 51 | and `tabItem`.name = `tabBin`.item_code and ifnull(`tabItem`.disabled, 0) = 0 %s""" |
| 52 | % conditions |
| 53 | ) |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 54 | |
| 55 | stock_value = frappe.db.sql(query, values) |
| 56 | |
Shreya Shah | e0a47ae | 2018-08-28 13:46:22 +0530 | [diff] [blame] | 57 | return stock_value |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 58 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 59 | |
s-aga-r | e782a05 | 2023-04-25 13:54:36 +0530 | [diff] [blame] | 60 | def get_stock_value_on( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 61 | warehouses: list | str | None = None, posting_date: str | None = None, item_code: str | None = None |
s-aga-r | e782a05 | 2023-04-25 13:54:36 +0530 | [diff] [blame] | 62 | ) -> float: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 63 | if not posting_date: |
| 64 | posting_date = nowdate() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 65 | |
s-aga-r | e43bc38 | 2023-04-19 12:05:17 +0530 | [diff] [blame] | 66 | sle = frappe.qb.DocType("Stock Ledger Entry") |
| 67 | query = ( |
| 68 | frappe.qb.from_(sle) |
s-aga-r | 9a37ac6 | 2023-04-21 13:28:14 +0530 | [diff] [blame] | 69 | .select(IfNull(Sum(sle.stock_value_difference), 0)) |
s-aga-r | e43bc38 | 2023-04-19 12:05:17 +0530 | [diff] [blame] | 70 | .where((sle.posting_date <= posting_date) & (sle.is_cancelled == 0)) |
| 71 | .orderby(CombineDatetime(sle.posting_date, sle.posting_time), order=frappe.qb.desc) |
| 72 | .orderby(sle.creation, order=frappe.qb.desc) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 73 | ) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 74 | |
s-aga-r | e782a05 | 2023-04-25 13:54:36 +0530 | [diff] [blame] | 75 | if warehouses: |
| 76 | if isinstance(warehouses, str): |
| 77 | warehouses = [warehouses] |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 78 | |
s-aga-r | e782a05 | 2023-04-25 13:54:36 +0530 | [diff] [blame] | 79 | warehouses = set(warehouses) |
| 80 | for wh in list(warehouses): |
| 81 | if frappe.db.get_value("Warehouse", wh, "is_group"): |
| 82 | warehouses.update(get_child_warehouses(wh)) |
| 83 | |
| 84 | query = query.where(sle.warehouse.isin(warehouses)) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 85 | |
| 86 | if item_code: |
s-aga-r | e43bc38 | 2023-04-19 12:05:17 +0530 | [diff] [blame] | 87 | query = query.where(sle.item_code == item_code) |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 88 | |
s-aga-r | 9a37ac6 | 2023-04-21 13:28:14 +0530 | [diff] [blame] | 89 | return query.run(as_list=True)[0][0] |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 90 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 91 | |
nick9822 | 6f48d4b | 2017-01-09 12:12:36 +0530 | [diff] [blame] | 92 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | def get_stock_balance( |
| 94 | item_code, |
| 95 | warehouse, |
| 96 | posting_date=None, |
| 97 | posting_time=None, |
| 98 | with_valuation_rate=False, |
| 99 | with_serial_no=False, |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 100 | inventory_dimensions_dict=None, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 101 | ): |
Rushabh Mehta | 2712e36 | 2015-02-17 12:50:20 +0530 | [diff] [blame] | 102 | """Returns stock balance quantity at given warehouse on given posting date or current date. |
| 103 | |
| 104 | If `with_valuation_rate` is True, will return tuple (qty, rate)""" |
Rushabh Mehta | dc93e0a | 2015-02-20 15:11:56 +0530 | [diff] [blame] | 105 | |
| 106 | from erpnext.stock.stock_ledger import get_previous_sle |
| 107 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 108 | if posting_date is None: |
| 109 | posting_date = nowdate() |
| 110 | if posting_time is None: |
| 111 | posting_time = nowtime() |
Rushabh Mehta | dc93e0a | 2015-02-20 15:11:56 +0530 | [diff] [blame] | 112 | |
Rohit Waghchaure | 560f822 | 2020-04-06 15:02:43 +0530 | [diff] [blame] | 113 | args = { |
Rushabh Mehta | dc93e0a | 2015-02-20 15:11:56 +0530 | [diff] [blame] | 114 | "item_code": item_code, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 115 | "warehouse": warehouse, |
Rushabh Mehta | dc93e0a | 2015-02-20 15:11:56 +0530 | [diff] [blame] | 116 | "posting_date": posting_date, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 117 | "posting_time": posting_time, |
Rohit Waghchaure | 560f822 | 2020-04-06 15:02:43 +0530 | [diff] [blame] | 118 | } |
| 119 | |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 120 | extra_cond = "" |
| 121 | if inventory_dimensions_dict: |
| 122 | for field, value in inventory_dimensions_dict.items(): |
| 123 | args[field] = value |
| 124 | extra_cond += f" and {field} = %({field})s" |
| 125 | |
| 126 | last_entry = get_previous_sle(args, extra_cond=extra_cond) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 127 | |
Rushabh Mehta | 2712e36 | 2015-02-17 12:50:20 +0530 | [diff] [blame] | 128 | if with_valuation_rate: |
Rohit Waghchaure | 560f822 | 2020-04-06 15:02:43 +0530 | [diff] [blame] | 129 | if with_serial_no: |
Rohit Waghchaure | 0165012 | 2024-02-06 13:31:36 +0530 | [diff] [blame] | 130 | serial_no_details = get_available_serial_nos( |
| 131 | frappe._dict( |
| 132 | { |
| 133 | "item_code": item_code, |
| 134 | "warehouse": warehouse, |
| 135 | "posting_date": posting_date, |
| 136 | "posting_time": posting_time, |
| 137 | "ignore_warehouse": 1, |
| 138 | } |
| 139 | ) |
| 140 | ) |
| 141 | |
| 142 | serial_nos = "" |
| 143 | if serial_no_details: |
| 144 | serial_nos = "\n".join(d.serial_no for d in serial_no_details) |
Rohit Waghchaure | 560f822 | 2020-04-06 15:02:43 +0530 | [diff] [blame] | 145 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 146 | return ( |
| 147 | (last_entry.qty_after_transaction, last_entry.valuation_rate, serial_nos) |
| 148 | if last_entry |
| 149 | else (0.0, 0.0, None) |
| 150 | ) |
Rohit Waghchaure | 560f822 | 2020-04-06 15:02:43 +0530 | [diff] [blame] | 151 | else: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 152 | return (last_entry.qty_after_transaction, last_entry.valuation_rate) if last_entry else (0.0, 0.0) |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 153 | else: |
nick9822 | cc699a9 | 2017-06-20 17:13:29 +0530 | [diff] [blame] | 154 | return last_entry.qty_after_transaction if last_entry else 0.0 |
Rushabh Mehta | f850987 | 2014-10-08 12:03:19 +0530 | [diff] [blame] | 155 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 156 | |
Rohit Waghchaure | 560f822 | 2020-04-06 15:02:43 +0530 | [diff] [blame] | 157 | def get_serial_nos_data(serial_nos): |
| 158 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | |
Rohit Waghchaure | 560f822 | 2020-04-06 15:02:43 +0530 | [diff] [blame] | 160 | return get_serial_nos(serial_nos) |
| 161 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 162 | |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 163 | @frappe.whitelist() |
| 164 | def get_latest_stock_qty(item_code, warehouse=None): |
| 165 | values, condition = [item_code], "" |
| 166 | if warehouse: |
| 167 | lft, rgt, is_group = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt", "is_group"]) |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 168 | |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 169 | if is_group: |
| 170 | values.extend([lft, rgt]) |
| 171 | condition += "and exists (\ |
| 172 | select name from `tabWarehouse` wh where wh.name = tabBin.warehouse\ |
| 173 | and wh.lft >= %s and wh.rgt <= %s)" |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 174 | |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 175 | else: |
| 176 | values.append(warehouse) |
| 177 | condition += " AND warehouse = %s" |
Sachin Mane | 19a5a5d | 2018-06-21 13:01:48 +0530 | [diff] [blame] | 178 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 179 | actual_qty = frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 180 | f"""select sum(actual_qty) from tabBin |
| 181 | where item_code=%s {condition}""", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 182 | values, |
| 183 | )[0][0] |
Nabin Hait | 949a920 | 2017-07-05 13:55:41 +0530 | [diff] [blame] | 184 | |
| 185 | return actual_qty |
| 186 | |
| 187 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 188 | def get_latest_stock_balance(): |
| 189 | bin_map = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 190 | for d in frappe.db.sql( |
| 191 | """SELECT item_code, warehouse, stock_value as stock_value |
| 192 | FROM tabBin""", |
| 193 | as_dict=1, |
| 194 | ): |
| 195 | bin_map.setdefault(d.warehouse, {}).setdefault(d.item_code, flt(d.stock_value)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 196 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 197 | return bin_map |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 198 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 199 | |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 200 | def get_bin(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 201 | bin = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 202 | if not bin: |
Ankush Menat | 08810db | 2022-01-30 16:25:42 +0530 | [diff] [blame] | 203 | bin_obj = _create_bin(item_code, warehouse) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 204 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 205 | bin_obj = frappe.get_doc("Bin", bin, for_update=True) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 206 | bin_obj.flags.ignore_permissions = True |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 207 | return bin_obj |
| 208 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 209 | |
| 210 | def get_or_make_bin(item_code: str, warehouse: str) -> str: |
Rohit Waghchaure | 9e5e2de | 2023-05-25 23:41:56 +0530 | [diff] [blame] | 211 | bin_record = frappe.get_cached_value("Bin", {"item_code": item_code, "warehouse": warehouse}) |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 212 | |
| 213 | if not bin_record: |
Ankush Menat | 08810db | 2022-01-30 16:25:42 +0530 | [diff] [blame] | 214 | bin_obj = _create_bin(item_code, warehouse) |
| 215 | bin_record = bin_obj.name |
| 216 | return bin_record |
| 217 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 218 | |
Ankush Menat | 08810db | 2022-01-30 16:25:42 +0530 | [diff] [blame] | 219 | def _create_bin(item_code, warehouse): |
| 220 | """Create a bin and take care of concurrent inserts.""" |
| 221 | |
| 222 | bin_creation_savepoint = "create_bin" |
| 223 | try: |
| 224 | frappe.db.savepoint(bin_creation_savepoint) |
| 225 | bin_obj = frappe.get_doc(doctype="Bin", item_code=item_code, warehouse=warehouse) |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 226 | bin_obj.flags.ignore_permissions = 1 |
| 227 | bin_obj.insert() |
Ankush Menat | 08810db | 2022-01-30 16:25:42 +0530 | [diff] [blame] | 228 | except frappe.UniqueValidationError: |
| 229 | frappe.db.rollback(save_point=bin_creation_savepoint) # preserve transaction in postgres |
| 230 | bin_obj = frappe.get_last_doc("Bin", {"item_code": item_code, "warehouse": warehouse}) |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 231 | |
Ankush Menat | 08810db | 2022-01-30 16:25:42 +0530 | [diff] [blame] | 232 | return bin_obj |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 233 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 234 | |
Nabin Hait | 5eefff1 | 2015-12-07 10:44:56 +0530 | [diff] [blame] | 235 | @frappe.whitelist() |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 236 | def get_incoming_rate(args, raise_error_if_no_rate=True): |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 237 | """Get Incoming Rate based on valuation method""" |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 238 | from erpnext.stock.stock_ledger import get_previous_sle, get_valuation_rate |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 239 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 240 | if isinstance(args, str): |
Nabin Hait | 41c8cf6 | 2015-12-08 14:50:24 +0530 | [diff] [blame] | 241 | args = json.loads(args) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 242 | |
Ankush Menat | ab92652 | 2022-02-19 15:37:03 +0530 | [diff] [blame] | 243 | in_rate = None |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 244 | |
| 245 | item_details = frappe.get_cached_value( |
| 246 | "Item", args.get("item_code"), ["has_serial_no", "has_batch_no"], as_dict=1 |
| 247 | ) |
| 248 | |
Rohit Waghchaure | 674bd3e | 2023-03-17 16:42:59 +0530 | [diff] [blame] | 249 | if isinstance(args, dict): |
| 250 | args = frappe._dict(args) |
| 251 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 252 | if item_details and item_details.has_serial_no and args.get("serial_and_batch_bundle"): |
Rohit Waghchaure | 674bd3e | 2023-03-17 16:42:59 +0530 | [diff] [blame] | 253 | args.actual_qty = args.qty |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 254 | sn_obj = SerialNoValuation( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 255 | sle=args, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 256 | warehouse=args.get("warehouse"), |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 257 | item_code=args.get("item_code"), |
Ankush Menat | ab92652 | 2022-02-19 15:37:03 +0530 | [diff] [blame] | 258 | ) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 259 | |
rohitwaghchaure | 4b24fcd | 2024-02-20 23:45:07 +0530 | [diff] [blame] | 260 | return sn_obj.get_incoming_rate() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 261 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 262 | elif item_details and item_details.has_batch_no and args.get("serial_and_batch_bundle"): |
Rohit Waghchaure | 674bd3e | 2023-03-17 16:42:59 +0530 | [diff] [blame] | 263 | args.actual_qty = args.qty |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 264 | batch_obj = BatchNoValuation( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 265 | sle=args, |
| 266 | warehouse=args.get("warehouse"), |
| 267 | item_code=args.get("item_code"), |
| 268 | ) |
| 269 | |
rohitwaghchaure | 4b24fcd | 2024-02-20 23:45:07 +0530 | [diff] [blame] | 270 | return batch_obj.get_incoming_rate() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 271 | |
rohitwaghchaure | 67d828d | 2024-01-27 12:00:06 +0530 | [diff] [blame] | 272 | elif (args.get("serial_no") or "").strip() and not args.get("serial_and_batch_bundle"): |
rohitwaghchaure | 4b24fcd | 2024-02-20 23:45:07 +0530 | [diff] [blame] | 273 | args.actual_qty = args.qty |
| 274 | args.serial_nos = get_serial_nos_data(args.get("serial_no")) |
| 275 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 276 | sn_obj = SerialNoValuation(sle=args, warehouse=args.get("warehouse"), item_code=args.get("item_code")) |
rohitwaghchaure | 4b24fcd | 2024-02-20 23:45:07 +0530 | [diff] [blame] | 277 | |
| 278 | return sn_obj.get_incoming_rate() |
rohitwaghchaure | 67d828d | 2024-01-27 12:00:06 +0530 | [diff] [blame] | 279 | elif ( |
| 280 | args.get("batch_no") |
| 281 | and frappe.db.get_value("Batch", args.get("batch_no"), "use_batchwise_valuation", cache=True) |
| 282 | and not args.get("serial_and_batch_bundle") |
| 283 | ): |
rohitwaghchaure | 4b24fcd | 2024-02-20 23:45:07 +0530 | [diff] [blame] | 284 | args.actual_qty = args.qty |
| 285 | args.batch_nos = frappe._dict({args.batch_no: args}) |
| 286 | |
| 287 | batch_obj = BatchNoValuation( |
| 288 | sle=args, |
rohitwaghchaure | 67d828d | 2024-01-27 12:00:06 +0530 | [diff] [blame] | 289 | warehouse=args.get("warehouse"), |
rohitwaghchaure | 4b24fcd | 2024-02-20 23:45:07 +0530 | [diff] [blame] | 290 | item_code=args.get("item_code"), |
rohitwaghchaure | 67d828d | 2024-01-27 12:00:06 +0530 | [diff] [blame] | 291 | ) |
| 292 | |
rohitwaghchaure | 4b24fcd | 2024-02-20 23:45:07 +0530 | [diff] [blame] | 293 | return batch_obj.get_incoming_rate() |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 294 | else: |
| 295 | valuation_method = get_valuation_method(args.get("item_code")) |
| 296 | previous_sle = get_previous_sle(args) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 297 | if valuation_method in ("FIFO", "LIFO"): |
rohitwaghchaure | ce8adec | 2017-12-15 12:13:50 +0530 | [diff] [blame] | 298 | if previous_sle: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 299 | previous_stock_queue = json.loads(previous_sle.get("stock_queue", "[]") or "[]") |
| 300 | in_rate = ( |
| 301 | _get_fifo_lifo_rate(previous_stock_queue, args.get("qty") or 0, valuation_method) |
| 302 | if previous_stock_queue |
Hossein Yousefian | 1d162ff | 2023-04-16 11:03:16 +0330 | [diff] [blame] | 303 | else None |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 304 | ) |
| 305 | elif valuation_method == "Moving Average": |
Hossein Yousefian | 1d162ff | 2023-04-16 11:03:16 +0330 | [diff] [blame] | 306 | in_rate = previous_sle.get("valuation_rate") |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 307 | |
Ankush Menat | ab92652 | 2022-02-19 15:37:03 +0530 | [diff] [blame] | 308 | if in_rate is None: |
Hossein Yousefian | 13d4f85 | 2023-04-18 14:50:09 +0330 | [diff] [blame] | 309 | voucher_no = args.get("voucher_no") or args.get("name") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 310 | in_rate = get_valuation_rate( |
| 311 | args.get("item_code"), |
| 312 | args.get("warehouse"), |
| 313 | args.get("voucher_type"), |
| 314 | voucher_no, |
| 315 | args.get("allow_zero_valuation"), |
| 316 | currency=erpnext.get_company_currency(args.get("company")), |
| 317 | company=args.get("company"), |
| 318 | raise_error_if_no_rate=raise_error_if_no_rate, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 319 | ) |
rohitwaghchaure | ce8adec | 2017-12-15 12:13:50 +0530 | [diff] [blame] | 320 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 321 | return flt(in_rate) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 322 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 323 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 324 | def get_batch_incoming_rate(item_code, warehouse, batch_no, posting_date, posting_time, creation=None): |
rohitwaghchaure | 67d828d | 2024-01-27 12:00:06 +0530 | [diff] [blame] | 325 | sle = frappe.qb.DocType("Stock Ledger Entry") |
| 326 | |
| 327 | timestamp_condition = CombineDatetime(sle.posting_date, sle.posting_time) < CombineDatetime( |
| 328 | posting_date, posting_time |
| 329 | ) |
| 330 | if creation: |
| 331 | timestamp_condition |= ( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 332 | CombineDatetime(sle.posting_date, sle.posting_time) == CombineDatetime(posting_date, posting_time) |
rohitwaghchaure | 67d828d | 2024-01-27 12:00:06 +0530 | [diff] [blame] | 333 | ) & (sle.creation < creation) |
| 334 | |
| 335 | batch_details = ( |
| 336 | frappe.qb.from_(sle) |
| 337 | .select(Sum(sle.stock_value_difference).as_("batch_value"), Sum(sle.actual_qty).as_("batch_qty")) |
| 338 | .where( |
| 339 | (sle.item_code == item_code) |
| 340 | & (sle.warehouse == warehouse) |
| 341 | & (sle.batch_no == batch_no) |
| 342 | & (sle.serial_and_batch_bundle.isnull()) |
| 343 | & (sle.is_cancelled == 0) |
| 344 | ) |
| 345 | .where(timestamp_condition) |
| 346 | ).run(as_dict=True) |
| 347 | |
| 348 | if batch_details and batch_details[0].batch_qty: |
| 349 | return batch_details[0].batch_value / batch_details[0].batch_qty |
| 350 | |
| 351 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 352 | def get_avg_purchase_rate(serial_nos): |
| 353 | """get average value of serial numbers""" |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 354 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 355 | serial_nos = get_valid_serial_nos(serial_nos) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 356 | return flt( |
| 357 | frappe.db.sql( |
| 358 | """select avg(purchase_rate) from `tabSerial No` |
| 359 | where name in (%s)""" |
| 360 | % ", ".join(["%s"] * len(serial_nos)), |
| 361 | tuple(serial_nos), |
| 362 | )[0][0] |
| 363 | ) |
| 364 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 365 | |
| 366 | def get_valuation_method(item_code): |
| 367 | """get valuation method from item or default""" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 368 | val_method = frappe.db.get_value("Item", item_code, "valuation_method", cache=True) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 369 | if not val_method: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 370 | val_method = frappe.db.get_single_value("Stock Settings", "valuation_method", cache=True) or "FIFO" |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 371 | return val_method |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 372 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 373 | |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 374 | def get_fifo_rate(previous_stock_queue, qty): |
| 375 | """get FIFO (average) Rate from Queue""" |
Ankush Menat | 61c5ad4 | 2022-01-15 18:06:50 +0530 | [diff] [blame] | 376 | return _get_fifo_lifo_rate(previous_stock_queue, qty, "FIFO") |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 377 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 378 | |
Ankush Menat | 61c5ad4 | 2022-01-15 18:06:50 +0530 | [diff] [blame] | 379 | def get_lifo_rate(previous_stock_queue, qty): |
| 380 | """get LIFO (average) Rate from Queue""" |
| 381 | return _get_fifo_lifo_rate(previous_stock_queue, qty, "LIFO") |
| 382 | |
| 383 | |
| 384 | def _get_fifo_lifo_rate(previous_stock_queue, qty, method): |
| 385 | ValuationKlass = LIFOValuation if method == "LIFO" else FIFOValuation |
| 386 | |
| 387 | stock_queue = ValuationKlass(previous_stock_queue) |
| 388 | if flt(qty) >= 0: |
| 389 | total_qty, total_value = stock_queue.get_total_stock_and_value() |
| 390 | return total_value / total_qty if total_qty else 0.0 |
| 391 | else: |
| 392 | popped_bins = stock_queue.remove_stock(abs(flt(qty))) |
| 393 | |
| 394 | total_qty, total_value = ValuationKlass(popped_bins).get_total_stock_and_value() |
| 395 | return total_value / total_qty if total_qty else 0.0 |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 396 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 397 | |
| 398 | def get_valid_serial_nos(sr_nos, qty=0, item_code=""): |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 399 | """split serial nos, validate and return list of valid serial nos""" |
| 400 | # TODO: remove duplicates in client side |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 401 | serial_nos = cstr(sr_nos).strip().replace(",", "\n").split("\n") |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 402 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 403 | valid_serial_nos = [] |
| 404 | for val in serial_nos: |
| 405 | if val: |
| 406 | val = val.strip() |
| 407 | if val in valid_serial_nos: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 408 | frappe.throw(_("Serial number {0} entered more than once").format(val)) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 409 | else: |
| 410 | valid_serial_nos.append(val) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 411 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 412 | if qty and len(valid_serial_nos) != abs(qty): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 413 | frappe.throw(_("{0} valid serial nos for Item {1}").format(abs(qty), item_code)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 414 | |
Rushabh Mehta | 0dbe898 | 2013-02-04 13:56:50 +0530 | [diff] [blame] | 415 | return valid_serial_nos |
Nabin Hait | a72c512 | 2013-03-06 18:50:53 +0530 | [diff] [blame] | 416 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 417 | |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 418 | def validate_warehouse_company(warehouse, company): |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 419 | warehouse_company = frappe.db.get_value("Warehouse", warehouse, "company", cache=True) |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 420 | if warehouse_company and warehouse_company != company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 421 | frappe.throw( |
| 422 | _("Warehouse {0} does not belong to company {1}").format(warehouse, company), |
| 423 | InvalidWarehouseCompany, |
| 424 | ) |
| 425 | |
Saurabh | 3d6aecd | 2016-06-20 17:25:45 +0530 | [diff] [blame] | 426 | |
Saurabh | 4d02949 | 2016-06-23 12:44:06 +0530 | [diff] [blame] | 427 | def is_group_warehouse(warehouse): |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 428 | if frappe.db.get_value("Warehouse", warehouse, "is_group", cache=True): |
Saurabh | 4d02949 | 2016-06-23 12:44:06 +0530 | [diff] [blame] | 429 | frappe.throw(_("Group node warehouse is not allowed to select for transactions")) |
Saif | b4cf72c | 2018-10-18 17:29:47 +0500 | [diff] [blame] | 430 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 431 | |
Jannat Patel | 30c8873 | 2021-02-11 11:46:48 +0530 | [diff] [blame] | 432 | def validate_disabled_warehouse(warehouse): |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 433 | if frappe.db.get_value("Warehouse", warehouse, "disabled", cache=True): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 434 | frappe.throw( |
| 435 | _("Disabled Warehouse {0} cannot be used for this transaction.").format( |
| 436 | get_link_to_form("Warehouse", warehouse) |
| 437 | ) |
| 438 | ) |
| 439 | |
Jannat Patel | 30c8873 | 2021-02-11 11:46:48 +0530 | [diff] [blame] | 440 | |
Saif | b4cf72c | 2018-10-18 17:29:47 +0500 | [diff] [blame] | 441 | def update_included_uom_in_report(columns, result, include_uom, conversion_factors): |
| 442 | if not include_uom or not conversion_factors: |
| 443 | return |
| 444 | |
rohitwaghchaure | ed1cc18 | 2019-09-30 15:15:52 +0530 | [diff] [blame] | 445 | is_dict_obj = False |
| 446 | if isinstance(result[0], dict): |
| 447 | is_dict_obj = True |
| 448 | |
| 449 | convertible_columns = {} |
| 450 | for idx, d in enumerate(columns): |
| 451 | key = d.get("fieldname") if is_dict_obj else idx |
| 452 | if d.get("convertible"): |
| 453 | convertible_columns.setdefault(key, d.get("convertible")) |
| 454 | |
| 455 | # Add new column to show qty/rate as per the selected UOM |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 456 | columns.insert( |
| 457 | idx + 1, |
| 458 | { |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 459 | "label": "{} (per {})".format(d.get("label"), include_uom), |
| 460 | "fieldname": "{}_{}".format(d.get("fieldname"), frappe.scrub(include_uom)), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 461 | "fieldtype": "Currency" if d.get("convertible") == "rate" else "Float", |
| 462 | }, |
| 463 | ) |
Saif | b4cf72c | 2018-10-18 17:29:47 +0500 | [diff] [blame] | 464 | |
rohitwaghchaure | 001ee5e | 2019-11-11 17:43:48 +0530 | [diff] [blame] | 465 | update_dict_values = [] |
Saif | b4cf72c | 2018-10-18 17:29:47 +0500 | [diff] [blame] | 466 | for row_idx, row in enumerate(result): |
rohitwaghchaure | ed1cc18 | 2019-09-30 15:15:52 +0530 | [diff] [blame] | 467 | data = row.items() if is_dict_obj else enumerate(row) |
| 468 | for key, value in data: |
Noah Jacob | d8668f7 | 2021-07-15 18:32:15 +0530 | [diff] [blame] | 469 | if key not in convertible_columns: |
rohitwaghchaure | ed1cc18 | 2019-09-30 15:15:52 +0530 | [diff] [blame] | 470 | continue |
Noah Jacob | d8668f7 | 2021-07-15 18:32:15 +0530 | [diff] [blame] | 471 | # If no conversion factor for the UOM, defaults to 1 |
| 472 | if not conversion_factors[row_idx]: |
| 473 | conversion_factors[row_idx] = 1 |
Saif | b4cf72c | 2018-10-18 17:29:47 +0500 | [diff] [blame] | 474 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 475 | if convertible_columns.get(key) == "rate": |
Noah Jacob | d8668f7 | 2021-07-15 18:32:15 +0530 | [diff] [blame] | 476 | new_value = flt(value) * conversion_factors[row_idx] |
rohitwaghchaure | ed1cc18 | 2019-09-30 15:15:52 +0530 | [diff] [blame] | 477 | else: |
Noah Jacob | d8668f7 | 2021-07-15 18:32:15 +0530 | [diff] [blame] | 478 | new_value = flt(value) / conversion_factors[row_idx] |
rohitwaghchaure | ed1cc18 | 2019-09-30 15:15:52 +0530 | [diff] [blame] | 479 | |
| 480 | if not is_dict_obj: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 481 | row.insert(key + 1, new_value) |
rohitwaghchaure | ed1cc18 | 2019-09-30 15:15:52 +0530 | [diff] [blame] | 482 | else: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 483 | new_key = f"{key}_{frappe.scrub(include_uom)}" |
rohitwaghchaure | 001ee5e | 2019-11-11 17:43:48 +0530 | [diff] [blame] | 484 | update_dict_values.append([row, new_key, new_value]) |
| 485 | |
| 486 | for data in update_dict_values: |
| 487 | row, key, value = data |
| 488 | row[key] = value |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 489 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 490 | |
Suraj Shetty | bc001d2 | 2019-09-16 19:57:04 +0530 | [diff] [blame] | 491 | def add_additional_uom_columns(columns, result, include_uom, conversion_factors): |
| 492 | if not include_uom or not conversion_factors: |
| 493 | return |
| 494 | |
| 495 | convertible_column_map = {} |
| 496 | for col_idx in list(reversed(range(0, len(columns)))): |
| 497 | col = columns[col_idx] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 498 | if isinstance(col, dict) and col.get("convertible") in ["rate", "qty"]: |
Suraj Shetty | bc001d2 | 2019-09-16 19:57:04 +0530 | [diff] [blame] | 499 | next_col = col_idx + 1 |
| 500 | columns.insert(next_col, col.copy()) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 501 | columns[next_col]["fieldname"] += "_alt" |
| 502 | convertible_column_map[col.get("fieldname")] = frappe._dict( |
| 503 | {"converted_col": columns[next_col]["fieldname"], "for_type": col.get("convertible")} |
| 504 | ) |
| 505 | if col.get("convertible") == "rate": |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 506 | columns[next_col]["label"] += f" (per {include_uom})" |
Suraj Shetty | bc001d2 | 2019-09-16 19:57:04 +0530 | [diff] [blame] | 507 | else: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 508 | columns[next_col]["label"] += f" ({include_uom})" |
Suraj Shetty | bc001d2 | 2019-09-16 19:57:04 +0530 | [diff] [blame] | 509 | |
| 510 | for row_idx, row in enumerate(result): |
| 511 | for convertible_col, data in convertible_column_map.items(): |
Rohit Waghchaure | a627d2a | 2023-06-20 15:55:18 +0530 | [diff] [blame] | 512 | conversion_factor = conversion_factors.get(row.get("item_code")) or 1.0 |
Suraj Shetty | bc001d2 | 2019-09-16 19:57:04 +0530 | [diff] [blame] | 513 | for_type = data.for_type |
| 514 | value_before_conversion = row.get(convertible_col) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 515 | if for_type == "rate": |
Suraj Shetty | bc001d2 | 2019-09-16 19:57:04 +0530 | [diff] [blame] | 516 | row[data.converted_col] = flt(value_before_conversion) * conversion_factor |
| 517 | else: |
| 518 | row[data.converted_col] = flt(value_before_conversion) / conversion_factor |
| 519 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 520 | result[row_idx] = row |
| 521 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 522 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 523 | def get_incoming_outgoing_rate_for_cancel(item_code, voucher_type, voucher_no, voucher_detail_no): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 524 | outgoing_rate = frappe.db.sql( |
Conor | ea28ed1 | 2022-06-17 10:47:48 -0500 | [diff] [blame] | 525 | """SELECT CASE WHEN actual_qty = 0 THEN 0 ELSE abs(stock_value_difference / actual_qty) END |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 526 | FROM `tabStock Ledger Entry` |
| 527 | WHERE voucher_type = %s and voucher_no = %s |
| 528 | and item_code = %s and voucher_detail_no = %s |
| 529 | ORDER BY CREATION DESC limit 1""", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 530 | (voucher_type, voucher_no, item_code, voucher_detail_no), |
| 531 | ) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 532 | |
| 533 | outgoing_rate = outgoing_rate[0][0] if outgoing_rate else 0.0 |
| 534 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 535 | return outgoing_rate |
| 536 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 537 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 538 | def is_reposting_item_valuation_in_progress(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 539 | reposting_in_progress = frappe.db.exists( |
| 540 | "Repost Item Valuation", {"docstatus": 1, "status": ["in", ["Queued", "In Progress"]]} |
| 541 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 542 | if reposting_in_progress: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 543 | frappe.msgprint( |
| 544 | _("Item valuation reposting in progress. Report might show incorrect item valuation."), alert=1 |
| 545 | ) |
| 546 | |
Ankush Menat | d37541d | 2021-12-10 12:04:10 +0530 | [diff] [blame] | 547 | |
| 548 | def check_pending_reposting(posting_date: str, throw_error: bool = True) -> bool: |
| 549 | """Check if there are pending reposting job till the specified posting date.""" |
| 550 | |
| 551 | filters = { |
| 552 | "docstatus": 1, |
Ankush Menat | b12fe0f | 2022-03-29 13:54:26 +0530 | [diff] [blame] | 553 | "status": ["in", ["Queued", "In Progress"]], |
Ankush Menat | d37541d | 2021-12-10 12:04:10 +0530 | [diff] [blame] | 554 | "posting_date": ["<=", posting_date], |
| 555 | } |
| 556 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 557 | reposting_pending = frappe.db.exists("Repost Item Valuation", filters) |
Ankush Menat | d37541d | 2021-12-10 12:04:10 +0530 | [diff] [blame] | 558 | if reposting_pending and throw_error: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 559 | msg = _( |
| 560 | "Stock/Accounts can not be frozen as processing of backdated entries is going on. Please try again later." |
| 561 | ) |
| 562 | frappe.msgprint( |
| 563 | msg, |
| 564 | raise_exception=PendingRepostingError, |
| 565 | title="Stock Reposting Ongoing", |
| 566 | indicator="red", |
| 567 | primary_action={ |
| 568 | "label": _("Show pending entries"), |
| 569 | "client_action": "erpnext.route_to_pending_reposts", |
| 570 | "args": filters, |
| 571 | }, |
| 572 | ) |
Ankush Menat | d37541d | 2021-12-10 12:04:10 +0530 | [diff] [blame] | 573 | |
| 574 | return bool(reposting_pending) |
Ankush Menat | 47f27a5 | 2022-04-01 15:20:40 +0530 | [diff] [blame] | 575 | |
| 576 | |
| 577 | @frappe.whitelist() |
Devin Slauenwhite | b88e850 | 2022-10-20 06:26:07 -0400 | [diff] [blame] | 578 | def scan_barcode(search_value: str) -> BarcodeScanResult: |
| 579 | def set_cache(data: BarcodeScanResult): |
| 580 | frappe.cache().set_value(f"erpnext:barcode_scan:{search_value}", data, expires_in_sec=120) |
| 581 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 582 | def get_cache() -> BarcodeScanResult | None: |
Devin Slauenwhite | b88e850 | 2022-10-20 06:26:07 -0400 | [diff] [blame] | 583 | if data := frappe.cache().get_value(f"erpnext:barcode_scan:{search_value}"): |
| 584 | return data |
| 585 | |
| 586 | if scan_data := get_cache(): |
| 587 | return scan_data |
Ankush Menat | 47f27a5 | 2022-04-01 15:20:40 +0530 | [diff] [blame] | 588 | |
| 589 | # search barcode no |
| 590 | barcode_data = frappe.db.get_value( |
| 591 | "Item Barcode", |
| 592 | {"barcode": search_value}, |
Ankush Menat | 3974fbb | 2022-06-01 16:43:56 +0530 | [diff] [blame] | 593 | ["barcode", "parent as item_code", "uom"], |
Ankush Menat | 47f27a5 | 2022-04-01 15:20:40 +0530 | [diff] [blame] | 594 | as_dict=True, |
| 595 | ) |
| 596 | if barcode_data: |
Devin Slauenwhite | b88e850 | 2022-10-20 06:26:07 -0400 | [diff] [blame] | 597 | _update_item_info(barcode_data) |
| 598 | set_cache(barcode_data) |
| 599 | return barcode_data |
Ankush Menat | 47f27a5 | 2022-04-01 15:20:40 +0530 | [diff] [blame] | 600 | |
| 601 | # search serial no |
| 602 | serial_no_data = frappe.db.get_value( |
| 603 | "Serial No", |
| 604 | search_value, |
| 605 | ["name as serial_no", "item_code", "batch_no"], |
| 606 | as_dict=True, |
| 607 | ) |
| 608 | if serial_no_data: |
Devin Slauenwhite | b88e850 | 2022-10-20 06:26:07 -0400 | [diff] [blame] | 609 | _update_item_info(serial_no_data) |
| 610 | set_cache(serial_no_data) |
| 611 | return serial_no_data |
Ankush Menat | 47f27a5 | 2022-04-01 15:20:40 +0530 | [diff] [blame] | 612 | |
| 613 | # search batch no |
| 614 | batch_no_data = frappe.db.get_value( |
| 615 | "Batch", |
| 616 | search_value, |
| 617 | ["name as batch_no", "item as item_code"], |
| 618 | as_dict=True, |
| 619 | ) |
| 620 | if batch_no_data: |
rohitwaghchaure | f09e213 | 2024-01-04 14:58:02 +0530 | [diff] [blame] | 621 | if frappe.get_cached_value("Item", batch_no_data.item_code, "has_serial_no"): |
| 622 | frappe.throw( |
| 623 | _( |
| 624 | "Batch No {0} is linked with Item {1} which has serial no. Please scan serial no instead." |
| 625 | ).format(search_value, batch_no_data.item_code) |
| 626 | ) |
| 627 | |
Devin Slauenwhite | b88e850 | 2022-10-20 06:26:07 -0400 | [diff] [blame] | 628 | _update_item_info(batch_no_data) |
| 629 | set_cache(batch_no_data) |
Ankush Menat | 11207c4 | 2022-10-20 16:17:57 +0530 | [diff] [blame] | 630 | return batch_no_data |
Ankush Menat | 47f27a5 | 2022-04-01 15:20:40 +0530 | [diff] [blame] | 631 | |
| 632 | return {} |
| 633 | |
| 634 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 635 | def _update_item_info(scan_result: dict[str, str | None]) -> dict[str, str | None]: |
Ankush Menat | 47f27a5 | 2022-04-01 15:20:40 +0530 | [diff] [blame] | 636 | if item_code := scan_result.get("item_code"): |
| 637 | if item_info := frappe.get_cached_value( |
| 638 | "Item", |
| 639 | item_code, |
| 640 | ["has_batch_no", "has_serial_no"], |
| 641 | as_dict=True, |
| 642 | ): |
| 643 | scan_result.update(item_info) |
| 644 | return scan_result |
Rohit Waghchaure | d80ca52 | 2024-02-07 21:56:21 +0530 | [diff] [blame] | 645 | |
| 646 | |
| 647 | def get_combine_datetime(posting_date, posting_time): |
| 648 | import datetime |
| 649 | |
| 650 | if isinstance(posting_date, str): |
| 651 | posting_date = getdate(posting_date) |
| 652 | |
| 653 | if isinstance(posting_time, str): |
| 654 | posting_time = get_time(posting_time) |
| 655 | |
| 656 | if isinstance(posting_time, datetime.timedelta): |
| 657 | posting_time = (datetime.datetime.min + posting_time).time() |
| 658 | |
Rohit Waghchaure | a73ba2c | 2024-02-21 17:32:02 +0530 | [diff] [blame] | 659 | return datetime.datetime.combine(posting_date, posting_time).replace(microsecond=0) |