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