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