[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Vendors/ -> Vendors.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   ********************************************************************************/
  11  class Vendors extends CRMEntity {
  12      var $log;
  13      var $db;
  14      var $table_name = "vtiger_vendor";
  15      var $table_index= 'vendorid';
  16      var $tab_name = Array('vtiger_crmentity','vtiger_vendor','vtiger_vendorcf');
  17      var $tab_name_index = Array('vtiger_crmentity'=>'crmid','vtiger_vendor'=>'vendorid','vtiger_vendorcf'=>'vendorid');
  18      /**
  19       * Mandatory table for supporting custom fields.
  20       */
  21      var $customFieldTable = Array('vtiger_vendorcf', 'vendorid');
  22      var $column_fields = Array();
  23  
  24          //Pavani: Assign value to entity_table
  25          var $entity_table = "vtiger_crmentity";
  26          var $sortby_fields = Array('vendorname','category');
  27  
  28          // This is the list of vtiger_fields that are in the lists.
  29      var $list_fields = Array(
  30                                  'Vendor Name'=>Array('vendor'=>'vendorname'),
  31                                  'Phone'=>Array('vendor'=>'phone'),
  32                                  'Email'=>Array('vendor'=>'email'),
  33                                  'Category'=>Array('vendor'=>'category')
  34                                  );
  35          var $list_fields_name = Array(
  36                                          'Vendor Name'=>'vendorname',
  37                                          'Phone'=>'phone',
  38                                          'Email'=>'email',
  39                                          'Category'=>'category'
  40                                       );
  41          var $list_link_field= 'vendorname';
  42  
  43      var $search_fields = Array(
  44                                  'Vendor Name'=>Array('vendor'=>'vendorname'),
  45                                  'Phone'=>Array('vendor'=>'phone')
  46                                  );
  47          var $search_fields_name = Array(
  48                                          'Vendor Name'=>'vendorname',
  49                                          'Phone'=>'phone'
  50                                       );
  51      //Specifying required fields for vendors
  52          var $required_fields =  array();
  53  
  54      // Used when enabling/disabling the mandatory fields for the module.
  55      // Refers to vtiger_field.fieldname values.
  56      var $mandatory_fields = Array('createdtime', 'modifiedtime', 'vendorname', 'assigned_user_id');
  57  
  58      //Added these variables which are used as default order by and sortorder in ListView
  59      var $default_order_by = 'vendorname';
  60      var $default_sort_order = 'ASC';
  61  
  62      // For Alphabetical search
  63      var $def_basicsearch_col = 'vendorname';
  64  
  65      /**    Constructor which will set the column_fields in this object
  66       */
  67  	function Vendors() {
  68          $this->log =LoggerManager::getLogger('vendor');
  69          $this->log->debug("Entering Vendors() method ...");
  70          $this->db = PearDatabase::getInstance();
  71          $this->column_fields = getColumnFields('Vendors');
  72          $this->log->debug("Exiting Vendor method ...");
  73      }
  74  
  75  	function save_module($module)
  76      {
  77      }
  78  
  79      /**    function used to get the list of products which are related to the vendor
  80       *    @param int $id - vendor id
  81       *    @return array - array which will be returned from the function GetRelatedList
  82       */
  83  	function get_products($id, $cur_tab_id, $rel_tab_id, $actions=false) {
  84          global $log, $singlepane_view,$currentModule,$current_user;
  85          $log->debug("Entering get_products(".$id.") method ...");
  86          $this_module = $currentModule;
  87  
  88          $related_module = vtlib_getModuleNameById($rel_tab_id);
  89          checkFileAccessForInclusion("modules/$related_module/$related_module.php");
  90          require_once("modules/$related_module/$related_module.php");
  91          $other = new $related_module();
  92          vtlib_setup_modulevars($related_module, $other);
  93          $singular_modname = vtlib_toSingular($related_module);
  94  
  95          $parenttab = getParentTab();
  96  
  97          if($singlepane_view == 'true')
  98              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
  99          else
 100              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 101  
 102          $button = '';
 103  
 104          if($actions) {
 105              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 106              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 107                  $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;";
 108              }
 109              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 110                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 111                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\";this.form.parent_id.value=\"\";' type='submit' name='button'" .
 112                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>";
 113              }
 114          }
 115  
 116          $query = "SELECT vtiger_products.productid, vtiger_products.productname, vtiger_products.productcode,
 117                      vtiger_products.commissionrate, vtiger_products.qty_per_unit, vtiger_products.unit_price,
 118                      vtiger_crmentity.crmid, vtiger_crmentity.smownerid,vtiger_vendor.vendorname
 119                        FROM vtiger_products
 120                        INNER JOIN vtiger_vendor ON vtiger_vendor.vendorid = vtiger_products.vendor_id
 121                        INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid INNER JOIN vtiger_productcf
 122                      ON vtiger_products.productid = vtiger_productcf.productid
 123                      LEFT JOIN vtiger_users
 124                          ON vtiger_users.id=vtiger_crmentity.smownerid
 125                      LEFT JOIN vtiger_groups
 126                          ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 127                        WHERE vtiger_crmentity.deleted = 0 AND vtiger_vendor.vendorid = $id";
 128  
 129          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 130  
 131          if($return_value == null) $return_value = Array();
 132          $return_value['CUSTOM_BUTTON'] = $button;
 133  
 134          $log->debug("Exiting get_products method ...");
 135          return $return_value;
 136      }
 137  
 138      /**    function used to get the list of purchase orders which are related to the vendor
 139       *    @param int $id - vendor id
 140       *    @return array - array which will be returned from the function GetRelatedList
 141       */
 142  	function get_purchase_orders($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 143          global $log, $singlepane_view,$currentModule,$current_user;
 144          $log->debug("Entering get_purchase_orders(".$id.") method ...");
 145          $this_module = $currentModule;
 146  
 147          $related_module = vtlib_getModuleNameById($rel_tab_id);
 148          checkFileAccessForInclusion("modules/$related_module/$related_module.php");
 149          require_once("modules/$related_module/$related_module.php");
 150          $other = new $related_module();
 151          vtlib_setup_modulevars($related_module, $other);
 152          $singular_modname = vtlib_toSingular($related_module);
 153  
 154          $parenttab = getParentTab();
 155  
 156          if($singlepane_view == 'true')
 157              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 158          else
 159              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 160  
 161          $button = '';
 162  
 163          if($actions) {
 164              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 165              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 166                  $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;";
 167              }
 168              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 169                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 170                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 171                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>";
 172              }
 173          }
 174  
 175          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 176                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 177          $query = "select case when (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname end as user_name,vtiger_crmentity.*, vtiger_purchaseorder.*,vtiger_vendor.vendorname from vtiger_purchaseorder inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_purchaseorder.purchaseorderid left outer join vtiger_vendor on vtiger_purchaseorder.vendorid=vtiger_vendor.vendorid LEFT JOIN vtiger_purchaseordercf ON vtiger_purchaseordercf.purchaseorderid = vtiger_purchaseorder.purchaseorderid LEFT JOIN vtiger_pobillads ON vtiger_pobillads.pobilladdressid = vtiger_purchaseorder.purchaseorderid LEFT JOIN vtiger_poshipads ON vtiger_poshipads.poshipaddressid = vtiger_purchaseorder.purchaseorderid  left join vtiger_groups on vtiger_groups.groupid=vtiger_crmentity.smownerid left join vtiger_users on vtiger_users.id=vtiger_crmentity.smownerid where vtiger_crmentity.deleted=0 and vtiger_purchaseorder.vendorid=".$id;
 178  
 179          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 180  
 181          if($return_value == null) $return_value = Array();
 182          $return_value['CUSTOM_BUTTON'] = $button;
 183  
 184          $log->debug("Exiting get_purchase_orders method ...");
 185          return $return_value;
 186      }
 187      //Pavani: Function to create, export query for vendors module
 188          /** Function to export the vendors in CSV Format
 189          * @param reference variable - where condition is passed when the query is executed
 190          * Returns Export Vendors Query.
 191          */
 192          function create_export_query($where)
 193          {
 194                  global $log;
 195                  global $current_user;
 196                  $log->debug("Entering create_export_query(".$where.") method ...");
 197  
 198                  include ("include/utils/ExportUtils.php");
 199  
 200                  //To get the Permitted fields query and the permitted fields list
 201                  $sql = getPermittedFieldsQuery("Vendors", "detail_view");
 202                  $fields_list = getFieldsListFromQuery($sql);
 203  
 204                  $query = "SELECT $fields_list FROM ".$this->entity_table."
 205                                  INNER JOIN vtiger_vendor
 206                                          ON vtiger_crmentity.crmid = vtiger_vendor.vendorid
 207                                  LEFT JOIN vtiger_vendorcf
 208                                          ON vtiger_vendorcf.vendorid=vtiger_vendor.vendorid
 209                                  LEFT JOIN vtiger_seattachmentsrel
 210                                          ON vtiger_vendor.vendorid=vtiger_seattachmentsrel.crmid
 211                                  LEFT JOIN vtiger_attachments
 212                                  ON vtiger_seattachmentsrel.attachmentsid = vtiger_attachments.attachmentsid
 213                                  LEFT JOIN vtiger_users
 214                                          ON vtiger_crmentity.smownerid = vtiger_users.id and vtiger_users.status='Active'
 215                                  ";
 216                  $where_auto = " vtiger_crmentity.deleted = 0 ";
 217  
 218                   if($where != "")
 219                     $query .= "  WHERE ($where) AND ".$where_auto;
 220                  else
 221                     $query .= "  WHERE ".$where_auto;
 222  
 223                  $log->debug("Exiting create_export_query method ...");
 224                  return $query;
 225          }
 226  
 227      /**    function used to get the list of contacts which are related to the vendor
 228       *    @param int $id - vendor id
 229       *    @return array - array which will be returned from the function GetRelatedList
 230       */
 231  	function get_contacts($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 232          global $log, $singlepane_view,$currentModule,$current_user;
 233          $log->debug("Entering get_contacts(".$id.") method ...");
 234          $this_module = $currentModule;
 235  
 236          $related_module = vtlib_getModuleNameById($rel_tab_id);
 237          checkFileAccessForInclusion("modules/$related_module/$related_module.php");
 238          require_once("modules/$related_module/$related_module.php");
 239          $other = new $related_module();
 240          vtlib_setup_modulevars($related_module, $other);
 241          $singular_modname = vtlib_toSingular($related_module);
 242  
 243          $parenttab = getParentTab();
 244  
 245          if($singlepane_view == 'true')
 246              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 247          else
 248              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 249  
 250          $button = '';
 251  
 252          if($actions) {
 253              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 254              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 255                  $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;";
 256              }
 257              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 258                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 259                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 260                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 261              }
 262          }
 263  
 264          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 265                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 266          $query = "SELECT case when (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname end as user_name,vtiger_contactdetails.*, vtiger_crmentity.crmid, vtiger_crmentity.smownerid,vtiger_vendorcontactrel.vendorid,vtiger_account.accountname from vtiger_contactdetails
 267                  inner join vtiger_crmentity on vtiger_crmentity.crmid = vtiger_contactdetails.contactid
 268                  inner join vtiger_vendorcontactrel on vtiger_vendorcontactrel.contactid=vtiger_contactdetails.contactid
 269                  INNER JOIN vtiger_contactaddress ON vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid
 270                  INNER JOIN vtiger_contactsubdetails ON vtiger_contactdetails.contactid = vtiger_contactsubdetails.contactsubscriptionid
 271                  INNER JOIN vtiger_customerdetails ON vtiger_contactdetails.contactid = vtiger_customerdetails.customerid
 272                  INNER JOIN vtiger_contactscf ON vtiger_contactdetails.contactid = vtiger_contactscf.contactid
 273                  left join vtiger_groups on vtiger_groups.groupid=vtiger_crmentity.smownerid
 274                  left join vtiger_account on vtiger_account.accountid = vtiger_contactdetails.accountid
 275                  left join vtiger_users on vtiger_users.id=vtiger_crmentity.smownerid
 276                  where vtiger_crmentity.deleted=0 and vtiger_vendorcontactrel.vendorid = ".$id;
 277  
 278          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 279  
 280          if($return_value == null) $return_value = Array();
 281          $return_value['CUSTOM_BUTTON'] = $button;
 282  
 283          $log->debug("Exiting get_contacts method ...");
 284          return $return_value;
 285      }
 286  
 287      /**
 288       * Move the related records of the specified list of id's to the given record.
 289       * @param String This module name
 290       * @param Array List of Entity Id's from which related records need to be transfered
 291       * @param Integer Id of the the Record to which the related records are to be moved
 292       */
 293  	function transferRelatedRecords($module, $transferEntityIds, $entityId) {
 294          global $adb,$log;
 295          $log->debug("Entering function transferRelatedRecords ($module, $transferEntityIds, $entityId)");
 296  
 297          $rel_table_arr = Array("Products"=>"vtiger_products","PurchaseOrder"=>"vtiger_purchaseorder","Contacts"=>"vtiger_vendorcontactrel");
 298  
 299          $tbl_field_arr = Array("vtiger_products"=>"productid","vtiger_vendorcontactrel"=>"contactid","vtiger_purchaseorder"=>"purchaseorderid");
 300  
 301          $entity_tbl_field_arr = Array("vtiger_products"=>"vendor_id","vtiger_vendorcontactrel"=>"vendorid","vtiger_purchaseorder"=>"vendorid");
 302  
 303          foreach($transferEntityIds as $transferId) {
 304              foreach($rel_table_arr as $rel_module=>$rel_table) {
 305                  $id_field = $tbl_field_arr[$rel_table];
 306                  $entity_id_field = $entity_tbl_field_arr[$rel_table];
 307                  // IN clause to avoid duplicate entries
 308                  $sel_result =  $adb->pquery("select $id_field from $rel_table where $entity_id_field=? " .
 309                          " and $id_field not in (select $id_field from $rel_table where $entity_id_field=?)",
 310                          array($transferId,$entityId));
 311                  $res_cnt = $adb->num_rows($sel_result);
 312                  if($res_cnt > 0) {
 313                      for($i=0;$i<$res_cnt;$i++) {
 314                          $id_field_value = $adb->query_result($sel_result,$i,$id_field);
 315                          $adb->pquery("update $rel_table set $entity_id_field=? where $entity_id_field=? and $id_field=?",
 316                              array($entityId,$transferId,$id_field_value));
 317                      }
 318                  }
 319              }
 320          }
 321          $log->debug("Exiting transferRelatedRecords...");
 322      }
 323  
 324      /** Returns a list of the associated emails
 325       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 326       * All Rights Reserved..
 327       * Contributor(s): ______________________________________..
 328      */
 329  	function get_emails($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 330          global $log, $singlepane_view,$currentModule,$current_user;
 331          $log->debug("Entering get_emails(".$id.") method ...");
 332          $this_module = $currentModule;
 333  
 334          $related_module = vtlib_getModuleNameById($rel_tab_id);
 335          checkFileAccessForInclusion("modules/$related_module/$related_module.php");
 336          require_once("modules/$related_module/$related_module.php");
 337          $other = new $related_module();
 338          vtlib_setup_modulevars($related_module, $other);
 339          $singular_modname = vtlib_toSingular($related_module);
 340  
 341          $parenttab = getParentTab();
 342  
 343          if($singlepane_view == 'true')
 344              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 345          else
 346              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 347  
 348          $button = '';
 349  
 350          $button .= '<input type="hidden" name="email_directing_module"><input type="hidden" name="record">';
 351  
 352          if($actions) {
 353              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 354              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 355                  $button .= "<input title='". getTranslatedString('LBL_ADD_NEW')." ". getTranslatedString($singular_modname)."' accessyKey='F' class='crmbutton small create' onclick='fnvshobj(this,\"sendmail_cont\");sendmail(\"$this_module\",$id);' type='button' name='button' value='". getTranslatedString('LBL_ADD_NEW')." ". getTranslatedString($singular_modname)."'></td>";
 356              }
 357          }
 358  
 359          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 360                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 361          $query = "SELECT case when (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname end as user_name,
 362              vtiger_activity.activityid, vtiger_activity.subject,
 363              vtiger_activity.activitytype, vtiger_crmentity.modifiedtime,
 364              vtiger_crmentity.crmid, vtiger_crmentity.smownerid, vtiger_activity.date_start,vtiger_activity.time_start, vtiger_seactivityrel.crmid as parent_id
 365              FROM vtiger_activity, vtiger_seactivityrel, vtiger_vendor, vtiger_users, vtiger_crmentity
 366              LEFT JOIN vtiger_groups
 367                  ON vtiger_groups.groupid=vtiger_crmentity.smownerid
 368              WHERE vtiger_seactivityrel.activityid = vtiger_activity.activityid
 369                  AND vtiger_vendor.vendorid = vtiger_seactivityrel.crmid
 370                  AND vtiger_users.id=vtiger_crmentity.smownerid
 371                  AND vtiger_crmentity.crmid = vtiger_activity.activityid
 372                  AND vtiger_vendor.vendorid = ".$id."
 373                  AND vtiger_activity.activitytype='Emails'
 374                  AND vtiger_crmentity.deleted = 0";
 375  
 376          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 377  
 378          if($return_value == null) $return_value = Array();
 379          $return_value['CUSTOM_BUTTON'] = $button;
 380  
 381          $log->debug("Exiting get_emails method ...");
 382          return $return_value;
 383      }
 384  
 385      /*
 386       * Function to get the primary query part of a report
 387       * @param - $module Primary module name
 388       * returns the query string formed on fetching the related data for report for primary module
 389       */
 390  	function generateReportsQuery($module, $queryPlanner) {
 391          $moduletable = $this->table_name;
 392          $moduleindex = $this->table_index;
 393          $modulecftable = $this->tab_name[2];
 394          $modulecfindex = $this->tab_name_index[$modulecftable];
 395  
 396          $query = "from $moduletable
 397              inner join $modulecftable as $modulecftable on $modulecftable.$modulecfindex=$moduletable.$moduleindex
 398              inner join vtiger_crmentity on vtiger_crmentity.crmid=$moduletable.$moduleindex
 399              left join vtiger_groups as vtiger_groups$module on vtiger_groups$module.groupid = vtiger_crmentity.smownerid
 400              left join vtiger_users as vtiger_users".$module." on vtiger_users".$module.".id = vtiger_crmentity.smownerid
 401              left join vtiger_groups on vtiger_groups.groupid = vtiger_crmentity.smownerid
 402              left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid
 403              left join vtiger_users as vtiger_lastModifiedByVendors on vtiger_lastModifiedByVendors.id = vtiger_crmentity.modifiedby ";
 404          return $query;
 405      }
 406  
 407      /*
 408       * Function to get the secondary query part of a report
 409       * @param - $module primary module name
 410       * @param - $secmodule secondary module name
 411       * returns the query string formed on fetching the related data for report for secondary module
 412       */
 413  	function generateReportsSecQuery($module,$secmodule, $queryplanner) {
 414  
 415          $matrix = $queryplanner->newDependencyMatrix();
 416  
 417          $matrix->setDependency("vtiger_crmentityVendors",array("vtiger_usersVendors","vtiger_lastModifiedByVendors"));
 418          $matrix->setDependency("vtiger_vendor",array("vtiger_crmentityVendors","vtiger_vendorcf","vtiger_email_trackVendors"));
 419          if (!$queryplanner->requireTable('vtiger_vendor', $matrix)) {
 420              return '';
 421          }
 422          $query = $this->getRelationQuery($module,$secmodule,"vtiger_vendor","vendorid", $queryplanner);
 423          // TODO Support query planner
 424          if ($queryplanner->requireTable("vtiger_crmentityVendors",$matrix)){
 425              $query .=" left join vtiger_crmentity as vtiger_crmentityVendors on vtiger_crmentityVendors.crmid=vtiger_vendor.vendorid and vtiger_crmentityVendors.deleted=0";
 426          }
 427          if ($queryplanner->requireTable("vtiger_vendorcf")){
 428              $query .=" left join vtiger_vendorcf on vtiger_vendorcf.vendorid = vtiger_crmentityVendors.crmid";
 429          }
 430          if ($queryplanner->requireTable("vtiger_email_trackVendors")){
 431              $query .=" LEFT JOIN vtiger_email_track AS vtiger_email_trackVendors ON vtiger_email_trackVendors.crmid = vtiger_vendor.vendorid";
 432          }
 433          if ($queryplanner->requireTable("vtiger_usersVendors")){
 434              $query .=" left join vtiger_users as vtiger_usersVendors on vtiger_usersVendors.id = vtiger_crmentityVendors.smownerid";
 435          }
 436          if ($queryplanner->requireTable("vtiger_lastModifiedByVendors")){
 437              $query .=" left join vtiger_users as vtiger_lastModifiedByVendors on vtiger_lastModifiedByVendors.id = vtiger_crmentityVendors.modifiedby ";
 438          }
 439          if ($queryplanner->requireTable("vtiger_createdbyVendors")){
 440              $query .= " left join vtiger_users as vtiger_createdbyVendors on vtiger_createdbyVendors.id = vtiger_crmentityVendors.smcreatorid ";
 441          }
 442          return $query;
 443      }
 444  
 445      /*
 446       * Function to get the relation tables for related modules
 447       * @param - $secmodule secondary module name
 448       * returns the array with table names and fieldnames storing relations between module and this module
 449       */
 450  	function setRelationTables($secmodule){
 451          $rel_tables = array (
 452              "Products" =>array("vtiger_products"=>array("vendor_id","productid"),"vtiger_vendor"=>"vendorid"),
 453              "PurchaseOrder" =>array("vtiger_purchaseorder"=>array("vendorid","purchaseorderid"),"vtiger_vendor"=>"vendorid"),
 454              "Contacts" =>array("vtiger_vendorcontactrel"=>array("vendorid","contactid"),"vtiger_vendor"=>"vendorid"),
 455              "Emails" => array("vtiger_seactivityrel"=>array("crmid","activityid"),"vtiger_vendor"=>"vendorid"),
 456          );
 457          return $rel_tables[$secmodule];
 458      }
 459  
 460      // Function to unlink all the dependent entities of the given Entity by Id
 461  	function unlinkDependencies($module, $id) {
 462          global $log;
 463          //Deleting Vendor related PO.
 464          $po_q = 'SELECT vtiger_crmentity.crmid FROM vtiger_crmentity
 465              INNER JOIN vtiger_purchaseorder ON vtiger_crmentity.crmid=vtiger_purchaseorder.purchaseorderid
 466              INNER JOIN vtiger_vendor ON vtiger_vendor.vendorid=vtiger_purchaseorder.vendorid
 467              WHERE vtiger_crmentity.deleted=0 AND vtiger_purchaseorder.vendorid=?';
 468          $po_res = $this->db->pquery($po_q, array($id));
 469          $po_ids_list = array();
 470          for($k=0;$k < $this->db->num_rows($po_res);$k++)
 471          {
 472              $po_id = $this->db->query_result($po_res,$k,"crmid");
 473              $po_ids_list[] = $po_id;
 474              $sql = 'UPDATE vtiger_crmentity SET deleted = 1 WHERE crmid = ?';
 475              $this->db->pquery($sql, array($po_id));
 476          }
 477          //Backup deleted Vendors related Potentials.
 478          $params = array($id, RB_RECORD_UPDATED, 'vtiger_crmentity', 'deleted', 'crmid', implode(",", $po_ids_list));
 479          $this->db->pquery('INSERT INTO vtiger_relatedlists_rb VALUES (?,?,?,?,?,?)', $params);
 480  
 481          //Backup Product-Vendor Relation
 482          $pro_q = 'SELECT productid FROM vtiger_products WHERE vendor_id=?';
 483          $pro_res = $this->db->pquery($pro_q, array($id));
 484          if ($this->db->num_rows($pro_res) > 0) {
 485              $pro_ids_list = array();
 486              for($k=0;$k < $this->db->num_rows($pro_res);$k++)
 487              {
 488                  $pro_ids_list[] = $this->db->query_result($pro_res,$k,"productid");
 489              }
 490              $params = array($id, RB_RECORD_UPDATED, 'vtiger_products', 'vendor_id', 'productid', implode(",", $pro_ids_list));
 491              $this->db->pquery('INSERT INTO vtiger_relatedlists_rb VALUES (?,?,?,?,?,?)', $params);
 492          }
 493          //Deleting Product-Vendor Relation.
 494          $pro_q = 'UPDATE vtiger_products SET vendor_id = 0 WHERE vendor_id = ?';
 495          $this->db->pquery($pro_q, array($id));
 496  
 497          /*//Backup Contact-Vendor Relaton
 498          $con_q = 'SELECT contactid FROM vtiger_vendorcontactrel WHERE vendorid = ?';
 499          $con_res = $this->db->pquery($con_q, array($id));
 500          if ($this->db->num_rows($con_res) > 0) {
 501              for($k=0;$k < $this->db->num_rows($con_res);$k++)
 502              {
 503                  $con_id = $this->db->query_result($con_res,$k,"contactid");
 504                  $params = array($id, RB_RECORD_DELETED, 'vtiger_vendorcontactrel', 'vendorid', 'contactid', $con_id);
 505                  $this->db->pquery('INSERT INTO vtiger_relatedlists_rb VALUES (?,?,?,?,?,?)', $params);
 506              }
 507          }
 508          //Deleting Contact-Vendor Relaton
 509          $vc_sql = 'DELETE FROM vtiger_vendorcontactrel WHERE vendorid=?';
 510          $this->db->pquery($vc_sql, array($id));*/
 511  
 512          parent::unlinkDependencies($module, $id);
 513      }
 514  
 515  	function save_related_module($module, $crmid, $with_module, $with_crmids) {
 516          $adb = PearDatabase::getInstance();
 517  
 518          if(!is_array($with_crmids)) $with_crmids = Array($with_crmids);
 519          foreach($with_crmids as $with_crmid) {
 520              if($with_module == 'Contacts')
 521                  $adb->pquery("insert into vtiger_vendorcontactrel values (?,?)", array($crmid, $with_crmid));
 522              elseif($with_module == 'Products')
 523                  $adb->pquery("update vtiger_products set vendor_id=? where productid=?", array($crmid, $with_crmid));
 524              else {
 525                  parent::save_related_module($module, $crmid, $with_module, $with_crmid);
 526              }
 527          }
 528      }
 529  
 530      // Function to unlink an entity with given Id from another entity
 531  	function unlinkRelationship($id, $return_module, $return_id) {
 532          global $log;
 533          if(empty($return_module) || empty($return_id)) return;
 534          if($return_module == 'Contacts') {
 535              $sql = 'DELETE FROM vtiger_vendorcontactrel WHERE vendorid=? AND contactid=?';
 536              $this->db->pquery($sql, array($id,$return_id));
 537          } else {
 538              parent::unlinkRelationship($id, $return_module, $return_id);
 539          }
 540      }
 541  
 542  }
 543  ?>


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