mirror of
https://github.com/frappe/erpnext.git
synced 2026-03-29 17:11:33 +02:00
fix: incorrect available qty for backdated stock reco with batch (#36581)
This commit is contained in:
@@ -6,7 +6,7 @@ from typing import Optional
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _, bold, msgprint
|
from frappe import _, bold, msgprint
|
||||||
from frappe.query_builder.functions import CombineDatetime, Sum
|
from frappe.query_builder.functions import CombineDatetime, Sum
|
||||||
from frappe.utils import cint, cstr, flt
|
from frappe.utils import add_to_date, cint, cstr, flt
|
||||||
|
|
||||||
import erpnext
|
import erpnext
|
||||||
from erpnext.accounts.utils import get_company_default
|
from erpnext.accounts.utils import get_company_default
|
||||||
@@ -570,44 +570,58 @@ class StockReconciliation(StockController):
|
|||||||
else:
|
else:
|
||||||
self._cancel()
|
self._cancel()
|
||||||
|
|
||||||
def recalculate_current_qty(self, item_code, batch_no):
|
def recalculate_current_qty(self, voucher_detail_no, sle_creation, add_new_sle=False):
|
||||||
from erpnext.stock.stock_ledger import get_valuation_rate
|
from erpnext.stock.stock_ledger import get_valuation_rate
|
||||||
|
|
||||||
sl_entries = []
|
sl_entries = []
|
||||||
for row in self.items:
|
for row in self.items:
|
||||||
if not (row.item_code == item_code and row.batch_no == batch_no):
|
if voucher_detail_no != row.name:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
current_qty = get_batch_qty_for_stock_reco(
|
current_qty = get_batch_qty_for_stock_reco(
|
||||||
item_code, row.warehouse, batch_no, self.posting_date, self.posting_time, self.name
|
row.item_code, row.warehouse, row.batch_no, self.posting_date, self.posting_time, self.name
|
||||||
)
|
)
|
||||||
|
|
||||||
precesion = row.precision("current_qty")
|
precesion = row.precision("current_qty")
|
||||||
if flt(current_qty, precesion) == flt(row.current_qty, precesion):
|
if flt(current_qty, precesion) != flt(row.current_qty, precesion):
|
||||||
continue
|
val_rate = get_valuation_rate(
|
||||||
|
row.item_code,
|
||||||
|
row.warehouse,
|
||||||
|
self.doctype,
|
||||||
|
self.name,
|
||||||
|
company=self.company,
|
||||||
|
batch_no=row.batch_no,
|
||||||
|
)
|
||||||
|
|
||||||
val_rate = get_valuation_rate(
|
row.current_valuation_rate = val_rate
|
||||||
item_code, row.warehouse, self.doctype, self.name, company=self.company, batch_no=batch_no
|
row.current_qty = current_qty
|
||||||
)
|
row.db_set(
|
||||||
|
{
|
||||||
|
"current_qty": row.current_qty,
|
||||||
|
"current_valuation_rate": row.current_valuation_rate,
|
||||||
|
"current_amount": flt(row.current_qty * row.current_valuation_rate),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
row.current_valuation_rate = val_rate
|
if (
|
||||||
if not row.current_qty and current_qty:
|
add_new_sle
|
||||||
sle = self.get_sle_for_items(row)
|
and not frappe.db.get_value(
|
||||||
sle.actual_qty = current_qty * -1
|
"Stock Ledger Entry",
|
||||||
sle.valuation_rate = val_rate
|
{"voucher_detail_no": row.name, "actual_qty": ("<", 0), "is_cancelled": 0},
|
||||||
sl_entries.append(sle)
|
"name",
|
||||||
|
)
|
||||||
row.current_qty = current_qty
|
and current_qty
|
||||||
row.db_set(
|
):
|
||||||
{
|
new_sle = self.get_sle_for_items(row)
|
||||||
"current_qty": row.current_qty,
|
new_sle.actual_qty = current_qty * -1
|
||||||
"current_valuation_rate": row.current_valuation_rate,
|
new_sle.valuation_rate = row.current_valuation_rate
|
||||||
"current_amount": flt(row.current_qty * row.current_valuation_rate),
|
new_sle.creation_time = add_to_date(sle_creation, seconds=-1)
|
||||||
}
|
sl_entries.append(new_sle)
|
||||||
)
|
|
||||||
|
|
||||||
if sl_entries:
|
if sl_entries:
|
||||||
self.make_sl_entries(sl_entries, allow_negative_stock=True)
|
self.make_sl_entries(sl_entries, allow_negative_stock=True)
|
||||||
|
if frappe.db.exists("Repost Item Valuation", {"voucher_no": self.name, "status": "Queued"}):
|
||||||
|
self.repost_future_sle_and_gle(force=True)
|
||||||
|
|
||||||
|
|
||||||
def get_batch_qty_for_stock_reco(
|
def get_batch_qty_for_stock_reco(
|
||||||
|
|||||||
@@ -759,13 +759,6 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
|
|||||||
|
|
||||||
se2.cancel()
|
se2.cancel()
|
||||||
|
|
||||||
self.assertTrue(frappe.db.exists("Repost Item Valuation", {"voucher_no": stock_reco.name}))
|
|
||||||
|
|
||||||
self.assertEqual(
|
|
||||||
frappe.db.get_value("Repost Item Valuation", {"voucher_no": stock_reco.name}, "status"),
|
|
||||||
"Completed",
|
|
||||||
)
|
|
||||||
|
|
||||||
sle = frappe.get_all(
|
sle = frappe.get_all(
|
||||||
"Stock Ledger Entry",
|
"Stock Ledger Entry",
|
||||||
filters={"item_code": item_code, "warehouse": warehouse, "is_cancelled": 0},
|
filters={"item_code": item_code, "warehouse": warehouse, "is_cancelled": 0},
|
||||||
@@ -775,6 +768,62 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
|
|||||||
|
|
||||||
self.assertEqual(flt(sle[0].qty_after_transaction), flt(50.0))
|
self.assertEqual(flt(sle[0].qty_after_transaction), flt(50.0))
|
||||||
|
|
||||||
|
def test_backdated_stock_reco_entry_with_batch(self):
|
||||||
|
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
|
||||||
|
|
||||||
|
item_code = self.make_item(
|
||||||
|
"Test New Batch Item ABCVSD",
|
||||||
|
{
|
||||||
|
"is_stock_item": 1,
|
||||||
|
"has_batch_no": 1,
|
||||||
|
"batch_number_series": "BNS9.####",
|
||||||
|
"create_new_batch": 1,
|
||||||
|
},
|
||||||
|
).name
|
||||||
|
|
||||||
|
warehouse = "_Test Warehouse - _TC"
|
||||||
|
|
||||||
|
# Stock Reco for 100, Balace Qty 100
|
||||||
|
stock_reco = create_stock_reconciliation(
|
||||||
|
item_code=item_code,
|
||||||
|
posting_date=nowdate(),
|
||||||
|
posting_time="11:00:00",
|
||||||
|
warehouse=warehouse,
|
||||||
|
qty=100,
|
||||||
|
rate=100,
|
||||||
|
)
|
||||||
|
|
||||||
|
sles = frappe.get_all(
|
||||||
|
"Stock Ledger Entry",
|
||||||
|
fields=["actual_qty", "batch_no"],
|
||||||
|
filters={"voucher_no": stock_reco.name, "is_cancelled": 0},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(len(sles), 1)
|
||||||
|
|
||||||
|
# Stock Reco for 100, Balace Qty 100
|
||||||
|
create_stock_reconciliation(
|
||||||
|
item_code=item_code,
|
||||||
|
posting_date=add_days(nowdate(), -1),
|
||||||
|
posting_time="11:00:00",
|
||||||
|
batch_no=sles[0].batch_no,
|
||||||
|
warehouse=warehouse,
|
||||||
|
qty=60,
|
||||||
|
rate=100,
|
||||||
|
)
|
||||||
|
|
||||||
|
sles = frappe.get_all(
|
||||||
|
"Stock Ledger Entry",
|
||||||
|
fields=["actual_qty"],
|
||||||
|
filters={"voucher_no": stock_reco.name, "is_cancelled": 0},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(len(sles), 2)
|
||||||
|
|
||||||
|
for row in sles:
|
||||||
|
if row.actual_qty < 0:
|
||||||
|
self.assertEqual(row.actual_qty, -60)
|
||||||
|
|
||||||
def test_update_stock_reconciliation_while_reposting(self):
|
def test_update_stock_reconciliation_while_reposting(self):
|
||||||
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
|
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
|
||||||
|
|
||||||
|
|||||||
@@ -199,6 +199,11 @@ def make_entry(args, allow_negative_stock=False, via_landed_cost_voucher=False):
|
|||||||
sle.allow_negative_stock = allow_negative_stock
|
sle.allow_negative_stock = allow_negative_stock
|
||||||
sle.via_landed_cost_voucher = via_landed_cost_voucher
|
sle.via_landed_cost_voucher = via_landed_cost_voucher
|
||||||
sle.submit()
|
sle.submit()
|
||||||
|
|
||||||
|
# Added to handle the case when the stock ledger entry is created from the repostig
|
||||||
|
if args.get("creation_time") and args.get("voucher_type") == "Stock Reconciliation":
|
||||||
|
sle.db_set("creation", args.get("creation_time"))
|
||||||
|
|
||||||
return sle
|
return sle
|
||||||
|
|
||||||
|
|
||||||
@@ -564,12 +569,7 @@ class update_entries_after(object):
|
|||||||
if not self.args.get("sle_id"):
|
if not self.args.get("sle_id"):
|
||||||
self.get_dynamic_incoming_outgoing_rate(sle)
|
self.get_dynamic_incoming_outgoing_rate(sle)
|
||||||
|
|
||||||
if (
|
if sle.voucher_type == "Stock Reconciliation" and sle.batch_no and sle.voucher_detail_no:
|
||||||
sle.voucher_type == "Stock Reconciliation"
|
|
||||||
and sle.batch_no
|
|
||||||
and sle.voucher_detail_no
|
|
||||||
and sle.actual_qty < 0
|
|
||||||
):
|
|
||||||
self.reset_actual_qty_for_stock_reco(sle)
|
self.reset_actual_qty_for_stock_reco(sle)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -634,14 +634,17 @@ class update_entries_after(object):
|
|||||||
self.update_outgoing_rate_on_transaction(sle)
|
self.update_outgoing_rate_on_transaction(sle)
|
||||||
|
|
||||||
def reset_actual_qty_for_stock_reco(self, sle):
|
def reset_actual_qty_for_stock_reco(self, sle):
|
||||||
current_qty = frappe.get_cached_value(
|
doc = frappe.get_cached_doc("Stock Reconciliation", sle.voucher_no)
|
||||||
"Stock Reconciliation Item", sle.voucher_detail_no, "current_qty"
|
doc.recalculate_current_qty(sle.voucher_detail_no, sle.creation, sle.actual_qty > 0)
|
||||||
)
|
|
||||||
|
|
||||||
if current_qty:
|
if sle.actual_qty < 0:
|
||||||
sle.actual_qty = current_qty * -1
|
sle.actual_qty = (
|
||||||
elif current_qty == 0:
|
flt(frappe.db.get_value("Stock Reconciliation Item", sle.voucher_detail_no, "current_qty"))
|
||||||
sle.is_cancelled = 1
|
* -1
|
||||||
|
)
|
||||||
|
|
||||||
|
if abs(sle.actual_qty) == 0.0:
|
||||||
|
sle.is_cancelled = 1
|
||||||
|
|
||||||
def validate_negative_stock(self, sle):
|
def validate_negative_stock(self, sle):
|
||||||
"""
|
"""
|
||||||
@@ -1433,8 +1436,6 @@ def update_qty_in_future_sle(args, allow_negative_stock=False):
|
|||||||
next_stock_reco_detail = get_next_stock_reco(args)
|
next_stock_reco_detail = get_next_stock_reco(args)
|
||||||
if next_stock_reco_detail:
|
if next_stock_reco_detail:
|
||||||
detail = next_stock_reco_detail[0]
|
detail = next_stock_reco_detail[0]
|
||||||
if detail.batch_no:
|
|
||||||
regenerate_sle_for_batch_stock_reco(detail)
|
|
||||||
|
|
||||||
# add condition to update SLEs before this date & time
|
# add condition to update SLEs before this date & time
|
||||||
datetime_limit_condition = get_datetime_limit_condition(detail)
|
datetime_limit_condition = get_datetime_limit_condition(detail)
|
||||||
@@ -1463,16 +1464,6 @@ def update_qty_in_future_sle(args, allow_negative_stock=False):
|
|||||||
validate_negative_qty_in_future_sle(args, allow_negative_stock)
|
validate_negative_qty_in_future_sle(args, allow_negative_stock)
|
||||||
|
|
||||||
|
|
||||||
def regenerate_sle_for_batch_stock_reco(detail):
|
|
||||||
doc = frappe.get_cached_doc("Stock Reconciliation", detail.voucher_no)
|
|
||||||
doc.recalculate_current_qty(detail.item_code, detail.batch_no)
|
|
||||||
|
|
||||||
if not frappe.db.exists(
|
|
||||||
"Repost Item Valuation", {"voucher_no": doc.name, "status": "Queued", "docstatus": "1"}
|
|
||||||
):
|
|
||||||
doc.repost_future_sle_and_gle(force=True)
|
|
||||||
|
|
||||||
|
|
||||||
def get_stock_reco_qty_shift(args):
|
def get_stock_reco_qty_shift(args):
|
||||||
stock_reco_qty_shift = 0
|
stock_reco_qty_shift = 0
|
||||||
if args.get("is_cancelled"):
|
if args.get("is_cancelled"):
|
||||||
|
|||||||
Reference in New Issue
Block a user