<?php

class SvtTypeMod extends DbMod{
  var $VarTableSvtTypeTypes = array('integer', 'text', 'integer');
  
  function SvtTypeMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.svttype_id as id, a.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableSvtType .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableSvtType .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
//    $ArrSqlArray['SqlWriteoffAll']['select'] = 'a.*, count(b.svttype_id) AS svt_count';
//    $ArrSqlArray['SqlWriteoffAll']['from'] = $this->VarTableSvtType .' AS a';
//    $ArrSqlArray['SqlWriteoffAll']['leftjoin'] = $this->VarTableSvt .' AS b ON a.svttype_id = b.svttype_id AND YEAR(b.svt_date) < YEAR(NOW())-a.svttype_writeoff+1';
//    if('' != $VarWhere){
//      $ArrSqlArray['SqlWriteoffAll']['where'] = $VarWhere;
//    }
    ///
    $ArrSqlArray['SqlSvtTypeAll']['select'] = 'a.svttype_id as id, a.*, count(b.svttype_id) AS svt_count';
    $ArrSqlArray['SqlSvtTypeAll']['from'] = $this->VarTableSvtType .' AS a';
    $ArrSqlArray['SqlSvtTypeAll']['leftjoin'] = $this->VarTableSvt .' AS b ON a.svttype_id = b.svttype_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlSvtTypeAll']['where'] = $VarWhere;
    }
    $ArrSqlArray['SqlSvtTypeAll']['group'] = 'a.svttype_id';
    if('' != $VarOrder){
      $ArrSqlArray['SqlSvtTypeAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlSvtTypeAll']['limit'] = $VarLimit;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($values){
    if(true === is_array($values) && 0 < count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableSvtType .'
                (svttype_id, svttype_name, svttype_writeoff)
                VALUES (?, ?, ?)';
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableSvtTypeTypes, MDB2_PREPARE_MANIP);
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['svttype_name'],
        $values['svttype_writeoff']
      );
      //SiteLibs::GetVarDump($data);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableSvtType, 'svttype_id');
      if (PEAR::isError($VarId)) {
        return array('error' => $VarId->getMessage());
      }
      return $VarId;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Edit($values){
    if(true === is_array($values) && 0 < count($values)){
      $VarSql = "UPDATE ". $this->VarTableSvtType ." SET
                  svttype_name = ?,
                  svttype_writeoff = ?
                  WHERE svttype_id = '".  $values['svttype_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['svttype_name'],
        $values['svttype_writeoff']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['svttype_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'svttype_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableSvtType ." WHERE ". $VarFiledName ." = '". $VarId ."'";
      //echo "sql: ". $sql ."<br />";
      $VarQueryResult = $this->ArrMsSp->SP_DbConnect->exec($sql);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return true;
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
  
  function PrepareData($values){
    if(true === is_array($values) && 0 < count($values)){
      $values['svttype_name'] = SiteLibs::ConvertTextToSql($values['svttype_name']);
      $values['svttype_writeoff'] = SiteLibs::ConvertSqlIntegerInsert($values['svttype_writeoff']);
    }
    return $values;
  }
}
?>