blob: abc286fcc6e7f8cfdc07f0fc49208d62009e95c9 [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() {
Shivam Mishra5738c932019-07-16 12:52:19 +053020 handle_move_add($(this), "Move")
Shivam Mishra38b930b2019-07-05 10:38:48 +053021 });
22
23 this.content.on('click', '.btn-add', function() {
Shivam Mishra7339c722019-07-16 13:19:13 +053024 handle_move_add($(this), "Add")
Shivam Mishra5738c932019-07-16 12:52:19 +053025 });
26
marination1087d972020-11-26 10:45:44 +053027 this.content.on('click', '.btn-edit', function() {
28 let item = unescape($(this).attr('data-item'));
29 let warehouse = unescape($(this).attr('data-warehouse'));
30 let company = unescape($(this).attr('data-company'));
31 frappe.db.get_value('Putaway Rule',
32 {'item_code': item, 'warehouse': warehouse, 'company': company}, 'name', (r) => {
33 frappe.set_route("Form", "Putaway Rule", r.name);
34 });
35 });
36
Shivam Mishra5738c932019-07-16 12:52:19 +053037 function handle_move_add(element, action) {
38 let item = unescape(element.attr('data-item'));
39 let warehouse = unescape(element.attr('data-warehouse'));
40 let actual_qty = unescape(element.attr('data-actual_qty'));
41 let disable_quick_entry = Number(unescape(element.attr('data-disable_quick_entry')));
42 let entry_type = action === "Move" ? "Material Transfer": null;
Shivam Mishra444091b2019-07-10 10:22:08 +053043
Shivam Mishrafd6ff622019-07-16 12:32:50 +053044 if (disable_quick_entry) {
Shivam Mishra5738c932019-07-16 12:52:19 +053045 open_stock_entry(item, warehouse, entry_type);
Shivam Mishrafd6ff622019-07-16 12:32:50 +053046 } else {
Shivam Mishra5738c932019-07-16 12:52:19 +053047 if (action === "Add") {
48 let rate = unescape($(this).attr('data-rate'));
49 erpnext.stock.move_item(item, null, warehouse, actual_qty, rate, function() { me.refresh(); });
50 }
51 else {
52 erpnext.stock.move_item(item, warehouse, null, actual_qty, null, function() { me.refresh(); });
53 }
Shivam Mishrafd6ff622019-07-16 12:32:50 +053054 }
Shivam Mishra5738c932019-07-16 12:52:19 +053055 }
Shivam Mishra38b930b2019-07-05 10:38:48 +053056
57 function open_stock_entry(item, warehouse, entry_type) {
58 frappe.model.with_doctype('Stock Entry', function() {
59 var doc = frappe.model.get_new_doc('Stock Entry');
60 if (entry_type) doc.stock_entry_type = entry_type;
61
62 var row = frappe.model.add_child(doc, 'items');
63 row.item_code = item;
64 row.s_warehouse = warehouse;
65
66 frappe.set_route('Form', doc.doctype, doc.name);
67 })
68 }
69
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053070 // more
71 this.content.find('.btn-more').on('click', function() {
marination1087d972020-11-26 10:45:44 +053072 me.start += this.page_length;
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053073 me.refresh();
74 });
75
76 },
77 refresh: function() {
78 if(this.before_refresh) {
79 this.before_refresh();
80 }
81
marination1087d972020-11-26 10:45:44 +053082 let args = {
83 item_code: this.item_code,
84 warehouse: this.warehouse,
85 parent_warehouse: this.parent_warehouse,
86 item_group: this.item_group,
87 company: this.company,
88 start: this.start,
89 sort_by: this.sort_by,
90 sort_order: this.sort_order
91 }
92
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053093 var me = this;
94 frappe.call({
marination1087d972020-11-26 10:45:44 +053095 method: this.method,
96 args: args,
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053097 callback: function(r) {
98 me.render(r.message);
99 }
100 });
101 },
102 render: function(data) {
marination1087d972020-11-26 10:45:44 +0530103 if (this.start===0) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530104 this.max_count = 0;
105 this.result.empty();
106 }
marination1087d972020-11-26 10:45:44 +0530107 if (this.page_name === "warehouse-capacity-summary") {
108 var context = this.get_capacity_dashboard_data(data);
109 } else {
110 var context = this.get_item_dashboard_data(data, this.max_count, true);
111 }
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530112
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530113 this.max_count = this.max_count;
114
115 // show more button
marination1087d972020-11-26 10:45:44 +0530116 if (data && data.length===(this.page_length + 1)) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530117 this.content.find('.more').removeClass('hidden');
118
119 // remove the last element
120 data.splice(-1);
121 } else {
122 this.content.find('.more').addClass('hidden');
123 }
124
Vinayak Jethe2c8ce5a2018-01-24 13:51:26 +0530125 // If not any stock in any warehouses provide a message to end user
Stavros Anastasiadis58b58782017-10-05 16:14:50 +0300126 if (context.data.length > 0) {
marination1087d972020-11-26 10:45:44 +0530127 this.content.find('.result').css('text-align', 'unset');
128 $(frappe.render_template(this.template, context)).appendTo(this.result);
Stavros Anastasiadis58b58782017-10-05 16:14:50 +0300129 } else {
marination1087d972020-11-26 10:45:44 +0530130 var message = __("No Stock Available Currently");
131 this.content.find('.result').css('text-align', 'center');
132
133 $(`<div class='text-muted' style='margin: 20px 5px; font-weight: lighter;'>
134 ${message} </div>`).appendTo(this.result);
Stavros Anastasiadis58b58782017-10-05 16:14:50 +0300135 }
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530136 },
marination1087d972020-11-26 10:45:44 +0530137
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530138 get_item_dashboard_data: function(data, max_count, show_item) {
139 if(!max_count) max_count = 0;
Rushabh Mehta0dcb8612016-05-31 07:22:37 +0530140 if(!data) data = [];
Vinayak Jethe549c1962018-01-24 19:47:59 +0530141
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530142 data.forEach(function(d) {
Nabin Haita6746402018-03-28 11:16:00 +0530143 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 +0530144 d.pending_qty = 0;
Nabin Hait2c7a6e62018-03-12 14:12:12 +0530145 d.total_reserved = d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract;
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530146 if(d.actual_or_pending > d.actual_qty) {
147 d.pending_qty = d.actual_or_pending - d.actual_qty;
148 }
149
150 max_count = Math.max(d.actual_or_pending, d.actual_qty,
151 d.total_reserved, max_count);
152 });
Vinayak Jethe549c1962018-01-24 19:47:59 +0530153
marination1087d972020-11-26 10:45:44 +0530154 let can_write = 0;
Vinayak Jethe549c1962018-01-24 19:47:59 +0530155 if(frappe.boot.user.can_write.indexOf("Stock Entry")>=0){
156 can_write = 1;
157 }
158
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530159 return {
160 data: data,
161 max_count: max_count,
Vinayak Jethe549c1962018-01-24 19:47:59 +0530162 can_write:can_write,
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530163 show_item: show_item || false
164 }
marination1087d972020-11-26 10:45:44 +0530165 },
166
167 get_capacity_dashboard_data: function(data) {
168 if(!data) data = [];
169
170 data.forEach(function(d) {
171 d.color = d.percent_occupied >=80 ? "#f8814f" : "#2490ef";
172 });
173
174 let can_write = 0;
175 if(frappe.boot.user.can_write.indexOf("Putaway Rule")>=0){
176 can_write = 1;
177 }
178
179 return {
180 data: data,
181 can_write: can_write,
182 }
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530183 }
Shivam Mishra444091b2019-07-10 10:22:08 +0530184})
185
186erpnext.stock.move_item = function(item, source, target, actual_qty, rate, callback) {
187 var dialog = new frappe.ui.Dialog({
188 title: target ? __('Add Item') : __('Move Item'),
189 fields: [
190 {fieldname: 'item_code', label: __('Item'),
191 fieldtype: 'Link', options: 'Item', read_only: 1},
192 {fieldname: 'source', label: __('Source Warehouse'),
193 fieldtype: 'Link', options: 'Warehouse', read_only: 1},
194 {fieldname: 'target', label: __('Target Warehouse'),
195 fieldtype: 'Link', options: 'Warehouse', reqd: 1},
196 {fieldname: 'qty', label: __('Quantity'), reqd: 1,
197 fieldtype: 'Float', description: __('Available {0}', [actual_qty]) },
198 {fieldname: 'rate', label: __('Rate'), fieldtype: 'Currency', hidden: 1 },
199 ],
200 })
201 dialog.show();
202 dialog.get_field('item_code').set_input(item);
203
204 if(source) {
205 dialog.get_field('source').set_input(source);
206 } else {
207 dialog.get_field('source').df.hidden = 1;
208 dialog.get_field('source').refresh();
209 }
210
211 if(rate) {
212 dialog.get_field('rate').set_value(rate);
213 dialog.get_field('rate').df.hidden = 0;
214 dialog.get_field('rate').refresh();
215 }
216
217 if(target) {
218 dialog.get_field('target').df.read_only = 1;
219 dialog.get_field('target').value = target;
220 dialog.get_field('target').refresh();
221 }
222
223 dialog.set_primary_action(__('Submit'), function() {
224 var values = dialog.get_values();
225 if(!values) {
226 return;
227 }
228 if(source && values.qty > actual_qty) {
229 frappe.msgprint(__('Quantity must be less than or equal to {0}', [actual_qty]));
230 return;
231 }
232 if(values.source === values.target) {
233 frappe.msgprint(__('Source and target warehouse must be different'));
234 }
235
236 frappe.call({
237 method: 'erpnext.stock.doctype.stock_entry.stock_entry_utils.make_stock_entry',
238 args: values,
239 freeze: true,
240 callback: function(r) {
241 frappe.show_alert(__('Stock Entry {0} created',
242 ['<a href="#Form/Stock Entry/'+r.message.name+'">' + r.message.name+ '</a>']));
243 dialog.hide();
244 callback(r);
245 },
246 });
247 });
248
249 $('<p style="margin-left: 10px;"><a class="link-open text-muted small">'
250 + __("Add more items or open full form") + '</a></p>')
251 .appendTo(dialog.body)
252 .find('.link-open')
253 .on('click', function() {
254 frappe.model.with_doctype('Stock Entry', function() {
255 var doc = frappe.model.get_new_doc('Stock Entry');
256 doc.from_warehouse = dialog.get_value('source');
257 doc.to_warehouse = dialog.get_value('target');
258 var row = frappe.model.add_child(doc, 'items');
259 row.item_code = dialog.get_value('item_code');
260 row.f_warehouse = dialog.get_value('target');
261 row.t_warehouse = dialog.get_value('target');
262 row.qty = dialog.get_value('qty');
263 row.conversion_factor = 1;
264 row.transfer_qty = dialog.get_value('qty');
265 row.basic_rate = dialog.get_value('rate');
266 frappe.set_route('Form', doc.doctype, doc.name);
267 })
268 });
Shivam Mishra7339c722019-07-16 13:19:13 +0530269}