[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/Webservices/LineItem/ -> VtigerTaxOperation.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  require_once  "include/Webservices/VtigerActorOperation.php";
  13  require_once  'include/Webservices/LineItem/VtigerTaxMeta.php';
  14  require_once ("include/events/include.inc");
  15  require_once  'modules/com_vtiger_workflow/VTEntityCache.inc';
  16  require_once  'data/CRMEntity.php';
  17  require_once  'include/events/SqlResultIterator.inc';
  18  require_once  'include/Webservices/LineItem/VtigerLineItemMeta.php';
  19  require_once  'include/Webservices/Retrieve.php';
  20  require_once  'include/Webservices/Update.php';
  21  require_once  'include/Webservices/Utils.php';
  22  require_once  'modules/Emails/mail.php';
  23  
  24  
  25  /**
  26   * Description of VtigerTaxOperation
  27   */
  28  class VtigerTaxOperation  extends VtigerActorOperation {
  29  
  30  	public function __construct($webserviceObject, $user, $adb, $log) {
  31          parent::__construct($webserviceObject,$user,$adb,$log);
  32          $this->entityTableName = $this->getActorTables();
  33          if($this->entityTableName === null){
  34              throw new WebServiceException(WebServiceErrorCode::$UNKOWNENTITY,"Entity is not associated with any tables");
  35          }
  36          $this->meta = new VtigerTaxMeta($this->entityTableName,$webserviceObject,$adb,$user);
  37          $this->moduleFields = null;
  38      }
  39  
  40  	public function create($elementType, $element) {
  41          $element = $this->restrictFields($element);
  42          $taxName = $this->getNewTaxName();
  43          $element['taxname'] = $taxName;
  44          $element['deleted'] = 0;
  45          $createdElement = parent::create($elementType, $element);
  46          $sql = "alter table vtiger_inventoryproductrel add column $taxName decimal(7,3)";
  47          $result = $this->pearDB->pquery($sql,array());
  48          if(!is_object($result)) {
  49              list($typeId,$id) = vtws_getIdComponents($element['id']);
  50              $this->dropRow($id);
  51              throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR,
  52                  "Database error while adding tax column($taxName) for inventory lineitem table");
  53          }
  54          return $createdElement;
  55      }
  56  
  57  	public function update($element) {
  58          $element['taxname'] = $this->getCurrentTaxName();
  59          return parent::update($element);
  60      }
  61  
  62  	public function delete($id) {
  63          $ids = vtws_getIdComponents($id);
  64          $elemId = $ids[1];
  65  
  66          $result = null;
  67          $query = 'update '.$this->entityTableName.' set deleted=1 where '.$this->meta->getObectIndexColumn().'=?';
  68          $transactionSuccessful = vtws_runQueryAsTransaction($query,array($elemId),$result);
  69          if(!$transactionSuccessful){
  70              throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR,
  71                  "Database error while performing required operation");
  72          }
  73          return array("status"=>"successful");
  74      }
  75  
  76  	private function dropRow($id) {
  77          $sql = 'delete from vtiger_inventorytaxinfo where taxid = ?';
  78          $params = array($id);
  79          $result = $this->pearDB->pquery($sql, $params);
  80      }
  81  
  82  	private function getCurrentTaxName() {
  83          $sql = 'select taxname from vtiger_inventorytaxinfo order by taxid desc limit 1';
  84          $params = array();
  85          $result = $this->pearDB->pquery($sql, $params);
  86          $it = new SqlResultIterator($this->pearDB, $result);
  87          $currentTaxName = null;
  88          foreach ($it as $row) {
  89              $currentTaxName = $row->taxname;
  90          }
  91          return $currentTaxName;
  92      }
  93  
  94  	private function getNewTaxName() {
  95          $currentTaxName = $this->getCurrentTaxName();
  96  
  97          if(empty($currentTaxName)) {
  98              return 'tax1';
  99          }
 100  
 101          $matches = null;
 102          if ( preg_match('/tax(\d+)/', $currentTaxName, $matches) != 0 ) {
 103              $taxNumber = (int) $matches[1];
 104              $taxNumber++;
 105              return 'tax'.$taxNumber;
 106          }
 107          return 'tax1';
 108      }
 109  
 110  }
 111  ?>


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