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