mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-25 07:54:46 +00:00
[report] Item-wise Purchase Register [issue] webnotes/erpnext#374
This commit is contained in:
@@ -25,15 +25,24 @@ wn.query_reports["Item-wise Purchase Register"] = {
|
|||||||
"fieldtype": "Link",
|
"fieldtype": "Link",
|
||||||
"options": "Account",
|
"options": "Account",
|
||||||
"get_query": function() {
|
"get_query": function() {
|
||||||
|
var company = wn.query_report.filters_by_name.company.get_value();
|
||||||
return {
|
return {
|
||||||
"query": "accounts.utils.get_account_list",
|
"query": "accounts.utils.get_account_list",
|
||||||
"filters": {
|
"filters": {
|
||||||
"is_pl_account": "No",
|
"is_pl_account": "No",
|
||||||
"debit_or_credit": "Credit",
|
"debit_or_credit": "Credit",
|
||||||
|
"company": company,
|
||||||
"master_type": "Supplier"
|
"master_type": "Supplier"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldname":"company",
|
||||||
|
"label": "Company",
|
||||||
|
"fieldtype": "Link",
|
||||||
|
"options": "Company",
|
||||||
|
"default": wn.defaults.get_default("company")
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -16,19 +16,28 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import webnotes
|
import webnotes
|
||||||
|
from webnotes.utils import flt
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
if not filters: filters = {}
|
if not filters: filters = {}
|
||||||
|
|
||||||
columns = get_columns()
|
columns = get_columns()
|
||||||
|
last_col = len(columns) - 1
|
||||||
|
|
||||||
item_list = get_items(filters)
|
item_list = get_items(filters)
|
||||||
aii_account_map = get_aii_accounts()
|
aii_account_map = get_aii_accounts()
|
||||||
|
item_tax, tax_accounts = get_tax_accounts(item_list, columns)
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
for d in item_list:
|
for d in item_list:
|
||||||
expense_head = d.expense_head or aii_account_map.get(d.company)
|
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.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
|
return columns, data
|
||||||
|
|
||||||
@@ -41,33 +50,60 @@ def get_columns():
|
|||||||
"Expense Account:Link/Account:140", "Qty:Float:120", "Rate:Currency:120",
|
"Expense Account:Link/Account:140", "Qty:Float:120", "Rate:Currency:120",
|
||||||
"Amount:Currency:120"]
|
"Amount:Currency:120"]
|
||||||
|
|
||||||
|
|
||||||
def get_conditions(filters):
|
def get_conditions(filters):
|
||||||
conditions = ""
|
conditions = ""
|
||||||
|
|
||||||
if filters.get("account"): conditions += " and pi.credit_to = %(account)s"
|
for opts in (("company", " and company=%(company)s"),
|
||||||
|
("account", " and pi.credit_to = %(account)s"),
|
||||||
if filters.get("item_code"): conditions += " and pi_item.item_code = %(item_code)s"
|
("item_code", " and pi_item.item_code = %(item_code)s"),
|
||||||
|
("from_date", " and pi.posting_date>=%(from_date)s"),
|
||||||
if filters.get("from_date"): conditions += " and pi.posting_date>=%(from_date)s"
|
("to_date", " and pi.posting_date<=%(to_date)s")):
|
||||||
if filters.get("to_date"): conditions += " and pi.posting_date<=%(to_date)s"
|
if filters.get(opts[0]):
|
||||||
|
conditions += opts[1]
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
def get_items(filters):
|
def get_items(filters):
|
||||||
conditions = get_conditions(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.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.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
|
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
|
from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item
|
||||||
where pi.name = pi_item.parent and pi.docstatus = 1 %s
|
where pi.name = pi_item.parent and pi.docstatus = 1 %s %s
|
||||||
order by pi.posting_date desc, pi_item.item_code desc""" % conditions, filters, as_dict=1)
|
order by pi.posting_date desc, pi_item.item_code desc""" % (conditions, match_conditions), filters, as_dict=1)
|
||||||
|
|
||||||
def get_aii_accounts():
|
def get_aii_accounts():
|
||||||
aii_account_map = {}
|
return dict(webnotes.conn.sql("select name, stock_received_but_not_billed from tabCompany"))
|
||||||
for d in webnotes.conn.sql("select name, stock_received_but_not_billed from tabCompany",
|
|
||||||
as_dict=1):
|
def get_tax_accounts(item_list, columns):
|
||||||
aii_account_map.setdefault(d.name, d.stock_received_but_not_billed)
|
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
|
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
|
||||||
@@ -36,7 +36,7 @@ wn.query_reports["Purchase Register"] = {
|
|||||||
"label": "Company",
|
"label": "Company",
|
||||||
"fieldtype": "Link",
|
"fieldtype": "Link",
|
||||||
"options": "Company",
|
"options": "Company",
|
||||||
"default": sys_defaults.company
|
"default": wn.defaults.get_default("company")
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -90,8 +90,8 @@ def get_columns(invoice_list):
|
|||||||
|
|
||||||
tax_accounts = webnotes.conn.sql_list("""select distinct account_head
|
tax_accounts = webnotes.conn.sql_list("""select distinct account_head
|
||||||
from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice'
|
from `tabPurchase Taxes and Charges` where parenttype = 'Purchase Invoice'
|
||||||
and docstatus = 1 and ifnull(account_head, '') != '' and parent in (%s)
|
and docstatus = 1 and ifnull(account_head, '') != '' and category in ('Total', 'Valuation and Total')
|
||||||
order by account_head""" %
|
and parent in (%s) order by account_head""" %
|
||||||
', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]))
|
', '.join(['%s']*len(invoice_list)), tuple([inv.name for inv in invoice_list]))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user