Skip to content

Commit

Permalink
fix: stock reco with incorrect serial numbers (#42865)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure authored Aug 22, 2024
1 parent a95a052 commit 54842ea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
28 changes: 8 additions & 20 deletions erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import frappe
from frappe import _, bold, msgprint
from frappe.query_builder.functions import CombineDatetime, Sum
from frappe.utils import add_to_date, cint, cstr, flt
from frappe.utils import add_to_date, cint, cstr, flt, get_link_to_form

import erpnext
from erpnext.accounts.utils import get_company_default
Expand Down Expand Up @@ -357,7 +357,6 @@ def get_sle_for_serialized_items(self, row, sl_entries, item):

sl_entries.append(args)

qty_after_transaction = 0
for serial_no in serial_nos:
args = self.get_sle_for_items(row, [serial_no])

Expand All @@ -373,27 +372,16 @@ def get_sle_for_serialized_items(self, row, sl_entries, item):
if previous_sle and row.warehouse != previous_sle.get("warehouse"):
# If serial no exists in different warehouse

warehouse = previous_sle.get("warehouse", "") or row.warehouse

if not qty_after_transaction:
qty_after_transaction = get_stock_balance(
row.item_code, warehouse, self.posting_date, self.posting_time
frappe.throw(
_(
"The Serial No {0} already exists in the warehouse {1}. It cannot be transferred to the warehouse {2}"
).format(
get_link_to_form("Serial No", serial_no),
bold(previous_sle.get("warehouse")),
row.warehouse,
)

qty_after_transaction -= 1

new_args = args.copy()
new_args.update(
{
"actual_qty": -1,
"qty_after_transaction": qty_after_transaction,
"warehouse": warehouse,
"valuation_rate": previous_sle.get("valuation_rate"),
}
)

sl_entries.append(new_args)

if row.qty:
args = self.get_sle_for_items(row)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,33 @@ def test_backdated_purchase_receipt_with_stock_reco(self):
active_serial_no = frappe.get_all("Serial No", filters={"status": "Active", "item_code": item_code})
self.assertEqual(len(active_serial_no), 5)

def test_stock_reco_for_serialized_item_with_different_warehouse(self):
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry

# Add new serial nos
serial_item_code = "Stock-Reco-Serial-Item-11"
warehouse = "_Test Warehouse - _TC"
serial_warehouse = "_Test Warehouse for Stock Reco1 - _TC"

self.make_item(
serial_item_code, {"is_stock_item": 1, "has_serial_no": 1, "serial_no_series": "SNT-SRS11.####"}
)

make_stock_entry(item_code=serial_item_code, target=warehouse, qty=10, basic_rate=100)
stock_entry = make_stock_entry(
item_code=serial_item_code, target=serial_warehouse, qty=10, basic_rate=200
)

sr = create_stock_reconciliation(
item_code=serial_item_code, warehouse=warehouse, qty=11, rate=200, do_not_submit=True
)

serial_nos = get_serial_nos(stock_entry.items[0].serial_no)

sr.items[0].serial_no += f"\n{serial_nos[0]}"
sr.save()
self.assertRaises(frappe.ValidationError, sr.submit)


def create_batch_item_with_batch(item_name, batch_id):
batch_item_doc = create_item(item_name, is_stock_item=1)
Expand Down

0 comments on commit 54842ea

Please sign in to comment.