[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

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

   1  <?php
   2  /*********************************************************************************
   3   * The contents of this file are subject to the SugarCRM Public License Version 1.1.2
   4   * ("License"); You may not use this file except in compliance with the
   5   * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
   6   * Software distributed under the License is distributed on an  "AS IS"  basis,
   7   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
   8   * the specific language governing rights and limitations under the License.
   9   * The Original Code is:  SugarCRM Open Source
  10   * The Initial Developer of the Original Code is SugarCRM, Inc.
  11   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.;
  12   * All Rights Reserved.
  13   * Contributor(s): ______________________________________.
  14   ********************************************************************************/
  15  /*********************************************************************************
  16   * $Header: /advent/projects/wesat/vtiger_crm/sugarcrm/modules/Potentials/Potentials.php,v 1.65 2005/04/28 08:08:27 rank Exp $
  17   * Description:  TODO: To be written.
  18   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  19   * All Rights Reserved.
  20   * Contributor(s): ______________________________________..
  21   ********************************************************************************/
  22  
  23  class Potentials extends CRMEntity {
  24      var $log;
  25      var $db;
  26  
  27      var $module_name="Potentials";
  28      var $table_name = "vtiger_potential";
  29      var $table_index= 'potentialid';
  30  
  31      var $tab_name = Array('vtiger_crmentity','vtiger_potential','vtiger_potentialscf');
  32      var $tab_name_index = Array('vtiger_crmentity'=>'crmid','vtiger_potential'=>'potentialid','vtiger_potentialscf'=>'potentialid');
  33      /**
  34       * Mandatory table for supporting custom fields.
  35       */
  36      var $customFieldTable = Array('vtiger_potentialscf', 'potentialid');
  37  
  38      var $column_fields = Array();
  39  
  40      var $sortby_fields = Array('potentialname','amount','closingdate','smownerid','accountname');
  41  
  42      // This is the list of vtiger_fields that are in the lists.
  43      var $list_fields = Array(
  44              'Potential'=>Array('potential'=>'potentialname'),
  45              'Organization Name'=>Array('potential'=>'related_to'),
  46              'Contact Name'=>Array('potential'=>'contact_id'),
  47              'Sales Stage'=>Array('potential'=>'sales_stage'),
  48              'Amount'=>Array('potential'=>'amount'),
  49              'Expected Close Date'=>Array('potential'=>'closingdate'),
  50              'Assigned To'=>Array('crmentity','smownerid')
  51              );
  52  
  53      var $list_fields_name = Array(
  54              'Potential'=>'potentialname',
  55              'Organization Name'=>'related_to',
  56              'Contact Name'=>'contact_id',
  57              'Sales Stage'=>'sales_stage',
  58              'Amount'=>'amount',
  59              'Expected Close Date'=>'closingdate',
  60              'Assigned To'=>'assigned_user_id');
  61  
  62      var $list_link_field= 'potentialname';
  63  
  64      var $search_fields = Array(
  65              'Potential'=>Array('potential'=>'potentialname'),
  66              'Related To'=>Array('potential'=>'related_to'),
  67              'Expected Close Date'=>Array('potential'=>'closedate')
  68              );
  69  
  70      var $search_fields_name = Array(
  71              'Potential'=>'potentialname',
  72              'Related To'=>'related_to',
  73              'Expected Close Date'=>'closingdate'
  74              );
  75  
  76      var $required_fields =  array();
  77  
  78      // Used when enabling/disabling the mandatory fields for the module.
  79      // Refers to vtiger_field.fieldname values.
  80      var $mandatory_fields = Array('assigned_user_id', 'createdtime', 'modifiedtime', 'potentialname');
  81  
  82      //Added these variables which are used as default order by and sortorder in ListView
  83      var $default_order_by = 'potentialname';
  84      var $default_sort_order = 'ASC';
  85  
  86      // For Alphabetical search
  87      var $def_basicsearch_col = 'potentialname';
  88  
  89      //var $groupTable = Array('vtiger_potentialgrouprelation','potentialid');
  90  	function Potentials() {
  91          $this->log = LoggerManager::getLogger('potential');
  92          $this->db = PearDatabase::getInstance();
  93          $this->column_fields = getColumnFields('Potentials');
  94      }
  95  
  96  	function save_module($module)
  97      {
  98      }
  99  
 100      /** Function to create list query
 101      * @param reference variable - where condition is passed when the query is executed
 102      * Returns Query.
 103      */
 104  	function create_list_query($order_by, $where)
 105      {
 106          global $log,$current_user;
 107          require('user_privileges/user_privileges_'.$current_user->id.'.php');
 108              require('user_privileges/sharing_privileges_'.$current_user->id.'.php');
 109              $tab_id = getTabid("Potentials");
 110          $log->debug("Entering create_list_query(".$order_by.",". $where.") method ...");
 111          // Determine if the vtiger_account name is present in the where clause.
 112          $account_required = preg_match("/accounts\.name/", $where);
 113  
 114          if($account_required)
 115          {
 116              $query = "SELECT vtiger_potential.potentialid,  vtiger_potential.potentialname, vtiger_potential.dateclosed FROM vtiger_potential, vtiger_account ";
 117              $where_auto = "account.accountid = vtiger_potential.related_to AND vtiger_crmentity.deleted=0 ";
 118          }
 119          else
 120          {
 121              $query = 'SELECT vtiger_potential.potentialid, vtiger_potential.potentialname, vtiger_crmentity.smcreatorid, vtiger_potential.closingdate FROM vtiger_potential inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_potential.potentialid LEFT JOIN vtiger_groups on vtiger_groups.groupid = vtiger_crmentity.smownerid left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid ';
 122              $where_auto = ' AND vtiger_crmentity.deleted=0';
 123          }
 124  
 125          $query .= $this->getNonAdminAccessControlQuery('Potentials',$current_user);
 126          if($where != "")
 127              $query .= " where $where ".$where_auto;
 128          else
 129              $query .= " where ".$where_auto;
 130          if($order_by != "")
 131              $query .= " ORDER BY $order_by";
 132  
 133          $log->debug("Exiting create_list_query method ...");
 134          return $query;
 135      }
 136  
 137      /** Function to export the Opportunities records in CSV Format
 138      * @param reference variable - order by is passed when the query is executed
 139      * @param reference variable - where condition is passed when the query is executed
 140      * Returns Export Potentials Query.
 141      */
 142  	function create_export_query($where)
 143      {
 144          global $log;
 145          global $current_user;
 146          $log->debug("Entering create_export_query(". $where.") method ...");
 147  
 148          include ("include/utils/ExportUtils.php");
 149  
 150          //To get the Permitted fields query and the permitted fields list
 151          $sql = getPermittedFieldsQuery("Potentials", "detail_view");
 152          $fields_list = getFieldsListFromQuery($sql);
 153  
 154          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 155                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 156          $query = "SELECT $fields_list,case when (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname end as user_name
 157                  FROM vtiger_potential
 158                  inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_potential.potentialid
 159                  LEFT JOIN vtiger_users ON vtiger_crmentity.smownerid=vtiger_users.id
 160                  LEFT JOIN vtiger_account on vtiger_potential.related_to=vtiger_account.accountid
 161                  LEFT JOIN vtiger_contactdetails on vtiger_potential.contact_id=vtiger_contactdetails.contactid
 162                  LEFT JOIN vtiger_potentialscf on vtiger_potentialscf.potentialid=vtiger_potential.potentialid
 163                  LEFT JOIN vtiger_groups
 164                      ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 165                  LEFT JOIN vtiger_campaign
 166                      ON vtiger_campaign.campaignid = vtiger_potential.campaignid";
 167  
 168          $query .= $this->getNonAdminAccessControlQuery('Potentials',$current_user);
 169          $where_auto = "  vtiger_crmentity.deleted = 0 ";
 170  
 171                  if($where != "")
 172                     $query .= "  WHERE ($where) AND ".$where_auto;
 173                  else
 174                     $query .= "  WHERE ".$where_auto;
 175  
 176          $log->debug("Exiting create_export_query method ...");
 177          return $query;
 178  
 179      }
 180  
 181  
 182  
 183      /** Returns a list of the associated contacts
 184       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 185       * All Rights Reserved..
 186       * Contributor(s): ______________________________________..
 187       */
 188  	function get_contacts($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 189          global $log, $singlepane_view,$currentModule,$current_user;
 190          $log->debug("Entering get_contacts(".$id.") method ...");
 191          $this_module = $currentModule;
 192  
 193          $related_module = vtlib_getModuleNameById($rel_tab_id);
 194          require_once("modules/$related_module/$related_module.php");
 195          $other = new $related_module();
 196          vtlib_setup_modulevars($related_module, $other);
 197          $singular_modname = vtlib_toSingular($related_module);
 198  
 199          $parenttab = getParentTab();
 200  
 201          if($singlepane_view == 'true')
 202              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 203          else
 204              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 205  
 206          $button = '';
 207  
 208          $accountid = $this->column_fields['related_to'];
 209          $search_string = "&fromPotential=true&acc_id=$accountid";
 210  
 211          if($actions) {
 212              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 213              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 214                  $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$search_string','test','width=640,height=602,resizable=0,scrollbars=0');\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 215              }
 216              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 217                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 218                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 219                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 220              }
 221          }
 222  
 223          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 224                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 225          $query = 'select case when (vtiger_users.user_name not like "") then '.$userNameSql.' else vtiger_groups.groupname end as user_name,
 226                      vtiger_contactdetails.accountid,vtiger_potential.potentialid, vtiger_potential.potentialname, vtiger_contactdetails.contactid,
 227                      vtiger_contactdetails.lastname, vtiger_contactdetails.firstname, vtiger_contactdetails.title, vtiger_contactdetails.department,
 228                      vtiger_contactdetails.email, vtiger_contactdetails.phone, vtiger_crmentity.crmid, vtiger_crmentity.smownerid,
 229                      vtiger_crmentity.modifiedtime , vtiger_account.accountname from vtiger_potential
 230                      left join vtiger_contpotentialrel on vtiger_contpotentialrel.potentialid = vtiger_potential.potentialid
 231                      inner join vtiger_contactdetails on ((vtiger_contactdetails.contactid = vtiger_contpotentialrel.contactid) or (vtiger_contactdetails.contactid = vtiger_potential.contact_id))
 232                      INNER JOIN vtiger_contactaddress ON vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid
 233                      INNER JOIN vtiger_contactsubdetails ON vtiger_contactdetails.contactid = vtiger_contactsubdetails.contactsubscriptionid
 234                      INNER JOIN vtiger_customerdetails ON vtiger_contactdetails.contactid = vtiger_customerdetails.customerid
 235                      INNER JOIN vtiger_contactscf ON vtiger_contactdetails.contactid = vtiger_contactscf.contactid
 236                      inner join vtiger_crmentity on vtiger_crmentity.crmid = vtiger_contactdetails.contactid
 237                      left join vtiger_account on vtiger_account.accountid = vtiger_contactdetails.accountid
 238                      left join vtiger_groups on vtiger_groups.groupid=vtiger_crmentity.smownerid
 239                      left join vtiger_users on vtiger_crmentity.smownerid=vtiger_users.id
 240                      where vtiger_potential.potentialid = '.$id.' and vtiger_crmentity.deleted=0';
 241  
 242          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 243  
 244          if($return_value == null) $return_value = Array();
 245          $return_value['CUSTOM_BUTTON'] = $button;
 246  
 247          $log->debug("Exiting get_contacts method ...");
 248          return $return_value;
 249      }
 250  
 251      /** Returns a list of the associated calls
 252       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 253       * All Rights Reserved..
 254       * Contributor(s): ______________________________________..
 255       */
 256  	function get_activities($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 257          global $log, $singlepane_view,$currentModule,$current_user;
 258          $log->debug("Entering get_activities(".$id.") method ...");
 259          $this_module = $currentModule;
 260  
 261          $related_module = vtlib_getModuleNameById($rel_tab_id);
 262          require_once("modules/$related_module/Activity.php");
 263          $other = new Activity();
 264          vtlib_setup_modulevars($related_module, $other);
 265          $singular_modname = vtlib_toSingular($related_module);
 266  
 267          $parenttab = getParentTab();
 268  
 269          if($singlepane_view == 'true')
 270              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 271          else
 272              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 273  
 274          $button = '';
 275  
 276          $button .= '<input type="hidden" name="activity_mode">';
 277  
 278          if($actions) {
 279              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 280              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 281                  if(getFieldVisibilityPermission('Calendar',$current_user->id,'parent_id', 'readwrite') == '0') {
 282                      $button .= "<input title='".getTranslatedString('LBL_NEW'). " ". getTranslatedString('LBL_TODO', $related_module) ."' class='crmbutton small create'" .
 283                          " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\";this.form.return_module.value=\"$this_module\";this.form.activity_mode.value=\"Task\";' type='submit' name='button'" .
 284                          " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString('LBL_TODO', $related_module) ."'>&nbsp;";
 285                  }
 286                  if(getFieldVisibilityPermission('Events',$current_user->id,'parent_id', 'readwrite') == '0') {
 287                      $button .= "<input title='".getTranslatedString('LBL_NEW'). " ". getTranslatedString('LBL_TODO', $related_module) ."' class='crmbutton small create'" .
 288                          " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\";this.form.return_module.value=\"$this_module\";this.form.activity_mode.value=\"Events\";' type='submit' name='button'" .
 289                          " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString('LBL_EVENT', $related_module) ."'>";
 290                  }
 291              }
 292          }
 293  
 294          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 295                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 296          $query = "SELECT vtiger_activity.activityid as 'tmp_activity_id',vtiger_activity.*,vtiger_seactivityrel.crmid as parent_id, vtiger_contactdetails.lastname,vtiger_contactdetails.firstname,
 297                      vtiger_crmentity.crmid, vtiger_crmentity.smownerid, vtiger_crmentity.modifiedtime,
 298                      case when (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname end as user_name,
 299                      vtiger_recurringevents.recurringtype from vtiger_activity
 300                      inner join vtiger_seactivityrel on vtiger_seactivityrel.activityid=vtiger_activity.activityid
 301                      inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_activity.activityid
 302                      left join vtiger_cntactivityrel on vtiger_cntactivityrel.activityid = vtiger_activity.activityid
 303                      left join vtiger_contactdetails on vtiger_contactdetails.contactid = vtiger_cntactivityrel.contactid
 304                      inner join vtiger_potential on vtiger_potential.potentialid=vtiger_seactivityrel.crmid
 305                      left join vtiger_users on vtiger_users.id=vtiger_crmentity.smownerid
 306                      left join vtiger_groups on vtiger_groups.groupid=vtiger_crmentity.smownerid
 307                      left outer join vtiger_recurringevents on vtiger_recurringevents.activityid=vtiger_activity.activityid
 308                      where vtiger_seactivityrel.crmid=".$id." and vtiger_crmentity.deleted=0
 309                      and ((vtiger_activity.activitytype='Task' and vtiger_activity.status not in ('Completed','Deferred'))
 310                      or (vtiger_activity.activitytype NOT in ('Emails','Task') and  vtiger_activity.eventstatus not in ('','Held'))) ";
 311  
 312          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 313  
 314          if($return_value == null) $return_value = Array();
 315          $return_value['CUSTOM_BUTTON'] = $button;
 316  
 317          $log->debug("Exiting get_activities method ...");
 318          return $return_value;
 319      }
 320  
 321       /**
 322       * Function to get Contact related Products
 323       * @param  integer   $id  - contactid
 324       * returns related Products record in array format
 325       */
 326  	function get_products($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 327          global $log, $singlepane_view,$currentModule,$current_user;
 328          $log->debug("Entering get_products(".$id.") method ...");
 329          $this_module = $currentModule;
 330  
 331          $related_module = vtlib_getModuleNameById($rel_tab_id);
 332          require_once("modules/$related_module/$related_module.php");
 333          $other = new $related_module();
 334          vtlib_setup_modulevars($related_module, $other);
 335          $singular_modname = vtlib_toSingular($related_module);
 336  
 337          $parenttab = getParentTab();
 338  
 339          if($singlepane_view == 'true')
 340              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 341          else
 342              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 343  
 344          $button = '';
 345  
 346          if($actions) {
 347              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 348              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 349                  $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;";
 350              }
 351              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 352                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 353                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 354                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 355              }
 356          }
 357  
 358          $query = "SELECT vtiger_products.productid, vtiger_products.productname, vtiger_products.productcode,
 359                  vtiger_products.commissionrate, vtiger_products.qty_per_unit, vtiger_products.unit_price,
 360                  vtiger_crmentity.crmid, vtiger_crmentity.smownerid
 361                  FROM vtiger_products
 362                  INNER JOIN vtiger_seproductsrel ON vtiger_products.productid = vtiger_seproductsrel.productid and vtiger_seproductsrel.setype = 'Potentials'
 363                  INNER JOIN vtiger_productcf
 364                  ON vtiger_products.productid = vtiger_productcf.productid
 365                  INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid
 366                  INNER JOIN vtiger_potential ON vtiger_potential.potentialid = vtiger_seproductsrel.crmid
 367                  LEFT JOIN vtiger_users
 368                      ON vtiger_users.id=vtiger_crmentity.smownerid
 369                  LEFT JOIN vtiger_groups
 370                      ON vtiger_groups.groupid = vtiger_crmentity.smownerid
 371                  WHERE vtiger_crmentity.deleted = 0 AND vtiger_potential.potentialid = $id";
 372  
 373          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 374  
 375          if($return_value == null) $return_value = Array();
 376          $return_value['CUSTOM_BUTTON'] = $button;
 377  
 378          $log->debug("Exiting get_products method ...");
 379          return $return_value;
 380      }
 381  
 382      /**    Function used to get the Sales Stage history of the Potential
 383       *    @param $id - potentialid
 384       *    return $return_data - array with header and the entries in format Array('header'=>$header,'entries'=>$entries_list) where as $header and $entries_list are array which contains all the column values of an row
 385       */
 386  	function get_stage_history($id)
 387      {
 388          global $log;
 389          $log->debug("Entering get_stage_history(".$id.") method ...");
 390  
 391          global $adb;
 392          global $mod_strings;
 393          global $app_strings;
 394  
 395          $query = 'select vtiger_potstagehistory.*, vtiger_potential.potentialname from vtiger_potstagehistory inner join vtiger_potential on vtiger_potential.potentialid = vtiger_potstagehistory.potentialid inner join vtiger_crmentity on vtiger_crmentity.crmid = vtiger_potential.potentialid where vtiger_crmentity.deleted = 0 and vtiger_potential.potentialid = ?';
 396          $result=$adb->pquery($query, array($id));
 397          $noofrows = $adb->num_rows($result);
 398  
 399          $header[] = $app_strings['LBL_AMOUNT'];
 400          $header[] = $app_strings['LBL_SALES_STAGE'];
 401          $header[] = $app_strings['LBL_PROBABILITY'];
 402          $header[] = $app_strings['LBL_CLOSE_DATE'];
 403          $header[] = $app_strings['LBL_LAST_MODIFIED'];
 404  
 405          //Getting the field permission for the current user. 1 - Not Accessible, 0 - Accessible
 406          //Sales Stage, Expected Close Dates are mandatory fields. So no need to do security check to these fields.
 407          global $current_user;
 408  
 409          //If field is accessible then getFieldVisibilityPermission function will return 0 else return 1
 410          $amount_access = (getFieldVisibilityPermission('Potentials', $current_user->id, 'amount') != '0')? 1 : 0;
 411          $probability_access = (getFieldVisibilityPermission('Potentials', $current_user->id, 'probability') != '0')? 1 : 0;
 412          $picklistarray = getAccessPickListValues('Potentials');
 413  
 414          $potential_stage_array = $picklistarray['sales_stage'];
 415          //- ==> picklist field is not permitted in profile
 416          //Not Accessible - picklist is permitted in profile but picklist value is not permitted
 417          $error_msg = 'Not Accessible';
 418  
 419          while($row = $adb->fetch_array($result))
 420          {
 421              $entries = Array();
 422  
 423              $entries[] = ($amount_access != 1)? $row['amount'] : 0;
 424              $entries[] = (in_array($row['stage'], $potential_stage_array))? $row['stage']: $error_msg;
 425              $entries[] = ($probability_access != 1) ? $row['probability'] : 0;
 426              $entries[] = DateTimeField::convertToUserFormat($row['closedate']);
 427              $date = new DateTimeField($row['lastmodified']);
 428              $entries[] = $date->getDisplayDate();
 429  
 430              $entries_list[] = $entries;
 431          }
 432  
 433          $return_data = Array('header'=>$header,'entries'=>$entries_list);
 434  
 435           $log->debug("Exiting get_stage_history method ...");
 436  
 437          return $return_data;
 438      }
 439  
 440      /**
 441      * Function to get Potential related Task & Event which have activity type Held, Completed or Deferred.
 442      * @param  integer   $id
 443      * returns related Task or Event record in array format
 444      */
 445  	function get_history($id)
 446      {
 447              global $log;
 448              $log->debug("Entering get_history(".$id.") method ...");
 449              $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 450                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 451              $query = "SELECT vtiger_activity.activityid, vtiger_activity.subject, vtiger_activity.status,
 452          vtiger_activity.eventstatus, vtiger_activity.activitytype,vtiger_activity.date_start,
 453          vtiger_activity.due_date, vtiger_activity.time_start,vtiger_activity.time_end,
 454          vtiger_crmentity.modifiedtime, vtiger_crmentity.createdtime,
 455          vtiger_crmentity.description,case when (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname end as user_name
 456                  from vtiger_activity
 457                  inner join vtiger_seactivityrel on vtiger_seactivityrel.activityid=vtiger_activity.activityid
 458                  inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_activity.activityid
 459                  left join vtiger_groups on vtiger_groups.groupid=vtiger_crmentity.smownerid
 460                  left join vtiger_users on vtiger_users.id=vtiger_crmentity.smownerid
 461                  where (vtiger_activity.activitytype != 'Emails')
 462                  and (vtiger_activity.status = 'Completed' or vtiger_activity.status = 'Deferred' or (vtiger_activity.eventstatus = 'Held' and vtiger_activity.eventstatus != ''))
 463                  and vtiger_seactivityrel.crmid=".$id."
 464                                  and vtiger_crmentity.deleted = 0";
 465          //Don't add order by, because, for security, one more condition will be added with this query in include/RelatedListView.php
 466  
 467          $log->debug("Exiting get_history method ...");
 468          return getHistory('Potentials',$query,$id);
 469      }
 470  
 471  
 472        /**
 473        * Function to get Potential related Quotes
 474        * @param  integer   $id  - potentialid
 475        * returns related Quotes record in array format
 476        */
 477  	function get_quotes($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 478          global $log, $singlepane_view,$currentModule,$current_user;
 479          $log->debug("Entering get_quotes(".$id.") method ...");
 480          $this_module = $currentModule;
 481  
 482          $related_module = vtlib_getModuleNameById($rel_tab_id);
 483          require_once("modules/$related_module/$related_module.php");
 484          $other = new $related_module();
 485          vtlib_setup_modulevars($related_module, $other);
 486          $singular_modname = vtlib_toSingular($related_module);
 487  
 488          $parenttab = getParentTab();
 489  
 490          if($singlepane_view == 'true')
 491              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 492          else
 493              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 494  
 495          $button = '';
 496  
 497          if($actions && getFieldVisibilityPermission($related_module, $current_user->id, 'potential_id', 'readwrite') == '0') {
 498              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 499              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 500                  $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;";
 501              }
 502              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 503                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 504                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 505                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 506              }
 507          }
 508  
 509          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 510                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 511          $query = "select case when (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname end as user_name,
 512                      vtiger_account.accountname, vtiger_crmentity.*, vtiger_quotes.*, vtiger_potential.potentialname from vtiger_quotes
 513                      inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_quotes.quoteid
 514                      left outer join vtiger_potential on vtiger_potential.potentialid=vtiger_quotes.potentialid
 515                      left join vtiger_groups on vtiger_groups.groupid=vtiger_crmentity.smownerid
 516                      LEFT JOIN vtiger_quotescf ON vtiger_quotescf.quoteid = vtiger_quotes.quoteid
 517                      LEFT JOIN vtiger_quotesbillads ON vtiger_quotesbillads.quotebilladdressid = vtiger_quotes.quoteid
 518                      LEFT JOIN vtiger_quotesshipads ON vtiger_quotesshipads.quoteshipaddressid = vtiger_quotes.quoteid
 519                      left join vtiger_users on vtiger_users.id=vtiger_crmentity.smownerid
 520                      LEFT join vtiger_account on vtiger_account.accountid=vtiger_quotes.accountid
 521                      where vtiger_crmentity.deleted=0 and vtiger_potential.potentialid=".$id;
 522  
 523          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 524  
 525          if($return_value == null) $return_value = Array();
 526          $return_value['CUSTOM_BUTTON'] = $button;
 527  
 528          $log->debug("Exiting get_quotes method ...");
 529          return $return_value;
 530      }
 531  
 532      /**
 533       * Function to get Potential related SalesOrder
 534        * @param  integer   $id  - potentialid
 535       * returns related SalesOrder record in array format
 536       */
 537  	function get_salesorder($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 538          global $log, $singlepane_view,$currentModule,$current_user;
 539          $log->debug("Entering get_salesorder(".$id.") method ...");
 540          $this_module = $currentModule;
 541  
 542          $related_module = vtlib_getModuleNameById($rel_tab_id);
 543          require_once("modules/$related_module/$related_module.php");
 544          $other = new $related_module();
 545          vtlib_setup_modulevars($related_module, $other);
 546          $singular_modname = vtlib_toSingular($related_module);
 547  
 548          $parenttab = getParentTab();
 549  
 550          if($singlepane_view == 'true')
 551              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 552          else
 553              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 554  
 555          $button = '';
 556  
 557          if($actions && getFieldVisibilityPermission($related_module, $current_user->id, 'potential_id', 'readwrite') == '0') {
 558              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 559              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 560                  $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;";
 561              }
 562              if(in_array('ADD', $actions) && isPermitted($related_module,1, '') == 'yes') {
 563                  $button .= "<input title='".getTranslatedString('LBL_ADD_NEW'). " ". getTranslatedString($singular_modname) ."' class='crmbutton small create'" .
 564                      " onclick='this.form.action.value=\"EditView\";this.form.module.value=\"$related_module\"' type='submit' name='button'" .
 565                      " value='". getTranslatedString('LBL_ADD_NEW'). " " . getTranslatedString($singular_modname) ."'>&nbsp;";
 566              }
 567          }
 568  
 569          $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
 570                              'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
 571          $query = "select vtiger_crmentity.*, vtiger_salesorder.*, vtiger_quotes.subject as quotename
 572              , vtiger_account.accountname, vtiger_potential.potentialname,case when
 573              (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname
 574              end as user_name from vtiger_salesorder
 575              inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_salesorder.salesorderid
 576              left outer join vtiger_quotes on vtiger_quotes.quoteid=vtiger_salesorder.quoteid
 577              left outer join vtiger_account on vtiger_account.accountid=vtiger_salesorder.accountid
 578              left outer join vtiger_potential on vtiger_potential.potentialid=vtiger_salesorder.potentialid
 579              left join vtiger_groups on vtiger_groups.groupid=vtiger_crmentity.smownerid
 580              LEFT JOIN vtiger_salesordercf ON vtiger_salesordercf.salesorderid = vtiger_salesorder.salesorderid
 581              LEFT JOIN vtiger_invoice_recurring_info ON vtiger_invoice_recurring_info.start_period = vtiger_salesorder.salesorderid
 582              LEFT JOIN vtiger_sobillads ON vtiger_sobillads.sobilladdressid = vtiger_salesorder.salesorderid
 583              LEFT JOIN vtiger_soshipads ON vtiger_soshipads.soshipaddressid = vtiger_salesorder.salesorderid
 584              left join vtiger_users on vtiger_users.id=vtiger_crmentity.smownerid
 585               where vtiger_crmentity.deleted=0 and vtiger_potential.potentialid = ".$id;
 586  
 587          $return_value = GetRelatedList($this_module, $related_module, $other, $query, $button, $returnset);
 588  
 589          if($return_value == null) $return_value = Array();
 590          $return_value['CUSTOM_BUTTON'] = $button;
 591  
 592          $log->debug("Exiting get_salesorder method ...");
 593          return $return_value;
 594      }
 595  
 596      /**
 597       * Move the related records of the specified list of id's to the given record.
 598       * @param String This module name
 599       * @param Array List of Entity Id's from which related records need to be transfered
 600       * @param Integer Id of the the Record to which the related records are to be moved
 601       */
 602  	function transferRelatedRecords($module, $transferEntityIds, $entityId) {
 603          global $adb,$log;
 604          $log->debug("Entering function transferRelatedRecords ($module, $transferEntityIds, $entityId)");
 605  
 606          $rel_table_arr = Array("Activities"=>"vtiger_seactivityrel","Contacts"=>"vtiger_contpotentialrel","Products"=>"vtiger_seproductsrel",
 607                          "Attachments"=>"vtiger_seattachmentsrel","Quotes"=>"vtiger_quotes","SalesOrder"=>"vtiger_salesorder",
 608                          "Documents"=>"vtiger_senotesrel");
 609  
 610          $tbl_field_arr = Array("vtiger_seactivityrel"=>"activityid","vtiger_contpotentialrel"=>"contactid","vtiger_seproductsrel"=>"productid",
 611                          "vtiger_seattachmentsrel"=>"attachmentsid","vtiger_quotes"=>"quoteid","vtiger_salesorder"=>"salesorderid",
 612                          "vtiger_senotesrel"=>"notesid");
 613  
 614          $entity_tbl_field_arr = Array("vtiger_seactivityrel"=>"crmid","vtiger_contpotentialrel"=>"potentialid","vtiger_seproductsrel"=>"crmid",
 615                          "vtiger_seattachmentsrel"=>"crmid","vtiger_quotes"=>"potentialid","vtiger_salesorder"=>"potentialid",
 616                          "vtiger_senotesrel"=>"crmid");
 617  
 618          foreach($transferEntityIds as $transferId) {
 619              foreach($rel_table_arr as $rel_module=>$rel_table) {
 620                  $id_field = $tbl_field_arr[$rel_table];
 621                  $entity_id_field = $entity_tbl_field_arr[$rel_table];
 622                  // IN clause to avoid duplicate entries
 623                  $sel_result =  $adb->pquery("select $id_field from $rel_table where $entity_id_field=? " .
 624                          " and $id_field not in (select $id_field from $rel_table where $entity_id_field=?)",
 625                          array($transferId,$entityId));
 626                  $res_cnt = $adb->num_rows($sel_result);
 627                  if($res_cnt > 0) {
 628                      for($i=0;$i<$res_cnt;$i++) {
 629                          $id_field_value = $adb->query_result($sel_result,$i,$id_field);
 630                          $adb->pquery("update $rel_table set $entity_id_field=? where $entity_id_field=? and $id_field=?",
 631                              array($entityId,$transferId,$id_field_value));
 632                      }
 633                  }
 634              }
 635          }
 636          parent::transferRelatedRecords($module, $transferEntityIds, $entityId);
 637          $log->debug("Exiting transferRelatedRecords...");
 638      }
 639  
 640      /*
 641       * Function to get the secondary query part of a report
 642       * @param - $module primary module name
 643       * @param - $secmodule secondary module name
 644       * returns the query string formed on fetching the related data for report for secondary module
 645       */
 646  	function generateReportsSecQuery($module,$secmodule,$queryplanner){
 647          $matrix = $queryplanner->newDependencyMatrix();
 648          $matrix->setDependency('vtiger_crmentityPotentials',array('vtiger_groupsPotentials','vtiger_usersPotentials','vtiger_lastModifiedByPotentials'));
 649          $matrix->setDependency('vtiger_potential', array('vtiger_crmentityPotentials','vtiger_accountPotentials',
 650                                              'vtiger_contactdetailsPotentials','vtiger_campaignPotentials','vtiger_potentialscf'));
 651  
 652  
 653          if (!$queryplanner->requireTable("vtiger_potential",$matrix)){
 654              return '';
 655          }
 656  
 657          $query = $this->getRelationQuery($module,$secmodule,"vtiger_potential","potentialid", $queryplanner);
 658  
 659          if ($queryplanner->requireTable("vtiger_crmentityPotentials",$matrix)){
 660              $query .= " left join vtiger_crmentity as vtiger_crmentityPotentials on vtiger_crmentityPotentials.crmid=vtiger_potential.potentialid and vtiger_crmentityPotentials.deleted=0";
 661          }
 662          if ($queryplanner->requireTable("vtiger_accountPotentials")){
 663              $query .= " left join vtiger_account as vtiger_accountPotentials on vtiger_potential.related_to = vtiger_accountPotentials.accountid";
 664          }
 665          if ($queryplanner->requireTable("vtiger_contactdetailsPotentials")){
 666              $query .= " left join vtiger_contactdetails as vtiger_contactdetailsPotentials on vtiger_potential.contact_id = vtiger_contactdetailsPotentials.contactid";
 667          }
 668          if ($queryplanner->requireTable("vtiger_potentialscf")){
 669              $query .= " left join vtiger_potentialscf on vtiger_potentialscf.potentialid = vtiger_potential.potentialid";
 670          }
 671          if ($queryplanner->requireTable("vtiger_groupsPotentials")){
 672              $query .= " left join vtiger_groups vtiger_groupsPotentials on vtiger_groupsPotentials.groupid = vtiger_crmentityPotentials.smownerid";
 673          }
 674          if ($queryplanner->requireTable("vtiger_usersPotentials")){
 675              $query .= " left join vtiger_users as vtiger_usersPotentials on vtiger_usersPotentials.id = vtiger_crmentityPotentials.smownerid";
 676          }
 677          if ($queryplanner->requireTable("vtiger_campaignPotentials")){
 678              $query .= " left join vtiger_campaign as vtiger_campaignPotentials on vtiger_potential.campaignid = vtiger_campaignPotentials.campaignid";
 679          }
 680          if ($queryplanner->requireTable("vtiger_lastModifiedByPotentials")){
 681              $query .= " left join vtiger_users as vtiger_lastModifiedByPotentials on vtiger_lastModifiedByPotentials.id = vtiger_crmentityPotentials.modifiedby ";
 682          }
 683          if ($queryplanner->requireTable("vtiger_createdbyPotentials")){
 684              $query .= " left join vtiger_users as vtiger_createdbyPotentials on vtiger_createdbyPotentials.id = vtiger_crmentityPotentials.smcreatorid ";
 685          }
 686          return $query;
 687      }
 688  
 689      /*
 690       * Function to get the relation tables for related modules
 691       * @param - $secmodule secondary module name
 692       * returns the array with table names and fieldnames storing relations between module and this module
 693       */
 694  	function setRelationTables($secmodule){
 695          $rel_tables = array (
 696              "Calendar" => array("vtiger_seactivityrel"=>array("crmid","activityid"),"vtiger_potential"=>"potentialid"),
 697              "Products" => array("vtiger_seproductsrel"=>array("crmid","productid"),"vtiger_potential"=>"potentialid"),
 698              "Quotes" => array("vtiger_quotes"=>array("potentialid","quoteid"),"vtiger_potential"=>"potentialid"),
 699              "SalesOrder" => array("vtiger_salesorder"=>array("potentialid","salesorderid"),"vtiger_potential"=>"potentialid"),
 700              "Documents" => array("vtiger_senotesrel"=>array("crmid","notesid"),"vtiger_potential"=>"potentialid"),
 701              "Accounts" => array("vtiger_potential"=>array("potentialid","related_to")),
 702              "Contacts" => array("vtiger_potential"=>array("potentialid","contact_id")),
 703          );
 704          return $rel_tables[$secmodule];
 705      }
 706  
 707      // Function to unlink all the dependent entities of the given Entity by Id
 708  	function unlinkDependencies($module, $id) {
 709          global $log;
 710          /*//Backup Activity-Potentials Relation
 711          $act_q = "select activityid from vtiger_seactivityrel where crmid = ?";
 712          $act_res = $this->db->pquery($act_q, array($id));
 713          if ($this->db->num_rows($act_res) > 0) {
 714              for($k=0;$k < $this->db->num_rows($act_res);$k++)
 715              {
 716                  $act_id = $this->db->query_result($act_res,$k,"activityid");
 717                  $params = array($id, RB_RECORD_DELETED, 'vtiger_seactivityrel', 'crmid', 'activityid', $act_id);
 718                  $this->db->pquery("insert into vtiger_relatedlists_rb values (?,?,?,?,?,?)", $params);
 719              }
 720          }
 721          $sql = 'delete from vtiger_seactivityrel where crmid = ?';
 722          $this->db->pquery($sql, array($id));*/
 723  
 724          parent::unlinkDependencies($module, $id);
 725      }
 726  
 727      // Function to unlink an entity with given Id from another entity
 728  	function unlinkRelationship($id, $return_module, $return_id) {
 729          global $log;
 730          if(empty($return_module) || empty($return_id)) return;
 731  
 732          if($return_module == 'Accounts') {
 733              $this->trash($this->module_name, $id);
 734          } elseif($return_module == 'Campaigns') {
 735              $sql = 'UPDATE vtiger_potential SET campaignid = ? WHERE potentialid = ?';
 736              $this->db->pquery($sql, array(null, $id));
 737          } elseif($return_module == 'Products') {
 738              $sql = 'DELETE FROM vtiger_seproductsrel WHERE crmid=? AND productid=?';
 739              $this->db->pquery($sql, array($id, $return_id));
 740          } elseif($return_module == 'Contacts') {
 741              $sql = 'DELETE FROM vtiger_contpotentialrel WHERE potentialid=? AND contactid=?';
 742              $this->db->pquery($sql, array($id, $return_id));
 743  
 744              //If contact related to potential through edit of record,that entry will be present in
 745              //vtiger_potential contact_id column,which should be set to zero
 746              $sql = 'UPDATE vtiger_potential SET contact_id = ? WHERE potentialid=? AND contact_id=?';
 747              $this->db->pquery($sql, array(0,$id, $return_id));
 748  
 749              // Potential directly linked with Contact (not through Account - vtiger_contpotentialrel)
 750              $directRelCheck = $this->db->pquery('SELECT related_to FROM vtiger_potential WHERE potentialid=? AND contact_id=?', array($id, $return_id));
 751              if($this->db->num_rows($directRelCheck)) {
 752                  $this->trash($this->module_name, $id);
 753              }
 754  
 755          } else {
 756              $sql = 'DELETE FROM vtiger_crmentityrel WHERE (crmid=? AND relmodule=? AND relcrmid=?) OR (relcrmid=? AND module=? AND crmid=?)';
 757              $params = array($id, $return_module, $return_id, $id, $return_module, $return_id);
 758              $this->db->pquery($sql, $params);
 759          }
 760      }
 761  
 762  	function save_related_module($module, $crmid, $with_module, $with_crmids) {
 763          $adb = PearDatabase::getInstance();
 764  
 765          if(!is_array($with_crmids)) $with_crmids = Array($with_crmids);
 766          foreach($with_crmids as $with_crmid) {
 767              if($with_module == 'Contacts') { //When we select contact from potential related list
 768                  $sql = "insert into vtiger_contpotentialrel values (?,?)";
 769                  $adb->pquery($sql, array($with_crmid, $crmid));
 770  
 771              } elseif($with_module == 'Products') {//when we select product from potential related list
 772                  $sql = "insert into vtiger_seproductsrel values (?,?,?)";
 773                  $adb->pquery($sql, array($crmid, $with_crmid,'Potentials'));
 774  
 775              } else {
 776                  parent::save_related_module($module, $crmid, $with_module, $with_crmid);
 777              }
 778          }
 779      }
 780  
 781  }
 782  ?>


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