[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Products/models/ -> Record.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 Products_Record_Model extends Vtiger_Record_Model {
  12  
  13      /**
  14       * Function to get Taxes Url
  15       * @return <String> Url
  16       */
  17  	function getTaxesURL() {
  18          return 'index.php?module=Inventory&action=GetTaxes&record='. $this->getId();
  19      }
  20  
  21      /**
  22       * Function to get available taxes for this record
  23       * @return <Array> List of available taxes
  24       */
  25  	function getTaxes() {
  26          $db = PearDatabase::getInstance();
  27  
  28          $result = $db->pquery('SELECT * FROM vtiger_producttaxrel
  29                      INNER JOIN vtiger_inventorytaxinfo ON vtiger_inventorytaxinfo.taxid = vtiger_producttaxrel.taxid
  30                      INNER JOIN vtiger_crmentity ON vtiger_producttaxrel.productid = vtiger_crmentity.crmid AND vtiger_crmentity.deleted = 0
  31                      WHERE vtiger_producttaxrel.productid = ? AND vtiger_inventorytaxinfo.deleted = 0', array($this->getId()));
  32          $taxes = array();
  33          for($i=0; $i<$db->num_rows($result); $i++) {
  34              $taxName = $db->query_result($result, $i, 'taxname');
  35              $tabLabel = $db->query_result($result, $i, 'taxlabel');
  36              $taxPercentage = $db->query_result($result, $i, 'taxpercentage');
  37              $taxes[$taxName] = array('percentage'=>$taxPercentage, 'label' => $tabLabel);
  38          }
  39          return $taxes;
  40      }
  41          
  42      /**
  43       * Function to get values of more currencies listprice
  44       * @return <Array> of listprice values
  45       */
  46  	static function getListPriceValues($id) {
  47          $db = PearDatabase::getInstance();            
  48          $listPrice = $db->pquery('SELECT * FROM vtiger_productcurrencyrel WHERE productid    = ?', array($id)); 
  49          $listpriceValues = array();
  50          for($i=0; $i<$db->num_rows($listPrice); $i++) {
  51              $listpriceValues[$db->query_result($listPrice, $i, 'currencyid')] = $db->query_result($listPrice, $i, 'actual_price');
  52          }
  53          return $listpriceValues;
  54      }
  55  
  56      /**
  57       * Function to get subproducts for this record
  58       * @return <Array> of subproducts
  59       */
  60  	function getSubProducts() {
  61          $db = PearDatabase::getInstance();
  62  
  63          $result = $db->pquery("SELECT vtiger_products.productid FROM vtiger_products
  64              INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid
  65              LEFT JOIN vtiger_seproductsrel ON vtiger_seproductsrel.crmid = vtiger_products.productid AND vtiger_seproductsrel.setype='Products'
  66              LEFT JOIN vtiger_users ON vtiger_users.id=vtiger_crmentity.smownerid
  67              LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid
  68              WHERE vtiger_crmentity.deleted = 0 AND vtiger_seproductsrel.productid = ? ", array($this->getId()));
  69  
  70          $subProductList = array();
  71          for($i=0; $i<$db->num_rows($result); $i++) {
  72              $subProductId = $db->query_result($result, $i, 'productid');
  73              $subProductList[] = Vtiger_Record_Model::getInstanceById($subProductId, 'Products');
  74          }
  75  
  76          return $subProductList;
  77      }
  78  
  79      /**
  80       * Function to get Url to Create a new Quote from this record
  81       * @return <String> Url to Create new Quote
  82       */
  83  	function getCreateQuoteUrl() {
  84          $quotesModuleModel = Vtiger_Module_Model::getInstance('Quotes');
  85  
  86          return "index.php?module=".$quotesModuleModel->getName()."&view=".$quotesModuleModel->getEditViewName()."&product_id=".$this->getId().
  87                  "&sourceModule=".$this->getModuleName()."&sourceRecord=".$this->getId()."&relationOperation=true";
  88      }
  89  
  90      /**
  91       * Function to get Url to Create a new Invoice from this record
  92       * @return <String> Url to Create new Invoice
  93       */
  94  	function getCreateInvoiceUrl() {
  95          $invoiceModuleModel = Vtiger_Module_Model::getInstance('Invoice');
  96  
  97          return "index.php?module=".$invoiceModuleModel->getName()."&view=".$invoiceModuleModel->getEditViewName()."&product_id=".$this->getId().
  98                  "&sourceModule=".$this->getModuleName()."&sourceRecord=".$this->getId()."&relationOperation=true";
  99      }
 100  
 101      /**
 102       * Function to get Url to Create a new PurchaseOrder from this record
 103       * @return <String> Url to Create new PurchaseOrder
 104       */
 105  	function getCreatePurchaseOrderUrl() {
 106          $purchaseOrderModuleModel = Vtiger_Module_Model::getInstance('PurchaseOrder');
 107  
 108          return "index.php?module=".$purchaseOrderModuleModel->getName()."&view=".$purchaseOrderModuleModel->getEditViewName()."&product_id=".$this->getId().
 109                  "&sourceModule=".$this->getModuleName()."&sourceRecord=".$this->getId()."&relationOperation=true";
 110      }
 111  
 112      /**
 113       * Function to get Url to Create a new SalesOrder from this record
 114       * @return <String> Url to Create new SalesOrder
 115       */
 116  	function getCreateSalesOrderUrl() {
 117          $salesOrderModuleModel = Vtiger_Module_Model::getInstance('SalesOrder');
 118  
 119          return "index.php?module=".$salesOrderModuleModel->getName()."&view=".$salesOrderModuleModel->getEditViewName()."&product_id=".$this->getId().
 120                  "&sourceModule=".$this->getModuleName()."&sourceRecord=".$this->getId()."&relationOperation=true";
 121      }
 122  
 123      /**
 124       * Function get details of taxes for this record
 125       * Function calls from Edit/Create view of Inventory Records
 126       * @param <Object> $focus
 127       * @return <Array> List of individual taxes
 128       */
 129  	function getDetailsForInventoryModule($focus) {
 130          $productId = $this->getId();
 131          $currentUser = Users_Record_Model::getCurrentUserModel();
 132          $productDetails = getAssociatedProducts($this->getModuleName(), $focus, $productId);
 133  
 134          $currentUserModel = Users_Record_Model::getCurrentUserModel();
 135          $convertedPriceDetails = $this->getModule()->getPricesForProducts($currentUserModel->get('currency_id'), array($productId));
 136          $productDetails[1]['listPrice1'] = number_format($convertedPriceDetails[$productId], $currentUserModel->get('no_of_currency_decimals'),'.','');
 137  
 138          $totalAfterDiscount = $productDetails[1]['totalAfterDiscount1'];
 139          $productTaxes = $productDetails[1]['taxes'];
 140          if (!empty ($productDetails)) {
 141              $taxCount = count($productTaxes);
 142              $taxTotal = '0.00';
 143  
 144              for($i=0; $i<$taxCount; $i++) {
 145                  $taxValue = $productTaxes[$i]['percentage'];
 146  
 147                  $taxAmount = $totalAfterDiscount * $taxValue / 100;
 148                  $taxTotal = $taxTotal + $taxAmount;
 149  
 150                  $productDetails[1]['taxes'][$i]['amount'] = $taxAmount;
 151                  $productDetails[1]['taxTotal1'] = $taxTotal;
 152              }
 153              $netPrice = $totalAfterDiscount + $taxTotal;
 154              $productDetails[1]['netPrice1'] = $netPrice;
 155              $productDetails[1]['final_details']['hdnSubTotal'] = $netPrice;
 156              $productDetails[1]['final_details']['grandTotal'] = $netPrice;
 157          }
 158  
 159          for ($i=1; $i<=count($productDetails); $i++) {
 160              $productId = $productDetails[$i]['hdnProductId'.$i];
 161              $productPrices = $this->getModule()->getPricesForProducts($currentUser->get('currency_id'), array($productId), $this->getModuleName());
 162              $productDetails[$i]['listPrice'.$i] = number_format($productPrices[$productId], $currentUser->get('no_of_currency_decimals'),'.','');
 163          }
 164          return $productDetails;
 165      }
 166  
 167      /**
 168       * Function to get Tax Class Details for this record(Product)
 169       * @return <Array> List of Taxes
 170       */
 171  	public function getTaxClassDetails() {
 172          $taxClassDetails = $this->get('taxClassDetails');
 173          if (!empty($taxClassDetails)) {
 174              return $taxClassDetails;
 175          }
 176  
 177          $record = $this->getId();
 178          if (empty ($record)) {
 179              return $this->getAllTaxes();
 180          }
 181  
 182          $taxClassDetails = getTaxDetailsForProduct($record, 'available_associated');
 183          $noOfTaxes = count($taxClassDetails);
 184  
 185          for($i=0; $i<$noOfTaxes; $i++) {
 186              $taxValue = getProductTaxPercentage($taxClassDetails[$i]['taxname'], $this->getId());
 187              $taxClassDetails[$i]['percentage'] = $taxValue;
 188              $taxClassDetails[$i]['check_name'] = $taxClassDetails[$i]['taxname'].'_check';
 189              $taxClassDetails[$i]['check_value'] = 1;
 190              //if the tax is not associated with the product then we should get the default value and unchecked
 191              if($taxValue == '') {
 192                  $taxClassDetails[$i]['check_value'] = 0;
 193                  $taxClassDetails[$i]['percentage'] = getTaxPercentage($taxClassDetails[$i]['taxname']);
 194              }
 195          }
 196  
 197          $this->set('taxClassDetails', $taxClassDetails);
 198          return $taxClassDetails;
 199      }
 200  
 201      /**
 202       * Function to get all taxes
 203       * @return <Array> List of taxes
 204       */
 205  	public function getAllTaxes() {
 206          $allTaxesList = $this->get('alltaxes');
 207          if (!empty($allTaxesList)) {
 208              return $allTaxesList;
 209          }
 210  
 211          $allTaxesList = getAllTaxes('available');
 212          $noOfTaxes = count($allTaxesList);
 213  
 214          for($i=0; $i<$noOfTaxes; $i++) {
 215              $allTaxesList[$i]['check_name'] = $allTaxesList[$i]['taxname'].'_check';
 216              $allTaxesList[$i]['check_value'] = 0;
 217          }
 218  
 219          $this->set('alltaxes', $allTaxesList);
 220          return $allTaxesList;
 221      }
 222  
 223      /**
 224       * Function to get price details
 225       * @return <Array> List of prices
 226       */
 227  	public function getPriceDetails() {
 228          $priceDetails = $this->get('priceDetails');
 229          if (!empty($priceDetails)) {
 230              return $priceDetails;
 231          }
 232          $priceDetails = getPriceDetailsForProduct($this->getId(), $this->get('unit_price'), 'available', $this->getModuleName());
 233          $this->set('priceDetails', $priceDetails);
 234          return $priceDetails;
 235      }
 236  
 237      /**
 238       * Function to get base currency details
 239       * @return <Array>
 240       */
 241  	public function getBaseCurrencyDetails() {
 242          $baseCurrencyDetails = $this->get('baseCurrencyDetails');
 243          if (!empty($baseCurrencyDetails)) {
 244              return $baseCurrencyDetails;
 245          }
 246  
 247          $recordId = $this->getId();
 248          if (!empty($recordId)) {
 249              $baseCurrency = getProductBaseCurrency($recordId, $this->getModuleName());
 250          } else {
 251              $currentUserModel = Users_Record_Model::getCurrentUserModel();
 252              $baseCurrency = fetchCurrency($currentUserModel->getId());
 253          }
 254          $baseCurrencyDetails = array('currencyid' => $baseCurrency);
 255  
 256          $baseCurrencySymbolDetails = getCurrencySymbolandCRate($baseCurrency);
 257          $baseCurrencyDetails = array_merge($baseCurrencyDetails, $baseCurrencySymbolDetails);
 258          $this->set('baseCurrencyDetails', $baseCurrencyDetails);
 259  
 260          return $baseCurrencyDetails;
 261      }
 262  
 263      /**
 264       * Function to get Image Details
 265       * @return <array> Image Details List
 266       */
 267  	public function getImageDetails() {
 268          $db = PearDatabase::getInstance();
 269          $imageDetails = array();
 270          $recordId = $this->getId();
 271  
 272          if ($recordId) {
 273              $sql = "SELECT vtiger_attachments.*, vtiger_crmentity.setype FROM vtiger_attachments
 274                          INNER JOIN vtiger_seattachmentsrel ON vtiger_seattachmentsrel.attachmentsid = vtiger_attachments.attachmentsid
 275                          INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_attachments.attachmentsid
 276                          WHERE vtiger_crmentity.setype = 'Products Image' AND vtiger_seattachmentsrel.crmid = ?";
 277  
 278              $result = $db->pquery($sql, array($recordId));
 279              $count = $db->num_rows($result);
 280  
 281              for($i=0; $i<$count; $i++) {
 282                  $imageIdsList[] = $db->query_result($result, $i, 'attachmentsid');
 283                  $imagePathList[] = $db->query_result($result, $i, 'path');
 284                  $imageName = $db->query_result($result, $i, 'name');
 285  
 286                  //decode_html - added to handle UTF-8 characters in file names
 287                  $imageOriginalNamesList[] = decode_html($imageName);
 288  
 289                  //urlencode - added to handle special characters like #, %, etc.,
 290                  $imageNamesList[] = $imageName;
 291              }
 292  
 293              if(is_array($imageOriginalNamesList)) {
 294                  $countOfImages = count($imageOriginalNamesList);
 295                  for($j=0; $j<$countOfImages; $j++) {
 296                      $imageDetails[] = array(
 297                              'id' => $imageIdsList[$j],
 298                              'orgname' => $imageOriginalNamesList[$j],
 299                              'path' => $imagePathList[$j].$imageIdsList[$j],
 300                              'name' => $imageNamesList[$j]
 301                      );
 302                  }
 303              }
 304          }
 305          return $imageDetails;
 306      }
 307      
 308      /**
 309       * Static Function to get the list of records matching the search key
 310       * @param <String> $searchKey
 311       * @return <Array> - List of Vtiger_Record_Model or Module Specific Record Model instances
 312       */
 313  	public static function getSearchResult($searchKey, $module=false) {
 314          $db = PearDatabase::getInstance();
 315  
 316          $query = 'SELECT label, crmid, setype, createdtime FROM vtiger_crmentity WHERE label LIKE ? AND vtiger_crmentity.deleted = 0';
 317          $params = array("%$searchKey%");
 318  
 319          if($module !== false) {
 320              $query .= ' AND setype = ?';
 321              if($module == 'Products'){
 322                  $query = 'SELECT label, crmid, setype, createdtime FROM vtiger_crmentity INNER JOIN vtiger_products ON 
 323                              vtiger_products.productid = vtiger_crmentity.crmid WHERE label LIKE ? AND vtiger_crmentity.deleted = 0 
 324                              AND vtiger_products.discontinued = 1 AND setype = ?';
 325              }else if($module == 'Services'){
 326                  $query = 'SELECT label, crmid, setype, createdtime FROM vtiger_crmentity INNER JOIN vtiger_service ON 
 327                              vtiger_service.serviceid = vtiger_crmentity.crmid WHERE label LIKE ? AND vtiger_crmentity.deleted = 0 
 328                              AND vtiger_service.discontinued = 1 AND setype = ?';
 329              }
 330              $params[] = $module;
 331          }
 332          //Remove the ordering for now to improve the speed
 333          //$query .= ' ORDER BY createdtime DESC';
 334  
 335          $result = $db->pquery($query, $params);
 336          $noOfRows = $db->num_rows($result);
 337  
 338          $moduleModels = $matchingRecords = $leadIdsList = array();
 339          for($i=0; $i<$noOfRows; ++$i) {
 340              $row = $db->query_result_rowdata($result, $i);
 341              if ($row['setype'] === 'Leads') {
 342                  $leadIdsList[] = $row['crmid'];
 343              }
 344          }
 345          $convertedInfo = Leads_Module_Model::getConvertedInfo($leadIdsList);
 346  
 347          for($i=0, $recordsCount = 0; $i<$noOfRows && $recordsCount<100; ++$i) {
 348              $row = $db->query_result_rowdata($result, $i);
 349              if ($row['setype'] === 'Leads' && $convertedInfo[$row['crmid']]) {
 350                  continue;
 351              }
 352              if(Users_Privileges_Model::isPermitted($row['setype'], 'DetailView', $row['crmid'])) {
 353                  $row['id'] = $row['crmid'];
 354                  $moduleName = $row['setype'];
 355                  if(!array_key_exists($moduleName, $moduleModels)) {
 356                      $moduleModels[$moduleName] = Vtiger_Module_Model::getInstance($moduleName);
 357                  }
 358                  $moduleModel = $moduleModels[$moduleName];
 359                  $modelClassName = Vtiger_Loader::getComponentClassName('Model', 'Record', $moduleName);
 360                  $recordInstance = new $modelClassName();
 361                  $matchingRecords[$moduleName][$row['id']] = $recordInstance->setData($row)->setModuleFromInstance($moduleModel);
 362                  $recordsCount++;
 363              }
 364          }
 365          return $matchingRecords;
 366      }
 367      
 368      /**
 369       * Function to get acive status of record
 370       */
 371  	public function getActiveStatusOfRecord(){
 372          $activeStatus = $this->get('discontinued');
 373          if($activeStatus){
 374              return $activeStatus;
 375          }
 376          $recordId = $this->getId();
 377          $db = PearDatabase::getInstance();
 378          $result = $db->pquery('SELECT discontinued FROM vtiger_products WHERE productid = ?',array($recordId));
 379          $activeStatus = $db->query_result($result, 'discontinued');
 380          return $activeStatus;
 381      }
 382      
 383      /**
 384       * Function updates ListPrice for Product/Service-PriceBook relation
 385       * @param <Integer> $relatedRecordId - PriceBook Id
 386       * @param <Integer> $price - listprice
 387       * @param <Integer> $currencyId - currencyId
 388       */
 389  	function updateListPrice($relatedRecordId, $price, $currencyId) {
 390          $db = PearDatabase::getInstance();
 391  
 392          $result = $db->pquery('SELECT * FROM vtiger_pricebookproductrel WHERE pricebookid = ? AND productid = ?',
 393                  array($relatedRecordId, $this->getId()));
 394          if($db->num_rows($result)) {
 395               $db->pquery('UPDATE vtiger_pricebookproductrel SET listprice = ? WHERE pricebookid = ? AND productid = ?',
 396                       array($price, $relatedRecordId, $this->getId()));
 397          } else {
 398              $db->pquery('INSERT INTO vtiger_pricebookproductrel (pricebookid,productid,listprice,usedcurrency) values(?,?,?,?)',
 399                      array($relatedRecordId, $this->getId(), $price, $currencyId));
 400          }
 401      }
 402  }


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