Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect active serial nos #39389

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions erpnext/public/js/utils/serial_no_batch_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ erpnext.SerialBatchPackageSelector = class SerialNoBatchBundleUpdate {
let warehouse = this.item?.type_of_transaction === "Outward" ?
(this.item.warehouse || this.item.s_warehouse) : "";

if (!warehouse && this.frm.doc.doctype === 'Stock Reconciliation') {
warehouse = this.get_warehouse();
}

return {
'item_code': this.item.item_code,
'warehouse': ["=", warehouse]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def set_incoming_rate_for_outward_transaction(

for d in self.entries:
available_qty = 0

if self.has_serial_no:
d.incoming_rate = abs(sn_obj.serial_no_incoming_rate.get(d.serial_no, 0.0))
else:
Expand Down Expand Up @@ -892,6 +893,13 @@ def add_serial_batch(self, data):
elif batch_nos:
self.set("entries", batch_nos)

def delete_serial_batch_entries(self):
SBBE = frappe.qb.DocType("Serial and Batch Entry")

frappe.qb.from_(SBBE).delete().where(SBBE.parent == self.name).run()

self.set("entries", [])


@frappe.whitelist()
def download_blank_csv_template(content):
Expand Down Expand Up @@ -1374,10 +1382,12 @@ def get_available_serial_nos(kwargs):
elif kwargs.based_on == "Expiry":
order_by = "amc_expiry_date asc"

filters = {"item_code": kwargs.item_code, "warehouse": ("is", "set")}
filters = {"item_code": kwargs.item_code}

if kwargs.warehouse:
filters["warehouse"] = kwargs.warehouse
if not kwargs.get("ignore_warehouse"):
filters["warehouse"] = ("is", "set")
if kwargs.warehouse:
filters["warehouse"] = kwargs.warehouse

# Since SLEs are not present against Reserved Stock [POS invoices, SRE], need to ignore reserved serial nos.
ignore_serial_nos = get_reserved_serial_nos(kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def set_current_serial_and_batch_bundle(self, voucher_detail_no=None, save=False
"warehouse": item.warehouse,
"posting_date": self.posting_date,
"posting_time": self.posting_time,
"ignore_warehouse": 1,
}
)
)
Expand Down Expand Up @@ -780,23 +781,37 @@ def recalculate_current_qty(self, voucher_detail_no, sle_creation, add_new_sle=F

current_qty = 0.0
if row.current_serial_and_batch_bundle:
current_qty = self.get_qty_for_serial_and_batch_bundle(row)
current_qty = self.get_current_qty_for_serial_or_batch(row)
elif row.serial_no:
item_dict = get_stock_balance_for(
row.item_code,
row.warehouse,
self.posting_date,
self.posting_time,
voucher_no=self.name,
)

current_qty = item_dict.get("qty")
row.current_serial_no = item_dict.get("serial_nos")
row.current_valuation_rate = item_dict.get("rate")
val_rate = item_dict.get("rate")
elif row.batch_no:
current_qty = get_batch_qty_for_stock_reco(
row.item_code, row.warehouse, row.batch_no, self.posting_date, self.posting_time, self.name
)

precesion = row.precision("current_qty")
if flt(current_qty, precesion) != flt(row.current_qty, precesion):
val_rate = get_valuation_rate(
row.item_code,
row.warehouse,
self.doctype,
self.name,
company=self.company,
batch_no=row.batch_no,
serial_and_batch_bundle=row.current_serial_and_batch_bundle,
)
if not row.serial_no:
val_rate = get_valuation_rate(
row.item_code,
row.warehouse,
self.doctype,
self.name,
company=self.company,
batch_no=row.batch_no,
serial_and_batch_bundle=row.current_serial_and_batch_bundle,
)

row.current_valuation_rate = val_rate
row.current_qty = current_qty
Expand Down Expand Up @@ -842,11 +857,56 @@ def has_negative_stock_allowed(self):

return allow_negative_stock

def get_qty_for_serial_and_batch_bundle(self, row):
def get_current_qty_for_serial_or_batch(self, row):
doc = frappe.get_doc("Serial and Batch Bundle", row.current_serial_and_batch_bundle)
precision = doc.entries[0].precision("qty")
current_qty = 0.0
if doc.has_serial_no:
current_qty = self.get_current_qty_for_serial_nos(doc)
elif doc.has_batch_no:
current_qty = self.get_current_qty_for_batch_nos(doc)

return abs(current_qty)

current_qty = 0
def get_current_qty_for_serial_nos(self, doc):
serial_nos_details = get_available_serial_nos(
frappe._dict(
{
"item_code": doc.item_code,
"warehouse": doc.warehouse,
"posting_date": self.posting_date,
"posting_time": self.posting_time,
"voucher_no": self.name,
"ignore_warehouse": 1,
}
)
)

if not serial_nos_details:
return 0.0

doc.delete_serial_batch_entries()
current_qty = 0.0
for serial_no_row in serial_nos_details:
current_qty += 1
doc.append(
"entries",
{
"serial_no": serial_no_row.serial_no,
"qty": -1,
"warehouse": doc.warehouse,
"batch_no": serial_no_row.batch_no,
},
)

doc.set_incoming_rate(save=True)
doc.calculate_qty_and_amount(save=True)
doc.db_update_all()

return current_qty

def get_current_qty_for_batch_nos(self, doc):
current_qty = 0.0
precision = doc.entries[0].precision("qty")
for d in doc.entries:
qty = (
get_batch_qty(
Expand All @@ -864,7 +924,7 @@ def get_qty_for_serial_and_batch_bundle(self, row):

current_qty += qty

return abs(current_qty)
return current_qty


def get_batch_qty_for_stock_reco(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,74 @@ def test_make_stock_zero_for_serial_batch_item(self):

self.assertEqual(len(serial_batch_bundle), 0)

def test_backdated_purchase_receipt_with_stock_reco(self):
item_code = self.make_item(
properties={
"is_stock_item": 1,
"has_serial_no": 1,
"serial_no_series": "TEST-SERIAL-.###",
}
).name

warehouse = "_Test Warehouse - _TC"

# Step - 1: Create a Backdated Purchase Receipt

pr1 = make_purchase_receipt(
item_code=item_code, warehouse=warehouse, qty=10, rate=100, posting_date=add_days(nowdate(), -3)
)
pr1.reload()

serial_nos = sorted(get_serial_nos_from_bundle(pr1.items[0].serial_and_batch_bundle))[:5]

# Step - 2: Create a Stock Reconciliation
sr1 = create_stock_reconciliation(
item_code=item_code,
warehouse=warehouse,
qty=5,
serial_no=serial_nos,
)

data = frappe.get_all(
"Stock Ledger Entry",
fields=["serial_no", "actual_qty", "stock_value_difference"],
filters={"voucher_no": sr1.name, "is_cancelled": 0},
order_by="creation",
)

for d in data:
if d.actual_qty < 0:
self.assertEqual(d.actual_qty, -10.0)
self.assertAlmostEqual(d.stock_value_difference, -1000.0)
else:
self.assertEqual(d.actual_qty, 5.0)
self.assertAlmostEqual(d.stock_value_difference, 500.0)

# Step - 3: Create a Purchase Receipt before the first Purchase Receipt
make_purchase_receipt(
item_code=item_code, warehouse=warehouse, qty=10, rate=200, posting_date=add_days(nowdate(), -5)
)

data = frappe.get_all(
"Stock Ledger Entry",
fields=["serial_no", "actual_qty", "stock_value_difference"],
filters={"voucher_no": sr1.name, "is_cancelled": 0},
order_by="creation",
)

for d in data:
if d.actual_qty < 0:
self.assertEqual(d.actual_qty, -20.0)
self.assertAlmostEqual(d.stock_value_difference, -3000.0)
else:
self.assertEqual(d.actual_qty, 5.0)
self.assertAlmostEqual(d.stock_value_difference, 500.0)

active_serial_no = frappe.get_all(
"Serial No", filters={"status": "Active", "item_code": item_code}
)
self.assertEqual(len(active_serial_no), 5)


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