e8cd9236da45291a017b861efbc33b5def017fbe
[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         $('#contact_cp_title_select').val(contact['cp_title']);
73         $('#contact_cp_abteilung_select').val(contact['cp_abteilung']);
74       } else {
75         $('#action_delete_contact').hide();
76         $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
77       }
78       if (data.contact.disable_cp_main === 1)
79         $("#contact_cp_main").prop("disabled", true);
80       else
81         $("#contact_cp_main").prop("disabled", false);
82       if ( params.onFormSet )
83         params.onFormSet();
84     });
85
86   };
87
88   var mapSearchStmts = [
89     '#street',
90     ', ',
91     '#zipcode',
92     ' ',
93     '#city',
94     ', ',
95     '#country'
96   ];
97
98   this.MapWidget = function(prefix, source_address)
99   {
100     var $mapSearchElements = [];
101     var $widgetWrapper;
102
103     var init = function() {
104       if( $mapSearchElements.length > 0 )
105         return;
106
107       for(var i in mapSearchStmts) {
108         var stmt = mapSearchStmts[i];
109         if( stmt.charAt(0) == '#' ) {
110           var $elem = $('#'+ prefix + stmt.substring(1));
111           if( $elem )
112             $mapSearchElements.push($elem);
113         }
114       }
115     };
116
117     var isNotEmpty = function() {
118       for(var i in $mapSearchElements)
119         if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() === '') )
120           return false;
121       return true;
122     };
123
124     var showMap = function() {
125       var searchString = "";
126
127       for(var i in mapSearchStmts) {
128         var stmt = mapSearchStmts[i];
129         if( stmt.charAt(0) == '#' ) {
130           var val = $('#'+ prefix + stmt.substring(1)).val();
131           if( val )
132             searchString += val;
133         }
134         else
135           searchString += stmt;
136       }
137
138       source_address = source_address || '';
139       var query      = source_address !== '' ? 'saddr=' + encodeURIComponent(source_address) + '&daddr=' : 'q=';
140       var url        = 'https://maps.google.com/maps?' + query + encodeURIComponent(searchString);
141
142       window.open(url, '_blank');
143       window.focus();
144     };
145
146     var render = function(widgetWrapper) {
147       init();
148
149       $widgetWrapper = $(widgetWrapper);
150
151       $widgetWrapper
152         .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
153         .click(function() {
154           showMap();
155         });
156       for(var i in $mapSearchElements)
157         $mapSearchElements[i].keyup(testInputs);
158       this.testInputs();
159     };
160
161     var testInputs = function() {
162       init();
163
164       if( isNotEmpty() )
165         $widgetWrapper.show();
166       else
167         $widgetWrapper.hide();
168     };
169
170     this.render = render;
171     this.testInputs = testInputs;
172   };
173
174   this.showHistoryWindow = function(id) {
175     var xPos = (screen.width - 800) / 2;
176     var yPos = (screen.height - 500) / 2;
177     var parm = "left="+ xPos +",top="+ yPos +",width=800,height=500,status=yes,scrollbars=yes";
178     var url = "common.pl?INPUT_ENCODING=UTF-8&action=show_history&longdescription=&input_name="+ encodeURIComponent(id);
179     window.open(url, "_new_generic", parm);
180   };
181
182   this.update_dial_action = function($input) {
183     var $action = $('#' + $input.prop('id') + '-dial-action');
184
185     if (!$action)
186       return true;
187
188     var number = $input.val().replace(/\s+/g, '');
189     if (number === '')
190       $action.hide();
191     else
192       $action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
193
194     return true;
195   };
196
197   this.init_dial_action = function(input) {
198     if ($('#_cti_enabled').val() != 1)
199       return false;
200
201     var $input    = $(input);
202     var action_id = $input.prop('id') + '-dial-action';
203
204     if (!$('#' + action_id).size()) {
205       var $action = $('<a href="" id="' + action_id + '" class="cti_call_action" target="_blank" tabindex="-1"></a>');
206       $input.wrap('<span nobr></span>').after($action);
207
208       $input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
209     }
210
211     kivi.CustomerVendor.update_dial_action($input);
212
213     return true;
214   };
215
216   this.inline_report = function(target, source, data){
217 //    alert("HALLO S " + source + " --T " + target + " tt D " + data);
218     $.ajax({
219       url:        source,
220       success:    function (rsp) {
221         $(target).html(rsp);
222         $(target).find('.paginate').find('a').click(function(event){ ns.redirect_event(event, target) });
223         $(target).find('a.report-generator-header-link').click(function(event){ ns.redirect_event(event, target) });
224       },
225       data:       data,
226     });
227   };
228   this.redirect_event = function(event, target){
229     event.preventDefault();
230     ns.inline_report(target, event.target + '', {});
231   };
232
233   var KEY = {
234     TAB:       9,
235     ENTER:     13,
236     SHIFT:     16,
237     CTRL:      17,
238     ALT:       18,
239     ESCAPE:    27,
240     PAGE_UP:   33,
241     PAGE_DOWN: 34,
242     LEFT:      37,
243     UP:        38,
244     RIGHT:     39,
245     DOWN:      40,
246   };
247
248   ns.Picker = function($real, options) {
249     var self = this;
250     this.o = $.extend(true, {
251       limit: 20,
252       delay: 50,
253       action: {
254         commit_none: function(){ },
255         commit_one:  function(){ $('#update_button').click(); },
256         commit_many: function(){ }
257       }
258     }, $real.data('customer-vendor-picker-data'), options);
259     this.$real              = $real;
260     this.real_id            = $real.attr('id');
261     this.last_real          = $real.val();
262     this.$dummy             = $($real.siblings()[0]);
263     this.autocomplete_open  = false;
264     this.state              = this.STATES.PICKED;
265     this.last_dummy         = this.$dummy.val();
266     this.timer              = undefined;
267
268     this.init();
269   };
270
271   ns.Picker.prototype = {
272     CLASSES: {
273       PICKED:       'customer-vendor-picker-picked',
274       UNDEFINED:    'customer-vendor-picker-undefined',
275     },
276     ajax_data: function(term) {
277       return {
278         'filter.all:substr:multi::ilike': term,
279         'filter.obsolete': 0,
280         current:  this.$real.val(),
281         type:     this.o.cv_type,
282       };
283     },
284     set_item: function(item) {
285       var self = this;
286       if (item.id) {
287         this.$real.val(item.id);
288         // autocomplete ui has name, use the value for ajax items, which contains displayable_name
289         this.$dummy.val(item.name ? item.name : item.value);
290       } else {
291         this.$real.val('');
292         this.$dummy.val('');
293       }
294       this.state      = this.STATES.PICKED;
295       this.last_real  = this.$real.val();
296       this.last_dummy = this.$dummy.val();
297       this.$real.trigger('change');
298
299       if (this.o.fat_set_item && item.id) {
300         $.ajax({
301           url: 'controller.pl?action=CustomerVendor/show.json',
302           data: { 'id': item.id, 'db': item.type },
303           success: function(rsp) {
304             self.$real.trigger('set_item:CustomerVendorPicker', rsp);
305           },
306         });
307       } else {
308         this.$real.trigger('set_item:CustomerVendorPicker', item);
309       }
310       this.annotate_state();
311     },
312     set_multi_items: function(data) {
313       this.run_action(this.o.action.set_multi_items, [ data ]);
314     },
315     make_defined_state: function() {
316       if (this.state == this.STATES.PICKED) {
317         this.annotate_state();
318         return true
319       } else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
320         this.set_item({})
321       else {
322         this.set_item({ id: this.last_real, name: this.last_dummy })
323       }
324       this.annotate_state();
325     },
326     annotate_state: function() {
327       if (this.state == this.STATES.PICKED)
328         this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
329       else if (this.state == this.STATES.UNDEFINED && this.$dummy.val() === '')
330         this.$dummy.removeClass(this.STATES.UNDEFINED).addClass(this.STATES.PICKED);
331       else {
332         this.$dummy.addClass(this.STATES.UNDEFINED).removeClass(this.STATES.PICKED);
333       }
334     },
335     handle_changed_text: function(callbacks) {
336       var self = this;
337       $.ajax({
338         url: 'controller.pl?action=CustomerVendor/ajaj_autocomplete',
339         dataType: "json",
340         data: $.extend( self.ajax_data(self.$dummy.val()), { prefer_exact: 1 } ),
341         success: function (data) {
342           if (data.length == 1) {
343             self.set_item(data[0]);
344             if (callbacks && callbacks.match_one) self.run_action(callbacks.match_one, [ data[0] ]);
345           } else if (data.length > 1) {
346             self.state = self.STATES.UNDEFINED;
347             if (callbacks && callbacks.match_many) self.run_action(callbacks.match_many, [ data ]);
348           } else {
349             self.state = self.STATES.UNDEFINED;
350             if (callbacks && callbacks.match_none) self.run_action(callbacks.match_none, [ self, self.$dummy.val() ]);
351           }
352           self.annotate_state();
353         }
354       });
355     },
356     handle_keydown: function(event) {
357       var self = this;
358       if (event.which == KEY.ENTER || event.which == KEY.TAB) {
359         // if string is empty assume they want to delete
360         if (self.$dummy.val() === '') {
361           self.set_item({});
362           return true;
363         } else if (self.state == self.STATES.PICKED) {
364           if (self.o.action.commit_one) {
365             self.run_action(self.o.action.commit_one);
366           }
367           return true;
368         }
369         if (event.which == KEY.TAB) {
370           event.preventDefault();
371           self.handle_changed_text();
372         }
373         if (event.which == KEY.ENTER) {
374           event.preventDefault();
375           self.handle_changed_text({
376             match_none: self.o.action.commit_none,
377             match_one:  self.o.action.commit_one,
378             match_many: self.o.action.commit_many
379           });
380           return false;
381         }
382       } else if (event.which == KEY.DOWN && !self.autocomplete_open) {
383         var old_options = self.$dummy.autocomplete('option');
384         self.$dummy.autocomplete('option', 'minLength', 0);
385         self.$dummy.autocomplete('search', self.$dummy.val());
386         self.$dummy.autocomplete('option', 'minLength', old_options.minLength);
387       } else if ((event.which != KEY.SHIFT) && (event.which != KEY.CTRL) && (event.which != KEY.ALT)) {
388         self.state = self.STATES.UNDEFINED;
389       }
390     },
391     init: function() {
392       var self = this;
393       this.$dummy.autocomplete({
394         source: function(req, rsp) {
395           $.ajax($.extend({}, self.o, {
396             url:      'controller.pl?action=CustomerVendor/ajaj_autocomplete',
397             dataType: "json",
398             type:     'get',
399             data:     self.ajax_data(req.term),
400             success:  function (data){ rsp(data) }
401           }));
402         },
403         select: function(event, ui) {
404           self.set_item(ui.item);
405           if (self.o.action.commit_one) {
406             self.run_action(self.o.action.commit_one);
407           }
408         },
409         search: function(event, ui) {
410           if ((event.which == KEY.SHIFT) || (event.which == KEY.CTRL) || (event.which == KEY.ALT))
411             event.preventDefault();
412         },
413         open: function() {
414           self.autocomplete_open = true;
415         },
416         close: function() {
417           self.autocomplete_open = false;
418         }
419       });
420       this.$dummy.keydown(function(event){ self.handle_keydown(event) });
421       this.$dummy.on('paste', function(){
422         setTimeout(function() {
423           self.handle_changed_text();
424         }, 1);
425       });
426       this.$dummy.blur(function(){
427         window.clearTimeout(self.timer);
428         self.timer = window.setTimeout(function() { self.annotate_state() }, 100);
429       });
430     },
431     run_action: function(code, args) {
432       if (typeof code === 'function')
433         code.apply(this, args)
434       else
435         kivi.run(code, args);
436     },
437     clear: function() {
438       this.set_item({});
439     }
440   };
441   ns.Picker.prototype.STATES = {
442     PICKED:    ns.Picker.prototype.CLASSES.PICKED,
443     UNDEFINED: ns.Picker.prototype.CLASSES.UNDEFINED
444   };
445
446   ns.reinit_widgets = function() {
447     kivi.run_once_for('input.customer_vendor_autocomplete', 'customer_vendor_picker', function(elt) {
448       if (!$(elt).data('customer_vendor_picker'))
449         $(elt).data('customer_vendor_picker', new kivi.CustomerVendor.Picker($(elt)));
450     });
451
452     $('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
453       kivi.CustomerVendor.init_dial_action($(elt));
454     });
455   }
456
457   ns.init = function() {
458     ns.reinit_widgets();
459   }
460
461   ns.get_price_report = function(target, source, data) {
462     $.ajax({
463       url:        source,
464       success:    function (rsp) {
465         $(target).html(rsp);
466         $(target).find('a.report-generator-header-link').click(function(event){ ns.price_report_redirect_event(event, target) });
467       },
468     });
469   };
470
471   ns.price_report_redirect_event = function (event, target) {
472     event.preventDefault();
473     ns.get_price_report(target, event.target + '');
474   };
475
476   ns.price_list_init = function () {
477     $("#customer_vendor_tabs").on('tabsbeforeactivate', function(event, ui){
478       if (ui.newPanel.attr('id') == 'price_list') {
479         ns.get_price_report('#price_list', "controller.pl?action=CustomerVendor/ajax_list_prices&id=" + $('#cv_id').val() + "&db=" + $('#db').val() + "&callback=" + $('#callback').val());
480       }
481       return 1;
482     });
483
484     $("#customer_vendor_tabs").on('tabscreate', function(event, ui){
485       if (ui.panel.attr('id') == 'price_list') {
486         ns.get_price_report('#price_list', "controller.pl?action=CustomerVendor/ajax_list_prices&id=" + $('#cv_id').val() + "&db=" + $('#db').val() + "&callback=" + $('#callback').val());
487       }
488       return 1;
489     });
490   }
491
492   $(function(){
493     ns.init();
494     ns.price_list_init();
495   });
496 });