Kundenstatistik: erster commit ohne Webtemplates
[kivitendo-erp.git] / js / kivi.CustomerVendor.js
1 namespace('kivi.CustomerVendor', function(ns) {
2
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;
6
7     $.getJSON(url, function(data) {
8       var shipto = data.shipto;
9       for(var key in shipto)
10         $('#shipto_'+ key).val(shipto[key])
11
12       kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.shipto_cvars, 'shipto_cvars_');
13
14       if ( shiptoId )
15         $('#action_delete_shipto').show();
16       else
17         $('#action_delete_shipto').hide();
18
19       if ( params.onFormSet )
20         params.onFormSet();
21     });
22   };
23
24   this.selectDelivery = function(fromDate, toDate) {
25     var deliveryId = $('#delivery_id').val();
26
27     if( !deliveryId )
28       $("#delivery").empty();
29     else {
30       var url = 'controller.pl?action=CustomerVendor/get_delivery&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ $('#delivery_id').val();
31
32       if( fromDate && toDate )
33         url += '&delivery_from='+ fromDate +'&delivery_to='+ toDate;
34
35       $('#delivery').load(url);
36     }
37   };
38
39   this.setCustomVariablesFromAJAJ = function(cvars, prefix) {
40     for (var key in cvars) {
41       var cvar  = cvars[key];
42       var $ctrl = $('#' + prefix + key);
43
44       if (cvar.type == 'bool')
45         $ctrl.prop('checked', cvar.value == 1 ? 'checked' : '');
46
47       else if ((cvar.type == 'customer') || (cvar.type == 'vendor'))
48         kivi.CustomerVendor.Picker($ctrl).set_item({ id: cvar.id, name: cvar.value });
49
50       else if (cvar.type == 'part')
51         kivi.Part.Picker($ctrl).set_item({ id: cvar.id, name: cvar.value });
52
53       else
54         $ctrl.val(cvar.value);
55     }
56   };
57
58   this.selectContact = function(params) {
59     var contactId = $('#contact_cp_id').val();
60
61           var url = 'controller.pl?action=CustomerVendor/ajaj_get_contact&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&contact_id='+ contactId;
62
63     $.getJSON(url, function(data) {
64       var contact = data.contact;
65       for(var key in contact)
66         $('#contact_'+ key).val(contact[key])
67
68       kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.contact_cvars, 'contact_cvars_');
69
70       if ( contactId )
71         $('#action_delete_contact').show();
72       else
73         $('#action_delete_contact').hide();
74
75       if ( params.onFormSet )
76         params.onFormSet();
77     });
78
79     $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
80   };
81
82   var mapSearchStmts = [
83     '#street',
84     ', ',
85     '#zipcode',
86     ' ',
87     '#city',
88     ', ',
89     '#country'
90   ];
91
92   this.MapWidget = function(prefix, source_address)
93   {
94     var $mapSearchElements = [];
95     var $widgetWrapper;
96
97     var init = function() {
98       if( $mapSearchElements.length > 0 )
99         return;
100
101       for(var i in mapSearchStmts) {
102         var stmt = mapSearchStmts[i];
103         if( stmt.charAt(0) == '#' ) {
104           var $elem = $('#'+ prefix + stmt.substring(1));
105           if( $elem )
106             $mapSearchElements.push($elem);
107         }
108       }
109     };
110
111     var isNotEmpty = function() {
112       for(var i in $mapSearchElements)
113         if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() === '') )
114           return false;
115       return true;
116     };
117
118     var showMap = function() {
119       var searchString = "";
120
121       for(var i in mapSearchStmts) {
122         var stmt = mapSearchStmts[i];
123         if( stmt.charAt(0) == '#' ) {
124           var val = $('#'+ prefix + stmt.substring(1)).val();
125           if( val )
126             searchString += val;
127         }
128         else
129           searchString += stmt;
130       }
131
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);
135
136       window.open(url, '_blank');
137       window.focus();
138     };
139
140     var render = function(widgetWrapper) {
141       init();
142
143       $widgetWrapper = $(widgetWrapper);
144
145       $widgetWrapper
146         .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
147         .click(function() {
148           showMap();
149         });
150       for(var i in $mapSearchElements)
151         $mapSearchElements[i].keyup(testInputs);
152       this.testInputs();
153     };
154
155     var testInputs = function() {
156       init();
157
158       if( isNotEmpty() )
159         $widgetWrapper.show();
160       else
161         $widgetWrapper.hide();
162     };
163
164     this.render = render;
165     this.testInputs = testInputs;
166   };
167
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);
174   };
175
176   this.update_dial_action = function($input) {
177     var $action = $('#' + $input.prop('id') + '-dial-action');
178
179     if (!$action)
180       return true;
181
182     var number = $input.val().replace(/\s+/g, '');
183     if (number === '')
184       $action.hide();
185     else
186       $action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
187
188     return true;
189   };
190
191   this.init_dial_action = function(input) {
192     if ($('#_cti_enabled').val() != 1)
193       return false;
194
195     var $input    = $(input);
196     var action_id = $input.prop('id') + '-dial-action';
197
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);
201
202       $input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
203     }
204
205     kivi.CustomerVendor.update_dial_action($input);
206
207     return true;
208   };
209
210   this.inline_report = function(target, source, data){
211 //    alert("HALLO S " + source + " --T " + target + " tt D " + data);
212     $.ajax({
213       url:        source,
214       success:    function (rsp) {
215         $(target).html(rsp);
216         $(target).find('.paginate').find('a').click(function(event){ ns.redirect_event(event, target) });
217         $(target).find('a.report-generator-header-link').click(function(event){ ns.redirect_event(event, target) });
218       },
219       data:       data,
220     });
221   };
222   this.redirect_event = function(event, target){
223     event.preventDefault();
224     ns.inline_report(target, event.target + '', {});
225   };
226
227   var KEY = {
228     TAB:       9,
229     ENTER:     13,
230     SHIFT:     16,
231     CTRL:      17,
232     ALT:       18,
233     ESCAPE:    27,
234     PAGE_UP:   33,
235     PAGE_DOWN: 34,
236     LEFT:      37,
237     UP:        38,
238     RIGHT:     39,
239     DOWN:      40,
240   };
241
242   ns.Picker = function($real, options) {
243     var self = this;
244     this.o = $.extend(true, {
245       limit: 20,
246       delay: 50,
247       action: {
248         commit_none: function(){ },
249         commit_one:  function(){ $('#update_button').click(); },
250         commit_many: function(){ }
251       }
252     }, $real.data('customer-vendor-picker-data'), options);
253     this.$real              = $real;
254     this.real_id            = $real.attr('id');
255     this.last_real          = $real.val();
256     this.$dummy             = $($real.siblings()[0]);
257     this.autocomplete_open  = false;
258     this.state              = this.STATES.PICKED;
259     this.last_dummy         = this.$dummy.val();
260     this.timer              = undefined;
261
262     this.init();
263   };
264
265   ns.Picker.prototype = {
266     CLASSES: {
267       PICKED:       'customer-vendor-picker-picked',
268       UNDEFINED:    'customer-vendor-picker-undefined',
269     },
270     ajax_data: function(term) {
271       return {
272         'filter.all:substr:multi::ilike': term,
273         'filter.obsolete': 0,
274         current:  this.$real.val(),
275         type:     this.o.cv_type,
276       };
277     },
278     set_item: function(item) {
279       var self = this;
280       if (item.id) {
281         this.$real.val(item.id);
282         // autocomplete ui has name, use the value for ajax items, which contains displayable_name
283         this.$dummy.val(item.name ? item.name : item.value);
284       } else {
285         this.$real.val('');
286         this.$dummy.val('');
287       }
288       this.state      = this.STATES.PICKED;
289       this.last_real  = this.$real.val();
290       this.last_dummy = this.$dummy.val();
291       this.$real.trigger('change');
292
293       if (this.o.fat_set_item && item.id) {
294         $.ajax({
295           url: 'controller.pl?action=CustomerVendor/show.json',
296           data: { 'id': item.id, 'db': item.type },
297           success: function(rsp) {
298             self.$real.trigger('set_item:CustomerVendorPicker', rsp);
299           },
300         });
301       } else {
302         this.$real.trigger('set_item:CustomerVendorPicker', item);
303       }
304       this.annotate_state();
305     },
306     set_multi_items: function(data) {
307       this.run_action(this.o.action.set_multi_items, [ data ]);
308     },
309     make_defined_state: function() {
310       if (this.state == this.STATES.PICKED) {
311         this.annotate_state();
312         return true
313       } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
314         this.set_item({})
315       else {
316         this.set_item({ id: this.last_real, name: this.last_dummy })
317       }
318       this.annotate_state();
319     },
320     annotate_state: function() {
321       if (this.state == this.STATES.PICKED)
322         this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
323       else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
324         this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
325       else {
326         this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
327       }
328     },
329     handle_changed_text: function(callbacks) {
330       var self = this;
331       $.ajax({
332         url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
333         dataType: "json",
334         data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
335         success: function (data) {
336           if (data.length == 1) {
337             self.set_item(data[0]);
338             if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
339           } else if (data.length > 1) {
340             self.state = self.STATES.UNDEFINED;
341             if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
342           } else {
343             self.state = self.STATES.UNDEFINED;
344             if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
345           }
346           self.annotate_state();
347         }
348       });
349     },
350     handle_keydown: function(event) {
351       var self = this;
352       if (event.which == KEY.ENTER || event.which == KEY.TAB) {
353         // if string is empty assume they want to delete
354         if (self.$dummy.val() === '') {
355           self.set_item({});
356           return true;
357         } else if (self.state == self.STATES.PICKED) {
358           if (self.o.action.commit_one) {
359             self.run_action(self.o.action.commit_one);
360           }
361           return true;
362         }
363         if (event.which == KEY.TAB) {
364           event.preventDefault();
365           self.handle_changed_text();
366         }
367         if (event.which == KEY.ENTER) {
368           event.preventDefault();
369           self.handle_changed_text({
370             match_none: self.o.action.commit_none,
371             match_one:  self.o.action.commit_one,
372             match_many: self.o.action.commit_many
373           });
374           return false;
375         }
376       } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
377         var old_options = self.$dummy.autocomplete('option');
378         self.$dummy.autocomplete('option', 'minLength', 0);
379         self.$dummy.autocomplete('search', self.$dummy.val());
380         self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
381       } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
382         self.state = self.STATES.UNDEFINED;
383       }
384     },
385     init: function() {
386       var self = this;
387       this.$dummy.autocomplete({
388         source: function(req, rsp) {
389           $.ajax($.extend({}, self.o, {
390             url:      'controller.pl?action=CustomerVendor/ajaj_autocomplete',
391             dataType: "json",
392             type:     'get',
393             data:     self.ajax_data(req.term),
394             success:  function (data){ rsp(data) }
395           }));
396         },
397         select: function(event, ui) {
398           self.set_item(ui.item);
399           if (self.o.action.commit_one) {
400             self.run_action(self.o.action.commit_one);
401           }
402         },
403         search: function(event, ui) {
404           if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
405             event.preventDefault();
406         },
407         open: function() {
408           self.autocomplete_open = true;
409         },
410         close: function() {
411           self.autocomplete_open = false;
412         }
413       });
414       this.$dummy.keydown(function(event){ self.handle_keydown(event) });
415       this.$dummy.on('paste', function(){
416         setTimeout(function() {
417           self.handle_changed_text();
418         }, 1);
419       });
420       this.$dummy.blur(function(){
421         window.clearTimeout(self.timer);
422         self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
423       });
424     },
425     run_action: function(code, args) {
426       if (typeof code === 'function')
427         code.apply(this, args)
428       else
429         kivi.run(code, args);
430     },
431     clear: function() {
432       this.set_item({});
433     }
434   };
435   ns.Picker.prototype.STATES = {
436     PICKED:    ns.Picker.prototype.CLASSES.PICKED,
437     UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
438   };
439
440   ns.reinit_widgets = function() {
441     kivi.run_once_for('input.customer_vendor_autocomplete', 'customer_vendor_picker', function(elt) {
442       if (!$(elt).data('customer_vendor_picker'))
443         $(elt).data('customer_vendor_picker', new kivi.CustomerVendor.Picker($(elt)));
444     });
445
446     $('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
447       kivi.CustomerVendor.init_dial_action($(elt));
448     });
449   }
450
451   ns.init = function() {
452     ns.reinit_widgets();
453   }
454
455   $(function(){
456     ns.init();
457   });
458 });