blob: 91384f61ed9eda0f1a8a838547dc9506f1a41c50 [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 Doshi805fdf32013-02-26 12:23:39 +053057 {id: "stock_uom", name: "UOM", field: "stock_uom", 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 = {};
Anand Doshid5aeb212013-03-08 12:46:48 +0530137 this.serialized_buying_rates = this.get_serialized_buying_rates();
Anand Doshi4279dec2012-12-25 18:35:12 +0530138
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 Hait319fb922013-01-22 14:40:20 +0530149 var valuation_method = item.valuation_method ?
150 item.valuation_method : sys_defaults.valuation_method;
151 var is_fifo = valuation_method == "FIFO";
152
Anand Doshi4279dec2012-12-25 18:35:12 +0530153 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 }
Anand Doshib8020502013-06-26 12:05:37 +0530165
166 me.round_item_values(item);
Anand Doshi4279dec2012-12-25 18:35:12 +0530167 }
168 }
169 },
170 update_groups: function() {
171 var me = this;
172
173 $.each(this.data, function(i, item) {
174 // update groups
175 if(!item.is_group && me.apply_filter(item, "brand")) {
176 var balance = item.opening;
177 $.each(me.columns, function(i, col) {
178 if(col.formatter==me.currency_formatter && !col.hidden) {
179 item[col.field] = balance + item[col.field];
180 balance = item[col.field];
181 }
182 });
183
184 var parent = me.parent_map[item.name];
185 while(parent) {
186 parent_group = me.item_by_name[parent];
187 $.each(me.columns, function(c, col) {
188 if (col.formatter == me.currency_formatter) {
189 parent_group[col.field] =
190 flt(parent_group[col.field])
191 + flt(item[col.field]);
192 }
193 });
194 parent = me.parent_map[parent];
195 }
196 }
197 });
198 },
199 get_plot_points: function(item, col, idx) {
200 return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
201 }
202});