[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/PriceBooks/ -> PriceBooks.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  
  12  class PriceBooks extends CRMEntity {
  13      var $log;
  14      var $db;
  15      var $table_name = "vtiger_pricebook";
  16      var $table_index= 'pricebookid';
  17      var $tab_name = Array('vtiger_crmentity','vtiger_pricebook','vtiger_pricebookcf');
  18      var $tab_name_index = Array('vtiger_crmentity'=>'crmid','vtiger_pricebook'=>'pricebookid','vtiger_pricebookcf'=>'pricebookid');
  19      /**
  20       * Mandatory table for supporting custom fields.
  21       */
  22      var $customFieldTable = Array('vtiger_pricebookcf', 'pricebookid');
  23      var $column_fields = Array();
  24  
  25      var $sortby_fields = Array('bookname');
  26  
  27          // This is the list of fields that are in the lists.
  28      var $list_fields = Array(
  29                                  'Price Book Name'=>Array('pricebook'=>'bookname'),
  30                                  'Active'=>Array('pricebook'=>'active')
  31                                  );
  32  
  33      var $list_fields_name = Array(
  34                                          'Price Book Name'=>'bookname',
  35                                          'Active'=>'active'
  36                                       );
  37      var $list_link_field= 'bookname';
  38  
  39      var $search_fields = Array(
  40                                  'Price Book Name'=>Array('pricebook'=>'bookname')
  41                                  );
  42      var $search_fields_name = Array(
  43                                          'Price Book Name'=>'bookname'
  44                                       );
  45  
  46      //Added these variables which are used as default order by and sortorder in ListView
  47      var $default_order_by = 'bookname';
  48      var $default_sort_order = 'ASC';
  49  
  50      var $mandatory_fields = Array('bookname','currency_id','pricebook_no','createdtime' ,'modifiedtime');
  51  
  52      // For Alphabetical search
  53      var $def_basicsearch_col = 'bookname';
  54  
  55      /**    Constructor which will set the column_fields in this object
  56       */
  57  	function PriceBooks() {
  58          $this->log =LoggerManager::getLogger('pricebook');
  59          $this->log->debug("Entering PriceBooks() method ...");
  60          $this->db = PearDatabase::getInstance();
  61          $this->column_fields = getColumnFields('PriceBooks');
  62          $this->log->debug("Exiting PriceBook method ...");
  63      }
  64  
  65  	function save_module($module)
  66      {
  67          // Update the list prices in the price book with the unit price, if the Currency has been changed
  68          $this->updateListPrices();
  69      }
  70  
  71      /* Function to Update the List prices for all the products of a current price book
  72         with its Unit price, if the Currency for Price book has changed. */
  73  	function updateListPrices() {
  74          global $log, $adb;
  75          $log->debug("Entering function updateListPrices...");
  76          $pricebook_currency = $this->column_fields['currency_id'];
  77          $prod_res = $adb->pquery("select * from vtiger_pricebookproductrel where pricebookid=? AND usedcurrency != ?",
  78                              array($this->id, $pricebook_currency));
  79          $numRows = $adb->num_rows($prod_res);
  80  
  81          for($i=0;$i<$numRows;$i++) {
  82              $product_id = $adb->query_result($prod_res,$i,'productid');
  83              $list_price = $adb->query_result($prod_res,$i,'listprice');
  84              $used_currency = $adb->query_result($prod_res,$i,'usedcurrency');
  85              $product_currency_info = getCurrencySymbolandCRate($used_currency);
  86              $product_conv_rate = $product_currency_info['rate'];
  87              $pricebook_currency_info = getCurrencySymbolandCRate($pricebook_currency);
  88              $pb_conv_rate = $pricebook_currency_info['rate'];
  89              $conversion_rate = $pb_conv_rate / $product_conv_rate;
  90              $computed_list_price = $list_price * $conversion_rate;
  91  
  92              $query = "update vtiger_pricebookproductrel set listprice=?, usedcurrency=? where pricebookid=? and productid=?";
  93              $params = array($computed_list_price, $pricebook_currency, $this->id, $product_id);
  94              $adb->pquery($query, $params);
  95          }
  96          $log->debug("Exiting function updateListPrices...");
  97      }
  98  
  99      /**    function used to get the products which are related to the pricebook
 100       *    @param int $id - pricebook id
 101           *      @return array - return an array which will be returned from the function getPriceBookRelatedProducts
 102          **/
 103  	function get_pricebook_products($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 104          global $log, $singlepane_view,$currentModule,$current_user;
 105          $log->debug("Entering get_pricebook_products(".$id.") method ...");
 106          $this_module = $currentModule;
 107  
 108          $related_module = vtlib_getModuleNameById($rel_tab_id);
 109          require_once("modules/$related_module/$related_module.php");
 110          $other = new $related_module();
 111          vtlib_setup_modulevars($related_module, $other);
 112          $singular_modname = vtlib_toSingular($related_module);
 113  
 114          $parenttab = getParentTab();
 115  
 116          if($singlepane_view == 'true')
 117              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 118          else
 119              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 120  
 121          $button = '';
 122  
 123          if($actions) {
 124              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 125              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 126                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='submit' name='button' onclick=\"this.form.action.value='AddProductsToPriceBook';this.form.module.value='$related_module';this.form.return_module.value='$currentModule';this.form.return_action.value='PriceBookDetailView'\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 127              }
 128          }
 129  
 130          $query = 'SELECT vtiger_products.productid, vtiger_products.productname, vtiger_products.productcode, vtiger_products.commissionrate,
 131                          vtiger_products.qty_per_unit, vtiger_products.unit_price, vtiger_crmentity.crmid, vtiger_crmentity.smownerid,
 132                          vtiger_pricebookproductrel.listprice
 133                  FROM vtiger_products
 134                  INNER JOIN vtiger_pricebookproductrel ON vtiger_products.productid = vtiger_pricebookproductrel.productid
 135                  INNER JOIN vtiger_crmentity on vtiger_crmentity.crmid = vtiger_products.productid
 136                  INNER JOIN vtiger_pricebook on vtiger_pricebook.pricebookid = vtiger_pricebookproductrel.pricebookid
 137                  LEFT JOIN vtiger_users ON vtiger_users.id=vtiger_crmentity.smownerid
 138                  LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid '
 139                  . getNonAdminAccessControlQuery($related_module, $current_user) .'
 140                  WHERE vtiger_pricebook.pricebookid = '.$id.' and vtiger_crmentity.deleted = 0';
 141  
 142          $this->retrieve_entity_info($id,$this_module);
 143          $return_value = getPriceBookRelatedProducts($query,$this,$returnset);
 144  
 145          if($return_value == null) $return_value = Array();
 146          $return_value['CUSTOM_BUTTON'] = $button;
 147  
 148          $log->debug("Exiting get_pricebook_products method ...");
 149          return $return_value;
 150      }
 151  
 152      /**    function used to get the services which are related to the pricebook
 153       *    @param int $id - pricebook id
 154           *      @return array - return an array which will be returned from the function getPriceBookRelatedServices
 155          **/
 156  	function get_pricebook_services($id, $cur_tab_id, $rel_tab_id, $actions=false) {
 157          global $log, $singlepane_view,$currentModule,$current_user;
 158          $log->debug("Entering get_pricebook_services(".$id.") method ...");
 159          $this_module = $currentModule;
 160  
 161          $related_module = vtlib_getModuleNameById($rel_tab_id);
 162          require_once("modules/$related_module/$related_module.php");
 163          $other = new $related_module();
 164          vtlib_setup_modulevars($related_module, $other);
 165          $singular_modname = vtlib_toSingular($related_module);
 166  
 167          $parenttab = getParentTab();
 168  
 169          if($singlepane_view == 'true')
 170              $returnset = '&return_module='.$this_module.'&return_action=DetailView&return_id='.$id;
 171          else
 172              $returnset = '&return_module='.$this_module.'&return_action=CallRelatedList&return_id='.$id;
 173  
 174          $button = '';
 175  
 176          if($actions) {
 177              if(is_string($actions)) $actions = explode(',', strtoupper($actions));
 178              if(in_array('SELECT', $actions) && isPermitted($related_module,4, '') == 'yes') {
 179                  $button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' type='submit' name='button' onclick=\"this.form.action.value='AddServicesToPriceBook';this.form.module.value='$related_module';this.form.return_module.value='$currentModule';this.form.return_action.value='PriceBookDetailView'\" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module) ."'>&nbsp;";
 180              }
 181          }
 182  
 183          $query = 'SELECT vtiger_service.serviceid, vtiger_service.servicename, vtiger_service.commissionrate,
 184                      vtiger_service.qty_per_unit, vtiger_service.unit_price, vtiger_crmentity.crmid, vtiger_crmentity.smownerid,
 185                      vtiger_pricebookproductrel.listprice
 186              FROM vtiger_service
 187              INNER JOIN vtiger_pricebookproductrel on vtiger_service.serviceid = vtiger_pricebookproductrel.productid
 188              INNER JOIN vtiger_crmentity on vtiger_crmentity.crmid = vtiger_service.serviceid
 189              INNER JOIN vtiger_pricebook on vtiger_pricebook.pricebookid = vtiger_pricebookproductrel.pricebookid
 190              LEFT JOIN vtiger_users ON vtiger_users.id=vtiger_crmentity.smownerid
 191              LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid '
 192              . getNonAdminAccessControlQuery($related_module, $current_user) .'
 193              WHERE vtiger_pricebook.pricebookid = '.$id.' and vtiger_crmentity.deleted = 0';
 194  
 195          $this->retrieve_entity_info($id,$this_module);
 196          $return_value = $other->getPriceBookRelatedServices($query,$this,$returnset);
 197  
 198          if($return_value == null) $return_value = Array();
 199          $return_value['CUSTOM_BUTTON'] = $button;
 200  
 201          $log->debug("Exiting get_pricebook_services method ...");
 202          return $return_value;
 203      }
 204  
 205      /**    function used to get whether the pricebook has related with a product or not
 206       *    @param int $id - product id
 207       *    @return true or false - if there are no pricebooks available or associated pricebooks for the product is equal to total number of pricebooks then return false, else return true
 208       */
 209  	function get_pricebook_noproduct($id)
 210      {
 211          global $log;
 212          $log->debug("Entering get_pricebook_noproduct(".$id.") method ...");
 213  
 214          $query = "select vtiger_crmentity.crmid, vtiger_pricebook.* from vtiger_pricebook inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_pricebook.pricebookid where vtiger_crmentity.deleted=0";
 215          $result = $this->db->pquery($query, array());
 216          $no_count = $this->db->num_rows($result);
 217          if($no_count !=0)
 218          {
 219                  $pb_query = 'select vtiger_crmentity.crmid, vtiger_pricebook.pricebookid,vtiger_pricebookproductrel.productid from vtiger_pricebook inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_pricebook.pricebookid inner join vtiger_pricebookproductrel on vtiger_pricebookproductrel.pricebookid=vtiger_pricebook.pricebookid where vtiger_crmentity.deleted=0 and vtiger_pricebookproductrel.productid=?';
 220              $result_pb = $this->db->pquery($pb_query, array($id));
 221              if($no_count == $this->db->num_rows($result_pb))
 222              {
 223                  $log->debug("Exiting get_pricebook_noproduct method ...");
 224                  return false;
 225              }
 226              elseif($this->db->num_rows($result_pb) == 0)
 227              {
 228                  $log->debug("Exiting get_pricebook_noproduct method ...");
 229                  return true;
 230              }
 231              elseif($this->db->num_rows($result_pb) < $no_count)
 232              {
 233                  $log->debug("Exiting get_pricebook_noproduct method ...");
 234                  return true;
 235              }
 236          }
 237          else
 238          {
 239              $log->debug("Exiting get_pricebook_noproduct method ...");
 240              return false;
 241          }
 242      }
 243  
 244      /*
 245       * Function to get the primary query part of a report
 246       * @param - $module Primary module name
 247       * returns the query string formed on fetching the related data for report for primary module
 248       */
 249  	function generateReportsQuery($module,$queryplanner){
 250                   $moduletable = $this->table_name;
 251                   $moduleindex = $this->table_index;
 252                  $modulecftable = $this->customFieldTable[0];
 253                  $modulecfindex = $this->customFieldTable[1];
 254  
 255                  $cfquery = '';
 256                  if(isset($modulecftable) && $queryplanner->requireTable($modulecftable) ){
 257                      $cfquery = "inner join $modulecftable as $modulecftable on $modulecftable.$modulecfindex=$moduletable.$moduleindex";
 258                  }
 259  
 260                   $query = "from $moduletable $cfquery
 261                      inner join vtiger_crmentity on vtiger_crmentity.crmid=$moduletable.$moduleindex";
 262                  if ($queryplanner->requireTable("vtiger_currency_info$module")){
 263                      $query .= "  left join vtiger_currency_info as vtiger_currency_info$module on vtiger_currency_info$module.id = $moduletable.currency_id";
 264                  }
 265                  if ($queryplanner->requireTable("vtiger_groups$module")){
 266                      $query .= " left join vtiger_groups as vtiger_groups$module on vtiger_groups$module.groupid = vtiger_crmentity.smownerid";
 267                  }
 268                  if ($queryplanner->requireTable("vtiger_users$module")){
 269                      $query .= " left join vtiger_users as vtiger_users$module on vtiger_users$module.id = vtiger_crmentity.smownerid";
 270                  }
 271                  $query .= " left join vtiger_groups on vtiger_groups.groupid = vtiger_crmentity.smownerid";
 272                  $query .= " left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid";
 273  
 274                  if ($queryplanner->requireTable("vtiger_lastModifiedByPriceBooks")){
 275                      $query .= " left join vtiger_users as vtiger_lastModifiedByPriceBooks on vtiger_lastModifiedByPriceBooks.id = vtiger_crmentity.modifiedby ";
 276                  }
 277                  return $query;
 278      }
 279  
 280      /*
 281       * Function to get the secondary query part of a report
 282       * @param - $module primary module name
 283       * @param - $secmodule secondary module name
 284       * returns the query string formed on fetching the related data for report for secondary module
 285       */
 286  	function generateReportsSecQuery($module,$secmodule,$queryplanner) {
 287  
 288          $matrix = $queryplanner->newDependencyMatrix();
 289  
 290          $matrix->setDependency("vtiger_crmentityPriceBooks",array("vtiger_usersPriceBooks","vtiger_groupsPriceBooks"));
 291          $matrix->setDependency("vtiger_pricebook",array("vtiger_crmentityPriceBooks","vtiger_currency_infoPriceBooks"));
 292          if (!$queryplanner->requireTable('vtiger_pricebook', $matrix)) {
 293              return '';
 294          }
 295  
 296          $query = $this->getRelationQuery($module,$secmodule,"vtiger_pricebook","pricebookid", $queryplanner);
 297          // TODO Support query planner
 298          if ($queryplanner->requireTable("vtiger_crmentityPriceBooks",$matrix)){
 299          $query .=" left join vtiger_crmentity as vtiger_crmentityPriceBooks on vtiger_crmentityPriceBooks.crmid=vtiger_pricebook.pricebookid and vtiger_crmentityPriceBooks.deleted=0";
 300          }
 301          if ($queryplanner->requireTable("vtiger_currency_infoPriceBooks")){
 302          $query .=" left join vtiger_currency_info as vtiger_currency_infoPriceBooks on vtiger_currency_infoPriceBooks.id = vtiger_pricebook.currency_id";
 303          }
 304          if ($queryplanner->requireTable("vtiger_usersPriceBooks")){
 305              $query .=" left join vtiger_users as vtiger_usersPriceBooks on vtiger_usersPriceBooks.id = vtiger_crmentityPriceBooks.smownerid";
 306          }
 307          if ($queryplanner->requireTable("vtiger_groupsPriceBooks")){
 308              $query .=" left join vtiger_groups as vtiger_groupsPriceBooks on vtiger_groupsPriceBooks.groupid = vtiger_crmentityPriceBooks.smownerid";
 309          }
 310          if ($queryplanner->requireTable("vtiger_lastModifiedByPriceBooks")){
 311              $query .=" left join vtiger_users as vtiger_lastModifiedByPriceBooks on vtiger_lastModifiedByPriceBooks.id = vtiger_crmentityPriceBooks.smownerid";
 312          }
 313          if ($queryplanner->requireTable("vtiger_createdbyPriceBooks")){
 314              $query .= " left join vtiger_users as vtiger_createdbyPriceBooks on vtiger_createdbyPriceBooks.id = vtiger_crmentityPriceBooks.smcreatorid ";
 315          }
 316          return $query;
 317      }
 318  
 319      /*
 320       * Function to get the relation tables for related modules
 321       * @param - $secmodule secondary module name
 322       * returns the array with table names and fieldnames storing relations between module and this module
 323       */
 324  	function setRelationTables($secmodule){
 325          $rel_tables = array (
 326              "Products" => array("vtiger_pricebookproductrel"=>array("pricebookid","productid"),"vtiger_pricebook"=>"pricebookid"),
 327              "Services" => array("vtiger_pricebookproductrel"=>array("pricebookid","productid"),"vtiger_pricebook"=>"pricebookid"),
 328          );
 329          return $rel_tables[$secmodule];
 330      }
 331  
 332  }
 333  ?>


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