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 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 4 | erpnext.StockAnalytics = class StockAnalytics extends erpnext.StockGridReport { |
| 5 | constructor(wrapper, opts) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 6 | var args = { |
| 7 | title: __("Stock Analytics"), |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 8 | parent: $(wrapper).find(".layout-main"), |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 9 | page: wrapper.page, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 10 | doctypes: [ |
| 11 | "Item", |
| 12 | "Item Group", |
| 13 | "Warehouse", |
| 14 | "Stock Ledger Entry", |
| 15 | "Brand", |
| 16 | "Fiscal Year", |
| 17 | "Serial No", |
| 18 | ], |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 19 | tree_grid: { |
| 20 | show: true, |
| 21 | parent_field: "parent_item_group", |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 22 | formatter: function (item) { |
| 23 | if (!item.is_group) { |
| 24 | return repl( |
| 25 | "<a \ |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 26 | onclick='frappe.cur_grid_report.show_stock_ledger(\"%(value)s\")'>\ |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 27 | %(value)s</a>", |
| 28 | { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 29 | value: item.name, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 30 | } |
| 31 | ); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 32 | } else { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 33 | return item.name; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 34 | } |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 35 | }, |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 36 | }, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 37 | }; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 38 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 39 | if (opts) $.extend(args, opts); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 40 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 41 | super(args); |
Faris Ansari | f97cc03 | 2021-05-07 15:32:06 +0530 | [diff] [blame] | 42 | |
| 43 | this.filters = [ |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 44 | { |
| 45 | fieldtype: "Select", |
| 46 | label: __("Value or Qty"), |
| 47 | fieldname: "value_or_qty", |
| 48 | options: [ |
| 49 | { label: __("Value"), value: "Value" }, |
| 50 | { label: __("Quantity"), value: "Quantity" }, |
| 51 | ], |
| 52 | filter: function (val, item, opts, me) { |
Faris Ansari | f97cc03 | 2021-05-07 15:32:06 +0530 | [diff] [blame] | 53 | return me.apply_zero_filter(val, item, opts, me); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 54 | }, |
| 55 | }, |
| 56 | { |
| 57 | fieldtype: "Select", |
| 58 | label: __("Brand"), |
| 59 | link: "Brand", |
| 60 | fieldname: "brand", |
| 61 | default_value: __("Select Brand..."), |
| 62 | filter: function (val, item, opts) { |
Faris Ansari | f97cc03 | 2021-05-07 15:32:06 +0530 | [diff] [blame] | 63 | return val == opts.default_value || item.brand == val || item._show; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 64 | }, |
| 65 | link_formatter: { filter_input: "brand" }, |
| 66 | }, |
| 67 | { |
| 68 | fieldtype: "Select", |
| 69 | label: __("Warehouse"), |
| 70 | link: "Warehouse", |
| 71 | fieldname: "warehouse", |
| 72 | default_value: __("Select Warehouse..."), |
| 73 | }, |
| 74 | { fieldtype: "Date", label: __("From Date"), fieldname: "from_date" }, |
| 75 | { fieldtype: "Date", label: __("To Date"), fieldname: "to_date" }, |
| 76 | { |
| 77 | fieldtype: "Select", |
| 78 | label: __("Range"), |
| 79 | fieldname: "range", |
| 80 | options: [ |
| 81 | { label: __("Daily"), value: "Daily" }, |
| 82 | { label: __("Weekly"), value: "Weekly" }, |
| 83 | { label: __("Monthly"), value: "Monthly" }, |
| 84 | { label: __("Quarterly"), value: "Quarterly" }, |
| 85 | { label: __("Yearly"), value: "Yearly" }, |
| 86 | ], |
| 87 | }, |
Faris Ansari | f97cc03 | 2021-05-07 15:32:06 +0530 | [diff] [blame] | 88 | ]; |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 89 | } |
| 90 | setup_columns() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 91 | var std_columns = [ |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 92 | { id: "name", name: __("Item"), field: "name", width: 300 }, |
| 93 | { id: "brand", name: __("Brand"), field: "brand", width: 100 }, |
| 94 | { id: "stock_uom", name: __("UOM"), field: "stock_uom", width: 100 }, |
| 95 | { |
| 96 | id: "opening", |
| 97 | name: __("Opening"), |
| 98 | field: "opening", |
| 99 | hidden: true, |
| 100 | formatter: this.currency_formatter, |
| 101 | }, |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 102 | ]; |
| 103 | |
| 104 | this.make_date_range_columns(); |
| 105 | this.columns = std_columns.concat(this.columns); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 106 | } |
| 107 | |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 108 | setup_filters() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 109 | var me = this; |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 110 | super.setup_filters(); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 111 | |
| 112 | this.trigger_refresh_on_change(["value_or_qty", "brand", "warehouse", "range"]); |
| 113 | |
| 114 | this.show_zero_check(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 115 | } |
| 116 | init_filter_values() { |
| 117 | super.init_filter_values(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 118 | this.filter_inputs.range && this.filter_inputs.range.val("Monthly"); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 119 | } |
| 120 | prepare_data() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 121 | var me = this; |
| 122 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 123 | if (!this.data) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 124 | var items = this.prepare_tree("Item", "Item Group"); |
| 125 | |
| 126 | me.parent_map = {}; |
| 127 | me.item_by_name = {}; |
| 128 | me.data = []; |
| 129 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 130 | $.each(items, function (i, v) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 131 | var d = copy_dict(v); |
| 132 | |
| 133 | me.data.push(d); |
| 134 | me.item_by_name[d.name] = d; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 135 | if (d.parent_item_group) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 136 | me.parent_map[d.name] = d.parent_item_group; |
| 137 | } |
| 138 | me.reset_item_values(d); |
| 139 | }); |
| 140 | this.set_indent(); |
| 141 | this.data[0].checked = true; |
| 142 | } else { |
| 143 | // otherwise, only reset values |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 144 | $.each(this.data, function (i, d) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 145 | me.reset_item_values(d); |
| 146 | d["closing_qty_value"] = 0; |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | this.prepare_balances(); |
| 151 | this.update_groups(); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 152 | } |
| 153 | prepare_balances() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 154 | var me = this; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 155 | var from_date = frappe.datetime.str_to_obj(this.from_date); |
| 156 | var to_date = frappe.datetime.str_to_obj(this.to_date); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 157 | var data = frappe.report_dump.data["Stock Ledger Entry"]; |
| 158 | |
| 159 | this.item_warehouse = {}; |
| 160 | this.serialized_buying_rates = this.get_serialized_buying_rates(); |
| 161 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 162 | for (var i = 0, j = data.length; i < j; i++) { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 163 | let diff = 0; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 164 | var sl = data[i]; |
| 165 | sl.posting_datetime = sl.posting_date + " " + sl.posting_time; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 166 | var posting_datetime = frappe.datetime.str_to_obj(sl.posting_datetime); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 167 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 168 | if (me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 169 | var item = me.item_by_name[sl.item_code]; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 170 | if (item.closing_qty_value == undefined) item.closing_qty_value = 0; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 171 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 172 | if (me.value_or_qty != "Quantity") { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 173 | var wh = me.get_item_warehouse(sl.warehouse, sl.item_code); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 174 | var valuation_method = item.valuation_method |
| 175 | ? item.valuation_method |
| 176 | : frappe.sys_defaults.valuation_method; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 177 | var is_fifo = valuation_method == "FIFO"; |
| 178 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 179 | if (sl.voucher_type == "Stock Reconciliation") { |
| 180 | diff = sl.qty_after_transaction * sl.valuation_rate - item.closing_qty_value; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 181 | wh.fifo_stack = [[sl.qty_after_transaction, sl.valuation_rate, sl.posting_date]]; |
| 182 | wh.balance_qty = sl.qty_after_transaction; |
| 183 | wh.balance_value = sl.valuation_rate * sl.qty_after_transaction; |
| 184 | } else { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 185 | diff = me.get_value_diff(wh, sl, is_fifo); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 186 | } |
| 187 | } else { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 188 | if (sl.voucher_type == "Stock Reconciliation") { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 189 | diff = sl.qty_after_transaction - item.closing_qty_value; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 190 | } else { |
Deepesh Garg | 3fa2a8c | 2023-07-15 18:03:16 +0530 | [diff] [blame] | 191 | diff = sl.qty; |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 195 | if (posting_datetime < from_date) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 196 | item.opening += diff; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 197 | } else if (posting_datetime <= to_date) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 198 | item[me.column_map[sl.posting_date].field] += diff; |
| 199 | } else { |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | item.closing_qty_value += diff; |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 204 | } |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 205 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 206 | } |
| 207 | update_groups() { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 208 | var me = this; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 209 | $.each(this.data, function (i, item) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 210 | // update groups |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 211 | if (!item.is_group && me.apply_filter(item, "brand")) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 212 | var balance = item.opening; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 213 | $.each(me.columns, function (i, col) { |
| 214 | if (col.formatter == me.currency_formatter && !col.hidden) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 215 | item[col.field] = balance + item[col.field]; |
| 216 | balance = item[col.field]; |
| 217 | } |
| 218 | }); |
| 219 | |
| 220 | var parent = me.parent_map[item.name]; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 221 | while (parent) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 222 | var parent_group = me.item_by_name[parent]; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 223 | $.each(me.columns, function (c, col) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 224 | if (col.formatter == me.currency_formatter) { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 225 | parent_group[col.field] = flt(parent_group[col.field]) + flt(item[col.field]); |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 226 | } |
| 227 | }); |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 228 | parent = me.parent_map[parent]; |
Anand Doshi | 4279dec | 2012-12-25 18:35:12 +0530 | [diff] [blame] | 229 | } |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 230 | } |
| 231 | }); |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 232 | } |
| 233 | show_stock_ledger(item_code) { |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 234 | frappe.route_options = { |
| 235 | item_code: item_code, |
| 236 | from_date: this.from_date, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 237 | to_date: this.to_date, |
Saurabh | b35906e | 2016-07-02 17:12:22 +0530 | [diff] [blame] | 238 | }; |
| 239 | frappe.set_route("query-report", "Stock Ledger"); |
| 240 | } |
Faris Ansari | 1fe891b | 2021-04-23 08:04:00 +0530 | [diff] [blame] | 241 | }; |