<?php

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

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

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*, b.po_name, c.organiz_name';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTablePo .' AS b, '. $this->VarTablePoLicence .' AS a';
    $ArrSqlArray['SqlAll']['leftjoin'] = $this->VarTableOrganiz .' AS c ON a.organiz_id = c.organiz_id';
    $ArrSqlArray['SqlAll']['where'] = 'a.po_id = b.po_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->VarTablePo .' AS b, '. $this->VarTablePoLicence .' AS a';
    $ArrSqlArray['SqlCount']['where'] = 'a.po_id = b.po_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->VarTablePoLicence .'
                (policence_id, po_id, organiz_id, policence_name, policence_number, policence_note, policence_dateb, policence_datee)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['po_id'],
        $values['organiz_id'],
        $values['policence_name'],
        $values['policence_number'],
        $values['policence_note'],
        $values['policence_dateb'],
        $values['policence_datee']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTablePoLicenceTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTablePoLicence, 'policence_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->VarTablePoLicence ." SET
                  po_id = ?,
                  organiz_id = ?,
                  policence_name = ?,
                  policence_number = ?,
                  policence_note = ?,
                  policence_dateb = ?,
                  policence_datee = ?
                  WHERE policence_id = '".  $values['policence_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['po_id'],
        $values['organiz_id'],
        $values['policence_name'],
        $values['policence_number'],
        $values['policence_note'],
        $values['policence_dateb'],
        $values['policence_datee']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['policence_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'policence_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTablePoLicence ." 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) && 1 <= count($values)){
      $values['po_id'] = NULL;
      if(true === isset($values['FrmAgreePoCheckboxes']) && '' != $values['FrmAgreePoCheckboxes']){
        $values['po_id'] = current($values['FrmAgreePoCheckboxes']);
      }
      $values['organiz_id'] = NULL;
      if(true === isset($values['FrmAgreeOrganizCheckboxes']) && '' != $values['FrmAgreeOrganizCheckboxes']){
        $values['organiz_id'] = current($values['FrmAgreeOrganizCheckboxes']);
      }
      $values['policence_dateb'] = SiteLibs::ConvertSqlDateInsert($values['policence_dateb']);
      $values['policence_datee'] = SiteLibs::ConvertSqlDateInsert($values['policence_datee']);
    }
    return $values;
  }
}
?>