blob: a8229baf0f911b2cc39deff88ed96e22b50e06b6 [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
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05304frappe.require("assets/erpnext/js/stock_grid_report.js");
Anand Doshi4279dec2012-12-25 18:35:12 +05305
6erpnext.StockAnalytics = erpnext.StockGridReport.extend({
7 init: function(wrapper, opts) {
8 var args = {
Pratik Vyasb52618c2014-04-14 16:25:30 +05309 title: __("Stock Analytics"),
Anand Doshi4279dec2012-12-25 18:35:12 +053010 page: wrapper,
11 parent: $(wrapper).find('.layout-main'),
Rushabh Mehta4096a3a2014-12-23 17:56:47 +053012 page: wrapper.page,
Nabin Haitb15764e2014-06-03 13:02:18 +053013 doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand",
Akhilesh Darjee6ca05482013-11-18 12:16:48 +053014 "Fiscal Year", "Serial No"],
Anand Doshi4279dec2012-12-25 18:35:12 +053015 tree_grid: {
Nabin Haitb15764e2014-06-03 13:02:18 +053016 show: true,
17 parent_field: "parent_item_group",
Anand Doshi4279dec2012-12-25 18:35:12 +053018 formatter: function(item) {
19 if(!item.is_group) {
Rushabh Mehta54047782013-12-26 11:07:46 +053020 return repl("<a \
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053021 onclick='frappe.cur_grid_report.show_stock_ledger(\"%(value)s\")'>\
Rushabh Mehta54047782013-12-26 11:07:46 +053022 %(value)s</a>", {
Anand Doshi4279dec2012-12-25 18:35:12 +053023 value: item.name,
Anand Doshi4279dec2012-12-25 18:35:12 +053024 });
25 } else {
26 return item.name;
27 }
Nabin Haitb15764e2014-06-03 13:02:18 +053028
Anand Doshi4279dec2012-12-25 18:35:12 +053029 }
30 },
31 }
Nabin Haitb15764e2014-06-03 13:02:18 +053032
Anand Doshi4279dec2012-12-25 18:35:12 +053033 if(opts) $.extend(args, opts);
Nabin Haitb15764e2014-06-03 13:02:18 +053034
Anand Doshi4279dec2012-12-25 18:35:12 +053035 this._super(args);
36 },
37 setup_columns: function() {
38 var std_columns = [
Rushabh Mehtac567e8e2015-02-03 17:55:52 +053039 {id: "_check", name: __("Plot"), field: "_check", width: 30,
Anand Doshi4279dec2012-12-25 18:35:12 +053040 formatter: this.check_formatter},
Pratik Vyasb52618c2014-04-14 16:25:30 +053041 {id: "name", name: __("Item"), field: "name", width: 300,
Anand Doshi4279dec2012-12-25 18:35:12 +053042 formatter: this.tree_formatter},
Pratik Vyasb52618c2014-04-14 16:25:30 +053043 {id: "brand", name: __("Brand"), field: "brand", width: 100},
44 {id: "stock_uom", name: __("UOM"), field: "stock_uom", width: 100},
45 {id: "opening", name: __("Opening"), field: "opening", hidden: true,
Anand Doshi4279dec2012-12-25 18:35:12 +053046 formatter: this.currency_formatter}
47 ];
48
49 this.make_date_range_columns();
50 this.columns = std_columns.concat(this.columns);
51 },
52 filters: [
Nabin Haitb15764e2014-06-03 13:02:18 +053053 {fieldtype:"Select", label: __("Value or Qty"), fieldname: "value_or_qty",
Rushabh Mehtaeb961372016-01-04 15:48:37 +053054 options:[{label:__("Value"), value:"Value"}, {label:__("Quantity"), value:"Quantity"}],
Anand Doshi4279dec2012-12-25 18:35:12 +053055 filter: function(val, item, opts, me) {
56 return me.apply_zero_filter(val, item, opts, me);
57 }},
Nabin Haitb15764e2014-06-03 13:02:18 +053058 {fieldtype:"Select", label: __("Brand"), link:"Brand", fieldname: "brand",
59 default_value: __("Select Brand..."), filter: function(val, item, opts) {
Anand Doshi4279dec2012-12-25 18:35:12 +053060 return val == opts.default_value || item.brand == val || item._show;
Nabin Hait30f53462012-12-28 15:39:55 +053061 }, link_formatter: {filter_input: "brand"}},
Nabin Haitb15764e2014-06-03 13:02:18 +053062 {fieldtype:"Select", label: __("Warehouse"), link:"Warehouse", fieldname: "warehouse",
63 default_value: __("Select Warehouse...")},
64 {fieldtype:"Date", label: __("From Date"), fieldname: "from_date"},
Nabin Haitb15764e2014-06-03 13:02:18 +053065 {fieldtype:"Date", label: __("To Date"), fieldname: "to_date"},
66 {fieldtype:"Select", label: __("Range"), fieldname: "range",
Rushabh Mehtaeb961372016-01-04 15:48:37 +053067 options:[
68 {label:__("Daily"), value:"Daily"},
69 {label:__("Weekly"), value:"Weekly"},
70 {label:__("Monthly"), value:"Monthly"},
71 {label:__("Quarterly"), value:"Quarterly"},
72 {label:__("Yearly"), value:"Yearly"},
73 ]}
Anand Doshi4279dec2012-12-25 18:35:12 +053074 ],
75 setup_filters: function() {
76 var me = this;
77 this._super();
Nabin Haitb15764e2014-06-03 13:02:18 +053078
Anand Doshi4279dec2012-12-25 18:35:12 +053079 this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
80
81 this.show_zero_check();
82 this.setup_plot_check();
83 },
84 init_filter_values: function() {
85 this._super();
86 this.filter_inputs.range && this.filter_inputs.range.val('Monthly');
87 },
88 prepare_data: function() {
89 var me = this;
Nabin Haitb15764e2014-06-03 13:02:18 +053090
Anand Doshi4279dec2012-12-25 18:35:12 +053091 if(!this.data) {
92 var items = this.prepare_tree("Item", "Item Group");
93
94 me.parent_map = {};
95 me.item_by_name = {};
96 me.data = [];
97
98 $.each(items, function(i, v) {
99 var d = copy_dict(v);
100
101 me.data.push(d);
102 me.item_by_name[d.name] = d;
103 if(d.parent_item_group) {
104 me.parent_map[d.name] = d.parent_item_group;
105 }
106 me.reset_item_values(d);
107 });
108 this.set_indent();
109 this.data[0].checked = true;
110 } else {
111 // otherwise, only reset values
112 $.each(this.data, function(i, d) {
113 me.reset_item_values(d);
Nabin Hait54c31b42014-12-30 16:33:07 +0530114 d["closing_qty_value"] = 0;
Anand Doshi4279dec2012-12-25 18:35:12 +0530115 });
116 }
Nabin Haitb15764e2014-06-03 13:02:18 +0530117
Anand Doshi4279dec2012-12-25 18:35:12 +0530118 this.prepare_balances();
119 this.update_groups();
Nabin Haitb15764e2014-06-03 13:02:18 +0530120
Anand Doshi4279dec2012-12-25 18:35:12 +0530121 },
122 prepare_balances: function() {
123 var me = this;
124 var from_date = dateutil.str_to_obj(this.from_date);
125 var to_date = dateutil.str_to_obj(this.to_date);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530126 var data = frappe.report_dump.data["Stock Ledger Entry"];
Anand Doshi4279dec2012-12-25 18:35:12 +0530127
128 this.item_warehouse = {};
Anand Doshid5aeb212013-03-08 12:46:48 +0530129 this.serialized_buying_rates = this.get_serialized_buying_rates();
Anand Doshi4279dec2012-12-25 18:35:12 +0530130
131 for(var i=0, j=data.length; i<j; i++) {
132 var sl = data[i];
133 sl.posting_datetime = sl.posting_date + " " + sl.posting_time;
134 var posting_datetime = dateutil.str_to_obj(sl.posting_datetime);
Nabin Haitb15764e2014-06-03 13:02:18 +0530135
Anand Doshi4279dec2012-12-25 18:35:12 +0530136 if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {
137 var item = me.item_by_name[sl.item_code];
Nabin Hait28913b92014-10-31 14:46:07 +0530138 if(item.closing_qty_value==undefined) item.closing_qty_value = 0;
Nabin Haitb15764e2014-06-03 13:02:18 +0530139
Anand Doshi4279dec2012-12-25 18:35:12 +0530140 if(me.value_or_qty!="Quantity") {
141 var wh = me.get_item_warehouse(sl.warehouse, sl.item_code);
Nabin Haitb15764e2014-06-03 13:02:18 +0530142 var valuation_method = item.valuation_method ?
Nabin Hait319fb922013-01-22 14:40:20 +0530143 item.valuation_method : sys_defaults.valuation_method;
144 var is_fifo = valuation_method == "FIFO";
Nabin Haitb15764e2014-06-03 13:02:18 +0530145
Nabin Haitb96c0142014-10-07 11:25:04 +0530146 if(sl.voucher_type=="Stock Reconciliation") {
147 var diff = (sl.qty_after_transaction * sl.valuation_rate) - item.closing_qty_value;
Nabin Hait995a7842015-06-03 16:44:26 +0530148 wh.fifo_stack = [[sl.qty_after_transaction, sl.valuation_rate, sl.posting_date]];
Nabin Haite96e83d2014-10-08 18:06:14 +0530149 wh.balance_qty = sl.qty_after_transaction;
150 wh.balance_value = sl.valuation_rate * sl.qty_after_transaction;
Nabin Haitb96c0142014-10-07 11:25:04 +0530151 } else {
152 var diff = me.get_value_diff(wh, sl, is_fifo);
153 }
Anand Doshi4279dec2012-12-25 18:35:12 +0530154 } else {
Nabin Haitb96c0142014-10-07 11:25:04 +0530155 if(sl.voucher_type=="Stock Reconciliation") {
156 var diff = sl.qty_after_transaction - item.closing_qty_value;
157 } else {
158 var diff = sl.qty;
159 }
Anand Doshi4279dec2012-12-25 18:35:12 +0530160 }
161
162 if(posting_datetime < from_date) {
163 item.opening += diff;
164 } else if(posting_datetime <= to_date) {
165 item[me.column_map[sl.posting_date].field] += diff;
166 } else {
167 break;
168 }
Nabin Haitb96c0142014-10-07 11:25:04 +0530169
170 item.closing_qty_value += diff;
Anand Doshi4279dec2012-12-25 18:35:12 +0530171 }
172 }
173 },
174 update_groups: function() {
175 var me = this;
Anand Doshi4279dec2012-12-25 18:35:12 +0530176 $.each(this.data, function(i, item) {
177 // update groups
178 if(!item.is_group && me.apply_filter(item, "brand")) {
179 var balance = item.opening;
180 $.each(me.columns, function(i, col) {
181 if(col.formatter==me.currency_formatter && !col.hidden) {
182 item[col.field] = balance + item[col.field];
183 balance = item[col.field];
184 }
185 });
Nabin Haitb15764e2014-06-03 13:02:18 +0530186
Anand Doshi4279dec2012-12-25 18:35:12 +0530187 var parent = me.parent_map[item.name];
188 while(parent) {
189 parent_group = me.item_by_name[parent];
190 $.each(me.columns, function(c, col) {
191 if (col.formatter == me.currency_formatter) {
Nabin Haitb15764e2014-06-03 13:02:18 +0530192 parent_group[col.field] =
Anand Doshi4279dec2012-12-25 18:35:12 +0530193 flt(parent_group[col.field])
194 + flt(item[col.field]);
195 }
196 });
197 parent = me.parent_map[parent];
198 }
199 }
200 });
201 },
202 get_plot_points: function(item, col, idx) {
203 return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
Rushabh Mehta54047782013-12-26 11:07:46 +0530204 },
205 show_stock_ledger: function(item_code) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530206 frappe.route_options = {
Rushabh Mehta54047782013-12-26 11:07:46 +0530207 item_code: item_code,
208 from_date: this.from_date,
209 to_date: this.to_date
210 };
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530211 frappe.set_route("query-report", "Stock Ledger");
Anand Doshi4279dec2012-12-25 18:35:12 +0530212 }
Nabin Haitb15764e2014-06-03 13:02:18 +0530213});