Merge pull request #4792 from anandpdoshi/optimize/stock-balance-report
[optimization] stock balance report - force index + improve memory usage
This commit is contained in:
commit
cc57088f5c
@ -14,21 +14,19 @@ def execute(filters=None):
|
|||||||
iwb_map = get_item_warehouse_map(filters)
|
iwb_map = get_item_warehouse_map(filters)
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
for company in sorted(iwb_map):
|
for (company, item, warehouse) in sorted(iwb_map):
|
||||||
for item in sorted(iwb_map[company]):
|
qty_dict = iwb_map[(company, item, warehouse)]
|
||||||
for wh in sorted(iwb_map[company][item]):
|
data.append([item, item_map[item]["item_name"],
|
||||||
qty_dict = iwb_map[company][item][wh]
|
item_map[item]["item_group"],
|
||||||
data.append([item, item_map[item]["item_name"],
|
item_map[item]["brand"],
|
||||||
item_map[item]["item_group"],
|
item_map[item]["description"], warehouse,
|
||||||
item_map[item]["brand"],
|
item_map[item]["stock_uom"], qty_dict.opening_qty,
|
||||||
item_map[item]["description"], wh,
|
qty_dict.opening_val, qty_dict.in_qty,
|
||||||
item_map[item]["stock_uom"], qty_dict.opening_qty,
|
qty_dict.in_val, qty_dict.out_qty,
|
||||||
qty_dict.opening_val, qty_dict.in_qty,
|
qty_dict.out_val, qty_dict.bal_qty,
|
||||||
qty_dict.in_val, qty_dict.out_qty,
|
qty_dict.bal_val, qty_dict.val_rate,
|
||||||
qty_dict.out_val, qty_dict.bal_qty,
|
company
|
||||||
qty_dict.bal_val, qty_dict.val_rate,
|
])
|
||||||
company
|
|
||||||
])
|
|
||||||
|
|
||||||
return columns, data
|
return columns, data
|
||||||
|
|
||||||
@ -63,7 +61,7 @@ def get_conditions(filters):
|
|||||||
frappe.throw(_("'From Date' is required"))
|
frappe.throw(_("'From Date' is required"))
|
||||||
|
|
||||||
if filters.get("to_date"):
|
if filters.get("to_date"):
|
||||||
conditions += " and posting_date <= '%s'" % filters["to_date"]
|
conditions += " and posting_date <= '%s'" % frappe.db.escape(filters["to_date"])
|
||||||
else:
|
else:
|
||||||
frappe.throw(_("'To Date' is required"))
|
frappe.throw(_("'To Date' is required"))
|
||||||
|
|
||||||
@ -76,25 +74,30 @@ def get_conditions(filters):
|
|||||||
def get_stock_ledger_entries(filters):
|
def get_stock_ledger_entries(filters):
|
||||||
conditions = get_conditions(filters)
|
conditions = get_conditions(filters)
|
||||||
return frappe.db.sql("""select item_code, warehouse, posting_date, actual_qty, valuation_rate,
|
return frappe.db.sql("""select item_code, warehouse, posting_date, actual_qty, valuation_rate,
|
||||||
company, voucher_type, qty_after_transaction, stock_value_difference
|
company, voucher_type, qty_after_transaction, stock_value_difference
|
||||||
from `tabStock Ledger Entry`
|
from `tabStock Ledger Entry` force index (posting_sort_index)
|
||||||
where docstatus < 2 %s order by posting_date, posting_time, name""" %
|
where docstatus < 2 %s order by posting_date, posting_time, name""" %
|
||||||
conditions, as_dict=1)
|
conditions, as_dict=1)
|
||||||
|
|
||||||
def get_item_warehouse_map(filters):
|
def get_item_warehouse_map(filters):
|
||||||
sle = get_stock_ledger_entries(filters)
|
|
||||||
iwb_map = {}
|
iwb_map = {}
|
||||||
|
from_date = getdate(filters["from_date"])
|
||||||
|
to_date = getdate(filters["to_date"])
|
||||||
|
|
||||||
|
sle = get_stock_ledger_entries(filters)
|
||||||
|
|
||||||
for d in sle:
|
for d in sle:
|
||||||
iwb_map.setdefault(d.company, {}).setdefault(d.item_code, {}).\
|
key = (d.company, d.item_code, d.warehouse)
|
||||||
setdefault(d.warehouse, frappe._dict({\
|
if key not in iwb_map:
|
||||||
|
iwb_map[key] = frappe._dict({
|
||||||
"opening_qty": 0.0, "opening_val": 0.0,
|
"opening_qty": 0.0, "opening_val": 0.0,
|
||||||
"in_qty": 0.0, "in_val": 0.0,
|
"in_qty": 0.0, "in_val": 0.0,
|
||||||
"out_qty": 0.0, "out_val": 0.0,
|
"out_qty": 0.0, "out_val": 0.0,
|
||||||
"bal_qty": 0.0, "bal_val": 0.0,
|
"bal_qty": 0.0, "bal_val": 0.0,
|
||||||
"val_rate": 0.0, "uom": None
|
"val_rate": 0.0, "uom": None
|
||||||
}))
|
})
|
||||||
qty_dict = iwb_map[d.company][d.item_code][d.warehouse]
|
|
||||||
|
qty_dict = iwb_map[(d.company, d.item_code, d.warehouse)]
|
||||||
|
|
||||||
if d.voucher_type == "Stock Reconciliation":
|
if d.voucher_type == "Stock Reconciliation":
|
||||||
qty_diff = flt(d.qty_after_transaction) - qty_dict.bal_qty
|
qty_diff = flt(d.qty_after_transaction) - qty_dict.bal_qty
|
||||||
@ -103,10 +106,11 @@ def get_item_warehouse_map(filters):
|
|||||||
|
|
||||||
value_diff = flt(d.stock_value_difference)
|
value_diff = flt(d.stock_value_difference)
|
||||||
|
|
||||||
if d.posting_date < getdate(filters["from_date"]):
|
if d.posting_date < from_date:
|
||||||
qty_dict.opening_qty += qty_diff
|
qty_dict.opening_qty += qty_diff
|
||||||
qty_dict.opening_val += value_diff
|
qty_dict.opening_val += value_diff
|
||||||
elif d.posting_date >= getdate(filters["from_date"]) and d.posting_date <= getdate(filters["to_date"]):
|
|
||||||
|
elif d.posting_date >= from_date and d.posting_date <= to_date:
|
||||||
if qty_diff > 0:
|
if qty_diff > 0:
|
||||||
qty_dict.in_qty += qty_diff
|
qty_dict.in_qty += qty_diff
|
||||||
qty_dict.in_val += value_diff
|
qty_dict.in_val += value_diff
|
||||||
|
Loading…
x
Reference in New Issue
Block a user