1 namespace('kivi.Part', function(ns) {
4 ns.open_history_popup = function() {
5 var id = $("#part_id").val();
7 url: 'controller.pl?action=Part/history&part.id=' + id,
8 dialog: { title: kivi.t8('History') },
12 ns.save = function() {
13 var data = $('#ic').serializeArray();
14 data.push({ name: 'action', value: 'Part/save' });
16 $.post("controller.pl", data, kivi.eval_json_result);
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);
25 ns.delete = function() {
26 var data = $('#ic').serializeArray();
27 data.push({ name: 'action', value: 'Part/delete' });
29 $.post("controller.pl", data, kivi.eval_json_result);
32 ns.reformat_number = function(event) {
33 $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
36 ns.set_tab_active_by_index = function (index) {
37 $("#ic_tabs").tabs({active: index})
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);
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();
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();
64 src = "image/down.png";
67 $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
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 });
74 $.post("controller.pl", data, kivi.eval_json_result);
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' });
82 $.post("controller.pl", data, kivi.eval_json_result);
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' });
90 $.post("controller.pl", data, kivi.eval_json_result);
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();
99 ns.set_assortment_lsg_sellprice = function() {
100 $("#items_lsg_sellprice_sum_basic").closest('td').find('input').val($("#items_lsg_sellprice_sum").html());
103 ns.set_assortment_douglas_sellprice = function() {
104 $("#items_douglas_sellprice_sum_basic").closest('td').find('input').val($("#items_douglas_sellprice_sum").html());
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();
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();
119 ns.renumber_positions = function() {
120 var part_type = $("#part_part_type").val();
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"]');
127 $(rows).each(function(idx, elt) {
129 var row = $(elt).closest('tr');
130 if ( idx % 2 === 0 ) {
131 if ( row.hasClass('listrow1') ) {
132 row.removeClass('listrow1');
133 row.addClass('listrow0');
136 if ( row.hasClass('listrow0') ) {
137 row.removeClass('listrow0');
138 row.addClass('listrow1');
144 ns.delete_item_row = function(clicked) {
145 var row = $(clicked).closest('tr');
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();
156 ns.add_assortment_item = function() {
157 if ($('#assortment_picker').val() === '') return;
159 $('#row_table_id thead a img').remove();
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();
167 $.post("controller.pl", data, kivi.eval_json_result);
170 ns.add_assembly_item = function() {
171 if ($('#assembly_picker').val() === '') return;
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();
179 $.post("controller.pl", data, kivi.eval_json_result);
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);
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);
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();
201 ns.redisplay_items = function(data) {
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();
210 $(data).each(function(idx, elt) {
211 new_rows.push(old_rows[elt.old_pos - 1]);
213 if (part_type === 'assortment') {
214 $(new_rows).appendTo($('#assortment_items'));
215 } else if ( part_type === 'assembly') {
216 $(new_rows).appendTo($('#assembly_items'));
218 ns.renumber_positions();
221 ns.focus_last_assortment_input = function () {
222 $("#assortment_items tr:last").find('input[type=text]').filter(':visible:first').focus();
225 ns.focus_last_assembly_input = function () {
226 $("#assembly_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
230 ns.makemodel_renumber_positions = function() {
231 $('.makemodel_row [name="position"]').each(function(idx, elt) {
236 ns.delete_makemodel_row = function(clicked) {
237 var row = $(clicked).closest('tr');
240 ns.makemodel_renumber_positions();
243 ns.add_makemodel_row = function() {
244 if ($('#add_makemodel').val() === '') return;
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();
250 $.post("controller.pl", data, kivi.eval_json_result);
253 ns.focus_last_makemodel_input = function () {
254 $("#makemodel_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
259 ns.customerprice_renumber_positions = function() {
260 $('.customerprice_row [name="position"]').each(function(idx, elt) {
265 ns.delete_customerprice_row = function(clicked) {
266 var row = $(clicked).closest('tr');
269 ns.customerprice_renumber_positions();
272 ns.add_customerprice_row = function() {
273 if ($('#add_customerprice').val() === '') return;
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();
279 $.post("controller.pl", data, kivi.eval_json_result);
282 ns.focus_last_customerprice_input = function () {
283 $("#customerprice_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
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);
306 ns.Picker = function($real, options) {
308 this.o = $.extend(true, {
312 commit_none: function(){ },
313 commit_one: function(){ $('#update_button').click(); },
314 commit_many: function(){ self.open_dialog(); }
317 }, $real.data('part-picker-data'), options);
319 this.real_id = $real.attr('id');
320 this.last_real = $real.val();
321 this.$dummy = $($real.siblings()[0]);
322 this.autocomplete_open = false;
323 this.state = this.STATES.PICKED;
324 this.last_dummy = this.$dummy.val();
325 this.timer = undefined;
326 this.dialog = undefined;
327 this.multiple_default = this.o.multiple;
332 ns.Picker.prototype = {
334 PICKED: 'partpicker-picked',
335 UNDEFINED: 'partpicker-undefined',
337 ajax_data: function(term) {
339 current: this.$real.val(),
342 if (this.o.part_type)
343 data['filter.part_type'] = this.o.part_type.split(',');
346 if (this.o.status == 'active') data['filter.obsolete'] = 0;
347 if (this.o.status == 'obsolete') data['filter.obsolete'] = 1;
349 data['filter.obsolete'] = 0;
351 if (this.o.classification_id)
352 data['filter.classification_id:any'] = this.o.classification_id.replaceAll(',', ' ');
355 data['filter.unit'] = this.o.unit.split(',');
357 if (this.o.convertible_unit)
358 data['filter.unit_obj.convertible_to'] = this.o.convertible_unit;
360 var filter_name = 'all';
361 if (this.o.with_makemodel) {
362 filter_name = 'all_with_makemodel';
364 if (this.o.with_customer_partnumber) {
365 filter_name = 'all_with_customer_partnumber';
367 data['filter.' + filter_name + ':substr:multi::ilike'] = term;
371 set_item: function(item) {
374 this.$real.val(item.id);
375 // autocomplete ui has name, use the value for ajax items, which contains displayable_name
376 this.$dummy.val(item.name ? item.name : item.value);
381 this.state = this.STATES.PICKED;
382 this.last_real = this.$real.val();
383 this.last_dummy = this.$dummy.val();
384 this.$real.trigger('change');
386 if (this.o.fat_set_item && item.id) {
388 url: 'controller.pl?action=Part/show.json',
389 data: { 'part.id': item.id },
390 success: function(rsp) {
391 self.$real.trigger('set_item:PartPicker', rsp);
395 this.$real.trigger('set_item:PartPicker', item);
397 this.annotate_state();
399 set_multi_items: function(data) {
400 this.run_action(this.o.action.set_multi_items, [ data ]);
402 make_defined_state: function() {
403 if (this.state == this.STATES.PICKED) {
404 this.annotate_state();
406 } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
409 this.set_item({ id: this.last_real, name: this.last_dummy })
411 this.annotate_state();
413 annotate_state: function() {
414 if (this.state == this.STATES.PICKED)
415 this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
416 else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
417 this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
419 this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
422 handle_changed_text: function(callbacks) {
425 url: 'controller.pl?action=Part/ajax_autocomplete',
427 data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
428 success: function (data) {
429 if (data.length == 1) {
430 self.set_item(data[0]);
431 if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
432 } else if (data.length > 1) {
433 self.state = self.STATES.UNDEFINED;
434 if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
436 self.state = self.STATES.UNDEFINED;
437 if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
439 self.annotate_state();
443 /* In case users are impatient and want to skip ahead:
444 * Capture <enter> key events and check if it's a unique hit.
445 * If it is, go ahead and assume it was selected. If it wasn't don't do
446 * anything so that autocompletion kicks in. For <tab> don't prevent
447 * propagation. It would be nice to catch it, but javascript is too stupid
448 * to fire a tab event later on, so we'd have to reimplement the "find
449 * next active element in tabindex order and focus it".
452 * event.which does not contain tab events in keypressed in firefox but will report 0
453 * chrome does not fire keypressed at all on tab or escape
455 handle_keydown: function(event) {
457 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
458 // if string is empty assume they want to delete
459 if (self.$dummy.val() === '') {
462 } else if (self.state == self.STATES.PICKED) {
463 if (self.o.action.commit_one) {
464 self.run_action(self.o.action.commit_one);
468 if (event.which == KEY.TAB) {
469 event.preventDefault();
470 self.handle_changed_text();
472 if (event.which == KEY.ENTER) {
473 event.preventDefault();
474 self.handle_changed_text({
475 match_none: self.o.action.commit_none,
476 match_one: self.o.action.commit_one,
477 match_many: self.o.action.commit_many
481 } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
482 var old_options = self.$dummy.autocomplete('option');
483 self.$dummy.autocomplete('option', 'minLength', 0);
484 self.$dummy.autocomplete('search', self.$dummy.val());
485 self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
486 } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
487 self.state = self.STATES.UNDEFINED;
490 open_dialog: function() {
491 if (this.o.multiple) {
492 this.o.multiple = this.multiple_default;
493 this.dialog = new ns.PickerMultiPopup(this);
495 this.dialog = new ns.PickerPopup(this);
498 close_dialog: function() {
499 this.dialog.close_dialog();
500 this.dialog = undefined;
504 this.$dummy.autocomplete({
505 source: function(req, rsp) {
506 $.ajax($.extend(self.o, {
507 url: 'controller.pl?action=Part/ajax_autocomplete',
509 data: self.ajax_data(req.term),
510 success: function (data){ rsp(data) }
513 select: function(event, ui) {
514 self.set_item(ui.item);
515 if (self.o.action.commit_one) {
516 self.run_action(self.o.action.commit_one);
519 search: function(event, ui) {
520 if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
521 event.preventDefault();
524 self.autocomplete_open = true;
527 self.autocomplete_open = false;
530 this.$dummy.keydown(function(event){ self.handle_keydown(event) });
531 this.$dummy.on('paste', function(){
532 setTimeout(function() {
533 self.handle_changed_text();
536 this.$dummy.blur(function(){
537 window.clearTimeout(self.timer);
538 self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
541 var popup_button = $('<span>').addClass('ppp_popup_button');
542 this.$dummy.after(popup_button);
543 popup_button.click(function() { self.open_dialog() });
545 run_action: function(code, args) {
546 if (typeof code === 'function')
547 code.apply(this, args)
549 kivi.run(code, args);
555 ns.Picker.prototype.STATES = {
556 PICKED: ns.Picker.prototype.CLASSES.PICKED,
557 UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
560 ns.PickerPopup = function(pp) {
561 this.timer = undefined;
566 ns.PickerPopup.prototype = {
567 open_dialog: function() {
570 url: 'controller.pl?action=Part/part_picker_search',
572 real_id: self.pp.real_id,
573 }, self.pp.ajax_data(this.pp.$dummy.val())),
574 id: 'part_selection',
576 title: kivi.t8('Part picker'),
580 load: function() { self.init_search(); }
582 window.clearTimeout(this.timer);
585 init_search: function() {
587 $('#part_picker_filter').keypress(function(e) { self.result_timer(e) }).focus();
588 $('#no_paginate').change(function() { self.update_results() });
589 this.update_results();
591 update_results: function() {
594 url: 'controller.pl?action=Part/part_picker_result',
596 no_paginate: $('#no_paginate').prop('checked') ? 1 : 0,
597 }, self.pp.ajax_data(function(){
598 var val = $('#part_picker_filter').val();
599 return val === undefined ? '' : val
601 success: function(data){
602 $('#part_picker_result').html(data);
607 init_results: function() {
609 $('div.part_picker_part').each(function(){
610 $(this).click(function(){
612 id: $(this).children('input.part_picker_id').val(),
613 name: $(this).children('input.part_picker_description').val(),
614 classification_id: $(this).children('input.part_picker_classification_id').val(),
615 ean: $(this).children('input.part_picker_ean').val(),
616 unit: $(this).children('input.part_picker_unit').val(),
617 partnumber: $(this).children('input.part_picker_partnumber').val(),
618 description: $(this).children('input.part_picker_description').val(),
621 self.pp.$dummy.focus();
625 $('#part_selection').keydown(function(e){
626 if (e.which == KEY.ESCAPE) {
628 self.pp.$dummy.focus();
632 result_timer: function(event) {
634 if (!$('no_paginate').prop('checked')) {
635 if (event.keyCode == KEY.PAGE_UP) {
636 $('#part_picker_result a.paginate-prev').click();
639 if (event.keyCode == KEY.PAGE_DOWN) {
640 $('#part_picker_result a.paginate-next').click();
644 window.clearTimeout(this.timer);
645 if (event.which == KEY.ENTER) {
646 self.update_results();
648 this.timer = window.setTimeout(function() { self.update_results() }, 100);
651 close_dialog: function() {
652 $('#part_selection').dialog('close');
656 ns.PickerMultiPopup = function(pp) {
658 this.callback = 'Part/add_multi_' + this.pp.o.part_type + '_items';
662 ns.PickerMultiPopup.prototype = {
663 open_dialog: function() {
665 $('#row_table_id thead a img').remove();
668 url: 'controller.pl?action=Part/show_multi_items_dialog',
670 real_id: self.pp.real_id,
671 show_pos_input: self.pp.o.multiple_pos_input,
672 }, self.pp.ajax_data(this.pp.$dummy.val())),
673 id: 'jq_multi_items_dialog',
675 title: kivi.t8('Add multiple items'),
685 init_search: function() {
687 $('#multi_items_filter_table input, #multi_items_filter_table select').keydown(function(event) {
688 if(event.which == KEY.ENTER) {
689 event.preventDefault();
690 self.update_results();
695 $('#multi_items_filter_all_substr_multi_ilike').focus();
696 $('#multi_items_filter_button').click(function(){ self.update_results() });
697 $('#multi_items_filter_reset').click(function(){ $("#multi_items_form").resetForm() });
698 $('#continue_button').click(function(){ self.add_multi_items() });
700 update_results: function() {
702 var data = $('#multi_items_form').serializeArray();
703 data.push({ name: 'type', value: self.pp.type });
704 data.push({ name: 'limit', value: self.pp.o.multiple_limit });
705 var ppdata = self.pp.ajax_data(function(){
706 var val = $('#multi_items_filter').val();
707 return val === undefined ? '' : val
709 $.each(Object.keys(ppdata), function() {data.push({ name: 'multi_items.' + this, value: ppdata[this]});});
711 url: 'controller.pl?action=Part/multi_items_update_result',
714 success: function(data){
715 $('#multi_items_result').html(data);
717 self.enable_continue();
721 set_qty_to_one: function(clicked) {
722 if ($(clicked).val() === '') {
723 $(clicked).val(kivi.format_amount(1.00, -2));
727 init_results: function() {
729 $('#multi_items_all_qty').change(function(event){
730 $('.multi_items_qty').val($(event.target).val());
732 $('.multi_items_qty').click(function(){ self.set_qty_to_one(this) });
734 result_timer: function(event) {
736 close_dialog: function() {
737 $('#jq_multi_items_dialog').dialog('close');
739 disable_continue: function() {
740 $('#multi_items_result input, #multi_items_position').off("keydown");
741 $('#continue_button').prop('disabled', true);
743 enable_continue: function() {
745 $('#multi_items_result input, #multi_items_position').keydown(function(event) {
746 if(event.keyCode == KEY.ENTER) {
747 event.preventDefault();
748 self.add_multi_items();
752 $('#continue_button').prop('disabled', false);
754 add_multi_items: function() {
756 var n_rows = $('.multi_items_qty').length;
757 if ( n_rows === 0) { return; }
760 n_rows = $('.multi_items_qty').filter(function() {
761 return $(this).val().length > 0;
763 if (n_rows === 0) { return; }
765 this.disable_continue();
767 var data = $('#multi_items_form').serializeArray();
768 this.pp.set_multi_items(data);
772 ns.reinit_widgets = function() {
773 kivi.run_once_for('input.part_autocomplete', 'part_picker', function(elt) {
774 if (!$(elt).data('part_picker'))
775 $(elt).data('part_picker', new kivi.Part.Picker($(elt)));
778 kivi.run_once_for('#customerprice_rows', 'customerprice_row_sort_renumber', function(elt) {
779 $(elt).on('sortstop', kivi.Part.customerprice_renumber_positions);
782 kivi.run_once_for('#makemodel_rows', 'makemodel_row_sort_renumber', function(elt) {
783 $(elt).on('sortstop', kivi.Part.makemodel_renumber_positions);
787 ns.init = function() {
792 $('#ic').on('focusout', '.reformat_number', function(event) {
793 ns.reformat_number(event);
796 $('#part_warehouse_id').change(kivi.Part.reload_bin_selection);