kivitendo/js/kivi.CustomerVendor.js @ d04d57fd
bc998bc1 | Thomas Heck | namespace('kivi.CustomerVendor', function(ns) {
|
||
4cb3a908 | Thomas Heck | |||
bc998bc1 | Thomas Heck | this.selectShipto = function(params) {
|
||
4cb3a908 | Thomas Heck | var shiptoId = $('#shipto_shipto_id').val();
|
||
6ef4190e | Moritz Bunkus | var url = 'controller.pl?action=CustomerVendor/ajaj_get_shipto&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ shiptoId;
|
||
4cb3a908 | Thomas Heck | |||
6ef4190e | Moritz Bunkus | $.getJSON(url, function(data) {
|
||
var shipto = data.shipto;
|
||||
for(var key in shipto)
|
||||
$('#shipto_'+ key).val(shipto[key])
|
||||
4cb3a908 | Thomas Heck | |||
6ef4190e | Moritz Bunkus | kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.shipto_cvars, 'shipto_cvars_');
|
||
4cb3a908 | Thomas Heck | |||
6ef4190e | Moritz Bunkus | if ( shiptoId )
|
||
4cb3a908 | Thomas Heck | $('#action_delete_shipto').show();
|
||
6ef4190e | Moritz Bunkus | else
|
||
$('#action_delete_shipto').hide();
|
||||
bc998bc1 | Thomas Heck | |||
6ef4190e | Moritz Bunkus | if ( params.onFormSet )
|
||
bc998bc1 | Thomas Heck | params.onFormSet();
|
||
6ef4190e | Moritz Bunkus | });
|
||
4cb3a908 | Thomas Heck | };
|
||
this.selectDelivery = function(fromDate, toDate) {
|
||||
var deliveryId = $('#delivery_id').val();
|
||||
if( !deliveryId )
|
||||
$("#delivery").empty();
|
||||
else {
|
||||
var url = 'controller.pl?action=CustomerVendor/get_delivery&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ $('#delivery_id').val();
|
||||
if( fromDate && toDate )
|
||||
url += '&delivery_from='+ fromDate +'&delivery_to='+ toDate;
|
||||
$('#delivery').load(url);
|
||||
}
|
||||
};
|
||||
6ef4190e | Moritz Bunkus | this.setCustomVariablesFromAJAJ = function(cvars, prefix) {
|
||
65690235 | Moritz Bunkus | for (var key in cvars) {
|
||
var cvar = cvars[key];
|
||||
6ef4190e | Moritz Bunkus | var $ctrl = $('#' + prefix + key);
|
||
65690235 | Moritz Bunkus | |||
if (cvar.type == 'bool')
|
||||
$ctrl.prop('checked', cvar.value == 1 ? 'checked' : '');
|
||||
else if ((cvar.type == 'customer') || (cvar.type == 'vendor'))
|
||||
7e7aae8d | Sven Schöling | kivi.CustomerVendor.Picker($ctrl).set_item({ id: cvar.id, name: cvar.value });
|
||
65690235 | Moritz Bunkus | |||
else if (cvar.type == 'part')
|
||||
5aec18fe | Sven Schöling | kivi.Part.Picker($ctrl).set_item({ id: cvar.id, name: cvar.value });
|
||
65690235 | Moritz Bunkus | |||
else
|
||||
$ctrl.val(cvar.value);
|
||||
}
|
||||
};
|
||||
bc998bc1 | Thomas Heck | this.selectContact = function(params) {
|
||
4cb3a908 | Thomas Heck | var contactId = $('#contact_cp_id').val();
|
||
c75c19ad | Bernd Bleßmann | var url = 'controller.pl?action=CustomerVendor/ajaj_get_contact&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&contact_id='+ contactId;
|
||
4cb3a908 | Thomas Heck | |||
a56b873b | Thomas Heck | $.getJSON(url, function(data) {
|
||
var contact = data.contact;
|
||||
for(var key in contact)
|
||||
180b77bc | Moritz Bunkus | $('#contact_'+ key).val(contact[key])
|
||
4cb3a908 | Thomas Heck | |||
6ef4190e | Moritz Bunkus | kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.contact_cvars, 'contact_cvars_');
|
||
4cb3a908 | Thomas Heck | |||
1dd6d84a | Bernd Bleßmann | if ( contactId ) {
|
||
4cb3a908 | Thomas Heck | $('#action_delete_contact').show();
|
||
1dd6d84a | Bernd Bleßmann | $('#contact_cp_title_select').val(contact['cp_title']);
|
||
cc6d7fec | Bernd Bleßmann | $('#contact_cp_abteilung_select').val(contact['cp_abteilung']);
|
||
1dd6d84a | Bernd Bleßmann | } else {
|
||
a56b873b | Thomas Heck | $('#action_delete_contact').hide();
|
||
1dd6d84a | Bernd Bleßmann | $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
|
||
}
|
||||
564b5410 | Jan Büren | if (data.contact.disable_cp_main === 1)
|
||
$("#contact_cp_main").prop("disabled", true);
|
||||
else
|
||||
$("#contact_cp_main").prop("disabled", false);
|
||||
a56b873b | Thomas Heck | if ( params.onFormSet )
|
||
bc998bc1 | Thomas Heck | params.onFormSet();
|
||
a56b873b | Thomas Heck | });
|
||
4cb3a908 | Thomas Heck | |||
};
|
||||
bc998bc1 | Thomas Heck | var mapSearchStmts = [
|
||
'#street',
|
||||
', ',
|
||||
'#zipcode',
|
||||
' ',
|
||||
'#city',
|
||||
', ',
|
||||
'#country'
|
||||
];
|
||||
df601bd3 | Moritz Bunkus | this.MapWidget = function(prefix, source_address)
|
||
ef6fcba9 | Thomas Heck | {
|
||
var $mapSearchElements = [];
|
||||
var $widgetWrapper;
|
||||
bc998bc1 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | var init = function() {
|
||
if( $mapSearchElements.length > 0 )
|
||||
return;
|
||||
bc998bc1 | Thomas Heck | |||
for(var i in mapSearchStmts) {
|
||||
var stmt = mapSearchStmts[i];
|
||||
if( stmt.charAt(0) == '#' ) {
|
||||
ef6fcba9 | Thomas Heck | var $elem = $('#'+ prefix + stmt.substring(1));
|
||
if( $elem )
|
||||
$mapSearchElements.push($elem);
|
||||
bc998bc1 | Thomas Heck | }
|
||
}
|
||||
ef6fcba9 | Thomas Heck | };
|
||
bc998bc1 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | var isNotEmpty = function() {
|
||
for(var i in $mapSearchElements)
|
||||
16e18c50 | Moritz Bunkus | if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() === '') )
|
||
ef6fcba9 | Thomas Heck | return false;
|
||
return true;
|
||||
};
|
||||
4cb3a908 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | var showMap = function() {
|
||
var searchString = "";
|
||||
4cb3a908 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | for(var i in mapSearchStmts) {
|
||
var stmt = mapSearchStmts[i];
|
||||
if( stmt.charAt(0) == '#' ) {
|
||||
var val = $('#'+ prefix + stmt.substring(1)).val();
|
||||
if( val )
|
||||
searchString += val;
|
||||
}
|
||||
bc998bc1 | Thomas Heck | else
|
||
ef6fcba9 | Thomas Heck | searchString += stmt;
|
||
}
|
||||
bc998bc1 | Thomas Heck | |||
df601bd3 | Moritz Bunkus | source_address = source_address || '';
|
||
16e18c50 | Moritz Bunkus | var query = source_address !== '' ? 'saddr=' + encodeURIComponent(source_address) + '&daddr=' : 'q=';
|
||
df601bd3 | Moritz Bunkus | var url = 'https://maps.google.com/maps?' + query + encodeURIComponent(searchString);
|
||
bc998bc1 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | window.open(url, '_blank');
|
||
window.focus();
|
||||
};
|
||||
bc998bc1 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | var render = function(widgetWrapper) {
|
||
init();
|
||||
bc998bc1 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | $widgetWrapper = $(widgetWrapper);
|
||
bc998bc1 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | $widgetWrapper
|
||
.html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
|
||||
.click(function() {
|
||||
showMap();
|
||||
});
|
||||
for(var i in $mapSearchElements)
|
||||
2c45e101 | Moritz Bunkus | $mapSearchElements[i].keyup(testInputs);
|
||
ef6fcba9 | Thomas Heck | this.testInputs();
|
||
};
|
||||
bc998bc1 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | var testInputs = function() {
|
||
init();
|
||||
4cb3a908 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | if( isNotEmpty() )
|
||
$widgetWrapper.show();
|
||||
4cb3a908 | Thomas Heck | else
|
||
ef6fcba9 | Thomas Heck | $widgetWrapper.hide();
|
||
};
|
||||
4cb3a908 | Thomas Heck | |||
ef6fcba9 | Thomas Heck | this.render = render;
|
||
this.testInputs = testInputs;
|
||||
4cb3a908 | Thomas Heck | };
|
||
this.showHistoryWindow = function(id) {
|
||||
var xPos = (screen.width - 800) / 2;
|
||||
var yPos = (screen.height - 500) / 2;
|
||||
var parm = "left="+ xPos +",top="+ yPos +",width=800,height=500,status=yes,scrollbars=yes";
|
||||
var url = "common.pl?INPUT_ENCODING=UTF-8&action=show_history&longdescription=&input_name="+ encodeURIComponent(id);
|
||||
window.open(url, "_new_generic", parm);
|
||||
};
|
||||
53d80f2a | Moritz Bunkus | |||
this.update_dial_action = function($input) {
|
||||
var $action = $('#' + $input.prop('id') + '-dial-action');
|
||||
if (!$action)
|
||||
return true;
|
||||
var number = $input.val().replace(/\s+/g, '');
|
||||
16e18c50 | Moritz Bunkus | if (number === '')
|
||
53d80f2a | Moritz Bunkus | $action.hide();
|
||
else
|
||||
$action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
|
||||
return true;
|
||||
};
|
||||
this.init_dial_action = function(input) {
|
||||
if ($('#_cti_enabled').val() != 1)
|
||||
return false;
|
||||
var $input = $(input);
|
||||
var action_id = $input.prop('id') + '-dial-action';
|
||||
if (!$('#' + action_id).size()) {
|
||||
var $action = $('<a href="" id="' + action_id + '" class="cti_call_action" target="_blank" tabindex="-1"></a>');
|
||||
$input.wrap('<span nobr></span>').after($action);
|
||||
$input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
|
||||
}
|
||||
kivi.CustomerVendor.update_dial_action($input);
|
||||
return true;
|
||||
};
|
||||
685108d7 | Sven Schöling | |||
this.inline_report = function(target, source, data){
|
||||
9508e215 | Werner Hahn | // alert("HALLO S " + source + " --T " + target + " tt D " + data);
|
||
685108d7 | Sven Schöling | $.ajax({
|
||
url: source,
|
||||
success: function (rsp) {
|
||||
$(target).html(rsp);
|
||||
$(target).find('.paginate').find('a').click(function(event){ ns.redirect_event(event, target) });
|
||||
$(target).find('a.report-generator-header-link').click(function(event){ ns.redirect_event(event, target) });
|
||||
},
|
||||
data: data,
|
||||
});
|
||||
};
|
||||
this.redirect_event = function(event, target){
|
||||
event.preventDefault();
|
||||
ns.inline_report(target, event.target + '', {});
|
||||
};
|
||||
53d80f2a | Moritz Bunkus | |||
7e7aae8d | Sven Schöling | var KEY = {
|
||
TAB: 9,
|
||||
ENTER: 13,
|
||||
SHIFT: 16,
|
||||
CTRL: 17,
|
||||
ALT: 18,
|
||||
ESCAPE: 27,
|
||||
PAGE_UP: 33,
|
||||
PAGE_DOWN: 34,
|
||||
LEFT: 37,
|
||||
UP: 38,
|
||||
RIGHT: 39,
|
||||
DOWN: 40,
|
||||
};
|
||||
ns.Picker = function($real, options) {
|
||||
var self = this;
|
||||
this.o = $.extend(true, {
|
||||
limit: 20,
|
||||
delay: 50,
|
||||
action: {
|
||||
commit_none: function(){ },
|
||||
commit_one: function(){ $('#update_button').click(); },
|
||||
commit_many: function(){ }
|
||||
}
|
||||
}, $real.data('customer-vendor-picker-data'), options);
|
||||
this.$real = $real;
|
||||
this.real_id = $real.attr('id');
|
||||
this.last_real = $real.val();
|
||||
this.$dummy = $($real.siblings()[0]);
|
||||
this.autocomplete_open = false;
|
||||
this.state = this.STATES.PICKED;
|
||||
this.last_dummy = this.$dummy.val();
|
||||
this.timer = undefined;
|
||||
this.init();
|
||||
};
|
||||
ns.Picker.prototype = {
|
||||
CLASSES: {
|
||||
PICKED: 'customer-vendor-picker-picked',
|
||||
UNDEFINED: 'customer-vendor-picker-undefined',
|
||||
},
|
||||
ajax_data: function(term) {
|
||||
return {
|
||||
'filter.all:substr:multi::ilike': term,
|
||||
'filter.obsolete': 0,
|
||||
current: this.$real.val(),
|
||||
fb722bcc | Bernd Bleßmann | type: this.o.cv_type,
|
||
7e7aae8d | Sven Schöling | };
|
||
},
|
||||
set_item: function(item) {
|
||||
var self = this;
|
||||
if (item.id) {
|
||||
this.$real.val(item.id);
|
||||
// autocomplete ui has name, use the value for ajax items, which contains displayable_name
|
||||
this.$dummy.val(item.name ? item.name : item.value);
|
||||
} else {
|
||||
this.$real.val('');
|
||||
this.$dummy.val('');
|
||||
}
|
||||
this.state = this.STATES.PICKED;
|
||||
this.last_real = this.$real.val();
|
||||
this.last_dummy = this.$dummy.val();
|
||||
this.$real.trigger('change');
|
||||
if (this.o.fat_set_item && item.id) {
|
||||
$.ajax({
|
||||
url: 'controller.pl?action=CustomerVendor/show.json',
|
||||
data: { 'id': item.id, 'db': item.type },
|
||||
success: function(rsp) {
|
||||
self.$real.trigger('set_item:CustomerVendorPicker', rsp);
|
||||
},
|
||||
});
|
||||
} else {
|
||||
this.$real.trigger('set_item:CustomerVendorPicker', item);
|
||||
}
|
||||
this.annotate_state();
|
||||
},
|
||||
set_multi_items: function(data) {
|
||||
this.run_action(this.o.action.set_multi_items, [ data ]);
|
||||
},
|
||||
make_defined_state: function() {
|
||||
if (this.state == this.STATES.PICKED) {
|
||||
this.annotate_state();
|
||||
return true
|
||||
} else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
|
||||
this.set_item({})
|
||||
else {
|
||||
this.set_item({ id: this.last_real, name: this.last_dummy })
|
||||
}
|
||||
this.annotate_state();
|
||||
},
|
||||
annotate_state: function() {
|
||||
if (this.state == this.STATES.PICKED)
|
||||
this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
|
||||
else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
|
||||
this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
|
||||
else {
|
||||
this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
|
||||
}
|
||||
},
|
||||
handle_changed_text: function(callbacks) {
|
||||
var self = this;
|
||||
$.ajax({
|
||||
url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
|
||||
dataType: "json",
|
||||
data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
|
||||
success: function (data) {
|
||||
if (data.length == 1) {
|
||||
self.set_item(data[0]);
|
||||
if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
|
||||
} else if (data.length > 1) {
|
||||
self.state = self.STATES.UNDEFINED;
|
||||
if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
|
||||
} else {
|
||||
self.state = self.STATES.UNDEFINED;
|
||||
if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
|
||||
}
|
||||
self.annotate_state();
|
||||
}
|
||||
});
|
||||
},
|
||||
handle_keydown: function(event) {
|
||||
var self = this;
|
||||
if (event.which == KEY.ENTER || event.which == KEY.TAB) {
|
||||
// if string is empty assume they want to delete
|
||||
if (self.$dummy.val() === '') {
|
||||
self.set_item({});
|
||||
return true;
|
||||
} else if (self.state == self.STATES.PICKED) {
|
||||
if (self.o.action.commit_one) {
|
||||
self.run_action(self.o.action.commit_one);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (event.which == KEY.TAB) {
|
||||
event.preventDefault();
|
||||
self.handle_changed_text();
|
||||
}
|
||||
if (event.which == KEY.ENTER) {
|
||||
bff364a0 | Sven Schöling | event.preventDefault();
|
||
7e7aae8d | Sven Schöling | self.handle_changed_text({
|
||
match_none: self.o.action.commit_none,
|
||||
match_one: self.o.action.commit_one,
|
||||
match_many: self.o.action.commit_many
|
||||
});
|
||||
return false;
|
||||
}
|
||||
} else if (event.which == KEY.DOWN && !self.autocomplete_open) {
|
||||
var old_options = self.$dummy.autocomplete('option');
|
||||
self.$dummy.autocomplete('option', 'minLength', 0);
|
||||
self.$dummy.autocomplete('search', self.$dummy.val());
|
||||
self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
|
||||
} else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
|
||||
self.state = self.STATES.UNDEFINED;
|
||||
}
|
||||
},
|
||||
init: function() {
|
||||
var self = this;
|
||||
this.$dummy.autocomplete({
|
||||
source: function(req, rsp) {
|
||||
$.ajax($.extend({}, self.o, {
|
||||
url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
|
||||
dataType: "json",
|
||||
type: 'get',
|
||||
data: self.ajax_data(req.term),
|
||||
success: function (data){ rsp(data) }
|
||||
}));
|
||||
},
|
||||
select: function(event, ui) {
|
||||
self.set_item(ui.item);
|
||||
76bc0ada | Sven Schöling | if (self.o.action.commit_one) {
|
||
self.run_action(self.o.action.commit_one);
|
||||
}
|
||||
7e7aae8d | Sven Schöling | },
|
||
search: function(event, ui) {
|
||||
if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
|
||||
event.preventDefault();
|
||||
},
|
||||
open: function() {
|
||||
self.autocomplete_open = true;
|
||||
},
|
||||
close: function() {
|
||||
self.autocomplete_open = false;
|
||||
}
|
||||
});
|
||||
this.$dummy.keydown(function(event){ self.handle_keydown(event) });
|
||||
this.$dummy.on('paste', function(){
|
||||
setTimeout(function() {
|
||||
self.handle_changed_text();
|
||||
}, 1);
|
||||
});
|
||||
this.$dummy.blur(function(){
|
||||
window.clearTimeout(self.timer);
|
||||
self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
|
||||
});
|
||||
},
|
||||
run_action: function(code, args) {
|
||||
if (typeof code === 'function')
|
||||
code.apply(this, args)
|
||||
else
|
||||
kivi.run(code, args);
|
||||
},
|
||||
clear: function() {
|
||||
this.set_item({});
|
||||
}
|
||||
};
|
||||
ns.Picker.prototype.STATES = {
|
||||
PICKED: ns.Picker.prototype.CLASSES.PICKED,
|
||||
UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
|
||||
};
|
||||
ns.reinit_widgets = function() {
|
||||
kivi.run_once_for('input.customer_vendor_autocomplete', 'customer_vendor_picker', function(elt) {
|
||||
if (!$(elt).data('customer_vendor_picker'))
|
||||
$(elt).data('customer_vendor_picker', new kivi.CustomerVendor.Picker($(elt)));
|
||||
});
|
||||
$('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
|
||||
kivi.CustomerVendor.init_dial_action($(elt));
|
||||
});
|
||||
}
|
||||
ns.init = function() {
|
||||
ns.reinit_widgets();
|
||||
}
|
||||
ffe54348 | Bernd Bleßmann | ns.get_price_report = function(target, source, data) {
|
||
$.ajax({
|
||||
url: source,
|
||||
success: function (rsp) {
|
||||
$(target).html(rsp);
|
||||
$(target).find('a.report-generator-header-link').click(function(event){ ns.price_report_redirect_event(event, target) });
|
||||
},
|
||||
});
|
||||
};
|
||||
ns.price_report_redirect_event = function (event, target) {
|
||||
event.preventDefault();
|
||||
ns.get_price_report(target, event.target + '');
|
||||
};
|
||||
ns.price_list_init = function () {
|
||||
$("#customer_vendor_tabs").on('tabsbeforeactivate', function(event, ui){
|
||||
if (ui.newPanel.attr('id') == 'price_list') {
|
||||
ns.get_price_report('#price_list', "controller.pl?action=CustomerVendor/ajax_list_prices&id=" + $('#cv_id').val() + "&db=" + $('#db').val() + "&callback=" + $('#callback').val());
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
$("#customer_vendor_tabs").on('tabscreate', function(event, ui){
|
||||
if (ui.panel.attr('id') == 'price_list') {
|
||||
ns.get_price_report('#price_list', "controller.pl?action=CustomerVendor/ajax_list_prices&id=" + $('#cv_id').val() + "&db=" + $('#db').val() + "&callback=" + $('#callback').val());
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
7e7aae8d | Sven Schöling | $(function(){
|
||
ns.init();
|
||||
ffe54348 | Bernd Bleßmann | ns.price_list_init();
|
||
53d80f2a | Moritz Bunkus | });
|
||
7e7aae8d | Sven Schöling | });
|