blob: ed325a16b8a9153f3c211104f296ac4a93c75442 [file] [log] [blame]
Rushabh Mehta3d2622c2016-04-25 17:53:42 +05301frappe.provide('erpnext.stock');
2
3erpnext.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 Mishra38b930b2019-07-05 10:38:48 +053019 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 Mehta3d2622c2016-04-25 17:53:42 +053044 // 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 Mehta057db062016-11-08 12:40:04 +053062 item_group: this.item_group,
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053063 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 Mehtaaed79e92016-06-02 17:49:16 +053082 if(data && data.length===21) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053083 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 Jethe2c8ce5a2018-01-24 13:51:26 +053091 // If not any stock in any warehouses provide a message to end user
Stavros Anastasiadis58b58782017-10-05 16:14:50 +030092 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 Mehtad5c64162017-11-14 15:27:28 +053096 $("<span class='text-muted small'>"+message+"</span>").appendTo(this.result);
Stavros Anastasiadis58b58782017-10-05 16:14:50 +030097 }
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053098 },
99 get_item_dashboard_data: function(data, max_count, show_item) {
100 if(!max_count) max_count = 0;
Rushabh Mehta0dcb8612016-05-31 07:22:37 +0530101 if(!data) data = [];
Vinayak Jethe549c1962018-01-24 19:47:59 +0530102
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530103 data.forEach(function(d) {
Nabin Haita6746402018-03-28 11:16:00 +0530104 d.actual_or_pending = d.projected_qty + d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract;
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530105 d.pending_qty = 0;
Nabin Hait2c7a6e62018-03-12 14:12:12 +0530106 d.total_reserved = d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract;
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530107 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 Jethe549c1962018-01-24 19:47:59 +0530114
115 var can_write = 0;
116 if(frappe.boot.user.can_write.indexOf("Stock Entry")>=0){
117 can_write = 1;
118 }
119
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530120 return {
121 data: data,
122 max_count: max_count,
Vinayak Jethe549c1962018-01-24 19:47:59 +0530123 can_write:can_write,
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530124 show_item: show_item || false
125 }
126 }
Shivam Mishrac41403c2019-06-27 16:33:19 +0530127})