1 namespace('kivi.DeliveryOrder', function(ns) {
2 ns.check_cv = function() {
3 if ($('#type').val() == 'sales_delivery_order') {
4 if ($('#order_customer_id').val() === '') {
5 alert(kivi.t8('Please select a customer.'));
9 if ($('#order_vendor_id').val() === '') {
10 alert(kivi.t8('Please select a vendor.'));
17 ns.check_duplicate_parts = function(question) {
18 var id_arr = $('[name="order.orderitems[].parts_id"]').map(function() { return this.value; }).get();
20 var i, obj = {}, pos = [];
22 for (i = 0; i < id_arr.length; i++) {
24 if (obj.hasOwnProperty(id)) {
31 question = question || kivi.t8("Do you really want to continue?");
32 return confirm(kivi.t8("There are duplicate parts at positions") + "\n"
33 + pos.join(', ') + "\n"
39 ns.check_valid_reqdate = function() {
40 if ($('#order_reqdate_as_date').val() === '') {
41 alert(kivi.t8('Please select a delivery date.'));
48 ns.save = function(action, warn_on_duplicates, warn_on_reqdate) {
49 if (!ns.check_cv()) return;
50 if (warn_on_duplicates && !ns.check_duplicate_parts()) return;
51 if (warn_on_reqdate && !ns.check_valid_reqdate()) return;
53 var data = $('#order_form').serializeArray();
54 data.push({ name: 'action', value: 'DeliveryOrder/' + action });
56 $.post("controller.pl", data, kivi.eval_json_result);
59 ns.delete_order = function() {
60 var data = $('#order_form').serializeArray();
61 data.push({ name: 'action', value: 'DeliveryOrder/delete' });
63 $.post("controller.pl", data, kivi.eval_json_result);
66 ns.show_print_options = function(warn_on_duplicates, warn_on_reqdate) {
67 if (!ns.check_cv()) return;
68 if (warn_on_duplicates && !ns.check_duplicate_parts(kivi.t8("Do you really want to print?"))) return;
69 if (warn_on_reqdate && !ns.check_valid_reqdate()) return;
74 title: kivi.t8('Print options'),
81 ns.print = function() {
82 $('#print_options').dialog('close');
84 var data = $('#order_form').serializeArray();
85 data = data.concat($('#print_options_form').serializeArray());
86 data.push({ name: 'action', value: 'DeliveryOrder/print' });
88 $.post("controller.pl", data, kivi.eval_json_result);
93 ns.setup_send_email_dialog = function() {
94 kivi.SalesPurchase.show_all_print_options_elements();
95 kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
97 $('#print_options_form table').first().remove().appendTo('#email_form_print_options');
99 var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
100 $('#email_form_' + to_focus).focus();
103 ns.finish_send_email_dialog = function() {
104 kivi.SalesPurchase.show_all_print_options_elements();
106 $('#email_form_print_options table').first().remove().prependTo('#print_options_form');
110 ns.show_email_dialog = function(html) {
111 var id = 'send_email_dialog';
112 var dialog_params = {
116 title: kivi.t8('Send email'),
118 beforeClose: kivi.DeliveryOrder.finish_send_email_dialog,
119 close: function(event, ui) {
120 email_dialog.remove();
124 $('#' + id).remove();
126 email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
127 email_dialog.html(html);
128 email_dialog.dialog(dialog_params);
130 kivi.DeliveryOrder.setup_send_email_dialog();
132 $('.cancel').click(ns.close_email_dialog);
137 ns.send_email = function() {
138 // push button only once -> slow response from mail server
139 ns.email_dialog_disable_send();
141 var data = $('#order_form').serializeArray();
142 data = data.concat($('[name^="email_form."]').serializeArray());
143 data = data.concat($('[name^="print_options."]').serializeArray());
144 data.push({ name: 'action', value: 'DeliveryOrder/send_email' });
145 $.post("controller.pl", data, kivi.eval_json_result);
148 ns.email_dialog_disable_send = function() {
149 // disable mail send event to prevent
150 // impatient users to send multiple times
151 $('#send_email').prop('disabled', true);
154 ns.close_email_dialog = function() {
155 email_dialog.dialog("close");
158 ns.set_number_in_title = function(elt) {
159 $('#nr_in_title').html($(elt).val());
162 ns.reload_cv_dependent_selections = function() {
163 $('#order_shipto_id').val('');
164 var data = $('#order_form').serializeArray();
165 data.push({ name: 'action', value: 'DeliveryOrder/customer_vendor_changed' });
167 $.post("controller.pl", data, kivi.eval_json_result);
170 ns.reformat_number = function(event) {
171 $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
174 ns.reformat_number_as_null_number = function(event) {
175 if ($(event.target).val() === '') {
178 ns.reformat_number(event);
181 ns.update_exchangerate = function(event) {
182 if (!ns.check_cv()) {
183 $('#order_currency_id').val($('#old_currency_id').val());
187 var rate_input = $('#order_exchangerate_as_null_number');
188 // unset exchangerate if currency changed
189 if ($('#order_currency_id').val() !== $('#old_currency_id').val()) {
193 // only set exchangerate if unset
194 if (rate_input.val() !== '') {
198 var data = $('#order_form').serializeArray();
199 data.push({ name: 'action', value: 'DeliveryOrder/update_exchangerate' });
202 url: 'controller.pl',
206 success: function(data){
207 if (!data.is_standard) {
208 $('#currency_name').text(data.currency_name);
209 if (data.exchangerate) {
210 rate_input.val(data.exchangerate);
214 $('#exchangerate_settings').show();
217 $('#exchangerate_settings').hide();
219 if ($('#order_currency_id').val() != $('#old_currency_id').val() ||
220 !data.is_standard && data.exchangerate != $('#old_exchangerate').val()) {
221 kivi.display_flash('warning', kivi.t8('You have changed the currency or exchange rate. Please check prices.'));
223 $('#old_currency_id').val($('#order_currency_id').val());
224 $('#old_exchangerate').val(data.exchangerate);
229 ns.exchangerate_changed = function(event) {
230 if (kivi.parse_amount($('#order_exchangerate_as_null_number').val()) != kivi.parse_amount($('#old_exchangerate').val())) {
231 kivi.display_flash('warning', kivi.t8('You have changed the currency or exchange rate. Please check prices.'));
232 $('#old_exchangerate').val($('#order_exchangerate_as_null_number').val());
236 ns.unit_change = function(event) {
237 var row = $(event.target).parents("tbody").first();
238 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
239 var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
240 var select_elt = $(row).find('[name="order.orderitems[].unit"]');
242 var oldval = $(select_elt).data('oldval');
243 $(select_elt).data('oldval', $(select_elt).val());
245 var data = $('#order_form').serializeArray();
246 data.push({ name: 'action', value: 'DeliveryOrder/unit_changed' },
247 { name: 'item_id', value: item_id_dom.val() },
248 { name: 'old_unit', value: oldval },
249 { name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
251 $.post("controller.pl", data, kivi.eval_json_result);
254 ns.update_sellprice = function(item_id, price_str) {
255 var row = $('#item_' + item_id).parents("tbody").first();
256 var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
257 var html_elt = $(row).find('[name="sellprice_text"]');
258 price_elt.val(price_str);
259 html_elt.html(price_str);
262 ns.load_second_row = function(row) {
263 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
264 var div_elt = $(row).find('[name="second_row"]');
266 if ($(div_elt).data('loaded') == 1) {
269 var data = $('#order_form').serializeArray();
270 data.push({ name: 'action', value: 'DeliveryOrder/load_second_rows' },
271 { name: 'item_ids[]', value: item_id_dom.val() });
273 $.post("controller.pl", data, kivi.eval_json_result);
276 ns.load_all_second_rows = function() {
277 var rows = $('.row_entry').filter(function(idx, elt) {
278 return $(elt).find('[name="second_row"]').data('loaded') != 1;
281 var item_ids = $.map(rows, function(elt) {
282 var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
283 return { name: 'item_ids[]', value: item_id };
286 if (item_ids.length == 0) {
290 var data = $('#order_form').serializeArray();
291 data.push({ name: 'action', value: 'DeliveryOrder/load_second_rows' });
292 data = data.concat(item_ids);
294 $.post("controller.pl", data, kivi.eval_json_result);
297 ns.hide_second_row = function(row) {
298 $(row).children().not(':first').hide();
299 $(row).data('expanded', 0);
300 var elt = $(row).find('.expand');
301 elt.attr('src', "image/expand.svg");
302 elt.attr('alt', kivi.t8('Show details'));
303 elt.attr('title', kivi.t8('Show details'));
306 ns.show_second_row = function(row) {
307 $(row).children().not(':first').show();
308 $(row).data('expanded', 1);
309 var elt = $(row).find('.expand');
310 elt.attr('src', "image/collapse.svg");
311 elt.attr('alt', kivi.t8('Hide details'));
312 elt.attr('title', kivi.t8('Hide details'));
315 ns.toggle_second_row = function(row) {
316 if ($(row).data('expanded') == 1) {
317 ns.hide_second_row(row);
319 ns.show_second_row(row);
323 ns.init_row_handlers = function() {
324 kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
325 $(elt).change(ns.reformat_number);
328 kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
329 $(elt).data('oldval', $(elt).val());
330 $(elt).change(ns.unit_change);
333 kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
334 $(elt).keydown(function(event) {
336 if (event.keyCode == 40 && event.shiftKey === true) {
338 event.preventDefault();
339 row = $(event.target).parents(".row_entry").first();
340 ns.load_second_row(row);
341 ns.show_second_row(row);
344 if (event.keyCode == 38 && event.shiftKey === true) {
346 event.preventDefault();
347 row = $(event.target).parents(".row_entry").first();
348 ns.hide_second_row(row);
354 kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
355 $(elt).click(function(event) {
356 event.preventDefault();
357 var row = $(event.target).parents(".row_entry").first();
358 ns.load_second_row(row);
359 ns.toggle_second_row(row);
366 ns.redisplay_line_values = function(is_sales, data) {
367 $('.row_entry').each(function(idx, elt) {
368 $(elt).find('[name="linetotal"]').html(data[idx][0]);
369 if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
370 var mt = data[idx][1];
371 var mp = data[idx][2];
373 if (mt[0] === '-') h += ' class="plus0"';
374 h += '>' + mt + ' ' + mp + '%';
376 $(elt).find('[name="linemargin"]').html(h);
381 ns.redisplay_cvpartnumbers = function(data) {
382 $('.row_entry').each(function(idx, elt) {
383 $(elt).find('[name="cvpartnumber"]').html(data[idx][0]);
387 ns.renumber_positions = function() {
388 $('.row_entry [name="position"]').each(function(idx, elt) {
391 $('.row_entry').each(function(idx, elt) {
392 $(elt).data("position", idx+1);
396 ns.reorder_items = function(order_by) {
397 var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
398 $('#row_table_id thead a img').remove();
403 src = "image/up.png";
406 src = "image/down.png";
409 $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
411 var data = $('#order_form').serializeArray();
412 data.push({ name: 'action', value: 'DeliveryOrder/reorder_items' },
413 { name: 'order_by', value: order_by },
414 { name: 'sort_dir', value: dir });
416 $.post("controller.pl", data, kivi.eval_json_result);
419 ns.redisplay_items = function(data) {
420 var old_rows = $('.row_entry').detach();
422 $(data).each(function(idx, elt) {
423 new_rows.push(old_rows[elt.old_pos - 1]);
425 $(new_rows).appendTo($('#row_table_id'));
426 ns.renumber_positions();
429 ns.get_insert_before_item_id = function(wanted_pos) {
430 if (wanted_pos === '') return;
432 var insert_before_item_id;
433 // selection by data does not seem to work if data is changed at runtime
434 // var elt = $('.row_entry [data-position="' + wanted_pos + '"]');
435 $('.row_entry').each(function(idx, elt) {
436 if ($(elt).data("position") == wanted_pos) {
437 insert_before_item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
442 return insert_before_item_id;
445 ns.add_item = function() {
446 if ($('#add_item_parts_id').val() === '') return;
447 if (!ns.check_cv()) return;
449 $('#row_table_id thead a img').remove();
451 var insert_before_item_id = ns.get_insert_before_item_id($('#add_item_position').val());
453 var data = $('#order_form').serializeArray();
454 data.push({ name: 'action', value: 'DeliveryOrder/add_item' },
455 { name: 'insert_before_item_id', value: insert_before_item_id });
457 $.post("controller.pl", data, kivi.eval_json_result);
460 ns.open_multi_items_dialog = function() {
461 if (!ns.check_cv()) return;
463 var pp = $("#add_item_parts_id").data("part_picker");
468 ns.add_multi_items = function(data) {
469 var insert_before_item_id = ns.get_insert_before_item_id($('#multi_items_position').val());
470 data = data.concat($('#order_form').serializeArray());
471 data.push({ name: 'action', value: 'DeliveryOrder/add_multi_items' },
472 { name: 'insert_before_item_id', value: insert_before_item_id });
473 $.post("controller.pl", data, kivi.eval_json_result);
476 ns.delete_order_item_row = function(clicked) {
477 var row = $(clicked).parents("tbody").first();
480 ns.renumber_positions();
483 ns.row_table_scroll_down = function() {
484 $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
487 ns.show_longdescription_dialog = function(clicked) {
488 var row = $(clicked).parents("tbody").first();
489 var position = $(row).find('[name="position"]').html();
490 var partnumber = $(row).find('[name="partnumber"]').html();
491 var description_elt = $(row).find('[name="order.orderitems[].description"]');
492 var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
495 runningnumber: position,
496 partnumber: partnumber,
497 description: description_elt.val(),
498 default_longdescription: longdescription_elt.val(),
499 set_function: function(val) {
500 longdescription_elt.val(val);
504 kivi.SalesPurchase.edit_longdescription_with_params(params);
507 ns.price_chooser_item_row = function(clicked) {
508 if (!ns.check_cv()) return;
509 var row = $(clicked).parents("tbody").first();
510 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
512 var data = $('#order_form').serializeArray();
513 data.push({ name: 'action', value: 'DeliveryOrder/price_popup' },
514 { name: 'item_id', value: item_id_dom.val() });
516 $.post("controller.pl", data, kivi.eval_json_result);
519 ns.show_vc_details_dialog = function() {
520 if (!ns.check_cv()) return;
524 if ($('#order_customer_id').val()) {
526 vc_id = $('#order_customer_id').val();
527 title = kivi.t8('Customer details');
530 vc_id = $('#order_vendor_id').val();
531 title = kivi.t8('Vendor details');
535 url: 'controller.pl',
536 data: { action: 'DeliveryOrder/show_customer_vendor_details_dialog',
537 type : $('#type').val(),
541 id: 'jq_customer_vendor_details_dialog',
551 ns.update_row_from_master_data = function(clicked) {
552 var row = $(clicked).parents("tbody").first();
553 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
555 var data = $('#order_form').serializeArray();
556 data.push({ name: 'action', value: 'DeliveryOrder/update_row_from_master_data' });
557 data.push({ name: 'item_ids[]', value: item_id_dom.val() });
559 $.post("controller.pl", data, kivi.eval_json_result);
562 ns.update_all_rows_from_master_data = function() {
563 var item_ids = $.map($('.row_entry'), function(elt) {
564 var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
565 return { name: 'item_ids[]', value: item_id };
568 if (item_ids.length == 0) {
572 var data = $('#order_form').serializeArray();
573 data.push({ name: 'action', value: 'DeliveryOrder/update_row_from_master_data' });
574 data = data.concat(item_ids);
576 $.post("controller.pl", data, kivi.eval_json_result);
579 ns.show_calculate_qty_dialog = function(clicked) {
580 var row = $(clicked).parents("tbody").first();
581 var input_id = $(row).find('[name="order.orderitems[].qty_as_number"]').attr('id');
582 var formula_id = $(row).find('[name="formula[+]"]').attr('id');
584 calculate_qty_selection_dialog("", input_id, "", formula_id);
588 ns.edit_custom_shipto = function() {
589 if (!ns.check_cv()) return;
591 kivi.SalesPurchase.edit_custom_shipto();
594 ns.purchase_order_check_for_direct_delivery = function() {
595 if ($('#type').val() != 'sales_order') {
596 kivi.submit_form_with_action($('#order_form'), 'DeliveryOrder/purchase_order');
601 if ($('#order_shipto_id').val() !== '') {
603 shipto = $('#order_shipto_id option:selected').text();
605 $('#shipto_inputs [id^="shipto"]').each(function(idx, elt) {
606 if (!empty) return true;
607 if (/^shipto_to_copy/.test($(elt).prop('id'))) return true;
608 if (/^shiptocp_gender/.test($(elt).prop('id'))) return true;
609 if (/^shiptocvar_/.test($(elt).prop('id'))) return true;
610 if ($(elt).val() !== '') {
615 var shipto_elements = [];
616 $([$('#shiptoname').val(), $('#shiptostreet').val(), $('#shiptozipcode').val(), $('#shiptocity').val()]).each(function(idx, elt) {
617 if (elt !== '') shipto_elements.push(elt);
619 shipto = shipto_elements.join('; ');
624 ns.direct_delivery_dialog(shipto);
626 kivi.submit_form_with_action($('#order_form'), 'DeliveryOrder/purchase_order');
630 ns.direct_delivery_callback = function(accepted) {
631 $('#direct-delivery-dialog').dialog('close');
634 $('<input type="hidden" name="use_shipto">').appendTo('#order_form').val('1');
637 kivi.submit_form_with_action($('#order_form'), 'DeliveryOrder/purchase_order');
640 ns.direct_delivery_dialog = function(shipto) {
641 $('#direct-delivery-dialog').remove();
643 var text1 = kivi.t8('You have entered or selected the following shipping address for this customer:');
644 var text2 = kivi.t8('Do you want to carry this shipping address over to the new purchase order so that the vendor can deliver the goods directly to your customer?');
645 var html = '<div id="direct-delivery-dialog"><p>' + text1 + '</p><p>' + shipto + '</p><p>' + text2 + '</p>';
646 html = html + '<hr><p>';
647 html = html + '<input type="button" value="' + kivi.t8('Yes') + '" size="30" onclick="kivi.DeliveryOrder.direct_delivery_callback(true)">';
648 html = html + ' ';
649 html = html + '<input type="button" value="' + kivi.t8('No') + '" size="30" onclick="kivi.DeliveryOrder.direct_delivery_callback(false)">';
650 html = html + '</p></div>';
651 $(html).hide().appendTo('#order_form');
653 kivi.popup_dialog({id: 'direct-delivery-dialog',
654 dialog: {title: kivi.t8('Carry over shipping address'),
659 ns.follow_up_window = function() {
660 var id = $('#id').val();
661 var type = $('#type').val();
663 var number_info = '';
664 if ($('#type').val() == 'sales_order' || $('#type').val() == 'purchase_order') {
665 number_info = $('#order_ordnumber').val();
666 } else if ($('#type').val() == 'sales_quotation' || $('#type').val() == 'request_quotation') {
667 number_info = $('#order_quonumber').val();
671 if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation') {
672 name_info = $('#order_customer_id_name').val();
673 } else if ($('#type').val() == 'purchase_order' || $('#type').val() == 'request_quotation') {
674 name_info = $('#order_vendor_id_name').val();
678 if (number_info !== '') { info += ' (' + number_info + ')' }
679 if (name_info !== '') { info += ' (' + name_info + ')' }
681 if (!$('#follow_up_rowcount').lenght) {
682 $('<input type="hidden" name="follow_up_rowcount" id="follow_up_rowcount">').appendTo('#order_form');
683 $('<input type="hidden" name="follow_up_trans_id_1" id="follow_up_trans_id_1">').appendTo('#order_form');
684 $('<input type="hidden" name="follow_up_trans_type_1" id="follow_up_trans_type_1">').appendTo('#order_form');
685 $('<input type="hidden" name="follow_up_trans_info_1" id="follow_up_trans_info_1">').appendTo('#order_form');
686 $('<input type="hidden" name="follow_up_trans_subject_1" id="follow_up_trans_subject_1">').appendTo('#order_form');
688 $('#follow_up_rowcount').val(1);
689 $('#follow_up_trans_id_1').val(id);
690 $('#follow_up_trans_type_1').val(type);
691 $('#follow_up_trans_info_1').val(info);
692 $('#follow_up_trans_subject_1').val($('#order_transaction_description').val());
697 ns.create_part = function() {
698 var data = $('#order_form').serializeArray();
699 data.push({ name: 'action', value: 'DeliveryOrder/create_part' });
701 $.post("controller.pl", data, kivi.eval_json_result);
707 $('#order_customer_id').change(kivi.DeliveryOrder.reload_cv_dependent_selections);
708 $('#order_vendor_id').change(kivi.DeliveryOrder.reload_cv_dependent_selections);
710 $('#order_currency_id').change(kivi.DeliveryOrder.update_exchangerate);
711 $('#order_transdate_as_date').change(kivi.DeliveryOrder.update_exchangerate);
712 $('#order_exchangerate_as_null_number').change(kivi.DeliveryOrder.exchangerate_changed);
714 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
715 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
717 $('.add_item_input').keydown(function(event) {
718 if (event.keyCode == 13) {
719 event.preventDefault();
720 kivi.DeliveryOrder.add_item();
725 kivi.DeliveryOrder.init_row_handlers();
727 $('#row_table_id').on('sortstop', function(event, ui) {
728 $('#row_table_id thead a img').remove();
729 kivi.DeliveryOrder.renumber_positions();
732 $('#expand_all').on('click', function(event) {
733 event.preventDefault();
734 if ($('#expand_all').data('expanded') == 1) {
735 $('#expand_all').data('expanded', 0);
736 $('#expand_all').attr('src', 'image/expand.svg');
737 $('#expand_all').attr('alt', kivi.t8('Show all details'));
738 $('#expand_all').attr('title', kivi.t8('Show all details'));
739 $('.row_entry').each(function(idx, elt) {
740 kivi.DeliveryOrder.hide_second_row(elt);
743 $('#expand_all').data('expanded', 1);
744 $('#expand_all').attr('src', "image/collapse.svg");
745 $('#expand_all').attr('alt', kivi.t8('Hide all details'));
746 $('#expand_all').attr('title', kivi.t8('Hide all details'));
747 kivi.DeliveryOrder.load_all_second_rows();
748 $('.row_entry').each(function(idx, elt) {
749 kivi.DeliveryOrder.show_second_row(elt);
755 $('.reformat_number_as_null_number').change(kivi.DeliveryOrder.reformat_number_as_null_number);