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.delete_order = function() {
59 var data = $('#order_form').serializeArray();
60 data.push({ name: 'action', value: 'Order/delete' });
62 $.post("controller.pl", data, kivi.eval_json_result);
65 ns.show_print_options = function() {
66 if (!ns.check_cv()) return;
71 title: kivi.t8('Print options'),
78 ns.print = function() {
79 $('#print_options').dialog('close');
81 var data = $('#order_form').serializeArray();
82 data = data.concat($('#print_options_form').serializeArray());
83 data.push({ name: 'action', value: 'Order/print' });
85 $.post("controller.pl", data, kivi.eval_json_result);
88 ns.download_pdf = function(pdf_filename, key) {
90 data.push({ name: 'action', value: 'Order/download_pdf' });
91 data.push({ name: 'type', value: $('#type').val() });
92 data.push({ name: 'pdf_filename', value: pdf_filename });
93 data.push({ name: 'key', value: key });
94 $.download("controller.pl", data);
97 ns.email = function() {
98 if (!ns.check_cv()) return;
99 var data = $('#order_form').serializeArray();
100 data.push({ name: 'action', value: 'Order/show_email_dialog' });
102 $.post("controller.pl", data, kivi.eval_json_result);
107 ns.show_email_dialog = function(html) {
108 var id = 'jqueryui_popup_dialog';
109 var dialog_params = {
114 close: function(event, ui) {
115 email_dialog.remove();
119 $('#' + id).remove();
121 email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
122 email_dialog.html(html);
123 email_dialog.dialog(dialog_params);
125 $('.cancel').click(ns.close_email_dialog);
130 ns.send_email = function() {
131 var data = $('#order_form').serializeArray();
132 data = data.concat($('#email_form').serializeArray());
133 data.push({ name: 'action', value: 'Order/send_email' });
134 $.post("controller.pl", data, kivi.eval_json_result);
137 ns.close_email_dialog = function() {
138 email_dialog.dialog("close");
141 ns.reload_cv_dependant_selections = function() {
142 var data = $('#order_form').serializeArray();
143 data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
145 $.post("controller.pl", data, kivi.eval_json_result);
148 ns.reformat_number = function(event) {
149 $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
152 ns.recalc_amounts_and_taxes = function() {
153 var data = $('#order_form').serializeArray();
154 data.push({ name: 'action', value: 'Order/recalc_amounts_and_taxes' });
156 $.post("controller.pl", data, kivi.eval_json_result);
159 ns.unit_change = function(event) {
160 var row = $(event.target).parents("tbody").first();
161 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
162 var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
163 var select_elt = $(row).find('[name="order.orderitems[].unit"]');
165 var oldval = $(select_elt).data('oldval');
166 $(select_elt).data('oldval', $(select_elt).val());
168 var data = $('#order_form').serializeArray();
169 data.push({ name: 'action', value: 'Order/unit_changed' });
170 data.push({ name: 'item_id', value: item_id_dom.val() });
171 data.push({ name: 'old_unit', value: oldval });
172 data.push({ name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
174 $.post("controller.pl", data, kivi.eval_json_result);
177 ns.update_sellprice = function(item_id, price_str) {
178 var row = $('#item_' + item_id).parents("tbody").first();
179 var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
180 var html_elt = $(row).find('[name="sellprice_text"]');
181 price_elt.val(price_str);
182 html_elt.html(price_str);
185 ns.load_second_row = function(row) {
186 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
187 var div_elt = $(row).find('[name="second_row"]');
189 if ($(div_elt).data('loaded') == 1) {
192 var data = $('#order_form').serializeArray();
193 data.push({ name: 'action', value: 'Order/load_second_rows' });
194 data.push({ name: 'item_ids[]', value: item_id_dom.val() });
196 $.post("controller.pl", data, kivi.eval_json_result);
199 ns.load_all_second_rows = function() {
200 var rows = $('.row_entry').filter(function(idx, elt) {
201 return $(elt).find('[name="second_row"]').data('loaded') != 1;
204 var item_ids = $.map(rows, function(elt) {
205 var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
206 return { name: 'item_ids[]', value: item_id };
209 if (item_ids.length == 0) {
213 var data = $('#order_form').serializeArray();
214 data.push({ name: 'action', value: 'Order/load_second_rows' });
215 data = data.concat(item_ids);
217 $.post("controller.pl", data, kivi.eval_json_result);
220 ns.hide_second_row = function(row) {
221 $(row).children().not(':first').hide();
222 $(row).data('expanded', 0);
223 var elt = $(row).find('.expand');
224 elt.attr('src', "image/expand.svg");
225 elt.attr('alt', kivi.t8('Show details'));
226 elt.attr('title', kivi.t8('Show details'));
229 ns.show_second_row = function(row) {
230 $(row).children().not(':first').show();
231 $(row).data('expanded', 1);
232 var elt = $(row).find('.expand');
233 elt.attr('src', "image/collapse.svg");
234 elt.attr('alt', kivi.t8('Hide details'));
235 elt.attr('title', kivi.t8('Hide details'));
238 ns.toggle_second_row = function(row) {
239 if ($(row).data('expanded') == 1) {
240 ns.hide_second_row(row);
242 ns.show_second_row(row);
246 ns.init_row_handlers = function() {
247 kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
248 $(elt).change(ns.recalc_amounts_and_taxes);
251 kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
252 $(elt).change(ns.reformat_number);
255 kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
256 $(elt).data('oldval', $(elt).val());
257 $(elt).change(ns.unit_change);
260 kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
261 $(elt).keydown(function(event) {
262 if(event.keyCode == 40 && event.shiftKey === true) {
264 event.preventDefault();
265 var row = $(event.target).parents(".row_entry").first();
266 ns.load_second_row(row);
267 ns.show_second_row(row);
270 if(event.keyCode == 38 && event.shiftKey === true) {
272 event.preventDefault();
273 var row = $(event.target).parents(".row_entry").first();
274 ns.hide_second_row(row);
280 kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
281 $(elt).click(function(event) {
282 event.preventDefault();
283 var row = $(event.target).parents(".row_entry").first();
284 ns.load_second_row(row);
285 ns.toggle_second_row(row);
292 ns.redisplay_line_values = function(is_sales, data) {
293 $('.row_entry').each(function(idx, elt) {
294 $(elt).find('[name="linetotal"]').html(data[idx][0]);
295 if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
296 var mt = data[idx][1];
297 var mp = data[idx][2];
299 if (mt[0] === '-') h += ' class="plus0"';
300 h += '>' + mt + ' ' + mp + '%';
302 $(elt).find('[name="linemargin"]').html(h);
307 ns.renumber_positions = function() {
308 $('.row_entry [name="position"]').each(function(idx, elt) {
313 ns.reorder_items = function(order_by) {
314 var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
315 $('#row_table_id thead a img').remove();
320 src = "image/up.png";
323 src = "image/down.png";
326 $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
328 var data = $('#order_form').serializeArray();
329 data.push({ name: 'action', value: 'Order/reorder_items' });
330 data.push({ name: 'order_by', value: order_by });
331 data.push({ name: 'sort_dir', value: dir });
333 $.post("controller.pl", data, kivi.eval_json_result);
336 ns.redisplay_items = function(data) {
337 var old_rows = $('.row_entry').detach();
339 $(data).each(function(idx, elt) {
340 new_rows.push(old_rows[elt.old_pos - 1]);
342 $(new_rows).appendTo($('#row_table_id'));
343 ns.renumber_positions();
346 ns.add_item = function() {
347 if ($('#add_item_parts_id').val() === '') return;
348 if (!ns.check_cv()) return;
350 $('#row_table_id thead a img').remove();
352 var data = $('#order_form').serializeArray();
353 data.push({ name: 'action', value: 'Order/add_item' });
355 $.post("controller.pl", data, kivi.eval_json_result);
358 ns.show_multi_items_dialog = function() {
359 if (!ns.check_cv()) return;
361 $('#row_table_id thead a img').remove();
364 url: 'controller.pl?action=Order/show_multi_items_dialog',
365 data: { type: $('#type').val(),
366 callback: 'Order/add_multi_items',
367 callback_data_id: 'order_form' },
368 id: 'jq_multi_items_dialog',
370 title: kivi.t8('Add multiple items'),
378 ns.close_multi_items_dialog = function() {
379 $('#jq_multi_items_dialog').dialog('close');
382 ns.delete_order_item_row = function(clicked) {
383 var row = $(clicked).parents("tbody").first();
386 ns.renumber_positions();
387 ns.recalc_amounts_and_taxes();
390 ns.row_table_scroll_down = function() {
391 $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
394 ns.show_longdescription_dialog = function(clicked) {
395 var row = $(clicked).parents("tbody").first();
396 var position = $(row).find('[name="position"]').html();
397 var partnumber = $(row).find('[name="partnumber"]').html();
398 var description_elt = $(row).find('[name="order.orderitems[].description"]');
399 var description = description_elt.val();
400 var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
403 if (!longdescription_elt.length) {
405 data.push({ name: 'action', value: 'Order/get_item_longdescription' });
406 data.push({ name: 'type', value: $('#type').val() });
407 data.push({ name: 'item_id', value: $(row).find('[name="order.orderitems[+].id"]').val() });
408 data.push({ name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() });
410 url: 'controller.pl',
415 success: function(val){
416 longdescription = val;
420 longdescription = longdescription_elt.val();
423 var params = { runningnumber: position,
424 partnumber: partnumber,
425 description: description,
426 default_longdescription: longdescription,
427 set_function: function(val){
428 longdescription_elt.remove();
429 $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
433 kivi.SalesPurchase.edit_longdescription_with_params(params);
436 ns.price_chooser_item_row = function(clicked) {
437 var row = $(clicked).parents("tbody").first();
438 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
440 var data = $('#order_form').serializeArray();
441 data.push({ name: 'action', value: 'Order/price_popup' });
442 data.push({ name: 'item_id', value: item_id_dom.val() });
444 $.post("controller.pl", data, kivi.eval_json_result);
447 ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
448 var row = $('#item_' + item_id).parents("tbody").first();
449 var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
450 var button_elt = $(row).find('[name="price_chooser_button"]');
452 button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
453 source_elt.val(source);
455 var editable_div_elt = $(row).find('[name="editable_price"]');
456 var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
457 if (price_editable == 1 && source === '') {
459 $(editable_div_elt).show();
460 $(not_editable_div_elt).hide();
461 $(editable_div_elt).find(':input').prop("disabled", false);
462 $(not_editable_div_elt).find(':input').prop("disabled", true);
465 $(editable_div_elt).hide();
466 $(not_editable_div_elt).show();
467 $(editable_div_elt).find(':input').prop("disabled", true);
468 $(not_editable_div_elt).find(':input').prop("disabled", false);
472 var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
473 var html_elt = $(row).find('[name="sellprice_text"]');
474 price_elt.val(price_str);
475 html_elt.html(price_str);
476 ns.recalc_amounts_and_taxes();
479 kivi.io.close_dialog();
482 ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
483 var row = $('#item_' + item_id).parents("tbody").first();
484 var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
485 var button_elt = $(row).find('[name="price_chooser_button"]');
487 button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
488 source_elt.val(source);
490 var editable_div_elt = $(row).find('[name="editable_discount"]');
491 var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
492 if (price_editable == 1 && source === '') {
494 $(editable_div_elt).show();
495 $(not_editable_div_elt).hide();
496 $(editable_div_elt).find(':input').prop("disabled", false);
497 $(not_editable_div_elt).find(':input').prop("disabled", true);
500 $(editable_div_elt).hide();
501 $(not_editable_div_elt).show();
502 $(editable_div_elt).find(':input').prop("disabled", true);
503 $(not_editable_div_elt).find(':input').prop("disabled", false);
507 var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
508 var html_elt = $(row).find('[name="discount_text"]');
509 discount_elt.val(discount_str);
510 html_elt.html(discount_str);
511 ns.recalc_amounts_and_taxes();
514 kivi.io.close_dialog();
520 if ($('#type').val() == 'sales_order') {
521 $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
523 $('#order_vendor_id').change(kivi.Order.reload_cv_dependant_selections);
526 if ($('#type').val() == 'sales_order') {
527 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
529 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
531 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
532 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
534 $('.add_item_input').keydown(function(event) {
535 if(event.keyCode == 13) {
536 event.preventDefault();
537 kivi.Order.add_item();
542 kivi.Order.init_row_handlers();
544 $('#row_table_id').on('sortstop', function(event, ui) {
545 $('#row_table_id thead a img').remove();
546 kivi.Order.renumber_positions();
549 $('#expand_all').on('click', function(event) {
550 event.preventDefault();
551 if ($('#expand_all').data('expanded') == 1) {
552 $('#expand_all').data('expanded', 0);
553 $('#expand_all').attr('src', 'image/expand.svg');
554 $('#expand_all').attr('alt', kivi.t8('Show all details'));
555 $('#expand_all').attr('title', kivi.t8('Show all details'));
556 $('.row_entry').each(function(idx, elt) {
557 kivi.Order.hide_second_row(elt);
560 $('#expand_all').data('expanded', 1);
561 $('#expand_all').attr('src', "image/collapse.svg");
562 $('#expand_all').attr('alt', kivi.t8('Hide all details'));
563 $('#expand_all').attr('title', kivi.t8('Hide all details'));
564 kivi.Order.load_all_second_rows();
565 $('.row_entry').each(function(idx, elt) {
566 kivi.Order.show_second_row(elt);