chore: get_valuation_rate sider fixes
- Use qb instead of db.sql - Don't use `args` as argument for function - Cleaner variable names
This commit is contained in:
parent
9f2793ccf1
commit
7e41d84a11
@ -932,44 +932,46 @@ def get_bom_item_rate(args, bom_doc):
|
|||||||
return flt(rate)
|
return flt(rate)
|
||||||
|
|
||||||
|
|
||||||
def get_valuation_rate(args):
|
def get_valuation_rate(data):
|
||||||
"""
|
"""
|
||||||
1) Get average valuation rate from all warehouses
|
1) Get average valuation rate from all warehouses
|
||||||
2) If no value, get last valuation rate from SLE
|
2) If no value, get last valuation rate from SLE
|
||||||
3) If no value, get valuation rate from Item
|
3) If no value, get valuation rate from Item
|
||||||
"""
|
"""
|
||||||
|
from frappe.query_builder.functions import Sum
|
||||||
|
|
||||||
|
item_code, company = data.get("item_code"), data.get("company")
|
||||||
valuation_rate = 0.0
|
valuation_rate = 0.0
|
||||||
item_valuation = frappe.db.sql(
|
|
||||||
"""
|
bin_table = frappe.qb.DocType("Bin")
|
||||||
select
|
wh_table = frappe.qb.DocType("Warehouse")
|
||||||
(sum(bin.stock_value) / sum(bin.actual_qty)) as valuation_rate
|
item_valuation = (
|
||||||
from
|
frappe.qb.from_(bin_table)
|
||||||
`tabBin` bin, `tabWarehouse` warehouse
|
.join(wh_table)
|
||||||
where
|
.on(bin_table.warehouse == wh_table.name)
|
||||||
bin.item_code=%(item)s
|
.select((Sum(bin_table.stock_value) / Sum(bin_table.actual_qty)).as_("valuation_rate"))
|
||||||
and bin.warehouse = warehouse.name
|
.where((bin_table.item_code == item_code) & (wh_table.company == company))
|
||||||
and warehouse.company=%(company)s""",
|
).run(as_dict=True)[0]
|
||||||
{"item": args["item_code"], "company": args["company"]},
|
|
||||||
as_dict=1,
|
|
||||||
)[0]
|
|
||||||
|
|
||||||
valuation_rate = item_valuation.get("valuation_rate")
|
valuation_rate = item_valuation.get("valuation_rate")
|
||||||
|
|
||||||
if (valuation_rate is not None) and valuation_rate <= 0:
|
if (valuation_rate is not None) and valuation_rate <= 0:
|
||||||
# Explicit null value check. If None, Bins don't exist, neither does SLE
|
# Explicit null value check. If None, Bins don't exist, neither does SLE
|
||||||
last_valuation_rate = frappe.db.sql(
|
sle = frappe.qb.DocType("Stock Ledger Entry")
|
||||||
"""select valuation_rate
|
last_val_rate = (
|
||||||
from `tabStock Ledger Entry`
|
frappe.qb.from_(sle)
|
||||||
where item_code = %s and valuation_rate > 0 and is_cancelled = 0
|
.select(sle.valuation_rate)
|
||||||
order by posting_date desc, posting_time desc, creation desc limit 1""",
|
.where((sle.item_code == item_code) & (sle.valuation_rate > 0) & (sle.is_cancelled == 0))
|
||||||
args["item_code"],
|
.orderby(sle.posting_date, order=frappe.qb.desc)
|
||||||
)
|
.orderby(sle.posting_time, order=frappe.qb.desc)
|
||||||
|
.orderby(sle.creation, order=frappe.qb.desc)
|
||||||
|
.limit(1)
|
||||||
|
).run(as_dict=True)
|
||||||
|
|
||||||
valuation_rate = flt(last_valuation_rate[0][0]) if last_valuation_rate else 0
|
valuation_rate = flt(last_val_rate[0].get("valuation_rate")) if last_val_rate else 0
|
||||||
|
|
||||||
if not valuation_rate:
|
if not valuation_rate:
|
||||||
valuation_rate = frappe.db.get_value("Item", args["item_code"], "valuation_rate")
|
valuation_rate = frappe.db.get_value("Item", item_code, "valuation_rate")
|
||||||
|
|
||||||
return flt(valuation_rate)
|
return flt(valuation_rate)
|
||||||
|
|
||||||
|
|||||||
@ -82,7 +82,6 @@ class TestBOM(FrappeTestCase):
|
|||||||
as_dict=True,
|
as_dict=True,
|
||||||
)
|
)
|
||||||
rm_base_rate = bom_rates[0].get("base_rate") if bom_rates else 0
|
rm_base_rate = bom_rates[0].get("base_rate") if bom_rates else 0
|
||||||
rm_rate = bom_rates[0].get("rate") if bom_rates else 0
|
|
||||||
|
|
||||||
# Reset item valuation rate
|
# Reset item valuation rate
|
||||||
reset_item_valuation_rate(item_code="_Test Item 2", qty=200, rate=rm_base_rate + 10)
|
reset_item_valuation_rate(item_code="_Test Item 2", qty=200, rate=rm_base_rate + 10)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user