blob: 28ff55da0936b589974e5f3d674e7e943a7b8c49 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Anand Doshi4279dec2012-12-25 18:35:12 +05303
Anand Doshi4279dec2012-12-25 18:35:12 +05304
Saurabhb35906e2016-07-02 17:12:22 +05305erpnext.StockAnalytics = erpnext.StockGridReport.extend({
6 init: function(wrapper, opts) {
7 var args = {
8 title: __("Stock Analytics"),
9 page: wrapper,
10 parent: $(wrapper).find('.layout-main'),
11 page: wrapper.page,
12 doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand",
13 "Fiscal Year", "Serial No"],
14 tree_grid: {
15 show: true,
16 parent_field: "parent_item_group",
17 formatter: function(item) {
18 if(!item.is_group) {
19 return repl("<a \
20 onclick='frappe.cur_grid_report.show_stock_ledger(\"%(value)s\")'>\
21 %(value)s</a>", {
22 value: item.name,
23 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053024 } else {
Saurabhb35906e2016-07-02 17:12:22 +053025 return item.name;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053026 }
27
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053028 }
Saurabhb35906e2016-07-02 17:12:22 +053029 },
30 }
31
32 if(opts) $.extend(args, opts);
33
34 this._super(args);
35 },
36 setup_columns: function() {
37 var std_columns = [
38 {id: "_check", name: __("Plot"), field: "_check", width: 30,
39 formatter: this.check_formatter},
40 {id: "name", name: __("Item"), field: "name", width: 300,
41 formatter: this.tree_formatter},
42 {id: "brand", name: __("Brand"), field: "brand", width: 100},
43 {id: "stock_uom", name: __("UOM"), field: "stock_uom", width: 100},
44 {id: "opening", name: __("Opening"), field: "opening", hidden: true,
45 formatter: this.currency_formatter}
46 ];
47
48 this.make_date_range_columns();
49 this.columns = std_columns.concat(this.columns);
50 },
51 filters: [
52 {fieldtype:"Select", label: __("Value or Qty"), fieldname: "value_or_qty",
53 options:[{label:__("Value"), value:"Value"}, {label:__("Quantity"), value:"Quantity"}],
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", fieldname: "brand",
58 default_value: __("Select Brand..."), filter: function(val, item, opts) {
59 return val == opts.default_value || item.brand == val || item._show;
60 }, link_formatter: {filter_input: "brand"}},
61 {fieldtype:"Select", label: __("Warehouse"), link:"Warehouse", fieldname: "warehouse",
62 default_value: __("Select Warehouse...")},
63 {fieldtype:"Date", label: __("From Date"), fieldname: "from_date"},
64 {fieldtype:"Date", label: __("To Date"), fieldname: "to_date"},
65 {fieldtype:"Select", label: __("Range"), fieldname: "range",
66 options:[
67 {label:__("Daily"), value:"Daily"},
68 {label:__("Weekly"), value:"Weekly"},
69 {label:__("Monthly"), value:"Monthly"},
70 {label:__("Quarterly"), value:"Quarterly"},
71 {label:__("Yearly"), value:"Yearly"},
72 ]}
73 ],
74 setup_filters: function() {
75 var me = this;
76 this._super();
77
78 this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
79
80 this.show_zero_check();
81 this.setup_chart_check();
82 },
83 init_filter_values: function() {
84 this._super();
85 this.filter_inputs.range && this.filter_inputs.range.val('Monthly');
86 },
87 prepare_data: function() {
88 var me = this;
89
90 if(!this.data) {
91 var items = this.prepare_tree("Item", "Item Group");
92
93 me.parent_map = {};
94 me.item_by_name = {};
95 me.data = [];
96
97 $.each(items, function(i, v) {
98 var d = copy_dict(v);
99
100 me.data.push(d);
101 me.item_by_name[d.name] = d;
102 if(d.parent_item_group) {
103 me.parent_map[d.name] = d.parent_item_group;
104 }
105 me.reset_item_values(d);
106 });
107 this.set_indent();
108 this.data[0].checked = true;
109 } else {
110 // otherwise, only reset values
111 $.each(this.data, function(i, d) {
112 me.reset_item_values(d);
113 d["closing_qty_value"] = 0;
114 });
115 }
116
117 this.prepare_balances();
118 this.update_groups();
119
120 },
121 prepare_balances: function() {
122 var me = this;
123 var from_date = dateutil.str_to_obj(this.from_date);
124 var to_date = dateutil.str_to_obj(this.to_date);
125 var data = frappe.report_dump.data["Stock Ledger Entry"];
126
127 this.item_warehouse = {};
128 this.serialized_buying_rates = this.get_serialized_buying_rates();
129
130 for(var i=0, j=data.length; i<j; i++) {
131 var sl = data[i];
132 sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
133 var posting_datetime = dateutil.str_to_obj(sl.posting_datetime);
134
135 if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
136 var item = me.item_by_name[sl.item_code];
137 if(item.closing_qty_value==undefined) item.closing_qty_value = 0;
138
139 if(me.value_or_qty!="Quantity") {
140 var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
141 var valuation_method = item.valuation_method ?
142 item.valuation_method : sys_defaults.valuation_method;
143 var is_fifo = valuation_method == "FIFO";
144
145 if(sl.voucher_type=="Stock Reconciliation") {
146 var diff = (sl.qty_after_transaction * sl.valuation_rate) - item.closing_qty_value;
147 wh.fifo_stack = [[sl.qty_after_transaction, sl.valuation_rate, sl.posting_date]];
148 wh.balance_qty = sl.qty_after_transaction;
149 wh.balance_value = sl.valuation_rate * sl.qty_after_transaction;
150 } else {
151 var diff = me.get_value_diff(wh, sl, is_fifo);
152 }
153 } else {
154 if(sl.voucher_type=="Stock Reconciliation") {
155 var diff = sl.qty_after_transaction - item.closing_qty_value;
156 } else {
157 var diff = sl.qty;
158 }
159 }
160
161 if(posting_datetime < from_date) {
162 item.opening += diff;
163 } else if(posting_datetime <= to_date) {
164 item[me.column_map[sl.posting_date].field] += diff;
165 } else {
166 break;
167 }
168
169 item.closing_qty_value += diff;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530170 }
Saurabhb35906e2016-07-02 17:12:22 +0530171 }
172 },
173 update_groups: function() {
174 var me = this;
175 $.each(this.data, function(i, item) {
176 // update groups
177 if(!item.is_group && me.apply_filter(item, "brand")) {
178 var balance = item.opening;
179 $.each(me.columns, function(i, col) {
180 if(col.formatter==me.currency_formatter && !col.hidden) {
181 item[col.field] = balance + item[col.field];
182 balance = item[col.field];
183 }
184 });
185
186 var parent = me.parent_map[item.name];
187 while(parent) {
188 parent_group = me.item_by_name[parent];
189 $.each(me.columns, function(c, col) {
190 if (col.formatter == me.currency_formatter) {
191 parent_group[col.field] =
192 flt(parent_group[col.field])
193 + flt(item[col.field]);
Anand Doshi4279dec2012-12-25 18:35:12 +0530194 }
195 });
Saurabhb35906e2016-07-02 17:12:22 +0530196 parent = me.parent_map[parent];
Anand Doshi4279dec2012-12-25 18:35:12 +0530197 }
Saurabhb35906e2016-07-02 17:12:22 +0530198 }
199 });
200 },
201 show_stock_ledger: function(item_code) {
202 frappe.route_options = {
203 item_code: item_code,
204 from_date: this.from_date,
205 to_date: this.to_date
206 };
207 frappe.set_route("query-report", "Stock Ledger");
208 }
209});
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530210