blob: 140c9dc90b676b02ca3bb95ddb49b11fd705433a [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 = [
Faris Ansari34dd0792018-03-22 11:12:02 +053037 {id: "name", name: __("Item"), field: "name", width: 300},
Saurabhb35906e2016-07-02 17:12:22 +053038 {id: "brand", name: __("Brand"), field: "brand", width: 100},
39 {id: "stock_uom", name: __("UOM"), field: "stock_uom", width: 100},
40 {id: "opening", name: __("Opening"), field: "opening", hidden: true,
41 formatter: this.currency_formatter}
42 ];
43
44 this.make_date_range_columns();
45 this.columns = std_columns.concat(this.columns);
46 },
47 filters: [
48 {fieldtype:"Select", label: __("Value or Qty"), fieldname: "value_or_qty",
49 options:[{label:__("Value"), value:"Value"}, {label:__("Quantity"), value:"Quantity"}],
50 filter: function(val, item, opts, me) {
51 return me.apply_zero_filter(val, item, opts, me);
52 }},
53 {fieldtype:"Select", label: __("Brand"), link:"Brand", fieldname: "brand",
54 default_value: __("Select Brand..."), filter: function(val, item, opts) {
55 return val == opts.default_value || item.brand == val || item._show;
56 }, link_formatter: {filter_input: "brand"}},
57 {fieldtype:"Select", label: __("Warehouse"), link:"Warehouse", fieldname: "warehouse",
58 default_value: __("Select Warehouse...")},
59 {fieldtype:"Date", label: __("From Date"), fieldname: "from_date"},
60 {fieldtype:"Date", label: __("To Date"), fieldname: "to_date"},
61 {fieldtype:"Select", label: __("Range"), fieldname: "range",
62 options:[
63 {label:__("Daily"), value:"Daily"},
64 {label:__("Weekly"), value:"Weekly"},
65 {label:__("Monthly"), value:"Monthly"},
66 {label:__("Quarterly"), value:"Quarterly"},
67 {label:__("Yearly"), value:"Yearly"},
68 ]}
69 ],
70 setup_filters: function() {
71 var me = this;
72 this._super();
73
74 this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
75
76 this.show_zero_check();
Saurabhb35906e2016-07-02 17:12:22 +053077 },
78 init_filter_values: function() {
79 this._super();
80 this.filter_inputs.range && this.filter_inputs.range.val('Monthly');
81 },
82 prepare_data: function() {
83 var me = this;
84
85 if(!this.data) {
86 var items = this.prepare_tree("Item", "Item Group");
87
88 me.parent_map = {};
89 me.item_by_name = {};
90 me.data = [];
91
92 $.each(items, function(i, v) {
93 var d = copy_dict(v);
94
95 me.data.push(d);
96 me.item_by_name[d.name] = d;
97 if(d.parent_item_group) {
98 me.parent_map[d.name] = d.parent_item_group;
99 }
100 me.reset_item_values(d);
101 });
102 this.set_indent();
103 this.data[0].checked = true;
104 } else {
105 // otherwise, only reset values
106 $.each(this.data, function(i, d) {
107 me.reset_item_values(d);
108 d["closing_qty_value"] = 0;
109 });
110 }
111
112 this.prepare_balances();
113 this.update_groups();
114
115 },
116 prepare_balances: function() {
117 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530118 var from_date = frappe.datetime.str_to_obj(this.from_date);
119 var to_date = frappe.datetime.str_to_obj(this.to_date);
Saurabhb35906e2016-07-02 17:12:22 +0530120 var data = frappe.report_dump.data["Stock Ledger Entry"];
121
122 this.item_warehouse = {};
123 this.serialized_buying_rates = this.get_serialized_buying_rates();
124
125 for(var i=0, j=data.length; i<j; i++) {
126 var sl = data[i];
127 sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
Faris Ansariab74ca72017-05-30 12:54:42 +0530128 var posting_datetime = frappe.datetime.str_to_obj(sl.posting_datetime);
Saurabhb35906e2016-07-02 17:12:22 +0530129
130 if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
131 var item = me.item_by_name[sl.item_code];
132 if(item.closing_qty_value==undefined) item.closing_qty_value = 0;
133
134 if(me.value_or_qty!="Quantity") {
135 var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
136 var valuation_method = item.valuation_method ?
Faris Ansariab74ca72017-05-30 12:54:42 +0530137 item.valuation_method : frappe.sys_defaults.valuation_method;
Saurabhb35906e2016-07-02 17:12:22 +0530138 var is_fifo = valuation_method == "FIFO";
139
140 if(sl.voucher_type=="Stock Reconciliation") {
141 var diff = (sl.qty_after_transaction * sl.valuation_rate) - item.closing_qty_value;
142 wh.fifo_stack = [[sl.qty_after_transaction, sl.valuation_rate, sl.posting_date]];
143 wh.balance_qty = sl.qty_after_transaction;
144 wh.balance_value = sl.valuation_rate * sl.qty_after_transaction;
145 } else {
146 var diff = me.get_value_diff(wh, sl, is_fifo);
147 }
148 } else {
149 if(sl.voucher_type=="Stock Reconciliation") {
150 var diff = sl.qty_after_transaction - item.closing_qty_value;
151 } else {
152 var diff = sl.qty;
153 }
154 }
155
156 if(posting_datetime < from_date) {
157 item.opening += diff;
158 } else if(posting_datetime <= to_date) {
159 item[me.column_map[sl.posting_date].field] += diff;
160 } else {
161 break;
162 }
163
164 item.closing_qty_value += diff;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530165 }
Saurabhb35906e2016-07-02 17:12:22 +0530166 }
167 },
168 update_groups: function() {
169 var me = this;
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) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530183 var parent_group = me.item_by_name[parent];
Saurabhb35906e2016-07-02 17:12:22 +0530184 $.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]);
Anand Doshi4279dec2012-12-25 18:35:12 +0530189 }
190 });
Saurabhb35906e2016-07-02 17:12:22 +0530191 parent = me.parent_map[parent];
Anand Doshi4279dec2012-12-25 18:35:12 +0530192 }
Saurabhb35906e2016-07-02 17:12:22 +0530193 }
194 });
195 },
196 show_stock_ledger: function(item_code) {
197 frappe.route_options = {
198 item_code: item_code,
199 from_date: this.from_date,
200 to_date: this.to_date
201 };
202 frappe.set_route("query-report", "Stock Ledger");
203 }
204});
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530205