Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 1 | // ERPNext - web based ERP (http://erpnext.com) |
| 2 | // Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | wn.require("app/js/stock_grid_report.js"); |
| 18 | |
| 19 | erpnext.StockAnalytics = erpnext.StockGridReport.extend({ |
| 20 | init: function(wrapper, opts) { |
| 21 | var args = { |
| 22 | title: "Stock Analytics", |
| 23 | page: wrapper, |
| 24 | parent: $(wrapper).find('.layout-main'), |
| 25 | appframe: wrapper.appframe, |
| 26 | doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand", |
| 27 | "Fiscal Year"], |
| 28 | tree_grid: { |
| 29 | show: true, |
| 30 | parent_field: "parent_item_group", |
| 31 | formatter: function(item) { |
| 32 | if(!item.is_group) { |
| 33 | return repl('<a href="#stock-ledger/item_code=%(enc_value)s">%(value)s</a>', |
| 34 | { |
| 35 | value: item.name, |
| 36 | enc_value: encodeURIComponent(item.name) |
| 37 | }); |
| 38 | } else { |
| 39 | return item.name; |
| 40 | } |
| 41 | |
| 42 | } |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | if(opts) $.extend(args, opts); |
| 47 | |
| 48 | this._super(args); |
| 49 | }, |
| 50 | setup_columns: function() { |
| 51 | var std_columns = [ |
| 52 | {id: "check", name: "Plot", field: "check", width: 30, |
| 53 | formatter: this.check_formatter}, |
| 54 | {id: "name", name: "Item", field: "name", width: 300, |
| 55 | formatter: this.tree_formatter}, |
Anand Doshi | 6ff2f66 | 2012-12-25 18:39:00 +0530 | [diff] [blame] | 56 | {id: "brand", name: "Brand", field: "brand", width: 100}, |
Anand Doshi | 805fdf3 | 2013-02-26 12:23:39 +0530 | [diff] [blame] | 57 | {id: "stock_uom", name: "UOM", field: "stock_uom", width: 100}, |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 58 | {id: "opening", name: "Opening", field: "opening", hidden: true, |
| 59 | formatter: this.currency_formatter} |
| 60 | ]; |
| 61 | |
| 62 | this.make_date_range_columns(); |
| 63 | this.columns = std_columns.concat(this.columns); |
| 64 | }, |
| 65 | filters: [ |
Nabin Hait | d797ffb | 2013-01-22 14:02:55 +0530 | [diff] [blame] | 66 | {fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"], |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 67 | filter: function(val, item, opts, me) { |
| 68 | return me.apply_zero_filter(val, item, opts, me); |
| 69 | }}, |
| 70 | {fieldtype:"Select", label: "Brand", link:"Brand", |
| 71 | default_value: "Select Brand...", filter: function(val, item, opts) { |
| 72 | return val == opts.default_value || item.brand == val || item._show; |
Nabin Hait | 30f5346 | 2012-12-28 15:39:55 +0530 | [diff] [blame] | 73 | }, link_formatter: {filter_input: "brand"}}, |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 74 | {fieldtype:"Select", label: "Warehouse", link:"Warehouse", |
| 75 | default_value: "Select Warehouse..."}, |
| 76 | {fieldtype:"Date", label: "From Date"}, |
| 77 | {fieldtype:"Label", label: "To"}, |
| 78 | {fieldtype:"Date", label: "To Date"}, |
| 79 | {fieldtype:"Select", label: "Range", |
| 80 | options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]}, |
| 81 | {fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"}, |
| 82 | {fieldtype:"Button", label: "Reset Filters"} |
| 83 | ], |
| 84 | setup_filters: function() { |
| 85 | var me = this; |
| 86 | this._super(); |
| 87 | |
| 88 | this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]); |
| 89 | |
| 90 | this.show_zero_check(); |
| 91 | this.setup_plot_check(); |
| 92 | }, |
| 93 | init_filter_values: function() { |
| 94 | this._super(); |
| 95 | this.filter_inputs.range && this.filter_inputs.range.val('Monthly'); |
| 96 | }, |
| 97 | prepare_data: function() { |
| 98 | var me = this; |
| 99 | |
| 100 | if(!this.data) { |
| 101 | var items = this.prepare_tree("Item", "Item Group"); |
| 102 | |
| 103 | me.parent_map = {}; |
| 104 | me.item_by_name = {}; |
| 105 | me.data = []; |
| 106 | |
| 107 | $.each(items, function(i, v) { |
| 108 | var d = copy_dict(v); |
| 109 | |
| 110 | me.data.push(d); |
| 111 | me.item_by_name[d.name] = d; |
| 112 | if(d.parent_item_group) { |
| 113 | me.parent_map[d.name] = d.parent_item_group; |
| 114 | } |
| 115 | me.reset_item_values(d); |
| 116 | }); |
| 117 | this.set_indent(); |
| 118 | this.data[0].checked = true; |
| 119 | } else { |
| 120 | // otherwise, only reset values |
| 121 | $.each(this.data, function(i, d) { |
| 122 | me.reset_item_values(d); |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | this.prepare_balances(); |
| 127 | this.update_groups(); |
| 128 | |
| 129 | }, |
| 130 | prepare_balances: function() { |
| 131 | var me = this; |
| 132 | var from_date = dateutil.str_to_obj(this.from_date); |
| 133 | var to_date = dateutil.str_to_obj(this.to_date); |
| 134 | var data = wn.report_dump.data["Stock Ledger Entry"]; |
| 135 | |
| 136 | this.item_warehouse = {}; |
Anand Doshi | d5aeb21 | 2013-03-08 12:46:48 +0530 | [diff] [blame] | 137 | this.serialized_buying_rates = this.get_serialized_buying_rates(); |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 138 | |
| 139 | for(var i=0, j=data.length; i<j; i++) { |
| 140 | var sl = data[i]; |
| 141 | sl.posting_datetime = sl.posting_date + " " + sl.posting_time; |
| 142 | var posting_datetime = dateutil.str_to_obj(sl.posting_datetime); |
| 143 | |
| 144 | if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) { |
| 145 | var item = me.item_by_name[sl.item_code]; |
| 146 | |
| 147 | if(me.value_or_qty!="Quantity") { |
| 148 | var wh = me.get_item_warehouse(sl.warehouse, sl.item_code); |
Nabin Hait | 319fb92 | 2013-01-22 14:40:20 +0530 | [diff] [blame] | 149 | var valuation_method = item.valuation_method ? |
| 150 | item.valuation_method : sys_defaults.valuation_method; |
| 151 | var is_fifo = valuation_method == "FIFO"; |
| 152 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 153 | var diff = me.get_value_diff(wh, sl, is_fifo); |
| 154 | } else { |
| 155 | var diff = sl.qty; |
| 156 | } |
| 157 | |
| 158 | if(posting_datetime < from_date) { |
| 159 | item.opening += diff; |
| 160 | } else if(posting_datetime <= to_date) { |
| 161 | item[me.column_map[sl.posting_date].field] += diff; |
| 162 | } else { |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | }, |
| 168 | update_groups: function() { |
| 169 | var me = this; |
| 170 | |
| 171 | $.each(this.data, function(i, item) { |
| 172 | // update groups |
| 173 | if(!item.is_group && me.apply_filter(item, "brand")) { |
| 174 | var balance = item.opening; |
| 175 | $.each(me.columns, function(i, col) { |
| 176 | if(col.formatter==me.currency_formatter && !col.hidden) { |
| 177 | item[col.field] = balance + item[col.field]; |
| 178 | balance = item[col.field]; |
| 179 | } |
| 180 | }); |
| 181 | |
| 182 | var parent = me.parent_map[item.name]; |
| 183 | while(parent) { |
| 184 | parent_group = me.item_by_name[parent]; |
| 185 | $.each(me.columns, function(c, col) { |
| 186 | if (col.formatter == me.currency_formatter) { |
| 187 | parent_group[col.field] = |
| 188 | flt(parent_group[col.field]) |
| 189 | + flt(item[col.field]); |
| 190 | } |
| 191 | }); |
| 192 | parent = me.parent_map[parent]; |
| 193 | } |
| 194 | } |
| 195 | }); |
| 196 | }, |
| 197 | get_plot_points: function(item, col, idx) { |
| 198 | return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]] |
| 199 | } |
| 200 | }); |