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();
74 if (data.contact.disable_cp_main === 1)
75 $("#contact_cp_main").prop("disabled", true);
77 $("#contact_cp_main").prop("disabled", false);
78 if ( params.onFormSet )
82 $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
85 var mapSearchStmts = [
95 this.MapWidget = function(prefix, source_address)
97 var $mapSearchElements = [];
100 var init = function() {
101 if( $mapSearchElements.length > 0 )
104 for(var i in mapSearchStmts) {
105 var stmt = mapSearchStmts[i];
106 if( stmt.charAt(0) == '#' ) {
107 var $elem = $('#'+ prefix + stmt.substring(1));
109 $mapSearchElements.push($elem);
114 var isNotEmpty = function() {
115 for(var i in $mapSearchElements)
116 if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() === '') )
121 var showMap = function() {
122 var searchString = "";
124 for(var i in mapSearchStmts) {
125 var stmt = mapSearchStmts[i];
126 if( stmt.charAt(0) == '#' ) {
127 var val = $('#'+ prefix + stmt.substring(1)).val();
132 searchString += stmt;
135 source_address = source_address || '';
136 var query = source_address !== '' ? 'saddr=' + encodeURIComponent(source_address) + '&daddr=' : 'q=';
137 var url = 'https://maps.google.com/maps?' + query + encodeURIComponent(searchString);
139 window.open(url, '_blank');
143 var render = function(widgetWrapper) {
146 $widgetWrapper = $(widgetWrapper);
149 .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
153 for(var i in $mapSearchElements)
154 $mapSearchElements[i].keyup(testInputs);
158 var testInputs = function() {
162 $widgetWrapper.show();
164 $widgetWrapper.hide();
167 this.render = render;
168 this.testInputs = testInputs;
171 this.showHistoryWindow = function(id) {
172 var xPos = (screen.width - 800) / 2;
173 var yPos = (screen.height - 500) / 2;
174 var parm = "left="+ xPos +",top="+ yPos +",width=800,height=500,status=yes,scrollbars=yes";
175 var url = "common.pl?INPUT_ENCODING=UTF-8&action=show_history&longdescription=&input_name="+ encodeURIComponent(id);
176 window.open(url, "_new_generic", parm);
179 this.update_dial_action = function($input) {
180 var $action = $('#' + $input.prop('id') + '-dial-action');
185 var number = $input.val().replace(/\s+/g, '');
189 $action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
194 this.init_dial_action = function(input) {
195 if ($('#_cti_enabled').val() != 1)
198 var $input = $(input);
199 var action_id = $input.prop('id') + '-dial-action';
201 if (!$('#' + action_id).size()) {
202 var $action = $('<a href="" id="' + action_id + '" class="cti_call_action" target="_blank" tabindex="-1"></a>');
203 $input.wrap('<span nobr></span>').after($action);
205 $input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
208 kivi.CustomerVendor.update_dial_action($input);
213 this.inline_report = function(target, source, data){
214 // alert("HALLO S " + source + " --T " + target + " tt D " + data);
217 success: function (rsp) {
219 $(target).find('.paginate').find('a').click(function(event){ ns.redirect_event(event, target) });
220 $(target).find('a.report-generator-header-link').click(function(event){ ns.redirect_event(event, target) });
225 this.redirect_event = function(event, target){
226 event.preventDefault();
227 ns.inline_report(target, event.target + '', {});
245 ns.Picker = function($real, options) {
247 this.o = $.extend(true, {
251 commit_none: function(){ },
252 commit_one: function(){ $('#update_button').click(); },
253 commit_many: function(){ }
255 }, $real.data('customer-vendor-picker-data'), options);
257 this.real_id = $real.attr('id');
258 this.last_real = $real.val();
259 this.$dummy = $($real.siblings()[0]);
260 this.autocomplete_open = false;
261 this.state = this.STATES.PICKED;
262 this.last_dummy = this.$dummy.val();
263 this.timer = undefined;
268 ns.Picker.prototype = {
270 PICKED: 'customer-vendor-picker-picked',
271 UNDEFINED: 'customer-vendor-picker-undefined',
273 ajax_data: function(term) {
275 'filter.all:substr:multi::ilike': term,
276 'filter.obsolete': 0,
277 current: this.$real.val(),
278 type: this.o.cv_type,
281 set_item: function(item) {
284 this.$real.val(item.id);
285 // autocomplete ui has name, use the value for ajax items, which contains displayable_name
286 this.$dummy.val(item.name ? item.name : item.value);
291 this.state = this.STATES.PICKED;
292 this.last_real = this.$real.val();
293 this.last_dummy = this.$dummy.val();
294 this.$real.trigger('change');
296 if (this.o.fat_set_item && item.id) {
298 url: 'controller.pl?action=CustomerVendor/show.json',
299 data: { 'id': item.id, 'db': item.type },
300 success: function(rsp) {
301 self.$real.trigger('set_item:CustomerVendorPicker', rsp);
305 this.$real.trigger('set_item:CustomerVendorPicker', item);
307 this.annotate_state();
309 set_multi_items: function(data) {
310 this.run_action(this.o.action.set_multi_items, [ data ]);
312 make_defined_state: function() {
313 if (this.state == this.STATES.PICKED) {
314 this.annotate_state();
316 } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
319 this.set_item({ id: this.last_real, name: this.last_dummy })
321 this.annotate_state();
323 annotate_state: function() {
324 if (this.state == this.STATES.PICKED)
325 this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
326 else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
327 this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
329 this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
332 handle_changed_text: function(callbacks) {
335 url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
337 data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
338 success: function (data) {
339 if (data.length == 1) {
340 self.set_item(data[0]);
341 if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
342 } else if (data.length > 1) {
343 self.state = self.STATES.UNDEFINED;
344 if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
346 self.state = self.STATES.UNDEFINED;
347 if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
349 self.annotate_state();
353 handle_keydown: function(event) {
355 if (event.which == KEY.ENTER || event.which == KEY.TAB) {
356 // if string is empty assume they want to delete
357 if (self.$dummy.val() === '') {
360 } else if (self.state == self.STATES.PICKED) {
361 if (self.o.action.commit_one) {
362 self.run_action(self.o.action.commit_one);
366 if (event.which == KEY.TAB) {
367 event.preventDefault();
368 self.handle_changed_text();
370 if (event.which == KEY.ENTER) {
371 event.preventDefault();
372 self.handle_changed_text({
373 match_none: self.o.action.commit_none,
374 match_one: self.o.action.commit_one,
375 match_many: self.o.action.commit_many
379 } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
380 var old_options = self.$dummy.autocomplete('option');
381 self.$dummy.autocomplete('option', 'minLength', 0);
382 self.$dummy.autocomplete('search', self.$dummy.val());
383 self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
384 } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
385 self.state = self.STATES.UNDEFINED;
390 this.$dummy.autocomplete({
391 source: function(req, rsp) {
392 $.ajax($.extend({}, self.o, {
393 url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
396 data: self.ajax_data(req.term),
397 success: function (data){ rsp(data) }
400 select: function(event, ui) {
401 self.set_item(ui.item);
402 if (self.o.action.commit_one) {
403 self.run_action(self.o.action.commit_one);
406 search: function(event, ui) {
407 if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
408 event.preventDefault();
411 self.autocomplete_open = true;
414 self.autocomplete_open = false;
417 this.$dummy.keydown(function(event){ self.handle_keydown(event) });
418 this.$dummy.on('paste', function(){
419 setTimeout(function() {
420 self.handle_changed_text();
423 this.$dummy.blur(function(){
424 window.clearTimeout(self.timer);
425 self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
428 run_action: function(code, args) {
429 if (typeof code === 'function')
430 code.apply(this, args)
432 kivi.run(code, args);
438 ns.Picker.prototype.STATES = {
439 PICKED: ns.Picker.prototype.CLASSES.PICKED,
440 UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
443 ns.reinit_widgets = function() {
444 kivi.run_once_for('input.customer_vendor_autocomplete', 'customer_vendor_picker', function(elt) {
445 if (!$(elt).data('customer_vendor_picker'))
446 $(elt).data('customer_vendor_picker', new kivi.CustomerVendor.Picker($(elt)));
449 $('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
450 kivi.CustomerVendor.init_dial_action($(elt));
454 ns.init = function() {
458 ns.get_price_report = function(target, source, data) {
461 success: function (rsp) {
463 $(target).find('a.report-generator-header-link').click(function(event){ ns.price_report_redirect_event(event, target) });
468 ns.price_report_redirect_event = function (event, target) {
469 event.preventDefault();
470 ns.get_price_report(target, event.target + '');
473 ns.price_list_init = function () {
474 $("#customer_vendor_tabs").on('tabsbeforeactivate', function(event, ui){
475 if (ui.newPanel.attr('id') == 'price_list') {
476 ns.get_price_report('#price_list', "controller.pl?action=CustomerVendor/ajax_list_prices&id=" + $('#cv_id').val() + "&db=" + $('#db').val() + "&callback=" + $('#callback').val());
481 $("#customer_vendor_tabs").on('tabscreate', function(event, ui){
482 if (ui.panel.attr('id') == 'price_list') {
483 ns.get_price_report('#price_list', "controller.pl?action=CustomerVendor/ajax_list_prices&id=" + $('#cv_id').val() + "&db=" + $('#db').val() + "&callback=" + $('#callback').val());
491 ns.price_list_init();