Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 1 | frappe.provide('erpnext.stock'); |
| 2 | |
| 3 | erpnext.stock.ItemDashboard = Class.extend({ |
| 4 | init: function(opts) { |
| 5 | $.extend(this, opts); |
| 6 | this.make(); |
| 7 | }, |
| 8 | make: function() { |
| 9 | var me = this; |
| 10 | this.start = 0; |
| 11 | if(!this.sort_by) { |
| 12 | this.sort_by = 'projected_qty'; |
| 13 | this.sort_order = 'asc'; |
| 14 | } |
| 15 | |
| 16 | this.content = $(frappe.render_template('item_dashboard')).appendTo(this.parent); |
| 17 | this.result = this.content.find('.result'); |
| 18 | |
Shivam Mishra | 38b930b | 2019-07-05 10:38:48 +0530 | [diff] [blame^] | 19 | this.content.on('click', '.btn-move', function() { |
| 20 | let item = unescape($(this).attr('data-item')); |
| 21 | let warehouse = unescape($(this).attr('data-warehouse')); |
| 22 | open_stock_entry(item, warehouse, "Material Transfer"); |
| 23 | }); |
| 24 | |
| 25 | this.content.on('click', '.btn-add', function() { |
| 26 | let item = unescape($(this).attr('data-item')); |
| 27 | let warehouse = unescape($(this).attr('data-warehouse')); |
| 28 | open_stock_entry(item, warehouse); |
| 29 | }); |
| 30 | |
| 31 | function open_stock_entry(item, warehouse, entry_type) { |
| 32 | frappe.model.with_doctype('Stock Entry', function() { |
| 33 | var doc = frappe.model.get_new_doc('Stock Entry'); |
| 34 | if (entry_type) doc.stock_entry_type = entry_type; |
| 35 | |
| 36 | var row = frappe.model.add_child(doc, 'items'); |
| 37 | row.item_code = item; |
| 38 | row.s_warehouse = warehouse; |
| 39 | |
| 40 | frappe.set_route('Form', doc.doctype, doc.name); |
| 41 | }) |
| 42 | } |
| 43 | |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 44 | // more |
| 45 | this.content.find('.btn-more').on('click', function() { |
| 46 | me.start += 20; |
| 47 | me.refresh(); |
| 48 | }); |
| 49 | |
| 50 | }, |
| 51 | refresh: function() { |
| 52 | if(this.before_refresh) { |
| 53 | this.before_refresh(); |
| 54 | } |
| 55 | |
| 56 | var me = this; |
| 57 | frappe.call({ |
| 58 | method: 'erpnext.stock.dashboard.item_dashboard.get_data', |
| 59 | args: { |
| 60 | item_code: this.item_code, |
| 61 | warehouse: this.warehouse, |
Rushabh Mehta | 057db06 | 2016-11-08 12:40:04 +0530 | [diff] [blame] | 62 | item_group: this.item_group, |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 63 | start: this.start, |
| 64 | sort_by: this.sort_by, |
| 65 | sort_order: this.sort_order, |
| 66 | }, |
| 67 | callback: function(r) { |
| 68 | me.render(r.message); |
| 69 | } |
| 70 | }); |
| 71 | }, |
| 72 | render: function(data) { |
| 73 | if(this.start===0) { |
| 74 | this.max_count = 0; |
| 75 | this.result.empty(); |
| 76 | } |
| 77 | |
| 78 | var context = this.get_item_dashboard_data(data, this.max_count, true); |
| 79 | this.max_count = this.max_count; |
| 80 | |
| 81 | // show more button |
Rushabh Mehta | aed79e9 | 2016-06-02 17:49:16 +0530 | [diff] [blame] | 82 | if(data && data.length===21) { |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 83 | this.content.find('.more').removeClass('hidden'); |
| 84 | |
| 85 | // remove the last element |
| 86 | data.splice(-1); |
| 87 | } else { |
| 88 | this.content.find('.more').addClass('hidden'); |
| 89 | } |
| 90 | |
Vinayak Jethe | 2c8ce5a | 2018-01-24 13:51:26 +0530 | [diff] [blame] | 91 | // If not any stock in any warehouses provide a message to end user |
Stavros Anastasiadis | 58b5878 | 2017-10-05 16:14:50 +0300 | [diff] [blame] | 92 | if (context.data.length > 0) { |
| 93 | $(frappe.render_template('item_dashboard_list', context)).appendTo(this.result); |
| 94 | } else { |
| 95 | var message = __(" Currently no stock available in any warehouse") |
Rushabh Mehta | d5c6416 | 2017-11-14 15:27:28 +0530 | [diff] [blame] | 96 | $("<span class='text-muted small'>"+message+"</span>").appendTo(this.result); |
Stavros Anastasiadis | 58b5878 | 2017-10-05 16:14:50 +0300 | [diff] [blame] | 97 | } |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 98 | }, |
| 99 | get_item_dashboard_data: function(data, max_count, show_item) { |
| 100 | if(!max_count) max_count = 0; |
Rushabh Mehta | 0dcb861 | 2016-05-31 07:22:37 +0530 | [diff] [blame] | 101 | if(!data) data = []; |
Vinayak Jethe | 549c196 | 2018-01-24 19:47:59 +0530 | [diff] [blame] | 102 | |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 103 | data.forEach(function(d) { |
Nabin Hait | a674640 | 2018-03-28 11:16:00 +0530 | [diff] [blame] | 104 | d.actual_or_pending = d.projected_qty + d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract; |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 105 | d.pending_qty = 0; |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 106 | d.total_reserved = d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract; |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 107 | if(d.actual_or_pending > d.actual_qty) { |
| 108 | d.pending_qty = d.actual_or_pending - d.actual_qty; |
| 109 | } |
| 110 | |
| 111 | max_count = Math.max(d.actual_or_pending, d.actual_qty, |
| 112 | d.total_reserved, max_count); |
| 113 | }); |
Vinayak Jethe | 549c196 | 2018-01-24 19:47:59 +0530 | [diff] [blame] | 114 | |
| 115 | var can_write = 0; |
| 116 | if(frappe.boot.user.can_write.indexOf("Stock Entry")>=0){ |
| 117 | can_write = 1; |
| 118 | } |
| 119 | |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 120 | return { |
| 121 | data: data, |
| 122 | max_count: max_count, |
Vinayak Jethe | 549c196 | 2018-01-24 19:47:59 +0530 | [diff] [blame] | 123 | can_write:can_write, |
Rushabh Mehta | 3d2622c | 2016-04-25 17:53:42 +0530 | [diff] [blame] | 124 | show_item: show_item || false |
| 125 | } |
| 126 | } |
Shivam Mishra | c41403c | 2019-06-27 16:33:19 +0530 | [diff] [blame] | 127 | }) |