From 508bd4c7d52a6db6924a90862a0cd69c4aee3474 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 26 Jul 2013 13:09:40 +0530 Subject: [PATCH] [report] Item-wise Purchase Register [issue] webnotes/erpnext#374 --- .../item_wise_purchase_register.js | 9 +++ .../item_wise_purchase_register.py | 72 ++++++++++++++----- .../purchase_register/purchase_register.js | 2 +- .../purchase_register/purchase_register.py | 4 +- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js b/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js index 8323a1af78..0805fe500f 100644 --- a/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js +++ b/accounts/report/item_wise_purchase_register/item_wise_purchase_register.js @@ -25,15 +25,24 @@ wn.query_reports["Item-wise Purchase Register"] = { "fieldtype": "Link", "options": "Account", "get_query": function() { + var company = wn.query_report.filters_by_name.company.get_value(); return { "query": "accounts.utils.get_account_list", "filters": { "is_pl_account": "No", "debit_or_credit": "Credit", + "company": company, "master_type": "Supplier" } } } + }, + { + "fieldname":"company", + "label": "Company", + "fieldtype": "Link", + "options": "Company", + "default": wn.defaults.get_default("company") } ] } \ No newline at end of file diff --git a/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py b/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py index 9f5f071e02..38408371b1 100644 --- a/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py +++ b/accounts/report/item_wise_purchase_register/item_wise_purchase_register.py @@ -16,19 +16,28 @@ from __future__ import unicode_literals import webnotes +from webnotes.utils import flt def execute(filters=None): if not filters: filters = {} - columns = get_columns() + last_col = len(columns) - 1 + item_list = get_items(filters) aii_account_map = get_aii_accounts() + item_tax, tax_accounts = get_tax_accounts(item_list, columns) + data = [] for d in item_list: expense_head = d.expense_head or aii_account_map.get(d.company) - data.append([d.item_code, d.item_name, d.item_group, d.name, d.posting_date, + row = [d.item_code, d.item_name, d.item_group, d.parent, d.posting_date, d.supplier_name, d.credit_to, d.project_name, d.company, d.purchase_order, - d.purchase_receipt, expense_head, d.qty, d.rate, d.amount]) + d.purchase_receipt, expense_head, d.qty, d.rate, d.amount] + for tax in tax_accounts: + row.append(item_tax.get(d.parent, {}).get(d.item_code, {}).get(tax, 0)) + + row.append(sum(row[last_col:])) + data.append(row) return columns, data @@ -41,33 +50,60 @@ def get_columns(): "Expense Account:Link/Account:140", "Qty:Float:120", "Rate:Currency:120", "Amount:Currency:120"] - def get_conditions(filters): conditions = "" - if filters.get("account"): conditions += " and pi.credit_to = %(account)s" - - if filters.get("item_code"): conditions += " and pi_item.item_code = %(item_code)s" - - if filters.get("from_date"): conditions += " and pi.posting_date>=%(from_date)s" - if filters.get("to_date"): conditions += " and pi.posting_date<=%(to_date)s" + for opts in (("company", " and company=%(company)s"), + ("account", " and pi.credit_to = %(account)s"), + ("item_code", " and pi_item.item_code = %(item_code)s"), + ("from_date", " and pi.posting_date>=%(from_date)s"), + ("to_date", " and pi.posting_date<=%(to_date)s")): + if filters.get(opts[0]): + conditions += opts[1] return conditions def get_items(filters): conditions = get_conditions(filters) - return webnotes.conn.sql("""select pi.name, pi.posting_date, pi.credit_to, pi.company, + match_conditions = webnotes.build_match_conditions("Purchase Invoice") + + return webnotes.conn.sql("""select pi_item.parent, pi.posting_date, pi.credit_to, pi.company, pi.supplier, pi.remarks, pi_item.item_code, pi_item.item_name, pi_item.item_group, pi_item.project_name, pi_item.purchase_order, pi_item.purchase_receipt, pi_item.expense_head, pi_item.qty, pi_item.rate, pi_item.amount, pi.supplier_name from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item - where pi.name = pi_item.parent and pi.docstatus = 1 %s - order by pi.posting_date desc, pi_item.item_code desc""" % conditions, filters, as_dict=1) + where pi.name = pi_item.parent and pi.docstatus = 1 %s %s + order by pi.posting_date desc, pi_item.item_code desc""" % (conditions, match_conditions), filters, as_dict=1) def get_aii_accounts(): - aii_account_map = {} - for d in webnotes.conn.sql("select name, stock_received_but_not_billed from tabCompany", - as_dict=1): - aii_account_map.setdefault(d.name, d.stock_received_but_not_billed) + return dict(webnotes.conn.sql("select name, stock_received_but_not_billed from tabCompany")) + +def get_tax_accounts(item_list, columns): + import json + item_tax = {} + tax_accounts = [] + + tax_details = webnotes.conn.sql("""select parent, account_head, item_wise_tax_detail + from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice' + and docstatus = 1 and ifnull(account_head, '') != '' and category in ('Total', 'Valuation and Total') + and parent in (%s)""" % ', '.join(['%s']*len(item_list)), tuple([item.parent for item in item_list])) - return aii_account_map \ No newline at end of file + for parent, account_head, item_wise_tax_detail in tax_details: + if account_head not in tax_accounts: + tax_accounts.append(account_head) + + invoice = item_tax.setdefault(parent, {}) + if item_wise_tax_detail: + try: + item_wise_tax_detail = json.loads(item_wise_tax_detail) + for item, tax_amount in item_wise_tax_detail.items(): + invoice.setdefault(item, {})[account_head] = flt(tax_amount) + + except ValueError: + continue + + tax_accounts.sort() + columns += [account_head + ":Currency:80" for account_head in tax_accounts] + columns.append("Total:Currency:80") + + return item_tax, tax_accounts \ No newline at end of file diff --git a/accounts/report/purchase_register/purchase_register.js b/accounts/report/purchase_register/purchase_register.js index 21e0547f4a..4fbd2aabe7 100644 --- a/accounts/report/purchase_register/purchase_register.js +++ b/accounts/report/purchase_register/purchase_register.js @@ -36,7 +36,7 @@ wn.query_reports["Purchase Register"] = { "label": "Company", "fieldtype": "Link", "options": "Company", - "default": sys_defaults.company + "default": wn.defaults.get_default("company") } ] } \ No newline at end of file diff --git a/accounts/report/purchase_register/purchase_register.py b/accounts/report/purchase_register/purchase_register.py index aa55a3b076..12b93162a2 100644 --- a/accounts/report/purchase_register/purchase_register.py +++ b/accounts/report/purchase_register/purchase_register.py @@ -90,8 +90,8 @@ def get_columns(invoice_list): tax_accounts = webnotes.conn.sql_list("""select distinct account_head from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice' - and docstatus = 1 and ifnull(account_head, '') != '' and parent in (%s) - order by account_head""" % + and docstatus = 1 and ifnull(account_head, '') != '' and category in ('Total', 'Valuation and Total') + and parent in (%s) order by account_head""" % ', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]))