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') + '">');
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 }
76 $.post("controller.pl", data, kivi.eval_json_result);
79 ns.assortment_recalc = function() {
80 var data = $('#assortment :input').serializeArray();
82 { name: 'action', value: 'Part/update_item_totals' },
83 { name: 'part_type', value: 'assortment' }
86 $.post("controller.pl", data, kivi.eval_json_result);
89 ns.assembly_recalc = function() {
90 var data = $('#assembly :input').serializeArray();
92 { name: 'action', value: 'Part/update_item_totals' },
93 { name: 'part_type', value: 'assembly' }
96 $.post("controller.pl", data, kivi.eval_json_result);
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();
105 ns.set_assortment_lsg_sellprice = function() {
106 $("#items_lsg_sellprice_sum_basic").closest('td').find('input').val($("#items_lsg_sellprice_sum").html());
109 ns.set_assortment_douglas_sellprice = function() {
110 $("#items_douglas_sellprice_sum_basic").closest('td').find('input').val($("#items_douglas_sellprice_sum").html());
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();
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();
125 ns.renumber_positions = function() {
126 var part_type = $("#part_part_type").val();
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"]');
133 $(rows).each(function(idx, elt) {
135 var row = $(elt).closest('tr');
136 if ( idx % 2 === 0 ) {
137 if ( row.hasClass('listrow1') ) {
138 row.removeClass('listrow1');
139 row.addClass('listrow0');
142 if ( row.hasClass('listrow0') ) {
143 row.removeClass('listrow0');
144 row.addClass('listrow1');
150 ns.delete_item_row = function(clicked) {
151 var row = $(clicked).closest('tr');
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();
162 ns.add_assortment_item = function() {
163 if ($('#assortment_picker').val() === '') return;
165 $('#row_table_id thead a img').remove();
167 var data = $('#assortment :input').serializeArray();
169 { name: 'action', value: 'Part/add_assortment_item' },
170 { name: 'part.id', value: $('#part_id').val() },
171 { name: 'part.part_type', value: 'assortment' }
173 $('#assortment_picker').data('part_picker').clear();
175 $.post("controller.pl", data, kivi.eval_json_result);
178 ns.add_assembly_item = function() {
179 if ($('#assembly_picker').val() === '') return;
181 var data = $('#assembly :input').serializeArray();
183 { name: 'action', value: 'Part/add_assembly_item' },
184 { name: 'part.id', value: $("#part_id").val() },
185 { name: 'part.part_type', value: 'assembly' }
187 $('#assembly_picker').data('part_picker').clear();
189 $.post("controller.pl", data, kivi.eval_json_result);
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);
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);
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();
211 ns.redisplay_items = function(data) {
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();
220 $(data).each(function(idx, elt) {
221 new_rows.push(old_rows[elt.old_pos - 1]);
223 if (part_type === 'assortment') {
224 $(new_rows).appendTo($('#assortment_items'));
225 } else if ( part_type === 'assembly') {
226 $(new_rows).appendTo($('#assembly_items'));
228 ns.renumber_positions();
231 ns.focus_last_assortment_input = function () {
232 $("#assortment_items tr:last").find('input[type=text]').filter(':visible:first').focus();
235 ns.focus_last_assembly_input = function () {
236 $("#assembly_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
240 ns.makemodel_renumber_positions = function() {
241 $('.makemodel_row [name="position"]').each(function(idx, elt) {
246 ns.delete_makemodel_row = function(clicked) {
247 var row = $(clicked).closest('tr');
250 ns.makemodel_renumber_positions();
253 ns.add_makemodel_row = function() {
254 if ($('#add_makemodel').val() === '') return;
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();
260 $.post("controller.pl", data, kivi.eval_json_result);
263 ns.focus_last_makemodel_input = function () {
264 $("#makemodel_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
269 ns.customerprice_renumber_positions = function() {
270 $('.customerprice_row [name="position"]').each(function(idx, elt) {
275 ns.delete_customerprice_row = function(clicked) {
276 var row = $(clicked).closest('tr');
279 ns.customerprice_renumber_positions();
282 ns.add_customerprice_row = function() {
283 if ($('#add_customerprice').val() === '') return;
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();
289 $.post("controller.pl", data, kivi.eval_json_result);
292 ns.focus_last_customerprice_input = function () {
293 $("#customerprice_rows tr:last").find('input[type=text]').filter(':visible:first').focus();
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);
316 ns.Picker = function($real, options) {
318 this.o = $.extend(true, {
322 commit_none: function(){ },
323 commit_one: function(){ $('#update_button').click(); },
324 commit_many: function(){ self.open_dialog(); }
327 }, $real.data('part-picker-data'), options);
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;
342 ns.Picker.prototype = {
344 PICKED: 'partpicker-picked',
345 UNDEFINED: 'partpicker-undefined',
347 ajax_data: function(term) {
349 current: this.$real.val(),
352 if (this.o.part_type)
353 data['filter.part_type'] = this.o.part_type.split(',');
356 if (this.o.status == 'active') data['filter.obsolete'] = 0;
357 if (this.o.status == 'obsolete') data['filter.obsolete'] = 1;
359 data['filter.obsolete'] = 0;
361 if (this.o.classification_id)
362 data['filter.classification_id:any'] = this.o.classification_id.replaceAll(',', ' ');
365 data['filter.unit'] = this.o.unit.split(',');
367 if (this.o.convertible_unit)
368 data['filter.unit_obj.convertible_to'] = this.o.convertible_unit;
370 var filter_name = 'all';
371 if (this.o.with_makemodel) {
372 filter_name = 'all_with_makemodel';
374 if (this.o.with_customer_partnumber) {
375 filter_name = 'all_with_customer_partnumber';
377 data['filter.' + filter_name + ':substr:multi::ilike'] = term;
381 set_item: function(item) {
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);
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');
396 if (this.o.fat_set_item && item.id) {
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);
405 this.$real.trigger('set_item:PartPicker', item);
407 this.annotate_state();
409 set_multi_items: function(data) {
410 this.run_action(this.o.action.set_multi_items, [ data ]);
412 make_defined_state: function() {
413 if (this.state == this.STATES.PICKED) {
414 this.annotate_state();
416 } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '') {
419 this.set_item({ id: this.last_real, name: this.last_dummy });
421 this.annotate_state();
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);
429 this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
432 handle_changed_text: function(callbacks) {
435 url: 'controller.pl?action=Part/ajax_autocomplete',
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 ]);
446 self.state = self.STATES.UNDEFINED;
447 if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
449 self.annotate_state();
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".
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
465 handle_keydown: function(event) {
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() === '') {
472 } else if (self.state == self.STATES.PICKED) {
473 if (self.o.action.commit_one) {
474 self.run_action(self.o.action.commit_one);
478 if (event.which == KEY.TAB) {
479 event.preventDefault();
480 self.handle_changed_text();
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
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;
500 open_dialog: function() {
501 if (this.o.multiple) {
502 this.o.multiple = this.multiple_default;
503 this.dialog = new ns.PickerMultiPopup(this);
505 this.dialog = new ns.PickerPopup(this);
508 close_dialog: function() {
509 this.dialog.close_dialog();
510 this.dialog = undefined;
514 this.$dummy.autocomplete({
515 source: function(req, rsp) {
516 $.ajax($.extend(self.o, {
517 url: 'controller.pl?action=Part/ajax_autocomplete',
519 data: self.ajax_data(req.term),
520 success: function (data){ rsp(data); }
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);
529 search: function(event) {
530 if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
531 event.preventDefault();
534 self.autocomplete_open = true;
537 self.autocomplete_open = false;
540 this.$dummy.keydown(function(event){ self.handle_keydown(event); });
541 this.$dummy.on('paste', function(){
542 setTimeout(function() {
543 self.handle_changed_text();
546 this.$dummy.blur(function(){
547 window.clearTimeout(self.timer);
548 self.timer = window.setTimeout(function() { self.annotate_state(); }, 100);
551 var popup_button = $('<span>').addClass('ppp_popup_button');
552 this.$dummy.after(popup_button);
553 popup_button.click(function() { self.open_dialog(); });
555 run_action: function(code, args) {
556 if (typeof code === 'function')
557 code.apply(this, args);
559 kivi.run(code, args);
565 ns.Picker.prototype.STATES = {
566 PICKED: ns.Picker.prototype.CLASSES.PICKED,
567 UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
570 ns.PickerPopup = function(pp) {
571 this.timer = undefined;
576 ns.PickerPopup.prototype = {
577 open_dialog: function() {
580 url: 'controller.pl?action=Part/part_picker_search',
582 real_id: self.pp.real_id,
583 }, self.pp.ajax_data(this.pp.$dummy.val())),
584 id: 'part_selection',
586 title: kivi.t8('Part picker'),
590 load: function() { self.init_search(); }
592 window.clearTimeout(this.timer);
595 init_search: function() {
597 $('#part_picker_filter').keypress(function(e) { self.result_timer(e); }).focus();
598 $('#no_paginate').change(function() { self.update_results(); });
599 this.update_results();
601 update_results: function() {
604 url: 'controller.pl?action=Part/part_picker_result',
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;
611 success: function(data){
612 $('#part_picker_result').html(data);
617 init_results: function() {
619 $('div.part_picker_part').each(function(){
620 $(this).click(function(){
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(),
631 self.pp.$dummy.focus();
635 $('#part_selection').keydown(function(e){
636 if (e.which == KEY.ESCAPE) {
638 self.pp.$dummy.focus();
642 result_timer: function(event) {
644 if (!$('no_paginate').prop('checked')) {
645 if (event.keyCode == KEY.PAGE_UP) {
646 $('#part_picker_result a.paginate-prev').click();
649 if (event.keyCode == KEY.PAGE_DOWN) {
650 $('#part_picker_result a.paginate-next').click();
654 window.clearTimeout(this.timer);
655 if (event.which == KEY.ENTER) {
656 self.update_results();
658 this.timer = window.setTimeout(function() { self.update_results(); }, 100);
661 close_dialog: function() {
662 $('#part_selection').dialog('close');
666 ns.PickerMultiPopup = function(pp) {
668 this.callback = 'Part/add_multi_' + this.pp.o.part_type + '_items';
672 ns.PickerMultiPopup.prototype = {
673 open_dialog: function() {
675 $('#row_table_id thead a img').remove();
678 url: 'controller.pl?action=Part/show_multi_items_dialog',
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',
685 title: kivi.t8('Add multiple items'),
695 init_search: function() {
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();
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(); });
710 update_results: function() {
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;
719 $.each(Object.keys(ppdata), function() {data.push({ name: 'multi_items.' + this, value: ppdata[this]});});
721 url: 'controller.pl?action=Part/multi_items_update_result',
724 success: function(data){
725 $('#multi_items_result').html(data);
727 self.enable_continue();
731 set_qty_to_one: function(clicked) {
732 if ($(clicked).val() === '') {
733 $(clicked).val(kivi.format_amount(1.00, -2));
737 init_results: function() {
739 $('#multi_items_all_qty').change(function(event){
740 $('.multi_items_qty').val($(event.target).val());
742 $('.multi_items_qty').click(function(){ self.set_qty_to_one(this); });
744 result_timer: function() {
746 close_dialog: function() {
747 $('#jq_multi_items_dialog').dialog('close');
749 disable_continue: function() {
750 $('#multi_items_result input, #multi_items_position').off("keydown");
751 $('#continue_button').prop('disabled', true);
753 enable_continue: function() {
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();
762 $('#continue_button').prop('disabled', false);
764 add_multi_items: function() {
766 var n_rows = $('.multi_items_qty').length;
767 if ( n_rows === 0) { return; }
770 n_rows = $('.multi_items_qty').filter(function() {
771 return $(this).val().length > 0;
773 if (n_rows === 0) { return; }
775 this.disable_continue();
777 var data = $('#multi_items_form').serializeArray();
778 this.pp.set_multi_items(data);
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)));
788 kivi.run_once_for('#customerprice_rows', 'customerprice_row_sort_renumber', function(elt) {
789 $(elt).on('sortstop', kivi.Part.customerprice_renumber_positions);
792 kivi.run_once_for('#makemodel_rows', 'makemodel_row_sort_renumber', function(elt) {
793 $(elt).on('sortstop', kivi.Part.makemodel_renumber_positions);
797 ns.init = function() {
802 $('#ic').on('focusout', '.reformat_number', function(event) {
803 ns.reformat_number(event);
806 $('#part_warehouse_id').change(kivi.Part.reload_bin_selection);