blob: b09b715275eae7b2ece36da695d34257cfa54e51 [file] [log] [blame]
Rushabh Mehta3d2622c2016-04-25 17:53:42 +05301frappe.provide('erpnext.stock');
2
Faris Ansari1fe891b2021-04-23 08:04:00 +05303erpnext.stock.ItemDashboard = class ItemDashboard {
4 constructor(opts) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +05305 $.extend(this, opts);
6 this.make();
Faris Ansari1fe891b2021-04-23 08:04:00 +05307 }
8 make() {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +05309 var me = this;
10 this.start = 0;
Anurag Mishraabf974d2021-03-17 18:40:21 +053011 if (!this.sort_by) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053012 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
Anurag Mishraabf974d2021-03-17 18:40:21 +053019 this.content.on('click', '.btn-move', function () {
20 handle_move_add($(this), "Move");
Shivam Mishra38b930b2019-07-05 10:38:48 +053021 });
22
Anurag Mishraabf974d2021-03-17 18:40:21 +053023 this.content.on('click', '.btn-add', function () {
24 handle_move_add($(this), "Add");
Shivam Mishra5738c932019-07-16 12:52:19 +053025 });
26
Anurag Mishraabf974d2021-03-17 18:40:21 +053027 this.content.on('click', '.btn-edit', function () {
marination1087d972020-11-26 10:45:44 +053028 let item = unescape($(this).attr('data-item'));
29 let warehouse = unescape($(this).attr('data-warehouse'));
30 let company = unescape($(this).attr('data-company'));
Anurag Mishraabf974d2021-03-17 18:40:21 +053031 frappe.db.get_value('Putaway Rule', {
32 'item_code': item,
33 'warehouse': warehouse,
34 'company': company
35 }, 'name', (r) => {
36 frappe.set_route("Form", "Putaway Rule", r.name);
37 });
marination1087d972020-11-26 10:45:44 +053038 });
39
Shivam Mishra5738c932019-07-16 12:52:19 +053040 function handle_move_add(element, action) {
41 let item = unescape(element.attr('data-item'));
42 let warehouse = unescape(element.attr('data-warehouse'));
43 let actual_qty = unescape(element.attr('data-actual_qty'));
44 let disable_quick_entry = Number(unescape(element.attr('data-disable_quick_entry')));
s-aga-rdc0ddf82023-02-03 18:08:34 +053045 let entry_type = action === "Move" ? "Material Transfer" : "Material Receipt";
Shivam Mishra444091b2019-07-10 10:22:08 +053046
Shivam Mishrafd6ff622019-07-16 12:32:50 +053047 if (disable_quick_entry) {
Shivam Mishra5738c932019-07-16 12:52:19 +053048 open_stock_entry(item, warehouse, entry_type);
Shivam Mishrafd6ff622019-07-16 12:32:50 +053049 } else {
Shivam Mishra5738c932019-07-16 12:52:19 +053050 if (action === "Add") {
51 let rate = unescape($(this).attr('data-rate'));
Anurag Mishraabf974d2021-03-17 18:40:21 +053052 erpnext.stock.move_item(item, null, warehouse, actual_qty, rate, function () {
53 me.refresh();
54 });
55 } else {
56 erpnext.stock.move_item(item, warehouse, null, actual_qty, null, function () {
57 me.refresh();
58 });
Shivam Mishra5738c932019-07-16 12:52:19 +053059 }
Shivam Mishrafd6ff622019-07-16 12:32:50 +053060 }
Shivam Mishra5738c932019-07-16 12:52:19 +053061 }
Shivam Mishra38b930b2019-07-05 10:38:48 +053062
63 function open_stock_entry(item, warehouse, entry_type) {
Anurag Mishraabf974d2021-03-17 18:40:21 +053064 frappe.model.with_doctype('Stock Entry', function () {
Shivam Mishra38b930b2019-07-05 10:38:48 +053065 var doc = frappe.model.get_new_doc('Stock Entry');
s-aga-rdc0ddf82023-02-03 18:08:34 +053066 if (entry_type) {
67 doc.stock_entry_type = entry_type;
68 }
Shivam Mishra38b930b2019-07-05 10:38:48 +053069
70 var row = frappe.model.add_child(doc, 'items');
71 row.item_code = item;
s-aga-rdc0ddf82023-02-03 18:08:34 +053072
73 if (entry_type === "Material Transfer") {
74 row.s_warehouse = warehouse;
75 }
76 else {
77 row.t_warehouse = warehouse;
78 }
Shivam Mishra38b930b2019-07-05 10:38:48 +053079
80 frappe.set_route('Form', doc.doctype, doc.name);
Anurag Mishraabf974d2021-03-17 18:40:21 +053081 });
Shivam Mishra38b930b2019-07-05 10:38:48 +053082 }
83
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053084 // more
Anurag Mishraabf974d2021-03-17 18:40:21 +053085 this.content.find('.btn-more').on('click', function () {
marinationa5d8d322020-12-09 16:27:18 +053086 me.start += me.page_length;
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053087 me.refresh();
88 });
89
Faris Ansari1fe891b2021-04-23 08:04:00 +053090 }
91 refresh() {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +053092 if(this.before_refresh) {
93 this.before_refresh();
94 }
95
marination1087d972020-11-26 10:45:44 +053096 let args = {
97 item_code: this.item_code,
98 warehouse: this.warehouse,
99 parent_warehouse: this.parent_warehouse,
100 item_group: this.item_group,
101 company: this.company,
102 start: this.start,
103 sort_by: this.sort_by,
104 sort_order: this.sort_order
marination0f3cfc52020-12-08 19:11:51 +0530105 };
marination1087d972020-11-26 10:45:44 +0530106
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530107 var me = this;
108 frappe.call({
marination1087d972020-11-26 10:45:44 +0530109 method: this.method,
110 args: args,
Anurag Mishraabf974d2021-03-17 18:40:21 +0530111 callback: function (r) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530112 me.render(r.message);
Devin Slauenwhite123920d2022-12-29 00:01:26 -0500113 if(me.after_refresh) {
114 me.after_refresh();
115 }
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530116 }
117 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530118 }
119 render(data) {
marination1087d972020-11-26 10:45:44 +0530120 if (this.start===0) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530121 this.max_count = 0;
122 this.result.empty();
123 }
124
marination0f3cfc52020-12-08 19:11:51 +0530125 let context = "";
marination1087d972020-11-26 10:45:44 +0530126 if (this.page_name === "warehouse-capacity-summary") {
marination0f3cfc52020-12-08 19:11:51 +0530127 context = this.get_capacity_dashboard_data(data);
marination1087d972020-11-26 10:45:44 +0530128 } else {
marination0f3cfc52020-12-08 19:11:51 +0530129 context = this.get_item_dashboard_data(data, this.max_count, true);
marination1087d972020-11-26 10:45:44 +0530130 }
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530131
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530132 this.max_count = this.max_count;
133
134 // show more button
Anurag Mishraabf974d2021-03-17 18:40:21 +0530135 if (data && data.length === (this.page_length + 1)) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530136 this.content.find('.more').removeClass('hidden');
137
138 // remove the last element
139 data.splice(-1);
140 } else {
141 this.content.find('.more').addClass('hidden');
142 }
143
Vinayak Jethe2c8ce5a2018-01-24 13:51:26 +0530144 // If not any stock in any warehouses provide a message to end user
Stavros Anastasiadis58b58782017-10-05 16:14:50 +0300145 if (context.data.length > 0) {
marination1087d972020-11-26 10:45:44 +0530146 this.content.find('.result').css('text-align', 'unset');
147 $(frappe.render_template(this.template, context)).appendTo(this.result);
Stavros Anastasiadis58b58782017-10-05 16:14:50 +0300148 } else {
marination1087d972020-11-26 10:45:44 +0530149 var message = __("No Stock Available Currently");
150 this.content.find('.result').css('text-align', 'center');
151
marinationa97eb6a2021-02-07 22:27:40 +0530152 $(`<div class='text-muted' style='margin: 20px 5px;'>
marination1087d972020-11-26 10:45:44 +0530153 ${message} </div>`).appendTo(this.result);
Stavros Anastasiadis58b58782017-10-05 16:14:50 +0300154 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530155 }
marination1087d972020-11-26 10:45:44 +0530156
Faris Ansari1fe891b2021-04-23 08:04:00 +0530157 get_item_dashboard_data(data, max_count, show_item) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530158 if(!max_count) max_count = 0;
Rushabh Mehta0dcb8612016-05-31 07:22:37 +0530159 if(!data) data = [];
Vinayak Jethe549c1962018-01-24 19:47:59 +0530160
Anurag Mishraabf974d2021-03-17 18:40:21 +0530161 data.forEach(function (d) {
Nabin Haita6746402018-03-28 11:16:00 +0530162 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 +0530163 d.pending_qty = 0;
Nabin Hait2c7a6e62018-03-12 14:12:12 +0530164 d.total_reserved = d.reserved_qty + d.reserved_qty_for_production + d.reserved_qty_for_sub_contract;
Anurag Mishraabf974d2021-03-17 18:40:21 +0530165 if (d.actual_or_pending > d.actual_qty) {
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530166 d.pending_qty = d.actual_or_pending - d.actual_qty;
167 }
168
169 max_count = Math.max(d.actual_or_pending, d.actual_qty,
170 d.total_reserved, max_count);
171 });
Vinayak Jethe549c1962018-01-24 19:47:59 +0530172
marination1087d972020-11-26 10:45:44 +0530173 let can_write = 0;
marination0f3cfc52020-12-08 19:11:51 +0530174 if (frappe.boot.user.can_write.indexOf("Stock Entry") >= 0) {
Vinayak Jethe549c1962018-01-24 19:47:59 +0530175 can_write = 1;
176 }
177
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530178 return {
179 data: data,
180 max_count: max_count,
Anurag Mishraabf974d2021-03-17 18:40:21 +0530181 can_write: can_write,
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530182 show_item: show_item || false
marinationb8aeb9e2021-01-05 12:16:25 +0530183 };
Faris Ansari1fe891b2021-04-23 08:04:00 +0530184 }
marination1087d972020-11-26 10:45:44 +0530185
Faris Ansari1fe891b2021-04-23 08:04:00 +0530186 get_capacity_dashboard_data(data) {
marination0f3cfc52020-12-08 19:11:51 +0530187 if (!data) data = [];
marination1087d972020-11-26 10:45:44 +0530188
Anurag Mishraabf974d2021-03-17 18:40:21 +0530189 data.forEach(function (d) {
190 d.color = d.percent_occupied >= 80 ? "#f8814f" : "#2490ef";
marination1087d972020-11-26 10:45:44 +0530191 });
192
193 let can_write = 0;
marination0f3cfc52020-12-08 19:11:51 +0530194 if (frappe.boot.user.can_write.indexOf("Putaway Rule") >= 0) {
marination1087d972020-11-26 10:45:44 +0530195 can_write = 1;
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530196 }
marination1087d972020-11-26 10:45:44 +0530197
198 return {
199 data: data,
200 can_write: can_write,
marination0f3cfc52020-12-08 19:11:51 +0530201 };
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530202 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530203};
Shivam Mishra444091b2019-07-10 10:22:08 +0530204
Anurag Mishraabf974d2021-03-17 18:40:21 +0530205erpnext.stock.move_item = function (item, source, target, actual_qty, rate, callback) {
Shivam Mishra444091b2019-07-10 10:22:08 +0530206 var dialog = new frappe.ui.Dialog({
207 title: target ? __('Add Item') : __('Move Item'),
Anurag Mishraabf974d2021-03-17 18:40:21 +0530208 fields: [{
209 fieldname: 'item_code',
210 label: __('Item'),
211 fieldtype: 'Link',
212 options: 'Item',
213 read_only: 1
214 },
215 {
216 fieldname: 'source',
217 label: __('Source Warehouse'),
218 fieldtype: 'Link',
219 options: 'Warehouse',
220 read_only: 1
221 },
222 {
223 fieldname: 'target',
224 label: __('Target Warehouse'),
225 fieldtype: 'Link',
226 options: 'Warehouse',
Ankush Menat88a21ca2022-01-26 01:01:10 +0530227 reqd: 1,
228 get_query() {
229 return {
230 filters: {
231 is_group: 0
232 }
233 }
234 }
Anurag Mishraabf974d2021-03-17 18:40:21 +0530235 },
236 {
237 fieldname: 'qty',
238 label: __('Quantity'),
239 reqd: 1,
240 fieldtype: 'Float',
241 description: __('Available {0}', [actual_qty])
242 },
243 {
244 fieldname: 'rate',
245 label: __('Rate'),
246 fieldtype: 'Currency',
247 hidden: 1
248 },
Shivam Mishra444091b2019-07-10 10:22:08 +0530249 ],
Anurag Mishraabf974d2021-03-17 18:40:21 +0530250 });
Shivam Mishra444091b2019-07-10 10:22:08 +0530251 dialog.show();
252 dialog.get_field('item_code').set_input(item);
253
Anurag Mishraabf974d2021-03-17 18:40:21 +0530254 if (source) {
Shivam Mishra444091b2019-07-10 10:22:08 +0530255 dialog.get_field('source').set_input(source);
256 } else {
257 dialog.get_field('source').df.hidden = 1;
258 dialog.get_field('source').refresh();
259 }
260
Anurag Mishraabf974d2021-03-17 18:40:21 +0530261 if (rate) {
Shivam Mishra444091b2019-07-10 10:22:08 +0530262 dialog.get_field('rate').set_value(rate);
263 dialog.get_field('rate').df.hidden = 0;
264 dialog.get_field('rate').refresh();
265 }
266
Anurag Mishraabf974d2021-03-17 18:40:21 +0530267 if (target) {
Shivam Mishra444091b2019-07-10 10:22:08 +0530268 dialog.get_field('target').df.read_only = 1;
269 dialog.get_field('target').value = target;
270 dialog.get_field('target').refresh();
271 }
272
Ankush Menat0bed5922022-01-26 01:01:33 +0530273 dialog.set_primary_action(__('Create Stock Entry'), function () {
Devin Slauenwhitecdf631b2022-06-29 02:36:47 -0400274 if (source && (dialog.get_value("qty") == 0 || dialog.get_value("qty") > actual_qty)) {
275 frappe.msgprint(__("Quantity must be greater than zero, and less or equal to {0}", [actual_qty]));
276 return;
277 }
278
279 if (dialog.get_value("source") === dialog.get_value("target")) {
280 frappe.msgprint(__("Source and target warehouse must be different"));
281 return;
282 }
283
Ankush Menat0bed5922022-01-26 01:01:33 +0530284 frappe.model.with_doctype('Stock Entry', function () {
285 let doc = frappe.model.get_new_doc('Stock Entry');
286 doc.from_warehouse = dialog.get_value('source');
287 doc.to_warehouse = dialog.get_value('target');
288 doc.stock_entry_type = doc.from_warehouse ? "Material Transfer" : "Material Receipt";
289 let row = frappe.model.add_child(doc, 'items');
290 row.item_code = dialog.get_value('item_code');
Ankush Menat3cc47d52022-02-28 13:04:24 +0530291 row.s_warehouse = dialog.get_value('source');
Ankush Menat0bed5922022-01-26 01:01:33 +0530292 row.t_warehouse = dialog.get_value('target');
293 row.qty = dialog.get_value('qty');
294 row.conversion_factor = 1;
295 row.transfer_qty = dialog.get_value('qty');
296 row.basic_rate = dialog.get_value('rate');
297 frappe.set_route('Form', doc.doctype, doc.name);
Shivam Mishra444091b2019-07-10 10:22:08 +0530298 });
299 });
Anurag Mishraabf974d2021-03-17 18:40:21 +0530300};