Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe 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 | |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 4 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 5 | erpnext.StockAnalytics = class StockAnalytics extends erpnext.StockGridReport { |
| 6 | constructor(wrapper, opts) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 7 | var args = { |
| 8 | title: __("Stock Analytics"), |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 9 | 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 Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 23 | } else { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 24 | return item.name; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 25 | } |
| 26 | |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 27 | } |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 28 | }, |
| 29 | } |
| 30 | |
| 31 | if(opts) $.extend(args, opts); |
| 32 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 33 | super(args); |
Faris Ansari | f97cc03 | 2021-05-07 15:32:06 +0530 | [diff] [blame] | 34 | |
| 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 Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 58 | } |
| 59 | setup_columns() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 60 | var std_columns = [ |
Faris Ansari | 34dd079 | 2018-03-22 11:12:02 +0530 | [diff] [blame] | 61 | {id: "name", name: __("Item"), field: "name", width: 300}, |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 62 | {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 Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 70 | } |
| 71 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 72 | setup_filters() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 73 | var me = this; |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 74 | super.setup_filters(); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 75 | |
| 76 | this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]); |
| 77 | |
| 78 | this.show_zero_check(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 79 | } |
| 80 | init_filter_values() { |
| 81 | super.init_filter_values(); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 82 | this.filter_inputs.range && this.filter_inputs.range.val('Monthly'); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 83 | } |
| 84 | prepare_data() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 85 | 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 Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 117 | } |
| 118 | prepare_balances() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 119 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 120 | var from_date = frappe.datetime.str_to_obj(this.from_date); |
| 121 | var to_date = frappe.datetime.str_to_obj(this.to_date); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 122 | 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++) { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 128 | let diff = 0; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 129 | var sl = data[i]; |
| 130 | sl.posting_datetime = sl.posting_date + " " + sl.posting_time; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 131 | var posting_datetime = frappe.datetime.str_to_obj(sl.posting_datetime); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 132 | |
| 133 | if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) { |
| 134 | var item = me.item_by_name[sl.item_code]; |
| 135 | if(item.closing_qty_value==undefined) item.closing_qty_value = 0; |
| 136 | |
| 137 | if(me.value_or_qty!="Quantity") { |
| 138 | var wh = me.get_item_warehouse(sl.warehouse, sl.item_code); |
| 139 | var valuation_method = item.valuation_method ? |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 140 | item.valuation_method : frappe.sys_defaults.valuation_method; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 141 | var is_fifo = valuation_method == "FIFO"; |
| 142 | |
| 143 | if(sl.voucher_type=="Stock Reconciliation") { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 144 | diff = (sl.qty_after_transaction * sl.valuation_rate) - item.closing_qty_value; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 145 | wh.fifo_stack = [[sl.qty_after_transaction, sl.valuation_rate, sl.posting_date]]; |
| 146 | wh.balance_qty = sl.qty_after_transaction; |
| 147 | wh.balance_value = sl.valuation_rate * sl.qty_after_transaction; |
| 148 | } else { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 149 | diff = me.get_value_diff(wh, sl, is_fifo); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 150 | } |
| 151 | } else { |
| 152 | if(sl.voucher_type=="Stock Reconciliation") { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 153 | diff = sl.qty_after_transaction - item.closing_qty_value; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 154 | } else { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 155 | diff = sl.qty; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | if(posting_datetime < from_date) { |
| 160 | item.opening += diff; |
| 161 | } else if(posting_datetime <= to_date) { |
| 162 | item[me.column_map[sl.posting_date].field] += diff; |
| 163 | } else { |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | item.closing_qty_value += diff; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 168 | } |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 169 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 170 | } |
| 171 | update_groups() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 172 | var me = this; |
| 173 | $.each(this.data, function(i, item) { |
| 174 | // update groups |
| 175 | if(!item.is_group && me.apply_filter(item, "brand")) { |
| 176 | var balance = item.opening; |
| 177 | $.each(me.columns, function(i, col) { |
| 178 | if(col.formatter==me.currency_formatter && !col.hidden) { |
| 179 | item[col.field] = balance + item[col.field]; |
| 180 | balance = item[col.field]; |
| 181 | } |
| 182 | }); |
| 183 | |
| 184 | var parent = me.parent_map[item.name]; |
| 185 | while(parent) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 186 | var parent_group = me.item_by_name[parent]; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 187 | $.each(me.columns, function(c, col) { |
| 188 | if (col.formatter == me.currency_formatter) { |
| 189 | parent_group[col.field] = |
| 190 | flt(parent_group[col.field]) |
| 191 | + flt(item[col.field]); |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 192 | } |
| 193 | }); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 194 | parent = me.parent_map[parent]; |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 195 | } |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 196 | } |
| 197 | }); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 198 | } |
| 199 | show_stock_ledger(item_code) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 200 | frappe.route_options = { |
| 201 | item_code: item_code, |
| 202 | from_date: this.from_date, |
| 203 | to_date: this.to_date |
| 204 | }; |
| 205 | frappe.set_route("query-report", "Stock Ledger"); |
| 206 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 207 | }; |