refactor: pass typed arguments to get_item_details methods (#44230)

* refactor: pass proper types to get_item_details methods

* chore: excempt previous commit from git blame
This commit is contained in:
David Arnold
2024-11-20 02:31:03 +01:00
committed by GitHub
parent 4ec23b5525
commit 9673bf85ec
16 changed files with 286 additions and 244 deletions

View File

@@ -26,6 +26,7 @@ from erpnext.setup.doctype.item_group.item_group import get_item_group_defaults
from erpnext.stock import get_warehouse_account_map
from erpnext.stock.doctype.item.item import get_item_defaults
from erpnext.stock.get_item_details import (
ItemDetailsCtx,
get_default_cost_center,
get_default_expense_account,
get_item_warehouse_,
@@ -748,7 +749,7 @@ def get_target_item_details(item_code=None, company=None):
item_group_defaults = get_item_group_defaults(item.name, company)
brand_defaults = get_brand_defaults(item.name, company)
out.cost_center = get_default_cost_center(
frappe._dict({"item_code": item.name, "company": company}),
ItemDetailsCtx({"item_code": item.name, "company": company}),
item_defaults,
item_group_defaults,
brand_defaults,
@@ -785,45 +786,42 @@ def get_target_asset_details(asset=None, company=None):
@frappe.whitelist()
def get_consumed_stock_item_details(args_):
if isinstance(args_, str):
args_ = json.loads(args_)
args = frappe._dict(args_)
@erpnext.normalize_ctx_input(ItemDetailsCtx)
def get_consumed_stock_item_details(ctx: ItemDetailsCtx):
out = frappe._dict()
item = frappe._dict()
if args.item_code:
item = frappe.get_cached_doc("Item", args.item_code)
if ctx.item_code:
item = frappe.get_cached_doc("Item", ctx.item_code)
out.item_name = item.item_name
out.batch_no = None
out.serial_no = ""
out.stock_qty = flt(args.stock_qty) or 1
out.stock_qty = flt(ctx.stock_qty) or 1
out.stock_uom = item.stock_uom
out.warehouse = get_item_warehouse_(args, item, overwrite_warehouse=True) if item else None
out.warehouse = get_item_warehouse_(ctx, item, overwrite_warehouse=True) if item else None
# Cost Center
item_defaults = get_item_defaults(item.name, args.company)
item_group_defaults = get_item_group_defaults(item.name, args.company)
brand_defaults = get_brand_defaults(item.name, args.company)
out.cost_center = get_default_cost_center(args, item_defaults, item_group_defaults, brand_defaults)
item_defaults = get_item_defaults(item.name, ctx.company)
item_group_defaults = get_item_group_defaults(item.name, ctx.company)
brand_defaults = get_brand_defaults(item.name, ctx.company)
out.cost_center = get_default_cost_center(ctx, item_defaults, item_group_defaults, brand_defaults)
if args.item_code and out.warehouse:
if ctx.item_code and out.warehouse:
incoming_rate_args = frappe._dict(
{
"item_code": args.item_code,
"item_code": ctx.item_code,
"warehouse": out.warehouse,
"posting_date": args.posting_date,
"posting_time": args.posting_time,
"posting_date": ctx.posting_date,
"posting_time": ctx.posting_time,
"qty": -1 * flt(out.stock_qty),
"voucher_type": args.doctype,
"voucher_no": args.name,
"company": args.company,
"serial_no": args.serial_no,
"batch_no": args.batch_no,
"voucher_type": ctx.doctype,
"voucher_no": ctx.name,
"company": ctx.company,
"serial_no": ctx.serial_no,
"batch_no": ctx.batch_no,
}
)
out.update(get_warehouse_details(incoming_rate_args))
@@ -851,31 +849,28 @@ def get_warehouse_details(args):
@frappe.whitelist()
def get_consumed_asset_details(args):
if isinstance(args, str):
args = json.loads(args)
args = frappe._dict(args)
@erpnext.normalize_ctx_input(ItemDetailsCtx)
def get_consumed_asset_details(ctx):
out = frappe._dict()
asset_details = frappe._dict()
if args.asset:
if ctx.asset:
asset_details = frappe.db.get_value(
"Asset", args.asset, ["asset_name", "item_code", "item_name"], as_dict=1
"Asset", ctx.asset, ["asset_name", "item_code", "item_name"], as_dict=1
)
if not asset_details:
frappe.throw(_("Asset {0} does not exist").format(args.asset))
frappe.throw(_("Asset {0} does not exist").format(ctx.asset))
out.item_code = asset_details.item_code
out.asset_name = asset_details.asset_name
out.item_name = asset_details.item_name
if args.asset:
if ctx.asset:
out.current_asset_value = flt(
get_asset_value_after_depreciation(args.asset, finance_book=args.finance_book)
get_asset_value_after_depreciation(ctx.asset, finance_book=ctx.finance_book)
)
out.asset_value = get_value_after_depreciation_on_disposal_date(
args.asset, args.posting_date, finance_book=args.finance_book
ctx.asset, ctx.posting_date, finance_book=ctx.finance_book
)
else:
out.current_asset_value = 0
@@ -884,7 +879,7 @@ def get_consumed_asset_details(args):
# Account
if asset_details.item_code:
out.fixed_asset_account = get_asset_category_account(
"fixed_asset_account", item=asset_details.item_code, company=args.company
"fixed_asset_account", item=asset_details.item_code, company=ctx.company
)
else:
out.fixed_asset_account = None
@@ -892,37 +887,32 @@ def get_consumed_asset_details(args):
# Cost Center
if asset_details.item_code:
item = frappe.get_cached_doc("Item", asset_details.item_code)
item_defaults = get_item_defaults(item.name, args.company)
item_group_defaults = get_item_group_defaults(item.name, args.company)
brand_defaults = get_brand_defaults(item.name, args.company)
out.cost_center = get_default_cost_center(args, item_defaults, item_group_defaults, brand_defaults)
item_defaults = get_item_defaults(item.name, ctx.company)
item_group_defaults = get_item_group_defaults(item.name, ctx.company)
brand_defaults = get_brand_defaults(item.name, ctx.company)
out.cost_center = get_default_cost_center(ctx, item_defaults, item_group_defaults, brand_defaults)
return out
@frappe.whitelist()
def get_service_item_details(args):
if isinstance(args, str):
args = json.loads(args)
args = frappe._dict(args)
@erpnext.normalize_ctx_input(ItemDetailsCtx)
def get_service_item_details(ctx):
out = frappe._dict()
item = frappe._dict()
if args.item_code:
item = frappe.get_cached_doc("Item", args.item_code)
if ctx.item_code:
item = frappe.get_cached_doc("Item", ctx.item_code)
out.item_name = item.item_name
out.qty = flt(args.qty) or 1
out.qty = flt(ctx.qty) or 1
out.uom = item.purchase_uom or item.stock_uom
item_defaults = get_item_defaults(item.name, args.company)
item_group_defaults = get_item_group_defaults(item.name, args.company)
brand_defaults = get_brand_defaults(item.name, args.company)
item_defaults = get_item_defaults(item.name, ctx.company)
item_group_defaults = get_item_group_defaults(item.name, ctx.company)
brand_defaults = get_brand_defaults(item.name, ctx.company)
out.expense_account = get_default_expense_account(
args, item_defaults, item_group_defaults, brand_defaults
)
out.cost_center = get_default_cost_center(args, item_defaults, item_group_defaults, brand_defaults)
out.expense_account = get_default_expense_account(ctx, item_defaults, item_group_defaults, brand_defaults)
out.cost_center = get_default_cost_center(ctx, item_defaults, item_group_defaults, brand_defaults)
return out