[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/layouts/vlayout/modules/Products/resources/ -> Edit.js (source)

   1  /*+***********************************************************************************
   2   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   3   * ("License"); You may not use this file except in compliance with the License
   4   * The Original Code is:  vtiger CRM Open Source
   5   * The Initial Developer of the Original Code is vtiger.
   6   * Portions created by vtiger are Copyright (C) vtiger.
   7   * All Rights Reserved.
   8   *************************************************************************************/
   9  
  10  Vtiger_Edit_Js("Products_Edit_Js",{
  11      
  12  },{
  13      baseCurrency : '',
  14      
  15      baseCurrencyName : '',
  16      //Container which stores the multi currency element
  17      multiCurrencyContainer : false,
  18      
  19      //Container which stores unit price
  20      unitPrice : false,
  21      
  22      /**
  23       * Function to get unit price
  24       */
  25      getUnitPrice : function(){
  26          if(this.unitPrice == false) {
  27              this.unitPrice = jQuery('input.unitPrice',this.getForm());
  28          }
  29          return this.unitPrice;
  30      },
  31      
  32      /**
  33       * Function to get more currencies container
  34       */
  35      getMoreCurrenciesContainer : function(){
  36          if(this.multiCurrencyContainer == false) {
  37              this.multiCurrencyContainer = jQuery('.multiCurrencyEditUI');
  38          }
  39          return this.multiCurrencyContainer;
  40      },
  41      
  42      /**
  43       * Function which aligns data just below global search element
  44       */
  45      alignBelowUnitPrice : function(dataToAlign) {
  46          var parentElem = jQuery('input[name="unit_price"]',this.getForm());
  47          dataToAlign.position({
  48              'of' : parentElem,
  49              'my': "left top",
  50              'at': "left bottom",
  51              'collision' : 'flip'
  52          });
  53          return this;
  54      },
  55      
  56      /**
  57       * Function to get current Element
  58       */
  59      getCurrentElem : function(e){
  60          return jQuery(e.currentTarget);
  61      },
  62      /**
  63       *Function to register events for taxes
  64       */
  65      registerEventForTaxes : function(){
  66          var thisInstance = this;
  67          var formElem = this.getForm();
  68          jQuery('.taxes').on('change',function(e){
  69              var elem = thisInstance.getCurrentElem(e);
  70              var taxBox  = elem.data('taxName');
  71              if(elem.is(':checked')) {
  72                  jQuery('input[name='+taxBox+']',formElem).removeClass('hide').show();
  73              }else{
  74                  jQuery('input[name='+taxBox+']',formElem).addClass('hide');
  75              }
  76  
  77          });
  78          return this;
  79      },
  80      
  81      /**
  82       * Function to register event for enabling base currency on radio button clicked
  83       */
  84      registerEventForEnableBaseCurrency : function(){
  85          var container = this.getMoreCurrenciesContainer();
  86          var thisInstance = this;
  87          jQuery(container).on('change','.baseCurrency',function(e){
  88              var elem = thisInstance.getCurrentElem(e);
  89              var parentElem = elem.closest('tr');
  90              if(elem.is(':checked')) {
  91                  var convertedPrice = jQuery('.convertedPrice',parentElem).val();
  92                  thisInstance.baseCurrencyName = parentElem.data('currencyId');
  93                  thisInstance.baseCurrency = convertedPrice;
  94              }
  95          });
  96          return this;
  97      },
  98      
  99      /**
 100       * Function to register event for reseting the currencies
 101       */
 102      registerEventForResetCurrency : function(){
 103          var container = this.getMoreCurrenciesContainer();
 104          var thisInstance = this;
 105          jQuery(container).on('click','.currencyReset',function(e){
 106              var parentElem = thisInstance.getCurrentElem(e).closest('tr');
 107              var unitPriceFieldData = thisInstance.getUnitPrice().data();
 108              var unitPrice = thisInstance.getDataBaseFormatUnitPrice();
 109              var conversionRate = jQuery('.conversionRate',parentElem).val();
 110              var price = parseFloat(unitPrice) * parseFloat(conversionRate);
 111              var userPreferredDecimalPlaces = unitPriceFieldData.numberOfDecimalPlaces;
 112              price = price.toFixed(userPreferredDecimalPlaces);
 113              var calculatedPrice = price.toString().replace('.',unitPriceFieldData.decimalSeperator);
 114              jQuery('.convertedPrice',parentElem).val(calculatedPrice);
 115          });
 116          return this;
 117      },
 118      
 119      /**
 120       *  Function to return stripped unit price
 121       */
 122          getDataBaseFormatUnitPrice : function(){
 123              var field = this.getUnitPrice();
 124              var unitPrice = field.val();
 125              if(unitPrice == ''){
 126                  unitPrice = 0;
 127              }else{
 128                  var fieldData = field.data();
 129                  //As replace is doing replace of single occurence and using regex 
 130                  //replace has a problem with meta characters  like (.,$),so using split and join
 131                  var strippedValue = unitPrice.split(fieldData.groupSeperator);
 132                  strippedValue = strippedValue.join("");
 133                  strippedValue = strippedValue.replace(fieldData.decimalSeperator, '.');
 134                  unitPrice = strippedValue;
 135              }
 136              return unitPrice;
 137          },
 138          
 139      calculateConversionRate : function() {
 140          var container = this.getMoreCurrenciesContainer();
 141          var baseCurrencyRow = container.find('.baseCurrency').filter(':checked').closest('tr');
 142          var baseCurrencyConvestationRate = baseCurrencyRow.find('.conversionRate');
 143          //if basecurrency has conversation rate as 1 then you dont have caliculate conversation rate
 144          if(baseCurrencyConvestationRate.val() == "1") {
 145              return;
 146          }
 147          var baseCurrencyRatePrevValue = baseCurrencyConvestationRate.val();
 148          
 149          container.find('.conversionRate').each(function(key,domElement) {
 150              var element = jQuery(domElement);
 151              if(!element.is(baseCurrencyConvestationRate)){
 152                  var prevValue = element.val();
 153                  element.val((prevValue/baseCurrencyRatePrevValue));
 154              }
 155          });
 156          baseCurrencyConvestationRate.val("1");
 157      },
 158      /**
 159       * Function to register event for enabling currency on checkbox checked
 160       */
 161      
 162      registerEventForEnableCurrency : function(){
 163          var container = this.getMoreCurrenciesContainer();
 164          var thisInstance = this;
 165          jQuery(container).on('change','.enableCurrency',function(e){
 166              var elem = thisInstance.getCurrentElem(e);
 167              var parentRow = elem.closest('tr');
 168              
 169              if(elem.is(':checked')) {
 170                  elem.attr('checked',"checked");
 171                  var conversionRate = jQuery('.conversionRate',parentRow).val();
 172                  var unitPriceFieldData = thisInstance.getUnitPrice().data();
 173                  var unitPrice = thisInstance.getDataBaseFormatUnitPrice();
 174                  var price = parseFloat(unitPrice)*parseFloat(conversionRate);
 175                  jQuery('input',parentRow).attr('disabled', true).removeAttr('disabled');
 176                  jQuery('button.currencyReset', parentRow).attr('disabled', true).removeAttr('disabled');
 177                  var userPreferredDecimalPlaces = unitPriceFieldData.numberOfDecimalPlaces;
 178                  price = price.toFixed(userPreferredDecimalPlaces);
 179                  var calculatedPrice = price.toString().replace('.',unitPriceFieldData.decimalSeperator);
 180                  jQuery('input.convertedPrice',parentRow).val(calculatedPrice)
 181              }else{
 182                  jQuery('input',parentRow).attr('disabled', true);
 183                  jQuery('input.enableCurrency',parentRow).removeAttr('disabled');
 184                  jQuery('button.currencyReset', parentRow).attr('disabled', 'disabled');
 185                  var baseCurrency = jQuery('.baseCurrency', parentRow);
 186                  if(baseCurrency.is(':checked')){
 187                      baseCurrency.removeAttr('checked');
 188                  }
 189              }
 190          })
 191          return this;
 192      },
 193      
 194      /**
 195       * Function to get more currencies UI
 196       */
 197      getMoreCurrenciesUI : function(){
 198          var aDeferred = jQuery.Deferred();
 199          var moduleName = app.getModuleName();
 200          var baseCurrency = jQuery('input[name="base_currency"]').val();
 201          var recordId = jQuery('input[name="record"]').val();
 202          var moreCurrenciesContainer = jQuery('#moreCurrenciesContainer');
 203          moreCurrenciesUi = moreCurrenciesContainer.find('.multiCurrencyEditUI');
 204          var moreCurrenciesUi;
 205              
 206          if(moreCurrenciesUi.length == 0){
 207              var moreCurrenciesParams = {
 208                  'module' : moduleName,
 209                  'view' : "MoreCurrenciesList",
 210                  'currency' : baseCurrency,
 211                  'record' : recordId
 212              }
 213  
 214              AppConnector.request(moreCurrenciesParams).then(
 215                  function(data){
 216                      moreCurrenciesContainer.html(data);
 217                      aDeferred.resolve(data);
 218                  },
 219                  function(textStatus, errorThrown){
 220                      aDeferred.reject(textStatus, errorThrown);
 221                  }
 222              );
 223          } else{
 224              aDeferred.resolve();
 225          }
 226          return aDeferred.promise();
 227      },
 228      
 229      /*
 230       * function to register events for more currencies link
 231       */
 232      registerEventForMoreCurrencies : function(){
 233          var thisInstance = this;
 234          var form = this.getForm();
 235          jQuery('#moreCurrencies').on('click',function(e){
 236              var progressInstance = jQuery.progressIndicator();
 237              thisInstance.getMoreCurrenciesUI().then(function(data){
 238                  var moreCurrenciesUi;
 239                  moreCurrenciesUi = jQuery('#moreCurrenciesContainer').find('.multiCurrencyEditUI');
 240                  if(moreCurrenciesUi.length > 0){
 241                      moreCurrenciesUi = moreCurrenciesUi.clone(true,true);
 242                      progressInstance.hide();
 243                      var css = {'text-align' : 'left','width':'65%'};
 244                      var callback = function(data){
 245                          var params = app.validationEngineOptions;
 246                          var form = data.find('#currencyContainer');
 247                          params.onValidationComplete = function(form, valid){
 248                              if(valid) {
 249                                  thisInstance.saveCurrencies();
 250                              }
 251                              return false;
 252                          }
 253                          form.validationEngine(params);
 254                          app.showScrollBar(data.find('.currencyContent'), {'height':'400px'});
 255                          thisInstance.baseCurrency = thisInstance.getUnitPrice().val();
 256                          var multiCurrencyEditUI = jQuery('.multiCurrencyEditUI');
 257                          thisInstance.multiCurrencyContainer = multiCurrencyEditUI;
 258                          thisInstance.calculateConversionRate();
 259                          thisInstance.registerEventForEnableCurrency().registerEventForEnableBaseCurrency()
 260                                              .registerEventForResetCurrency().triggerForBaseCurrencyCalc();
 261                      }
 262                      var moreCurrenciesContainer = jQuery('#moreCurrenciesContainer').find('.multiCurrencyEditUI');
 263                      var contentInsideForm = moreCurrenciesUi.find('.multiCurrencyContainer').html();
 264                      moreCurrenciesUi.find('.multiCurrencyContainer').remove();
 265                      var form = '<form id="currencyContainer"></form>'
 266                      jQuery(form).insertAfter(moreCurrenciesUi.find('.modal-header'));
 267                      moreCurrenciesUi.find('form').html(contentInsideForm);
 268                      moreCurrenciesContainer.find('input[name^=curname]').each(function(index,element){
 269                          var dataValue = jQuery(element).val();
 270                          var dataId = jQuery(element).attr('id');
 271                          moreCurrenciesUi.find('#'+dataId).val(dataValue);
 272                      });
 273  
 274                      var modalWindowParams = {
 275                          data : moreCurrenciesUi,
 276                          css : css,
 277                          cb : callback
 278                      }
 279                      app.showModalWindow(modalWindowParams)
 280                  }
 281              })
 282          });
 283      },
 284      /**
 285       * Function to calculate base currency price value if unit
 286       * present on click of more currencies
 287       */
 288      triggerForBaseCurrencyCalc : function(){
 289          var multiCurrencyEditUI = this.getMoreCurrenciesContainer();
 290          var baseCurrency = multiCurrencyEditUI.find('.enableCurrency');
 291          jQuery.each(baseCurrency,function(key,val){
 292              if(jQuery(val).is(':checked')){
 293                  var baseCurrencyRow = jQuery(val).closest('tr');
 294                  if(parseFloat(baseCurrencyRow.find('.convertedPrice').val()) == 0.00) {
 295                      baseCurrencyRow.find('.currencyReset').trigger('click');
 296                  }
 297              } else {
 298                  var baseCurrencyRow = jQuery(val).closest('tr');
 299                  baseCurrencyRow.find('.convertedPrice').val('');
 300              }
 301          })
 302      },
 303      
 304      /**
 305       * Function to register onchange event for unit price
 306       */
 307      registerEventForUnitPrice : function(){
 308          var thisInstance = this;
 309          var unitPrice = this.getUnitPrice();
 310          unitPrice.on('change',function(){
 311              thisInstance.triggerForBaseCurrencyCalc();
 312          })
 313      },
 314  
 315      registerRecordPreSaveEvent : function(form) {
 316          var thisInstance = this;
 317          if(typeof form == 'undefined') {
 318              form = this.getForm();
 319          }
 320  
 321          form.on(Vtiger_Edit_Js.recordPreSave, function(e, data) {
 322              var multiCurrencyContent = jQuery('#moreCurrenciesContainer').find('.currencyContent');
 323              var unitPrice = thisInstance.getUnitPrice();
 324              if((multiCurrencyContent.length < 1) && (unitPrice.length > 0)){
 325                  e.preventDefault();
 326                  thisInstance.getMoreCurrenciesUI().then(function(data){
 327                      thisInstance.preSaveConfigOfForm(form);
 328                      form.submit();
 329                  })
 330              }else if(multiCurrencyContent.length > 0){
 331                  thisInstance.preSaveConfigOfForm(form);
 332              }
 333          })
 334      },
 335      
 336      /**
 337       * Function to handle settings before save of record
 338       */
 339      preSaveConfigOfForm : function(form) {
 340          var unitPrice = this.getUnitPrice();
 341          if(unitPrice.length > 0){
 342              var unitPriceValue = unitPrice.val();
 343              var baseCurrencyName = form.find('[name="base_currency"]').val();
 344              form.find('[name="'+ baseCurrencyName +'"]').val(unitPriceValue);
 345              form.find('#requstedUnitPrice').attr('name',baseCurrencyName).val(unitPriceValue);
 346          }
 347      },
 348      
 349      saveCurrencies : function(){
 350          var thisInstance = this;
 351          var errorMessage,params;
 352          var form = jQuery('#currencyContainer');
 353          var editViewForm = thisInstance.getForm();
 354          var modalContainer = jQuery('#globalmodal');
 355          var enabledBaseCurrency = modalContainer.find('.enableCurrency').filter(':checked');
 356          if(enabledBaseCurrency.length < 1){
 357              errorMessage = app.vtranslate('JS_PLEASE_SELECT_BASE_CURRENCY_FOR_PRODUCT');
 358              params = {
 359                  text: errorMessage,
 360                  'type':'error'
 361              };
 362              Vtiger_Helper_Js.showMessage(params);
 363              form.removeData('submit');
 364              return;
 365          }
 366          enabledBaseCurrency.attr('checked',"checked");
 367          modalContainer.find('.enableCurrency').filter(":not(:checked)").removeAttr('checked');
 368          var selectedBaseCurrency = modalContainer.find('.baseCurrency').filter(':checked');
 369          if(selectedBaseCurrency.length < 1){
 370              errorMessage = app.vtranslate('JS_PLEASE_ENABLE_BASE_CURRENCY_FOR_PRODUCT');
 371              params = {
 372                  text: errorMessage,
 373                  'type':'error'
 374              };
 375              Vtiger_Helper_Js.showMessage(params);
 376              form.removeData('submit');
 377              return;
 378          }
 379          selectedBaseCurrency.attr('checked',"checked");
 380          modalContainer.find('.baseCurrency').filter(":not(:checked)").removeAttr('checked');
 381          var parentElem = selectedBaseCurrency.closest('tr');
 382          var convertedPrice = jQuery('.convertedPrice',parentElem).val();
 383          thisInstance.baseCurrencyName = parentElem.data('currencyId');
 384          thisInstance.baseCurrency = convertedPrice;
 385          
 386          thisInstance.getUnitPrice().val(thisInstance.baseCurrency);
 387          jQuery('input[name="base_currency"]',editViewForm).val(thisInstance.baseCurrencyName);
 388          
 389          var savedValuesOfMultiCurrency = modalContainer.find('.currencyContent').html();
 390          var moreCurrenciesContainer = jQuery('#moreCurrenciesContainer');
 391          moreCurrenciesContainer.find('.currencyContent').html(savedValuesOfMultiCurrency);
 392          modalContainer.find('input[name^=curname]').each(function(index,element){
 393              var dataValue = jQuery(element).val();
 394              var dataId = jQuery(element).attr('id');
 395              moreCurrenciesContainer.find('.currencyContent').find('#'+dataId).val(dataValue);
 396          });
 397          app.hideModalWindow();
 398      },
 399      
 400      registerSubmitEvent: function() {
 401          var editViewForm = this.getForm();
 402  
 403          editViewForm.submit(function(e){
 404              if((editViewForm.find('[name="existingImages"]').length >= 1) || (editViewForm.find('[name="imagename[]"]').length > 1)){
 405                  jQuery.fn.MultiFile.disableEmpty(); // before submiting the form - See more at: http://www.fyneworks.com/jquery/multiple-file-upload/#sthash.UTGHmNv3.dpuf
 406              }
 407              //Form should submit only once for multiple clicks also
 408              if(typeof editViewForm.data('submit') != "undefined") {
 409                  return false;
 410              } else {
 411                  var module = jQuery(e.currentTarget).find('[name="module"]').val();
 412                  if(editViewForm.validationEngine('validate')) {
 413                      //Once the form is submiting add data attribute to that form element
 414                      editViewForm.data('submit', 'true');
 415                          //on submit form trigger the recordPreSave event
 416                          var recordPreSaveEvent = jQuery.Event(Vtiger_Edit_Js.recordPreSave);
 417                          editViewForm.trigger(recordPreSaveEvent, {'value' : 'edit'});
 418                          if(recordPreSaveEvent.isDefaultPrevented()) {
 419                              //If duplicate record validation fails, form should submit again
 420                              editViewForm.removeData('submit');
 421                              e.preventDefault();
 422                          }
 423                  } else {
 424                      //If validation fails, form should submit again
 425                      editViewForm.removeData('submit');
 426                      // to avoid hiding of error message under the fixed nav bar
 427                      app.formAlignmentAfterValidation(editViewForm);
 428                  }
 429              }
 430          });
 431      },
 432      
 433      registerEvents : function(){
 434          this._super();
 435          this.registerEventForMoreCurrencies();
 436          this.registerEventForTaxes();
 437          this.registerEventForUnitPrice();
 438          this.registerRecordPreSaveEvent();
 439      }
 440  })


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1