Skip to content

Commit

Permalink
fix: stock ledger balance qty for the batch (#40274)
Browse files Browse the repository at this point in the history
(cherry picked from commit e178ffc)
  • Loading branch information
rohitwaghchaure authored and mergify[bot] committed Mar 6, 2024
1 parent 50e49be commit 39a1474
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def validate_batch_inventory(self):

available_batches = get_available_batches_qty(available_batches)
for batch_no in batches:
if batch_no not in available_batches or available_batches[batch_no] < 0:
if batch_no in available_batches and available_batches[batch_no] < 0:
if flt(available_batches.get(batch_no)) < 0:
self.validate_negative_batch(batch_no, available_batches[batch_no])

Expand Down
10 changes: 9 additions & 1 deletion erpnext/stock/report/stock_ledger/stock_ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ frappe.query_reports["Stock Ledger"] = {
"fieldname":"batch_no",
"label": __("Batch No"),
"fieldtype": "Link",
"options": "Batch"
"options": "Batch",
on_change() {
const batch_no = frappe.query_report.get_filter_value('batch_no');
if (batch_no) {
frappe.query_report.set_filter_value('segregate_serial_batch_bundle', 1);
} else {
frappe.query_report.set_filter_value('segregate_serial_batch_bundle', 0);
}
}
},
{
"fieldname":"brand",
Expand Down
21 changes: 15 additions & 6 deletions erpnext/stock/report/stock_ledger/stock_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


import copy
from collections import defaultdict

import frappe
from frappe import _
Expand Down Expand Up @@ -31,7 +32,7 @@ def execute(filters=None):
bundle_details = {}

if filters.get("segregate_serial_batch_bundle"):
bundle_details = get_serial_batch_bundle_details(sl_entries)
bundle_details = get_serial_batch_bundle_details(sl_entries, filters)

data = []
conversion_factors = []
Expand All @@ -47,12 +48,13 @@ def execute(filters=None):
available_serial_nos = {}
inventory_dimension_filters_applied = check_inventory_dimension_filters_applied(filters)

batch_balance_dict = defaultdict(float)
for sle in sl_entries:
item_detail = item_details[sle.item_code]

sle.update(item_detail)
if bundle_info := bundle_details.get(sle.serial_and_batch_bundle):
data.extend(get_segregated_bundle_entries(sle, bundle_info))
data.extend(get_segregated_bundle_entries(sle, bundle_info, batch_balance_dict))
continue

if filters.get("batch_no") or inventory_dimension_filters_applied:
Expand Down Expand Up @@ -85,15 +87,14 @@ def execute(filters=None):
return columns, data


def get_segregated_bundle_entries(sle, bundle_details):
def get_segregated_bundle_entries(sle, bundle_details, batch_balance_dict):
segregated_entries = []
qty_before_transaction = sle.qty_after_transaction - sle.actual_qty
stock_value_before_transaction = sle.stock_value - sle.stock_value_difference

for row in bundle_details:
new_sle = copy.deepcopy(sle)
new_sle.update(row)

new_sle.update(
{
"in_out_rate": flt(new_sle.stock_value_difference / row.qty) if row.qty else 0,
Expand All @@ -105,6 +106,10 @@ def get_segregated_bundle_entries(sle, bundle_details):
}
)

if row.batch_no:
batch_balance_dict[row.batch_no] += row.qty
new_sle.update({"qty_after_transaction": batch_balance_dict[row.batch_no]})

qty_before_transaction += row.qty
stock_value_before_transaction += new_sle.stock_value_difference

Expand All @@ -117,7 +122,7 @@ def get_segregated_bundle_entries(sle, bundle_details):
return segregated_entries


def get_serial_batch_bundle_details(sl_entries):
def get_serial_batch_bundle_details(sl_entries, filters=None):
bundle_details = []
for sle in sl_entries:
if sle.serial_and_batch_bundle:
Expand All @@ -126,10 +131,14 @@ def get_serial_batch_bundle_details(sl_entries):
if not bundle_details:
return frappe._dict({})

query_filers = {"parent": ("in", bundle_details)}
if filters.get("batch_no"):
query_filers["batch_no"] = filters.batch_no

_bundle_details = frappe._dict({})
batch_entries = frappe.get_all(
"Serial and Batch Entry",
filters={"parent": ("in", bundle_details)},
filters=query_filers,
fields=["parent", "qty", "incoming_rate", "stock_value_difference", "batch_no", "serial_no"],
order_by="parent, idx",
)
Expand Down
4 changes: 1 addition & 3 deletions erpnext/stock/serial_batch_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,7 @@ def update_batch_qty(self):
batches = frappe._dict({self.sle.batch_no: self.sle.actual_qty})

batches_qty = get_available_batches(
frappe._dict(
{"item_code": self.item_code, "warehouse": self.warehouse, "batch_no": list(batches.keys())}
)
frappe._dict({"item_code": self.item_code, "batch_no": list(batches.keys())})
)

for batch_no in batches:
Expand Down

0 comments on commit 39a1474

Please sign in to comment.