<?php

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

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    $ArrSqlArray['SqlAll']['select'] = 'a.*, b.organiz_name, c.repairstatus_name, c.repairstatus_color, d.users_fio, f.svt_id, f.svt_invnum, f.svt_serialnum, f.svt_name, f.svt_buxname';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableOrganiz .' AS b, '. $this->VarTableRepairStatus .' AS c, '. $this->VarTableRepair .' AS a';
    $ArrSqlArray['SqlAll']['leftjoin'] =  $this->VarTableUsers .' AS d ON a.users_id = d.users_id LEFT JOIN '. $this->VarTableDemands .' AS e ON a.demands_id = e.demands_id LEFT JOIN '. $this->VarTableSvt .' AS f ON e.svt_id = f.svt_id';
    $ArrSqlArray['SqlAll']['where'] = 'a.organiz_id = b.organiz_id AND a.repairstatus_id = c.repairstatus_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] .= ' AND '. $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
//    $ArrSqlArray['SqlRow']['select'] = 'a.*, b.organiz_name, c.repairstatus_name, d.users_fio';
//    $ArrSqlArray['SqlRow']['from'] = $this->VarTableOrganiz .' AS b, '. $this->VarTableRepairStatus .' AS c, '. $this->VarTableRepair .' AS a';
//    $ArrSqlArray['SqlRow']['leftjoin'] =  $this->VarTableUsers .' AS d ON a.users_id = d.users_id';
//    $ArrSqlArray['SqlRow']['where'] = 'a.organiz_id = b.organiz_id AND a.repairstatus_id = c.repairstatus_id';
//    if('' != $VarWhere){
//      $ArrSqlArray['SqlRow']['where'] .= ' AND '. $VarWhere;
//    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableOrganiz .' AS b, '. $this->VarTableRepairStatus .' AS c, '. $this->VarTableRepair .' AS a';
    $ArrSqlArray['SqlCount']['leftjoin'] =  $this->VarTableUsers .' AS d ON a.users_id = d.users_id LEFT JOIN '. $this->VarTableDemands .' AS e ON a.demands_id = e.demands_id LEFT JOIN '. $this->VarTableSvt .' AS f ON e.svt_id = f.svt_id';
    $ArrSqlArray['SqlCount']['where'] = 'a.organiz_id = b.organiz_id AND a.repairstatus_id = c.repairstatus_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrSqlArray['SqlByYearCount']['select'] = 'count(*) AS cnt, year(repair_date_e) AS year_repair_date_e';
    $ArrSqlArray['SqlByYearCount']['from'] = $this->VarTableRepair .' AS a';
    $ArrSqlArray['SqlByYearCount']['where'] = 'a.repair_date_e IS NOT NULL';
    if('' != $VarWhere){
      $ArrSqlArray['SqlByYearCount']['where'] .= ' AND '. $VarWhere;
    }
    $ArrSqlArray['SqlByYearCount']['group'] = 'year(a.repair_date_b)';
    if('' != $VarOrder){
      $ArrSqlArray['SqlByYearCount']['order'] = $VarOrder;
    }
    ///
    $ArrSqlArray['SqlForFirstPageCount']['select'] = 'count(a.repair_id) AS cnt, b.repairstatus_id, b.repairstatus_name, b.repairstatus_color';
    $ArrSqlArray['SqlForFirstPageCount']['from'] = $this->VarTableRepairStatus .' AS b, '. $this->VarTableRepair .' AS a';
    $ArrSqlArray['SqlForFirstPageCount']['where'] = 'a.repairstatus_id = b.repairstatus_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlForFirstPageCount']['where'] .= ' AND '. $VarWhere;
    }
    $ArrSqlArray['SqlForFirstPageCount']['group'] = 'a.repairstatus_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->VarTableRepair .'
                (repair_id, demands_id, organiz_id, repairstatus_id, repair_summa, repair_content, repair_numdoc, repair_numdocdate, repair_date_b, repair_date_e, users_id)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['demands_id'],
        $values['organiz_id'],
        $values['repairstatus_id'],
        $values['repair_summa'],
        $values['repair_content'],
        $values['repair_numdoc'],
        $values['repair_numdocdate'],
        $values['repair_date_b'],
        $values['repair_date_e'],
        $values['users_id']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableRepairTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableRepair, 'repair_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->VarTableRepair ." SET 
                  demands_id = ?,
                  organiz_id = ?,
                  repairstatus_id = ?,
                  repair_summa = ?,
                  repair_content = ?,
                  repair_numdoc = ?,
                  repair_numdocdate = ?,
                  repair_date_b = ?,
                  repair_date_e = ?,
                  users_id = ?
                WHERE repair_id ='".  $values['repair_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
                  $values['demands_id'],
                  $values['organiz_id'],
                  $values['repairstatus_id'],
                  $values['repair_summa'],
                  $values['repair_content'],
                  $values['repair_numdoc'], 
                  $values['repair_numdocdate'],
                  $values['repair_date_b'],
                  $values['repair_date_e'], 
                  $values['users_id']
                );
      $sth =& $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['demands_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }
//
  function Del($VarId = '', $VarFiledName = 'repair_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableRepair ." 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['organiz_id'] = NULL;
      if(true === isset($values['FrmAgreeOrganizCheckboxes']) && '' != current($values['FrmAgreeOrganizCheckboxes'])){
        $values['organiz_id'] = current($values['FrmAgreeOrganizCheckboxes']);
      }
      $values['demands_id'] = NULL;
      if(true === isset($values['FrmAgreeDemandsCheckboxes']) && '' != current($values['FrmAgreeDemandsCheckboxes'])){
        $values['demands_id'] = current($values['FrmAgreeDemandsCheckboxes']);
      }
      $values['repairstatus_id'] = NULL;
      if(true === isset($values['FrmAgreeRepairStatusCheckboxes']) && '' != current($values['FrmAgreeRepairStatusCheckboxes'])){
        $values['repairstatus_id'] = current($values['FrmAgreeRepairStatusCheckboxes']);
      }
      if(true === isset($values['repair_date_b']) && '' != $values['repair_date_b']){
        $values['repair_date_b'] = SiteLibs::ConvertSqlDateInsert($values['repair_date_b']);
      }
      else{
        $values['repair_date_b'] = NULL;
      }      
      if(true === isset($values['repair_date_e']) && '' != $values['repair_date_e']){
        $values['repair_date_e'] = SiteLibs::ConvertSqlDateInsert($values['repair_date_e']);
      }
      else{
        $values['repair_date_e'] = NULL;
      }
      $values['users_id'] = NULL;
      if(true === isset($values['FrmAgreeUsersCheckboxes']) && '' != current($values['FrmAgreeUsersCheckboxes'])){
        $values['users_id'] = current($values['FrmAgreeUsersCheckboxes']);
      }
      if(true === isset($values['repair_numdocdate']) && '' != $values['repair_numdocdate']){
        $values['repair_numdocdate'] = SiteLibs::ConvertSqlDateInsert($values['repair_numdocdate']);
      }
      else{
        $values['repair_numdocdate'] = NULL;
      }
    }
    return $values;
  }
}
?>