mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-20 18:06:30 +00:00
Merge pull request #46383 from mihir-kandoi/st29657-2
feat: available serial no report
This commit is contained in:
@@ -149,6 +149,17 @@ def get_serial_nos(serial_no):
|
||||
return [s.strip() for s in cstr(serial_no).strip().replace(",", "\n").split("\n") if s.strip()]
|
||||
|
||||
|
||||
def get_serial_nos_from_sle_list(bundles):
|
||||
table = frappe.qb.DocType("Serial and Batch Entry")
|
||||
query = frappe.qb.from_(table).select(table.parent, table.serial_no).where(table.parent.isin(bundles))
|
||||
data = query.run(as_dict=True)
|
||||
|
||||
result = {}
|
||||
for d in data:
|
||||
result.setdefault(d.parent, []).append(d.serial_no)
|
||||
return result
|
||||
|
||||
|
||||
def clean_serial_no_string(serial_no: str) -> str:
|
||||
if not serial_no:
|
||||
return ""
|
||||
|
||||
118
erpnext/stock/report/available_serial_no/available_serial_no.js
Normal file
118
erpnext/stock/report/available_serial_no/available_serial_no.js
Normal file
@@ -0,0 +1,118 @@
|
||||
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||
// License: GNU General Public License v3. See license.txt
|
||||
|
||||
frappe.query_reports["Available Serial No"] = {
|
||||
filters: [
|
||||
{
|
||||
fieldname: "company",
|
||||
label: __("Company"),
|
||||
fieldtype: "Link",
|
||||
options: "Company",
|
||||
default: frappe.defaults.get_user_default("Company"),
|
||||
reqd: 1,
|
||||
},
|
||||
{
|
||||
fieldname: "from_date",
|
||||
label: __("From Date"),
|
||||
fieldtype: "Date",
|
||||
default: frappe.datetime.add_months(frappe.datetime.get_today(), -1),
|
||||
reqd: 1,
|
||||
},
|
||||
{
|
||||
fieldname: "to_date",
|
||||
label: __("To Date"),
|
||||
fieldtype: "Date",
|
||||
default: frappe.datetime.get_today(),
|
||||
reqd: 1,
|
||||
},
|
||||
{
|
||||
fieldname: "warehouse",
|
||||
label: __("Warehouse"),
|
||||
fieldtype: "Link",
|
||||
options: "Warehouse",
|
||||
get_query: function () {
|
||||
const company = frappe.query_report.get_filter_value("company");
|
||||
return {
|
||||
filters: { company: company },
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldname: "item_code",
|
||||
label: __("Item"),
|
||||
fieldtype: "Link",
|
||||
options: "Item",
|
||||
get_query: function () {
|
||||
return {
|
||||
query: "erpnext.controllers.queries.item_query",
|
||||
filters: {
|
||||
has_serial_no: 1,
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldname: "item_group",
|
||||
label: __("Item Group"),
|
||||
fieldtype: "Link",
|
||||
options: "Item Group",
|
||||
},
|
||||
{
|
||||
fieldname: "batch_no",
|
||||
label: __("Batch No"),
|
||||
fieldtype: "Link",
|
||||
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",
|
||||
label: __("Brand"),
|
||||
fieldtype: "Link",
|
||||
options: "Brand",
|
||||
},
|
||||
{
|
||||
fieldname: "voucher_no",
|
||||
label: __("Voucher #"),
|
||||
fieldtype: "Data",
|
||||
},
|
||||
{
|
||||
fieldname: "project",
|
||||
label: __("Project"),
|
||||
fieldtype: "Link",
|
||||
options: "Project",
|
||||
},
|
||||
{
|
||||
fieldname: "include_uom",
|
||||
label: __("Include UOM"),
|
||||
fieldtype: "Link",
|
||||
options: "UOM",
|
||||
},
|
||||
{
|
||||
fieldname: "valuation_field_type",
|
||||
label: __("Valuation Field Type"),
|
||||
fieldtype: "Select",
|
||||
width: "80",
|
||||
options: "Currency\nFloat",
|
||||
default: "Currency",
|
||||
},
|
||||
],
|
||||
formatter: function (value, row, column, data, default_formatter) {
|
||||
value = default_formatter(value, row, column, data);
|
||||
if (column.fieldname == "out_qty" && data && data.out_qty < 0) {
|
||||
value = "<span style='color:red'>" + value + "</span>";
|
||||
} else if (column.fieldname == "in_qty" && data && data.in_qty > 0) {
|
||||
value = "<span style='color:green'>" + value + "</span>";
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
};
|
||||
|
||||
erpnext.utils.add_inventory_dimensions("Balance Serial No", 10);
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"add_total_row": 0,
|
||||
"apply_user_permissions": 1,
|
||||
"creation": "2025-03-07 10:54:09.429215",
|
||||
"disabled": 0,
|
||||
"docstatus": 0,
|
||||
"doctype": "Report",
|
||||
"idx": 0,
|
||||
"is_standard": "Yes",
|
||||
"modified": "2025-03-07 10:54:09.429215",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Stock",
|
||||
"name": "Available Serial No",
|
||||
"owner": "Administrator",
|
||||
"prepared_report": 0,
|
||||
"ref_doctype": "Stock Ledger Entry",
|
||||
"report_name": "Available Serial No",
|
||||
"report_type": "Script Report",
|
||||
"roles": [
|
||||
{
|
||||
"role": "Stock User"
|
||||
},
|
||||
{
|
||||
"role": "Accounts Manager"
|
||||
}
|
||||
],
|
||||
"timeout": 0
|
||||
}
|
||||
328
erpnext/stock/report/available_serial_no/available_serial_no.py
Normal file
328
erpnext/stock/report/available_serial_no/available_serial_no.py
Normal file
@@ -0,0 +1,328 @@
|
||||
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||
# License: GNU General Public License v3. See license.txt
|
||||
|
||||
import frappe
|
||||
from frappe import _
|
||||
from frappe.query_builder.functions import Sum
|
||||
from frappe.utils import cint, flt
|
||||
|
||||
from erpnext.stock.doctype.inventory_dimension.inventory_dimension import get_inventory_dimensions
|
||||
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos, get_serial_nos_from_sle_list
|
||||
from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import get_stock_balance_for
|
||||
from erpnext.stock.report.stock_ledger.stock_ledger import (
|
||||
check_inventory_dimension_filters_applied,
|
||||
get_item_details,
|
||||
get_item_group_condition,
|
||||
get_opening_balance,
|
||||
get_opening_balance_from_batch,
|
||||
get_stock_ledger_entries,
|
||||
)
|
||||
from erpnext.stock.utils import (
|
||||
is_reposting_item_valuation_in_progress,
|
||||
update_included_uom_in_report,
|
||||
)
|
||||
|
||||
|
||||
def execute(filters=None):
|
||||
is_reposting_item_valuation_in_progress()
|
||||
include_uom = filters.get("include_uom")
|
||||
columns = get_columns(filters)
|
||||
items = get_items(filters)
|
||||
sl_entries = get_stock_ledger_entries(filters, items)
|
||||
item_details = get_item_details(items, sl_entries, include_uom)
|
||||
|
||||
opening_row, actual_qty, stock_value = get_opening_balance_data(filters, columns, sl_entries)
|
||||
|
||||
precision = cint(frappe.db.get_single_value("System Settings", "float_precision"))
|
||||
data, conversion_factors = process_stock_ledger_entries(
|
||||
filters, sl_entries, item_details, opening_row, actual_qty, stock_value, precision
|
||||
)
|
||||
|
||||
update_included_uom_in_report(columns, data, include_uom, conversion_factors)
|
||||
return columns, data
|
||||
|
||||
|
||||
def get_opening_balance_data(filters, columns, sl_entries):
|
||||
if filters.get("batch_no"):
|
||||
opening_row = get_opening_balance_from_batch(filters, columns, sl_entries)
|
||||
else:
|
||||
opening_row = get_opening_balance(filters, columns, sl_entries)
|
||||
|
||||
actual_qty = opening_row.get("qty_after_transaction") if opening_row else 0
|
||||
stock_value = opening_row.get("stock_value") if opening_row else 0
|
||||
return opening_row, actual_qty, stock_value
|
||||
|
||||
|
||||
def process_stock_ledger_entries(
|
||||
filters, sl_entries, item_details, opening_row, actual_qty, stock_value, precision
|
||||
):
|
||||
data = []
|
||||
conversion_factors = []
|
||||
|
||||
if opening_row:
|
||||
data.append(opening_row)
|
||||
conversion_factors.append(0)
|
||||
|
||||
batch_balance_dict = frappe._dict({})
|
||||
|
||||
if actual_qty and filters.get("batch_no"):
|
||||
batch_balance_dict[filters.batch_no] = [actual_qty, stock_value]
|
||||
|
||||
available_serial_nos = get_serial_nos_from_sle_list(
|
||||
[sle.serial_and_batch_bundle for sle in sl_entries if sle.serial_and_batch_bundle]
|
||||
)
|
||||
|
||||
for sle in sl_entries:
|
||||
update_stock_ledger_entry(
|
||||
sle, item_details, filters, actual_qty, stock_value, batch_balance_dict, precision
|
||||
)
|
||||
update_available_serial_nos(available_serial_nos, sle)
|
||||
data.append(sle)
|
||||
|
||||
if filters.get("include_uom"):
|
||||
conversion_factors.append(item_details[sle.item_code].conversion_factor)
|
||||
|
||||
return data, conversion_factors
|
||||
|
||||
|
||||
def update_stock_ledger_entry(
|
||||
sle, item_details, filters, actual_qty, stock_value, batch_balance_dict, precision
|
||||
):
|
||||
item_detail = item_details[sle.item_code]
|
||||
sle.update(item_detail)
|
||||
|
||||
if filters.get("batch_no") or check_inventory_dimension_filters_applied(filters):
|
||||
actual_qty += flt(sle.actual_qty, precision)
|
||||
stock_value += sle.stock_value_difference
|
||||
|
||||
if sle.batch_no:
|
||||
batch_balance_dict.setdefault(sle.batch_no, [0, 0])
|
||||
batch_balance_dict[sle.batch_no][0] += sle.actual_qty
|
||||
|
||||
if sle.voucher_type == "Stock Reconciliation" and not sle.actual_qty:
|
||||
actual_qty = sle.qty_after_transaction
|
||||
stock_value = sle.stock_value
|
||||
|
||||
sle.update({"qty_after_transaction": actual_qty, "stock_value": stock_value})
|
||||
|
||||
sle.update({"in_qty": max(sle.actual_qty, 0), "out_qty": min(sle.actual_qty, 0)})
|
||||
|
||||
if sle.actual_qty:
|
||||
sle["in_out_rate"] = flt(sle.stock_value_difference / sle.actual_qty, precision)
|
||||
elif sle.voucher_type == "Stock Reconciliation":
|
||||
sle["in_out_rate"] = sle.valuation_rate
|
||||
|
||||
|
||||
def update_available_serial_nos(available_serial_nos, sle):
|
||||
serial_nos = (
|
||||
get_serial_nos(sle.serial_no)
|
||||
if sle.serial_no
|
||||
else available_serial_nos.get(sle.serial_and_batch_bundle)
|
||||
)
|
||||
key = (sle.item_code, sle.warehouse)
|
||||
if key not in available_serial_nos:
|
||||
stock_balance = get_stock_balance_for(
|
||||
sle.item_code, sle.warehouse, sle.posting_date, sle.posting_time
|
||||
)
|
||||
serials = get_serial_nos(stock_balance["serial_nos"]) if stock_balance["serial_nos"] else []
|
||||
available_serial_nos.setdefault(key, serials)
|
||||
sle.balance_serial_no = "\n".join(serials)
|
||||
return
|
||||
|
||||
existing_serial_no = available_serial_nos[key]
|
||||
for sn in serial_nos:
|
||||
if sn in existing_serial_no:
|
||||
existing_serial_no.remove(sn)
|
||||
else:
|
||||
existing_serial_no.append(sn)
|
||||
|
||||
sle.balance_serial_no = "\n".join(existing_serial_no)
|
||||
|
||||
|
||||
def get_columns(filters):
|
||||
columns = [
|
||||
{"label": _("Date"), "fieldname": "date", "fieldtype": "Datetime", "width": 150},
|
||||
{
|
||||
"label": _("Item"),
|
||||
"fieldname": "item_code",
|
||||
"fieldtype": "Link",
|
||||
"options": "Item",
|
||||
"width": 100,
|
||||
},
|
||||
{"label": _("Item Name"), "fieldname": "item_name", "width": 100},
|
||||
{
|
||||
"label": _("Stock UOM"),
|
||||
"fieldname": "stock_uom",
|
||||
"fieldtype": "Link",
|
||||
"options": "UOM",
|
||||
"width": 90,
|
||||
},
|
||||
]
|
||||
|
||||
for dimension in get_inventory_dimensions():
|
||||
columns.append(
|
||||
{
|
||||
"label": _(dimension.doctype),
|
||||
"fieldname": dimension.fieldname,
|
||||
"fieldtype": "Link",
|
||||
"options": dimension.doctype,
|
||||
"width": 110,
|
||||
}
|
||||
)
|
||||
|
||||
columns.extend(
|
||||
[
|
||||
{
|
||||
"label": _("In Qty"),
|
||||
"fieldname": "in_qty",
|
||||
"fieldtype": "Float",
|
||||
"width": 80,
|
||||
"convertible": "qty",
|
||||
},
|
||||
{
|
||||
"label": _("Out Qty"),
|
||||
"fieldname": "out_qty",
|
||||
"fieldtype": "Float",
|
||||
"width": 80,
|
||||
"convertible": "qty",
|
||||
},
|
||||
{
|
||||
"label": _("Balance Qty"),
|
||||
"fieldname": "qty_after_transaction",
|
||||
"fieldtype": "Float",
|
||||
"width": 100,
|
||||
"convertible": "qty",
|
||||
},
|
||||
{
|
||||
"label": _("Warehouse"),
|
||||
"fieldname": "warehouse",
|
||||
"fieldtype": "Link",
|
||||
"options": "Warehouse",
|
||||
"width": 150,
|
||||
},
|
||||
{
|
||||
"label": _("Item Group"),
|
||||
"fieldname": "item_group",
|
||||
"fieldtype": "Link",
|
||||
"options": "Item Group",
|
||||
"width": 100,
|
||||
},
|
||||
{
|
||||
"label": _("Brand"),
|
||||
"fieldname": "brand",
|
||||
"fieldtype": "Link",
|
||||
"options": "Brand",
|
||||
"width": 100,
|
||||
},
|
||||
{"label": _("Description"), "fieldname": "description", "width": 200},
|
||||
{
|
||||
"label": _("Incoming Rate"),
|
||||
"fieldname": "incoming_rate",
|
||||
"fieldtype": "Currency",
|
||||
"width": 110,
|
||||
"options": "Company:company:default_currency",
|
||||
"convertible": "rate",
|
||||
},
|
||||
{
|
||||
"label": _("Avg Rate (Balance Stock)"),
|
||||
"fieldname": "valuation_rate",
|
||||
"fieldtype": filters.valuation_field_type,
|
||||
"width": 180,
|
||||
"options": "Company:company:default_currency"
|
||||
if filters.valuation_field_type == "Currency"
|
||||
else None,
|
||||
"convertible": "rate",
|
||||
},
|
||||
{
|
||||
"label": _("Valuation Rate"),
|
||||
"fieldname": "in_out_rate",
|
||||
"fieldtype": filters.valuation_field_type,
|
||||
"width": 140,
|
||||
"options": "Company:company:default_currency"
|
||||
if filters.valuation_field_type == "Currency"
|
||||
else None,
|
||||
"convertible": "rate",
|
||||
},
|
||||
{
|
||||
"label": _("Balance Value"),
|
||||
"fieldname": "stock_value",
|
||||
"fieldtype": "Currency",
|
||||
"width": 110,
|
||||
"options": "Company:company:default_currency",
|
||||
},
|
||||
{
|
||||
"label": _("Value Change"),
|
||||
"fieldname": "stock_value_difference",
|
||||
"fieldtype": "Currency",
|
||||
"width": 110,
|
||||
"options": "Company:company:default_currency",
|
||||
},
|
||||
{"label": _("Voucher Type"), "fieldname": "voucher_type", "width": 110},
|
||||
{
|
||||
"label": _("Voucher #"),
|
||||
"fieldname": "voucher_no",
|
||||
"fieldtype": "Dynamic Link",
|
||||
"options": "voucher_type",
|
||||
"width": 100,
|
||||
},
|
||||
{
|
||||
"label": _("Batch"),
|
||||
"fieldname": "batch_no",
|
||||
"fieldtype": "Link",
|
||||
"options": "Batch",
|
||||
"width": 100,
|
||||
},
|
||||
{
|
||||
"label": _("Serial No"),
|
||||
"fieldname": "serial_no",
|
||||
"fieldtype": "Link",
|
||||
"options": "Serial No",
|
||||
"width": 100,
|
||||
},
|
||||
{
|
||||
"label": _("Serial and Batch Bundle"),
|
||||
"fieldname": "serial_and_batch_bundle",
|
||||
"fieldtype": "Link",
|
||||
"options": "Serial and Batch Bundle",
|
||||
"width": 100,
|
||||
},
|
||||
{"label": _("Balance Serial No"), "fieldname": "balance_serial_no", "width": 100},
|
||||
{
|
||||
"label": _("Project"),
|
||||
"fieldname": "project",
|
||||
"fieldtype": "Link",
|
||||
"options": "Project",
|
||||
"width": 100,
|
||||
},
|
||||
{
|
||||
"label": _("Company"),
|
||||
"fieldname": "company",
|
||||
"fieldtype": "Link",
|
||||
"options": "Company",
|
||||
"width": 110,
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
return columns
|
||||
|
||||
|
||||
def get_items(filters):
|
||||
item = frappe.qb.DocType("Item")
|
||||
query = frappe.qb.from_(item).select(item.name).where(item.has_serial_no == 1)
|
||||
conditions = []
|
||||
|
||||
if item_code := filters.get("item_code"):
|
||||
conditions.append(item.name == item_code)
|
||||
else:
|
||||
if brand := filters.get("brand"):
|
||||
conditions.append(item.brand == brand)
|
||||
if item_group := filters.get("item_group"):
|
||||
if condition := get_item_group_condition(item_group, item):
|
||||
conditions.append(condition)
|
||||
|
||||
if conditions:
|
||||
for condition in conditions:
|
||||
query = query.where(condition)
|
||||
|
||||
return query.run(pluck=True)
|
||||
@@ -0,0 +1,45 @@
|
||||
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
|
||||
# See license.txt
|
||||
|
||||
import frappe
|
||||
from frappe.tests import IntegrationTestCase
|
||||
from frappe.utils import add_days, today
|
||||
|
||||
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
|
||||
from erpnext.stock.doctype.item.test_item import create_item
|
||||
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
|
||||
|
||||
|
||||
class TestStockLedgerReeport(IntegrationTestCase):
|
||||
def setUp(self) -> None:
|
||||
item = create_item("_Test Item with Serial No", is_stock_item=1)
|
||||
item.has_serial_no = 1
|
||||
item.serial_no_series = "TEST.###"
|
||||
item.save(ignore_permissions=True)
|
||||
|
||||
self.filters = frappe._dict(
|
||||
company="_Test Company",
|
||||
from_date=today(),
|
||||
to_date=add_days(today(), 30),
|
||||
item_code="_Test Item With Serial No",
|
||||
)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
frappe.db.rollback()
|
||||
|
||||
def test_available_serial_no(self):
|
||||
report = frappe.get_doc("Report", "Available Serial No")
|
||||
|
||||
make_purchase_receipt(qty=10, item_code="_Test Item with Serial No")
|
||||
data = report.get_data(filters=self.filters)
|
||||
serial_nos = [item for item in data[-1][-1]["balance_serial_no"].split("\n")]
|
||||
|
||||
# Test 1: Since we have created an inward entry with Purchase Receipt of 10 qty, we should have 10 serial nos
|
||||
self.assertEqual(len(serial_nos), 10)
|
||||
|
||||
create_delivery_note(qty=5, item_code="_Test Item with Serial No")
|
||||
data = report.get_data(filters=self.filters)
|
||||
serial_nos = [item for item in data[-1][-1]["balance_serial_no"].split("\n")]
|
||||
|
||||
# Test 2: Since we have created a delivery note of 5 qty, we should have 5 serial nos
|
||||
self.assertEqual(len(serial_nos), 5)
|
||||
Reference in New Issue
Block a user