4b4aa3f50975c4c866a4287a8c82e00efce17956
[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.setCustomVariablesFromAJAJ = function(cvars) {
45     for (var key in cvars) {
46       var cvar  = cvars[key];
47       var $ctrl = $('#contact_cvars_'+ key);
48
49       console.log($ctrl, cvar);
50
51       if (cvar.type == 'bool')
52         $ctrl.prop('checked', cvar.value == 1 ? 'checked' : '');
53
54       else if ((cvar.type == 'customer') || (cvar.type == 'vendor'))
55         kivi.CustomerVendorPicker($ctrl).set_item({ id: cvar.id, name: cvar.value });
56
57       else if (cvar.type == 'part')
58         kivi.PartPicker($ctrl).set_item({ id: cvar.id, name: cvar.value });
59
60       else
61         $ctrl.val(cvar.value);
62     }
63   };
64
65   this.selectContact = function(params) {
66     var contactId = $('#contact_cp_id').val();
67
68           var url = 'controller.pl?action=CustomerVendor/ajaj_get_contact&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&contact_id='+ contactId;
69
70     $.getJSON(url, function(data) {
71       var contact = data.contact;
72       for(var key in contact)
73         $(document.getElementById('contact_'+ key)).val(contact[key])
74
75       kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.contact_cvars);
76
77       if ( contactId )
78         $('#action_delete_contact').show();
79       else
80         $('#action_delete_contact').hide();
81
82       if ( params.onFormSet )
83         params.onFormSet();
84     });
85
86     $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
87   };
88
89   var mapSearchStmts = [
90     '#street',
91     ', ',
92     '#zipcode',
93     ' ',
94     '#city',
95     ', ',
96     '#country'
97   ];
98
99   this.MapWidget = function(prefix, source_address)
100   {
101     var $mapSearchElements = [];
102     var $widgetWrapper;
103
104     var init = function() {
105       if( $mapSearchElements.length > 0 )
106         return;
107
108       for(var i in mapSearchStmts) {
109         var stmt = mapSearchStmts[i];
110         if( stmt.charAt(0) == '#' ) {
111           var $elem = $('#'+ prefix + stmt.substring(1));
112           if( $elem )
113             $mapSearchElements.push($elem);
114         }
115       }
116     };
117
118     var isNotEmpty = function() {
119       for(var i in $mapSearchElements)
120         if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() == '') )
121           return false;
122       return true;
123     };
124
125     var showMap = function() {
126       var searchString = "";
127
128       for(var i in mapSearchStmts) {
129         var stmt = mapSearchStmts[i];
130         if( stmt.charAt(0) == '#' ) {
131           var val = $('#'+ prefix + stmt.substring(1)).val();
132           if( val )
133             searchString += val;
134         }
135         else
136           searchString += stmt;
137       }
138
139       source_address = source_address || '';
140       var query      = source_address != '' ? 'saddr=' + encodeURIComponent(source_address) + '&daddr=' : 'q=';
141       var url        = 'https://maps.google.com/maps?' + query + encodeURIComponent(searchString);
142
143       window.open(url, '_blank');
144       window.focus();
145     };
146
147     var render = function(widgetWrapper) {
148       init();
149
150       $widgetWrapper = $(widgetWrapper);
151
152       $widgetWrapper
153         .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
154         .click(function() {
155           showMap();
156         });
157       for(var i in $mapSearchElements)
158         $mapSearchElements[i].keyup(function() {
159           testInputs();
160         });
161       this.testInputs();
162     };
163
164     var testInputs = function() {
165       init();
166
167       if( isNotEmpty() )
168         $widgetWrapper.show();
169       else
170         $widgetWrapper.hide();
171     };
172
173     this.render = render;
174     this.testInputs = testInputs;
175   };
176
177   this.showHistoryWindow = function(id) {
178     var xPos = (screen.width - 800) / 2;
179     var yPos = (screen.height - 500) / 2;
180     var parm = "left="+ xPos +",top="+ yPos +",width=800,height=500,status=yes,scrollbars=yes";
181     var url = "common.pl?INPUT_ENCODING=UTF-8&action=show_history&longdescription=&input_name="+ encodeURIComponent(id);
182     window.open(url, "_new_generic", parm);
183   };
184
185   this.update_dial_action = function($input) {
186     var $action = $('#' + $input.prop('id') + '-dial-action');
187
188     if (!$action)
189       return true;
190
191     var number = $input.val().replace(/\s+/g, '');
192     if (number == '')
193       $action.hide();
194     else
195       $action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
196
197     return true;
198   };
199
200   this.init_dial_action = function(input) {
201     if ($('#_cti_enabled').val() != 1)
202       return false;
203
204     var $input    = $(input);
205     var action_id = $input.prop('id') + '-dial-action';
206
207     if (!$('#' + action_id).size()) {
208       var $action = $('<a href="" id="' + action_id + '" class="cti_call_action" target="_blank" tabindex="-1"></a>');
209       $input.wrap('<span nobr></span>').after($action);
210
211       $input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
212     }
213
214     kivi.CustomerVendor.update_dial_action($input);
215
216     return true;
217   };
218
219   this.inline_report = function(target, source, data){
220     $.ajax({
221       url:        source,
222       success:    function (rsp) {
223         $(target).html(rsp);
224         $(target).find('.paginate').find('a').click(function(event){ ns.redirect_event(event, target) });
225         $(target).find('a.report-generator-header-link').click(function(event){ ns.redirect_event(event, target) });
226       },
227       data:       data,
228     });
229   };
230   this.redirect_event = function(event, target){
231     event.preventDefault();
232     ns.inline_report(target, event.target + '', {});
233   };
234 });
235
236 function local_reinit_widgets() {
237   $('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
238     kivi.CustomerVendor.init_dial_action($(elt));
239   });
240 }