blob: 53be93c4e4dc9679fe472451e7ed6ea105be8350 [file] [log] [blame]
Anand Doshi4279dec2012-12-25 18:35:12 +05301// 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
17wn.require("app/js/stock_grid_report.js");
18
19erpnext.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 Doshi6ff2f662012-12-25 18:39:00 +053056 {id: "brand", name: "Brand", field: "brand", width: 100},
Anand Doshi4279dec2012-12-25 18:35:12 +053057 {id: "opening", name: "Opening", field: "opening", hidden: true,
58 formatter: this.currency_formatter}
59 ];
60
61 this.make_date_range_columns();
62 this.columns = std_columns.concat(this.columns);
63 },
64 filters: [
Nabin Haitd797ffb2013-01-22 14:02:55 +053065 {fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"],
Anand Doshi4279dec2012-12-25 18:35:12 +053066 filter: function(val, item, opts, me) {
67 return me.apply_zero_filter(val, item, opts, me);
68 }},
69 {fieldtype:"Select", label: "Brand", link:"Brand",
70 default_value: "Select Brand...", filter: function(val, item, opts) {
71 return val == opts.default_value || item.brand == val || item._show;
Nabin Hait30f53462012-12-28 15:39:55 +053072 }, link_formatter: {filter_input: "brand"}},
Anand Doshi4279dec2012-12-25 18:35:12 +053073 {fieldtype:"Select", label: "Warehouse", link:"Warehouse",
74 default_value: "Select Warehouse..."},
75 {fieldtype:"Date", label: "From Date"},
76 {fieldtype:"Label", label: "To"},
77 {fieldtype:"Date", label: "To Date"},
78 {fieldtype:"Select", label: "Range",
79 options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]},
80 {fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"},
81 {fieldtype:"Button", label: "Reset Filters"}
82 ],
83 setup_filters: function() {
84 var me = this;
85 this._super();
86
87 this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
88
89 this.show_zero_check();
90 this.setup_plot_check();
91 },
92 init_filter_values: function() {
93 this._super();
94 this.filter_inputs.range && this.filter_inputs.range.val('Monthly');
95 },
96 prepare_data: function() {
97 var me = this;
98
99 if(!this.data) {
100 var items = this.prepare_tree("Item", "Item Group");
101
102 me.parent_map = {};
103 me.item_by_name = {};
104 me.data = [];
105
106 $.each(items, function(i, v) {
107 var d = copy_dict(v);
108
109 me.data.push(d);
110 me.item_by_name[d.name] = d;
111 if(d.parent_item_group) {
112 me.parent_map[d.name] = d.parent_item_group;
113 }
114 me.reset_item_values(d);
115 });
116 this.set_indent();
117 this.data[0].checked = true;
118 } else {
119 // otherwise, only reset values
120 $.each(this.data, function(i, d) {
121 me.reset_item_values(d);
122 });
123 }
124
125 this.prepare_balances();
126 this.update_groups();
127
128 },
129 prepare_balances: function() {
130 var me = this;
131 var from_date = dateutil.str_to_obj(this.from_date);
132 var to_date = dateutil.str_to_obj(this.to_date);
133 var data = wn.report_dump.data["Stock Ledger Entry"];
134
135 this.item_warehouse = {};
136
137 for(var i=0, j=data.length; i<j; i++) {
138 var sl = data[i];
139 sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
140 var posting_datetime = dateutil.str_to_obj(sl.posting_datetime);
141
142 if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
143 var item = me.item_by_name[sl.item_code];
144
145 if(me.value_or_qty!="Quantity") {
146 var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
Nabin Haitd797ffb2013-01-22 14:02:55 +0530147 var is_fifo = item.valuation_method == "FIFO";
Anand Doshi4279dec2012-12-25 18:35:12 +0530148 var diff = me.get_value_diff(wh, sl, is_fifo);
149 } else {
150 var diff = sl.qty;
151 }
152
153 if(posting_datetime < from_date) {
154 item.opening += diff;
155 } else if(posting_datetime <= to_date) {
156 item[me.column_map[sl.posting_date].field] += diff;
157 } else {
158 break;
159 }
160 }
161 }
162 },
163 update_groups: function() {
164 var me = this;
165
166 $.each(this.data, function(i, item) {
167 // update groups
168 if(!item.is_group && me.apply_filter(item, "brand")) {
169 var balance = item.opening;
170 $.each(me.columns, function(i, col) {
171 if(col.formatter==me.currency_formatter && !col.hidden) {
172 item[col.field] = balance + item[col.field];
173 balance = item[col.field];
174 }
175 });
176
177 var parent = me.parent_map[item.name];
178 while(parent) {
179 parent_group = me.item_by_name[parent];
180 $.each(me.columns, function(c, col) {
181 if (col.formatter == me.currency_formatter) {
182 parent_group[col.field] =
183 flt(parent_group[col.field])
184 + flt(item[col.field]);
185 }
186 });
187 parent = me.parent_map[parent];
188 }
189 }
190 });
191 },
192 get_plot_points: function(item, col, idx) {
193 return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
194 }
195});