Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | // License: GNU General Public License v3. See license.txt |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 3 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 4 | frappe.require("assets/erpnext/js/stock_grid_report.js"); |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 5 | |
| 6 | erpnext.StockAnalytics = erpnext.StockGridReport.extend({ |
| 7 | init: function(wrapper, opts) { |
| 8 | var args = { |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 9 | title: __("Stock Analytics"), |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 10 | page: wrapper, |
| 11 | parent: $(wrapper).find('.layout-main'), |
| 12 | appframe: wrapper.appframe, |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 13 | doctypes: ["Item", "Item Group", "Warehouse", "Stock Ledger Entry", "Brand", |
Akhilesh Darjee | 6ca0548 | 2013-11-18 12:16:48 +0530 | [diff] [blame] | 14 | "Fiscal Year", "Serial No"], |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 15 | tree_grid: { |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 16 | show: true, |
| 17 | parent_field: "parent_item_group", |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 18 | formatter: function(item) { |
| 19 | if(!item.is_group) { |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 20 | return repl("<a \ |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 21 | onclick='frappe.cur_grid_report.show_stock_ledger(\"%(value)s\")'>\ |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 22 | %(value)s</a>", { |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 23 | value: item.name, |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 24 | }); |
| 25 | } else { |
| 26 | return item.name; |
| 27 | } |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 28 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 29 | } |
| 30 | }, |
| 31 | } |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 32 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 33 | if(opts) $.extend(args, opts); |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 34 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 35 | this._super(args); |
| 36 | }, |
| 37 | setup_columns: function() { |
| 38 | var std_columns = [ |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 39 | {id: "check", name: __("Plot"), field: "check", width: 30, |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 40 | formatter: this.check_formatter}, |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 41 | {id: "name", name: __("Item"), field: "name", width: 300, |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 42 | formatter: this.tree_formatter}, |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 43 | {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 Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 46 | formatter: this.currency_formatter} |
| 47 | ]; |
| 48 | |
| 49 | this.make_date_range_columns(); |
| 50 | this.columns = std_columns.concat(this.columns); |
| 51 | }, |
| 52 | filters: [ |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 53 | {fieldtype:"Select", label: __("Value or Qty"), fieldname: "value_or_qty", |
| 54 | options:["Value", "Quantity"], |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 55 | filter: function(val, item, opts, me) { |
| 56 | return me.apply_zero_filter(val, item, opts, me); |
| 57 | }}, |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 58 | {fieldtype:"Select", label: __("Brand"), link:"Brand", fieldname: "brand", |
| 59 | default_value: __("Select Brand..."), filter: function(val, item, opts) { |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 60 | return val == opts.default_value || item.brand == val || item._show; |
Nabin Hait | 30f5346 | 2012-12-28 15:39:55 +0530 | [diff] [blame] | 61 | }, link_formatter: {filter_input: "brand"}}, |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 62 | {fieldtype:"Select", label: __("Warehouse"), link:"Warehouse", fieldname: "warehouse", |
| 63 | default_value: __("Select Warehouse...")}, |
| 64 | {fieldtype:"Date", label: __("From Date"), fieldname: "from_date"}, |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 65 | {fieldtype:"Label", label: __("To")}, |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 66 | {fieldtype:"Date", label: __("To Date"), fieldname: "to_date"}, |
| 67 | {fieldtype:"Select", label: __("Range"), fieldname: "range", |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 68 | options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]}, |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 69 | {fieldtype:"Button", label: __("Refresh"), icon:"icon-refresh icon-white"}, |
Rushabh Mehta | f944e81 | 2014-05-27 15:22:04 +0530 | [diff] [blame] | 70 | {fieldtype:"Button", label: __("Reset Filters"), icon: "icon-filter"} |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 71 | ], |
| 72 | setup_filters: function() { |
| 73 | var me = this; |
| 74 | this._super(); |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 75 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 76 | this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]); |
| 77 | |
| 78 | this.show_zero_check(); |
| 79 | this.setup_plot_check(); |
| 80 | }, |
| 81 | init_filter_values: function() { |
| 82 | this._super(); |
| 83 | this.filter_inputs.range && this.filter_inputs.range.val('Monthly'); |
| 84 | }, |
| 85 | prepare_data: function() { |
| 86 | var me = this; |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 87 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 88 | if(!this.data) { |
| 89 | var items = this.prepare_tree("Item", "Item Group"); |
| 90 | |
| 91 | me.parent_map = {}; |
| 92 | me.item_by_name = {}; |
| 93 | me.data = []; |
| 94 | |
| 95 | $.each(items, function(i, v) { |
| 96 | var d = copy_dict(v); |
| 97 | |
| 98 | me.data.push(d); |
| 99 | me.item_by_name[d.name] = d; |
| 100 | if(d.parent_item_group) { |
| 101 | me.parent_map[d.name] = d.parent_item_group; |
| 102 | } |
| 103 | me.reset_item_values(d); |
| 104 | }); |
| 105 | this.set_indent(); |
| 106 | this.data[0].checked = true; |
| 107 | } else { |
| 108 | // otherwise, only reset values |
| 109 | $.each(this.data, function(i, d) { |
| 110 | me.reset_item_values(d); |
| 111 | }); |
| 112 | } |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 113 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 114 | this.prepare_balances(); |
| 115 | this.update_groups(); |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 116 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 117 | }, |
| 118 | prepare_balances: function() { |
| 119 | var me = this; |
| 120 | var from_date = dateutil.str_to_obj(this.from_date); |
| 121 | var to_date = dateutil.str_to_obj(this.to_date); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 122 | var data = frappe.report_dump.data["Stock Ledger Entry"]; |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 123 | |
| 124 | this.item_warehouse = {}; |
Anand Doshi | d5aeb21 | 2013-03-08 12:46:48 +0530 | [diff] [blame] | 125 | this.serialized_buying_rates = this.get_serialized_buying_rates(); |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 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; |
| 130 | var posting_datetime = dateutil.str_to_obj(sl.posting_datetime); |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 131 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 132 | if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) { |
| 133 | var item = me.item_by_name[sl.item_code]; |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 134 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 135 | if(me.value_or_qty!="Quantity") { |
| 136 | var wh = me.get_item_warehouse(sl.warehouse, sl.item_code); |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 137 | var valuation_method = item.valuation_method ? |
Nabin Hait | 319fb92 | 2013-01-22 14:40:20 +0530 | [diff] [blame] | 138 | item.valuation_method : sys_defaults.valuation_method; |
| 139 | var is_fifo = valuation_method == "FIFO"; |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 140 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 141 | var diff = me.get_value_diff(wh, sl, is_fifo); |
| 142 | } else { |
| 143 | var diff = sl.qty; |
| 144 | } |
| 145 | |
| 146 | if(posting_datetime < from_date) { |
| 147 | item.opening += diff; |
| 148 | } else if(posting_datetime <= to_date) { |
| 149 | item[me.column_map[sl.posting_date].field] += diff; |
| 150 | } else { |
| 151 | break; |
| 152 | } |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | }, |
| 156 | update_groups: function() { |
| 157 | var me = this; |
| 158 | |
| 159 | $.each(this.data, function(i, item) { |
| 160 | // update groups |
| 161 | if(!item.is_group && me.apply_filter(item, "brand")) { |
| 162 | var balance = item.opening; |
| 163 | $.each(me.columns, function(i, col) { |
| 164 | if(col.formatter==me.currency_formatter && !col.hidden) { |
| 165 | item[col.field] = balance + item[col.field]; |
| 166 | balance = item[col.field]; |
| 167 | } |
| 168 | }); |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 169 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 170 | var parent = me.parent_map[item.name]; |
| 171 | while(parent) { |
| 172 | parent_group = me.item_by_name[parent]; |
| 173 | $.each(me.columns, function(c, col) { |
| 174 | if (col.formatter == me.currency_formatter) { |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 175 | parent_group[col.field] = |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 176 | flt(parent_group[col.field]) |
| 177 | + flt(item[col.field]); |
| 178 | } |
| 179 | }); |
| 180 | parent = me.parent_map[parent]; |
| 181 | } |
| 182 | } |
| 183 | }); |
| 184 | }, |
| 185 | get_plot_points: function(item, col, idx) { |
| 186 | return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]] |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 187 | }, |
| 188 | show_stock_ledger: function(item_code) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 189 | frappe.route_options = { |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 190 | item_code: item_code, |
| 191 | from_date: this.from_date, |
| 192 | to_date: this.to_date |
| 193 | }; |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 194 | frappe.set_route("query-report", "Stock Ledger"); |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 195 | } |
Nabin Hait | b15764e | 2014-06-03 13:02:18 +0530 | [diff] [blame] | 196 | }); |