|
namespace('kivi.Order', function(ns) {
|
|
ns.check_cv = function() {
|
|
if ($('#type').val() == 'sales_order_intake' || $('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation') {
|
|
if ($('#order_customer_id').val() === '') {
|
|
alert(kivi.t8('Please select a customer.'));
|
|
return false;
|
|
}
|
|
} else {
|
|
if ($('#order_vendor_id').val() === '') {
|
|
alert(kivi.t8('Please select a vendor.'));
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
};
|
|
|
|
ns.check_duplicate_parts = function(question) {
|
|
var id_arr = $('[name="order.orderitems[].parts_id"]').map(function() { return this.value; }).get();
|
|
|
|
var i, obj = {}, pos = [];
|
|
|
|
for (i = 0; i < id_arr.length; i++) {
|
|
var id = id_arr[i];
|
|
if (obj.hasOwnProperty(id)) {
|
|
pos.push(i + 1);
|
|
}
|
|
obj[id] = 0;
|
|
}
|
|
|
|
if (pos.length > 0) {
|
|
question = question || kivi.t8("Do you really want to continue?");
|
|
return confirm(kivi.t8("There are duplicate parts at positions") + "\n"
|
|
+ pos.join(', ') + "\n"
|
|
+ question);
|
|
}
|
|
return true;
|
|
};
|
|
|
|
ns.check_valid_reqdate = function() {
|
|
if ($('#order_reqdate_as_date').val() === '') {
|
|
alert(kivi.t8('Please select a delivery date.'));
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
};
|
|
|
|
ns.save = function(params) {
|
|
if (!ns.check_cv()) return;
|
|
|
|
const action = params.action;
|
|
const warn_on_duplicates = params.warn_on_duplicates;
|
|
const warn_on_reqdate = params.warn_on_reqdate;
|
|
const form_params = params.form_params;
|
|
|
|
if (warn_on_duplicates && !ns.check_duplicate_parts()) return;
|
|
if (warn_on_reqdate && !ns.check_valid_reqdate()) return;
|
|
|
|
var data = $('#order_form').serializeArray();
|
|
data.push({ name: 'action', value: 'Order/' + action });
|
|
|
|
if (form_params) {
|
|
if (Array.isArray(form_params)) {
|
|
form_params.forEach(function(item) {
|
|
data.push(item);
|
|
});
|
|
} else {
|
|
data.push(form_params);
|
|
}
|
|
}
|
|
|
|
if (params.data)
|
|
data = $.merge(data, params.data);
|
|
|
|
$.post("controller.pl", data, kivi.eval_json_result);
|
|
};
|
|
|
|
ns.delete_order = function() {
|
|
var data = $('#order_form').serializeArray();
|
|
data.push({ name: 'action', value: 'Order/delete' });
|
|
|
|
$.post("controller.pl", data, kivi.eval_json_result);
|
|
};
|
|
|
|
ns.show_print_options = function(params) {
|
|
if (!ns.check_cv()) return;
|
|
|
|
const warn_on_duplicates = params.warn_on_duplicates;
|
|
const warn_on_reqdate = params.warn_on_reqdate;
|
|
|
|
if (warn_on_duplicates && !ns.check_duplicate_parts(kivi.t8("Do you really want to print?"))) return;
|
|
if (warn_on_reqdate && !ns.check_valid_reqdate()) return;
|
|
|
|
kivi.popup_dialog({
|
|
id: 'print_options',
|
|
dialog: {
|
|
title: kivi.t8('Print options'),
|
|
width: 800,
|
|
height: 300
|
|
}
|
|
});
|
|
};
|
|
|
|
ns.print = function() {
|
|
$('#print_options').dialog('close');
|
|
|
|
var data = $('#order_form').serializeArray();
|
|
data = data.concat($('#print_options_form').serializeArray());
|
|
data.push({ name: 'action', value: 'Order/print' });
|
|
|
|
$.post("controller.pl", data, kivi.eval_json_result);
|
|
};
|
|
|
|
var email_dialog;
|
|
|
|
ns.setup_send_email_dialog = function() {
|
|
kivi.SalesPurchase.show_all_print_options_elements();
|
|
kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
|
|
|
|
$('#print_options_form table').first().remove().appendTo('#email_form_print_options');
|
|
|
|
$('select#format').change(kivi.Order.adjust_email_attachment_name_for_template_format);
|
|
kivi.Order.adjust_email_attachment_name_for_template_format();
|
|
|
|
var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
|
|
$('#email_form_' + to_focus).focus();
|
|
};
|
|
|
|
ns.finish_send_email_dialog = function() {
|
|
kivi.SalesPurchase.show_all_print_options_elements();
|
|
|
|
$('#email_form_print_options table').first().remove().prependTo('#print_options_form');
|
|
return true;
|
|
};
|
|
|
|
ns.show_email_dialog = function(html) {
|
|
var id = 'send_email_dialog';
|
|
var dialog_params = {
|
|
id: id,
|
|
width: 800,
|
|
height: 600,
|
|
title: kivi.t8('Send email'),
|
|
modal: true,
|
|
beforeClose: kivi.Order.finish_send_email_dialog,
|
|
close: function(event, ui) {
|
|
email_dialog.remove();
|
|
}
|
|
};
|
|
|
|
$('#' + id).remove();
|
|
|
|
email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
|
|
email_dialog.html(html);
|
|
email_dialog.dialog(dialog_params);
|
|
|
|
kivi.Order.setup_send_email_dialog();
|
|
|
|
$('.cancel').click(ns.close_email_dialog);
|
|
|
|
return true;
|
|
};
|
|
|
|
ns.send_email = function() {
|
|
// push button only once -> slow response from mail server
|
|
ns.email_dialog_disable_send();
|
|
|
|
var data = $('#order_form').serializeArray();
|
|
data = data.concat($('[name^="email_form."]').serializeArray());
|
|
data = data.concat($('[name^="print_options."]').serializeArray());
|
|
data.push({ name: 'action', value: 'Order/send_email' });
|
|
$.post("controller.pl", data, kivi.eval_json_result);
|
|
};
|
|
|
|
ns.email_dialog_disable_send = function() {
|
|
// disable mail send event to prevent
|
|
// impatient users to send multiple times
|
|
$('#send_email').prop('disabled', true);
|
|
};
|
|
|
|
ns.close_email_dialog = function() {
|
|
email_dialog.dialog("close");
|
|
};
|
|
|
|
ns.adjust_email_attachment_name_for_template_format = function() {
|
|
var $filename_elt = $('#email_form_attachment_filename');
|
|
var $format_elt = $('select#format');
|
|
|
|
if (!$filename_elt || !$format_elt)
|
|
return;
|
|
|
|
var format = $format_elt.val().toLowerCase();
|
|
var new_ext = format == 'html' ? 'html' : format == 'opendocument' ? 'odt' : 'pdf';
|
|
var filename = $filename_elt.val();
|
|
|
|
$filename_elt.val(filename.replace(/[^.]+$/, new_ext));
|
|
};
|
|
|
|
ns.set_number_in_title = function(elt) {
|
|
$('#nr_in_title').html($(elt).val());
|
|
};
|
|
|
|
ns.reload_cv_dependent_selections = function() {
|
|
$('#order_shipto_id').val('');
|
|
var data = $('#order_form').serializeArray();
|
|
data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
|
|
|
|
$.post("controller.pl", data, kivi.eval_json_result);
|
|
};
|
|
|
|
ns.reformat_number = function(event) {
|
|
$(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
|
|
};
|
|
|
|
ns.reformat_number_as_null_number = function(event) {
|
|
if ($(event.target).val() === '') {
|
|
return;
|
|
}
|
|
ns.reformat_number(event);
|
|
};
|
|
|
|
ns.update_exchangerate = function(event) {
|
|
if (!ns.check_cv()) {
|
|
$('#order_currency_id').val($('#old_currency_id').val());
|
|
return;
|
|
}
|
|
|
|
var rate_input = $('#order_exchangerate_as_null_number');
|
|
// unset exchangerate if currency changed
|
|
if ($('#order_currency_id').val() !== $('#old_currency_id').val()) {
|
|
rate_input.val('');
|
|
}
|
|
|
|
// only set exchangerate if unset
|
|
if (rate_input.val() !== '') {
|
|
return;
|
|
}
|
|
|
|
var data = $('#order_form').serializeArray();
|
|
data.push({ name: 'action', value: 'Order/update_exchangerate' });
|
|
|
|
$.ajax({
|
|
url: 'controller.pl',
|
|
data: data,
|
|
method: 'POST',
|
|
dataType: 'json',
|
|
success: function(data){
|
|
if (!data.is_standard) {
|
|
$('#currency_name').text(data.currency_name);
|
|
if (data.exchangerate) {
|
|
rate_input.val(data.exchangerate);
|
|
} else {
|
|
rate_input.val('');
|
|
}
|
|
$('#exchangerate_settings').show();
|
|
} else {
|
|
rate_input.val('');
|
|
$('#exchangerate_settings').hide();
|
|
}
|
|
if ($('#order_currency_id').val() != $('#old_currency_id').val() ||
|
|
!data.is_standard && data.exchangerate != $('#old_exchangerate').val()) {
|
|
kivi.display_flash('warning', kivi.t8('You have changed the currency or exchange rate. Please check prices.'));
|
|
}
|
|
$('#old_currency_id').val($('#order_currency_id').val());
|
|
$('#old_exchangerate').val(data.exchangerate);
|
|
}
|
|
});
|
|
};
|
|
|
|
ns.exchangerate_changed = function(event) {
|
|
if (kivi.parse_amount($('#order_exchangerate_as_null_number').val()) != kivi.parse_amount($('#old_exchangerate').val())) {
|
|
kivi.display_flash('warning', kivi.t8('You have changed the currency or exchange rate. Please check prices.'));
|
|
$('#old_exchangerate').val($('#order_exchangerate_as_null_number').val());
|
|
}
|
|
};
|
|
|
|
ns.recalc_amounts_and_taxes = function() {
|
|
if (!kivi.validate_form('#order_form')) return;
|
|
|
|
var data = $('#order_form').serializeArray();
|
|
data.push({ name: 'action', value: 'Order/recalc_amounts_and_taxes' });
|
|
|
|
$.post("controller.pl", data, kivi.eval_json_result);
|
|
};
|
|
|
|
ns.unit_change = function(event) {
|
|
var row = $(event.target).parents("tbody").first();
|
|
var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
|
|
var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
|
|
var select_elt = $(row).find('[name="order.orderitems[].unit"]');
|
|
|
|
var oldval = $(select_elt).data('oldval');
|
|
$(select_elt).data('oldval', $(select_elt).val());
|
|
|
|
var data = $('#order_form').serializeArray();
|
|
data.push({ name: 'action', value: 'Order/unit_changed' },
|
|
{ name: 'item_id', value: item_id_dom.val() },
|
|
{ name: 'old_unit', value: oldval },
|
|
{ name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
|
|
|
|
$.post("controller.pl", data, kivi.eval_json_result);
|
|
};
|
|
|
|
ns.update_sellprice = function(item_id, price_str) {
|
|
var row = $('#item_' + item_id).parents("tbody").first();
|
|
var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
|
|
var html_elt = $(row).find('[name="sellprice_text"]');
|
|
price_elt.val(price_str);
|
|
html_elt.html(price_str);
|
|
};
|
|
|
|
ns.load_second_row = function(row) {
|
|
var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
|
|
var div_elt = $(row).find('[name="second_row"]');
|
|
|
|
if ($(div#L739" data-txt="739">
type : $('#type').val(),
|
|
|
vc : vc,
|
|
vc_id : vc_id
|
|
},
|
|
id: 'jq_customer_vendor_details_dialog',
|
|
dialog: {
|
|
title: title,
|
|
width: 800,
|
|
height: 650
|
|
}
|
|
});
|
|
return true;
|
|
};
|
|
|
|
ns.update_row_from_master_data = function(clicked) {
|
|
|