[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Settings/Vtiger/models/ -> TaxRecord.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 Settings_Vtiger_TaxRecord_Model extends Vtiger_Base_Model{
  12      
  13      const PRODUCT_AND_SERVICE_TAX = 0;
  14      const SHIPPING_AND_HANDLING_TAX = 1;
  15     
  16      
  17      public function __construct($values = array()) {
  18          parent::__construct($values);
  19          $this->unMarkDeleted();
  20      }
  21      
  22      
  23      private $type;
  24      
  25     
  26      public function getId() {
  27          return $this->get('taxid');
  28      }
  29      
  30      public function getName() {
  31          return $this->get('taxlabel');
  32      }
  33      
  34  	public function getTax() {
  35          return $this->get('percentage');
  36      }
  37      
  38      public function isDeleted() {
  39          return $this->get('deleted') == 0 ? false : true;
  40      }
  41      
  42      public function markDeleted() {
  43          return $this->set('deleted','1');
  44      }
  45      
  46      public function unMarkDeleted() {
  47          return $this->set('deleted','0');
  48      }
  49      
  50      public function setType($type) {
  51          $this->type = $type;
  52          return $this;
  53      }
  54      
  55      public function getType() {
  56          return $this->type;
  57      }
  58      
  59      public function isProductTax() {
  60          return ($this->getType() == self::PRODUCT_AND_SERVICE_TAX) ? true : false;
  61      }
  62      
  63      public function isShippingTax() {
  64          return ($this->getType() == self::SHIPPING_AND_HANDLING_TAX) ? true : false;
  65      }
  66      
  67  	public function getCreateTaxUrl() {
  68          return '?module=Vtiger&parent=Settings&view=TaxAjax';
  69      }
  70      
  71  	public function getEditTaxUrl() {
  72          return '?module=Vtiger&parent=Settings&view=TaxAjax&type='.$this->getType().'&taxid='.$this->getId();
  73      }
  74      
  75      private function getTableNameFromType(){
  76          $tablename = 'vtiger_inventorytaxinfo';
  77          
  78          if($this->isShippingTax()) {
  79              $tablename = 'vtiger_shippingtaxinfo';
  80          }
  81          return $tablename;
  82      }
  83      
  84      public function save() {
  85          $db = PearDatabase::getInstance();
  86          
  87          $tablename = $this->getTableNameFromType();
  88          
  89          $taxId = $this->getId();
  90          
  91          if(!empty($taxId)) {
  92              $deleted = 0;
  93              if($this->isDeleted()) {
  94                  $deleted = 1;
  95              }
  96              $query = 'UPDATE '.$tablename.' SET taxlabel=?,percentage=?,deleted=? WHERE taxid=?';
  97              $params = array($this->getName(),$this->get('percentage'),$deleted,$taxId);
  98              $db->pquery($query,$params);
  99          }else{
 100              $taxId = $this->addTax();   
 101          }
 102          return $taxId;
 103      }
 104      
 105      /**    Function used to add the tax type which will do database alterations

 106       *    @param string $taxlabel - tax label name to be added

 107       *    @param string $taxvalue - tax value to be added

 108       *      @param string $sh - sh or empty , if sh passed then the tax will be added in shipping and handling related table

 109       *      @return void

 110       */
 111      public function addTax() {
 112          $adb = PearDatabase::getInstance();
 113  
 114          $tableName = $this->getTableNameFromType();
 115          $taxid = $adb->getUniqueID($tableName);
 116          $taxLabel = $this->getName();
 117          $percentage = $this->get('percentage');
 118          
 119          //if the tax is not available then add this tax.

 120          //Add this tax as a column in related table    

 121          if($this->isShippingTax()) {
 122              $taxname = "shtax".$taxid;
 123              $query = "ALTER TABLE vtiger_inventoryshippingrel ADD COLUMN $taxname decimal(7,3) DEFAULT NULL";
 124          } else {
 125              $taxname = "tax".$taxid;
 126              $query = "ALTER TABLE vtiger_inventoryproductrel ADD COLUMN $taxname decimal(7,3) DEFAULT NULL";
 127          }
 128          $res = $adb->pquery($query, array());
 129          
 130          vimport('~~/include/utils/utils.php');
 131          
 132          if ($this->isProductTax()) {
 133              // TODO Review: if field addition is required in shipping-tax case too.

 134              // NOTE: shtax1, shtax2, shtax3 that is added as default should also be taken care.

 135              
 136              $inventoryModules = getInventoryModules();
 137              foreach ($inventoryModules as $moduleName) {
 138                  $moduleInstance = Vtiger_Module::getInstance($moduleName);
 139                  $blockInstance = Vtiger_Block::getInstance('LBL_ITEM_DETAILS',$moduleInstance);
 140                  $field = new Vtiger_Field();
 141  
 142                  $field->name = $taxname;
 143                  $field->label = $taxLabel;
 144                  $field->column = $taxname;
 145                  $field->table = 'vtiger_inventoryproductrel';
 146                  $field->uitype = '83';
 147                  $field->typeofdata = 'V~O';
 148                  $field->readonly = '0';
 149                  $field->displaytype = '5';
 150                  $field->masseditable = '0';
 151  
 152                  $blockInstance->addField($field);
 153              }
 154          }
 155  
 156          //if the tax is added as a column then we should add this tax in the list of taxes

 157          if($res) {
 158              $query = 'INSERT INTO '.$tableName.' values(?,?,?,?,?)';
 159              $params = array($taxid, $taxname, $taxLabel, $percentage, 0);
 160              $adb->pquery($query, $params);
 161              return $taxid;
 162          }
 163          throw new Error('Error occurred while adding tax');
 164      }
 165      
 166      public static function getProductTaxes() {
 167          vimport('~~/include/utils/InventoryUtils.php');
 168          $taxes = getAllTaxes();
 169          $recordList = array();
 170          foreach($taxes as $taxInfo) {
 171              $taxRecord = new self();
 172              $taxRecord->setData($taxInfo)->setType(self::PRODUCT_AND_SERVICE_TAX);
 173              $recordList[] = $taxRecord;
 174          }
 175          return $recordList;
 176      }
 177      
 178      public static function getShippingTaxes() {
 179          vimport('~~/include/utils/InventoryUtils.php');
 180          $taxes = getAllTaxes('all','sh');
 181          $recordList = array();
 182          foreach($taxes as $taxInfo) {
 183              $taxRecord = new self();
 184              $taxRecord->setData($taxInfo)->setType(self::SHIPPING_AND_HANDLING_TAX);
 185              $recordList[] = $taxRecord;
 186          }
 187          return $recordList;
 188      }
 189      
 190      public static function getInstanceById($id, $type = self::PRODUCT_AND_SERVICE_TAX) {
 191          $db = PearDatabase::getInstance();
 192          $tablename = 'vtiger_inventorytaxinfo';
 193          
 194          if($type == self::SHIPPING_AND_HANDLING_TAX) {
 195              $tablename = 'vtiger_shippingtaxinfo';
 196          }
 197          
 198          $query = 'SELECT * FROM '.$tablename.' WHERE taxid=?';
 199          $result = $db->pquery($query,array($id));
 200          $taxRecordModel = new self();
 201          if($db->num_rows($result) > 0) {
 202              $row = $db->query_result_rowdata($result,0);
 203              $taxRecordModel->setData($row)->setType($type);
 204          }
 205          return $taxRecordModel;
 206      }
 207      
 208  	public static function checkDuplicate($label, $excludedIds = array(), $type = self::PRODUCT_AND_SERVICE_TAX) {
 209          $db = PearDatabase::getInstance();
 210          
 211          if(!is_array($excludedIds)) {
 212              if(!empty($excludedIds)){
 213                  $excludedIds = array($excludedIds);
 214              }else{
 215                  $excludedIds = array();
 216              }
 217          }
 218          $tablename = 'vtiger_inventorytaxinfo';
 219          
 220          if($type == self::SHIPPING_AND_HANDLING_TAX) {
 221              $tablename = 'vtiger_shippingtaxinfo';
 222          }
 223          
 224          $query = 'SELECT 1 FROM '.$tablename.' WHERE taxlabel = ?';
 225          $params = array($label);
 226  
 227          if (!empty($excludedIds)) {
 228              $query .= " AND taxid NOT IN (". generateQuestionMarks($excludedIds). ")";
 229              $params = array_merge($params, $excludedIds);
 230          }
 231          $result = $db->pquery($query,$params);
 232          return ($db->num_rows($result) > 0) ? true : false;
 233      }
 234      
 235  }


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