From f200c5296d7a9161eb13b0abe178c660aefdf26c Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Fri, 21 Sep 2012 15:19:40 +0530 Subject: [PATCH 1/3] added sales analytics --- .../financial_analytics.js | 10 +- erpnext/patches/patch_list.py | 4 + erpnext/patches/september_2012/plot_patch.py | 32 +++ .../selling/page/sales_analytics/__init__.py | 0 .../page/sales_analytics/sales_analytics.js | 262 ++++++++++++++++++ .../page/sales_analytics/sales_analytics.txt | 28 ++ erpnext/startup/report_data_map.py | 74 ++++- .../page/stock_analytics/stock_analytics.js | 180 +++++++----- public/js/all-app.js | 8 +- 9 files changed, 500 insertions(+), 98 deletions(-) create mode 100644 erpnext/patches/september_2012/plot_patch.py create mode 100644 erpnext/selling/page/sales_analytics/__init__.py create mode 100644 erpnext/selling/page/sales_analytics/sales_analytics.js create mode 100644 erpnext/selling/page/sales_analytics/sales_analytics.txt diff --git a/erpnext/accounts/page/financial_analytics/financial_analytics.js b/erpnext/accounts/page/financial_analytics/financial_analytics.js index 1f0a01d4a5..6fbecd6b2b 100644 --- a/erpnext/accounts/page/financial_analytics/financial_analytics.js +++ b/erpnext/accounts/page/financial_analytics/financial_analytics.js @@ -164,13 +164,5 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({ }); return data; - }, - get_plot_options: function() { - return { - grid: { hoverable: true, clickable: true }, - xaxis: { mode: "time", - min: dateutil.str_to_obj(this.from_date).getTime(), - max: dateutil.str_to_obj(this.to_date).getTime() } - } - }, + } }) \ No newline at end of file diff --git a/erpnext/patches/patch_list.py b/erpnext/patches/patch_list.py index dbd4b8c10b..d0ec56d6ec 100644 --- a/erpnext/patches/patch_list.py +++ b/erpnext/patches/patch_list.py @@ -584,4 +584,8 @@ patch_list = [ 'patch_module': 'patches.september_2012', 'patch_file': 'add_stock_ledger_entry_index', }, + { + 'patch_module': 'patches.september_2012', + 'patch_file': 'plot_patch', + }, ] diff --git a/erpnext/patches/september_2012/plot_patch.py b/erpnext/patches/september_2012/plot_patch.py new file mode 100644 index 0000000000..fda0a27b8d --- /dev/null +++ b/erpnext/patches/september_2012/plot_patch.py @@ -0,0 +1,32 @@ +import webnotes +def execute(): + set_master_name_in_accounts() + set_customer_in_sales_invoices() + reset_lft_rgt() + +def set_master_name_in_accounts(): + accounts = webnotes.conn.sql("""select name, account_name, master_type from tabAccount + where ifnull(master_name, '')=''""", as_dict=1) + for acc in accounts: + if acc["master_type"] in ["Customer", "Supplier"]: + master = webnotes.conn.sql("""select name from `tab%s` + where name=%s """ % (acc["master_type"], "%s"), acc["account_name"]) + if master: + webnotes.conn.sql("""update `tabAccount` + set master_name=%s where name=%s""", (master[0][0], acc["name"])) + +def set_customer_in_sales_invoices(): + webnotes.conn.sql("""update `tabSales Invoice` si + set si.customer=(select a.master_name from `tabAccount` a where a.name=si.debit_to) + where ifnull(si.customer, '')=''""") + +def reset_lft_rgt(): + from webnotes.utils.nestedset import rebuild_tree + + rebuild_tree("Item Group", "parent_item_group") + rebuild_tree("Customer Group", "parent_customer_group") + rebuild_tree("Territory", "parent_territory") + rebuild_tree("Account", "parent_account") + rebuild_tree("Cost Center", "parent_cost_center") + rebuild_tree("Sales Person", "parent_sales_person") + \ No newline at end of file diff --git a/erpnext/selling/page/sales_analytics/__init__.py b/erpnext/selling/page/sales_analytics/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/selling/page/sales_analytics/sales_analytics.js b/erpnext/selling/page/sales_analytics/sales_analytics.js new file mode 100644 index 0000000000..8a0d9b26d6 --- /dev/null +++ b/erpnext/selling/page/sales_analytics/sales_analytics.js @@ -0,0 +1,262 @@ +wn.pages['sales-analytics'].onload = function(wrapper) { + wn.ui.make_app_page({ + parent: wrapper, + title: 'Sales Analytics', + single_column: true + }); + new erpnext.SalesAnalytics(wrapper); +} + +erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({ + init: function(wrapper) { + this._super({ + title: "Sales Analytics", + page: wrapper, + parent: $(wrapper).find('.layout-main'), + appframe: wrapper.appframe, + doctypes: ["Item", "Item Group", "Customer", "Customer Group", "Company", + "Sales Invoice", "Sales Invoice Item", "Territory"], + tree_grid: { show: true } + }); + + this.tree_grids = { + "Customer Group": { + label: "Customer Group / Customer", + show: true, + item_key: "customer", + parent_field: "parent_customer_group", + formatter: function(item) { + // return repl('%(value)s', { + // value: item.name, + // enc_value: encodeURIComponent(item.name) + // }); + return item.name; + } + }, + "Customer": { + label: "Customer", + show: false, + item_key: "customer", + formatter: function(item) { + return item.name; + } + }, + "Item Group": { + label: "Item", + show: true, + parent_field: "parent_item_group", + item_key: "item_code", + formatter: function(item) { + return item.name; + } + }, + "Item": { + label: "Item", + show: false, + item_key: "item_code", + formatter: function(item) { + return item.name; + } + }, + "Territory": { + label: "Territory / Customer", + show: true, + item_key: "customer", + parent_field: "parent_territory", + formatter: function(item) { + return item.name; + } + } + } + }, + setup_columns: function() { + this.tree_grid = this.tree_grids[this.tree_type]; + + var std_columns = [ + {id: "check", name: "Plot", field: "check", width: 30, + formatter: this.check_formatter}, + {id: "name", name: this.tree_grid.label, field: "name", width: 300, + formatter: this.tree_formatter, doctype: "Customer"}, + {id: "total", name: "Total", field: "total", plot: false, + formatter: this.currency_formatter} + ]; + + this.make_date_range_columns(); + this.columns = std_columns.concat(this.columns); + }, + filters: [ + {fieldtype:"Select", label: "Tree Type", options:["Customer Group", "Customer", + "Item Group", "Item", "Territory"], + filter: function(val, item, opts, me) { + return me.apply_zero_filter(val, item, opts, me); + }}, + {fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"]}, + {fieldtype:"Select", label: "Company", link:"Company", + default_value: "Select Company..."}, + {fieldtype:"Date", label: "From Date"}, + {fieldtype:"Label", label: "To"}, + {fieldtype:"Date", label: "To Date"}, + {fieldtype:"Select", label: "Range", + options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]}, + {fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"}, + {fieldtype:"Button", label: "Reset Filters"} + ], + setup_filters: function() { + var me = this; + this._super(); + + this.filter_inputs.value_or_qty.change(function() { + me.filter_inputs.refresh.click(); + }); + + this.filter_inputs.tree_type.change(function() { + me.filter_inputs.refresh.click(); + }); + + this.show_zero_check() + this.setup_plot_check(); + }, + init_filter_values: function() { + this._super(); + this.filter_inputs.range.val('Weekly'); + }, + prepare_data: function() { + var me = this; + if (!this.tl) { + this.make_tl("Sales Invoice", "Sales Invoice Item"); + + // add 'Not Set' Customer & Item + // (Customer / Item are not mandatory!!) + wn.report_dump.data["Customer"].push({ + name: "Not Set", + parent_customer_group: "All Customer Groups", + parent_territory: "All Territories", + id: "Not Set", + }); + + wn.report_dump.data["Item"].push({ + name: "Not Set", + parent_item_group: "All Item Groups", + id: "Not Set", + }); + } + + if(!this.data || me.item_type != me.tree_type) { + if(me.tree_type=='Customer') { + var items = wn.report_dump.data["Customer"]; + } if(me.tree_type=='Customer Group') { + var items = this.prepare_tree("Customer", "Customer Group"); + } else if(me.tree_type=="Item Group") { + var items = this.prepare_tree("Item", "Item Group"); + } else if(me.tree_type=="Item") { + var items = wn.report_dump.data["Item"]; + } else if(me.tree_type=="Territory") { + var items = this.prepare_tree("Customer", "Territory"); + } + + me.item_type = me.tree_type + me.parent_map = {}; + me.item_by_name = {}; + me.data = []; + + $.each(items, function(i, v) { + var d = copy_dict(v); + + me.data.push(d); + me.item_by_name[d.name] = d; + if(d[me.tree_grid.parent_field]) { + me.parent_map[d.name] = d[me.tree_grid.parent_field]; + } + me.reset_item_values(d); + }); + + this.set_indent(); + + } else { + // otherwise, only reset values + $.each(this.data, function(i, d) { + me.reset_item_values(d); + }); + } + + this.prepare_balances(); + if(me.tree_grid.show) { + this.set_totals(false); + this.update_groups(); + } else { + this.set_totals(true); + } + + }, + make_tl: function(parent_doctype, doctype) { + var me = this; + var tmap = {}; + $.each(wn.report_dump.data[doctype], function(i, v) { + if(!tmap[v.parent]) tmap[v.parent] = []; + tmap[v.parent].push(v); + }); + this.tl = []; + $.each(wn.report_dump.data[parent_doctype], function(i, parent) { + $.each(tmap[parent.name], function(i, d) { + me.tl.push($.extend(copy_dict(parent), d)); + }); + }); + }, + prepare_balances: function() { + var me = this; + var from_date = dateutil.str_to_obj(this.from_date); + var to_date = dateutil.str_to_obj(this.to_date); + var is_val = this.value_or_qty == 'Value'; + + $.each(this.tl, function(i, tl) { + if (me.is_default('company') ? true : me.apply_filter(tl, "company")) { + var posting_date = dateutil.str_to_obj(tl.posting_date); + if (posting_date >= from_date && posting_date <= to_date) { + var item = me.item_by_name[tl[me.tree_grid.item_key]] || me.item_by_name['Not Set']; + item[me.column_map[tl.posting_date].field] += (is_val ? tl.amount : tl.qty); + } + } + }); + }, + update_groups: function() { + var me = this; + + $.each(this.data, function(i, item) { + var parent = me.parent_map[item.name]; + while(parent) { + parent_group = me.item_by_name[parent]; + + $.each(me.columns, function(c, col) { + if (col.formatter == me.currency_formatter) { + parent_group[col.field] = + flt(parent_group[col.field]) + + flt(item[col.field]); + } + }); + parent = me.parent_map[parent]; + } + }); + }, + set_totals: function(sort) { + var me = this; + var checked = false; + $.each(this.data, function(i, d) { + d.total = 0.0; + $.each(me.columns, function(i, col) { + if(col.formatter==me.currency_formatter && !col.hidden && col.field!="total") + d.total += d[col.field]; + if(d.checked) checked = true; + }) + }); + + if(sort)this.data = this.data.sort(function(a, b) { return a.total < b.total; }); + + if(!this.checked) { + this.data[0].checked = true; + } + }, + get_plot_points: function(item, col, idx) { + return [[dateutil.str_to_obj(col.id).getTime(), item[col.field]], + [dateutil.user_to_obj(col.name).getTime(), item[col.field]]]; + } +}); \ No newline at end of file diff --git a/erpnext/selling/page/sales_analytics/sales_analytics.txt b/erpnext/selling/page/sales_analytics/sales_analytics.txt new file mode 100644 index 0000000000..3782df6e14 --- /dev/null +++ b/erpnext/selling/page/sales_analytics/sales_analytics.txt @@ -0,0 +1,28 @@ +# Page, sales-analytics +[ + + # These values are common in all dictionaries + { + u'creation': '2012-09-21 12:06:14', + u'docstatus': 0, + u'modified': '2012-09-21 12:06:14', + u'modified_by': u'Administrator', + u'owner': u'Administrator' + }, + + # These values are common for all Page + { + u'doctype': u'Page', + 'module': u'Selling', + u'name': u'__common__', + 'page_name': u'sales-analytics', + 'standard': u'Yes', + 'title': u'Sales Analytics' + }, + + # Page, sales-analytics + { + u'doctype': u'Page', + u'name': u'sales-analytics' + } +] \ No newline at end of file diff --git a/erpnext/startup/report_data_map.py b/erpnext/startup/report_data_map.py index 506685397e..0de1e736a1 100644 --- a/erpnext/startup/report_data_map.py +++ b/erpnext/startup/report_data_map.py @@ -20,6 +20,16 @@ from __future__ import unicode_literals # "remember to add indexes!" data_map = { + "Company": { + "columns": ["name"], + "conditions": ["docstatus < 2"] + }, + "Fiscal Year": { + "columns": ["name", "year_start_date", + "adddate(adddate(year_start_date, interval 1 year), interval -1 day) as year_end_date"] + }, + + # Accounts "Account": { "columns": ["name", "parent_account", "lft", "rgt", "debit_or_credit", "is_pl_account", "company"], @@ -40,13 +50,23 @@ data_map = { "company": ["Company", "name"] } }, - "Company": { - "columns": ["name"], - "conditions": ["docstatus < 2"] + + # Stock + "Item": { + "columns": ["name", "if(item_name=name, '', item_name) as item_name", + "item_group as parent_item_group", "stock_uom", "brand"], + "order_by": "name", + "links": { + "parent_item_group": ["Item Group", "name"], + } }, - "Fiscal Year": { - "columns": ["name", "year_start_date", - "adddate(adddate(year_start_date, interval 1 year), interval -1 day) as year_end_date"] + "Item Group": { + "columns": ["name", "parent_item_group"], + "order_by": "lft" + }, + "Warehouse": { + "columns": ["name"], + "order_by": "name" }, "Stock Ledger Entry": { "columns": ["posting_date", "posting_time", "item_code", "warehouse", "actual_qty as qty", @@ -59,17 +79,41 @@ data_map = { }, "force_index": "posting_sort_index" }, - "Item": { - "columns": ["name", "if(item_name=name, '', item_name) as item_name", - "item_group as parent_item_group", "stock_uom", "brand"], - "order_by": "name" + + # Sales + "Customer": { + "columns": ["name", "if(customer_name=name, '', customer_name) as customer_name", + "customer_group as parent_customer_group", "territory as parent_territory"], + "order_by": "name", + "links": { + "parent_customer_group": ["Customer Group", "name"], + "parent_territory": ["Territory", "name"], + } }, - "Item Group": { - "columns": ["name", "lft", "rgt", "parent_item_group"], + "Customer Group": { + "columns": ["name", "parent_customer_group"], "order_by": "lft" }, - "Warehouse": { - "columns": ["name"], - "order_by": "name" + "Territory": { + "columns": ["name", "parent_territory"], + "order_by": "lft" + }, + "Sales Invoice": { + "columns": ["name", "customer", "posting_date", "company"], + "conditions": ["docstatus=1"], + "order_by": "posting_date", + "links": { + "customer": ["Customer", "name"], + "company":["Company", "name"] + } + }, + "Sales Invoice Item": { + "columns": ["parent", "item_code", "qty", "amount"], + "conditions": ["docstatus=1", "ifnull(parent, '')!=''"], + "order_by": "parent", + "links": { + "parent": ["Sales Invoice", "name"], + "item_code": ["Item", "name"] + } } } diff --git a/erpnext/stock/page/stock_analytics/stock_analytics.js b/erpnext/stock/page/stock_analytics/stock_analytics.js index 2e65f5c5ef..42b60b2ef7 100644 --- a/erpnext/stock/page/stock_analytics/stock_analytics.js +++ b/erpnext/stock/page/stock_analytics/stock_analytics.js @@ -25,7 +25,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ enc_value: encodeURIComponent(item.name) }); } - } + }, }) }, setup_columns: function() { @@ -35,10 +35,6 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ {id: "name", name: "Item", field: "name", width: 300, formatter: this.tree_formatter, doctype: "Item"}, {id: "opening", name: "Opening", field: "opening", hidden: true, - formatter: this.currency_formatter}, - {id: "balance_qty", name: "Balance Qty", field: "balance_qty", hidden: true, - formatter: this.currency_formatter}, - {id: "balance_value", name: "Balance Value", field: "balance_value", hidden: true, formatter: this.currency_formatter} ]; @@ -46,7 +42,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ this.columns = std_columns.concat(this.columns); }, filters: [ - {fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"], + {fieldtype:"Select", label: "Value or Qty", options:["Value (Weighted Average)", "Value (FIFO)", "Quantity"], filter: function(val, item, opts, me) { return me.apply_zero_filter(val, item, opts, me); }}, @@ -90,9 +86,9 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ }, prepare_data: function() { var me = this; - + if(!this.data) { - var items = this.get_item_tree(); + var items = this.prepare_tree("Item", "Item Group"); me.parent_map = {}; me.item_by_name = {}; @@ -109,6 +105,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ me.reset_item_values(d); }); this.set_indent(); + this.data[0].checked = true; } else { // otherwise, only reset values $.each(this.data, function(i, d) { @@ -120,30 +117,20 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ this.update_groups(); }, - get_item_tree: function() { - // prepare map with items in respective item group - var item_group_map = {}; - $.each(wn.report_dump.data["Item"], function(i, item) { - var parent = item.parent_item_group - if(!item_group_map[parent]) item_group_map[parent] = []; - item_group_map[parent].push(item); - }); - - // arrange items besides their parent item groups - var items = []; - $.each(wn.report_dump.data["Item Group"], function(i, group){ - group.is_group = true; - items.push(group); - items = items.concat(item_group_map[group.name] || []); - }); - return items; - }, prepare_balances: function() { var me = this; var from_date = dateutil.str_to_obj(this.from_date); var to_date = dateutil.str_to_obj(this.to_date); var data = wn.report_dump.data["Stock Ledger Entry"]; - var is_value = me.value_or_qty == "Value"; + + var warehouse_item = {}; + var get_warehouse_item = function(warehouse, item) { + if(!warehouse_item[warehouse]) warehouse_item[warehouse] = {}; + if(!warehouse_item[warehouse][item]) warehouse_item[warehouse][item] = { + balance_qty: 0.0, balance_value: 0.0, fifo_stack: [] + }; + return warehouse_item[warehouse][item]; + } for(var i=0, j=data.length; i 0 ? sl.incoming_rate : - (item.balance_qty.toFixed(2) == 0.00 ? 0 : flt(item.balance_value) / flt(item.balance_qty)); - var value_diff = (rate * sl.qty); - - // update balance - item.balance_qty += sl.qty; - item.balance_value += value_diff; - - var diff = is_value ? value_diff : sl.qty; + + if(me.value_or_qty!="Quantity") { + var wh = get_warehouse_item(sl.warehouse, sl.item_code); + var diff = me.get_value_diff(wh, sl); + } else { + var diff = sl.qty; + } if(posting_datetime < from_date) { item.opening += diff; @@ -174,6 +157,92 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ } } }, + get_value_diff: function(wh, sl) { + var is_fifo = this.value_or_qty== "Value (FIFO)"; + + // value + if(sl.qty > 0) { + // incoming - rate is given + var rate = sl.incoming_rate; + var add_qty = sl.qty; + if(wh.balance_qty < 0) { + // negative valuation + // only add value of quantity if + // the balance goes above 0 + add_qty = wh.balance_qty + sl.qty; + if(add_qty < 0) { + add_qty = 0; + } + } + var value_diff = (rate * add_qty); + + if(add_qty) + wh.fifo_stack.push([add_qty, sl.incoming_rate]); + } else { + // outgoing + + if(is_fifo) { + var value_diff = this.get_fifo_value_diff(wh, sl); + } else { + // average rate for weighted average + var rate = (wh.balance_qty.toFixed(2) == 0.00 ? 0 : + flt(wh.balance_value) / flt(wh.balance_qty)); + + // no change in value if negative qty + if((wh.balance_qty + sl.qty).toFixed(2) >= 0.00) + var value_diff = (rate * sl.qty); + else + var value_diff = -wh.balance_value; + } + } + + // update balance (only needed in case of valuation) + wh.balance_qty += sl.qty; + wh.balance_value += value_diff; + + if(sl.item_code=="0.5Motor") { + console.log([sl.voucher_no, sl.qty, sl.warehouse, value_diff]); + console.log(wh.fifo_stack); + } + + return value_diff; + }, + get_fifo_value_diff: function(wh, sl) { + // get exact rate from fifo stack + var fifo_stack = wh.fifo_stack.reverse(); + var fifo_value_diff = 0.0; + var qty = -sl.qty; + + for(var i=0, j=fifo_stack.length; i= qty) { + batch[0] = batch[0] - qty; + fifo_value_diff += (qty * batch[1]); + + qty = 0.0; + if(batch[0]) { + // batch still has qty put it back + fifo_stack.push(batch); + } + + // all qty found + break; + } else { + // consume this batch fully + fifo_value_diff += (batch[0] * batch[1]); + qty = qty - batch[0]; + } + } + if(qty) { + // msgprint("Negative values not allowed for FIFO valuation!\ + // Item " + sl.item_code.bold() + " on " + dateutil.str_to_user(sl.posting_datetime).bold() + + // " becomes negative. Values computed will not be accurate."); + } + + // reset the updated stack + wh.fifo_stack = fifo_stack.reverse(); + return -fifo_value_diff; + }, update_groups: function() { var me = this; @@ -203,36 +272,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ } }); }, - get_plot_data: function() { - var data = []; - var me = this; - $.each(this.data, function(i, item) { - if (item.checked) { - data.push({ - label: item.name, - data: $.map(me.columns, function(col, idx) { - if(col.formatter==me.currency_formatter && !col.hidden) { - return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]] - } - }), - points: {show: true}, - lines: {show: true, fill: true}, - }); - - // prepend opening - data[data.length-1].data = [[dateutil.str_to_obj(me.from_date).getTime(), - item.opening]].concat(data[data.length-1].data); - } - }); - - return data; - }, - get_plot_options: function() { - return { - grid: { hoverable: true, clickable: true }, - xaxis: { mode: "time", - min: dateutil.str_to_obj(this.from_date).getTime(), - max: dateutil.str_to_obj(this.to_date).getTime() } - } + get_plot_points: function(item, col, idx) { + return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]] } -}); +}); \ No newline at end of file diff --git a/public/js/all-app.js b/public/js/all-app.js index fdd710bf67..d6c99a0ddd 100644 --- a/public/js/all-app.js +++ b/public/js/all-app.js @@ -1076,8 +1076,8 @@ me.list.run();});this.dialog.show();},add_column:function(c){var w=$('
').appendTo(this.parent);if(this.filters){this.make_filters();} +if(missing.length){wn.call({method:"webnotes.widgets.report_dump.get_data",args:{doctypes:doctypes,missing:missing},callback:function(r){$.each(r.message,function(doctype,doctype_data){var data=[];$.each(doctype_data.data,function(i,d){var row={};$.each(doctype_data.columns,function(idx,col){row[col]=d[idx];});row.id=row.name||doctype+"-"+i +data.push(row);});wn.report_dump.data[doctype]=data;});$.each(r.message,function(doctype,doctype_data){if(doctype_data.links){$.each(wn.report_dump.data[doctype],function(row_idx,row){$.each(doctype_data.links,function(link_key,link){if(wn.report_dump.data[link[0]][row[link_key]]){row[link_key]=wn.report_dump.data[link[0]][row[link_key]][link[1]];}else{row[link_key]=null;}})})}});callback();},progress_bar:progress_bar})}else{callback();}}});wn.provide("wn.views");wn.views.GridReport=Class.extend({init:function(opts){this.filter_inputs={};this.preset_checks=[];this.tree_grid={show:false};$.extend(this,opts);this.wrapper=$('
').appendTo(this.parent);if(this.filters){this.make_filters();} this.make_waiting();this.import_slickgrid();var me=this;this.get_data();},bind_show:function(){var me=this;$(this.page).bind('show',function(){wn.cur_grid_report=me;me.apply_filters_from_route();me.refresh();});},get_data:function(){var me=this;wn.report_dump.with_data(this.doctypes,function(){me.setup_filters();me.init_filter_values();me.refresh();},this.wrapper.find(".progress .bar"));},setup_filters:function(){var me=this;$.each(me.filter_inputs,function(i,v){var opts=v.get(0).opts;if(opts.fieldtype=="Select"&&inList(me.doctypes,opts.link)){$(v).add_options($.map(wn.report_dump.data[opts.link],function(d){return d.name;}));}});this.filter_inputs.refresh&&this.filter_inputs.refresh.click(function(){me.set_route();});this.filter_inputs.reset_filters&&this.filter_inputs.reset_filters.click(function(){me.init_filter_values();me.set_route();});},init_filter_values:function(){var me=this;$.each(this.filter_inputs,function(key,filter){var opts=filter.get(0).opts;if(sys_defaults[key]){filter.val(sys_defaults[key]);}else if(opts.fieldtype=='Select'){filter.get(0).selectedIndex=0;}else if(opts.fieldtype=='Data'){filter.val("");}}) if(this.filter_inputs.from_date) this.filter_inputs.from_date.val(dateutil.str_to_user(sys_defaults.year_start_date));if(this.filter_inputs.to_date) @@ -1107,7 +1107,7 @@ e.stopImmediatePropagation();}});},tree_formatter:function(row,cell,value,column (15*dataContext["indent"])+"px'>";var idx=me.dataView.getIdxById(dataContext.id);var link=me.tree_grid.formatter(dataContext);if(columnDef.doctype){link+=me.get_link_open_icon(columnDef.doctype,value);} if(data[idx+1]&&data[idx+1].indent>data[idx].indent){if(dataContext._collapsed){return spacer+"  "+link;}else{return spacer+"  "+link;}}else{return spacer+"  "+link;}},tree_dataview_filter:function(item){var me=wn.cur_grid_report;if(!me.apply_filters(item))return false;var parent=item[me.tree_grid.parent_field];while(parent){if(me.item_by_name[parent]._collapsed){return false;} parent=me.parent_map[parent];} -return true;},set_indent:function(){var me=this;$.each(this.data,function(i,d){var indent=0;var parent=me.parent_map[d.name];if(parent){while(parent){indent++;parent=me.parent_map[parent];}} +return true;},prepare_tree:function(item_dt,group_dt){var group_data=wn.report_dump.data[group_dt];var item_data=wn.report_dump.data[item_dt];var me=this;var item_group_map={};var group_ids=$.map(group_data,function(v){return v.id;});$.each(item_data,function(i,item){var parent=item[me.tree_grid.parent_field];if(!item_group_map[parent])item_group_map[parent]=[];if(group_ids.indexOf(item.name)==-1){item_group_map[parent].push(item);}else{msgprint("Ignoring Item "+item.name.bold()+", because a group exists with the same name!");}});var items=[];$.each(group_data,function(i,group){group.is_group=true;items.push(group);items=items.concat(item_group_map[group.name]||[]);});return items;},set_indent:function(){var me=this;$.each(this.data,function(i,d){var indent=0;var parent=me.parent_map[d.name];if(parent){while(parent){indent++;parent=me.parent_map[parent];}} d.indent=indent;});},apply_filters:function(item){var filters=this.filter_inputs;if(item._show)return true;for(i in filters){if(!this.apply_filter(item,i))return false;} return true;},apply_filter:function(item,fieldname){var filter=this.filter_inputs[fieldname].get(0);if(filter.opts.filter){if(!filter.opts.filter(this[filter.opts.fieldname],item,filter.opts,this)){return false;}} return true;},apply_zero_filter:function(val,item,opts,me){if(!me.show_zero){for(var i=0,j=me.columns.length;i0.001||flt(item[col.field])<-0.001){return true;}}} @@ -1131,7 +1131,7 @@ this.previousPoint=null;this.wrapper.find('.plot').bind("plothover",function(eve else{$("#"+me.tooltip_id).remove();me.previousPoint=null;}});},get_tooltip_text:function(label,x,y){var date=dateutil.obj_to_user(new Date(x));var value=fmt_money(y);return value+" on "+date;},get_view_data:function(){var res=[];var col_map=$.map(this.columns,function(v){return v.field;});for(var i=0,len=this.dataView.getLength();i Date: Fri, 21 Sep 2012 19:46:24 +0530 Subject: [PATCH 2/3] Stock Ageing & release of Analytics --- .../page/accounts_home/accounts_home.html | 15 + .../financial_analytics.js | 65 +++-- .../page/general_ledger/general_ledger.js | 19 +- .../buying/page/buying_home/buying_home.html | 5 + .../page/purchase_analytics/__init__.py | 0 .../purchase_analytics/purchase_analytics.js | 264 ++++++++++++++++++ .../purchase_analytics/purchase_analytics.txt | 28 ++ erpnext/patches/september_2012/plot_patch.py | 10 +- .../page/sales_analytics/sales_analytics.js | 36 +-- .../page/selling_home/selling_home.html | 5 + erpnext/setup/__init__.py | 3 +- erpnext/startup/js/modules.js | 12 +- erpnext/startup/report_data_map.py | 39 ++- erpnext/stock/page/stock_ageing/__init__.py | 0 .../stock/page/stock_ageing/stock_ageing.js | 168 +++++++++++ .../stock/page/stock_ageing/stock_ageing.txt | 28 ++ .../page/stock_analytics/stock_analytics.js | 127 ++------- erpnext/stock/page/stock_home/stock_home.html | 15 + .../stock/page/stock_ledger/stock_ledger.js | 63 ++++- public/js/all-app.js | 31 +- public/js/app/account_tree_grid.js | 7 +- public/js/app/stock_grid_report.js | 105 +++++++ 22 files changed, 869 insertions(+), 176 deletions(-) create mode 100644 erpnext/buying/page/purchase_analytics/__init__.py create mode 100644 erpnext/buying/page/purchase_analytics/purchase_analytics.js create mode 100644 erpnext/buying/page/purchase_analytics/purchase_analytics.txt create mode 100644 erpnext/stock/page/stock_ageing/__init__.py create mode 100644 erpnext/stock/page/stock_ageing/stock_ageing.js create mode 100644 erpnext/stock/page/stock_ageing/stock_ageing.txt create mode 100644 public/js/app/stock_grid_report.js diff --git a/erpnext/accounts/page/accounts_home/accounts_home.html b/erpnext/accounts/page/accounts_home/accounts_home.html index 5e1b1c8b45..11cf126565 100644 --- a/erpnext/accounts/page/accounts_home/accounts_home.html +++ b/erpnext/accounts/page/accounts_home/accounts_home.html @@ -17,6 +17,21 @@

Chart of Cost Centers

Structure cost centers

+
+

General Ledger + beta +

+

General Ledger Report

+
+

Trial Balance + beta +

+

Tree view of all Account balances

+
+

Financial Analytics + beta +

+

Visual representation of financial trends


diff --git a/erpnext/accounts/page/financial_analytics/financial_analytics.js b/erpnext/accounts/page/financial_analytics/financial_analytics.js index 6fbecd6b2b..95350a09bf 100644 --- a/erpnext/accounts/page/financial_analytics/financial_analytics.js +++ b/erpnext/accounts/page/financial_analytics/financial_analytics.js @@ -32,7 +32,7 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({ if(item._show) return true; // pl or bs - var out = (val=='Profit and Loss') ? item.is_pl_account=='Yes' : item.is_pl_account!='Yes'; + var out = (val!='Balance Sheet') ? item.is_pl_account=='Yes' : item.is_pl_account!='Yes'; if(!out) return false; return me.apply_zero_filter(val, item, opts, me); @@ -69,7 +69,7 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({ this._super(); this.filter_inputs.pl_or_bs.change(function() { me.filter_inputs.refresh.click(); - }); + }).add_options($.map(wn.report_dump.data["Cost Center"], function(v) {return v.name;})); this.setup_plot_check(); }, init_filter_values: function() { @@ -79,29 +79,51 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({ prepare_balances: function() { var me = this; + // setup cost center map + if(!this.cost_center_by_name) { + this.cost_center_by_name = this.make_name_map(wn.report_dump.data["Cost Center"]); + } + + var cost_center = inList(["Balance Sheet", "Profit and Loss"], this.pl_or_bs) + ? null : this.cost_center_by_name[this.pl_or_bs]; + $.each(wn.report_dump.data['GL Entry'], function(i, gl) { - var posting_date = dateutil.str_to_obj(gl.posting_date); - var account = me.item_by_name[gl.account]; - var col = me.column_map[gl.posting_date]; - - if(col) { - if(gl.voucher_type=='Period Closing Voucher') { - // period closing voucher not to be added - // to profit and loss accounts (else will become zero!!) - if(account.is_pl_account!='Yes') - me.add_balance(col.field, account, gl); + var filter_by_cost_center = (function() { + if(cost_center) { + if(gl.cost_center) { + var gl_cost_center = me.cost_center_by_name[gl.cost_center]; + return gl_cost_center.lft >= cost_center.lft && gl_cost_center.rgt <= cost_center.rgt; + } else { + return false; + } } else { - me.add_balance(col.field, account, gl); + return true; + } + })(); + + if(filter_by_cost_center) { + var posting_date = dateutil.str_to_obj(gl.posting_date); + var account = me.item_by_name[gl.account]; + var col = me.column_map[gl.posting_date]; + if(col) { + if(gl.voucher_type=='Period Closing Voucher') { + // period closing voucher not to be added + // to profit and loss accounts (else will become zero!!) + if(account.is_pl_account!='Yes') + me.add_balance(col.field, account, gl); + } else { + me.add_balance(col.field, account, gl); + } + + } else if(account.is_pl_account!='Yes' + && (posting_date < dateutil.str_to_obj(me.from_date))) { + me.add_balance('opening', account, gl); } - - } else if(account.is_pl_account!='Yes' - && (posting_date < dateutil.str_to_obj(me.from_date))) { - me.add_balance('opening', account, gl); } }); // make balances as cumulative - if(me.filter_inputs.pl_or_bs.val()=='Balance Sheet') { + if(me.pl_or_bs=='Balance Sheet') { $.each(me.data, function(i, ac) { if((ac.rgt - ac.lft)==1 && ac.is_pl_account!='Yes') { var opening = flt(ac.opening); @@ -135,15 +157,16 @@ erpnext.FinancialAnalytics = erpnext.AccountTreeGrid.extend({ get_plot_data: function() { var data = []; var me = this; - var pl_or_bs = this.filter_inputs.pl_or_bs.val(); + var pl_or_bs = this.pl_or_bs; $.each(this.data, function(i, account) { - var show = pl_or_bs == "Profit and Loss" ? account.is_pl_account=="Yes" : account.is_pl_account!="Yes"; + + var show = pl_or_bs != "Balance Sheet" ? account.is_pl_account=="Yes" : account.is_pl_account!="Yes"; if (show && account.checked && me.apply_filter(account, "company")) { data.push({ label: account.name, data: $.map(me.columns, function(col, idx) { if(col.formatter==me.currency_formatter && !col.hidden) { - if (pl_or_bs == "Profit and Loss") { + if (pl_or_bs != "Balance Sheet") { return [[dateutil.str_to_obj(col.id).getTime(), account[col.field]], [dateutil.user_to_obj(col.name).getTime(), account[col.field]]]; } else { diff --git a/erpnext/accounts/page/general_ledger/general_ledger.js b/erpnext/accounts/page/general_ledger/general_ledger.js index a47c076148..cbe88fb4e2 100644 --- a/erpnext/accounts/page/general_ledger/general_ledger.js +++ b/erpnext/accounts/page/general_ledger/general_ledger.js @@ -1,3 +1,19 @@ +// ERPNext - web based ERP (http://erpnext.com) +// Copyright (C) 2012 Web Notes Technologies Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + wn.pages['general-ledger'].onload = function(wrapper) { wn.ui.make_app_page({ parent: wrapper, @@ -10,7 +26,7 @@ wn.pages['general-ledger'].onload = function(wrapper) { page: wrapper, parent: $(wrapper).find('.layout-main'), appframe: wrapper.appframe, - doctypes: ["Company", "Account", "GL Entry"], + doctypes: ["Company", "Account", "GL Entry", "Cost Center"], setup_columns: function() { this.columns = [ @@ -20,7 +36,6 @@ wn.pages['general-ledger'].onload = function(wrapper) { link_formatter: { filter_input: "account", open_btn: true, - doctype: '"Account"' }}, {id: "debit", name: "Debit", field: "debit", width: 100, formatter: this.currency_formatter}, diff --git a/erpnext/buying/page/buying_home/buying_home.html b/erpnext/buying/page/buying_home/buying_home.html index c2eead1100..384a77e725 100644 --- a/erpnext/buying/page/buying_home/buying_home.html +++ b/erpnext/buying/page/buying_home/buying_home.html @@ -23,6 +23,11 @@

Address

Address Master

+
+

Purchase Analytics + beta +

+

Purchase trends based on Purchase Invoice


diff --git a/erpnext/buying/page/purchase_analytics/__init__.py b/erpnext/buying/page/purchase_analytics/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/buying/page/purchase_analytics/purchase_analytics.js b/erpnext/buying/page/purchase_analytics/purchase_analytics.js new file mode 100644 index 0000000000..ac0e41fe43 --- /dev/null +++ b/erpnext/buying/page/purchase_analytics/purchase_analytics.js @@ -0,0 +1,264 @@ +// ERPNext - web based ERP (http://erpnext.com) +// Copyright (C) 2012 Web Notes Technologies Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +wn.pages['purchase-analytics'].onload = function(wrapper) { + wn.ui.make_app_page({ + parent: wrapper, + title: 'Purchase Analytics', + single_column: true + }); + + new erpnext.PurchaseAnalytics(wrapper); +} + +erpnext.PurchaseAnalytics = wn.views.TreeGridReport.extend({ + init: function(wrapper) { + this._super({ + title: "Purchase Analytics", + page: wrapper, + parent: $(wrapper).find('.layout-main'), + appframe: wrapper.appframe, + doctypes: ["Item", "Item Group", "Supplier", "Supplier Type", "Company", + "Purchase Invoice", "Purchase Invoice Item"], + tree_grid: { show: true } + }); + + this.tree_grids = { + "Supplier Type": { + label: "Supplier Type / Supplier", + show: true, + item_key: "supplier", + parent_field: "parent_supplier_type", + formatter: function(item) { + // return repl('%(value)s', { + // value: item.name, + // enc_value: encodeURIComponent(item.name) + // }); + return item.name; + } + }, + "Supplier": { + label: "Supplier", + show: false, + item_key: "supplier", + formatter: function(item) { + return item.name; + } + }, + "Item Group": { + label: "Item", + show: true, + parent_field: "parent_item_group", + item_key: "item_code", + formatter: function(item) { + return item.name; + } + }, + "Item": { + label: "Item", + show: false, + item_key: "item_code", + formatter: function(item) { + return item.name; + } + }, + } + }, + setup_columns: function() { + this.tree_grid = this.tree_grids[this.tree_type]; + + var std_columns = [ + {id: "check", name: "Plot", field: "check", width: 30, + formatter: this.check_formatter}, + {id: "name", name: this.tree_grid.label, field: "name", width: 300, + formatter: this.tree_formatter}, + {id: "total", name: "Total", field: "total", plot: false, + formatter: this.currency_formatter} + ]; + + this.make_date_range_columns(); + this.columns = std_columns.concat(this.columns); + }, + filters: [ + {fieldtype:"Select", label: "Tree Type", options:["Supplier Type", "Supplier", + "Item Group", "Item"], + filter: function(val, item, opts, me) { + return me.apply_zero_filter(val, item, opts, me); + }}, + {fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"]}, + {fieldtype:"Select", label: "Company", link:"Company", + default_value: "Select Company..."}, + {fieldtype:"Date", label: "From Date"}, + {fieldtype:"Label", label: "To"}, + {fieldtype:"Date", label: "To Date"}, + {fieldtype:"Select", label: "Range", + options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]}, + {fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"}, + {fieldtype:"Button", label: "Reset Filters"} + ], + setup_filters: function() { + var me = this; + this._super(); + + this.filter_inputs.value_or_qty.change(function() { + me.filter_inputs.refresh.click(); + }); + + this.filter_inputs.tree_type.change(function() { + me.filter_inputs.refresh.click(); + }); + + this.show_zero_check() + this.setup_plot_check(); + }, + init_filter_values: function() { + this._super(); + this.filter_inputs.range.val('Weekly'); + }, + prepare_data: function() { + var me = this; + if (!this.tl) { + this.make_transaction_list("Purchase Invoice", "Purchase Invoice Item"); + + // add 'Not Set' Supplier & Item + // Add 'All Supplier Types' Supplier Type + // (Supplier / Item are not mandatory!!) + // Set parent supplier type for tree view + + $.each(wn.report_dump.data["Supplier Type"], function(i, v) { + v['parent_supplier_type'] = "All Supplier Types" + }) + + wn.report_dump.data["Supplier Type"] = [{ + name: "All Supplier Types", + id: "All Supplier Types", + }].concat(wn.report_dump.data["Supplier Type"]); + + wn.report_dump.data["Supplier"].push({ + name: "Not Set", + parent_supplier_type: "All Supplier Types", + id: "Not Set", + }); + + wn.report_dump.data["Item"].push({ + name: "Not Set", + parent_item_group: "All Item Groups", + id: "Not Set", + }); + } + + if(!this.data || me.item_type != me.tree_type) { + if(me.tree_type=='Supplier') { + var items = wn.report_dump.data["Supplier"]; + } if(me.tree_type=='Supplier Type') { + var items = this.prepare_tree("Supplier", "Supplier Type"); + } else if(me.tree_type=="Item Group") { + var items = this.prepare_tree("Item", "Item Group"); + } else if(me.tree_type=="Item") { + var items = wn.report_dump.data["Item"]; + } + + me.item_type = me.tree_type + me.parent_map = {}; + me.item_by_name = {}; + me.data = []; + + $.each(items, function(i, v) { + var d = copy_dict(v); + + me.data.push(d); + me.item_by_name[d.name] = d; + if(d[me.tree_grid.parent_field]) { + me.parent_map[d.name] = d[me.tree_grid.parent_field]; + } + me.reset_item_values(d); + }); + + this.set_indent(); + + } else { + // otherwise, only reset values + $.each(this.data, function(i, d) { + me.reset_item_values(d); + }); + } + + this.prepare_balances(); + if(me.tree_grid.show) { + this.set_totals(false); + this.update_groups(); + } else { + this.set_totals(true); + } + }, + prepare_balances: function() { + var me = this; + var from_date = dateutil.str_to_obj(this.from_date); + var to_date = dateutil.str_to_obj(this.to_date); + var is_val = this.value_or_qty == 'Value'; + + $.each(this.tl, function(i, tl) { + if (me.is_default('company') ? true : me.apply_filter(tl, "company")) { + var posting_date = dateutil.str_to_obj(tl.posting_date); + if (posting_date >= from_date && posting_date <= to_date) { + var item = me.item_by_name[tl[me.tree_grid.item_key]] || me.item_by_name['Not Set']; + item[me.column_map[tl.posting_date].field] += (is_val ? tl.amount : tl.qty); + } + } + }); + }, + update_groups: function() { + var me = this; + + $.each(this.data, function(i, item) { + var parent = me.parent_map[item.name]; + while(parent) { + parent_group = me.item_by_name[parent]; + + $.each(me.columns, function(c, col) { + if (col.formatter == me.currency_formatter) { + parent_group[col.field] = + flt(parent_group[col.field]) + + flt(item[col.field]); + } + }); + parent = me.parent_map[parent]; + } + }); + }, + set_totals: function(sort) { + var me = this; + var checked = false; + $.each(this.data, function(i, d) { + d.total = 0.0; + $.each(me.columns, function(i, col) { + if(col.formatter==me.currency_formatter && !col.hidden && col.field!="total") + d.total += d[col.field]; + if(d.checked) checked = true; + }) + }); + + if(sort)this.data = this.data.sort(function(a, b) { return b.total - a.total; }); + + if(!this.checked) { + this.data[0].checked = true; + } + }, + get_plot_points: function(item, col, idx) { + return [[dateutil.str_to_obj(col.id).getTime(), item[col.field]], + [dateutil.user_to_obj(col.name).getTime(), item[col.field]]]; + } +}); \ No newline at end of file diff --git a/erpnext/buying/page/purchase_analytics/purchase_analytics.txt b/erpnext/buying/page/purchase_analytics/purchase_analytics.txt new file mode 100644 index 0000000000..71d1c9b644 --- /dev/null +++ b/erpnext/buying/page/purchase_analytics/purchase_analytics.txt @@ -0,0 +1,28 @@ +# Page, purchase-analytics +[ + + # These values are common in all dictionaries + { + u'creation': '2012-09-21 15:21:10', + u'docstatus': 0, + u'modified': '2012-09-21 15:21:10', + u'modified_by': u'Administrator', + u'owner': u'Administrator' + }, + + # These values are common for all Page + { + u'doctype': u'Page', + 'module': u'Buying', + u'name': u'__common__', + 'page_name': u'purchase-analytics', + 'standard': u'Yes', + 'title': u'Purchase Analytics' + }, + + # Page, purchase-analytics + { + u'doctype': u'Page', + u'name': u'purchase-analytics' + } +] \ No newline at end of file diff --git a/erpnext/patches/september_2012/plot_patch.py b/erpnext/patches/september_2012/plot_patch.py index fda0a27b8d..822194dcfd 100644 --- a/erpnext/patches/september_2012/plot_patch.py +++ b/erpnext/patches/september_2012/plot_patch.py @@ -3,6 +3,7 @@ def execute(): set_master_name_in_accounts() set_customer_in_sales_invoices() reset_lft_rgt() + add_analytics_role() def set_master_name_in_accounts(): accounts = webnotes.conn.sql("""select name, account_name, master_type from tabAccount @@ -29,4 +30,11 @@ def reset_lft_rgt(): rebuild_tree("Account", "parent_account") rebuild_tree("Cost Center", "parent_cost_center") rebuild_tree("Sales Person", "parent_sales_person") - \ No newline at end of file + +def add_analytics_role(): + from webnotes.model.doc import Document + Document("Role", fielddata={ + "name": "Analytics", + "role_name": "Analytics", + "module": "Setup", + }).save(1); \ No newline at end of file diff --git a/erpnext/selling/page/sales_analytics/sales_analytics.js b/erpnext/selling/page/sales_analytics/sales_analytics.js index 8a0d9b26d6..86ab0ebd09 100644 --- a/erpnext/selling/page/sales_analytics/sales_analytics.js +++ b/erpnext/selling/page/sales_analytics/sales_analytics.js @@ -1,3 +1,19 @@ +// ERPNext - web based ERP (http://erpnext.com) +// Copyright (C) 2012 Web Notes Technologies Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + wn.pages['sales-analytics'].onload = function(wrapper) { wn.ui.make_app_page({ parent: wrapper, @@ -7,7 +23,7 @@ wn.pages['sales-analytics'].onload = function(wrapper) { new erpnext.SalesAnalytics(wrapper); } -erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({ +erpnext.SalesAnalytics = wn.views.TreeGridReport.extend({ init: function(wrapper) { this._super({ title: "Sales Analytics", @@ -76,7 +92,7 @@ erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({ {id: "check", name: "Plot", field: "check", width: 30, formatter: this.check_formatter}, {id: "name", name: this.tree_grid.label, field: "name", width: 300, - formatter: this.tree_formatter, doctype: "Customer"}, + formatter: this.tree_formatter}, {id: "total", name: "Total", field: "total", plot: false, formatter: this.currency_formatter} ]; @@ -123,7 +139,7 @@ erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({ prepare_data: function() { var me = this; if (!this.tl) { - this.make_tl("Sales Invoice", "Sales Invoice Item"); + this.make_transaction_list("Sales Invoice", "Sales Invoice Item"); // add 'Not Set' Customer & Item // (Customer / Item are not mandatory!!) @@ -188,20 +204,6 @@ erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({ } }, - make_tl: function(parent_doctype, doctype) { - var me = this; - var tmap = {}; - $.each(wn.report_dump.data[doctype], function(i, v) { - if(!tmap[v.parent]) tmap[v.parent] = []; - tmap[v.parent].push(v); - }); - this.tl = []; - $.each(wn.report_dump.data[parent_doctype], function(i, parent) { - $.each(tmap[parent.name], function(i, d) { - me.tl.push($.extend(copy_dict(parent), d)); - }); - }); - }, prepare_balances: function() { var me = this; var from_date = dateutil.str_to_obj(this.from_date); diff --git a/erpnext/selling/page/selling_home/selling_home.html b/erpnext/selling/page/selling_home/selling_home.html index 164d3653e5..1917b01213 100644 --- a/erpnext/selling/page/selling_home/selling_home.html +++ b/erpnext/selling/page/selling_home/selling_home.html @@ -26,6 +26,11 @@

Address

Address Master

+
+

Sales Analytics + beta +

+

Sales trends based on Sales Invoice


diff --git a/erpnext/setup/__init__.py b/erpnext/setup/__init__.py index dfc93b57a7..749205251f 100644 --- a/erpnext/setup/__init__.py +++ b/erpnext/setup/__init__.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals install_docs = [ - {'doctype':'Role', 'role_name': 'System Manager', 'name': 'System Manager'} + {'doctype':'Role', 'role_name': 'System Manager', 'name': 'System Manager'}, + {'doctype':'Role', 'role_name': 'Analytics', 'name': 'Analytics'} ] diff --git a/erpnext/startup/js/modules.js b/erpnext/startup/js/modules.js index a3e7ee8ee8..71d012e44b 100644 --- a/erpnext/startup/js/modules.js +++ b/erpnext/startup/js/modules.js @@ -55,7 +55,17 @@ erpnext.module_page.hide_links = function(wrapper) { var txt = $(this).text(); $(this).parent().css('color', '#999').html(txt); } - });} + }); + + // pages + $(wrapper).find('[data-role]').each(function() { + if(!has_common(user_roles, [$(this).attr("data-role"), "System Manager"])) { + var html = $(this).html(); + $(this).parent().css('color', '#999'); + $(this).replaceWith(html); + } + }); +} // make list of reports diff --git a/erpnext/startup/report_data_map.py b/erpnext/startup/report_data_map.py index 0de1e736a1..b2be566565 100644 --- a/erpnext/startup/report_data_map.py +++ b/erpnext/startup/report_data_map.py @@ -36,8 +36,7 @@ data_map = { "order_by": "lft" }, "Cost Center": { - "columns": ["name", "parent_cost_center", "lft", "rgt", "debit_or_credit", - "company"], + "columns": ["name", "lft", "rgt"], "order_by": "lft" }, "GL Entry": { @@ -47,14 +46,15 @@ data_map = { "order_by": "posting_date, account", "links": { "account": ["Account", "name"], - "company": ["Company", "name"] + "company": ["Company", "name"], + "cost_center": ["Cost Center", "name"] } }, # Stock "Item": { "columns": ["name", "if(item_name=name, '', item_name) as item_name", - "item_group as parent_item_group", "stock_uom", "brand"], + "item_group as parent_item_group", "stock_uom", "brand", "valuation_method"], "order_by": "name", "links": { "parent_item_group": ["Item Group", "name"], @@ -115,5 +115,36 @@ data_map = { "parent": ["Sales Invoice", "name"], "item_code": ["Item", "name"] } + }, + "Supplier": { + "columns": ["name", "if(supplier_name=name, '', supplier_name) as supplier_name", + "supplier_type as parent_supplier_type"], + "order_by": "name", + "links": { + "parent_supplier_type": ["Supplier Type", "name"], + } + }, + "Supplier Type": { + "columns": ["name"], + "order_by": "name" + }, + "Purchase Invoice": { + "columns": ["name", "supplier", "posting_date", "company"], + "conditions": ["docstatus=1"], + "order_by": "posting_date", + "links": { + "supplier": ["Supplier", "name"], + "company":["Company", "name"] + } + }, + "Purchase Invoice Item": { + "columns": ["parent", "item_code", "qty", "amount"], + "conditions": ["docstatus=1", "ifnull(parent, '')!=''"], + "order_by": "parent", + "links": { + "parent": ["Purchase Invoice", "name"], + "item_code": ["Item", "name"] + } } + } diff --git a/erpnext/stock/page/stock_ageing/__init__.py b/erpnext/stock/page/stock_ageing/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/stock/page/stock_ageing/stock_ageing.js b/erpnext/stock/page/stock_ageing/stock_ageing.js new file mode 100644 index 0000000000..39c2613f1d --- /dev/null +++ b/erpnext/stock/page/stock_ageing/stock_ageing.js @@ -0,0 +1,168 @@ +// ERPNext - web based ERP (http://erpnext.com) +// Copyright (C) 2012 Web Notes Technologies Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + + +wn.pages['stock-ageing'].onload = function(wrapper) { + wn.ui.make_app_page({ + parent: wrapper, + title: 'Stock Ageing', + single_column: true + }); + + new erpnext.StockAgeing(wrapper); + +} + +wn.require("js/app/stock_grid_report.js"); + +erpnext.StockAgeing = erpnext.StockGridReport.extend({ + init: function(wrapper) { + this._super({ + title: "Stock Ageing", + page: wrapper, + parent: $(wrapper).find('.layout-main'), + appframe: wrapper.appframe, + doctypes: ["Item", "Warehouse", "Stock Ledger Entry", "Item Group"], + }) + }, + setup_columns: function() { + this.columns = [ + {id: "name", name: "Item", field: "name", width: 300, + formatter: this.link_formatter}, + {id: "average_age", name: "Average Age", field: "average_age", + formatter: this.currency_formatter}, + {id: "earliest", name: "Earliest", field: "earliest", + formatter: this.currency_formatter}, + {id: "latest", name: "Latest", field: "latest", + formatter: this.currency_formatter} + ]; + }, + filters: [ + {fieldtype:"Select", label: "Warehouse", link:"Warehouse", + default_value: "Select Warehouse..."}, + {fieldtype:"Select", label: "Plot By", + options: ["Average Age", "Earliest", "Latest"]}, + {fieldtype:"Date", label: "To Date"}, + {fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"}, + {fieldtype:"Button", label: "Reset Filters"} + ], + setup_filters: function() { + var me = this; + this._super(); + + this.filter_inputs.warehouse.change(function() { + me.filter_inputs.refresh.click(); + }); + this.filter_inputs.plot_by.change(function() { + me.filter_inputs.refresh.click(); + }); + }, + init_filter_values: function() { + this._super(); + this.filter_inputs.to_date.val(dateutil.obj_to_user(new Date())); + }, + prepare_data: function() { + var me = this; + + if(!this.data) { + me.data = wn.report_dump.data["Item"]; + me.item_by_name = me.make_name_map(me.data); + } + + $.each(this.data, function(i, d) { + me.reset_item_values(d); + }); + + this.prepare_balances(); + }, + prepare_balances: function() { + var me = this; + var to_date = dateutil.str_to_obj(this.to_date); + var data = wn.report_dump.data["Stock Ledger Entry"]; + + this.item_warehouse = {}; + + for(var i=0, j=data.length; i to_date) + break; + } + } + + $.each(me.data, function(i, item) { + var full_fifo_stack = []; + if(me.is_default("warehouse")) { + $.each(me.item_warehouse[item.name] || {}, function(i, wh) { + full_fifo_stack = full_fifo_stack.concat(wh.fifo_stack || []) + }); + } else { + full_fifo_stack = me.get_item_warehouse(me.warehouse, item.name) || []; + } + + var age_qty = total_qty = 0.0; + var min_age = max_age = null; + + $.each(full_fifo_stack, function(i, batch) { + var batch_age = dateutil.get_diff(me.to_date, batch[2]); + age_qty += batch_age * batch[1]; + total_qty += batch[1]; + max_age = Math.max(max_age, batch_age); + if(min_age===null) min_age=batch_age; else min_age = Math.min(min_age, batch_age) + }); + + item.average_age = total_qty.toFixed(2)==0.0 ? 0 : (age_qty / total_qty).toFixed(2); + item.earliest = max_age || 0.0; + item.latest = min_age || 0.0; + }); + + this.data = this.data.sort(function(a, b) { + var sort_by = me.plot_by.replace(" ", "_").toLowerCase(); + return b[sort_by] - a[sort_by]; + }); + }, + get_plot_data: function() { + var data = []; + var me = this; + + data.push({ + label: me.plot_by, + data: $.map(me.data, function(item, idx) { + return [[idx+1, item[me.plot_by.replace(" ", "_").toLowerCase() ]]] + }), + bars: {show: true}, + }); + + return data.length ? data : false; + }, + get_plot_options: function() { + var me = this; + return { + grid: { hoverable: true, clickable: true }, + xaxis: { + ticks: $.map(me.data, function(item, idx) { return [[idx+1, item.name]] }), + max: 20 + } + } + } +}); \ No newline at end of file diff --git a/erpnext/stock/page/stock_ageing/stock_ageing.txt b/erpnext/stock/page/stock_ageing/stock_ageing.txt new file mode 100644 index 0000000000..bda41e5f7a --- /dev/null +++ b/erpnext/stock/page/stock_ageing/stock_ageing.txt @@ -0,0 +1,28 @@ +# Page, stock-ageing +[ + + # These values are common in all dictionaries + { + u'creation': '2012-09-21 18:21:31', + u'docstatus': 0, + u'modified': '2012-09-21 18:21:31', + u'modified_by': u'Administrator', + u'owner': u'Administrator' + }, + + # These values are common for all Page + { + u'doctype': u'Page', + 'module': u'Stock', + u'name': u'__common__', + 'page_name': u'stock-ageing', + 'standard': u'Yes', + 'title': u'Stock Ageing' + }, + + # Page, stock-ageing + { + u'doctype': u'Page', + u'name': u'stock-ageing' + } +] \ No newline at end of file diff --git a/erpnext/stock/page/stock_analytics/stock_analytics.js b/erpnext/stock/page/stock_analytics/stock_analytics.js index 42b60b2ef7..1989043888 100644 --- a/erpnext/stock/page/stock_analytics/stock_analytics.js +++ b/erpnext/stock/page/stock_analytics/stock_analytics.js @@ -1,3 +1,19 @@ +// ERPNext - web based ERP (http://erpnext.com) +// Copyright (C) 2012 Web Notes Technologies Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + wn.pages['stock-analytics'].onload = function(wrapper) { wn.ui.make_app_page({ parent: wrapper, @@ -8,7 +24,9 @@ wn.pages['stock-analytics'].onload = function(wrapper) { new erpnext.StockAnalytics(wrapper); } -erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ +wn.require("js/app/stock_grid_report.js"); + +erpnext.StockAnalytics = erpnext.StockGridReport.extend({ init: function(wrapper) { this._super({ title: "Stock Analytics", @@ -33,7 +51,7 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ {id: "check", name: "Plot", field: "check", width: 30, formatter: this.check_formatter}, {id: "name", name: "Item", field: "name", width: 300, - formatter: this.tree_formatter, doctype: "Item"}, + formatter: this.tree_formatter}, {id: "opening", name: "Opening", field: "opening", hidden: true, formatter: this.currency_formatter} ]; @@ -42,7 +60,8 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ this.columns = std_columns.concat(this.columns); }, filters: [ - {fieldtype:"Select", label: "Value or Qty", options:["Value (Weighted Average)", "Value (FIFO)", "Quantity"], + {fieldtype:"Select", label: "Value or Qty", options:["Value (Weighted Average)", + "Value (FIFO)", "Quantity"], filter: function(val, item, opts, me) { return me.apply_zero_filter(val, item, opts, me); }}, @@ -123,15 +142,8 @@ erpnext.StockAnalytics = wn.views.GridReportWithPlot.extend({ var to_date = dateutil.str_to_obj(this.to_date); var data = wn.report_dump.data["Stock Ledger Entry"]; - var warehouse_item = {}; - var get_warehouse_item = function(warehouse, item) { - if(!warehouse_item[warehouse]) warehouse_item[warehouse] = {}; - if(!warehouse_item[warehouse][item]) warehouse_item[warehouse][item] = { - balance_qty: 0.0, balance_value: 0.0, fifo_stack: [] - }; - return warehouse_item[warehouse][item]; - } - + this.item_warehouse = {}; + for(var i=0, j=data.length; i 0) { - // incoming - rate is given - var rate = sl.incoming_rate; - var add_qty = sl.qty; - if(wh.balance_qty < 0) { - // negative valuation - // only add value of quantity if - // the balance goes above 0 - add_qty = wh.balance_qty + sl.qty; - if(add_qty < 0) { - add_qty = 0; - } - } - var value_diff = (rate * add_qty); - - if(add_qty) - wh.fifo_stack.push([add_qty, sl.incoming_rate]); - } else { - // outgoing - - if(is_fifo) { - var value_diff = this.get_fifo_value_diff(wh, sl); - } else { - // average rate for weighted average - var rate = (wh.balance_qty.toFixed(2) == 0.00 ? 0 : - flt(wh.balance_value) / flt(wh.balance_qty)); - - // no change in value if negative qty - if((wh.balance_qty + sl.qty).toFixed(2) >= 0.00) - var value_diff = (rate * sl.qty); - else - var value_diff = -wh.balance_value; - } - } - - // update balance (only needed in case of valuation) - wh.balance_qty += sl.qty; - wh.balance_value += value_diff; - - if(sl.item_code=="0.5Motor") { - console.log([sl.voucher_no, sl.qty, sl.warehouse, value_diff]); - console.log(wh.fifo_stack); - } - - return value_diff; - }, - get_fifo_value_diff: function(wh, sl) { - // get exact rate from fifo stack - var fifo_stack = wh.fifo_stack.reverse(); - var fifo_value_diff = 0.0; - var qty = -sl.qty; - - for(var i=0, j=fifo_stack.length; i= qty) { - batch[0] = batch[0] - qty; - fifo_value_diff += (qty * batch[1]); - - qty = 0.0; - if(batch[0]) { - // batch still has qty put it back - fifo_stack.push(batch); - } - - // all qty found - break; - } else { - // consume this batch fully - fifo_value_diff += (batch[0] * batch[1]); - qty = qty - batch[0]; - } - } - if(qty) { - // msgprint("Negative values not allowed for FIFO valuation!\ - // Item " + sl.item_code.bold() + " on " + dateutil.str_to_user(sl.posting_datetime).bold() + - // " becomes negative. Values computed will not be accurate."); - } - - // reset the updated stack - wh.fifo_stack = fifo_stack.reverse(); - return -fifo_value_diff; - }, update_groups: function() { var me = this; diff --git a/erpnext/stock/page/stock_home/stock_home.html b/erpnext/stock/page/stock_home/stock_home.html index 0939a77681..b3a2848363 100644 --- a/erpnext/stock/page/stock_home/stock_home.html +++ b/erpnext/stock/page/stock_home/stock_home.html @@ -23,6 +23,21 @@

Warehouse

Warehouse is where items are stored

+
+

Stock Ledger + beta +

+

Log of stock movements

+
+

Stock Analytics + beta +

+

Visual representation of stock trends

+
+

Stock Ageing + beta +

+

Analysis of slow moving stock


diff --git a/erpnext/stock/page/stock_ledger/stock_ledger.js b/erpnext/stock/page/stock_ledger/stock_ledger.js index deb3743102..f3b7a66ea8 100644 --- a/erpnext/stock/page/stock_ledger/stock_ledger.js +++ b/erpnext/stock/page/stock_ledger/stock_ledger.js @@ -1,3 +1,19 @@ +// ERPNext - web based ERP (http://erpnext.com) +// Copyright (C) 2012 Web Notes Technologies Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + wn.pages['stock-ledger'].onload = function(wrapper) { wn.ui.make_app_page({ parent: wrapper, @@ -8,7 +24,9 @@ wn.pages['stock-ledger'].onload = function(wrapper) { new erpnext.StockLedger(wrapper); } -erpnext.StockLedger = wn.views.GridReportWithPlot.extend({ +wn.require("js/app/stock_grid_report.js"); + +erpnext.StockLedger = erpnext.StockGridReport.extend({ init: function(wrapper) { this._super({ title: "Stock Ledger", @@ -28,15 +46,16 @@ erpnext.StockLedger = wn.views.GridReportWithPlot.extend({ link_formatter: { filter_input: "item_code", open_btn: true, - doctype: '"Item"' }}, {id: "warehouse", name: "Warehouse", field: "warehouse", width: 100, link_formatter: {filter_input: "warehouse"}}, {id: "qty", name: "Qty", field: "qty", width: 100, formatter: this.currency_formatter}, - {id: "balance", name: "Balance", field: "balance", width: 100, + {id: "balance", name: "Balance Qty", field: "balance", width: 100, formatter: this.currency_formatter, hidden: this.hide_balance}, + {id: "balance_value", name: "Balance Value", field: "balance_value", width: 100, + formatter: this.currency_formatter, hidden: this.hide_balance}, {id: "voucher_type", name: "Voucher Type", field: "voucher_type", width: 120}, {id: "voucher_no", name: "Voucher No", field: "voucher_no", width: 160, link_formatter: { @@ -86,36 +105,53 @@ erpnext.StockLedger = wn.views.GridReportWithPlot.extend({ var opening = { item_code: "On " + dateutil.str_to_user(this.from_date), qty: 0.0, balance: 0.0, - id:"_opening", _show: true, _style: "font-weight: bold" + id:"_opening", _show: true, _style: "font-weight: bold", balance_value: 0.0 } var total_in = { - item_code: "Total In", qty: 0.0, balance: 0.0, + item_code: "Total In", qty: 0.0, balance: 0.0, balance_value: 0.0, id:"_total_in", _show: true, _style: "font-weight: bold" } var total_out = { - item_code: "Total Out", qty: 0.0, balance: 0.0, + item_code: "Total Out", qty: 0.0, balance: 0.0, balance_value: 0.0, id:"_total_out", _show: true, _style: "font-weight: bold" } // clear balance - $.each(wn.report_dump.data["Item"], function(i, item) { item.balance = 0.0; }); + $.each(wn.report_dump.data["Item"], function(i, item) { + item.balance = item.balance_value = 0.0; + }); + + // initialize warehouse-item map + this.item_warehouse = {}; // for(var i=0, j=data.length; i 0) total_in.qty += sl.qty; - else total_out.qty += (-1 * sl.qty); + if(sl.qty > 0) { + total_in.qty += sl.qty; + total_in.balance_value += value_diff; + } else { + total_out.qty += (-1 * sl.qty); + total_out.balance_value += value_diff; + } } } @@ -129,7 +165,10 @@ erpnext.StockLedger = wn.views.GridReportWithPlot.extend({ // update balance if((!me.is_default("warehouse") ? me.apply_filter(sl, "warehouse") : true)) { sl.balance = me.item_by_name[sl.item_code].balance + sl.qty; - me.item_by_name[sl.item_code].balance = sl.balance; + me.item_by_name[sl.item_code].balance = sl.balance; + + sl.balance_value = me.item_by_name[sl.item_code].balance_value + value_diff; + me.item_by_name[sl.item_code].balance_value = sl.balance_value; } } @@ -137,8 +176,10 @@ erpnext.StockLedger = wn.views.GridReportWithPlot.extend({ var closing = { item_code: "On " + dateutil.str_to_user(this.to_date), balance: (out ? out[out.length-1].balance : 0), qty: 0, + balance_value: (out ? out[out.length-1].balance_value : 0), id:"_closing", _show: true, _style: "font-weight: bold" }; + total_out.balance_value = -total_out.balance_value; var out = [opening].concat(out).concat([total_in, total_out, closing]); } diff --git a/public/js/all-app.js b/public/js/all-app.js index d6c99a0ddd..342b51d237 100644 --- a/public/js/all-app.js +++ b/public/js/all-app.js @@ -1076,9 +1076,8 @@ me.list.run();});this.dialog.show();},add_column:function(c){var w=$('
').appendTo(this.parent);if(this.filters){this.make_filters();} -this.make_waiting();this.import_slickgrid();var me=this;this.get_data();},bind_show:function(){var me=this;$(this.page).bind('show',function(){wn.cur_grid_report=me;me.apply_filters_from_route();me.refresh();});},get_data:function(){var me=this;wn.report_dump.with_data(this.doctypes,function(){me.setup_filters();me.init_filter_values();me.refresh();},this.wrapper.find(".progress .bar"));},setup_filters:function(){var me=this;$.each(me.filter_inputs,function(i,v){var opts=v.get(0).opts;if(opts.fieldtype=="Select"&&inList(me.doctypes,opts.link)){$(v).add_options($.map(wn.report_dump.data[opts.link],function(d){return d.name;}));}});this.filter_inputs.refresh&&this.filter_inputs.refresh.click(function(){me.set_route();});this.filter_inputs.reset_filters&&this.filter_inputs.reset_filters.click(function(){me.init_filter_values();me.set_route();});},init_filter_values:function(){var me=this;$.each(this.filter_inputs,function(key,filter){var opts=filter.get(0).opts;if(sys_defaults[key]){filter.val(sys_defaults[key]);}else if(opts.fieldtype=='Select'){filter.get(0).selectedIndex=0;}else if(opts.fieldtype=='Data'){filter.val("");}}) +if(missing.length){wn.call({method:"webnotes.widgets.report_dump.get_data",args:{doctypes:doctypes,missing:missing},callback:function(r){$.each(r.message,function(doctype,doctype_data){var data=[];$.each(doctype_data.data,function(i,d){var row={};$.each(doctype_data.columns,function(idx,col){row[col]=d[idx];});row.id=row.name||doctype+"-"+i;row.doctype=doctype;data.push(row);});wn.report_dump.data[doctype]=data;});$.each(r.message,function(doctype,doctype_data){if(doctype_data.links){$.each(wn.report_dump.data[doctype],function(row_idx,row){$.each(doctype_data.links,function(link_key,link){if(wn.report_dump.data[link[0]][row[link_key]]){row[link_key]=wn.report_dump.data[link[0]][row[link_key]][link[1]];}else{row[link_key]=null;}})})}});callback();},progress_bar:progress_bar})}else{callback();}}});wn.provide("wn.views");wn.views.GridReport=Class.extend({init:function(opts){this.filter_inputs={};this.preset_checks=[];this.tree_grid={show:false};$.extend(this,opts);this.wrapper=$('
').appendTo(this.parent);if(this.filters){this.make_filters();} +this.make_waiting();this.import_slickgrid();var me=this;this.get_data();},bind_show:function(){var me=this;$(this.page).bind('show',function(){wn.cur_grid_report=me;me.apply_filters_from_route();me.refresh();});},get_data:function(){var me=this;wn.report_dump.with_data(this.doctypes,function(){me.setup_filters();me.init_filter_values();me.refresh();},this.wrapper.find(".progress .bar"));},setup_filters:function(){var me=this;$.each(me.filter_inputs,function(i,v){var opts=v.get(0).opts;if(opts.fieldtype=="Select"&&inList(me.doctypes,opts.link)){$(v).add_options($.map(wn.report_dump.data[opts.link],function(d){return d.name;}));}});this.filter_inputs.refresh&&this.filter_inputs.refresh.click(function(){me.set_route();});this.filter_inputs.reset_filters&&this.filter_inputs.reset_filters.click(function(){me.init_filter_values();me.set_route();});this.filter_inputs.range&&this.filter_inputs.range.change(function(){me.set_route();});},init_filter_values:function(){var me=this;$.each(this.filter_inputs,function(key,filter){var opts=filter.get(0).opts;if(sys_defaults[key]){filter.val(sys_defaults[key]);}else if(opts.fieldtype=='Select'){filter.get(0).selectedIndex=0;}else if(opts.fieldtype=='Data'){filter.val("");}}) if(this.filter_inputs.from_date) this.filter_inputs.from_date.val(dateutil.str_to_user(sys_defaults.year_start_date));if(this.filter_inputs.to_date) this.filter_inputs.to_date.val(dateutil.str_to_user(sys_defaults.year_end_date));},make_filters:function(){var me=this;$.each(this.filters,function(i,v){v.fieldname=v.fieldname||v.label.replace(/ /g,'_').toLowerCase();var input=null;if(v.fieldtype=='Select'){input=me.appframe.add_select(v.label,v.options||[v.default_value]);}else if(v.fieldtype=='Button'){input=me.appframe.add_button(v.label);if(v.icon){$('').prependTo(input);}}else if(v.fieldtype=='Date'){input=me.appframe.add_date(v.label);}else if(v.fieldtype=='Label'){input=me.appframe.add_label(v.label);}else if(v.fieldtype=='Data'){input=me.appframe.add_data(v.label);} @@ -1096,19 +1095,12 @@ this.make();this.show_zero=$('.show-zero input:checked').length;this.load_filter Print \ | \ Export \ -
').appendTo(this.wrapper);this.wrapper.find(".grid-report-export").click(function(){return me.export();});this.grid_wrapper=$("
").appendTo(this.wrapper);this.id=wn.dom.set_unique_id(this.grid_wrapper.get(0));$('').appendTo(this.wrapper);this.bind_show();wn.cur_grid_report=this;this.apply_filters_from_route();$(this.wrapper).trigger('make');},apply_filters_from_route:function(){var hash=decodeURIComponent(window.location.hash);var me=this;if(hash.indexOf('/')!=-1){$.each(hash.split('/').splice(1).join('/').split('&'),function(i,f){var f=f.split("=");if(me.filter_inputs[f[0]]){me.filter_inputs[f[0]].val(decodeURIComponent(f[1]));}else{console.log("Invalid filter: "+f[0]);}});}else{this.init_filter_values();}},set_route:function(){wn.set_route(wn.container.page.page_name,$.map(this.filter_inputs,function(v){var val=v.val();var opts=v.get(0).opts;if(val&&val!=opts.default_value) return encodeURIComponent(opts.fieldname) -+'='+encodeURIComponent(val);}).join('&'))},options:{editable:false,enableColumnReorder:false},render:function(){this.grid=new Slick.Grid("#"+this.id,this.dataView,this.dataview_columns,this.options);var me=this;this.dataView.onRowsChanged.subscribe(function(e,args){me.grid.invalidateRows(args.rows);me.grid.render();});this.dataView.onRowCountChanged.subscribe(function(e,args){me.grid.updateRowCount();me.grid.render();});this.tree_grid.show&&this.add_tree_grid_events();},prepare_data_view:function(){this.dataView=new Slick.Data.DataView({inlineFilters:true});this.dataView.beginUpdate();this.dataView.setItems(this.data);if(this.dataview_filter)this.dataView.setFilter(this.dataview_filter);if(this.tree_grid.show)this.dataView.setFilter(this.tree_dataview_filter);this.dataView.endUpdate();},export:function(){var me=this;var res=[$.map(this.columns,function(v){return v.name;})].concat(this.get_view_data());wn.require("js/lib/downloadify/downloadify.min.js");wn.require("js/lib/downloadify/swfobject.js");var id=wn.dom.set_unique_id();var msgobj=msgprint('

You must have Flash 10 installed to download this file.

');Downloadify.create(id,{filename:function(){return me.title+'.csv';},data:function(){return wn.to_csv(res);},swf:'js/lib/downloadify/downloadify.swf',downloadImage:'js/lib/downloadify/download.png',onComplete:function(){msgobj.hide();},onCancel:function(){msgobj.hide();},onError:function(){msgobj.hide();},width:100,height:30,transparent:true,append:false});return false;},add_tree_grid_events:function(){var me=this;this.grid.onClick.subscribe(function(e,args){if($(e.target).hasClass("toggle")){var item=me.dataView.getItem(args.row);if(item){if(!item._collapsed){item._collapsed=true;}else{item._collapsed=false;} -me.dataView.updateItem(item.id,item);} -e.stopImmediatePropagation();}});},tree_formatter:function(row,cell,value,columnDef,dataContext){var me=wn.cur_grid_report;value=value.replace(/&/g,"&").replace(//g,">");var data=me.data;var spacer="";var idx=me.dataView.getIdxById(dataContext.id);var link=me.tree_grid.formatter(dataContext);if(columnDef.doctype){link+=me.get_link_open_icon(columnDef.doctype,value);} -if(data[idx+1]&&data[idx+1].indent>data[idx].indent){if(dataContext._collapsed){return spacer+"  "+link;}else{return spacer+"  "+link;}}else{return spacer+"  "+link;}},tree_dataview_filter:function(item){var me=wn.cur_grid_report;if(!me.apply_filters(item))return false;var parent=item[me.tree_grid.parent_field];while(parent){if(me.item_by_name[parent]._collapsed){return false;} -parent=me.parent_map[parent];} -return true;},prepare_tree:function(item_dt,group_dt){var group_data=wn.report_dump.data[group_dt];var item_data=wn.report_dump.data[item_dt];var me=this;var item_group_map={};var group_ids=$.map(group_data,function(v){return v.id;});$.each(item_data,function(i,item){var parent=item[me.tree_grid.parent_field];if(!item_group_map[parent])item_group_map[parent]=[];if(group_ids.indexOf(item.name)==-1){item_group_map[parent].push(item);}else{msgprint("Ignoring Item "+item.name.bold()+", because a group exists with the same name!");}});var items=[];$.each(group_data,function(i,group){group.is_group=true;items.push(group);items=items.concat(item_group_map[group.name]||[]);});return items;},set_indent:function(){var me=this;$.each(this.data,function(i,d){var indent=0;var parent=me.parent_map[d.name];if(parent){while(parent){indent++;parent=me.parent_map[parent];}} -d.indent=indent;});},apply_filters:function(item){var filters=this.filter_inputs;if(item._show)return true;for(i in filters){if(!this.apply_filter(item,i))return false;} ++'='+encodeURIComponent(val);}).join('&'))},options:{editable:false,enableColumnReorder:false},render:function(){this.grid=new Slick.Grid("#"+this.id,this.dataView,this.dataview_columns,this.options);var me=this;this.dataView.onRowsChanged.subscribe(function(e,args){me.grid.invalidateRows(args.rows);me.grid.render();});this.dataView.onRowCountChanged.subscribe(function(e,args){me.grid.updateRowCount();me.grid.render();});this.tree_grid.show&&this.add_tree_grid_events();},prepare_data_view:function(){this.dataView=new Slick.Data.DataView({inlineFilters:true});this.dataView.beginUpdate();this.dataView.setItems(this.data);if(this.dataview_filter)this.dataView.setFilter(this.dataview_filter);if(this.tree_grid.show)this.dataView.setFilter(this.tree_dataview_filter);this.dataView.endUpdate();},export:function(){var me=this;var res=[$.map(this.columns,function(v){return v.name;})].concat(this.get_view_data());wn.require("js/lib/downloadify/downloadify.min.js");wn.require("js/lib/downloadify/swfobject.js");var id=wn.dom.set_unique_id();var msgobj=msgprint('

You must have Flash 10 installed to download this file.

');Downloadify.create(id,{filename:function(){return me.title+'.csv';},data:function(){return wn.to_csv(res);},swf:'js/lib/downloadify/downloadify.swf',downloadImage:'js/lib/downloadify/download.png',onComplete:function(){msgobj.hide();},onCancel:function(){msgobj.hide();},onError:function(){msgobj.hide();},width:100,height:30,transparent:true,append:false});return false;},apply_filters:function(item){var filters=this.filter_inputs;if(item._show)return true;for(i in filters){if(!this.apply_filter(item,i))return false;} return true;},apply_filter:function(item,fieldname){var filter=this.filter_inputs[fieldname].get(0);if(filter.opts.filter){if(!filter.opts.filter(this[filter.opts.fieldname],item,filter.opts,this)){return false;}} return true;},apply_zero_filter:function(val,item,opts,me){if(!me.show_zero){for(var i=0,j=me.columns.length;i0.001||flt(item[col.field])<-0.001){return true;}}} return false;} @@ -1118,20 +1110,27 @@ var link_formatter=me.dataview_columns[cell].link_formatter;var html=repl('\ %(value)s',{value:value,col_name:link_formatter.filter_input,page_name:wn.container.page.page_name}) -if(link_formatter.open_btn){html+=me.get_link_open_icon(eval(link_formatter.doctype),value);} +if(link_formatter.open_btn){var doctype=link_formatter.doctype?eval(link_formatter.doctype):dataContext.doctype;html+=me.get_link_open_icon(doctype,value);} return html;}}})},get_link_open_icon:function(doctype,name){return repl(' \ ',{name:name,doctype:doctype});},make_date_range_columns:function(){this.columns=[];var me=this;var range=this.filter_inputs.range.val();this.from_date=dateutil.user_to_str(this.filter_inputs.from_date.val());this.to_date=dateutil.user_to_str(this.filter_inputs.to_date.val());var date_diff=dateutil.get_diff(this.to_date,this.from_date);me.column_map={};var add_column=function(date){me.columns.push({id:date,name:dateutil.str_to_user(date),field:date,formatter:me.currency_formatter,width:100});} var build_columns=function(condition){for(var i=0;i'+contents+'
').css({position:'absolute',display:'none',top:y+5,left:x+5,border:'1px solid #fdd',padding:'2px','background-color':'#fee',opacity:0.80}).appendTo("body").fadeIn(200);} this.previousPoint=null;this.wrapper.find('.plot').bind("plothover",function(event,pos,item){if(item){if(me.previousPoint!=item.dataIndex){me.previousPoint=item.dataIndex;$("#"+me.tooltip_id).remove();showTooltip(item.pageX,item.pageY,me.get_tooltip_text(item.series.label,item.datapoint[0],item.datapoint[1]));}} else{$("#"+me.tooltip_id).remove();me.previousPoint=null;}});},get_tooltip_text:function(label,x,y){var date=dateutil.obj_to_user(new Date(x));var value=fmt_money(y);return value+" on "+date;},get_view_data:function(){var res=[];var col_map=$.map(this.columns,function(v){return v.field;});for(var i=0,len=this.dataView.getLength();i/g,">");var data=me.data;var spacer="";var idx=me.dataView.getIdxById(dataContext.id);var link=me.tree_grid.formatter(dataContext);if(dataContext.doctype){link+=me.get_link_open_icon(dataContext.doctype,value);} +if(data[idx+1]&&data[idx+1].indent>data[idx].indent){if(dataContext._collapsed){return spacer+"  "+link;}else{return spacer+"  "+link;}}else{return spacer+"  "+link;}},tree_dataview_filter:function(item){var me=wn.cur_grid_report;if(!me.apply_filters(item))return false;var parent=item[me.tree_grid.parent_field];while(parent){if(me.item_by_name[parent]._collapsed){return false;} +parent=me.parent_map[parent];} +return true;},prepare_tree:function(item_dt,group_dt){var group_data=wn.report_dump.data[group_dt];var item_data=wn.report_dump.data[item_dt];var me=this;var item_group_map={};var group_ids=$.map(group_data,function(v){return v.id;});$.each(item_data,function(i,item){var parent=item[me.tree_grid.parent_field];if(!item_group_map[parent])item_group_map[parent]=[];if(group_ids.indexOf(item.name)==-1){item_group_map[parent].push(item);}else{msgprint("Ignoring Item "+item.name.bold()+", because a group exists with the same name!");}});var items=[];$.each(group_data,function(i,group){group.is_group=true;items.push(group);items=items.concat(item_group_map[group.name]||[]);});return items;},set_indent:function(){var me=this;$.each(this.data,function(i,d){var indent=0;var parent=me.parent_map[d.name];if(parent){while(parent){indent++;parent=me.parent_map[parent];}} +d.indent=indent;});},}); /* * lib/js/legacy/widgets/dialog.js */ @@ -2340,7 +2339,7 @@ erpnext.hide_naming_series=function(){if(cur_frm.fields_dict.naming_series){hide * erpnext/startup/js/modules.js */ wn.provide('erpnext.module_page');erpnext.module_page.setup_page=function(module,wrapper){erpnext.module_page.hide_links(wrapper);erpnext.module_page.make_list(module,wrapper);$(wrapper).find("a[title]").tooltip({delay:{show:500,hide:100}});} -erpnext.module_page.hide_links=function(wrapper){$(wrapper).find('[href*="List/"]').each(function(){var href=$(this).attr('href');var dt=href.split('/')[1];if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[data-doctype]').each(function(){var dt=$(this).attr('data-doctype');if(wn.boot.profile.all_read.indexOf(dt)==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[href*="Form/"]').each(function(){var href=$(this).attr('href');var dt=href.split('/')[1];if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});} +erpnext.module_page.hide_links=function(wrapper){$(wrapper).find('[href*="List/"]').each(function(){var href=$(this).attr('href');var dt=href.split('/')[1];if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[data-doctype]').each(function(){var dt=$(this).attr('data-doctype');if(wn.boot.profile.all_read.indexOf(dt)==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[href*="Form/"]').each(function(){var href=$(this).attr('href');var dt=href.split('/')[1];if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1){var txt=$(this).text();$(this).parent().css('color','#999').html(txt);}});$(wrapper).find('[data-role]').each(function(){if(!has_common(user_roles,[$(this).attr("data-role"),"System Manager"])){var html=$(this).html();$(this).parent().css('color','#999');$(this).replaceWith(html);}});} erpnext.module_page.make_list=function(module,wrapper){var $w=$(wrapper).find('.reports-list');var $parent1=$('
').appendTo($w);var $parent2=$('
').appendTo($w);wrapper.list1=new wn.ui.Listing({parent:$parent1,method:'utilities.get_sc_list',render_row:function(row,data){if(!data.parent_doc_type)data.parent_doc_type=data.doc_type;$(row).html(repl('\ %(criteria_name)s',data))},args:{module:module},no_refresh:true,callback:function(r){erpnext.module_page.hide_links($parent1)}});wrapper.list1.run();wrapper.list2=new wn.ui.Listing({parent:$parent2,method:'utilities.get_report_list',render_row:function(row,data){$(row).html(repl('. -erpnext.AccountTreeGrid = wn.views.GridReportWithPlot.extend({ +erpnext.AccountTreeGrid = wn.views.TreeGridReport.extend({ init: function(wrapper, title) { this._super({ title: title, page: wrapper, parent: $(wrapper).find('.layout-main'), appframe: wrapper.appframe, - doctypes: ["Company", "Fiscal Year", "Account", "GL Entry"], + doctypes: ["Company", "Fiscal Year", "Account", "GL Entry", "Cost Center"], tree_grid: { show: true, parent_field: "parent_account", @@ -111,6 +111,9 @@ erpnext.AccountTreeGrid = wn.views.GridReportWithPlot.extend({ this.prepare_balances(); }, + init_account: function(d) { + this.reset_item_values(d); + }, prepare_balances: function() { var gl = wn.report_dump.data['GL Entry']; diff --git a/public/js/app/stock_grid_report.js b/public/js/app/stock_grid_report.js new file mode 100644 index 0000000000..6f86e24ae5 --- /dev/null +++ b/public/js/app/stock_grid_report.js @@ -0,0 +1,105 @@ +// ERPNext - web based ERP (http://erpnext.com) +// Copyright (C) 2012 Web Notes Technologies Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +erpnext.StockGridReport = wn.views.TreeGridReport.extend({ + get_item_warehouse: function(warehouse, item) { + if(!this.item_warehouse[item]) this.item_warehouse[item] = {}; + if(!this.item_warehouse[item][warehouse]) this.item_warehouse[item][warehouse] = { + balance_qty: 0.0, balance_value: 0.0, fifo_stack: [] + }; + return this.item_warehouse[item][warehouse]; + }, + + get_value_diff: function(wh, sl, is_fifo) { + // value + if(sl.qty > 0) { + // incoming - rate is given + var rate = sl.incoming_rate; + var add_qty = sl.qty; + if(wh.balance_qty < 0) { + // negative valuation + // only add value of quantity if + // the balance goes above 0 + add_qty = wh.balance_qty + sl.qty; + if(add_qty < 0) { + add_qty = 0; + } + } + var value_diff = (rate * add_qty); + + if(add_qty) + wh.fifo_stack.push([add_qty, sl.incoming_rate, sl.posting_date]); + } else { + // outgoing + + if(is_fifo) { + var value_diff = this.get_fifo_value_diff(wh, sl); + } else { + // average rate for weighted average + var rate = (wh.balance_qty.toFixed(2) == 0.00 ? 0 : + flt(wh.balance_value) / flt(wh.balance_qty)); + + // no change in value if negative qty + if((wh.balance_qty + sl.qty).toFixed(2) >= 0.00) + var value_diff = (rate * sl.qty); + else + var value_diff = -wh.balance_value; + } + } + + // update balance (only needed in case of valuation) + wh.balance_qty += sl.qty; + wh.balance_value += value_diff; + + return value_diff; + }, + get_fifo_value_diff: function(wh, sl) { + // get exact rate from fifo stack + var fifo_stack = (wh.fifo_stack || []).reverse(); + var fifo_value_diff = 0.0; + var qty = -sl.qty; + + for(var i=0, j=fifo_stack.length; i= qty) { + batch[0] = batch[0] - qty; + fifo_value_diff += (qty * batch[1]); + + qty = 0.0; + if(batch[0]) { + // batch still has qty put it back + fifo_stack.push(batch); + } + + // all qty found + break; + } else { + // consume this batch fully + fifo_value_diff += (batch[0] * batch[1]); + qty = qty - batch[0]; + } + } + if(qty) { + // msgprint("Negative values not allowed for FIFO valuation!\ + // Item " + sl.item_code.bold() + " on " + dateutil.str_to_user(sl.posting_datetime).bold() + + // " becomes negative. Values computed will not be accurate."); + } + + // reset the updated stack + wh.fifo_stack = fifo_stack.reverse(); + return -fifo_value_diff; + }, +}); \ No newline at end of file From 473f7d6314dac2b55ee6ac7dca5cf1731360cd17 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 24 Sep 2012 10:05:14 +0530 Subject: [PATCH 3/3] removed signin.erpnext.com --- erpnext/utilities/page/messages/messages.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/erpnext/utilities/page/messages/messages.py b/erpnext/utilities/page/messages/messages.py index 9627d99e21..25284132f3 100644 --- a/erpnext/utilities/page/messages/messages.py +++ b/erpnext/utilities/page/messages/messages.py @@ -93,8 +93,6 @@ def notify(arg=None): Comment: %s To answer, please login to your erpnext account! - - https://signin.erpnext.com ''' % (fn, arg['txt']) from webnotes.model.code import get_obj