1 namespace('kivi.CustomerVendor', function(ns) {
3 this.selectShipto = function(params) {
4 var shiptoId = $('#shipto_shipto_id').val();
5 var url = 'controller.pl?action=CustomerVendor/ajaj_get_shipto&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ shiptoId;
7 $.getJSON(url, function(data) {
8 var shipto = data.shipto;
10 $('#shipto_'+ key).val(shipto[key])
12 kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.shipto_cvars, 'shipto_cvars_');
15 $('#action_delete_shipto').show();
17 $('#action_delete_shipto').hide();
19 if ( params.onFormSet )
24 this.selectDelivery = function(fromDate, toDate) {
25 var deliveryId = $('#delivery_id').val();
28 $("#delivery").empty();
30 var url = 'controller.pl?action=CustomerVendor/get_delivery&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ $('#delivery_id').val();
32 if( fromDate && toDate )
33 url += '&delivery_from='+ fromDate +'&delivery_to='+ toDate;
35 $('#delivery').load(url);
39 this.setCustomVariablesFromAJAJ = function(cvars, prefix) {
40 for (var key in cvars) {
41 var cvar = cvars[key];
42 var $ctrl = $('#' + prefix + key);
44 if (cvar.type == 'bool')
45 $ctrl.prop('checked', cvar.value == 1 ? 'checked' : '');
47 else if ((cvar.type == 'customer') || (cvar.type == 'vendor'))
48 kivi.CustomerVendor.Picker($ctrl).set_item({ id: cvar.id, name: cvar.value });
50 else if (cvar.type == 'part')
51 kivi.Part.Picker($ctrl).set_item({ id: cvar.id, name: cvar.value });
54 $ctrl.val(cvar.value);
58 this.selectContact = function(params) {
59 var contactId = $('#contact_cp_id').val();
61 var url = 'controller.pl?action=CustomerVendor/ajaj_get_contact&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&contact_id='+ contactId;
63 $.getJSON(url, function(data) {
64 var contact = data.contact;
65 for(var key in contact)
66 $('#contact_'+ key).val(contact[key])
68 kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.contact_cvars, 'contact_cvars_');
71 $('#action_delete_contact').show();
73 $('#action_delete_contact').hide();
75 if ( params.onFormSet )
79 $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
82 var mapSearchStmts = [
92 this.MapWidget = function(prefix, source_address)
94 var $mapSearchElements = [];
97 var init = function() {
98 if( $mapSearchElements.length > 0 )
101 for(var i in mapSearchStmts) {
102 var stmt = mapSearchStmts[i];
103 if( stmt.charAt(0) == '#' ) {
104 var $elem = $('#'+ prefix + stmt.substring(1));
106 $mapSearchElements.push($elem);
111 var isNotEmpty = function() {
112 for(var i in $mapSearchElements)
113 if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() === '') )
118 var showMap = function() {
119 var searchString = "";
121 for(var i in mapSearchStmts) {
122 var stmt = mapSearchStmts[i];
123 if( stmt.charAt(0) == '#' ) {
124 var val = $('#'+ prefix + stmt.substring(1)).val();
129 searchString += stmt;
132 source_address = source_address || '';
133 var query = source_address !== '' ? 'saddr=' + encodeURIComponent(source_address) + '&daddr=' : 'q=';
134 var url = 'https://maps.google.com/maps?' + query + encodeURIComponent(searchString);
136 window.open(url, '_blank');
140 var render = function(widgetWrapper) {
143 $widgetWrapper = $(widgetWrapper);
146 .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
150 for(var i in $mapSearchElements)
151 $mapSearchElements[i].keyup(testInputs);
155 var testInputs = function() {
159 $widgetWrapper.show();
161 $widgetWrapper.hide();
164 this.render = render;
165 this.testInputs = testInputs;
168 this.showHistoryWindow = function(id) {
169 var xPos = (screen.width - 800) / 2;
170 var yPos = (screen.height - 500) / 2;
171 var parm = "left="+ xPos +",top="+ yPos +",width=800,height=500,status=yes,scrollbars=yes";
172 var url = "common.pl?INPUT_ENCODING=UTF-8&action=show_history&longdescription=&input_name="+ encodeURIComponent(id);
173 window.open(url, "_new_generic", parm);
176 this.update_dial_action = function($input) {
177 var $action = $('#' + $input.prop('id') + '-dial-action');
182 var number = $input.val().replace(/\s+/g, '');
186 $action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
191 this.init_dial_action = function(input) {
192 if ($('#_cti_enabled').val() != 1)
195 var $input = $(input);
196 var action_id = $input.prop('id') + '-dial-action';
198 if (!$('#' + action_id).size()) {
199 var $action = $('<a href="" id="' + action_id + '" class="cti_call_action" target="_blank" tabindex="-1"></a>');
200 $input.wrap('<span nobr></span>').after($action);
202 $input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
205 kivi.CustomerVendor.update_dial_action($input);
210 this.inline_report = function(target, source, data){
213 success: function (rsp) {
215 $(target).find('.paginate').find('a').click(function(event){ ns.redirect_event(event, target) });
216 $(target).find('a.report-generator-header-link').click(function(event){ ns.redirect_event(event, target) });
221 this.redirect_event = function(event, target){
222 event.preventDefault();
223 ns.inline_report(target, event.target + '', {});
241 ns.Picker = function($real, options) {
243 this.o = $.extend(true, {
247 commit_none: function(){ },
248 commit_one: function(){ $('#update_button').click(); },
249 commit_many: function(){ }
251 }, $real.data('customer-vendor-picker-data'), options);
253 this.real_id = $real.attr('id');
254 this.last_real = $real.val();
255 this.$dummy = $($real.siblings()[0]);
256 this.autocomplete_open = false;
257 this.state = this.STATES.PICKED;
258 this.last_dummy = this.$dummy.val();
259 this.timer = undefined;
264 ns.Picker.prototype = {
266 PICKED: 'customer-vendor-picker-picked',
267 UNDEFINED: 'customer-vendor-picker-undefined',
269 ajax_data: function(term) {
271 'filter.all:substr:multi::ilike': term,
272 'filter.obsolete': 0,
273 current: this.$real.val(),
277 set_item: function(item) {
280 this.$real.val(item.id);
281 // autocomplete ui has name, use the value for ajax items, which contains displayable_name
282 this.$dummy.val(item.name ? item.name : item.value);
287 this.state = this.STATES.PICKED;
288 this.last_real = this.$real.val();
289 this.last_dummy = this.$dummy.val();
290 this.$real.trigger('change');
292 if (this.o.fat_set_item && item.id) {
294 url: 'controller.pl?action=CustomerVendor/show.json',
295 data: { 'id': item.id, 'db': item.type },
296 success: function(rsp) {
297 self.$real.trigger('set_item:CustomerVendorPicker', rsp);
301 this.$real.trigger('set_item:CustomerVendorPicker', item);
303 this.annotate_state();
305 set_multi_items: function(data) {
306 this.run_action(this.o.action.set_multi_items, [ data ]);
308 make_defined_state: function() {
309 if (this.state == this.STATES.PICKED) {
310 this.annotate_state();
312 } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
315 this.set_item({ id: this.last_real, name: this.last_dummy })
317 this.annotate_state();
319 annotate_state: function() {
320 if (this.state == this.STATES.PICKED)
321 this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
322 else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
323 this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
325 this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
328 handle_changed_text: function(callbacks) {
331 url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
333 data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
334 success: function (data) {
335 if (data.length == 1) {
336 self.set_item(data[0]);
337 if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
338 } else if (data.length > 1) {
339 self.state = self.STATES.UNDEFINED;
340 if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
342 self.state = self.STATES.UNDEFINED;
343 if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
345 self.annotate_state();
349 handle_keydown: function(event) {
351 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
352 // if string is empty assume they want to delete
353 if (self.$dummy.val() === '') {
356 } else if (self.state == self.STATES.PICKED) {
357 if (self.o.action.commit_one) {
358 self.run_action(self.o.action.commit_one);
362 if (event.which == KEY.TAB) {
363 event.preventDefault();
364 self.handle_changed_text();
366 if (event.which == KEY.ENTER) {
367 event.preventDefault();
368 self.handle_changed_text({
369 match_none: self.o.action.commit_none,
370 match_one: self.o.action.commit_one,
371 match_many: self.o.action.commit_many
375 } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
376 var old_options = self.$dummy.autocomplete('option');
377 self.$dummy.autocomplete('option', 'minLength', 0);
378 self.$dummy.autocomplete('search', self.$dummy.val());
379 self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
380 } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
381 self.state = self.STATES.UNDEFINED;
386 this.$dummy.autocomplete({
387 source: function(req, rsp) {
388 $.ajax($.extend({}, self.o, {
389 url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
392 data: self.ajax_data(req.term),
393 success: function (data){ rsp(data) }
396 select: function(event, ui) {
397 self.set_item(ui.item);
399 search: function(event, ui) {
400 if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
401 event.preventDefault();
404 self.autocomplete_open = true;
407 self.autocomplete_open = false;
410 this.$dummy.keydown(function(event){ self.handle_keydown(event) });
411 this.$dummy.on('paste', function(){
412 setTimeout(function() {
413 self.handle_changed_text();
416 this.$dummy.blur(function(){
417 window.clearTimeout(self.timer);
418 self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
421 run_action: function(code, args) {
422 if (typeof code === 'function')
423 code.apply(this, args)
425 kivi.run(code, args);
431 ns.Picker.prototype.STATES = {
432 PICKED: ns.Picker.prototype.CLASSES.PICKED,
433 UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
436 ns.reinit_widgets = function() {
437 kivi.run_once_for('input.customer_vendor_autocomplete', 'customer_vendor_picker', function(elt) {
438 if (!$(elt).data('customer_vendor_picker'))
439 $(elt).data('customer_vendor_picker', new kivi.CustomerVendor.Picker($(elt)));
442 $('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
443 kivi.CustomerVendor.init_dial_action($(elt));
447 ns.init = function() {