Artikelstammdaten: Einfügen eines Kunden führt zur Fehlermeldung
[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
249     $.post("controller.pl", data, kivi.eval_json_result);
250   };
251
252   ns.focus_last_makemodel_input = function () {
253     $("#makemodel_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
254   };
255
256
257   // customerprice
258   ns.customerprice_renumber_positions = function() {
259     $('.customerprice_row [name="position"]').each(function(idx, elt) {
260       $(elt).html(idx+1);
261     });
262   };
263
264   ns.delete_customerprice_row = function(clicked) {
265     var row = $(clicked).closest('tr');
266     $(row).remove();
267
268     ns.customerprice_renumber_positions();
269   };
270
271   ns.add_customerprice_row = function() {
272     if ($('#add_customerprice').val() === '') return;
273
274     var data = $('#customerprice_table :input').serializeArray();
275     data.push({ name: 'action', value: 'Part/add_customerprice_row' });
276
277     $.post("controller.pl", data, kivi.eval_json_result);
278   };
279
280   ns.focus_last_customerprice_input = function () {
281     $("#customerprice_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
282   };
283
284
285   ns.reload_bin_selection = function() {
286     $.post("controller.pl", { action: 'Part/warehouse_changed', warehouse_id: function(){ return $('#part_warehouse_id').val() } },   kivi.eval_json_result);
287   }
288
289   var KEY = {
290     TAB:       9,
291     ENTER:     13,
292     SHIFT:     16,
293     CTRL:      17,
294     ALT:       18,
295     ESCAPE:    27,
296     PAGE_UP:   33,
297     PAGE_DOWN: 34,
298     LEFT:      37,
299     UP:        38,
300     RIGHT:     39,
301     DOWN:      40,
302   };
303
304   ns.Picker = function($real, options) {
305     var self = this;
306     this.o = $.extend(true, {
307       limit: 20,
308       delay: 50,
309       action: {
310         commit_none: function(){ },
311         commit_one:  function(){ $('#update_button').click(); },
312         commit_many: function(){ self.open_dialog(); }
313       }
314     }, $real.data('part-picker-data'), options);
315     this.$real              = $real;
316     this.real_id            = $real.attr('id');
317     this.last_real          = $real.val();
318     this.$dummy             = $($real.siblings()[0]);
319     this.autocomplete_open  = false;
320     this.state              = this.STATES.PICKED;
321     this.last_dummy         = this.$dummy.val();
322     this.timer              = undefined;
323     this.dialog             = undefined;
324
325     this.init();
326   };
327
328   ns.Picker.prototype = {
329     CLASSES: {
330       PICKED:       'partpicker-picked',
331       UNDEFINED:    'partpicker-undefined',
332     },
333     ajax_data: function(term) {
334       var data = {
335         'filter.all:substr:multi::ilike': term,
336         'filter.obsolete': 0,
337         current:  this.$real.val(),
338       };
339
340       if (this.o.part_type)
341         data['filter.part_type'] = this.o.part_type.split(',');
342
343       if (this.o.classification_id)
344         data['filter.classification_id'] = this.o.classification_id.split(',');
345
346       if (this.o.unit)
347         data['filter.unit'] = this.o.unit.split(',');
348
349       if (this.o.convertible_unit)
350         data['filter.unit_obj.convertible_to'] = this.o.convertible_unit;
351
352       return data;
353     },
354     set_item: function(item) {
355       var self = this;
356       if (item.id) {
357         this.$real.val(item.id);
358         // autocomplete ui has name, use the value for ajax items, which contains displayable_name
359         this.$dummy.val(item.name ? item.name : item.value);
360       } else {
361         this.$real.val('');
362         this.$dummy.val('');
363       }
364       this.state      = this.STATES.PICKED;
365       this.last_real  = this.$real.val();
366       this.last_dummy = this.$dummy.val();
367       this.$real.trigger('change');
368
369       if (this.o.fat_set_item && item.id) {
370         $.ajax({
371           url: 'controller.pl?action=Part/show.json',
372           data: { 'part.id': item.id },
373           success: function(rsp) {
374             self.$real.trigger('set_item:PartPicker', rsp);
375           },
376         });
377       } else {
378         this.$real.trigger('set_item:PartPicker', item);
379       }
380       this.annotate_state();
381     },
382     set_multi_items: function(data) {
383       this.run_action(this.o.action.set_multi_items, [ data ]);
384     },
385     make_defined_state: function() {
386       if (this.state == this.STATES.PICKED) {
387         this.annotate_state();
388         return true
389       } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
390         this.set_item({})
391       else {
392         this.set_item({ id: this.last_real, name: this.last_dummy })
393       }
394       this.annotate_state();
395     },
396     annotate_state: function() {
397       if (this.state == this.STATES.PICKED)
398         this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
399       else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
400         this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
401       else {
402         this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
403       }
404     },
405     handle_changed_text: function(callbacks) {
406       var self = this;
407       $.ajax({
408         url: 'controller.pl?action=Part/ajax_autocomplete',
409         dataType: "json",
410         data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
411         success: function (data) {
412           if (data.length == 1) {
413             self.set_item(data[0]);
414             if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
415           } else if (data.length > 1) {
416             self.state = self.STATES.UNDEFINED;
417             if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
418           } else {
419             self.state = self.STATES.UNDEFINED;
420             if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
421           }
422           self.annotate_state();
423         }
424       });
425     },
426     /*  In case users are impatient and want to skip ahead:
427      *  Capture <enter> key events and check if it's a unique hit.
428      *  If it is, go ahead and assume it was selected. If it wasn't don't do
429      *  anything so that autocompletion kicks in.  For <tab> don't prevent
430      *  propagation. It would be nice to catch it, but javascript is too stupid
431      *  to fire a tab event later on, so we'd have to reimplement the "find
432      *  next active element in tabindex order and focus it".
433      */
434     /* note:
435      *  event.which does not contain tab events in keypressed in firefox but will report 0
436      *  chrome does not fire keypressed at all on tab or escape
437      */
438     handle_keydown: function(event) {
439       var self = this;
440       if (event.which == KEY.ENTER || event.which == KEY.TAB) {
441         // if string is empty assume they want to delete
442         if (self.$dummy.val() === '') {
443           self.set_item({});
444           return true;
445         } else if (self.state == self.STATES.PICKED) {
446           if (self.o.action.commit_one) {
447             self.run_action(self.o.action.commit_one);
448           }
449           return true;
450         }
451         if (event.which == KEY.TAB) {
452           event.preventDefault();
453           self.handle_changed_text();
454         }
455         if (event.which == KEY.ENTER) {
456           event.preventDefault();
457           self.handle_changed_text({
458             match_none: self.o.action.commit_none,
459             match_one:  self.o.action.commit_one,
460             match_many: self.o.action.commit_many
461           });
462           return false;
463         }
464       } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
465         var old_options = self.$dummy.autocomplete('option');
466         self.$dummy.autocomplete('option', 'minLength', 0);
467         self.$dummy.autocomplete('search', self.$dummy.val());
468         self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
469       } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
470         self.state = self.STATES.UNDEFINED;
471       }
472     },
473     open_dialog: function() {
474       if (this.o.multiple) {
475         this.dialog = new ns.PickerMultiPopup(this);
476       } else {
477         this.dialog = new ns.PickerPopup(this);
478       }
479     },
480     close_dialog: function() {
481       this.dialog.close_dialog();
482       this.dialog = undefined;
483     },
484     init: function() {
485       var self = this;
486       this.$dummy.autocomplete({
487         source: function(req, rsp) {
488           $.ajax($.extend(self.o, {
489             url:      'controller.pl?action=Part/ajax_autocomplete',
490             dataType: "json",
491             data:     self.ajax_data(req.term),
492             success:  function (data){ rsp(data) }
493           }));
494         },
495         select: function(event, ui) {
496           self.set_item(ui.item);
497           if (self.o.action.commit_one) {
498             self.run_action(self.o.action.commit_one);
499           }
500         },
501         search: function(event, ui) {
502           if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
503             event.preventDefault();
504         },
505         open: function() {
506           self.autocomplete_open = true;
507         },
508         close: function() {
509           self.autocomplete_open = false;
510         }
511       });
512       this.$dummy.keydown(function(event){ self.handle_keydown(event) });
513       this.$dummy.on('paste', function(){
514         setTimeout(function() {
515           self.handle_changed_text();
516         }, 1);
517       });
518       this.$dummy.blur(function(){
519         window.clearTimeout(self.timer);
520         self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
521       });
522
523       var popup_button = $('<span>').addClass('ppp_popup_button');
524       this.$dummy.after(popup_button);
525       popup_button.click(function() { self.open_dialog() });
526     },
527     run_action: function(code, args) {
528       if (typeof code === 'function')
529         code.apply(this, args)
530       else
531         kivi.run(code, args);
532     },
533     clear: function() {
534       this.set_item({});
535     }
536   };
537   ns.Picker.prototype.STATES = {
538     PICKED:    ns.Picker.prototype.CLASSES.PICKED,
539     UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
540   };
541
542   ns.PickerPopup = function(pp) {
543     this.timer = undefined;
544     this.pp    = pp;
545     this.open_dialog();
546   };
547
548   ns.PickerPopup.prototype = {
549     open_dialog: function() {
550       var self = this;
551       kivi.popup_dialog({
552         url: 'controller.pl?action=Part/part_picker_search',
553         data: self.pp.ajax_data(this.pp.$dummy.val()),
554         id: 'part_selection',
555         dialog: {
556           title: kivi.t8('Part picker'),
557           width: 800,
558           height: 800,
559         },
560         load: function() { self.init_search(); }
561       });
562       window.clearTimeout(this.timer);
563       return true;
564     },
565     init_search: function() {
566       var self = this;
567       $('#part_picker_filter').keypress(function(e) { self.result_timer(e) }).focus();
568       $('#no_paginate').change(function() { self.update_results() });
569       this.update_results();
570     },
571     update_results: function() {
572       var self = this;
573       $.ajax({
574         url: 'controller.pl?action=Part/part_picker_result',
575         data: $.extend({
576           no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
577         }, self.pp.ajax_data(function(){
578           var val = $('#part_picker_filter').val();
579           return val === undefined ? '' : val
580         })),
581         success: function(data){
582           $('#part_picker_result').html(data);
583           self.init_results();
584         }
585       });
586     },
587     init_results: function() {
588       var self = this;
589       $('div.part_picker_part').each(function(){
590         $(this).click(function(){
591           self.pp.set_item({
592             id:   $(this).children('input.part_picker_id').val(),
593             name: $(this).children('input.part_picker_description').val(),
594             classification_id: $(this).children('input.part_picker_classification_id').val(),
595             ean:  $(this).children('input.part_picker_ean').val(),
596             unit: $(this).children('input.part_picker_unit').val(),
597             partnumber:  $(this).children('input.part_picker_partnumber').val(),
598             description: $(this).children('input.part_picker_description').val(),
599           });
600           self.close_dialog();
601           self.pp.$dummy.focus();
602           return true;
603         });
604       });
605       $('#part_selection').keydown(function(e){
606          if (e.which == KEY.ESCAPE) {
607            self.close_dialog();
608            self.pp.$dummy.focus();
609          }
610       });
611     },
612     result_timer: function(event) {
613       var self = this;
614       if (!$('no_paginate').prop('checked')) {
615         if (event.keyCode == KEY.PAGE_UP) {
616           $('#part_picker_result a.paginate-prev').click();
617           return;
618         }
619         if (event.keyCode == KEY.PAGE_DOWN) {
620           $('#part_picker_result a.paginate-next').click();
621           return;
622         }
623       }
624       window.clearTimeout(this.timer);
625       if (event.which == KEY.ENTER) {
626         self.update_results();
627       } else {
628         this.timer = window.setTimeout(function() { self.update_results() }, 100);
629       }
630     },
631     close_dialog: function() {
632       $('#part_selection').dialog('close');
633     }
634   };
635
636   ns.PickerMultiPopup = function(pp) {
637     this.pp       = pp;
638     this.callback = 'Part/add_multi_' + this.pp.o.part_type + '_items';
639     this.open_dialog();
640   };
641
642   ns.PickerMultiPopup.prototype = {
643     open_dialog: function() {
644       var self = this;
645       $('#row_table_id thead a img').remove();
646
647       kivi.popup_dialog({
648         url: 'controller.pl?action=Part/show_multi_items_dialog',
649         data: $.extend({
650           real_id: self.pp.real_id,
651         }, self.pp.ajax_data(this.pp.$dummy.val())),
652         id: 'jq_multi_items_dialog',
653         dialog: {
654           title: kivi.t8('Add multiple items'),
655           width:  800,
656           height: 800
657         },
658         load: function() {
659           self.init_search();
660         }
661       });
662       return true;
663     },
664     init_search: function() {
665       var self = this;
666       $('#multi_items_filter_table input, #multi_items_filter_table select').keydown(function(event) {
667         if(event.which == KEY.ENTER) {
668           event.preventDefault();
669           self.update_results();
670           return false;
671         }
672       });
673
674       $('#multi_items_filter_all_substr_multi_ilike').focus();
675       $('#multi_items_filter_button').click(function(){ self.update_results() });
676       $('#multi_items_filter_reset').click(function(){ $("#multi_items_form").resetForm() });
677       $('#continue_button').click(function(){ self.add_multi_items() });
678     },
679     update_results: function() {
680       var self = this;
681       var data = $('#multi_items_form').serializeArray();
682       data.push({ name: 'type', value: self.pp.type });
683       $.ajax({
684         url: 'controller.pl?action=Part/multi_items_update_result',
685         data: data,
686         method: 'post',
687         success: function(data){
688           $('#multi_items_result').html(data);
689           self.init_results();
690           self.enable_continue();
691         }
692       });
693     },
694     set_qty_to_one: function(clicked) {
695       if ($(clicked).val() === '') {
696         $(clicked).val(kivi.format_amount(1.00, -2));
697       }
698       $(clicked).select();
699     },
700     init_results: function() {
701       var self = this;
702       $('#multi_items_all_qty').change(function(event){
703         $('.multi_items_qty').val($(event.target).val());
704       });
705       $('.multi_items_qty').click(function(){ self.set_qty_to_one(this) });
706     },
707     result_timer: function(event) {
708     },
709     close_dialog: function() {
710       $('#jq_multi_items_dialog').dialog('close');
711     },
712     disable_continue: function() {
713       $('#multi_items_result input').off("keydown");
714       $('#continue_button').prop('disabled', true);
715     },
716     enable_continue: function() {
717       var self = this;
718       $('#multi_items_result input').keydown(function(event) {
719         if(event.keyCode == KEY.ENTER) {
720           event.preventDefault();
721           self.add_multi_items();
722           return false;
723         }
724       });
725       $('#continue_button').prop('disabled', false);
726     },
727     add_multi_items: function() {
728       // rows at all
729       var n_rows = $('.multi_items_qty').length;
730       if ( n_rows === 0) { return; }
731
732       // filled rows
733       n_rows = $('.multi_items_qty').filter(function() {
734         return $(this).val().length > 0;
735       }).length;
736       if (n_rows === 0) { return; }
737
738       this.disable_continue();
739
740       var data = $('#multi_items_form').serializeArray();
741       this.pp.set_multi_items(data);
742     }
743   };
744
745   ns.reinit_widgets = function() {
746     kivi.run_once_for('input.part_autocomplete', 'part_picker', function(elt) {
747       if (!$(elt).data('part_picker'))
748         $(elt).data('part_picker', new kivi.Part.Picker($(elt)));
749     });
750
751     kivi.run_once_for('#customerprice_rows', 'customerprice_row_sort_renumber', function(elt) {
752       $(elt).on('sortstop', kivi.Part.customerprice_renumber_positions);
753     });
754
755     kivi.run_once_for('#makemodel_rows', 'makemodel_row_sort_renumber', function(elt) {
756       $(elt).on('sortstop', kivi.Part.makemodel_renumber_positions);
757     });
758   }
759
760   ns.init = function() {
761     ns.reinit_widgets();
762   }
763
764   $(function(){
765     $('#ic').on('focusout', '.reformat_number', function(event) {
766        ns.reformat_number(event);
767     });
768
769     $('#part_warehouse_id').change(kivi.Part.reload_bin_selection);
770
771     ns.init();
772   });
773 });