<?php

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

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTablePurchaseStatus .' AS a';
    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->VarTablePurchaseStatus .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlWriteoffAll']['select'] = 'a.*, count(b._id) AS svt_count';
    $ArrSqlArray['SqlWriteoffAll']['from'] = $this->VarTablePurchaseStatus .' AS a';
    $ArrSqlArray['SqlWriteoffAll']['leftjoin'] = $this->VarTableSvt .' AS b ON a.purchasestatus_id = b.purchasestatus_id AND YEAR(b.svt_date) < YEAR(NOW())-a.purchasestatus_writeoff+1';
    if('' != $VarWhere){
      $ArrSqlArray['SqlWriteoffAll']['where'] = $VarWhere;
    }
    $ArrSqlArray['SqlWriteoffAll']['group'] = 'a.purchasestatus_id';
    if('' != $VarOrder){
      $ArrSqlArray['SqlWriteoffAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlWriteoffAll']['limit'] = $VarLimit;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($values){
    if(true === is_array($values) && 1 <= count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTablePurchaseStatus .'
                (purchasestatus_id, purchasestatus_name, purchasestatus_color, purchasestatus_order)
                VALUES (?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['purchasestatus_name'],
        $values['purchasestatus_color'],
        $values['purchasestatus_order']
      );
      //SiteLibs::GetVarDump($data);
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTablePurchaseStatusTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTablePurchaseStatus, 'purchasestatus_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->VarTablePurchaseStatus ." SET
                  purchasestatus_name = ?,
                  purchasestatus_color = ?,
                  purchasestatus_order = ?
                  WHERE purchasestatus_id = '".  $values['purchasestatus_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['purchasestatus_name'],
        $values['purchasestatus_color'],
        $values['purchasestatus_order']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['purchasestatus_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'purchasestatus_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTablePurchaseStatus ." 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['purchasestatus_name'] = SiteLibs::ConvertTextToSql($values['purchasestatus_name']);
      $values['purchasestatus_color'] = SiteLibs::ConvertTextToSql($values['purchasestatus_color']);
      $values['purchasestatus_order'] = SiteLibs::ConvertSqlIntegerInsert($values['purchasestatus_order']);
    }
    return $values;
  }
}
?>