1 namespace('kivi.Order', function(ns) {
2 ns.check_cv = function() {
3 if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation') {
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 save?");
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: 'order.language_id', value: $('#language_id').val() }); // language from print options
55 data.push({ name: 'action', value: 'Order/' + action });
57 $.post("controller.pl", data, kivi.eval_json_result);
60 ns.delete_order = function() {
61 var data = $('#order_form').serializeArray();
62 data.push({ name: 'action', value: 'Order/delete' });
64 $.post("controller.pl", data, kivi.eval_json_result);
67 ns.show_print_options = function(warn_on_duplicates) {
68 if (!ns.check_cv()) return;
69 if (warn_on_duplicates && !ns.check_duplicate_parts(kivi.t8("Do you really want to print?"))) 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: 'order.language_id', value: $('#language_id').val() }); // language from print options
87 data.push({ name: 'action', value: 'Order/print' });
89 $.post("controller.pl", data, kivi.eval_json_result);
92 ns.email = function(warn_on_duplicates) {
93 if (warn_on_duplicates && !ns.check_duplicate_parts(kivi.t8("Do you really want to send by mail?"))) return;
94 if (!ns.check_cv()) return;
96 var data = $('#order_form').serializeArray();
97 data.push({ name: 'order.language_id', value: $('#language_id').val() }); // language from print options
98 data.push({ name: 'action', value: 'Order/show_email_dialog' });
100 $.post("controller.pl", data, kivi.eval_json_result);
105 ns.setup_send_email_dialog = function() {
106 kivi.SalesPurchase.show_all_print_options_elements();
107 kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
109 $('#print_options_form table').first().remove().appendTo('#email_form_print_options');
111 var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
112 $('#email_form_' + to_focus).focus();
115 ns.finish_send_email_dialog = function() {
116 kivi.SalesPurchase.show_all_print_options_elements();
118 $('#email_form_print_options table').first().remove().prependTo('#print_options_form');
122 ns.show_email_dialog = function(html) {
123 var id = 'send_email_dialog';
124 var dialog_params = {
128 title: kivi.t8('Send email'),
130 beforeClose: kivi.Order.finish_send_email_dialog,
131 close: function(event, ui) {
132 email_dialog.remove();
136 $('#' + id).remove();
138 email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
139 email_dialog.html(html);
140 email_dialog.dialog(dialog_params);
142 kivi.Order.setup_send_email_dialog();
144 $('.cancel').click(ns.close_email_dialog);
149 ns.send_email = function() {
150 var data = $('#order_form').serializeArray();
151 data = data.concat($('[name^="email_form."]').serializeArray());
152 data = data.concat($('[name^="print_options."]').serializeArray());
153 data.push({ name: 'order.language_id', value: $('#language_id').val() }); // language from print options
154 data.push({ name: 'action', value: 'Order/send_email' });
155 $.post("controller.pl", data, kivi.eval_json_result);
158 ns.close_email_dialog = function() {
159 email_dialog.dialog("close");
162 ns.set_number_in_title = function(elt) {
163 $('#nr_in_title').html($(elt).val());
166 ns.reload_cv_dependent_selections = function() {
167 $('#order_shipto_id').val('');
168 var data = $('#order_form').serializeArray();
169 data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
171 $.post("controller.pl", data, kivi.eval_json_result);
174 ns.reformat_number = function(event) {
175 $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
178 ns.reformat_number_as_null_number = function(event) {
179 if ($(event.target).val() === '') {
182 ns.reformat_number(event);
185 ns.update_exchangerate = function(event) {
186 if (!ns.check_cv()) {
187 $('#order_currency_id').val($('#old_currency_id').val());
191 var rate_input = $('#order_exchangerate_as_null_number');
192 // unset exchangerate if currency changed
193 if ($('#order_currency_id').val() !== $('#old_currency_id').val()) {
197 // only set exchangerate if unset
198 if (rate_input.val() !== '') {
202 var data = $('#order_form').serializeArray();
203 data.push({ name: 'action', value: 'Order/update_exchangerate' });
206 url: 'controller.pl',
210 success: function(data){
211 if (!data.is_standard) {
212 $('#currency_name').text(data.currency_name);
213 if (data.exchangerate) {
214 rate_input.val(data.exchangerate);
218 $('#exchangerate_settings').show();
221 $('#exchangerate_settings').hide();
223 if ($('#order_currency_id').val() != $('#old_currency_id').val() ||
224 !data.is_standard && data.exchangerate != $('#old_exchangerate').val()) {
225 kivi.display_flash('warning', kivi.t8('You have changed the currency or exchange rate. Please check prices.'));
227 $('#old_currency_id').val($('#order_currency_id').val());
228 $('#old_exchangerate').val(data.exchangerate);
233 ns.exchangerate_changed = function(event) {
234 if (kivi.parse_amount($('#order_exchangerate_as_null_number').val()) != kivi.parse_amount($('#old_exchangerate').val())) {
235 kivi.display_flash('warning', kivi.t8('You have changed the currency or exchange rate. Please check prices.'));
236 $('#old_exchangerate').val($('#order_exchangerate_as_null_number').val());
240 ns.recalc_amounts_and_taxes = function() {
241 var data = $('#order_form').serializeArray();
242 data.push({ name: 'action', value: 'Order/recalc_amounts_and_taxes' });
244 $.post("controller.pl", data, kivi.eval_json_result);
247 ns.unit_change = function(event) {
248 var row = $(event.target).parents("tbody").first();
249 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
250 var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
251 var select_elt = $(row).find('[name="order.orderitems[].unit"]');
253 var oldval = $(select_elt).data('oldval');
254 $(select_elt).data('oldval', $(select_elt).val());
256 var data = $('#order_form').serializeArray();
257 data.push({ name: 'action', value: 'Order/unit_changed' },
258 { name: 'item_id', value: item_id_dom.val() },
259 { name: 'old_unit', value: oldval },
260 { name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
262 $.post("controller.pl", data, kivi.eval_json_result);
265 ns.update_sellprice = function(item_id, price_str) {
266 var row = $('#item_' + item_id).parents("tbody").first();
267 var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
268 var html_elt = $(row).find('[name="sellprice_text"]');
269 price_elt.val(price_str);
270 html_elt.html(price_str);
273 ns.load_second_row = function(row) {
274 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
275 var div_elt = $(row).find('[name="second_row"]');
277 if ($(div_elt).data('loaded') == 1) {
280 var data = $('#order_form').serializeArray();
281 data.push({ name: 'action', value: 'Order/load_second_rows' },
282 { name: 'item_ids[]', value: item_id_dom.val() });
284 $.post("controller.pl", data, kivi.eval_json_result);
287 ns.load_all_second_rows = function() {
288 var rows = $('.row_entry').filter(function(idx, elt) {
289 return $(elt).find('[name="second_row"]').data('loaded') != 1;
292 var item_ids = $.map(rows, function(elt) {
293 var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
294 return { name: 'item_ids[]', value: item_id };
297 if (item_ids.length == 0) {
301 var data = $('#order_form').serializeArray();
302 data.push({ name: 'action', value: 'Order/load_second_rows' });
303 data = data.concat(item_ids);
305 $.post("controller.pl", data, kivi.eval_json_result);
308 ns.hide_second_row = function(row) {
309 $(row).children().not(':first').hide();
310 $(row).data('expanded', 0);
311 var elt = $(row).find('.expand');
312 elt.attr('src', "image/expand.svg");
313 elt.attr('alt', kivi.t8('Show details'));
314 elt.attr('title', kivi.t8('Show details'));
317 ns.show_second_row = function(row) {
318 $(row).children().not(':first').show();
319 $(row).data('expanded', 1);
320 var elt = $(row).find('.expand');
321 elt.attr('src', "image/collapse.svg");
322 elt.attr('alt', kivi.t8('Hide details'));
323 elt.attr('title', kivi.t8('Hide details'));
326 ns.toggle_second_row = function(row) {
327 if ($(row).data('expanded') == 1) {
328 ns.hide_second_row(row);
330 ns.show_second_row(row);
334 ns.init_row_handlers = function() {
335 kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
336 $(elt).change(ns.recalc_amounts_and_taxes);
339 kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
340 $(elt).change(ns.reformat_number);
343 kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
344 $(elt).data('oldval', $(elt).val());
345 $(elt).change(ns.unit_change);
348 kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
349 $(elt).keydown(function(event) {
351 if (event.keyCode == 40 && event.shiftKey === true) {
353 event.preventDefault();
354 row = $(event.target).parents(".row_entry").first();
355 ns.load_second_row(row);
356 ns.show_second_row(row);
359 if (event.keyCode == 38 && event.shiftKey === true) {
361 event.preventDefault();
362 row = $(event.target).parents(".row_entry").first();
363 ns.hide_second_row(row);
369 kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
370 $(elt).click(function(event) {
371 event.preventDefault();
372 var row = $(event.target).parents(".row_entry").first();
373 ns.load_second_row(row);
374 ns.toggle_second_row(row);
381 ns.redisplay_line_values = function(is_sales, data) {
382 $('.row_entry').each(function(idx, elt) {
383 $(elt).find('[name="linetotal"]').html(data[idx][0]);
384 if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
385 var mt = data[idx][1];
386 var mp = data[idx][2];
388 if (mt[0] === '-') h += ' class="plus0"';
389 h += '>' + mt + ' ' + mp + '%';
391 $(elt).find('[name="linemargin"]').html(h);
396 ns.redisplay_cvpartnumbers = function(data) {
397 $('.row_entry').each(function(idx, elt) {
398 $(elt).find('[name="cvpartnumber"]').html(data[idx][0]);
402 ns.renumber_positions = function() {
403 $('.row_entry [name="position"]').each(function(idx, elt) {
406 $('.row_entry').each(function(idx, elt) {
407 $(elt).data("position", idx+1);
411 ns.reorder_items = function(order_by) {
412 var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
413 $('#row_table_id thead a img').remove();
418 src = "image/up.png";
421 src = "image/down.png";
424 $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
426 var data = $('#order_form').serializeArray();
427 data.push({ name: 'action', value: 'Order/reorder_items' },
428 { name: 'order_by', value: order_by },
429 { name: 'sort_dir', value: dir });
431 $.post("controller.pl", data, kivi.eval_json_result);
434 ns.redisplay_items = function(data) {
435 var old_rows = $('.row_entry').detach();
437 $(data).each(function(idx, elt) {
438 new_rows.push(old_rows[elt.old_pos - 1]);
440 $(new_rows).appendTo($('#row_table_id'));
441 ns.renumber_positions();
444 ns.get_insert_before_item_id = function(wanted_pos) {
445 if (wanted_pos === '') return;
447 var insert_before_item_id;
448 // selection by data does not seem to work if data is changed at runtime
449 // var elt = $('.row_entry [data-position="' + wanted_pos + '"]');
450 $('.row_entry').each(function(idx, elt) {
451 if ($(elt).data("position") == wanted_pos) {
452 insert_before_item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
457 return insert_before_item_id;
460 ns.add_item = function() {
461 if ($('#add_item_parts_id').val() === '') return;
462 if (!ns.check_cv()) return;
464 $('#row_table_id thead a img').remove();
466 var insert_before_item_id = ns.get_insert_before_item_id($('#add_item_position').val());
468 var data = $('#order_form').serializeArray();
469 data.push({ name: 'action', value: 'Order/add_item' },
470 { name: 'insert_before_item_id', value: insert_before_item_id });
472 $.post("controller.pl", data, kivi.eval_json_result);
475 ns.setup_multi_items_dialog = function() {
476 $('#multi_items_filter_table input, #multi_items_filter_table select').keydown(function(event) {
477 if (event.keyCode == 13) {
478 event.preventDefault();
479 ns.multi_items_dialog_update_result();
484 $('#multi_items_filter_all_substr_multi_ilike').focus();
487 ns.show_multi_items_dialog = function() {
488 if (!ns.check_cv()) return;
490 $('#row_table_id thead a img').remove();
493 url: 'controller.pl?action=Order/show_multi_items_dialog',
494 data: { type: $('#type').val() },
495 id: 'jq_multi_items_dialog',
496 load: kivi.Order.setup_multi_items_dialog,
498 title: kivi.t8('Add multiple items'),
506 ns.close_multi_items_dialog = function() {
507 $('#jq_multi_items_dialog').dialog('close');
510 ns.multi_items_dialog_update_result = function() {
511 var data = $('#multi_items_form').serializeArray();
512 data.push({ name: 'type', value: $('#type').val() });
514 url: 'controller.pl?action=Order/multi_items_update_result',
517 success: function(data) {
518 $('#multi_items_result').html(data);
519 ns.multi_items_dialog_enable_continue();
520 ns.multi_items_result_setup_events();
525 ns.multi_items_dialog_disable_continue = function() {
526 // disable keydown-event and continue button to prevent
527 // impatient users to add parts multiple times
528 $('#multi_items_result input, #multi_items_position').off("keydown");
529 $('#multi_items_dialog_continue_button').prop('disabled', true);
532 ns.multi_items_dialog_enable_continue = function() {
533 $('#multi_items_result input, #multi_items_position').keydown(function(event) {
534 if(event.keyCode == 13) {
535 event.preventDefault();
536 ns.add_multi_items();
540 $('#multi_items_dialog_continue_button').prop('disabled', false);
543 ns.multi_items_result_setup_events = function() {
544 $('#multi_items_all_qty').change(ns.reformat_number);
545 $('#multi_items_all_qty').change(function(event) {
546 $('.multi_items_qty').val($(event.target).val());
548 $('.multi_items_qty').change(ns.reformat_number);
551 ns.add_multi_items = function() {
553 var n_rows = $('.multi_items_qty').length;
554 if (n_rows == 0) return;
557 n_rows = $('.multi_items_qty').filter(function() {
558 return $(this).val().length > 0;
560 if (n_rows == 0) return;
562 ns.multi_items_dialog_disable_continue();
564 var insert_before_item_id = ns.get_insert_before_item_id($('#multi_items_position').val());
566 var data = $('#order_form').serializeArray();
567 data = data.concat($('#multi_items_form').serializeArray());
568 data.push({ name: 'action', value: 'Order/add_multi_items' },
569 { name: 'insert_before_item_id', value: insert_before_item_id });
570 $.post("controller.pl", data, kivi.eval_json_result);
573 ns.set_input_to_one = function(clicked) {
574 if ($(clicked).val() == '') {
575 $(clicked).val(kivi.format_amount(1.00, -2));
580 ns.delete_order_item_row = function(clicked) {
581 var row = $(clicked).parents("tbody").first();
584 ns.renumber_positions();
585 ns.recalc_amounts_and_taxes();
588 ns.row_table_scroll_down = function() {
589 $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
592 ns.show_longdescription_dialog = function(clicked) {
593 var row = $(clicked).parents("tbody").first();
594 var position = $(row).find('[name="position"]').html();
595 var partnumber = $(row).find('[name="partnumber"]').html();
596 var description_elt = $(row).find('[name="order.orderitems[].description"]');
597 var description = description_elt.val();
598 var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
601 if (!longdescription_elt.length) {
603 { name: 'action', value: 'Order/get_item_longdescription' },
604 { name: 'type', value: $('#type').val() },
605 { name: 'item_id', value: $(row).find('[name="order.orderitems[+].id"]').val() },
606 { name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() }
610 url: 'controller.pl',
615 success: function(val) {
616 longdescription = val;
620 longdescription = longdescription_elt.val();
624 runningnumber: position,
625 partnumber: partnumber,
626 description: description,
627 default_longdescription: longdescription,
628 set_function: function(val) {
629 longdescription_elt.remove();
630 $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
634 kivi.SalesPurchase.edit_longdescription_with_params(params);
637 ns.price_chooser_item_row = function(clicked) {
638 if (!ns.check_cv()) return;
639 var row = $(clicked).parents("tbody").first();
640 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
642 var data = $('#order_form').serializeArray();
643 data.push({ name: 'action', value: 'Order/price_popup' },
644 { name: 'item_id', value: item_id_dom.val() });
646 $.post("controller.pl", data, kivi.eval_json_result);
649 ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
650 var row = $('#item_' + item_id).parents("tbody").first();
651 var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
652 var button_elt = $(row).find('[name="price_chooser_button"]');
654 button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
655 source_elt.val(source);
657 var editable_div_elt = $(row).find('[name="editable_price"]');
658 var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
659 if (price_editable == 1 && source === '') {
661 $(editable_div_elt).show();
662 $(not_editable_div_elt).hide();
663 $(editable_div_elt).find(':input').prop("disabled", false);
664 $(not_editable_div_elt).find(':input').prop("disabled", true);
667 $(editable_div_elt).hide();
668 $(not_editable_div_elt).show();
669 $(editable_div_elt).find(':input').prop("disabled", true);
670 $(not_editable_div_elt).find(':input').prop("disabled", false);
674 var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
675 var html_elt = $(row).find('[name="sellprice_text"]');
676 price_elt.val(price_str);
677 html_elt.html(price_str);
678 ns.recalc_amounts_and_taxes();
681 kivi.io.close_dialog();
684 ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
685 var row = $('#item_' + item_id).parents("tbody").first();
686 var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
687 var button_elt = $(row).find('[name="price_chooser_button"]');
689 button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
690 source_elt.val(source);
692 var editable_div_elt = $(row).find('[name="editable_discount"]');
693 var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
694 if (price_editable == 1 && source === '') {
696 $(editable_div_elt).show();
697 $(not_editable_div_elt).hide();
698 $(editable_div_elt).find(':input').prop("disabled", false);
699 $(not_editable_div_elt).find(':input').prop("disabled", true);
702 $(editable_div_elt).hide();
703 $(not_editable_div_elt).show();
704 $(editable_div_elt).find(':input').prop("disabled", true);
705 $(not_editable_div_elt).find(':input').prop("disabled", false);
709 var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
710 var html_elt = $(row).find('[name="discount_text"]');
711 discount_elt.val(discount_str);
712 html_elt.html(discount_str);
713 ns.recalc_amounts_and_taxes();
716 kivi.io.close_dialog();
719 ns.show_periodic_invoices_config_dialog = function() {
720 if ($('#type').val() !== 'sales_order') return;
723 url: 'controller.pl?action=Order/show_periodic_invoices_config_dialog',
724 data: { type: $('#type').val(),
726 config: $('#order_periodic_invoices_config').val(),
727 customer_id: $('#order_customer_id').val(),
728 transdate_as_date: $('#order_transdate_as_date').val(),
729 language_id: $('#language_id').val()
731 id: 'jq_periodic_invoices_config_dialog',
732 load: kivi.reinit_widgets,
734 title: kivi.t8('Edit the configuration for periodic invoices'),
742 ns.close_periodic_invoices_config_dialog = function() {
743 $('#jq_periodic_invoices_config_dialog').dialog('close');
746 ns.assign_periodic_invoices_config = function() {
747 var data = $('[name="Form"]').serializeArray();
748 data.push({ name: 'type', value: $('#type').val() },
749 { name: 'action', value: 'Order/assign_periodic_invoices_config' });
750 $.post("controller.pl", data, kivi.eval_json_result);
753 ns.check_save_active_periodic_invoices = function() {
754 var type = $('#type').val();
755 if (type !== 'sales_order') return true;
759 url: 'controller.pl',
760 data: { action: 'Order/get_has_active_periodic_invoices',
763 config: $('#order_periodic_invoices_config').val(),
768 success: function(val) {
774 return confirm(kivi.t8('This sales order has an active configuration for periodic invoices. If you save then all subsequently created invoices will contain those changes as well, but not those that have already been created. Do you want to continue?'));
780 ns.show_vc_details_dialog = function() {
781 if (!ns.check_cv()) return;
785 if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
787 vc_id = $('#order_customer_id').val();
788 title = kivi.t8('Customer details');
791 vc_id = $('#order_vendor_id').val();
792 title = kivi.t8('Vendor details');
796 url: 'controller.pl',
797 data: { action: 'Order/show_customer_vendor_details_dialog',
798 type : $('#type').val(),
802 id: 'jq_customer_vendor_details_dialog',
812 ns.update_row_from_master_data = function(clicked) {
813 var row = $(clicked).parents("tbody").first();
814 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
816 var data = $('#order_form').serializeArray();
817 data.push({ name: 'action', value: 'Order/update_row_from_master_data' });
818 data.push({ name: 'item_ids[]', value: item_id_dom.val() });
820 $.post("controller.pl", data, kivi.eval_json_result);
823 ns.update_all_rows_from_master_data = function() {
824 var item_ids = $.map($('.row_entry'), function(elt) {
825 var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
826 return { name: 'item_ids[]', value: item_id };
829 if (item_ids.length == 0) {
833 var data = $('#order_form').serializeArray();
834 data.push({ name: 'action', value: 'Order/update_row_from_master_data' });
835 data = data.concat(item_ids);
837 $.post("controller.pl", data, kivi.eval_json_result);
840 ns.show_calculate_qty_dialog = function(clicked) {
841 var row = $(clicked).parents("tbody").first();
842 var input_id = $(row).find('[name="order.orderitems[].qty_as_number"]').attr('id');
843 var formula_id = $(row).find('[name="formula[+]"]').attr('id');
845 calculate_qty_selection_dialog("", input_id, "", formula_id);
849 ns.edit_custom_shipto = function() {
850 if (!ns.check_cv()) return;
852 kivi.SalesPurchase.edit_custom_shipto();
855 ns.purchase_order_check_for_direct_delivery = function() {
856 if ($('#type').val() != 'sales_order') {
857 kivi.submit_form_with_action($('#order_form'), 'Order/purchase_order');
862 if ($('#order_shipto_id').val() !== '') {
864 shipto = $('#order_shipto_id option:selected').text();
866 $('#shipto_inputs [id^="shipto"]').each(function(idx, elt) {
867 if (!empty) return true;
868 if (/^shipto_to_copy/.test($(elt).prop('id'))) return true;
869 if (/^shiptocp_gender/.test($(elt).prop('id'))) return true;
870 if (/^shiptocvar_/.test($(elt).prop('id'))) return true;
871 if ($(elt).val() !== '') {
876 var shipto_elements = [];
877 $([$('#shiptoname').val(), $('#shiptostreet').val(), $('#shiptozipcode').val(), $('#shiptocity').val()]).each(function(idx, elt) {
878 if (elt !== '') shipto_elements.push(elt);
880 shipto = shipto_elements.join('; ');
885 ns.direct_delivery_dialog(shipto);
887 kivi.submit_form_with_action($('#order_form'), 'Order/purchase_order');
891 ns.direct_delivery_callback = function(accepted) {
892 $('#direct-delivery-dialog').dialog('close');
895 $('<input type="hidden" name="use_shipto">').appendTo('#order_form').val('1');
898 kivi.submit_form_with_action($('#order_form'), 'Order/purchase_order');
901 ns.direct_delivery_dialog = function(shipto) {
902 $('#direct-delivery-dialog').remove();
904 var text1 = kivi.t8('You have entered or selected the following shipping address for this customer:');
905 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?');
906 var html = '<div id="direct-delivery-dialog"><p>' + text1 + '</p><p>' + shipto + '</p><p>' + text2 + '</p>';
907 html = html + '<hr><p>';
908 html = html + '<input type="button" value="' + kivi.t8('Yes') + '" size="30" onclick="kivi.Order.direct_delivery_callback(true)">';
909 html = html + ' ';
910 html = html + '<input type="button" value="' + kivi.t8('No') + '" size="30" onclick="kivi.Order.direct_delivery_callback(false)">';
911 html = html + '</p></div>';
912 $(html).hide().appendTo('#order_form');
914 kivi.popup_dialog({id: 'direct-delivery-dialog',
915 dialog: {title: kivi.t8('Carry over shipping address'),
923 if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
924 $('#order_customer_id').change(kivi.Order.reload_cv_dependent_selections);
926 $('#order_vendor_id').change(kivi.Order.reload_cv_dependent_selections);
929 $('#order_currency_id').change(kivi.Order.update_exchangerate);
930 $('#order_transdate_as_date').change(kivi.Order.update_exchangerate);
931 $('#order_exchangerate_as_null_number').change(kivi.Order.exchangerate_changed);
933 if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
934 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
936 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
938 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
939 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
941 $('.add_item_input').keydown(function(event) {
942 if (event.keyCode == 13) {
943 event.preventDefault();
944 kivi.Order.add_item();
949 kivi.Order.init_row_handlers();
951 $('#row_table_id').on('sortstop', function(event, ui) {
952 $('#row_table_id thead a img').remove();
953 kivi.Order.renumber_positions();
956 $('#expand_all').on('click', function(event) {
957 event.preventDefault();
958 if ($('#expand_all').data('expanded') == 1) {
959 $('#expand_all').data('expanded', 0);
960 $('#expand_all').attr('src', 'image/expand.svg');
961 $('#expand_all').attr('alt', kivi.t8('Show all details'));
962 $('#expand_all').attr('title', kivi.t8('Show all details'));
963 $('.row_entry').each(function(idx, elt) {
964 kivi.Order.hide_second_row(elt);
967 $('#expand_all').data('expanded', 1);
968 $('#expand_all').attr('src', "image/collapse.svg");
969 $('#expand_all').attr('alt', kivi.t8('Hide all details'));
970 $('#expand_all').attr('title', kivi.t8('Hide all details'));
971 kivi.Order.load_all_second_rows();
972 $('.row_entry').each(function(idx, elt) {
973 kivi.Order.show_second_row(elt);
979 $('.reformat_number_as_null_number').change(kivi.Order.reformat_number_as_null_number);