// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt
erpnext.StockAnalytics = erpnext.StockGridReport.extend({
	init: function(wrapper, opts) {
		var args = {
			title: __("Stock Analytics"),
			parent: $(wrapper).find('.layout-main'),
			page: wrapper.page,
			doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand",
				"Fiscal Year", "Serial No"],
			tree_grid: {
				show: true,
				parent_field: "parent_item_group",
				formatter: function(item) {
					if(!item.is_group) {
						return repl("\
							%(value)s", {
								value: item.name,
							});
					} else {
						return item.name;
					}
				}
			},
		}
		if(opts) $.extend(args, opts);
		this._super(args);
	},
	setup_columns: function() {
		var std_columns = [
			{id: "name", name: __("Item"), field: "name", width: 300},
			{id: "brand", name: __("Brand"), field: "brand", width: 100},
			{id: "stock_uom", name: __("UOM"), field: "stock_uom", width: 100},
			{id: "opening", name: __("Opening"), field: "opening", hidden: true,
				formatter: this.currency_formatter}
		];
		this.make_date_range_columns();
		this.columns = std_columns.concat(this.columns);
	},
	filters: [
		{fieldtype:"Select", label: __("Value or Qty"), fieldname: "value_or_qty",
			options:[{label:__("Value"), value:"Value"}, {label:__("Quantity"), value:"Quantity"}],
			filter: function(val, item, opts, me) {
				return me.apply_zero_filter(val, item, opts, me);
			}},
		{fieldtype:"Select", label: __("Brand"), link:"Brand", fieldname: "brand",
			default_value: __("Select Brand..."), filter: function(val, item, opts) {
				return val == opts.default_value || item.brand == val || item._show;
			}, link_formatter: {filter_input: "brand"}},
		{fieldtype:"Select", label: __("Warehouse"), link:"Warehouse", fieldname: "warehouse",
			default_value: __("Select Warehouse...")},
		{fieldtype:"Date", label: __("From Date"), fieldname: "from_date"},
		{fieldtype:"Date", label: __("To Date"), fieldname: "to_date"},
		{fieldtype:"Select", label: __("Range"), fieldname: "range",
			options:[
				{label:__("Daily"), value:"Daily"},
				{label:__("Weekly"), value:"Weekly"},
				{label:__("Monthly"), value:"Monthly"},
				{label:__("Quarterly"), value:"Quarterly"},
				{label:__("Yearly"), value:"Yearly"},
			]}
	],
	setup_filters: function() {
		var me = this;
		this._super();
		this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]);
		this.show_zero_check();
	},
	init_filter_values: function() {
		this._super();
		this.filter_inputs.range && this.filter_inputs.range.val('Monthly');
	},
	prepare_data: function() {
		var me = this;
		if(!this.data) {
			var items = this.prepare_tree("Item", "Item Group");
			me.parent_map = {};
			me.item_by_name = {};
			me.data = [];
			$.each(items, function(i, v) {
				var d = copy_dict(v);
				me.data.push(d);
				me.item_by_name[d.name] = d;
				if(d.parent_item_group) {
					me.parent_map[d.name] = d.parent_item_group;
				}
				me.reset_item_values(d);
			});
			this.set_indent();
			this.data[0].checked = true;
		} else {
			// otherwise, only reset values
			$.each(this.data, function(i, d) {
				me.reset_item_values(d);
				d["closing_qty_value"] = 0;
			});
		}
		this.prepare_balances();
		this.update_groups();
	},
	prepare_balances: function() {
		var me = this;
		var from_date = frappe.datetime.str_to_obj(this.from_date);
		var to_date = frappe.datetime.str_to_obj(this.to_date);
		var data = frappe.report_dump.data["Stock Ledger Entry"];
		this.item_warehouse = {};
		this.serialized_buying_rates = this.get_serialized_buying_rates();
		for(var i=0, j=data.length; i