mebil
[kivitendo-erp.git] / js / kivi.CustomerVendor.js
index 371434c..d682067 100644 (file)
@@ -1,23 +1,24 @@
-namespace('kivi.CustomerVendor', function() {
+namespace('kivi.CustomerVendor', function(ns) {
 
-  this.selectShipto = function() {
+  this.selectShipto = function(params) {
     var shiptoId = $('#shipto_shipto_id').val();
+    var url      = 'controller.pl?action=CustomerVendor/ajaj_get_shipto&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ shiptoId;
 
-    if( shiptoId ) {
-      var url = 'controller.pl?action=CustomerVendor/ajaj_get_shipto&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&shipto_id='+ shiptoId;
+    $.getJSON(url, function(data) {
+      var shipto = data.shipto;
+      for(var key in shipto)
+        $('#shipto_'+ key).val(shipto[key])
 
-      $.getJSON(url, function(data) {
-        for(var key in data)
-          $(document.getElementById('shipto_'+ key)).val(data[key]);
+      kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.shipto_cvars, 'shipto_cvars_');
 
+      if ( shiptoId )
         $('#action_delete_shipto').show();
-      });
-    }
-    else {
-      $('#shipto :input').not(':button, :submit, :reset, :hidden').val('');
+      else
+        $('#action_delete_shipto').hide();
 
-      $('#action_delete_shipto').hide();
-    }
+      if ( params.onFormSet )
+        params.onFormSet();
+    });
   };
 
   this.selectDelivery = function(fromDate, toDate) {
@@ -35,62 +36,133 @@ namespace('kivi.CustomerVendor', function() {
     }
   };
 
-  this.selectContact = function() {
+  this.setCustomVariablesFromAJAJ = function(cvars, prefix) {
+    for (var key in cvars) {
+      var cvar  = cvars[key];
+      var $ctrl = $('#' + prefix + key);
+
+      if (cvar.type == 'bool')
+        $ctrl.prop('checked', cvar.value == 1 ? 'checked' : '');
+
+      else if ((cvar.type == 'customer') || (cvar.type == 'vendor'))
+        kivi.CustomerVendorPicker($ctrl).set_item({ id: cvar.id, name: cvar.value });
+
+      else if (cvar.type == 'part')
+        kivi.PartPicker($ctrl).set_item({ id: cvar.id, name: cvar.value });
+
+      else
+        $ctrl.val(cvar.value);
+    }
+  };
+
+  this.selectContact = function(params) {
     var contactId = $('#contact_cp_id').val();
 
-    if( contactId ) {
-      var url = 'controller.pl?action=CustomerVendor/ajaj_get_contact&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&contact_id='+ contactId;
+         var url = 'controller.pl?action=CustomerVendor/ajaj_get_contact&id='+ $('#cv_id').val() +'&db='+ $('#db').val() +'&contact_id='+ contactId;
 
-      $.getJSON(url, function(data) {
-        var contact = data.contact;
-        for(var key in contact)
-          $(document.getElementById('contact_'+ key)).val(contact[key]);
+    $.getJSON(url, function(data) {
+      var contact = data.contact;
+      for(var key in contact)
+        $('#contact_'+ key).val(contact[key])
 
-        var cvars = data.contact_cvars;
-        for(var key in cvars)
-          $(document.getElementById('contact_cvar_'+ key)).val(cvars[key]);
+      kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.contact_cvars, 'contact_cvars_');
 
+      if ( contactId )
         $('#action_delete_contact').show();
-      });
-    }
-    else {
-      $('#contacts :input').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
+      else
+        $('#action_delete_contact').hide();
 
-      $('#action_delete_contact').hide();
-    }
+      if ( params.onFormSet )
+        params.onFormSet();
+    });
 
     $('#contact_cp_title_select, #contact_cp_abteilung_select').val('');
   };
 
-
-  this.showMap = function(prefix) {
-    var searchStmts = [
-      '#street',
-      ', ',
-      '#zipcode',
-      ' ',
-      '#city',
-      ', ',
-      '#country'
-    ];
-
-    var searchString = "";
-
-    for(var i in searchStmts) {
-      var stmt = searchStmts[i];
-      if( stmt.charAt(0) == '#' ) {
-        var val = $('#'+ prefix + stmt.substring(1)).val();
-        if( val )
-          searchString += val;
+  var mapSearchStmts = [
+    '#street',
+    ', ',
+    '#zipcode',
+    ' ',
+    '#city',
+    ', ',
+    '#country'
+  ];
+
+  this.MapWidget = function(prefix, source_address)
+  {
+    var $mapSearchElements = [];
+    var $widgetWrapper;
+
+    var init = function() {
+      if( $mapSearchElements.length > 0 )
+        return;
+
+      for(var i in mapSearchStmts) {
+        var stmt = mapSearchStmts[i];
+        if( stmt.charAt(0) == '#' ) {
+          var $elem = $('#'+ prefix + stmt.substring(1));
+          if( $elem )
+            $mapSearchElements.push($elem);
+        }
       }
-      else
-        searchString += stmt;
-    }
+    };
+
+    var isNotEmpty = function() {
+      for(var i in $mapSearchElements)
+        if( ($mapSearchElements[i].attr('id') != prefix + 'country') && ($mapSearchElements[i].val() === '') )
+          return false;
+      return true;
+    };
+
+    var showMap = function() {
+      var searchString = "";
+
+      for(var i in mapSearchStmts) {
+        var stmt = mapSearchStmts[i];
+        if( stmt.charAt(0) == '#' ) {
+          var val = $('#'+ prefix + stmt.substring(1)).val();
+          if( val )
+            searchString += val;
+        }
+        else
+          searchString += stmt;
+      }
+
+      source_address = source_address || '';
+      var query      = source_address !== '' ? 'saddr=' + encodeURIComponent(source_address) + '&daddr=' : 'q=';
+      var url        = 'https://maps.google.com/maps?' + query + encodeURIComponent(searchString);
+
+      window.open(url, '_blank');
+      window.focus();
+    };
 
-    var url = 'https://maps.google.com/maps?q='+ encodeURIComponent(searchString);
+    var render = function(widgetWrapper) {
+      init();
 
-    window.open(url, '_blank');
-    window.focus();
+      $widgetWrapper = $(widgetWrapper);
+
+      $widgetWrapper
+        .html('<img src="image/map.png" alt="'+ kivi.t8("Map") +'" title="'+ kivi.t8("Map") +'" />')
+        .click(function() {
+          showMap();
+        });
+      for(var i in $mapSearchElements)
+        $mapSearchElements[i].keyup(testInputs);
+      this.testInputs();
+    };
+
+    var testInputs = function() {
+      init();
+
+      if( isNotEmpty() )
+        $widgetWrapper.show();
+      else
+        $widgetWrapper.hide();
+    };
+
+    this.render = render;
+    this.testInputs = testInputs;
   };
 
   this.showHistoryWindow = function(id) {
@@ -100,4 +172,60 @@ namespace('kivi.CustomerVendor', function() {
     var url = "common.pl?INPUT_ENCODING=UTF-8&action=show_history&longdescription=&input_name="+ encodeURIComponent(id);
     window.open(url, "_new_generic", parm);
   };
+
+  this.update_dial_action = function($input) {
+    var $action = $('#' + $input.prop('id') + '-dial-action');
+
+    if (!$action)
+      return true;
+
+    var number = $input.val().replace(/\s+/g, '');
+    if (number === '')
+      $action.hide();
+    else
+      $action.prop('href', 'controller.pl?action=CTI/call&number=' + encodeURIComponent(number)).show();
+
+    return true;
+  };
+
+  this.init_dial_action = function(input) {
+    if ($('#_cti_enabled').val() != 1)
+      return false;
+
+    var $input    = $(input);
+    var action_id = $input.prop('id') + '-dial-action';
+
+    if (!$('#' + action_id).size()) {
+      var $action = $('<a href="" id="' + action_id + '" class="cti_call_action" target="_blank" tabindex="-1"></a>');
+      $input.wrap('<span nobr></span>').after($action);
+
+      $input.change(function() { kivi.CustomerVendor.update_dial_action($input); });
+    }
+
+    kivi.CustomerVendor.update_dial_action($input);
+
+    return true;
+  };
+
+  this.inline_report = function(target, source, data){
+    $.ajax({
+      url:        source,
+      success:    function (rsp) {
+        $(target).html(rsp);
+        $(target).find('.paginate').find('a').click(function(event){ ns.redirect_event(event, target) });
+        $(target).find('a.report-generator-header-link').click(function(event){ ns.redirect_event(event, target) });
+      },
+      data:       data,
+    });
+  };
+  this.redirect_event = function(event, target){
+    event.preventDefault();
+    ns.inline_report(target, event.target + '', {});
+  };
 });
+
+function local_reinit_widgets() {
+  $('#cv_phone,#shipto_shiptophone,#contact_cp_phone1,#contact_cp_phone2,#contact_cp_mobile1,#contact_cp_mobile2').each(function(idx, elt) {
+    kivi.CustomerVendor.init_dial_action($(elt));
+  });
+}