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.init_row_handlers = function() {
186 kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
187 $(elt).change(ns.recalc_amounts_and_taxes);
190 kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
191 $(elt).change(ns.reformat_number);
194 kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
195 $(elt).data('oldval', $(elt).val());
196 $(elt).change(ns.unit_change);
199 kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
200 $(elt).keydown(function(event) {
202 if(event.keyCode == 40 && event.shiftKey === true) {
204 event.preventDefault();
205 row = $(event.target).parents(".row_entry").first();
206 $(row).children().not(':first').show();
209 if(event.keyCode == 38 && event.shiftKey === true) {
211 event.preventDefault();
212 row = $(event.target).parents(".row_entry").first();
213 $(row).children().not(':first').hide();
217 $(elt).dblclick(function(event) {
218 event.preventDefault();
219 var row = $(event.target).parents(".row_entry").first();
220 $(row).children().not(':first').toggle();
226 ns.redisplay_linetotals = function(data) {
227 $('.row_entry [name="linetotal"]').each(function(idx, elt) {
228 $(elt).html(data[idx]);
232 ns.renumber_positions = function() {
233 $('.row_entry [name="position"]').each(function(idx, elt) {
238 ns.reorder_items = function(order_by) {
239 var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
240 $('#row_table_id thead a img').remove();
245 src = "image/up.png";
248 src = "image/down.png";
251 $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
253 var data = $('#order_form').serializeArray();
254 data.push({ name: 'action', value: 'Order/reorder_items' });
255 data.push({ name: 'order_by', value: order_by });
256 data.push({ name: 'sort_dir', value: dir });
258 $.post("controller.pl", data, kivi.eval_json_result);
261 ns.redisplay_items = function(data) {
262 var old_rows = $('.row_entry').detach();
264 $(data).each(function(idx, elt) {
265 new_rows.push(old_rows[elt.old_pos - 1]);
267 $(new_rows).appendTo($('#row_table_id'));
268 ns.renumber_positions();
271 ns.add_item = function() {
272 if ($('#add_item_parts_id').val() === '') return;
273 if (!ns.check_cv()) return;
275 $('#row_table_id thead a img').remove();
277 var data = $('#order_form').serializeArray();
278 data.push({ name: 'action', value: 'Order/add_item' });
280 $.post("controller.pl", data, kivi.eval_json_result);
283 ns.show_multi_items_dialog = function() {
284 if (!ns.check_cv()) return;
286 $('#row_table_id thead a img').remove();
289 url: 'controller.pl?action=Order/show_multi_items_dialog',
290 data: { type: $('#type').val(),
291 callback: 'Order/add_multi_items',
292 callback_data_id: 'order_form' },
293 id: 'jq_multi_items_dialog',
295 title: kivi.t8('Add multiple items'),
303 ns.close_multi_items_dialog = function() {
304 $('#jq_multi_items_dialog').dialog('close');
307 ns.delete_order_item_row = function(clicked) {
308 var row = $(clicked).parents("tbody").first();
311 ns.renumber_positions();
312 ns.recalc_amounts_and_taxes();
315 ns.row_table_scroll_down = function() {
316 $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
319 ns.show_longdescription_dialog = function(clicked) {
320 var row = $(clicked).parents("tbody").first();
321 var position = $(row).find('[name="position"]').html();
322 var partnumber = $(row).find('[name="partnumber"]').html();
323 var description_elt = $(row).find('[name="order.orderitems[].description"]');
324 var description = description_elt.val();
325 var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
328 if (!longdescription_elt.length) {
330 data.push({ name: 'action', value: 'Order/get_item_longdescription' });
331 data.push({ name: 'type', value: $('#type').val() });
332 data.push({ name: 'item_id', value: $(row).find('[name="order.orderitems[+].id"]').val() });
333 data.push({ name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() });
335 url: 'controller.pl',
340 success: function(val){
341 longdescription = val;
345 longdescription = longdescription_elt.val();
348 var params = { runningnumber: position,
349 partnumber: partnumber,
350 description: description,
351 default_longdescription: longdescription,
352 set_function: function(val){
353 longdescription_elt.remove();
354 $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
358 kivi.SalesPurchase.edit_longdescription_with_params(params);
361 ns.price_chooser_item_row = function(clicked) {
362 var row = $(clicked).parents("tbody").first();
363 var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
365 var data = $('#order_form').serializeArray();
366 data.push({ name: 'action', value: 'Order/price_popup' });
367 data.push({ name: 'item_id', value: item_id_dom.val() });
369 $.post("controller.pl", data, kivi.eval_json_result);
372 ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
373 var row = $('#item_' + item_id).parents("tbody").first();
374 var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
375 var button_elt = $(row).find('[name="price_chooser_button"]');
377 button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
378 source_elt.val(source);
380 var editable_div_elt = $(row).find('[name="editable_price"]');
381 var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
382 if (price_editable == 1 && source === '') {
384 $(editable_div_elt).show();
385 $(not_editable_div_elt).hide();
386 $(editable_div_elt).find(':input').prop("disabled", false);
387 $(not_editable_div_elt).find(':input').prop("disabled", true);
390 $(editable_div_elt).hide();
391 $(not_editable_div_elt).show();
392 $(editable_div_elt).find(':input').prop("disabled", true);
393 $(not_editable_div_elt).find(':input').prop("disabled", false);
397 var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
398 var html_elt = $(row).find('[name="sellprice_text"]');
399 price_elt.val(price_str);
400 html_elt.html(price_str);
401 ns.recalc_amounts_and_taxes();
404 kivi.io.close_dialog();
407 ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
408 var row = $('#item_' + item_id).parents("tbody").first();
409 var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
410 var button_elt = $(row).find('[name="price_chooser_button"]');
412 button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
413 source_elt.val(source);
415 var editable_div_elt = $(row).find('[name="editable_discount"]');
416 var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
417 if (price_editable == 1 && source === '') {
419 $(editable_div_elt).show();
420 $(not_editable_div_elt).hide();
421 $(editable_div_elt).find(':input').prop("disabled", false);
422 $(not_editable_div_elt).find(':input').prop("disabled", true);
425 $(editable_div_elt).hide();
426 $(not_editable_div_elt).show();
427 $(editable_div_elt).find(':input').prop("disabled", true);
428 $(not_editable_div_elt).find(':input').prop("disabled", false);
432 var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
433 var html_elt = $(row).find('[name="discount_text"]');
434 discount_elt.val(discount_str);
435 html_elt.html(discount_str);
436 ns.recalc_amounts_and_taxes();
439 kivi.io.close_dialog();
445 if ($('#type').val() == 'sales_order') {
446 $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
448 $('#order_vendor_id').change(kivi.Order.reload_cv_dependant_selections);
451 if ($('#type').val() == 'sales_order') {
452 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
454 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
456 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
457 $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
459 $('.add_item_input').keydown(function(event) {
460 if(event.keyCode == 13) {
461 event.preventDefault();
462 kivi.Order.add_item();
467 kivi.Order.init_row_handlers();
469 $('#row_table_id').on('sortstop', function(event, ui) {
470 $('#row_table_id thead a img').remove();
471 kivi.Order.renumber_positions();