blob: 53641ce996219638ce97d65ef2956a3db3c93dc8 [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: [
65 {fieldtype:"Select", label: "Value or Qty", options:["Value (Weighted Average)",
66 "Value (FIFO)", "Quantity"],
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 Hait30f53462012-12-28 15:39:55 +053073 }, link_formatter: {filter_input: "brand"}},
Anand Doshi4279dec2012-12-25 18:35:12 +053074 {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 = {};
137
138 for(var i=0, j=data.length; i<j; i++) {
139 var sl = data[i];
140 sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
141 var posting_datetime = dateutil.str_to_obj(sl.posting_datetime);
142
143 if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
144 var item = me.item_by_name[sl.item_code];
145
146 if(me.value_or_qty!="Quantity") {
147 var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
148 var is_fifo = this.value_or_qty== "Value (FIFO)";
149 var diff = me.get_value_diff(wh, sl, is_fifo);
150 } else {
151 var diff = sl.qty;
152 }
153
154 if(posting_datetime < from_date) {
155 item.opening += diff;
156 } else if(posting_datetime <= to_date) {
157 item[me.column_map[sl.posting_date].field] += diff;
158 } else {
159 break;
160 }
161 }
162 }
163 },
164 update_groups: function() {
165 var me = this;
166
167 $.each(this.data, function(i, item) {
168 // update groups
169 if(!item.is_group && me.apply_filter(item, "brand")) {
170 var balance = item.opening;
171 $.each(me.columns, function(i, col) {
172 if(col.formatter==me.currency_formatter && !col.hidden) {
173 item[col.field] = balance + item[col.field];
174 balance = item[col.field];
175 }
176 });
177
178 var parent = me.parent_map[item.name];
179 while(parent) {
180 parent_group = me.item_by_name[parent];
181 $.each(me.columns, function(c, col) {
182 if (col.formatter == me.currency_formatter) {
183 parent_group[col.field] =
184 flt(parent_group[col.field])
185 + flt(item[col.field]);
186 }
187 });
188 parent = me.parent_map[parent];
189 }
190 }
191 });
192 },
193 get_plot_points: function(item, col, idx) {
194 return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
195 }
196});