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