bfef730d4f793a01912005e8bfa525e48a64a439
[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
6     if( shiptoId ) {
7       var url = 'controller.pl?action=CustomerVendor/ajaj_get_shipto&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ shiptoId;
8
9       $.getJSON(url, function(data) {
10         for(var key in data)
11           $(document.getElementById('shipto_'+ key)).val(data[key]);
12
13         $('#action_delete_shipto').show();
14
15         if( params.onFormSet )
16           params.onFormSet();
17       });
18     }
19     else {
20       $('#shipto :input').not(':button, :submit, :reset, :hidden').val('');
21
22       $('#action_delete_shipto').hide();
23
24       if( params.onFormSet )
25         params.onFormSet();
26     }
27   };
28
29   this.selectDelivery = function(fromDate, toDate) {
30     var deliveryId = $('#delivery_id').val();
31
32     if( !deliveryId )
33       $("#delivery").empty();
34     else {
35       var url = 'controller.pl?action=CustomerVendor/get_delivery&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ $('#delivery_id').val();
36
37       if( fromDate && toDate )
38         url += '&delivery_from='+ fromDate +'&delivery_to='+ toDate;
39
40       $('#delivery').load(url);
41     }
42   };
43
44   this.selectContact = function(params) {
45     var contactId = $('#contact_cp_id').val();
46
47           var url = 'controller.pl?action=CustomerVendor/ajaj_get_contact&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&contact_id='+ contactId;
48
49     $.getJSON(url, function(data) {
50       var contact = data.contact;
51       for(var key in contact)
52         $(document.getElementById('contact_'+ key)).val(contact[key])
53
54       var cvars = data.contact_cvars;
55       for(var key in cvars)
56         $(document.getElementById('contact_cvars_'+ key)).val(cvars[key]);
57
58       if ( contactId )
59         $('#action_delete_contact').show();
60       else
61         $('#action_delete_contact').hide();
62
63       if ( params.onFormSet )
64         params.onFormSet();
65     });
66
67     $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
68   };
69
70   var mapSearchStmts = [
71     '#street',
72     ', ',
73     '#zipcode',
74     ' ',
75     '#city',
76     ', ',
77     '#country'
78   ];
79
80   this.MapWidget = function(prefix)
81   {
82     var $mapSearchElements = [];
83     var $widgetWrapper;
84
85     var init = function() {
86       if( $mapSearchElements.length > 0 )
87         return;
88
89       for(var i in mapSearchStmts) {
90         var stmt = mapSearchStmts[i];
91         if( stmt.charAt(0) == '#' ) {
92           var $elem = $('#'+ prefix + stmt.substring(1));
93           if( $elem )
94             $mapSearchElements.push($elem);
95         }
96       }
97     };
98
99     var isNotEmpty = function() {
100       for(var i in $mapSearchElements)
101         if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() == '') )
102           return false;
103       return true;
104     };
105
106     var showMap = function() {
107       var searchString = "";
108
109       for(var i in mapSearchStmts) {
110         var stmt = mapSearchStmts[i];
111         if( stmt.charAt(0) == '#' ) {
112           var val = $('#'+ prefix + stmt.substring(1)).val();
113           if( val )
114             searchString += val;
115         }
116         else
117           searchString += stmt;
118       }
119
120       var url = 'https://maps.google.com/maps?q='+ encodeURIComponent(searchString);
121
122       window.open(url, '_blank');
123       window.focus();
124     };
125
126     var render = function(widgetWrapper) {
127       init();
128
129       $widgetWrapper = $(widgetWrapper);
130
131       $widgetWrapper
132         .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
133         .click(function() {
134           showMap();
135         });
136       for(var i in $mapSearchElements)
137         $mapSearchElements[i].keyup(function() {
138           testInputs();
139         });
140       this.testInputs();
141     };
142
143     var testInputs = function() {
144       init();
145
146       if( isNotEmpty() )
147         $widgetWrapper.show();
148       else
149         $widgetWrapper.hide();
150     };
151
152     this.render = render;
153     this.testInputs = testInputs;
154   };
155
156   this.showHistoryWindow = function(id) {
157     var xPos = (screen.width - 800) / 2;
158     var yPos = (screen.height - 500) / 2;
159     var parm = "left="+ xPos +",top="+ yPos +",width=800,height=500,status=yes,scrollbars=yes";
160     var url = "common.pl?INPUT_ENCODING=UTF-8&action=show_history&longdescription=&input_name="+ encodeURIComponent(id);
161     window.open(url, "_new_generic", parm);
162   };
163
164   this.update_dial_action = function($input) {
165     var $action = $('#' + $input.prop('id') + '-dial-action');
166
167     if (!$action)
168       return true;
169
170     var number = $input.val().replace(/\s+/g, '');
171     if (number == '')
172       $action.hide();
173     else
174       $action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
175
176     return true;
177   };
178
179   this.init_dial_action = function(input) {
180     if ($('#_cti_enabled').val() != 1)
181       return false;
182
183     var $input    = $(input);
184     var action_id = $input.prop('id') + '-dial-action';
185
186     if (!$('#' + action_id).size()) {
187       var $action = $('<a href="" id="' + action_id + '" class="cti_call_action" target="_blank" tabindex="-1"></a>');
188       $input.wrap('<span nobr></span>').after($action);
189
190       $input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
191     }
192
193     kivi.CustomerVendor.update_dial_action($input);
194
195     return true;
196   };
197 });
198
199 function local_reinit_widgets() {
200   $('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
201     kivi.CustomerVendor.init_dial_action($(elt));
202   });
203 }