fix(report): Cost center filters in financial statements and general ledger

This commit is contained in:
Nabin Hait 2018-09-07 13:17:11 +05:30
parent ac696b9497
commit b479a87031
7 changed files with 51 additions and 84 deletions

View File

@ -1,17 +1,17 @@
{
"add_total_row": 0,
"apply_user_permissions": 1,
"creation": "2014-07-14 05:24:20.385279",
"disabled": 0,
"docstatus": 0,
"doctype": "Report",
"idx": 2,
"is_standard": "Yes",
"modified": "2017-02-24 20:12:47.161127",
"modified": "2018-09-07 12:18:21.850851",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Balance Sheet",
"owner": "Administrator",
"prepared_report": 0,
"ref_doctype": "GL Entry",
"report_name": "Balance Sheet",
"report_type": "Script Report",

View File

@ -351,7 +351,8 @@ def set_gl_entries_by_account(
"from_date": from_date,
"to_date": to_date,
"lft": root_lft,
"rgt": root_rgt
"rgt": root_rgt,
"cost_center": filters.cost_center
},
as_dict=True)
@ -381,10 +382,8 @@ def get_additional_conditions(from_date, ignore_closing_entries, filters):
additional_conditions.append("project = '%s'" % (frappe.db.escape(filters.get("project"))))
if filters.get("cost_center"):
if not isinstance(filters.get("cost_center"), list):
cost_centers = str(filters.get("cost_center")).strip()
filters.cost_center = [d.strip() for d in cost_centers.split(',') if d]
additional_conditions.append(get_cost_center_cond(filters.get("cost_center")))
filters.cost_center = get_cost_centers_with_children(filters.cost_center)
additional_conditions.append("cost_center in %(cost_center)s")
company_finance_book = erpnext.get_default_finance_book(filters.get("company"))
@ -397,14 +396,17 @@ def get_additional_conditions(from_date, ignore_closing_entries, filters):
return " and {}".format(" and ".join(additional_conditions)) if additional_conditions else ""
def get_cost_centers_with_children(cost_centers):
if not isinstance(cost_centers, list):
cost_centers = [d.strip() for d in str(cost_centers).strip().split(',') if d]
def get_cost_center_cond(cost_center):
cost_centers = frappe.db.get_all("Cost Center", {"name": ["in", cost_center]},
["name", "lft", "rgt"])
all_cost_centers = []
for d in cost_centers:
lft, rgt = frappe.db.get_value("Cost Center", d, ["lft", "rgt"])
children = frappe.get_all("Cost Center", filters={"lft": [">=", lft], "rgt": ["<=", rgt]})
all_cost_centers += [c.name for c in children]
lft_rgt = " or ".join(["(lft >=%s and rgt <=%s)" % (d.lft, d.rgt) for d in cost_centers])
return """ cost_center in (select name from `tabCost Center` where %s)""" % (lft_rgt)
return list(set(all_cost_centers))
def get_columns(periodicity, period_list, accumulated_values=1, company=None):
columns = [{

View File

@ -17,21 +17,6 @@ frappe.query_reports["General Ledger"] = {
"fieldtype": "Link",
"options": "Finance Book"
},
{
"fieldname":"cost_center",
"label": __("Cost Center"),
"fieldtype": "Link",
"options": "Cost Center",
"get_query": function() {
var company = frappe.query_report.get_filter_value('company');
return {
"doctype": "Cost Center",
"filters": {
"company": company,
}
}
}
},
{
"fieldname":"from_date",
"label": __("From Date"),

View File

@ -8,7 +8,7 @@ from erpnext.accounts.report.utils import get_currency, convert_to_presentation_
from frappe.utils import getdate, cstr, flt, fmt_money
from frappe import _, _dict
from erpnext.accounts.utils import get_account_currency
from erpnext.accounts.report.financial_statements import get_cost_centers_with_children
from six import iteritems
def execute(filters=None):
@ -155,9 +155,8 @@ def get_conditions(filters):
where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt))
if filters.get("cost_center"):
lft, rgt = frappe.db.get_value("Cost Center", filters["cost_center"], ["lft", "rgt"])
conditions.append("""cost_center in (select name from `tabCost Center`
where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt))
filters.cost_center = get_cost_centers_with_children(filters.cost_center)
conditions.append("cost_center in %(cost_center)s")
if filters.get("voucher_no"):
conditions.append("voucher_no=%(voucher_no)s")
@ -179,9 +178,6 @@ def get_conditions(filters):
if filters.get("project"):
conditions.append("project in %(project)s")
if filters.get("cost_center"):
conditions.append("cost_center in %(cost_center)s")
company_finance_book = erpnext.get_default_finance_book(filters.get("company"))
if not filters.get("finance_book") or (filters.get("finance_book") == company_finance_book):
filters['finance_book'] = company_finance_book

View File

@ -7,37 +7,6 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() {
erpnext.financial_statements);
frappe.query_reports["Profit and Loss Statement"]["filters"].push(
{
"fieldname":"cost_center",
"label": __("Cost Center"),
"fieldtype": "MultiSelect",
get_data: function() {
var cost_centers = frappe.query_report.get_filter_value("cost_center") || "";
const values = cost_centers.split(/\s*,\s*/).filter(d => d);
const txt = cost_centers.match(/[^,\s*]*$/)[0] || '';
let data = [];
frappe.call({
type: "GET",
method:'frappe.desk.search.search_link',
async: false,
no_spinner: true,
args: {
doctype: "Cost Center",
txt: txt,
filters: {
"company": frappe.query_report.get_filter_value("company"),
"name": ["not in", values]
}
},
callback: function(r) {
data = r.results;
}
});
return data;
}
},
{
"fieldname":"project",
"label": __("Project"),

View File

@ -81,16 +81,32 @@ function get_filters(){
{
"fieldname":"cost_center",
"label": __("Cost Center"),
"fieldtype": "Link",
"options": "Cost Center",
"get_query": function() {
var company = frappe.query_report.get_filter_value('company');
return {
"doctype": "Cost Center",
"filters": {
"company": company,
"fieldtype": "MultiSelect",
get_data: function() {
var cost_centers = frappe.query_report.get_filter_value("cost_center") || "";
const values = cost_centers.split(/\s*,\s*/).filter(d => d);
const txt = cost_centers.match(/[^,\s*]*$/)[0] || '';
let data = [];
frappe.call({
type: "GET",
method:'frappe.desk.search.search_link',
async: false,
no_spinner: true,
args: {
doctype: "Cost Center",
txt: txt,
filters: {
"company": frappe.query_report.get_filter_value("company"),
"name": ["not in", values]
}
},
callback: function(r) {
data = r.results;
}
};
});
return data;
}
},
{

View File

@ -392,15 +392,14 @@ def insert_item_price(args):
price_list_rate = (args.rate / args.get('conversion_factor')
if args.get("conversion_factor") else args.rate)
name = frappe.db.get_value('Item Price',
{'item_code': args.item_code, 'price_list': args.price_list, 'currency': args.currency}, 'name')
if name:
item_price = frappe.get_doc('Item Price', name)
item_price.price_list_rate = price_list_rate
item_price.save()
frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code,
args.price_list))
item_price = frappe.db.get_value('Item Price',
{'item_code': args.item_code, 'price_list': args.price_list, 'currency': args.currency},
['name', 'price_list_rate'], as_dict=1)
if item_price and item_price.name:
if item_price.price_list_rate != price_list_rate:
frappe.db.set_value('Item Price', item_price.name, "price_list_rate", price_list_rate)
frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code,
args.price_list), alert=True)
else:
item_price = frappe.get_doc({
"doctype": "Item Price",