blob: a343c3402afddbf7eb3d2cdf467ab469648363be [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
Faris Ansari1fe891b2021-04-23 08:04:00 +05305erpnext.StockAnalytics = class StockAnalytics extends erpnext.StockGridReport {
6 constructor(wrapper, opts) {
Saurabhb35906e2016-07-02 17:12:22 +05307 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
Faris Ansari1fe891b2021-04-23 08:04:00 +053033 super(args);
Faris Ansarif97cc032021-05-07 15:32:06 +053034
35 this.filters = [
36 {fieldtype:"Select", label: __("Value or Qty"), fieldname: "value_or_qty",
37 options:[{label:__("Value"), value:"Value"}, {label:__("Quantity"), value:"Quantity"}],
38 filter: function(val, item, opts, me) {
39 return me.apply_zero_filter(val, item, opts, me);
40 }},
41 {fieldtype:"Select", label: __("Brand"), link:"Brand", fieldname: "brand",
42 default_value: __("Select Brand..."), filter: function(val, item, opts) {
43 return val == opts.default_value || item.brand == val || item._show;
44 }, link_formatter: {filter_input: "brand"}},
45 {fieldtype:"Select", label: __("Warehouse"), link:"Warehouse", fieldname: "warehouse",
46 default_value: __("Select Warehouse...")},
47 {fieldtype:"Date", label: __("From Date"), fieldname: "from_date"},
48 {fieldtype:"Date", label: __("To Date"), fieldname: "to_date"},
49 {fieldtype:"Select", label: __("Range"), fieldname: "range",
50 options:[
51 {label:__("Daily"), value:"Daily"},
52 {label:__("Weekly"), value:"Weekly"},
53 {label:__("Monthly"), value:"Monthly"},
54 {label:__("Quarterly"), value:"Quarterly"},
55 {label:__("Yearly"), value:"Yearly"},
56 ]}
57 ];
Faris Ansari1fe891b2021-04-23 08:04:00 +053058 }
59 setup_columns() {
Saurabhb35906e2016-07-02 17:12:22 +053060 var std_columns = [
Faris Ansari34dd0792018-03-22 11:12:02 +053061 {id: "name", name: __("Item"), field: "name", width: 300},
Saurabhb35906e2016-07-02 17:12:22 +053062 {id: "brand", name: __("Brand"), field: "brand", width: 100},
63 {id: "stock_uom", name: __("UOM"), field: "stock_uom", width: 100},
64 {id: "opening", name: __("Opening"), field: "opening", hidden: true,
65 formatter: this.currency_formatter}
66 ];
67
68 this.make_date_range_columns();
69 this.columns = std_columns.concat(this.columns);
Faris Ansari1fe891b2021-04-23 08:04:00 +053070 }
71
Faris Ansari1fe891b2021-04-23 08:04:00 +053072 setup_filters() {
Saurabhb35906e2016-07-02 17:12:22 +053073 var me = this;
Faris Ansari1fe891b2021-04-23 08:04:00 +053074 super.setup_filters();
Saurabhb35906e2016-07-02 17:12:22 +053075
76 this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
77
78 this.show_zero_check();
Faris Ansari1fe891b2021-04-23 08:04:00 +053079 }
80 init_filter_values() {
81 super.init_filter_values();
Saurabhb35906e2016-07-02 17:12:22 +053082 this.filter_inputs.range && this.filter_inputs.range.val('Monthly');
Faris Ansari1fe891b2021-04-23 08:04:00 +053083 }
84 prepare_data() {
Saurabhb35906e2016-07-02 17:12:22 +053085 var me = this;
86
87 if(!this.data) {
88 var items = this.prepare_tree("Item", "Item Group");
89
90 me.parent_map = {};
91 me.item_by_name = {};
92 me.data = [];
93
94 $.each(items, function(i, v) {
95 var d = copy_dict(v);
96
97 me.data.push(d);
98 me.item_by_name[d.name] = d;
99 if(d.parent_item_group) {
100 me.parent_map[d.name] = d.parent_item_group;
101 }
102 me.reset_item_values(d);
103 });
104 this.set_indent();
105 this.data[0].checked = true;
106 } else {
107 // otherwise, only reset values
108 $.each(this.data, function(i, d) {
109 me.reset_item_values(d);
110 d["closing_qty_value"] = 0;
111 });
112 }
113
114 this.prepare_balances();
115 this.update_groups();
116
Faris Ansari1fe891b2021-04-23 08:04:00 +0530117 }
118 prepare_balances() {
Saurabhb35906e2016-07-02 17:12:22 +0530119 var me = this;
Faris Ansariab74ca72017-05-30 12:54:42 +0530120 var from_date = frappe.datetime.str_to_obj(this.from_date);
121 var to_date = frappe.datetime.str_to_obj(this.to_date);
Saurabhb35906e2016-07-02 17:12:22 +0530122 var data = frappe.report_dump.data["Stock Ledger Entry"];
123
124 this.item_warehouse = {};
125 this.serialized_buying_rates = this.get_serialized_buying_rates();
126
127 for(var i=0, j=data.length; i<j; i++) {
128 var sl = data[i];
129 sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
Faris Ansariab74ca72017-05-30 12:54:42 +0530130 var posting_datetime = frappe.datetime.str_to_obj(sl.posting_datetime);
Saurabhb35906e2016-07-02 17:12:22 +0530131
132 if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
133 var item = me.item_by_name[sl.item_code];
134 if(item.closing_qty_value==undefined) item.closing_qty_value = 0;
135
136 if(me.value_or_qty!="Quantity") {
137 var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
138 var valuation_method = item.valuation_method ?
Faris Ansariab74ca72017-05-30 12:54:42 +0530139 item.valuation_method : frappe.sys_defaults.valuation_method;
Saurabhb35906e2016-07-02 17:12:22 +0530140 var is_fifo = valuation_method == "FIFO";
141
142 if(sl.voucher_type=="Stock Reconciliation") {
143 var diff = (sl.qty_after_transaction * sl.valuation_rate) - item.closing_qty_value;
144 wh.fifo_stack = [[sl.qty_after_transaction, sl.valuation_rate, sl.posting_date]];
145 wh.balance_qty = sl.qty_after_transaction;
146 wh.balance_value = sl.valuation_rate * sl.qty_after_transaction;
147 } else {
148 var diff = me.get_value_diff(wh, sl, is_fifo);
149 }
150 } else {
151 if(sl.voucher_type=="Stock Reconciliation") {
152 var diff = sl.qty_after_transaction - item.closing_qty_value;
153 } else {
154 var diff = sl.qty;
155 }
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 }
165
166 item.closing_qty_value += diff;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530167 }
Saurabhb35906e2016-07-02 17:12:22 +0530168 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530169 }
170 update_groups() {
Saurabhb35906e2016-07-02 17:12:22 +0530171 var me = this;
172 $.each(this.data, function(i, item) {
173 // update groups
174 if(!item.is_group && me.apply_filter(item, "brand")) {
175 var balance = item.opening;
176 $.each(me.columns, function(i, col) {
177 if(col.formatter==me.currency_formatter && !col.hidden) {
178 item[col.field] = balance + item[col.field];
179 balance = item[col.field];
180 }
181 });
182
183 var parent = me.parent_map[item.name];
184 while(parent) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530185 var parent_group = me.item_by_name[parent];
Saurabhb35906e2016-07-02 17:12:22 +0530186 $.each(me.columns, function(c, col) {
187 if (col.formatter == me.currency_formatter) {
188 parent_group[col.field] =
189 flt(parent_group[col.field])
190 + flt(item[col.field]);
Anand Doshi4279dec2012-12-25 18:35:12 +0530191 }
192 });
Saurabhb35906e2016-07-02 17:12:22 +0530193 parent = me.parent_map[parent];
Anand Doshi4279dec2012-12-25 18:35:12 +0530194 }
Saurabhb35906e2016-07-02 17:12:22 +0530195 }
196 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530197 }
198 show_stock_ledger(item_code) {
Saurabhb35906e2016-07-02 17:12:22 +0530199 frappe.route_options = {
200 item_code: item_code,
201 from_date: this.from_date,
202 to_date: this.to_date
203 };
204 frappe.set_route("query-report", "Stock Ledger");
205 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530206};