fix: Stock Reconciliation Insufficient Stock Error (#37494)

* fix: Stock Reconciliation Insufficient Stock Error

* fix: linter

* test: add test case for Stock Reco Batch Item
This commit is contained in:
s-aga-r
2023-10-14 16:53:29 +05:30
committed by GitHub
parent bae6c5bf5f
commit 9406ddbff0
2 changed files with 77 additions and 19 deletions

View File

@@ -956,6 +956,58 @@ class TestStockReconciliation(FrappeTestCase, StockTestMixin):
self.assertRaises(frappe.ValidationError, sr.save) self.assertRaises(frappe.ValidationError, sr.save)
@change_settings("Stock Settings", {"allow_negative_stock": 0})
def test_backdated_stock_reco_for_batch_item_dont_have_future_sle(self):
# Step - 1: Create a Batch Item
from erpnext.stock.doctype.item.test_item import make_item
item = make_item(
properties={
"is_stock_item": 1,
"has_batch_no": 1,
"create_new_batch": 1,
"batch_number_series": "TEST-BATCH-.###",
}
).name
# Step - 2: Create Opening Stock Reconciliation
sr1 = create_stock_reconciliation(
item_code=item,
warehouse="_Test Warehouse - _TC",
qty=10,
purpose="Opening Stock",
posting_date=add_days(nowdate(), -2),
)
# Step - 3: Create Stock Entry (Material Receipt)
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
se1 = make_stock_entry(
item_code=item,
target="_Test Warehouse - _TC",
qty=100,
)
# Step - 4: Create Stock Entry (Material Issue)
make_stock_entry(
item_code=item,
source="_Test Warehouse - _TC",
qty=100,
batch_no=se1.items[0].batch_no,
purpose="Material Issue",
)
# Step - 5: Create Stock Reconciliation (Backdated) after the Stock Reconciliation 1 (Step - 2)
sr2 = create_stock_reconciliation(
item_code=item,
warehouse="_Test Warehouse - _TC",
qty=5,
batch_no=sr1.items[0].batch_no,
posting_date=add_days(nowdate(), -1),
)
self.assertEqual(sr2.docstatus, 1)
def create_batch_item_with_batch(item_name, batch_id): def create_batch_item_with_batch(item_name, batch_id):
batch_item_doc = create_item(item_name, is_stock_item=1) batch_item_doc = create_item(item_name, is_stock_item=1)

View File

@@ -1617,27 +1617,33 @@ def is_negative_with_precision(neg_sle, is_batch=False):
return qty_deficit < 0 and abs(qty_deficit) > 0.0001 return qty_deficit < 0 and abs(qty_deficit) > 0.0001
def get_future_sle_with_negative_qty(args): def get_future_sle_with_negative_qty(sle):
return frappe.db.sql( SLE = frappe.qb.DocType("Stock Ledger Entry")
""" query = (
select frappe.qb.from_(SLE)
qty_after_transaction, posting_date, posting_time, .select(
voucher_type, voucher_no SLE.qty_after_transaction, SLE.posting_date, SLE.posting_time, SLE.voucher_type, SLE.voucher_no
from `tabStock Ledger Entry` )
where .where(
item_code = %(item_code)s (SLE.item_code == sle.item_code)
and warehouse = %(warehouse)s & (SLE.warehouse == sle.warehouse)
and voucher_no != %(voucher_no)s & (SLE.voucher_no != sle.voucher_no)
and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) & (
and is_cancelled = 0 CombineDatetime(SLE.posting_date, SLE.posting_time)
and qty_after_transaction < 0 >= CombineDatetime(sle.posting_date, sle.posting_time)
order by timestamp(posting_date, posting_time) asc )
limit 1 & (SLE.is_cancelled == 0)
""", & (SLE.qty_after_transaction < 0)
args, )
as_dict=1, .orderby(CombineDatetime(SLE.posting_date, SLE.posting_time))
.limit(1)
) )
if sle.voucher_type == "Stock Reconciliation" and sle.batch_no:
query = query.where(SLE.batch_no == sle.batch_no)
return query.run(as_dict=True)
def get_future_sle_with_negative_batch_qty(args): def get_future_sle_with_negative_batch_qty(args):
return frappe.db.sql( return frappe.db.sql(