Skip to content

Commit

Permalink
Merge pull request #34814 from frappe/mergify/bp/version-14-hotfix/pr…
Browse files Browse the repository at this point in the history
…-34808

fix: reposting record not created for backdated stock reconciliation  (backport #34808)
  • Loading branch information
rohitwaghchaure authored Apr 11, 2023
2 parents 994272b + 9b90323 commit 21aea52
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
16 changes: 13 additions & 3 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 Sum
from frappe.query_builder.functions import CombineDatetime, Sum
from frappe.utils import cint, cstr, flt

import erpnext
Expand Down Expand Up @@ -575,7 +575,9 @@ def recalculate_current_qty(self, item_code, batch_no):
if not (row.item_code == item_code and row.batch_no == batch_no):
continue

row.current_qty = get_batch_qty_for_stock_reco(item_code, row.warehouse, batch_no)
row.current_qty = get_batch_qty_for_stock_reco(
item_code, row.warehouse, batch_no, self.posting_date, self.posting_time, self.name
)

qty, val_rate = get_stock_balance(
item_code,
Expand All @@ -596,7 +598,9 @@ def recalculate_current_qty(self, item_code, batch_no):
)


def get_batch_qty_for_stock_reco(item_code, warehouse, batch_no):
def get_batch_qty_for_stock_reco(
item_code, warehouse, batch_no, posting_date, posting_time, voucher_no
):
ledger = frappe.qb.DocType("Stock Ledger Entry")

query = (
Expand All @@ -610,6 +614,12 @@ def get_batch_qty_for_stock_reco(item_code, warehouse, batch_no):
& (ledger.docstatus == 1)
& (ledger.is_cancelled == 0)
& (ledger.batch_no == batch_no)
& (ledger.posting_date <= posting_date)
& (
CombineDatetime(ledger.posting_date, ledger.posting_time)
<= CombineDatetime(posting_date, posting_time)
)
& (ledger.voucher_no != voucher_no)
)
.groupby(ledger.batch_no)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,79 @@ def test_serial_no_batch_no_item(self):
self.assertEqual(flt(sl_entry.actual_qty), 1.0)
self.assertEqual(flt(sl_entry.qty_after_transaction), 1.0)

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

item_code = self.make_item(
"Test New Batch Item ABCV",
{
"is_stock_item": 1,
"has_batch_no": 1,
"batch_number_series": "BNS9.####",
"create_new_batch": 1,
},
).name

warehouse = "_Test Warehouse - _TC"

# Added 100 Qty, Balace Qty 100
se1 = make_stock_entry(
item_code=item_code, posting_time="09:00:00", target=warehouse, qty=100, basic_rate=700
)

# Removed 50 Qty, Balace Qty 50
se2 = make_stock_entry(
item_code=item_code,
batch_no=se1.items[0].batch_no,
posting_time="10:00:00",
source=warehouse,
qty=50,
basic_rate=700,
)

# Stock Reco for 100, Balace Qty 100
stock_reco = create_stock_reconciliation(
item_code=item_code,
posting_time="11:00:00",
warehouse=warehouse,
batch_no=se1.items[0].batch_no,
qty=100,
rate=100,
)

# Removed 50 Qty, Balace Qty 50
make_stock_entry(
item_code=item_code,
batch_no=se1.items[0].batch_no,
posting_time="12:00:00",
source=warehouse,
qty=50,
basic_rate=700,
)

self.assertFalse(frappe.db.exists("Repost Item Valuation", {"voucher_no": stock_reco.name}))

# Cancel the backdated Stock Entry se2,
# Since Stock Reco entry in the future the Balace Qty should remain as it's (50)

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(
"Stock Ledger Entry",
filters={"item_code": item_code, "warehouse": warehouse, "is_cancelled": 0},
fields=["qty_after_transaction"],
order_by="posting_time desc, creation desc",
)

self.assertEqual(flt(sle[0].qty_after_transaction), flt(50.0))


def create_batch_item_with_batch(item_name, batch_id):
batch_item_doc = create_item(item_name, is_stock_item=1)
Expand Down
1 change: 1 addition & 0 deletions erpnext/stock/stock_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,7 @@ def regenerate_sle_for_batch_stock_reco(detail):
doc.recalculate_current_qty(detail.item_code, detail.batch_no)
doc.docstatus = 1
doc.update_stock_ledger()
doc.repost_future_sle_and_gle()


def get_stock_reco_qty_shift(args):
Expand Down

0 comments on commit 21aea52

Please sign in to comment.