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