<?php

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

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*, b.users_fio, c.svtgiveoutstatus_name, c.svtgiveoutstatus_color, g.users_fio AS master_fio, s.svt_name, s.svt_buxname, s.svt_invnum, s.svt_serialnum';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableSvtGiveout .' AS a, '. $this->VarTableUsers .' AS b, '. $this->VarTableSvtGiveoutStatus .' AS c, '. $this->VarTableUsers .' AS g, '. $this->VarTableSvt .' AS s';
    $ArrSqlArray['SqlAll']['where'] = 'a.users_id = b.users_id AND a.svtgiveoutstatus_id = c.svtgiveoutstatus_id AND a.demands_masterid = g.users_id AND a.svt_id = s.svt_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->VarTableSvtGiveout .' AS a, '. $this->VarTableUsers .' AS b, '. $this->VarTableSvtGiveoutStatus .' AS c, '. $this->VarTableUsers .' AS g, '. $this->VarTableSvt .' AS s';
    $ArrSqlArray['SqlCount']['where'] = 'a.users_id = b.users_id AND a.svtgiveoutstatus_id = c.svtgiveoutstatus_id AND a.demands_masterid = g.users_id AND a.svt_id = s.svt_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrSqlArray['SqlForFirstPageCount']['select'] = 'count(a.svtgiveout_id) AS cnt, b.svtgiveoutstatus_id, b.svtgiveoutstatus_name, b.svtgiveoutstatus_color';
    $ArrSqlArray['SqlForFirstPageCount']['from'] = $this->VarTableSvtGiveoutStatus.' AS b, '. $this->VarTableSvtGiveout .' AS a';
    $ArrSqlArray['SqlForFirstPageCount']['where'] = 'a.svtgiveoutstatus_id = b.svtgiveoutstatus_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlForFirstPageCount']['where'] .= ' AND '. $VarWhere;
    }
    $ArrSqlArray['SqlForFirstPageCount']['group'] = 'a.svtgiveoutstatus_id';
    if('' != $VarOrder){
      $ArrSqlArray['SqlForFirstPageCount']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlForFirstPageCount']['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->VarTableSvtGiveout .'
                (svtgiveout_id, demands_masterid, svt_id, users_id, svtgiveoutstatus_id, svtgiveout_note, svtgiveout_date_b, svtgiveout_date_e, svtgiveout_date)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['demands_masterid'],
        $values['svt_id'],
        $values['users_id'],
        $values['svtgiveoutstatus_id'],
        $values['svtgiveout_note'],
        $values['svtgiveout_date_b'],
        $values['svtgiveout_date_e'],
        $values['svtgiveout_date']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableSvtGiveoutTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableSvtGiveout, 'svtgiveout_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->VarTableSvtGiveout ." SET 
                  demands_masterid = ?,
                  svt_id = ?,
                  users_id = ?,
                  svtgiveoutstatus_id = ?,
                  svtgiveout_note = ?,
                  svtgiveout_date_b = ?,
                  svtgiveout_date_e = ?
                WHERE svtgiveout_id ='".  $values['svtgiveout_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
                  $values['demands_masterid'],
                  $values['svt_id'],
                  $values['users_id'],
                  $values['svtgiveoutstatus_id'],
                  $values['svtgiveout_note'],
                  $values['svtgiveout_date_b'],
                  $values['svtgiveout_date_e']
                );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['svtgiveout_id'];
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'svtgiveout_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableSvtGiveout ." 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['demands_masterid'] = NULL;
      if(true === isset($values['FrmAgreeMasterCheckboxes']) && '' != $values['FrmAgreeMasterCheckboxes']){
        $values['demands_masterid'] = current($values['FrmAgreeMasterCheckboxes']);
      }
      $values['svt_id'] = NULL;
      if(true === isset($values['FrmAgreeSvtCheckboxes']) && '' != $values['FrmAgreeSvtCheckboxes']){
        $values['svt_id'] = current($values['FrmAgreeSvtCheckboxes']);
      }
      $values['users_id'] = NULL;
      if(true === isset($values['FrmAgreeUsersCheckboxes']) && '' != $values['FrmAgreeUsersCheckboxes']){
        $values['users_id'] = current($values['FrmAgreeUsersCheckboxes']);
      }
      $values['svtgiveoutstatus_id'] = NULL;
      if(true === isset($values['FrmAgreeSvtGiveoutStatusCheckboxes']) && '' != $values['FrmAgreeSvtGiveoutStatusCheckboxes']){
        $values['svtgiveoutstatus_id'] = current($values['FrmAgreeSvtGiveoutStatusCheckboxes']);
      }
      $values['svtgiveout_date_b'] = SiteLibs::ConvertSqlDateInsert($values['svtgiveout_date_b']);
      $values['svtgiveout_date_e'] = SiteLibs::ConvertSqlDateInsert($values['svtgiveout_date_e']);
      $values['svtgiveout_date'] = date('YmdHis');
    }
    return $values;
  }
}
?>