<?php

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

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*, b.structureorganization_title, c.organiz_name, d.svttype_name, e.users_fio';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlAll']['leftjoin'] = $this->VarTableStructureOrganization .' AS b ON a.structureorganization_id = b.structureorganization_id 
                                         LEFT JOIN '. $this->VarTableOrganiz .' AS c ON a.organiz_id = c.organiz_id
                                         LEFT JOIN '. $this->VarTableSvtType .' AS d ON a.svttype_id = d.svttype_id
                                         LEFT JOIN '. $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
    //$ArrSqlArray['SqlAll']['where'] = '';
    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->VarTableSvt .' AS a';
    $ArrSqlArray['SqlCount']['leftjoin'] = $this->VarTableStructureOrganization .' AS b ON a.structureorganization_id = b.structureorganization_id 
                                         LEFT JOIN '. $this->VarTableOrganiz .' AS c ON a.organiz_id = c.organiz_id
                                         LEFT JOIN '. $this->VarTableSvtType .' AS d ON a.svttype_id = d.svttype_id
                                         LEFT JOIN '. $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlStatusValAll']['select'] = 'a.*, b.structureorganization_title, c.organiz_name, d.svttype_name, e.users_fio';
    $ArrSqlArray['SqlStatusValAll']['from'] = $this->VarTableSvtStatusVal .' AS f, '. $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlStatusValAll']['leftjoin'] = $this->VarTableStructureOrganization .' AS b ON a.structureorganization_id = b.structureorganization_id 
                                         LEFT JOIN '. $this->VarTableOrganiz .' AS c ON a.organiz_id = c.organiz_id
                                         LEFT JOIN '. $this->VarTableSvtType .' AS d ON a.svttype_id = d.svttype_id
                                         LEFT JOIN '. $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
    $ArrSqlArray['SqlStatusValAll']['where'] = 'a.svt_id = f.svt_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlStatusValAll']['where'] .= ' AND '. $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlStatusValAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlStatusValAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlStatusValCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlStatusValCount']['from'] = $this->VarTableSvtStatusVal .' AS f, '. $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlStatusValCount']['leftjoin'] = $this->VarTableStructureOrganization .' AS b ON a.structureorganization_id = b.structureorganization_id 
                                         LEFT JOIN '. $this->VarTableOrganiz .' AS c ON a.organiz_id = c.organiz_id
                                         LEFT JOIN '. $this->VarTableSvtType .' AS d ON a.svttype_id = d.svttype_id
                                         LEFT JOIN '. $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
    $ArrSqlArray['SqlStatusValCount']['where'] = 'a.svt_id = f.svt_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlStatusValCount']['where'] .= ' AND ' .$VarWhere;
    }
     ///
    $ArrSqlArray['SqlPoAll']['select'] = 'a.*, b.structureorganization_title, c.organiz_name, d.svttype_name, e.users_fio';
    $ArrSqlArray['SqlPoAll']['from'] = $this->VarTableSvtPo .' AS sp, '. $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlPoAll']['leftjoin'] = $this->VarTableStructureOrganization .' AS b ON a.structureorganization_id = b.structureorganization_id 
                                         LEFT JOIN '. $this->VarTableOrganiz .' AS c ON a.organiz_id = c.organiz_id
                                         LEFT JOIN '. $this->VarTableSvtType .' AS d ON a.svttype_id = d.svttype_id
                                         LEFT JOIN '. $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
    $ArrSqlArray['SqlPoAll']['where'] = 'a.svt_id = sp.svt_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlPoAll']['where'] .= ' AND '. $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlPoAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlPoAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlPoCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlPoCount']['from'] = $this->VarTableSvtPo .' AS sp, '. $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlPoCount']['leftjoin'] = $this->VarTableSvtType .' AS d ON a.svttype_id = d.svttype_id';
    $ArrSqlArray['SqlPoCount']['where'] = 'a.svt_id = sp.svt_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlPoCount']['where'] .= ' AND ' .$VarWhere;
    }
    ///
//    $ArrSqlArray['SqlRow']['select'] = 'a.*, b.structureorganization_title, c.organiz_name, d.svttype_name, e.users_fio';
//    $ArrSqlArray['SqlRow']['from'] = $this->VarTableStructureOrganization .' AS b, '. $this->VarTableOrganiz .' AS c, '. $this->VarTableSvtType .' AS d, '. $this->VarTableSvt .' AS a';
//    $ArrSqlArray['SqlRow']['leftjoin'] = $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
//    $ArrSqlArray['SqlRow']['where'] = 'a.structureorganization_id = b.structureorganization_id AND a.organiz_id = c.organiz_id AND a.svttype_id = d.svttype_id';
    $ArrSqlArray['SqlRow']['select'] = 'a.*, b.structureorganization_title, c.organiz_name, d.svttype_name, e.users_fio';
    $ArrSqlArray['SqlRow']['from'] = $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlRow']['leftjoin'] = $this->VarTableStructureOrganization .' AS b ON a.structureorganization_id = b.structureorganization_id
                                         LEFT JOIN '. $this->VarTableOrganiz .' AS c ON a.organiz_id = c.organiz_id
                                         LEFT JOIN '. $this->VarTableSvtType .' AS d ON a.svttype_id = d.svttype_id
                                         LEFT JOIN '. $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
    $ArrSqlArray['SqlRow']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlRow']['where'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrSqlArray['SqlByStatusAll']['select'] = 'a.*, b.structureorganization_title, c.organiz_name, d.svttype_name, e.users_fio';
    $ArrSqlArray['SqlByStatusAll']['from'] = $this->VarTableStructureOrganization .' AS b, '. $this->VarTableOrganiz .' AS c, '. $this->VarTableSvtType .' AS d, '. $this->VarTableSvtStatusVal .' AS f, '. $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlByStatusAll']['leftjoin'] = $this->VarTableUsers .' AS e ON a.users_id = e.users_id';
    $ArrSqlArray['SqlByStatusAll']['where'] = 'a.structureorganization_id = b.structureorganization_id AND a.organiz_id = c.organiz_id AND a.svttype_id = d.svttype_id AND a.svt_id = f.svt_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlByStatusAll']['where'] .= ' AND '. $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlByStatusAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlByStatusAll']['limit'] = $VarLimit;
    }
    ///
    $ArrSqlArray['SqlByStatusCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlByStatusCount']['from'] = $this->VarTableSvtStatusVal .' AS f, '. $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlByStatusCount']['where'] = 'a.svt_id = f.svt_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlByStatusCount']['where'] .= ' AND ' .$VarWhere;
    }
    ///
    $ArrSqlArray['SqlByWriteoffCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlByWriteoffCount']['from'] = $this->VarTableSvtType .' AS d, '. $this->VarTableSvt .' AS a';
    $ArrSqlArray['SqlByWriteoffCount']['where'] = 'a.svttype_id = d.svttype_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlByWriteoffCount']['where'] .= ' AND ' .$VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

   function Add($values){
     if(true === is_array($values) && 1 <= count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableSvt .'
                (svt_id, svt_name, svt_buxname, svt_invnum, svt_serialnum, svt_garant_date, structureorganization_id, users_id, svttype_id, organiz_id, svt_note, svt_date)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableSvtTypes, MDB2_PREPARE_MANIP);
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['svt_name'],
        $values['svt_buxname'],
        $values['svt_invnum'],
        $values['svt_serialnum'],
        $values['svt_garant_date'],
        $values['structureorganization_id'],
        $values['users_id'],
        $values['svttype_id'],
        $values['organiz_id'],
        $values['svt_note'],
        $values['svt_date']
      );
      //SiteLibs::GetVarDump($data);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableSvt, 'svt_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) && 1 <= count($values)){
      $VarSql = "UPDATE ". $this->VarTableSvt ." SET
                  svt_name = ?,
                  svt_buxname = ?,
                  svt_invnum = ?,
                  svt_serialnum = ?,
                  svt_garant_date = ?,
                  structureorganization_id = ?,
                  users_id = ?,
                  svttype_id = ?,
                  organiz_id = ?,
                  svt_note = ?,
                  svt_date = ?
                  WHERE svt_id = '". $values['svt_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['svt_name'],
        $values['svt_buxname'],
        $values['svt_invnum'],
        $values['svt_serialnum'],
        $values['svt_garant_date'],
        $values['structureorganization_id'],
        $values['users_id'],
        $values['svttype_id'],
        $values['organiz_id'],
        $values['svt_note'],
        $values['svt_date']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      if(PEAR::isError($sth)) {
        return array('error' => $sth->getMessage());
      }
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['svt_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }
  
  function Move($values){
    if(true === is_array($values) && 1 <= count($values)){
      $VarSql = "UPDATE ". $this->VarTableSvt ." SET
                  structureorganization_id = ?,
                  users_id = ?
                  WHERE svt_id = '". $values['svt_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['structureorganization_id'],
        $values['users_id']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      if(PEAR::isError($sth)) {
        return array('error' => $sth->getMessage());
      }
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['svt_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'svt_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableSvt ." 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['svt_name'] = SiteLibs::ConvertTextToSql($values['svt_name']);
      $values['svt_buxname'] = SiteLibs::ConvertTextToSql($values['svt_buxname']);
      $values['svt_invnum'] = SiteLibs::ConvertTextToSql($values['svt_invnum']);
      $values['svt_serialnum'] = SiteLibs::ConvertTextToSql($values['svt_serialnum']);
      $values['svt_garant_date'] = SiteLibs::ConvertSqlDateInsert($values['svt_garant_date']);
      $values['structureorganization_id'] = '';
      if(true === isset($values['FrmAgreeStructureOrganizationCheckboxes']) && '' != current($values['FrmAgreeStructureOrganizationCheckboxes'])){
        $values['structureorganization_id'] = current($values['FrmAgreeStructureOrganizationCheckboxes']);
      }
      $values['users_id'] = '';
      if(true === isset($values['FrmAgreeUsersCheckboxes']) && '' != current($values['FrmAgreeUsersCheckboxes'])){
        $values['users_id'] = current($values['FrmAgreeUsersCheckboxes']);
      }
      $values['svttype_id'] = '';
      if(true === isset($values['FrmAgreeSvtTypeCheckboxes']) && '' != current($values['FrmAgreeSvtTypeCheckboxes'])){
        $values['svttype_id'] = current($values['FrmAgreeSvtTypeCheckboxes']);
      }
      $values['organiz_id'] = '';
      if(true === isset($values['FrmAgreeOrganizCheckboxes']) && '' != current($values['FrmAgreeOrganizCheckboxes'])){
        $values['organiz_id'] = current($values['FrmAgreeOrganizCheckboxes']);
      }
      $values['svt_note'] = SiteLibs::ConvertTextToSql($values['svt_note']);
      $values['svt_date'] = SiteLibs::ConvertSqlDateInsert($values['svt_date']);
    }
    return $values;
  }
}
?>