<?php

class CartridgeMod extends DbMod{
  var $VarTableCartridgeTypes = array('integer', 'integer', 'text', 'text', 'integer', 'integer', 'text', 'date', 'date');

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

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*, b.cartridgetype_name, b.cartridgetype_writeoff, c.svt_buxname, d.organiz_name';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableCartridgeType .' AS b, '. $this->VarTableCartridge .' AS a';
    $ArrSqlArray['SqlAll']['leftjoin'] = $this->VarTableSvt .' AS c ON a.svt_id = c.svt_id
                                         LEFT JOIN '. $this->VarTableOrganiz .' AS d ON a.organiz_id = d.organiz_id';
    $ArrSqlArray['SqlAll']['where'] = 'a.cartridgetype_id = b.cartridgetype_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] .= ' AND '. $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 b, '. $this->VarTableCartridge .' AS a';
    $ArrSqlArray['SqlCount']['leftjoin'] = $this->VarTableSvt .' AS c ON a.svt_id = c.svt_id
                                         LEFT JOIN '. $this->VarTableOrganiz .' AS d ON a.organiz_id = d.organiz_id';
    $ArrSqlArray['SqlCount']['where'] = 'a.cartridgetype_id = b.cartridgetype_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

   function Add($values){
    if(true === is_array($values) && 0 < count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableCartridge .'
                (cartridge_id, svt_id, organiz_id, cartridge_name, cartridge_serialnum, cartridgetype_id, cartridge_writeoff, cartridge_note, cartridge_loaddate, cartridge_buydate)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';  
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['svt_id'],
        $values['organiz_id'],
        $values['cartridge_name'],
        $values['cartridge_serialnum'],
        $values['cartridgetype_id'],
        $values['cartridge_writeoff'],
        $values['cartridge_note'],
        $values['cartridge_loaddate'],
        $values['cartridge_buydate']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableCartridgeTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableCartridge, 'cartridge_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->VarTableCartridge ." SET
                  svt_id = ?,
                  organiz_id = ?,
                  cartridge_name = ?,
                  cartridge_serialnum = ?,
                  cartridgetype_id = ?,
                  cartridge_writeoff = ?,
                  cartridge_note = ?,
                  cartridge_loaddate = ?,
                  cartridge_buydate = ?
                  WHERE cartridge_id = '".  $values['cartridge_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['svt_id'],
        $values['organiz_id'],
        $values['cartridge_name'],
        $values['cartridge_serialnum'],
        $values['cartridgetype_id'],
        $values['cartridge_writeoff'],
        $values['cartridge_note'],
        $values['cartridge_loaddate'],
        $values['cartridge_buydate'],
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['cartridge_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'cartridge_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableCartridge ." 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['svt_id'] = NULL;
      if(true === isset($values['FrmAgreeSvtCheckboxes']) && '' != $values['FrmAgreeSvtCheckboxes']){
        $values['svt_id'] = current($values['FrmAgreeSvtCheckboxes']);
      }
      $values['organiz_id'] = NULL;
      if(true === isset($values['FrmAgreeOrganizCheckboxes']) && '' != $values['FrmAgreeOrganizCheckboxes']){
        $values['organiz_id'] = current($values['FrmAgreeOrganizCheckboxes']);
      }
      $values['cartridgetype_id'] = NULL;
      if(true === isset($values['FrmAgreeCartridgeTypeCheckboxes']) && '' != $values['FrmAgreeCartridgeTypeCheckboxes']){
        $values['cartridgetype_id'] = current($values['FrmAgreeCartridgeTypeCheckboxes']);
      }
      $values['cartridge_loaddate'] = SiteLibs::ConvertSqlDateInsert($values['cartridge_loaddate']);
      $values['cartridge_buydate'] = SiteLibs::ConvertSqlDateInsert($values['cartridge_buydate']);
    }
    return $values;
  }
}
?>