mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-13 01:34:10 +00:00
fix: use get_batch_qty to fetch batch data
(cherry picked from commit cf03d03033)
This commit is contained in:
committed by
Mergify
parent
e7fcacbe69
commit
10b0da8bc8
@@ -5,7 +5,8 @@ import json
|
|||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.query_builder.functions import Sum
|
|
||||||
|
from erpnext.stock.doctype.batch.batch import get_batch_qty
|
||||||
|
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
@@ -37,43 +38,56 @@ def get_columns() -> list[dict]:
|
|||||||
return columns
|
return columns
|
||||||
|
|
||||||
|
|
||||||
def get_data(filters):
|
def get_data(filters=None):
|
||||||
item_filter = filters.get("item")
|
filters = filters or {}
|
||||||
batch_filter = filters.get("batch")
|
|
||||||
|
|
||||||
stock_ledger_entry = frappe.qb.DocType("Stock Ledger Entry")
|
item = filters.get("item")
|
||||||
batch_ledger = frappe.qb.DocType("Serial and Batch Entry")
|
batch_no = filters.get("batch")
|
||||||
batch_table = frappe.qb.DocType("Batch")
|
|
||||||
|
batch_sle_data = get_batch_qty(item_code=item, batch_no=batch_no) or []
|
||||||
|
|
||||||
|
stock_qty_map = {}
|
||||||
|
for row in batch_sle_data:
|
||||||
|
batch = row.get("batch_no")
|
||||||
|
if not batch:
|
||||||
|
continue
|
||||||
|
stock_qty_map[batch] = stock_qty_map.get(batch, 0) + (row.get("qty") or 0)
|
||||||
|
|
||||||
|
batch = frappe.qb.DocType("Batch")
|
||||||
|
|
||||||
query = (
|
query = (
|
||||||
frappe.qb.from_(stock_ledger_entry)
|
frappe.qb.from_(batch)
|
||||||
.inner_join(batch_ledger)
|
.select(batch.name, batch.item, batch.item_name, batch.batch_qty)
|
||||||
.on(stock_ledger_entry.serial_and_batch_bundle == batch_ledger.parent)
|
.where(batch.disabled == 0)
|
||||||
.inner_join(batch_table)
|
|
||||||
.on(batch_ledger.batch_no == batch_table.name)
|
|
||||||
.select(
|
|
||||||
batch_table.item.as_("item_code"),
|
|
||||||
batch_table.item_name.as_("item_name"),
|
|
||||||
batch_table.name.as_("batch"),
|
|
||||||
batch_table.batch_qty.as_("batch_qty"),
|
|
||||||
Sum(batch_ledger.qty).as_("stock_qty"),
|
|
||||||
(Sum(batch_ledger.qty) - batch_table.batch_qty).as_("difference"),
|
|
||||||
)
|
|
||||||
.where(batch_table.disabled == 0)
|
|
||||||
.where(stock_ledger_entry.is_cancelled == 0)
|
|
||||||
.groupby(batch_table.name)
|
|
||||||
.having((Sum(batch_ledger.qty) - batch_table.batch_qty) != 0)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if item_filter:
|
if item:
|
||||||
query = query.where(batch_table.item == item_filter)
|
query = query.where(batch.item == item)
|
||||||
|
if batch_no:
|
||||||
|
query = query.where(batch.name == batch_no)
|
||||||
|
|
||||||
if batch_filter:
|
batch_records = query.run(as_dict=True) or []
|
||||||
query = query.where(batch_table.name == batch_filter)
|
|
||||||
|
|
||||||
data = query.run(as_dict=True)
|
result = []
|
||||||
|
for batch_doc in batch_records:
|
||||||
|
name = batch_doc.get("name")
|
||||||
|
batch_qty = batch_doc.get("batch_qty") or 0
|
||||||
|
stock_qty = stock_qty_map.get(name, 0)
|
||||||
|
difference = stock_qty - batch_qty
|
||||||
|
|
||||||
return data
|
if difference != 0:
|
||||||
|
result.append(
|
||||||
|
{
|
||||||
|
"item_code": batch_doc.get("item"),
|
||||||
|
"item_name": batch_doc.get("item_name"),
|
||||||
|
"batch": name,
|
||||||
|
"batch_qty": batch_qty,
|
||||||
|
"stock_qty": stock_qty,
|
||||||
|
"difference": difference,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
|
|||||||
Reference in New Issue
Block a user