blob: e7e89cec6b546017c15f4eb59096b39a41fd0d8f [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
19 // move
20 this.content.on('click', '.btn-move', function() {
21 erpnext.stock.move_item($(this).attr('data-item'), $(this).attr('data-warehouse'),
22 null, $(this).attr('data-actual_qty'), null, function() { me.refresh(); });
23 });
24
25 this.content.on('click', '.btn-add', function() {
26 erpnext.stock.move_item($(this).attr('data-item'), null, $(this).attr('data-warehouse'),
27 $(this).attr('data-actual_qty'), $(this).attr('data-rate'),
28 function() { me.refresh(); });
29 });
30
31 // more
32 this.content.find('.btn-more').on('click', function() {
33 me.start += 20;
34 me.refresh();
35 });
36
37 },
38 refresh: function() {
39 if(this.before_refresh) {
40 this.before_refresh();
41 }
42
43 var me = this;
44 frappe.call({
45 method: 'erpnext.stock.dashboard.item_dashboard.get_data',
46 args: {
47 item_code: this.item_code,
48 warehouse: this.warehouse,
49 start: this.start,
50 sort_by: this.sort_by,
51 sort_order: this.sort_order,
52 },
53 callback: function(r) {
54 me.render(r.message);
55 }
56 });
57 },
58 render: function(data) {
59 if(this.start===0) {
60 this.max_count = 0;
61 this.result.empty();
62 }
63
64 var context = this.get_item_dashboard_data(data, this.max_count, true);
65 this.max_count = this.max_count;
66
67 // show more button
68 if(data.length===21) {
69 this.content.find('.more').removeClass('hidden');
70
71 // remove the last element
72 data.splice(-1);
73 } else {
74 this.content.find('.more').addClass('hidden');
75 }
76
77 $(frappe.render_template('item_dashboard_list', context)).appendTo(this.result);
78
79 },
80 get_item_dashboard_data: function(data, max_count, show_item) {
81 if(!max_count) max_count = 0;
82 data.forEach(function(d) {
83 d.actual_or_pending = d.projected_qty + d.reserved_qty + d.reserved_qty_for_production;
84 d.pending_qty = 0;
85 d.total_reserved = d.reserved_qty + d.reserved_qty_for_production;
86 if(d.actual_or_pending > d.actual_qty) {
87 d.pending_qty = d.actual_or_pending - d.actual_qty;
88 }
89
90 max_count = Math.max(d.actual_or_pending, d.actual_qty,
91 d.total_reserved, max_count);
92 });
93 return {
94 data: data,
95 max_count: max_count,
96 show_item: show_item || false
97 }
98 }
99})
100
101erpnext.stock.move_item = function(item, source, target, actual_qty, rate, callback) {
102 var dialog = new frappe.ui.Dialog({
103 title: target ? __('Add Item') : __('Move Item'),
104 fields: [
105 {fieldname: 'item_code', label: __('Item'),
106 fieldtype: 'Link', options: 'Item', read_only: 1},
107 {fieldname: 'source', label: __('Source Warehouse'),
108 fieldtype: 'Link', options: 'Warehouse', read_only: 1},
109 {fieldname: 'target', label: __('Target Warehouse'),
110 fieldtype: 'Link', options: 'Warehouse', reqd: 1},
111 {fieldname: 'qty', label: __('Quantity'), reqd: 1,
112 fieldtype: 'Float', description: __('Available {0}', [actual_qty]) },
113 {fieldname: 'rate', label: __('Rate'), fieldtype: 'Currency', hidden: 1 },
114 ],
115 })
116 dialog.show();
117 dialog.get_field('item_code').set_input(item);
118
119 if(source) {
120 dialog.get_field('source').set_input(source);
121 } else {
122 dialog.get_field('source').df.hidden = 1;
123 dialog.get_field('source').refresh();
124 }
125
126 if(rate) {
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530127 dialog.get_field('rate').set_value(rate);
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530128 dialog.get_field('rate').df.hidden = 0;
129 dialog.get_field('rate').refresh();
130 }
131
132 if(target) {
133 dialog.get_field('target').df.read_only = 1;
134 dialog.get_field('target').value = target;
135 dialog.get_field('target').refresh();
136 }
137
138 dialog.set_primary_action(__('Submit'), function() {
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530139 var values = dialog.get_values();
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530140 if(!values) {
141 return;
142 }
143 if(source && values.qty > actual_qty) {
144 frappe.msgprint(__('Quantity must be less than or equal to {0}', [actual_qty]));
145 return;
146 }
147 if(values.source === values.target) {
148 frappe.msgprint(__('Source and target warehouse must be different'));
149 }
150
151 frappe.call({
152 method: 'erpnext.stock.doctype.stock_entry.stock_entry_utils.make_stock_entry',
153 args: values,
154 callback: function(r) {
155 frappe.show_alert(__('Stock Entry {0} created',
156 ['<a href="#Form/Stock Entry/'+r.message.name+'">' + r.message.name+ '</a>']));
157 dialog.hide();
158 callback(r);
159 },
160 });
161 });
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530162
163 $('<p><a class="link-open">' + __("Add more items or open form") + '</a></p>')
164 .appendTo(dialog.body)
165 .find('.link-open')
166 .on('click', function() {
167 var doc = frappe.new_doc('Stock Entry');
168 doc.from_warehouse = dialog.get_value('source');
169 doc.to_warehouse = dialog.get_value('target');
170 row = frappe.model.add_child(doc, 'items');
171 row.item_code = dialog.get_value('item_code');
172 row.qty = dialog.get_value('qty');
173 row.basic_rate = dialog.get_value('rate');
174 frappe.set_route('Form', doc.doctype, doc.name);
175 });
Rushabh Mehta3d2622c2016-04-25 17:53:42 +0530176}