PartPicker: Mehrfachauswahl: Ursprüngliche Filter für Dialog merken.
[kivitendo-erp.git] / js / kivi.Part.js
1 namespace('kivi.Part', function(ns) {
2   'use strict';
3
4   ns.open_history_popup = function() {
5     var id = $("#part_id").val();
6     kivi.popup_dialog({
7       url:    'controller.pl?action=Part/history&part.id=' + id,
8       dialog: { title: kivi.t8('History') },
9     });
10   }
11
12   ns.save = function() {
13     var data = $('#ic').serializeArray();
14     data.push({ name: 'action', value: 'Part/save' });
15
16     $.post("controller.pl", data, kivi.eval_json_result);
17   };
18
19   ns.use_as_new = function() {
20     var oldid = $("#part_id").val();
21     $('#ic').attr('action', 'controller.pl?action=Part/use_as_new&old_id=' + oldid);
22     $('#ic').submit();
23   };
24
25   ns.delete = function() {
26     var data = $('#ic').serializeArray();
27     data.push({ name: 'action', value: 'Part/delete' });
28
29     $.post("controller.pl", data, kivi.eval_json_result);
30   };
31
32   ns.reformat_number = function(event) {
33     $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
34   };
35
36   ns.set_tab_active_by_index = function (index) {
37     $("#ic_tabs").tabs({active: index})
38   };
39
40   ns.set_tab_active_by_name= function (name) {
41     var index = $('#ic_tabs a[href=#' + name + ']').parent().index();
42     ns.set_tab_active_by_index(index);
43   };
44
45   ns.reorder_items = function(order_by) {
46     var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
47     var part_type = $("#part_part_type").val();
48
49     var data;
50     if (part_type === 'assortment') {
51       $('#assortment thead a img').remove();
52       data = $('#assortment :input').serializeArray();
53     } else if ( part_type === 'assembly') {
54       $('#assembly thead a img').remove();
55       data = $('#assembly :input').serializeArray();
56     }
57
58     var src;
59     if (dir == "1") {
60       dir = "0";
61       src = "image/up.png";
62     } else {
63       dir = "1";
64       src = "image/down.png";
65     }
66
67     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
68
69     data.push({ name: 'action',    value: 'Part/reorder_items' },
70               { name: 'order_by',  value: order_by             },
71               { name: 'part_type', value: part_type            },
72               { name: 'sort_dir',  value: dir                  });
73
74     $.post("controller.pl", data, kivi.eval_json_result);
75   };
76
77   ns.assortment_recalc = function() {
78     var data = $('#assortment :input').serializeArray();
79     data.push({ name: 'action', value: 'Part/update_item_totals' },
80               { name: 'part_type', value: 'assortment'                   });
81
82     $.post("controller.pl", data, kivi.eval_json_result);
83   };
84
85   ns.assembly_recalc = function() {
86     var data = $('#assembly :input').serializeArray();
87     data.push( { name: 'action',    value: 'Part/update_item_totals' },
88                { name: 'part_type', value: 'assembly'                        });
89
90     $.post("controller.pl", data, kivi.eval_json_result);
91   };
92
93   ns.set_assortment_sellprice = function() {
94     $("#part_sellprice_as_number").val($("#items_sellprice_sum").html());
95     // ns.set_tab_active_by_name('basic_data');
96     // $("#part_sellprice_as_number").focus();
97   };
98
99   ns.set_assortment_lsg_sellprice = function() {
100     $("#items_lsg_sellprice_sum_basic").closest('td').find('input').val($("#items_lsg_sellprice_sum").html());
101   };
102
103   ns.set_assortment_douglas_sellprice = function() {
104     $("#items_douglas_sellprice_sum_basic").closest('td').find('input').val($("#items_douglas_sellprice_sum").html());
105   };
106
107   ns.set_assortment_lastcost = function() {
108     $("#part_lastcost_as_number").val($("#items_lastcost_sum").html());
109     // ns.set_tab_active_by_name('basic_data');
110     // $("#part_lastcost_as_number").focus();
111   };
112
113   ns.set_assembly_sellprice = function() {
114     $("#part_sellprice_as_number").val($("#items_sellprice_sum").html());
115     // ns.set_tab_active_by_name('basic_data');
116     // $("#part_sellprice_as_number").focus();
117   };
118
119   ns.renumber_positions = function() {
120     var part_type = $("#part_part_type").val();
121     var rows;
122     if (part_type === 'assortment') {
123       rows = $('.assortment_item_row [name="position"]');
124     } else if ( part_type === 'assembly') {
125       rows = $('.assembly_item_row [name="position"]');
126     }
127     $(rows).each(function(idx, elt) {
128       $(elt).html(idx+1);
129       var row = $(elt).closest('tr');
130       if ( idx % 2 === 0 ) {
131         if ( row.hasClass('listrow1') ) {
132           row.removeClass('listrow1');
133           row.addClass('listrow0');
134         }
135       } else {
136         if ( row.hasClass('listrow0') ) {
137           row.removeClass('listrow0');
138           row.addClass('listrow1');
139         }
140       }
141     });
142   };
143
144   ns.delete_item_row = function(clicked) {
145     var row = $(clicked).closest('tr');
146     $(row).remove();
147     var part_type = $("#part_part_type").val();
148     ns.renumber_positions();
149     if (part_type === 'assortment') {
150       ns.assortment_recalc();
151     } else if ( part_type === 'assembly') {
152       ns.assembly_recalc();
153     }
154   };
155
156   ns.add_assortment_item = function() {
157     if ($('#assortment_picker').val() === '') return;
158
159     $('#row_table_id thead a img').remove();
160
161     var data = $('#assortment :input').serializeArray();
162     data.push({ name: 'action', value: 'Part/add_assortment_item' },
163               { name: 'part.id', value: $('#part_id').val()       },
164               { name: 'part.part_type', value: 'assortment'       });
165     $('#assortment_picker').data('part_picker').clear();
166
167     $.post("controller.pl", data, kivi.eval_json_result);
168   };
169
170   ns.add_assembly_item = function() {
171     if ($('#assembly_picker').val() === '') return;
172
173     var data = $('#assembly :input').serializeArray();
174     data.push({ name: 'action', value: 'Part/add_assembly_item' },
175               { name: 'part.id', value: $("#part_id").val()     },
176               { name: 'part.part_type', value: 'assembly'       });
177     $('#assembly_picker').data('part_picker').clear();
178
179     $.post("controller.pl", data, kivi.eval_json_result);
180   };
181
182   ns.set_multi_assembly_items = function(data) {
183     data.push({ name: 'part.id',        value: $('#part_id').val() });
184     data.push({ name: 'part.part_type', value: $('#part_part_type').val() });
185     $.post("controller.pl?action=Part/add_multi_assembly_items", data, kivi.eval_json_result);
186   }
187
188   ns.set_multi_assortment_items = function(data) {
189     data.push({ name: 'part.id', value: $('#part_id').val() });
190     data.push({ name: 'part.part_type', value: $('#part_part_type').val() });
191     $.post("controller.pl?action=Part/add_multi_assortment_items", data, kivi.eval_json_result);
192   }
193
194   ns.close_picker_dialogs = function() {
195     $('.part_autocomplete').each(function(_, e) {
196       var picker = $(e).data('part_picker');
197       if (picker && picker.dialog) picker.close_dialog();
198     });
199   }
200
201   ns.redisplay_items = function(data) {
202     var old_rows;
203     var part_type = $("#part_part_type").val();
204     if (part_type === 'assortment') {
205       old_rows = $('.assortment_item_row').detach();
206     } else if ( part_type === 'assembly') {
207       old_rows = $('.assembly_item_row').detach();
208     }
209     var new_rows = [];
210     $(data).each(function(idx, elt) {
211       new_rows.push(old_rows[elt.old_pos - 1]);
212     });
213     if (part_type === 'assortment') {
214       $(new_rows).appendTo($('#assortment_items'));
215     } else if ( part_type === 'assembly') {
216       $(new_rows).appendTo($('#assembly_items'));
217     }
218     ns.renumber_positions();
219   };
220
221   ns.focus_last_assortment_input = function () {
222     $("#assortment_items tr:last").find('input[type=text]').filter(':visible:first').focus();
223   };
224
225   ns.focus_last_assembly_input = function () {
226     $("#assembly_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
227   };
228
229   // makemodel
230   ns.makemodel_renumber_positions = function() {
231     $('.makemodel_row [name="position"]').each(function(idx, elt) {
232       $(elt).html(idx+1);
233     });
234   };
235
236   ns.delete_makemodel_row = function(clicked) {
237     var row = $(clicked).closest('tr');
238     $(row).remove();
239
240     ns.makemodel_renumber_positions();
241   };
242
243   ns.add_makemodel_row = function() {
244     if ($('#add_makemodel').val() === '') return;
245
246     var data = $('#makemodel_table :input').serializeArray();
247     data.push({ name: 'action', value: 'Part/add_makemodel_row' });
248     $('#add_makemodel').data('customer_vendor_picker').clear();
249
250     $.post("controller.pl", data, kivi.eval_json_result);
251   };
252
253   ns.focus_last_makemodel_input = function () {
254     $("#makemodel_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
255   };
256
257
258   // customerprice
259   ns.customerprice_renumber_positions = function() {
260     $('.customerprice_row [name="position"]').each(function(idx, elt) {
261       $(elt).html(idx+1);
262     });
263   };
264
265   ns.delete_customerprice_row = function(clicked) {
266     var row = $(clicked).closest('tr');
267     $(row).remove();
268
269     ns.customerprice_renumber_positions();
270   };
271
272   ns.add_customerprice_row = function() {
273     if ($('#add_customerprice').val() === '') return;
274
275     var data = $('#customerprice_table :input').serializeArray();
276     data.push({ name: 'action', value: 'Part/add_customerprice_row' });
277     $('#add_customerprice').data('customer_vendor_picker').clear();
278
279     $.post("controller.pl", data, kivi.eval_json_result);
280   };
281
282   ns.focus_last_customerprice_input = function () {
283     $("#customerprice_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
284   };
285
286
287   ns.reload_bin_selection = function() {
288     $.post("controller.pl", { action: 'Part/warehouse_changed', warehouse_id: function(){ return $('#part_warehouse_id').val() } },   kivi.eval_json_result);
289   }
290
291   var KEY = {
292     TAB:       9,
293     ENTER:     13,
294     SHIFT:     16,
295     CTRL:      17,
296     ALT:       18,
297     ESCAPE:    27,
298     PAGE_UP:   33,
299     PAGE_DOWN: 34,
300     LEFT:      37,
301     UP:        38,
302     RIGHT:     39,
303     DOWN:      40,
304   };
305
306   ns.Picker = function($real, options) {
307     var self = this;
308     this.o = $.extend(true, {
309       limit: 20,
310       delay: 50,
311       action: {
312         commit_none: function(){ },
313         commit_one:  function(){ $('#update_button').click(); },
314         commit_many: function(){ self.open_dialog(); }
315       }
316     }, $real.data('part-picker-data'), options);
317     this.$real              = $real;
318     this.real_id            = $real.attr('id');
319     this.last_real          = $real.val();
320     this.$dummy             = $($real.siblings()[0]);
321     this.autocomplete_open  = false;
322     this.state              = this.STATES.PICKED;
323     this.last_dummy         = this.$dummy.val();
324     this.timer              = undefined;
325     this.dialog             = undefined;
326
327     this.init();
328   };
329
330   ns.Picker.prototype = {
331     CLASSES: {
332       PICKED:       'partpicker-picked',
333       UNDEFINED:    'partpicker-undefined',
334     },
335     ajax_data: function(term) {
336       var data = {
337         'filter.all:substr:multi::ilike': term,
338         current:  this.$real.val(),
339       };
340
341       if (this.o.part_type)
342         data['filter.part_type'] = this.o.part_type.split(',');
343
344       if (this.o.status) {
345         if (this.o.status == 'active')   data['filter.obsolete'] = 0;
346         if (this.o.status == 'obsolete') data['filter.obsolete'] = 1;
347       } else
348         data['filter.obsolete'] = 0;
349
350       if (this.o.classification_id)
351         data['filter.classification_id'] = this.o.classification_id.split(',');
352
353       if (this.o.unit)
354         data['filter.unit'] = this.o.unit.split(',');
355
356       if (this.o.convertible_unit)
357         data['filter.unit_obj.convertible_to'] = this.o.convertible_unit;
358
359       return data;
360     },
361     set_item: function(item) {
362       var self = this;
363       if (item.id) {
364         this.$real.val(item.id);
365         // autocomplete ui has name, use the value for ajax items, which contains displayable_name
366         this.$dummy.val(item.name ? item.name : item.value);
367       } else {
368         this.$real.val('');
369         this.$dummy.val('');
370       }
371       this.state      = this.STATES.PICKED;
372       this.last_real  = this.$real.val();
373       this.last_dummy = this.$dummy.val();
374       this.$real.trigger('change');
375
376       if (this.o.fat_set_item && item.id) {
377         $.ajax({
378           url: 'controller.pl?action=Part/show.json',
379           data: { 'part.id': item.id },
380           success: function(rsp) {
381             self.$real.trigger('set_item:PartPicker', rsp);
382           },
383         });
384       } else {
385         this.$real.trigger('set_item:PartPicker', item);
386       }
387       this.annotate_state();
388     },
389     set_multi_items: function(data) {
390       this.run_action(this.o.action.set_multi_items, [ data ]);
391     },
392     make_defined_state: function() {
393       if (this.state == this.STATES.PICKED) {
394         this.annotate_state();
395         return true
396       } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
397         this.set_item({})
398       else {
399         this.set_item({ id: this.last_real, name: this.last_dummy })
400       }
401       this.annotate_state();
402     },
403     annotate_state: function() {
404       if (this.state == this.STATES.PICKED)
405         this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
406       else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
407         this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
408       else {
409         this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
410       }
411     },
412     handle_changed_text: function(callbacks) {
413       var self = this;
414       $.ajax({
415         url: 'controller.pl?action=Part/ajax_autocomplete',
416         dataType: "json",
417         data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
418         success: function (data) {
419           if (data.length == 1) {
420             self.set_item(data[0]);
421             if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
422           } else if (data.length > 1) {
423             self.state = self.STATES.UNDEFINED;
424             if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
425           } else {
426             self.state = self.STATES.UNDEFINED;
427             if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
428           }
429           self.annotate_state();
430         }
431       });
432     },
433     /*  In case users are impatient and want to skip ahead:
434      *  Capture <enter> key events and check if it's a unique hit.
435      *  If it is, go ahead and assume it was selected. If it wasn't don't do
436      *  anything so that autocompletion kicks in.  For <tab> don't prevent
437      *  propagation. It would be nice to catch it, but javascript is too stupid
438      *  to fire a tab event later on, so we'd have to reimplement the "find
439      *  next active element in tabindex order and focus it".
440      */
441     /* note:
442      *  event.which does not contain tab events in keypressed in firefox but will report 0
443      *  chrome does not fire keypressed at all on tab or escape
444      */
445     handle_keydown: function(event) {
446       var self = this;
447       if (event.which == KEY.ENTER || event.which == KEY.TAB) {
448         // if string is empty assume they want to delete
449         if (self.$dummy.val() === '') {
450           self.set_item({});
451           return true;
452         } else if (self.state == self.STATES.PICKED) {
453           if (self.o.action.commit_one) {
454             self.run_action(self.o.action.commit_one);
455           }
456           return true;
457         }
458         if (event.which == KEY.TAB) {
459           event.preventDefault();
460           self.handle_changed_text();
461         }
462         if (event.which == KEY.ENTER) {
463           event.preventDefault();
464           self.handle_changed_text({
465             match_none: self.o.action.commit_none,
466             match_one:  self.o.action.commit_one,
467             match_many: self.o.action.commit_many
468           });
469           return false;
470         }
471       } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
472         var old_options = self.$dummy.autocomplete('option');
473         self.$dummy.autocomplete('option', 'minLength', 0);
474         self.$dummy.autocomplete('search', self.$dummy.val());
475         self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
476       } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
477         self.state = self.STATES.UNDEFINED;
478       }
479     },
480     open_dialog: function() {
481       if (this.o.multiple) {
482         this.dialog = new ns.PickerMultiPopup(this);
483       } else {
484         this.dialog = new ns.PickerPopup(this);
485       }
486     },
487     close_dialog: function() {
488       this.dialog.close_dialog();
489       this.dialog = undefined;
490     },
491     init: function() {
492       var self = this;
493       this.$dummy.autocomplete({
494         source: function(req, rsp) {
495           $.ajax($.extend(self.o, {
496             url:      'controller.pl?action=Part/ajax_autocomplete',
497             dataType: "json",
498             data:     self.ajax_data(req.term),
499             success:  function (data){ rsp(data) }
500           }));
501         },
502         select: function(event, ui) {
503           self.set_item(ui.item);
504           if (self.o.action.commit_one) {
505             self.run_action(self.o.action.commit_one);
506           }
507         },
508         search: function(event, ui) {
509           if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
510             event.preventDefault();
511         },
512         open: function() {
513           self.autocomplete_open = true;
514         },
515         close: function() {
516           self.autocomplete_open = false;
517         }
518       });
519       this.$dummy.keydown(function(event){ self.handle_keydown(event) });
520       this.$dummy.on('paste', function(){
521         setTimeout(function() {
522           self.handle_changed_text();
523         }, 1);
524       });
525       this.$dummy.blur(function(){
526         window.clearTimeout(self.timer);
527         self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
528       });
529
530       var popup_button = $('<span>').addClass('ppp_popup_button');
531       this.$dummy.after(popup_button);
532       popup_button.click(function() { self.open_dialog() });
533     },
534     run_action: function(code, args) {
535       if (typeof code === 'function')
536         code.apply(this, args)
537       else
538         kivi.run(code, args);
539     },
540     clear: function() {
541       this.set_item({});
542     }
543   };
544   ns.Picker.prototype.STATES = {
545     PICKED:    ns.Picker.prototype.CLASSES.PICKED,
546     UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
547   };
548
549   ns.PickerPopup = function(pp) {
550     this.timer = undefined;
551     this.pp    = pp;
552     this.open_dialog();
553   };
554
555   ns.PickerPopup.prototype = {
556     open_dialog: function() {
557       var self = this;
558       kivi.popup_dialog({
559         url: 'controller.pl?action=Part/part_picker_search',
560         data: self.pp.ajax_data(this.pp.$dummy.val()),
561         id: 'part_selection',
562         dialog: {
563           title: kivi.t8('Part picker'),
564           width: 800,
565           height: 800,
566         },
567         load: function() { self.init_search(); }
568       });
569       window.clearTimeout(this.timer);
570       return true;
571     },
572     init_search: function() {
573       var self = this;
574       $('#part_picker_filter').keypress(function(e) { self.result_timer(e) }).focus();
575       $('#no_paginate').change(function() { self.update_results() });
576       this.update_results();
577     },
578     update_results: function() {
579       var self = this;
580       $.ajax({
581         url: 'controller.pl?action=Part/part_picker_result',
582         data: $.extend({
583           no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
584         }, self.pp.ajax_data(function(){
585           var val = $('#part_picker_filter').val();
586           return val === undefined ? '' : val
587         })),
588         success: function(data){
589           $('#part_picker_result').html(data);
590           self.init_results();
591         }
592       });
593     },
594     init_results: function() {
595       var self = this;
596       $('div.part_picker_part').each(function(){
597         $(this).click(function(){
598           self.pp.set_item({
599             id:   $(this).children('input.part_picker_id').val(),
600             name: $(this).children('input.part_picker_description').val(),
601             classification_id: $(this).children('input.part_picker_classification_id').val(),
602             ean:  $(this).children('input.part_picker_ean').val(),
603             unit: $(this).children('input.part_picker_unit').val(),
604             partnumber:  $(this).children('input.part_picker_partnumber').val(),
605             description: $(this).children('input.part_picker_description').val(),
606           });
607           self.close_dialog();
608           self.pp.$dummy.focus();
609           return true;
610         });
611       });
612       $('#part_selection').keydown(function(e){
613          if (e.which == KEY.ESCAPE) {
614            self.close_dialog();
615            self.pp.$dummy.focus();
616          }
617       });
618     },
619     result_timer: function(event) {
620       var self = this;
621       if (!$('no_paginate').prop('checked')) {
622         if (event.keyCode == KEY.PAGE_UP) {
623           $('#part_picker_result a.paginate-prev').click();
624           return;
625         }
626         if (event.keyCode == KEY.PAGE_DOWN) {
627           $('#part_picker_result a.paginate-next').click();
628           return;
629         }
630       }
631       window.clearTimeout(this.timer);
632       if (event.which == KEY.ENTER) {
633         self.update_results();
634       } else {
635         this.timer = window.setTimeout(function() { self.update_results() }, 100);
636       }
637     },
638     close_dialog: function() {
639       $('#part_selection').dialog('close');
640     }
641   };
642
643   ns.PickerMultiPopup = function(pp) {
644     this.pp       = pp;
645     this.callback = 'Part/add_multi_' + this.pp.o.part_type + '_items';
646     this.open_dialog();
647   };
648
649   ns.PickerMultiPopup.prototype = {
650     open_dialog: function() {
651       var self = this;
652       $('#row_table_id thead a img').remove();
653
654       kivi.popup_dialog({
655         url: 'controller.pl?action=Part/show_multi_items_dialog',
656         data: $.extend({
657           real_id: self.pp.real_id,
658         }, self.pp.ajax_data(this.pp.$dummy.val())),
659         id: 'jq_multi_items_dialog',
660         dialog: {
661           title: kivi.t8('Add multiple items'),
662           width:  800,
663           height: 800
664         },
665         load: function() {
666           self.init_search();
667         }
668       });
669       return true;
670     },
671     init_search: function() {
672       var self = this;
673       $('#multi_items_filter_table input, #multi_items_filter_table select').keydown(function(event) {
674         if(event.which == KEY.ENTER) {
675           event.preventDefault();
676           self.update_results();
677           return false;
678         }
679       });
680
681       $('#multi_items_filter_all_substr_multi_ilike').focus();
682       $('#multi_items_filter_button').click(function(){ self.update_results() });
683       $('#multi_items_filter_reset').click(function(){ $("#multi_items_form").resetForm() });
684       $('#continue_button').click(function(){ self.add_multi_items() });
685     },
686     update_results: function() {
687       var self = this;
688       var data = $('#multi_items_form').serializeArray();
689       data.push({ name: 'type', value: self.pp.type });
690       var ppdata = self.pp.ajax_data(function(){
691         var val = $('#multi_items_filter').val();
692         return val === undefined ? '' : val
693       });
694       $.each(Object.keys(ppdata), function() {data.push({ name: 'multi_items.' + this, value: ppdata[this]});});
695       $.ajax({
696         url: 'controller.pl?action=Part/multi_items_update_result',
697         data: data,
698         method: 'post',
699         success: function(data){
700           $('#multi_items_result').html(data);
701           self.init_results();
702           self.enable_continue();
703         }
704       });
705     },
706     set_qty_to_one: function(clicked) {
707       if ($(clicked).val() === '') {
708         $(clicked).val(kivi.format_amount(1.00, -2));
709       }
710       $(clicked).select();
711     },
712     init_results: function() {
713       var self = this;
714       $('#multi_items_all_qty').change(function(event){
715         $('.multi_items_qty').val($(event.target).val());
716       });
717       $('.multi_items_qty').click(function(){ self.set_qty_to_one(this) });
718     },
719     result_timer: function(event) {
720     },
721     close_dialog: function() {
722       $('#jq_multi_items_dialog').dialog('close');
723     },
724     disable_continue: function() {
725       $('#multi_items_result input').off("keydown");
726       $('#continue_button').prop('disabled', true);
727     },
728     enable_continue: function() {
729       var self = this;
730       $('#multi_items_result input').keydown(function(event) {
731         if(event.keyCode == KEY.ENTER) {
732           event.preventDefault();
733           self.add_multi_items();
734           return false;
735         }
736       });
737       $('#continue_button').prop('disabled', false);
738     },
739     add_multi_items: function() {
740       // rows at all
741       var n_rows = $('.multi_items_qty').length;
742       if ( n_rows === 0) { return; }
743
744       // filled rows
745       n_rows = $('.multi_items_qty').filter(function() {
746         return $(this).val().length > 0;
747       }).length;
748       if (n_rows === 0) { return; }
749
750       this.disable_continue();
751
752       var data = $('#multi_items_form').serializeArray();
753       this.pp.set_multi_items(data);
754     }
755   };
756
757   ns.reinit_widgets = function() {
758     kivi.run_once_for('input.part_autocomplete', 'part_picker', function(elt) {
759       if (!$(elt).data('part_picker'))
760         $(elt).data('part_picker', new kivi.Part.Picker($(elt)));
761     });
762
763     kivi.run_once_for('#customerprice_rows', 'customerprice_row_sort_renumber', function(elt) {
764       $(elt).on('sortstop', kivi.Part.customerprice_renumber_positions);
765     });
766
767     kivi.run_once_for('#makemodel_rows', 'makemodel_row_sort_renumber', function(elt) {
768       $(elt).on('sortstop', kivi.Part.makemodel_renumber_positions);
769     });
770   }
771
772   ns.init = function() {
773     ns.reinit_widgets();
774   }
775
776   $(function(){
777     $('#ic').on('focusout', '.reformat_number', function(event) {
778        ns.reformat_number(event);
779     });
780
781     $('#part_warehouse_id').change(kivi.Part.reload_bin_selection);
782
783     ns.init();
784   });
785 });