[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Products/ -> Products.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  class Products extends CRMEntity {
  11      var $db, $log; // Used in class functions of CRMEntity
  12  
  13      var $table_name = 'vtiger_products';
  14      var $table_index= 'productid';
  15      var $column_fields = Array();
  16  
  17      /**
  18       * Mandatory table for supporting custom fields.
  19       */
  20      var $customFieldTable = Array('vtiger_productcf','productid');
  21  
  22      var $tab_name = Array('vtiger_crmentity','vtiger_products','vtiger_productcf');
  23  
  24      var $tab_name_index = Array('vtiger_crmentity'=>'crmid','vtiger_products'=>'productid','vtiger_productcf'=>'productid','vtiger_seproductsrel'=>'productid','vtiger_producttaxrel'=>'productid');
  25  
  26  
  27  
  28      // This is the list of vtiger_fields that are in the lists.
  29      var $list_fields = Array(
  30          'Product Name'=>Array('products'=>'productname'),
  31          'Part Number'=>Array('products'=>'productcode'),
  32          'Commission Rate'=>Array('products'=>'commissionrate'),
  33          'Qty/Unit'=>Array('products'=>'qty_per_unit'),
  34          'Unit Price'=>Array('products'=>'unit_price')
  35      );
  36      var $list_fields_name = Array(
  37          'Product Name'=>'productname',
  38          'Part Number'=>'productcode',
  39          'Commission Rate'=>'commissionrate',
  40          'Qty/Unit'=>'qty_per_unit',
  41          'Unit Price'=>'unit_price'
  42      );
  43  
  44      var $list_link_field= 'productname';
  45  
  46      var $search_fields = Array(
  47          'Product Name'=>Array('products'=>'productname'),
  48          'Part Number'=>Array('products'=>'productcode'),
  49          'Unit Price'=>Array('products'=>'unit_price')
  50      );
  51      var $search_fields_name = Array(
  52          'Product Name'=>'productname',
  53          'Part Number'=>'productcode',
  54          'Unit Price'=>'unit_price'
  55      );
  56  
  57      var $required_fields = Array(
  58              'productname'=>1
  59      );
  60  
  61      // Placeholder for sort fields - All the fields will be initialized for Sorting through initSortFields
  62      var $sortby_fields = Array();
  63      var $def_basicsearch_col = 'productname';
  64  
  65      //Added these variables which are used as default order by and sortorder in ListView
  66      var $default_order_by = 'productname';
  67      var $default_sort_order = 'ASC';
  68  
  69      // Used when enabling/disabling the mandatory fields for the module.
  70      // Refers to vtiger_field.fieldname values.
  71      var $mandatory_fields = Array('createdtime', 'modifiedtime', 'productname', 'assigned_user_id');
  72       // Josh added for importing and exporting -added in patch2
  73      var $unit_price;
  74  
  75      /**    Constructor which will set the column_fields in this object
  76       */
  77  	function Products() {
  78          $this->log =LoggerManager::getLogger('product');
  79          $this->log->debug("Entering Products() method ...");
  80          $this->db = PearDatabase::getInstance();
  81          $this->column_fields = getColumnFields('Products');
  82          $this->log->debug("Exiting Product method ...");
  83      }
  84  
  85  	function save_module($module)
  86      {
  87          //Inserting into product_taxrel table
  88          if($_REQUEST['ajxaction'] != 'DETAILVIEW' && $_REQUEST['action'] != 'MassEditSave' && $_REQUEST['action'] != 'ProcessDuplicates')
  89          {
  90              $this->insertTaxInformation('vtiger_producttaxrel', 'Products');
  91              $this->insertPriceInformation('vtiger_productcurrencyrel', 'Products');
  92          }
  93  
  94          // Update unit price value in vtiger_productcurrencyrel
  95          $this->updateUnitPrice();
  96          //Inserting into attachments
  97          $this->insertIntoAttachment($this->id,'Products');
  98  
  99      }
 100  
 101      /**    function to save the product tax information in vtiger_producttaxrel table
 102       *    @param string $tablename - vtiger_tablename to save the product tax relationship (producttaxrel)
 103       *    @param string $module     - current module name
 104       *    $return void
 105      */
 106  	function insertTaxInformation($tablename, $module)
 107      {
 108          global $adb, $log;
 109          $log->debug("Entering into insertTaxInformation($tablename, $module) method ...");
 110          $tax_details = getAllTaxes();
 111  
 112          $tax_per = '';
 113          //Save the Product - tax relationship if corresponding tax check box is enabled
 114          //Delete the existing tax if any
 115          if($this->mode == 'edit')
 116          {
 117              for($i=0;$i<count($tax_details);$i++)
 118              {
 119                  $taxid = getTaxId($tax_details[$i]['taxname']);
 120                  $sql = "delete from vtiger_producttaxrel where productid=? and taxid=?";
 121                  $adb->pquery($sql, array($this->id,$taxid));
 122              }
 123          }
 124          for($i=0;$i<count($tax_details);$i++)
 125          {
 126              $tax_name = $tax_details[$i]['taxname'];
 127              $tax_checkname = $tax_details[$i]['taxname']."_check";
 128              if($_REQUEST[$tax_checkname] == 'on' || $_REQUEST[$tax_checkname] == 1)
 129              {
 130                  $taxid = getTaxId($tax_name);
 131                  $tax_per = $_REQUEST[$tax_name];
 132                  if($tax_per == '')
 133                  {
 134                      $log->debug("Tax selected but value not given so default value will be saved.");
 135                      $tax_per = getTaxPercentage($tax_name);
 136                  }
 137  
 138                  $log->debug("Going to save the Product - $tax_name tax relationship");
 139  
 140                  $query = "insert into vtiger_producttaxrel values(?,?,?)";
 141                  $adb->pquery($query, array($this->id,$taxid,$tax_per));
 142              }
 143          }
 144  
 145          $log->debug("Exiting from insertTaxInformation($tablename, $module) method ...");
 146      }
 147  
 148      /**    function to save the product price information in vtiger_productcurrencyrel table
 149       *    @param string $tablename - vtiger_tablename to save the product currency relationship (productcurrencyrel)
 150       *    @param string $module     - current module name
 151       *    $return void
 152      */
 153  	function insertPriceInformation($tablename, $module)
 154      {
 155          global $adb, $log, $current_user;
 156          $log->debug("Entering into insertPriceInformation($tablename, $module) method ...");
 157          //removed the update of currency_id based on the logged in user's preference : fix 6490
 158  
 159          $currency_details = getAllCurrencies('all');
 160  
 161          //Delete the existing currency relationship if any
 162          if($this->mode == 'edit' && $_REQUEST['action'] !== 'MassEditSave')
 163          {
 164              for($i=0;$i<count($currency_details);$i++)
 165              {
 166                  $curid = $currency_details[$i]['curid'];
 167                  $sql = "delete from vtiger_productcurrencyrel where productid=? and currencyid=?";
 168                  $adb->pquery($sql, array($this->id,$curid));
 169              }
 170          }
 171  
 172          $product_base_conv_rate = getBaseConversionRateForProduct($this->id, $this->mode);
 173          $currencySet = 0;
 174          //Save the Product - Currency relationship if corresponding currency check box is enabled
 175          for($i=0;$i<count($currency_details);$i++)
 176          {
 177              $curid = $currency_details[$i]['curid'];
 178              $curname = $currency_details[$i]['currencylabel'];
 179              $cur_checkname = 'cur_' . $curid . '_check';
 180              $cur_valuename = 'curname' . $curid;
 181  
 182              $requestPrice = CurrencyField::convertToDBFormat($_REQUEST['unit_price'], null, true);
 183              $actualPrice = CurrencyField::convertToDBFormat($_REQUEST[$cur_valuename], null, true);
 184              if($_REQUEST[$cur_checkname] == 'on' || $_REQUEST[$cur_checkname] == 1)
 185              {
 186                  $conversion_rate = $currency_details[$i]['conversionrate'];
 187                  $actual_conversion_rate = $product_base_conv_rate * $conversion_rate;
 188                  $converted_price = $actual_conversion_rate * $requestPrice;
 189  
 190                  $log->debug("Going to save the Product - $curname currency relationship");
 191  
 192                  $query = "insert into vtiger_productcurrencyrel values(?,?,?,?)";
 193                  $adb->pquery($query, array($this->id,$curid,$converted_price,$actualPrice));
 194  
 195                  // Update the Product information with Base Currency choosen by the User.
 196                  if ($_REQUEST['base_currency'] == $cur_valuename) {
 197                      $currencySet = 1;
 198                      $adb->pquery("update vtiger_products set currency_id=?, unit_price=? where productid=?", array($curid, $actualPrice, $this->id));
 199                  }
 200              }
 201              if(!$currencySet){
 202                  $curid = fetchCurrency($current_user->id);
 203                  $adb->pquery("update vtiger_products set currency_id=? where productid=?", array($curid, $this->id));
 204              }
 205          }
 206  
 207          $log->debug("Exiting from insertPriceInformation($tablename, $module) method ...");
 208      }
 209  
 210  	function updateUnitPrice() {
 211          $prod_res = $this->db->pquery("select unit_price, currency_id from vtiger_products where productid=?", array($this->id));
 212          $prod_unit_price = $this->db->query_result($prod_res, 0, 'unit_price');
 213          $prod_base_currency = $this->db->query_result($prod_res, 0, 'currency_id');
 214  
 215          $query = "update vtiger_productcurrencyrel set actual_price=? where productid=? and currencyid=?";
 216          $params = array($prod_unit_price, $this->id, $prod_base_currency);
 217          $this->db->pquery($query, $params);
 218      }
 219  
 220  	function insertIntoAttachment($id,$module)
 221      {
 222          global  $log,$adb;
 223          $log->debug("Entering into insertIntoAttachment($id,$module) method.");
 224  
 225          $file_saved = false;
 226          foreach($_FILES as $fileindex => $files)
 227          {
 228              if($files['name'] != '' && $files['size'] > 0)
 229              {
 230                    if($_REQUEST[$fileindex.'_hidden'] != '')
 231                        $files['original_name'] = vtlib_purify($_REQUEST[$fileindex.'_hidden']);
 232                    else
 233                        $files['original_name'] = stripslashes($files['name']);
 234                    $files['original_name'] = str_replace('"','',$files['original_name']);
 235                  $file_saved = $this->uploadAndSaveFile($id,$module,$files);
 236              }
 237          }
 238  
 239          //Updating image information in main table of products
 240          $existingImageSql = 'SELECT name FROM vtiger_seattachmentsrel INNER JOIN vtiger_attachments ON
 241                                  vtiger_seattachmentsrel.attachmentsid = vtiger_attachments.attachmentsid LEFT JOIN vtiger_products ON
 242                                  vtiger_products.productid = vtiger_seattachmentsrel.crmid WHERE vtiger_seattachmentsrel.crmid = ?';
 243          $existingImages = $adb->pquery($existingImageSql,array($id));
 244          $numOfRows = $adb->num_rows($existingImages);
 245          $productImageMap = array();
 246  
 247          for ($i = 0; $i < $numOfRows; $i++) {
 248              $imageName = $adb->query_result($existingImages, $i, "name");
 249              array_push($productImageMap, decode_html($imageName));
 250          }
 251          $commaSeperatedFileNames = implode(",", $productImageMap);
 252  
 253          $adb->pquery('UPDATE vtiger_products SET imagename = ? WHERE productid = ?',array($commaSeperatedFileNames,$id));
 254  
 255          //Remove the deleted vtiger_attachments from db - Products
 256          if($module == 'Products' && $_REQUEST['del_file_list'] != '')
 257          {
 258              $del_file_list = explode("###",trim($_REQUEST['del_file_list'],"###"));
 259              foreach($del_file_list as $del_file_name)
 260              {
 261                  $attach_res = $adb->pquery("select vtiger_attachments.attachmentsid from vtiger_attachments inner join vtiger_seattachmentsrel on vtiger_attachments.attachmentsid=vtiger_seattachmentsrel.attachmentsid where crmid=? and name=?", array($id,$del_file_name));
 262                  $attachments_id = $adb->query_result($attach_res,0,'attachmentsid');
 263  
 264                  $del_res1 = $adb->pquery("delete from vtiger_attachments where attachmentsid=?", array($attachments_id));
 265                  $del_res2 = $adb->pquery("delete from vtiger_seattachmentsrel where attachmentsid=?", array($attachments_id));
 266              }
 267          }
 268  
 269          $log->debug("Exiting from insertIntoAttachment($id,$module) method.");
 270      }
 271  
 272  
 273  
 274      /**    function used to get the list of leads which are related to the product
 275       *    @param int $id - product id
 276       *    @return array - array which will be returned from the function GetRelatedList
 277       */
 278  	function get_leads($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 279          global $log, $singlepane_view,$currentModule,$current_user;
 280          $log->debug("Entering get_leads(".$id.") method ...");
 281          $this_module = $currentModule;
 282  
 283          $related_module = vtlib_getModuleNameById($rel_tab_id);
 284          require_once("modules/$related_module/$related_module.php");
 285          $other = new $related_module();
 286          vtlib_setup_modulevars($related_module, $other);
 287          $singular_modname = vtlib_toSingular($related_module);
 288  
 289          $parenttab = getParentTab();
 290  
 291          if($singlepane_view == 'true')
 292              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 293          else
 294              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 295  
 296          $button = '';
 297  
 298          if($actions) {
 299              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 300              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 301                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 302              }
 303              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 304                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 305                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 306                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 307              }
 308          }
 309  
 310          $query = "SELECT vtiger_leaddetails.leadid, vtiger_crmentity.crmid, vtiger_leaddetails.firstname, vtiger_leaddetails.lastname, vtiger_leaddetails.company, vtiger_leadaddress.phone, vtiger_leadsubdetails.website, vtiger_leaddetails.email, case when (vtiger_users.user_name not like \"\") then vtiger_users.user_name else vtiger_groups.groupname end as user_name, vtiger_crmentity.smownerid, vtiger_products.productname, vtiger_products.qty_per_unit, vtiger_products.unit_price, vtiger_products.expiry_date
 311              FROM vtiger_leaddetails
 312              INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_leaddetails.leadid
 313              INNER JOIN vtiger_leadaddress ON vtiger_leadaddress.leadaddressid = vtiger_leaddetails.leadid
 314              INNER JOIN vtiger_leadsubdetails ON vtiger_leadsubdetails.leadsubscriptionid = vtiger_leaddetails.leadid
 315              INNER JOIN vtiger_seproductsrel ON vtiger_seproductsrel.crmid=vtiger_leaddetails.leadid
 316              INNER JOIN vtiger_products ON vtiger_seproductsrel.productid = vtiger_products.productid
 317              INNER JOIN vtiger_leadscf ON vtiger_leaddetails.leadid = vtiger_leadscf.leadid
 318              LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid
 319              LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 320              WHERE vtiger_crmentity.deleted = 0 AND vtiger_products.productid = ".$id;
 321  
 322          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 323  
 324          if($return_value == null) $return_value = Array();
 325          $return_value['CUSTOM_BUTTON'] = $button;
 326  
 327          $log->debug("Exiting get_leads method ...");
 328          return $return_value;
 329      }
 330  
 331      /**    function used to get the list of accounts which are related to the product
 332       *    @param int $id - product id
 333       *    @return array - array which will be returned from the function GetRelatedList
 334       */
 335  	function get_accounts($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 336          global $log, $singlepane_view,$currentModule,$current_user;
 337          $log->debug("Entering get_accounts(".$id.") method ...");
 338          $this_module = $currentModule;
 339  
 340          $related_module = vtlib_getModuleNameById($rel_tab_id);
 341          require_once("modules/$related_module/$related_module.php");
 342          $other = new $related_module();
 343          vtlib_setup_modulevars($related_module, $other);
 344          $singular_modname = vtlib_toSingular($related_module);
 345  
 346          $parenttab = getParentTab();
 347  
 348          if($singlepane_view == 'true')
 349              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 350          else
 351              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 352  
 353          $button = '';
 354  
 355          if($actions) {
 356              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 357              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 358                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 359              }
 360              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 361                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 362                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 363                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 364              }
 365          }
 366  
 367          $query = "SELECT vtiger_account.accountid, vtiger_crmentity.crmid, vtiger_account.accountname, vtiger_accountbillads.bill_city, vtiger_account.website, vtiger_account.phone, case when (vtiger_users.user_name not like \"\") then vtiger_users.user_name else vtiger_groups.groupname end as user_name, vtiger_crmentity.smownerid, vtiger_products.productname, vtiger_products.qty_per_unit, vtiger_products.unit_price, vtiger_products.expiry_date
 368              FROM vtiger_account
 369              INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_account.accountid
 370              INNER JOIN vtiger_accountbillads ON vtiger_accountbillads.accountaddressid = vtiger_account.accountid
 371              LEFT JOIN vtiger_accountshipads ON vtiger_accountshipads.accountaddressid = vtiger_account.accountid
 372              INNER JOIN vtiger_seproductsrel ON vtiger_seproductsrel.crmid=vtiger_account.accountid
 373              INNER JOIN vtiger_products ON vtiger_seproductsrel.productid = vtiger_products.productid
 374              INNER JOIN vtiger_accountscf ON vtiger_account.accountid = vtiger_accountscf.accountid
 375              LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid
 376              LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 377              WHERE vtiger_crmentity.deleted = 0 AND vtiger_products.productid = ".$id;
 378  
 379          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 380  
 381          if($return_value == null) $return_value = Array();
 382          $return_value['CUSTOM_BUTTON'] = $button;
 383  
 384          $log->debug("Exiting get_accounts method ...");
 385          return $return_value;
 386      }
 387  
 388      /**    function used to get the list of contacts which are related to the product
 389       *    @param int $id - product id
 390       *    @return array - array which will be returned from the function GetRelatedList
 391       */
 392  	function get_contacts($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 393          global $log, $singlepane_view,$currentModule,$current_user;
 394          $log->debug("Entering get_contacts(".$id.") method ...");
 395          $this_module = $currentModule;
 396  
 397          $related_module = vtlib_getModuleNameById($rel_tab_id);
 398          require_once("modules/$related_module/$related_module.php");
 399          $other = new $related_module();
 400          vtlib_setup_modulevars($related_module, $other);
 401          $singular_modname = vtlib_toSingular($related_module);
 402  
 403          $parenttab = getParentTab();
 404  
 405          if($singlepane_view == 'true')
 406              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 407          else
 408              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 409  
 410          $button = '';
 411  
 412          if($actions) {
 413              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 414              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 415                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 416              }
 417              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 418                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 419                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 420                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 421              }
 422          }
 423  
 424          $query = "SELECT vtiger_contactdetails.firstname, vtiger_contactdetails.lastname, vtiger_contactdetails.title, vtiger_contactdetails.accountid, vtiger_contactdetails.email, vtiger_contactdetails.phone, vtiger_crmentity.crmid, case when (vtiger_users.user_name not like \"\") then vtiger_users.user_name else vtiger_groups.groupname end as user_name, vtiger_crmentity.smownerid, vtiger_products.productname, vtiger_products.qty_per_unit, vtiger_products.unit_price, vtiger_products.expiry_date,vtiger_account.accountname
 425              FROM vtiger_contactdetails
 426              INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_contactdetails.contactid
 427              INNER JOIN vtiger_seproductsrel ON vtiger_seproductsrel.crmid=vtiger_contactdetails.contactid
 428              INNER JOIN vtiger_contactaddress ON vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid
 429              INNER JOIN vtiger_contactsubdetails ON vtiger_contactdetails.contactid = vtiger_contactsubdetails.contactsubscriptionid
 430              INNER JOIN vtiger_customerdetails ON vtiger_contactdetails.contactid = vtiger_customerdetails.customerid
 431              INNER JOIN vtiger_contactscf ON vtiger_contactdetails.contactid = vtiger_contactscf.contactid
 432              INNER JOIN vtiger_products ON vtiger_seproductsrel.productid = vtiger_products.productid
 433              LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid
 434              LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 435              LEFT JOIN vtiger_account ON vtiger_account.accountid = vtiger_contactdetails.accountid
 436              WHERE vtiger_crmentity.deleted = 0 AND vtiger_products.productid = ".$id;
 437  
 438          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 439  
 440          if($return_value == null) $return_value = Array();
 441          $return_value['CUSTOM_BUTTON'] = $button;
 442  
 443          $log->debug("Exiting get_contacts method ...");
 444          return $return_value;
 445      }
 446  
 447  
 448      /**    function used to get the list of potentials which are related to the product
 449       *    @param int $id - product id
 450       *    @return array - array which will be returned from the function GetRelatedList
 451       */
 452  	function get_opportunities($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 453          global $log, $singlepane_view,$currentModule,$current_user;
 454          $log->debug("Entering get_opportunities(".$id.") method ...");
 455          $this_module = $currentModule;
 456  
 457          $related_module = vtlib_getModuleNameById($rel_tab_id);
 458          require_once("modules/$related_module/$related_module.php");
 459          $other = new $related_module();
 460          vtlib_setup_modulevars($related_module, $other);
 461          $singular_modname = vtlib_toSingular($related_module);
 462  
 463          $parenttab = getParentTab();
 464  
 465          if($singlepane_view == 'true')
 466              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 467          else
 468              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 469  
 470          $button = '';
 471  
 472          if($actions) {
 473              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 474              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 475                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 476              }
 477              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 478                  $button .= "<input title='".getTranslatedString('LBL_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 479                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 480                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 481              }
 482          }
 483  
 484          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 485                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 486          $query = "SELECT vtiger_potential.potentialid, vtiger_crmentity.crmid,
 487              vtiger_potential.potentialname, vtiger_account.accountname, vtiger_potential.related_to, vtiger_potential.contact_id,
 488              vtiger_potential.sales_stage, vtiger_potential.amount, vtiger_potential.closingdate,
 489              case when (vtiger_users.user_name not like '') then $userNameSql else
 490              vtiger_groups.groupname end as user_name, vtiger_crmentity.smownerid,
 491              vtiger_products.productname, vtiger_products.qty_per_unit, vtiger_products.unit_price,
 492              vtiger_products.expiry_date FROM vtiger_potential
 493              INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_potential.potentialid
 494              INNER JOIN vtiger_seproductsrel ON vtiger_seproductsrel.crmid = vtiger_potential.potentialid
 495              INNER JOIN vtiger_products ON vtiger_seproductsrel.productid = vtiger_products.productid
 496              INNER JOIN vtiger_potentialscf ON vtiger_potential.potentialid = vtiger_potentialscf.potentialid
 497              LEFT JOIN vtiger_account ON vtiger_potential.related_to = vtiger_account.accountid
 498              LEFT JOIN vtiger_contactdetails ON vtiger_potential.contact_id = vtiger_contactdetails.contactid
 499              LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid
 500              LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 501              WHERE vtiger_crmentity.deleted = 0 AND vtiger_products.productid = ".$id;
 502  
 503          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 504  
 505          if($return_value == null) $return_value = Array();
 506          $return_value['CUSTOM_BUTTON'] = $button;
 507  
 508          $log->debug("Exiting get_opportunities method ...");
 509          return $return_value;
 510      }
 511  
 512      /**    function used to get the list of tickets which are related to the product
 513       *    @param int $id - product id
 514       *    @return array - array which will be returned from the function GetRelatedList
 515       */
 516  	function get_tickets($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 517          global $log, $singlepane_view,$currentModule,$current_user;
 518          $log->debug("Entering get_tickets(".$id.") method ...");
 519          $this_module = $currentModule;
 520  
 521          $related_module = vtlib_getModuleNameById($rel_tab_id);
 522          require_once("modules/$related_module/$related_module.php");
 523          $other = new $related_module();
 524          vtlib_setup_modulevars($related_module, $other);
 525          $singular_modname = vtlib_toSingular($related_module);
 526  
 527          $parenttab = getParentTab();
 528  
 529          if($singlepane_view == 'true')
 530              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 531          else
 532              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 533  
 534          $button = '';
 535  
 536          if($actions && getFieldVisibilityPermission($related_module, $current_user->id, 'product_id','readwrite') == '0') {
 537              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 538              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 539                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 540              }
 541              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 542                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 543                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 544                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 545              }
 546          }
 547  
 548          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 549                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 550          $query = "SELECT  case when (vtiger_users.user_name not like \"\") then $userNameSql else vtiger_groups.groupname end as user_name, vtiger_users.id,
 551              vtiger_products.productid, vtiger_products.productname,
 552              vtiger_troubletickets.ticketid,
 553              vtiger_troubletickets.parent_id, vtiger_troubletickets.title,
 554              vtiger_troubletickets.status, vtiger_troubletickets.priority,
 555              vtiger_crmentity.crmid, vtiger_crmentity.smownerid,
 556              vtiger_crmentity.modifiedtime, vtiger_troubletickets.ticket_no
 557              FROM vtiger_troubletickets
 558              INNER JOIN vtiger_crmentity
 559                  ON vtiger_crmentity.crmid = vtiger_troubletickets.ticketid
 560              LEFT JOIN vtiger_products
 561                  ON vtiger_products.productid = vtiger_troubletickets.product_id
 562              LEFT JOIN vtiger_ticketcf ON vtiger_troubletickets.ticketid = vtiger_ticketcf.ticketid
 563              LEFT JOIN vtiger_users
 564                  ON vtiger_users.id = vtiger_crmentity.smownerid
 565              LEFT JOIN vtiger_groups
 566                  ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 567              WHERE vtiger_crmentity.deleted = 0
 568              AND vtiger_products.productid = ".$id;
 569  
 570          $log->debug("Exiting get_tickets method ...");
 571  
 572          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 573  
 574          if($return_value == null) $return_value = Array();
 575          $return_value['CUSTOM_BUTTON'] = $button;
 576  
 577          $log->debug("Exiting get_tickets method ...");
 578          return $return_value;
 579      }
 580  
 581      /**    function used to get the list of activities which are related to the product
 582       *    @param int $id - product id
 583       *    @return array - array which will be returned from the function GetRelatedList
 584       */
 585  	function get_activities($id)
 586      {
 587          global $log, $singlepane_view;
 588          $log->debug("Entering get_activities(".$id.") method ...");
 589          global $app_strings;
 590  
 591          require_once ('modules/Calendar/Activity.php');
 592  
 593              //if($this->column_fields['contact_id']!=0 && $this->column_fields['contact_id']!='')
 594              $focus = new Activity();
 595  
 596          $button = '';
 597  
 598          if($singlepane_view == 'true')
 599              $returnset = '&return_module=Products&return_action=DetailView&return_id='.$id;
 600          else
 601              $returnset = '&return_module=Products&return_action=CallRelatedList&return_id='.$id;
 602  
 603  
 604          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 605                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 606          $query = "SELECT vtiger_contactdetails.lastname,
 607              vtiger_contactdetails.firstname,
 608              vtiger_contactdetails.contactid,
 609              vtiger_activity.*,
 610              vtiger_seactivityrel.crmid as parent_id,
 611              vtiger_crmentity.crmid, vtiger_crmentity.smownerid,
 612              vtiger_crmentity.modifiedtime,
 613              $userNameSql,
 614              vtiger_recurringevents.recurringtype
 615              FROM vtiger_activity
 616              INNER JOIN vtiger_seactivityrel
 617                  ON vtiger_seactivityrel.activityid = vtiger_activity.activityid
 618              INNER JOIN vtiger_crmentity
 619                  ON vtiger_crmentity.crmid=vtiger_activity.activityid
 620              LEFT JOIN vtiger_cntactivityrel
 621                  ON vtiger_cntactivityrel.activityid = vtiger_activity.activityid
 622              LEFT JOIN vtiger_contactdetails
 623                  ON vtiger_contactdetails.contactid = vtiger_cntactivityrel.contactid
 624              LEFT JOIN vtiger_users
 625                  ON vtiger_users.id = vtiger_crmentity.smownerid
 626              LEFT OUTER JOIN vtiger_recurringevents
 627                  ON vtiger_recurringevents.activityid = vtiger_activity.activityid
 628              LEFT JOIN vtiger_groups
 629                  ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 630              WHERE vtiger_seactivityrel.crmid=".$id."
 631              AND (activitytype != 'Emails')";
 632          $log->debug("Exiting get_activities method ...");
 633          return GetRelatedList('Products','Calendar',$focus,$query,$button,$returnset);
 634      }
 635  
 636      /**    function used to get the list of quotes which are related to the product
 637       *    @param int $id - product id
 638       *    @return array - array which will be returned from the function GetRelatedList
 639       */
 640  	function get_quotes($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 641          global $log, $singlepane_view,$currentModule,$current_user;
 642          $log->debug("Entering get_quotes(".$id.") method ...");
 643          $this_module = $currentModule;
 644  
 645          $related_module = vtlib_getModuleNameById($rel_tab_id);
 646          require_once("modules/$related_module/$related_module.php");
 647          $other = new $related_module();
 648          vtlib_setup_modulevars($related_module, $other);
 649          $singular_modname = vtlib_toSingular($related_module);
 650  
 651          $parenttab = getParentTab();
 652  
 653          if($singlepane_view == 'true')
 654              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 655          else
 656              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 657  
 658          $button = '';
 659  
 660          if($actions) {
 661              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 662              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 663                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 664              }
 665              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 666                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 667                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 668                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 669              }
 670          }
 671  
 672          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 673                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 674          $query = "SELECT vtiger_crmentity.*,
 675              vtiger_quotes.*,
 676              vtiger_potential.potentialname,
 677              vtiger_account.accountname,
 678              vtiger_inventoryproductrel.productid,
 679              case when (vtiger_users.user_name not like '') then $userNameSql
 680                  else vtiger_groups.groupname end as user_name
 681              FROM vtiger_quotes
 682              INNER JOIN vtiger_crmentity
 683                  ON vtiger_crmentity.crmid = vtiger_quotes.quoteid
 684              INNER JOIN vtiger_inventoryproductrel
 685                  ON vtiger_inventoryproductrel.id = vtiger_quotes.quoteid
 686              LEFT OUTER JOIN vtiger_account
 687                  ON vtiger_account.accountid = vtiger_quotes.accountid
 688              LEFT OUTER JOIN vtiger_potential
 689                  ON vtiger_potential.potentialid = vtiger_quotes.potentialid
 690              LEFT JOIN vtiger_groups
 691                  ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 692              LEFT JOIN vtiger_quotescf
 693                  ON vtiger_quotescf.quoteid = vtiger_quotes.quoteid
 694              LEFT JOIN vtiger_quotesbillads
 695                  ON vtiger_quotesbillads.quotebilladdressid = vtiger_quotes.quoteid
 696              LEFT JOIN vtiger_quotesshipads
 697                  ON vtiger_quotesshipads.quoteshipaddressid = vtiger_quotes.quoteid
 698              LEFT JOIN vtiger_users
 699                  ON vtiger_users.id = vtiger_crmentity.smownerid
 700              WHERE vtiger_crmentity.deleted = 0
 701              AND vtiger_inventoryproductrel.productid = ".$id;
 702  
 703          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 704  
 705          if($return_value == null) $return_value = Array();
 706          $return_value['CUSTOM_BUTTON'] = $button;
 707  
 708          $log->debug("Exiting get_quotes method ...");
 709          return $return_value;
 710      }
 711  
 712      /**    function used to get the list of purchase orders which are related to the product
 713       *    @param int $id - product id
 714       *    @return array - array which will be returned from the function GetRelatedList
 715       */
 716  	function get_purchase_orders($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 717          global $log, $singlepane_view,$currentModule,$current_user;
 718          $log->debug("Entering get_purchase_orders(".$id.") method ...");
 719          $this_module = $currentModule;
 720  
 721          $related_module = vtlib_getModuleNameById($rel_tab_id);
 722          require_once("modules/$related_module/$related_module.php");
 723          $other = new $related_module();
 724          vtlib_setup_modulevars($related_module, $other);
 725          $singular_modname = vtlib_toSingular($related_module);
 726  
 727          $parenttab = getParentTab();
 728  
 729          if($singlepane_view == 'true')
 730              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 731          else
 732              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 733  
 734          $button = '';
 735  
 736          if($actions) {
 737              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 738              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 739                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 740              }
 741              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 742                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 743                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 744                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 745              }
 746          }
 747  
 748          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 749                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 750          $query = "SELECT vtiger_crmentity.*,
 751              vtiger_purchaseorder.*,
 752              vtiger_products.productname,
 753              vtiger_inventoryproductrel.productid,
 754              case when (vtiger_users.user_name not like '') then $userNameSql
 755                  else vtiger_groups.groupname end as user_name
 756              FROM vtiger_purchaseorder
 757              INNER JOIN vtiger_crmentity
 758                  ON vtiger_crmentity.crmid = vtiger_purchaseorder.purchaseorderid
 759              INNER JOIN vtiger_inventoryproductrel
 760                  ON vtiger_inventoryproductrel.id = vtiger_purchaseorder.purchaseorderid
 761              INNER JOIN vtiger_products
 762                  ON vtiger_products.productid = vtiger_inventoryproductrel.productid
 763              LEFT JOIN vtiger_groups
 764                  ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 765              LEFT JOIN vtiger_purchaseordercf
 766                  ON vtiger_purchaseordercf.purchaseorderid = vtiger_purchaseorder.purchaseorderid
 767              LEFT JOIN vtiger_pobillads
 768                  ON vtiger_pobillads.pobilladdressid = vtiger_purchaseorder.purchaseorderid
 769              LEFT JOIN vtiger_poshipads
 770                  ON vtiger_poshipads.poshipaddressid = vtiger_purchaseorder.purchaseorderid
 771              LEFT JOIN vtiger_users
 772                  ON vtiger_users.id = vtiger_crmentity.smownerid
 773              WHERE vtiger_crmentity.deleted = 0
 774              AND vtiger_products.productid = ".$id;
 775  
 776          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 777  
 778          if($return_value == null) $return_value = Array();
 779          $return_value['CUSTOM_BUTTON'] = $button;
 780  
 781          $log->debug("Exiting get_purchase_orders method ...");
 782          return $return_value;
 783      }
 784  
 785      /**    function used to get the list of sales orders which are related to the product
 786       *    @param int $id - product id
 787       *    @return array - array which will be returned from the function GetRelatedList
 788       */
 789  	function get_salesorder($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 790          global $log, $singlepane_view,$currentModule,$current_user;
 791          $log->debug("Entering get_salesorder(".$id.") method ...");
 792          $this_module = $currentModule;
 793  
 794          $related_module = vtlib_getModuleNameById($rel_tab_id);
 795          require_once("modules/$related_module/$related_module.php");
 796          $other = new $related_module();
 797          vtlib_setup_modulevars($related_module, $other);
 798          $singular_modname = vtlib_toSingular($related_module);
 799  
 800          $parenttab = getParentTab();
 801  
 802          if($singlepane_view == 'true')
 803              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 804          else
 805              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 806  
 807          $button = '';
 808  
 809          if($actions) {
 810              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 811              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 812                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 813              }
 814              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 815                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 816                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 817                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 818              }
 819          }
 820  
 821          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 822                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 823          $query = "SELECT vtiger_crmentity.*,
 824              vtiger_salesorder.*,
 825              vtiger_products.productname AS productname,
 826              vtiger_account.accountname,
 827              case when (vtiger_users.user_name not like '') then $userNameSql
 828                  else vtiger_groups.groupname end as user_name
 829              FROM vtiger_salesorder
 830              INNER JOIN vtiger_crmentity
 831                  ON vtiger_crmentity.crmid = vtiger_salesorder.salesorderid
 832              INNER JOIN vtiger_inventoryproductrel
 833                  ON vtiger_inventoryproductrel.id = vtiger_salesorder.salesorderid
 834              INNER JOIN vtiger_products
 835                  ON vtiger_products.productid = vtiger_inventoryproductrel.productid
 836              LEFT OUTER JOIN vtiger_account
 837                  ON vtiger_account.accountid = vtiger_salesorder.accountid
 838              LEFT JOIN vtiger_groups
 839                  ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 840              LEFT JOIN vtiger_salesordercf
 841                  ON vtiger_salesordercf.salesorderid = vtiger_salesorder.salesorderid
 842              LEFT JOIN vtiger_invoice_recurring_info
 843                  ON vtiger_invoice_recurring_info.start_period = vtiger_salesorder.salesorderid
 844              LEFT JOIN vtiger_sobillads
 845                  ON vtiger_sobillads.sobilladdressid = vtiger_salesorder.salesorderid
 846              LEFT JOIN vtiger_soshipads
 847                  ON vtiger_soshipads.soshipaddressid = vtiger_salesorder.salesorderid
 848              LEFT JOIN vtiger_users
 849                  ON vtiger_users.id = vtiger_crmentity.smownerid
 850              WHERE vtiger_crmentity.deleted = 0
 851              AND vtiger_products.productid = ".$id;
 852  
 853          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 854  
 855          if($return_value == null) $return_value = Array();
 856          $return_value['CUSTOM_BUTTON'] = $button;
 857  
 858          $log->debug("Exiting get_salesorder method ...");
 859          return $return_value;
 860      }
 861  
 862      /**    function used to get the list of invoices which are related to the product
 863       *    @param int $id - product id
 864       *    @return array - array which will be returned from the function GetRelatedList
 865       */
 866  	function get_invoices($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 867          global $log, $singlepane_view,$currentModule,$current_user;
 868          $log->debug("Entering get_invoices(".$id.") method ...");
 869          $this_module = $currentModule;
 870  
 871          $related_module = vtlib_getModuleNameById($rel_tab_id);
 872          require_once("modules/$related_module/$related_module.php");
 873          $other = new $related_module();
 874          vtlib_setup_modulevars($related_module, $other);
 875          $singular_modname = vtlib_toSingular($related_module);
 876  
 877          $parenttab = getParentTab();
 878  
 879          if($singlepane_view == 'true')
 880              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 881          else
 882              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 883  
 884          $button = '';
 885  
 886          if($actions) {
 887              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 888              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 889                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 890              }
 891              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 892                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 893                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 894                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 895              }
 896          }
 897  
 898          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 899                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 900          $query = "SELECT vtiger_crmentity.*,
 901              vtiger_invoice.*,
 902              vtiger_inventoryproductrel.quantity,
 903              vtiger_account.accountname,
 904              case when (vtiger_users.user_name not like '') then $userNameSql
 905                  else vtiger_groups.groupname end as user_name
 906              FROM vtiger_invoice
 907              INNER JOIN vtiger_crmentity
 908                  ON vtiger_crmentity.crmid = vtiger_invoice.invoiceid
 909              LEFT OUTER JOIN vtiger_account
 910                  ON vtiger_account.accountid = vtiger_invoice.accountid
 911              INNER JOIN vtiger_inventoryproductrel
 912                  ON vtiger_inventoryproductrel.id = vtiger_invoice.invoiceid
 913              LEFT JOIN vtiger_groups
 914                  ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 915              LEFT JOIN vtiger_invoicecf
 916                  ON vtiger_invoicecf.invoiceid = vtiger_invoice.invoiceid
 917              LEFT JOIN vtiger_invoicebillads
 918                  ON vtiger_invoicebillads.invoicebilladdressid = vtiger_invoice.invoiceid
 919              LEFT JOIN vtiger_invoiceshipads
 920                  ON vtiger_invoiceshipads.invoiceshipaddressid = vtiger_invoice.invoiceid
 921              LEFT JOIN vtiger_users
 922                  ON  vtiger_users.id=vtiger_crmentity.smownerid
 923              WHERE vtiger_crmentity.deleted = 0
 924              AND vtiger_inventoryproductrel.productid = ".$id;
 925  
 926          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 927  
 928          if($return_value == null) $return_value = Array();
 929          $return_value['CUSTOM_BUTTON'] = $button;
 930  
 931          $log->debug("Exiting get_invoices method ...");
 932          return $return_value;
 933      }
 934  
 935      /**    function used to get the list of pricebooks which are related to the product
 936       *    @param int $id - product id
 937       *    @return array - array which will be returned from the function GetRelatedList
 938       */
 939  	function get_product_pricebooks($id, $cur_tab_id, $rel_tab_id, $actions=false)
 940      {
 941          global $log,$singlepane_view,$currentModule;
 942          $log->debug("Entering get_product_pricebooks(".$id.") method ...");
 943  
 944          $related_module = vtlib_getModuleNameById($rel_tab_id);
 945          checkFileAccessForInclusion("modules/$related_module/$related_module.php");
 946          require_once("modules/$related_module/$related_module.php");
 947          $focus = new $related_module();
 948          $singular_modname = vtlib_toSingular($related_module);
 949  
 950          $button = '';
 951          if($actions) {
 952              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 953              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes' && isPermitted($currentModule,'EditView',$id) == 'yes') {
 954                  $button .= "<input title='".getTranslatedString('LBL_ADD_TO'). " ". getTranslatedString($related_module) ."' class='crmbutton small create'" .
 955                      " onclick='this.form.action.value=\"AddProductToPriceBooks\";this.form.module.value=\"$currentModule\"' type='submit' name='button'" .
 956                      " value='". getTranslatedString('LBL_ADD_TO'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 957              }
 958          }
 959  
 960          if($singlepane_view == 'true')
 961              $returnset = '&return_module=Products&return_action=DetailView&return_id='.$id;
 962          else
 963              $returnset = '&return_module=Products&return_action=CallRelatedList&return_id='.$id;
 964  
 965  
 966          $query = "SELECT vtiger_crmentity.crmid,
 967              vtiger_pricebook.*,
 968              vtiger_pricebookproductrel.productid as prodid
 969              FROM vtiger_pricebook
 970              INNER JOIN vtiger_crmentity
 971                  ON vtiger_crmentity.crmid = vtiger_pricebook.pricebookid
 972              INNER JOIN vtiger_pricebookproductrel
 973                  ON vtiger_pricebookproductrel.pricebookid = vtiger_pricebook.pricebookid
 974              INNER JOIN vtiger_pricebookcf
 975                  ON vtiger_pricebookcf.pricebookid = vtiger_pricebook.pricebookid
 976              WHERE vtiger_crmentity.deleted = 0
 977              AND vtiger_pricebookproductrel.productid = ".$id;
 978          $log->debug("Exiting get_product_pricebooks method ...");
 979  
 980          $return_value = GetRelatedList($currentModule, $related_module, $focus, $query, $button, $returnset);
 981  
 982          if($return_value == null) $return_value = Array();
 983          $return_value['CUSTOM_BUTTON'] = $button;
 984  
 985          return $return_value;
 986      }
 987  
 988      /**    function used to get the number of vendors which are related to the product
 989       *    @param int $id - product id
 990       *    @return int number of rows - return the number of products which do not have relationship with vendor
 991       */
 992  	function product_novendor()
 993      {
 994          global $log;
 995          $log->debug("Entering product_novendor() method ...");
 996          $query = "SELECT vtiger_products.productname, vtiger_crmentity.deleted
 997              FROM vtiger_products
 998              INNER JOIN vtiger_crmentity
 999                  ON vtiger_crmentity.crmid = vtiger_products.productid
1000              WHERE vtiger_crmentity.deleted = 0
1001              AND vtiger_products.vendor_id is NULL";
1002          $result=$this->db->pquery($query, array());
1003          $log->debug("Exiting product_novendor method ...");
1004          return $this->db->num_rows($result);
1005      }
1006  
1007      /**
1008      * Function to get Product's related Products
1009      * @param  integer   $id      - productid
1010      * returns related Products record in array format
1011      */
1012  	function get_products($id, $cur_tab_id, $rel_tab_id, $actions=false) {
1013          global $log, $singlepane_view,$currentModule,$current_user;
1014          $log->debug("Entering get_products(".$id.") method ...");
1015          $this_module = $currentModule;
1016  
1017          $related_module = vtlib_getModuleNameById($rel_tab_id);
1018          require_once("modules/$related_module/$related_module.php");
1019          $other = new $related_module();
1020          vtlib_setup_modulevars($related_module, $other);
1021          $singular_modname = vtlib_toSingular($related_module);
1022  
1023          $parenttab = getParentTab();
1024  
1025          if($singlepane_view == 'true')
1026              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
1027          else
1028              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
1029  
1030          $button = '';
1031  
1032          if($actions && $this->ismember_check() === 0) {
1033              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
1034              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
1035                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview&select=enable&form=EditView&form_submit=false&recordid=$id&parenttab=$parenttab','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
1036              }
1037              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
1038                  $button .= "<input type='hidden' name='createmode' id='createmode' value='link' />".
1039                      "<input title='".getTranslatedString('LBL_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
1040                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\";' type='submit' name='button'" .
1041                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
1042              }
1043          }
1044  
1045          $query = "SELECT vtiger_products.productid, vtiger_products.productname,
1046              vtiger_products.productcode, vtiger_products.commissionrate,
1047              vtiger_products.qty_per_unit, vtiger_products.unit_price,
1048              vtiger_crmentity.crmid, vtiger_crmentity.smownerid
1049              FROM vtiger_products
1050              INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid
1051              INNER JOIN vtiger_productcf
1052                  ON vtiger_products.productid = vtiger_productcf.productid
1053              LEFT JOIN vtiger_seproductsrel ON vtiger_seproductsrel.crmid = vtiger_products.productid AND vtiger_seproductsrel.setype='Products'
1054              LEFT JOIN vtiger_users
1055                  ON vtiger_users.id=vtiger_crmentity.smownerid
1056              LEFT JOIN vtiger_groups
1057                  ON vtiger_groups.groupid = vtiger_crmentity.smownerid
1058              WHERE vtiger_crmentity.deleted = 0 AND vtiger_seproductsrel.productid = $id ";
1059  
1060          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
1061  
1062          if($return_value == null) $return_value = Array();
1063          $return_value['CUSTOM_BUTTON'] = $button;
1064  
1065          $log->debug("Exiting get_products method ...");
1066          return $return_value;
1067      }
1068  
1069      /**
1070      * Function to get Product's related Products
1071      * @param  integer   $id      - productid
1072      * returns related Products record in array format
1073      */
1074  	function get_parent_products($id)
1075      {
1076          global $log, $singlepane_view;
1077                  $log->debug("Entering get_products(".$id.") method ...");
1078  
1079          global $app_strings;
1080  
1081          $focus = new Products();
1082  
1083          $button = '';
1084  
1085          if(isPermitted("Products",1,"") == 'yes')
1086          {
1087              $button .= '<input title="'.$app_strings['LBL_NEW_PRODUCT'].'" accessyKey="F" class="button" onclick="this.form.action.value=\'EditView\';this.form.module.value=\'Products\';this.form.return_module.value=\'Products\';this.form.return_action.value=\'DetailView\'" type="submit" name="button" value="'.$app_strings['LBL_NEW_PRODUCT'].'">&nbsp;';
1088          }
1089          if($singlepane_view == 'true')
1090              $returnset = '&return_module=Products&return_action=DetailView&is_parent=1&return_id='.$id;
1091          else
1092              $returnset = '&return_module=Products&return_action=CallRelatedList&is_parent=1&return_id='.$id;
1093  
1094          $query = "SELECT vtiger_products.productid, vtiger_products.productname,
1095              vtiger_products.productcode, vtiger_products.commissionrate,
1096              vtiger_products.qty_per_unit, vtiger_products.unit_price,
1097              vtiger_crmentity.crmid, vtiger_crmentity.smownerid
1098              FROM vtiger_products
1099              INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid
1100              INNER JOIN vtiger_seproductsrel ON vtiger_seproductsrel.productid = vtiger_products.productid AND vtiger_seproductsrel.setype='Products'
1101              INNER JOIN vtiger_productcf ON vtiger_products.productid = vtiger_productcf.productid
1102  
1103              WHERE vtiger_crmentity.deleted = 0 AND vtiger_seproductsrel.crmid = $id ";
1104  
1105          $log->debug("Exiting get_products method ...");
1106          return GetRelatedList('Products','Products',$focus,$query,$button,$returnset);
1107      }
1108  
1109      /**    function used to get the export query for product
1110       *    @param reference $where - reference of the where variable which will be added with the query
1111       *    @return string $query - return the query which will give the list of products to export
1112       */
1113  	function create_export_query($where)
1114      {
1115          global $log, $current_user;
1116          $log->debug("Entering create_export_query(".$where.") method ...");
1117  
1118          include ("include/utils/ExportUtils.php");
1119  
1120          //To get the Permitted fields query and the permitted fields list
1121          $sql = getPermittedFieldsQuery("Products", "detail_view");
1122          $fields_list = getFieldsListFromQuery($sql);
1123  
1124          $query = "SELECT $fields_list FROM ".$this->table_name ."
1125              INNER JOIN vtiger_crmentity
1126                  ON vtiger_crmentity.crmid = vtiger_products.productid
1127              LEFT JOIN vtiger_productcf
1128                  ON vtiger_products.productid = vtiger_productcf.productid
1129              LEFT JOIN vtiger_vendor
1130                  ON vtiger_vendor.vendorid = vtiger_products.vendor_id";
1131  
1132          $query .= " LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid";
1133          $query .= " LEFT JOIN vtiger_users ON vtiger_crmentity.smownerid = vtiger_users.id AND vtiger_users.status='Active'";
1134          $query .= $this->getNonAdminAccessControlQuery('Products',$current_user);
1135          $where_auto = " vtiger_crmentity.deleted=0";
1136  
1137          if($where != '') $query .= " WHERE ($where) AND $where_auto";
1138          else $query .= " WHERE $where_auto";
1139  
1140          $log->debug("Exiting create_export_query method ...");
1141          return $query;
1142      }
1143  
1144      /** Function to check if the product is parent of any other product
1145      */
1146  	function isparent_check(){
1147          global $adb;
1148          $isparent_query = $adb->pquery(getListQuery("Products")." AND (vtiger_products.productid IN (SELECT productid from vtiger_seproductsrel WHERE vtiger_seproductsrel.productid = ? AND vtiger_seproductsrel.setype='Products'))",array($this->id));
1149          $isparent = $adb->num_rows($isparent_query);
1150          return $isparent;
1151      }
1152  
1153      /** Function to check if the product is member of other product
1154      */
1155  	function ismember_check(){
1156          global $adb;
1157          $ismember_query = $adb->pquery(getListQuery("Products")." AND (vtiger_products.productid IN (SELECT crmid from vtiger_seproductsrel WHERE vtiger_seproductsrel.crmid = ? AND vtiger_seproductsrel.setype='Products'))",array($this->id));
1158          $ismember = $adb->num_rows($ismember_query);
1159          return $ismember;
1160      }
1161  
1162      /**
1163       * Move the related records of the specified list of id's to the given record.
1164       * @param String This module name
1165       * @param Array List of Entity Id's from which related records need to be transfered
1166       * @param Integer Id of the the Record to which the related records are to be moved
1167       */
1168  	function transferRelatedRecords($module, $transferEntityIds, $entityId) {
1169          global $adb,$log;
1170          $log->debug("Entering function transferRelatedRecords ($module, $transferEntityIds, $entityId)");
1171  
1172          $rel_table_arr = Array("HelpDesk"=>"vtiger_troubletickets","Products"=>"vtiger_seproductsrel","Attachments"=>"vtiger_seattachmentsrel",
1173                  "Quotes"=>"vtiger_inventoryproductrel","PurchaseOrder"=>"vtiger_inventoryproductrel","SalesOrder"=>"vtiger_inventoryproductrel",
1174                  "Invoice"=>"vtiger_inventoryproductrel","PriceBooks"=>"vtiger_pricebookproductrel","Leads"=>"vtiger_seproductsrel",
1175                  "Accounts"=>"vtiger_seproductsrel","Potentials"=>"vtiger_seproductsrel","Contacts"=>"vtiger_seproductsrel",
1176                  "Documents"=>"vtiger_senotesrel",'Assets'=>'vtiger_assets',);
1177  
1178          $tbl_field_arr = Array("vtiger_troubletickets"=>"ticketid","vtiger_seproductsrel"=>"crmid","vtiger_seattachmentsrel"=>"attachmentsid",
1179                  "vtiger_inventoryproductrel"=>"id","vtiger_pricebookproductrel"=>"pricebookid","vtiger_seproductsrel"=>"crmid",
1180                  "vtiger_senotesrel"=>"notesid",'vtiger_assets'=>'assetsid');
1181  
1182          $entity_tbl_field_arr = Array("vtiger_troubletickets"=>"product_id","vtiger_seproductsrel"=>"crmid","vtiger_seattachmentsrel"=>"crmid",
1183                  "vtiger_inventoryproductrel"=>"productid","vtiger_pricebookproductrel"=>"productid","vtiger_seproductsrel"=>"productid",
1184                  "vtiger_senotesrel"=>"crmid",'vtiger_assets'=>'product');
1185  
1186          foreach($transferEntityIds as $transferId) {
1187              foreach($rel_table_arr as $rel_module=>$rel_table) {
1188                  $id_field = $tbl_field_arr[$rel_table];
1189                  $entity_id_field = $entity_tbl_field_arr[$rel_table];
1190                  // IN clause to avoid duplicate entries
1191                  $sel_result =  $adb->pquery("select $id_field from $rel_table where $entity_id_field=? " .
1192                          " and $id_field not in (select $id_field from $rel_table where $entity_id_field=?)",
1193                          array($transferId,$entityId));
1194                  $res_cnt = $adb->num_rows($sel_result);
1195                  if($res_cnt > 0) {
1196                      for($i=0;$i<$res_cnt;$i++) {
1197                          $id_field_value = $adb->query_result($sel_result,$i,$id_field);
1198                          $adb->pquery("update $rel_table set $entity_id_field=? where $entity_id_field=? and $id_field=?",
1199                              array($entityId,$transferId,$id_field_value));
1200                      }
1201                  }
1202              }
1203          }
1204          $log->debug("Exiting transferRelatedRecords...");
1205      }
1206  
1207      /*
1208       * Function to get the secondary query part of a report
1209       * @param - $module primary module name
1210       * @param - $secmodule secondary module name
1211       * returns the query string formed on fetching the related data for report for secondary module
1212       */
1213  	function generateReportsSecQuery($module,$secmodule,$queryplanner) {
1214          global $current_user;
1215          $matrix = $queryplanner->newDependencyMatrix();
1216  
1217          $matrix->setDependency("vtiger_crmentityProducts",array("vtiger_groupsProducts","vtiger_usersProducts","vtiger_lastModifiedByProducts"));
1218          $matrix->setDependency("vtiger_products",array("innerProduct","vtiger_crmentityProducts","vtiger_productcf","vtiger_vendorRelProducts"));
1219          //query planner Support  added
1220          if (!$queryplanner->requireTable('vtiger_products', $matrix)) {
1221              return '';
1222          }
1223          $query = $this->getRelationQuery($module,$secmodule,"vtiger_products","productid", $queryplanner);
1224          if ($queryplanner->requireTable("innerProduct")){
1225              $query .= " LEFT JOIN (
1226                      SELECT vtiger_products.productid,
1227                              (CASE WHEN (vtiger_products.currency_id = 1 ) THEN vtiger_products.unit_price
1228                                  ELSE (vtiger_products.unit_price / vtiger_currency_info.conversion_rate) END
1229                              ) AS actual_unit_price
1230                      FROM vtiger_products
1231                      LEFT JOIN vtiger_currency_info ON vtiger_products.currency_id = vtiger_currency_info.id
1232                      LEFT JOIN vtiger_productcurrencyrel ON vtiger_products.productid = vtiger_productcurrencyrel.productid
1233                      AND vtiger_productcurrencyrel.currencyid = ". $current_user->currency_id . "
1234                  ) AS innerProduct ON innerProduct.productid = vtiger_products.productid";
1235          }
1236          if ($queryplanner->requireTable("vtiger_crmentityProducts")){
1237              $query .= " left join vtiger_crmentity as vtiger_crmentityProducts on vtiger_crmentityProducts.crmid=vtiger_products.productid and vtiger_crmentityProducts.deleted=0";
1238          }
1239          if ($queryplanner->requireTable("vtiger_productcf")){
1240              $query .= " left join vtiger_productcf on vtiger_products.productid = vtiger_productcf.productid";
1241          }
1242              if ($queryplanner->requireTable("vtiger_groupsProducts")){
1243              $query .= " left join vtiger_groups as vtiger_groupsProducts on vtiger_groupsProducts.groupid = vtiger_crmentityProducts.smownerid";
1244          }
1245          if ($queryplanner->requireTable("vtiger_usersProducts")){
1246              $query .= " left join vtiger_users as vtiger_usersProducts on vtiger_usersProducts.id = vtiger_crmentityProducts.smownerid";
1247          }
1248          if ($queryplanner->requireTable("vtiger_vendorRelProducts")){
1249              $query .= " left join vtiger_vendor as vtiger_vendorRelProducts on vtiger_vendorRelProducts.vendorid = vtiger_products.vendor_id";
1250          }
1251          if ($queryplanner->requireTable("vtiger_lastModifiedByProducts")){
1252              $query .= " left join vtiger_users as vtiger_lastModifiedByProducts on vtiger_lastModifiedByProducts.id = vtiger_crmentityProducts.modifiedby ";
1253          }
1254          if ($queryplanner->requireTable("vtiger_createdbyProducts")){
1255              $query .= " left join vtiger_users as vtiger_createdbyProducts on vtiger_createdbyProducts.id = vtiger_crmentityProducts.smcreatorid ";
1256          }
1257          return $query;
1258      }
1259  
1260      /*
1261       * Function to get the relation tables for related modules
1262       * @param - $secmodule secondary module name
1263       * returns the array with table names and fieldnames storing relations between module and this module
1264       */
1265  	function setRelationTables($secmodule){
1266          $rel_tables = array (
1267              "HelpDesk" => array("vtiger_troubletickets"=>array("product_id","ticketid"),"vtiger_products"=>"productid"),
1268              "Quotes" => array("vtiger_inventoryproductrel"=>array("productid","id"),"vtiger_products"=>"productid"),
1269              "PurchaseOrder" => array("vtiger_inventoryproductrel"=>array("productid","id"),"vtiger_products"=>"productid"),
1270              "SalesOrder" => array("vtiger_inventoryproductrel"=>array("productid","id"),"vtiger_products"=>"productid"),
1271              "Invoice" => array("vtiger_inventoryproductrel"=>array("productid","id"),"vtiger_products"=>"productid"),
1272              "Leads" => array("vtiger_seproductsrel"=>array("productid","crmid"),"vtiger_products"=>"productid"),
1273              "Accounts" => array("vtiger_seproductsrel"=>array("productid","crmid"),"vtiger_products"=>"productid"),
1274              "Contacts" => array("vtiger_seproductsrel"=>array("productid","crmid"),"vtiger_products"=>"productid"),
1275              "Potentials" => array("vtiger_seproductsrel"=>array("productid","crmid"),"vtiger_products"=>"productid"),
1276              "Products" => array("vtiger_products"=>array("productid","product_id"),"vtiger_products"=>"productid"),
1277              "PriceBooks" => array("vtiger_pricebookproductrel"=>array("productid","pricebookid"),"vtiger_products"=>"productid"),
1278              "Documents" => array("vtiger_senotesrel"=>array("crmid","notesid"),"vtiger_products"=>"productid"),
1279          );
1280          return $rel_tables[$secmodule];
1281      }
1282  
1283  	function deleteProduct2ProductRelation($record,$return_id,$is_parent){
1284          global $adb;
1285          if($is_parent==0){
1286              $sql = "delete from vtiger_seproductsrel WHERE crmid = ? AND productid = ?";
1287              $adb->pquery($sql, array($record,$return_id));
1288          } else {
1289              $sql = "delete from vtiger_seproductsrel WHERE crmid = ? AND productid = ?";
1290              $adb->pquery($sql, array($return_id,$record));
1291          }
1292      }
1293  
1294      // Function to unlink all the dependent entities of the given Entity by Id
1295  	function unlinkDependencies($module, $id) {
1296          global $log;
1297          //Backup Campaigns-Product Relation
1298          $cmp_q = 'SELECT campaignid FROM vtiger_campaign WHERE product_id = ?';
1299          $cmp_res = $this->db->pquery($cmp_q, array($id));
1300          if ($this->db->num_rows($cmp_res) > 0) {
1301              $cmp_ids_list = array();
1302              for($k=0;$k < $this->db->num_rows($cmp_res);$k++)
1303              {
1304                  $cmp_ids_list[] = $this->db->query_result($cmp_res,$k,"campaignid");
1305              }
1306              $params = array($id, RB_RECORD_UPDATED, 'vtiger_campaign', 'product_id', 'campaignid', implode(",", $cmp_ids_list));
1307              $this->db->pquery('INSERT INTO vtiger_relatedlists_rb VALUES (?,?,?,?,?,?)', $params);
1308          }
1309          //we have to update the product_id as null for the campaigns which are related to this product
1310          $this->db->pquery('UPDATE vtiger_campaign SET product_id=0 WHERE product_id = ?', array($id));
1311  
1312          $this->db->pquery('DELETE from vtiger_seproductsrel WHERE productid=? or crmid=?',array($id,$id));
1313  
1314          parent::unlinkDependencies($module, $id);
1315      }
1316  
1317      // Function to unlink an entity with given Id from another entity
1318  	function unlinkRelationship($id, $return_module, $return_id) {
1319          global $log;
1320          if(empty($return_module) || empty($return_id)) return;
1321  
1322          if($return_module == 'Calendar') {
1323              $sql = 'DELETE FROM vtiger_seactivityrel WHERE crmid = ? AND activityid = ?';
1324              $this->db->pquery($sql, array($id, $return_id));
1325          } elseif($return_module == 'Leads' || $return_module == 'Contacts' || $return_module == 'Potentials') {
1326              $sql = 'DELETE FROM vtiger_seproductsrel WHERE productid = ? AND crmid = ?';
1327              $this->db->pquery($sql, array($id, $return_id));
1328          } elseif($return_module == 'Vendors') {
1329              $sql = 'UPDATE vtiger_products SET vendor_id = ? WHERE productid = ?';
1330              $this->db->pquery($sql, array(null, $id));
1331          } elseif($return_module == 'Accounts') {
1332              $sql = 'DELETE FROM vtiger_seproductsrel WHERE productid = ? AND (crmid = ? OR crmid IN (SELECT contactid FROM vtiger_contactdetails WHERE accountid=?))';
1333              $param = array($id, $return_id,$return_id);
1334              $this->db->pquery($sql, $param);
1335          } else {
1336              $sql = 'DELETE FROM vtiger_crmentityrel WHERE (crmid=? AND relmodule=? AND relcrmid=?) OR (relcrmid=? AND module=? AND crmid=?)';
1337              $params = array($id, $return_module, $return_id, $id, $return_module, $return_id);
1338              $this->db->pquery($sql, $params);
1339          }
1340      }
1341  
1342  	function save_related_module($module, $crmid, $with_module, $with_crmids) {
1343          $adb = PearDatabase::getInstance();
1344  
1345          if(!is_array($with_crmids)) $with_crmids = Array($with_crmids);
1346          foreach($with_crmids as $with_crmid) {
1347              if($with_module == 'Leads' || $with_module == 'Accounts' ||
1348                      $with_module == 'Contacts' || $with_module == 'Potentials' || $with_module == 'Products'){
1349                  $query = $adb->pquery("SELECT * from vtiger_seproductsrel WHERE crmid=? and productid=?",array($crmid, $with_crmid));
1350                  if($adb->num_rows($query)==0){
1351                      $adb->pquery("insert into vtiger_seproductsrel values (?,?,?)", array($with_crmid, $crmid, $with_module));
1352                  }
1353              }
1354              else {
1355                  parent::save_related_module($module, $crmid, $with_module, $with_crmid);
1356              }
1357          }
1358      }
1359  
1360  }
1361  ?>


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