Skip to content

Commit

Permalink
fix: incorrect stock balance quantity for batch item
Browse files Browse the repository at this point in the history
(cherry picked from commit ef4bd77)
  • Loading branch information
rohitwaghchaure authored and mergify[bot] committed Apr 5, 2023
1 parent 8ba1e0f commit d817c50
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
49 changes: 49 additions & 0 deletions erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import frappe
from frappe import _, bold, msgprint
from frappe.query_builder.functions import Sum
from frappe.utils import cint, cstr, flt

import erpnext
Expand Down Expand Up @@ -569,6 +570,54 @@ def cancel(self):
else:
self._cancel()

def recalculate_current_qty(self, item_code, batch_no):
for row in self.items:
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)

qty, val_rate = get_stock_balance(
item_code,
row.warehouse,
self.posting_date,
self.posting_time,
with_valuation_rate=True,
)

row.current_valuation_rate = val_rate

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),
}
)


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

query = (
frappe.qb.from_(ledger)
.select(
Sum(ledger.actual_qty).as_("batch_qty"),
)
.where(
(ledger.item_code == item_code)
& (ledger.warehouse == warehouse)
& (ledger.docstatus == 1)
& (ledger.is_cancelled == 0)
& (ledger.batch_no == batch_no)
)
.groupby(ledger.batch_no)
)

sle = query.run(as_dict=True)

return flt(sle[0].batch_qty) if sle else 0


@frappe.whitelist()
def get_items(
Expand Down
15 changes: 14 additions & 1 deletion erpnext/stock/stock_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,9 @@ def update_qty_in_future_sle(args, allow_negative_stock=False):
next_stock_reco_detail = get_next_stock_reco(args)
if next_stock_reco_detail:
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
datetime_limit_condition = get_datetime_limit_condition(detail)

Expand Down Expand Up @@ -1364,6 +1367,16 @@ def update_qty_in_future_sle(args, allow_negative_stock=False):
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.docstatus = 2
doc.update_stock_ledger()

doc.recalculate_current_qty(detail.item_code, detail.batch_no)
doc.docstatus = 1
doc.update_stock_ledger()


def get_stock_reco_qty_shift(args):
stock_reco_qty_shift = 0
if args.get("is_cancelled"):
Expand Down Expand Up @@ -1393,7 +1406,7 @@ def get_next_stock_reco(args):
return frappe.db.sql(
"""
select
name, posting_date, posting_time, creation, voucher_no
name, posting_date, posting_time, creation, voucher_no, item_code, batch_no, actual_qty
from
`tabStock Ledger Entry`
where
Expand Down

0 comments on commit d817c50

Please sign in to comment.