1 namespace('kivi.Order', function(ns) {
2 ns.check_cv = function() {
3 if ($('#type').val() == 'sales_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_save_duplicate_parts = function() {
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 return confirm(kivi.t8("There are duplicate parts at positions") + "\n"
32 + pos.join(', ') + "\n"
33 + kivi.t8("Do you really want to save?"));
38 ns.save = function(warn_on_duplicates) {
39 if (!ns.check_cv()) return;
40 if (warn_on_duplicates && !ns.check_save_duplicate_parts()) return;
42 var data = $('#order_form').serializeArray();
43 data.push({ name: 'action', value: 'Order/save' });
45 $.post("controller.pl", data, kivi.eval_json_result);
48 ns.save_and_delivery_order = function(warn_on_duplicates) {
49 if (!ns.check_cv()) return;
50 if (warn_on_duplicates && !ns.check_save_duplicate_parts()) return;
52 var data = $('#order_form').serializeArray();
53 data.push({ name: 'action', value: 'Order/save_and_delivery_order' });
55 $.post("controller.pl", data, kivi.eval_json_result);
58 ns.save_and_invoice = function(warn_on_duplicates) {
59 if (!ns.check_cv()) return;
60 if (warn_on_duplicates && !ns.check_save_duplicate_parts()) return;
62 var data = $('#order_form').serializeArray();
63 data.push({ name: 'action', value: 'Order/save_and_invoice' });
65 $.post("controller.pl", data, kivi.eval_json_result);
68 ns.delete_order = function() {
69 var data = $('#order_form').serializeArray();
70 data.push({ name: 'action', value: 'Order/delete' });
72 $.post("controller.pl", data, kivi.eval_json_result);
75 ns.show_print_options = function() {
76 if (!ns.check_cv()) return;
81 title: kivi.t8('Print options'),
88 ns.print = function() {
89 $('#print_options').dialog('close');
91 var data = $('#order_form').serializeArray();
92 data = data.concat($('#print_options_form').serializeArray());
93 data.push({ name: 'action', value: 'Order/print' });
95 $.post("controller.pl", data, kivi.eval_json_result);
98 ns.download_pdf = function(pdf_filename, key) {
100 data.push({ name: 'action', value: 'Order/download_pdf' });
101 data.push({ name: 'type', value: $('#type').val() });
102 data.push({ name: 'pdf_filename', value: pdf_filename });
103 data.push({ name: 'key', value: key });
104 $.download("controller.pl", data);
107 ns.email = function() {
108 if (!ns.check_cv()) return;
109 var data = $('#order_form').serializeArray();
110 data.push({ name: 'action', value: 'Order/show_email_dialog' });
112 $.post("controller.pl", data, kivi.eval_json_result);
117 ns.show_email_dialog = function(html) {
118 var id = 'jqueryui_popup_dialog';
119 var dialog_params = {
124 close: function(event, ui) {
125 email_dialog.remove();
129 $('#' + id).remove();
131 email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
132 email_dialog.html(html);
133 email_dialog.dialog(dialog_params);
135 $('.cancel').click(ns.close_email_dialog);
140 ns.send_email = function() {
141 var data = $('#order_form').serializeArray();
142 data = data.concat($('#email_form').serializeArray());
143 data.push({ name: 'action', value: 'Order/send_email' });
144 $.post("controller.pl", data, kivi.eval_json_result);
147 ns.close_email_dialog = function() {
148 email_dialog.dialog("close");
151 ns.reload_cv_dependant_selections = function() {
152 var data = $('#order_form').serializeArray();
153 data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
155 $.post("controller.pl", data, kivi.eval_json_result);
158 ns.reformat_number = function(event) {
159 $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
162 ns.recalc_amounts_and_taxes = function() {
163 var data = $('#order_form').serializeArray();
164 data.push({ name: 'action', value: 'Order/recalc_amounts_and_taxes' });
166 $.post("controller.pl", data, kivi.eval_json_result);
169 ns.unit_change = function(event) {
170 var row = $(event.target).parents("tbody").first();
171 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
172 var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
173 var select_elt = $(row).find('[name="order.orderitems[].unit"]');
175 var oldval = $(select_elt).data('oldval');
176 $(select_elt).data('oldval', $(select_elt).val());
178 var data = $('#order_form').serializeArray();
179 data.push({ name: 'action', value: 'Order/unit_changed' });
180 data.push({ name: 'item_id', value: item_id_dom.val() });
181 data.push({ name: 'old_unit', value: oldval });
182 data.push({ name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
184 $.post("controller.pl", data, kivi.eval_json_result);
187 ns.update_sellprice = function(item_id, price_str) {
188 var row = $('#item_' + item_id).parents("tbody").first();
189 var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
190 var html_elt = $(row).find('[name="sellprice_text"]');
191 price_elt.val(price_str);
192 html_elt.html(price_str);
195 ns.load_second_row = function(row) {
196 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
197 var div_elt = $(row).find('[name="second_row"]');
199 if ($(div_elt).data('loaded') == 1) {
202 var data = $('#order_form').serializeArray();
203 data.push({ name: 'action', value: 'Order/load_second_rows' });
204 data.push({ name: 'item_ids[]', value: item_id_dom.val() });
206 $.post("controller.pl", data, kivi.eval_json_result);
209 ns.load_all_second_rows = function() {
210 var rows = $('.row_entry').filter(function(idx, elt) {
211 return $(elt).find('[name="second_row"]').data('loaded') != 1;
214 var item_ids = $.map(rows, function(elt) {
215 var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
216 return { name: 'item_ids[]', value: item_id };
219 if (item_ids.length == 0) {
223 var data = $('#order_form').serializeArray();
224 data.push({ name: 'action', value: 'Order/load_second_rows' });
225 data = data.concat(item_ids);
227 $.post("controller.pl", data, kivi.eval_json_result);
230 ns.hide_second_row = function(row) {
231 $(row).children().not(':first').hide();
232 $(row).data('expanded', 0);
233 var elt = $(row).find('.expand');
234 elt.attr('src', "image/expand.svg");
235 elt.attr('alt', kivi.t8('Show details'));
236 elt.attr('title', kivi.t8('Show details'));
239 ns.show_second_row = function(row) {
240 $(row).children().not(':first').show();
241 $(row).data('expanded', 1);
242 var elt = $(row).find('.expand');
243 elt.attr('src', "image/collapse.svg");
244 elt.attr('alt', kivi.t8('Hide details'));
245 elt.attr('title', kivi.t8('Hide details'));
248 ns.toggle_second_row = function(row) {
249 if ($(row).data('expanded') == 1) {
250 ns.hide_second_row(row);
252 ns.show_second_row(row);
256 ns.init_row_handlers = function() {
257 kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
258 $(elt).change(ns.recalc_amounts_and_taxes);
261 kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
262 $(elt).change(ns.reformat_number);
265 kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
266 $(elt).data('oldval', $(elt).val());
267 $(elt).change(ns.unit_change);
270 kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
271 $(elt).keydown(function(event) {
272 if(event.keyCode == 40 && event.shiftKey === true) {
274 event.preventDefault();
275 var row = $(event.target).parents(".row_entry").first();
276 ns.load_second_row(row);
277 ns.show_second_row(row);
280 if(event.keyCode == 38 && event.shiftKey === true) {
282 event.preventDefault();
283 var row = $(event.target).parents(".row_entry").first();
284 ns.hide_second_row(row);
290 kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
291 $(elt).click(function(event) {
292 event.preventDefault();
293 var row = $(event.target).parents(".row_entry").first();
294 ns.load_second_row(row);
295 ns.toggle_second_row(row);
302 ns.redisplay_line_values = function(is_sales, data) {
303 $('.row_entry').each(function(idx, elt) {
304 $(elt).find('[name="linetotal"]').html(data[idx][0]);
305 if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
306 var mt = data[idx][1];
307 var mp = data[idx][2];
309 if (mt[0] === '-') h += ' class="plus0"';
310 h += '>' + mt + ' ' + mp + '%';
312 $(elt).find('[name="linemargin"]').html(h);
317 ns.renumber_positions = function() {
318 $('.row_entry [name="position"]').each(function(idx, elt) {
323 ns.reorder_items = function(order_by) {
324 var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
325 $('#row_table_id thead a img').remove();
330 src = "image/up.png";
333 src = "image/down.png";
336 $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
338 var data = $('#order_form').serializeArray();
339 data.push({ name: 'action', value: 'Order/reorder_items' });
340 data.push({ name: 'order_by', value: order_by });
341 data.push({ name: 'sort_dir', value: dir });
343 $.post("controller.pl", data, kivi.eval_json_result);
346 ns.redisplay_items = function(data) {
347 var old_rows = $('.row_entry').detach();
349 $(data).each(function(idx, elt) {
350 new_rows.push(old_rows[elt.old_pos - 1]);
352 $(new_rows).appendTo($('#row_table_id'));
353 ns.renumber_positions();
356 ns.add_item = function() {
357 if ($('#add_item_parts_id').val() === '') return;
358 if (!ns.check_cv()) return;
360 $('#row_table_id thead a img').remove();
362 var data = $('#order_form').serializeArray();
363 data.push({ name: 'action', value: 'Order/add_item' });
365 $.post("controller.pl", data, kivi.eval_json_result);
368 ns.show_multi_items_dialog = function() {
369 if (!ns.check_cv()) return;
371 $('#row_table_id thead a img').remove();
374 url: 'controller.pl?action=Order/show_multi_items_dialog',
375 data: { type: $('#type').val(),
376 callback: 'Order/add_multi_items',
377 callback_data_id: 'order_form' },
378 id: 'jq_multi_items_dialog',
380 title: kivi.t8('Add multiple items'),
388 ns.close_multi_items_dialog = function() {
389 $('#jq_multi_items_dialog').dialog('close');
392 ns.delete_order_item_row = function(clicked) {
393 var row = $(clicked).parents("tbody").first();
396 ns.renumber_positions();
397 ns.recalc_amounts_and_taxes();
400 ns.row_table_scroll_down = function() {
401 $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
404 ns.show_longdescription_dialog = function(clicked) {
405 var row = $(clicked).parents("tbody").first();
406 var position = $(row).find('[name="position"]').html();
407 var partnumber = $(row).find('[name="partnumber"]').html();
408 var description_elt = $(row).find('[name="order.orderitems[].description"]');
409 var description = description_elt.val();
410 var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
413 if (!longdescription_elt.length) {
415 data.push({ name: 'action', value: 'Order/get_item_longdescription' });
416 data.push({ name: 'type', value: $('#type').val() });
417 data.push({ name: 'item_id', value: $(row).find('[name="order.orderitems[+].id"]').val() });
418 data.push({ name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() });
420 url: 'controller.pl',
425 success: function(val){
426 longdescription = val;
430 longdescription = longdescription_elt.val();
433 var params = { runningnumber: position,
434 partnumber: partnumber,
435 description: description,
436 default_longdescription: longdescription,
437 set_function: function(val){
438 longdescription_elt.remove();
439 $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
443 kivi.SalesPurchase.edit_longdescription_with_params(params);
446 ns.price_chooser_item_row = function(clicked) {
447 var row = $(clicked).parents("tbody").first();
448 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
450 var data = $('#order_form').serializeArray();
451 data.push({ name: 'action', value: 'Order/price_popup' });
452 data.push({ name: 'item_id', value: item_id_dom.val() });
454 $.post("controller.pl", data, kivi.eval_json_result);
457 ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
458 var row = $('#item_' + item_id).parents("tbody").first();
459 var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
460 var button_elt = $(row).find('[name="price_chooser_button"]');
462 button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
463 source_elt.val(source);
465 var editable_div_elt = $(row).find('[name="editable_price"]');
466 var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
467 if (price_editable == 1 && source === '') {
469 $(editable_div_elt).show();
470 $(not_editable_div_elt).hide();
471 $(editable_div_elt).find(':input').prop("disabled", false);
472 $(not_editable_div_elt).find(':input').prop("disabled", true);
475 $(editable_div_elt).hide();
476 $(not_editable_div_elt).show();
477 $(editable_div_elt).find(':input').prop("disabled", true);
478 $(not_editable_div_elt).find(':input').prop("disabled", false);
482 var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
483 var html_elt = $(row).find('[name="sellprice_text"]');
484 price_elt.val(price_str);
485 html_elt.html(price_str);
486 ns.recalc_amounts_and_taxes();
489 kivi.io.close_dialog();
492 ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
493 var row = $('#item_' + item_id).parents("tbody").first();
494 var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
495 var button_elt = $(row).find('[name="price_chooser_button"]');
497 button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
498 source_elt.val(source);
500 var editable_div_elt = $(row).find('[name="editable_discount"]');
501 var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
502 if (price_editable == 1 && source === '') {
504 $(editable_div_elt).show();
505 $(not_editable_div_elt).hide();
506 $(editable_div_elt).find(':input').prop("disabled", false);
507 $(not_editable_div_elt).find(':input').prop("disabled", true);
510 $(editable_div_elt).hide();
511 $(not_editable_div_elt).show();
512 $(editable_div_elt).find(':input').prop("disabled", true);
513 $(not_editable_div_elt).find(':input').prop("disabled", false);
517 var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
518 var html_elt = $(row).find('[name="discount_text"]');
519 discount_elt.val(discount_str);
520 html_elt.html(discount_str);
521 ns.recalc_amounts_and_taxes();
524 kivi.io.close_dialog();
530 if ($('#type').val() == 'sales_order') {
531 $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
533 $('#order_vendor_id').change(kivi.Order.reload_cv_dependant_selections);
536 if ($('#type').val() == 'sales_order') {
537 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
539 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
541 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
542 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
544 $('.add_item_input').keydown(function(event) {
545 if(event.keyCode == 13) {
546 event.preventDefault();
547 kivi.Order.add_item();
552 kivi.Order.init_row_handlers();
554 $('#row_table_id').on('sortstop', function(event, ui) {
555 $('#row_table_id thead a img').remove();
556 kivi.Order.renumber_positions();
559 $('#expand_all').on('click', function(event) {
560 event.preventDefault();
561 if ($('#expand_all').data('expanded') == 1) {
562 $('#expand_all').data('expanded', 0);
563 $('#expand_all').attr('src', 'image/expand.svg');
564 $('#expand_all').attr('alt', kivi.t8('Show all details'));
565 $('#expand_all').attr('title', kivi.t8('Show all details'));
566 $('.row_entry').each(function(idx, elt) {
567 kivi.Order.hide_second_row(elt);
570 $('#expand_all').data('expanded', 1);
571 $('#expand_all').attr('src', "image/collapse.svg");
572 $('#expand_all').attr('alt', kivi.t8('Hide all details'));
573 $('#expand_all').attr('title', kivi.t8('Hide all details'));
574 kivi.Order.load_all_second_rows();
575 $('.row_entry').each(function(idx, elt) {
576 kivi.Order.show_second_row(elt);