<?php

class CartridgeTypeMod extends DbMod{
  var $VarTableCartridgeTypeTypes = array('integer', 'text', 'text', 'integer', 'integer');

	function CartridgeTypeMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableCartridgeType .' 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->VarTableCartridgeType .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlWriteoffAll']['select'] = 'a.*, count(b.cartridgetype_id) AS cartridge_count';
    $ArrSqlArray['SqlWriteoffAll']['from'] = $this->VarTableCartridgeType .' AS a';
    $ArrSqlArray['SqlWriteoffAll']['leftjoin'] = $this->VarTableCartridge .' AS b ON a.cartridgetype_id = b.cartridgetype_id AND YEAR(b.cartridge_date) < YEAR(NOW())-a.cartridgetype_writeoff+1';
    if('' != $VarWhere){
      $ArrSqlArray['SqlWriteoffAll']['where'] = $VarWhere;
    }
    $ArrSqlArray['SqlWriteoffAll']['group'] = 'a.cartridgetype_id';
    if('' != $VarOrder){
      $ArrSqlArray['SqlWriteoffAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlWriteoffAll']['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->VarTableCartridgeType .'
                (cartridgetype_id, cartridgetype_name, cartridgetype_partcode, cartridgetype_numprintlist, cartridgetype_writeoff)
                VALUES (?, ?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['cartridgetype_name'],
        $values['cartridgetype_partcode'],
        $values['cartridgetype_numprintlist'],
        $values['cartridgetype_writeoff']
      );
      //SiteLibs::GetVarDump($data);
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableCartridgeTypeTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableCartridgeType, 'cartridgetype_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->VarTableCartridgeType ." SET
                  cartridgetype_name = ?,
                  cartridgetype_partcode = ?,
                  cartridgetype_numprintlist = ?,
                  cartridgetype_writeoff = ?
                  WHERE cartridgetype_id = '".  $values['cartridgetype_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['cartridgetype_name'],
        $values['cartridgetype_partcode'],
        $values['cartridgetype_numprintlist'],
        $values['cartridgetype_writeoff']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['cartridgetype_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'cartridgetype_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableCartridgeType ." 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)){
     
    }
    return $values;
  }
}
?>