blob: 5ebe65f8039a44a55209b8c9a879bc70088ed841 [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},
Nabin Hait92a02182013-01-22 17:52:43 +053056 {id: "stock_uom", name: "UOM", field: "stock_uom", width: 100},
Anand Doshi6ff2f662012-12-25 18:39:00 +053057 {id: "brand", name: "Brand", field: "brand", width: 100},
Anand Doshi4279dec2012-12-25 18:35:12 +053058 {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 Haitd797ffb2013-01-22 14:02:55 +053066 {fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"],
Anand Doshi4279dec2012-12-25 18:35:12 +053067 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);
Nabin Hait319fb922013-01-22 14:40:20 +0530148 var valuation_method = item.valuation_method ?
149 item.valuation_method : sys_defaults.valuation_method;
150 var is_fifo = valuation_method == "FIFO";
151
Anand Doshi4279dec2012-12-25 18:35:12 +0530152 var diff = me.get_value_diff(wh, sl, is_fifo);
153 } else {
154 var diff = sl.qty;
155 }
156
157 if(posting_datetime < from_date) {
158 item.opening += diff;
159 } else if(posting_datetime <= to_date) {
160 item[me.column_map[sl.posting_date].field] += diff;
161 } else {
162 break;
163 }
164 }
165 }
166 },
167 update_groups: function() {
168 var me = this;
169
170 $.each(this.data, function(i, item) {
171 // update groups
172 if(!item.is_group && me.apply_filter(item, "brand")) {
173 var balance = item.opening;
174 $.each(me.columns, function(i, col) {
175 if(col.formatter==me.currency_formatter && !col.hidden) {
176 item[col.field] = balance + item[col.field];
177 balance = item[col.field];
178 }
179 });
180
181 var parent = me.parent_map[item.name];
182 while(parent) {
183 parent_group = me.item_by_name[parent];
184 $.each(me.columns, function(c, col) {
185 if (col.formatter == me.currency_formatter) {
186 parent_group[col.field] =
187 flt(parent_group[col.field])
188 + flt(item[col.field]);
189 }
190 });
191 parent = me.parent_map[parent];
192 }
193 }
194 });
195 },
196 get_plot_points: function(item, col, idx) {
197 return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
198 }
199});