[optimization] [minor] pass filtered stock ledger entries to get_buying_amount

This commit is contained in:
Anand Doshi 2013-08-07 19:27:30 +05:30
parent b0dab89626
commit 5dd6b1d082
6 changed files with 39 additions and 28 deletions

View File

@ -10,9 +10,7 @@ def execute(filters=None):
if not filters: filters = {} if not filters: filters = {}
stock_ledger_entries = get_stock_ledger_entries(filters) stock_ledger_entries = get_stock_ledger_entries(filters)
source = get_source_data(filters) source = get_source_data(filters)
item_sales_bom = get_item_sales_bom() item_sales_bom = get_item_sales_bom()
columns = ["Delivery Note/Sales Invoice::120", "Link::30", "Posting Date:Date", "Posting Time", columns = ["Delivery Note/Sales Invoice::120", "Link::30", "Posting Date:Date", "Posting Time",
@ -20,12 +18,12 @@ def execute(filters=None):
"Qty:Float", "Selling Rate:Currency", "Avg. Buying Rate:Currency", "Qty:Float", "Selling Rate:Currency", "Avg. Buying Rate:Currency",
"Selling Amount:Currency", "Buying Amount:Currency", "Selling Amount:Currency", "Buying Amount:Currency",
"Gross Profit:Currency", "Gross Profit %:Percent", "Project:Link/Project"] "Gross Profit:Currency", "Gross Profit %:Percent", "Project:Link/Project"]
data = [] data = []
for row in source: for row in source:
selling_amount = flt(row.amount) selling_amount = flt(row.amount)
buying_amount = get_buying_amount(row.item_code, row.warehouse, -1*row.qty,
row.parenttype, row.name, row.item_row, stock_ledger_entries, buying_amount = get_buying_amount(row.item_code, row.parenttype, row.name, row.item_row,
stock_ledger_entries.get((row.item_code, row.warehouse), []),
item_sales_bom.get(row.parenttype, {}).get(row.name, webnotes._dict())) item_sales_bom.get(row.parenttype, {}).get(row.name, webnotes._dict()))
buying_amount = buying_amount > 0 and buying_amount or 0 buying_amount = buying_amount > 0 and buying_amount or 0
@ -57,7 +55,16 @@ def get_stock_ledger_entries(filters):
query += " order by item_code desc, warehouse desc, posting_date desc, posting_time desc, name desc" query += " order by item_code desc, warehouse desc, posting_date desc, posting_time desc, name desc"
return webnotes.conn.sql(query, filters, as_dict=True) res = webnotes.conn.sql(query, filters, as_dict=True)
out = {}
for r in res:
if (r.item_code, r.warehouse) not in out:
out[(r.item_code, r.warehouse)] = []
out[(r.item_code, r.warehouse)].append(r)
return out
def get_item_sales_bom(): def get_item_sales_bom():
item_sales_bom = {} item_sales_bom = {}

View File

@ -103,8 +103,8 @@ class SellingController(StockController):
for item in self.doclist.get({"parentfield": self.fname}): for item in self.doclist.get({"parentfield": self.fname}):
if item.item_code in self.stock_items or \ if item.item_code in self.stock_items or \
(item_sales_bom and item_sales_bom.get(item.item_code)): (item_sales_bom and item_sales_bom.get(item.item_code)):
buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, buying_amount = get_buying_amount(item.item_code, self.doc.doctype, self.doc.name, item.name,
self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, stock_ledger_entries.get((item.item_code, item.warehouse), []),
item_sales_bom) item_sales_bom)
item.buying_amount = buying_amount >= 0.01 and buying_amount or 0 item.buying_amount = buying_amount >= 0.01 and buying_amount or 0

View File

@ -38,11 +38,13 @@ class StockController(AccountsController):
return gl_entries return gl_entries
def get_stock_ledger_entries(self, item_list=None, warehouse_list=None): def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
out = {}
if not (item_list and warehouse_list): if not (item_list and warehouse_list):
item_list, warehouse_list = self.get_distinct_item_warehouse() item_list, warehouse_list = self.get_distinct_item_warehouse()
if item_list and warehouse_list: if item_list and warehouse_list:
return webnotes.conn.sql("""select item_code, voucher_type, voucher_no, res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
voucher_detail_no, posting_date, posting_time, stock_value, voucher_detail_no, posting_date, posting_time, stock_value,
warehouse, actual_qty as qty from `tabStock Ledger Entry` warehouse, actual_qty as qty from `tabStock Ledger Entry`
where ifnull(`is_cancelled`, "No") = "No" and company = %s where ifnull(`is_cancelled`, "No") = "No" and company = %s
@ -52,6 +54,14 @@ class StockController(AccountsController):
('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))), ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
tuple([self.doc.company] + item_list + warehouse_list), as_dict=1) tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
for r in res:
if (r.item_code, r.warehouse) not in out:
out[(r.item_code, r.warehouse)] = []
out[(r.item_code, r.warehouse)].append(r)
return out
def get_distinct_item_warehouse(self): def get_distinct_item_warehouse(self):
item_list = [] item_list = []
warehouse_list = [] warehouse_list = []

View File

@ -113,4 +113,3 @@ def on_doctype_update():
webnotes.conn.commit() webnotes.conn.commit()
webnotes.conn.sql("""alter table `tabStock Ledger Entry` webnotes.conn.sql("""alter table `tabStock Ledger Entry`
add index posting_sort_index(posting_date, posting_time, name)""") add index posting_sort_index(posting_date, posting_time, name)""")
webnotes.conn.begin()

View File

@ -290,9 +290,8 @@ class DocType(StockController):
self.doc.stock_value_difference = 0.0 self.doc.stock_value_difference = 0.0
for d in self.entries: for d in self.entries:
self.doc.stock_value_difference -= get_buying_amount(d.item_code, d.warehouse, self.doc.stock_value_difference -= get_buying_amount(d.item_code, self.doc.doctype, self.doc.name,
d.actual_qty, self.doc.doctype, self.doc.name, d.voucher_detail_no, d.voucher_detail_no, stock_ledger_entries.get((d.item_code, d.warehouse), []))
stock_ledger_entries)
webnotes.conn.set(self.doc, "stock_value_difference", self.doc.stock_value_difference) webnotes.conn.set(self.doc, "stock_value_difference", self.doc.stock_value_difference)
def make_gl_entries(self): def make_gl_entries(self):

View File

@ -152,32 +152,28 @@ def get_warehouse_list(doctype, txt, searchfield, start, page_len, filters):
wlist.append([w]) wlist.append([w])
return wlist return wlist
def get_buying_amount(item_code, warehouse, qty, voucher_type, voucher_no, voucher_detail_no, def get_buying_amount(item_code, voucher_type, voucher_no, voucher_detail_no,
stock_ledger_entries, item_sales_bom=None): stock_ledger_entries, item_sales_bom=None):
if item_sales_bom and item_sales_bom.get(item_code): if item_sales_bom and item_sales_bom.get(item_code):
# sales bom item # sales bom item
buying_amount = 0.0 buying_amount = 0.0
for bom_item in item_sales_bom[item_code]: for bom_item in item_sales_bom[item_code]:
if bom_item.get("parent_detail_docname")==voucher_detail_no: if bom_item.get("parent_detail_docname")==voucher_detail_no:
buying_amount += _get_buying_amount(voucher_type, voucher_no, voucher_detail_no, buying_amount += _get_buying_amount(voucher_type, voucher_no, voucher_detail_no, stock_ledger_entries)
bom_item.item_code, bom_item.warehouse or warehouse,
bom_item.total_qty or (bom_item.qty * qty), stock_ledger_entries)
return buying_amount return buying_amount
else: else:
# doesn't have sales bom # doesn't have sales bom
return _get_buying_amount(voucher_type, voucher_no, voucher_detail_no, return _get_buying_amount(voucher_type, voucher_no, voucher_detail_no, stock_ledger_entries)
item_code, warehouse, qty, stock_ledger_entries)
def _get_buying_amount(voucher_type, voucher_no, item_row, item_code, warehouse, qty, def _get_buying_amount(voucher_type, voucher_no, item_row, stock_ledger_entries):
stock_ledger_entries): # IMP NOTE
relevant_stock_ledger_entries = [sle for sle in stock_ledger_entries # stock_ledger_entries should already be filtered by item_code and warehouse and
if sle.item_code == item_code and sle.warehouse == warehouse] # sorted by posting_date desc, posting_time desc
for i, sle in enumerate(stock_ledger_entries):
for i, sle in enumerate(relevant_stock_ledger_entries):
if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \ if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \
sle.voucher_detail_no == item_row: sle.voucher_detail_no == item_row:
previous_stock_value = len(relevant_stock_ledger_entries) > i+1 and \ previous_stock_value = len(stock_ledger_entries) > i+1 and \
flt(relevant_stock_ledger_entries[i+1].stock_value) or 0.0 flt(stock_ledger_entries[i+1].stock_value) or 0.0
buying_amount = previous_stock_value - flt(sle.stock_value) buying_amount = previous_stock_value - flt(sle.stock_value)