<?php

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

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'p.*, purchase_price*purchase_quantity AS purchase_value, s.structureorganization_title, ps.purchasestatus_name, ps.purchasestatus_color';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTablePurchaseStatus .' AS ps, '. $this->VarTablePurchase .' AS p';
    $ArrSqlArray['SqlAll']['leftjoin'] = $this->VarTableStructureOrganization .' AS s ON p.structureorganization_id = s.structureorganization_id';
    $ArrSqlArray['SqlAll']['where'] = 'p.purchasestatus_id = ps.purchasestatus_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->VarTablePurchaseStatus .' AS ps, '. $this->VarTablePurchase .' AS p';
    $ArrSqlArray['SqlCount']['leftjoin'] = $this->VarTableStructureOrganization .' AS s ON p.structureorganization_id = s.structureorganization_id';
    $ArrSqlArray['SqlCount']['where'] = 'p.purchasestatus_id = ps.purchasestatus_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['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->VarTablePurchase .'
                (purchase_id, structureorganization_id, purchasestatus_id, purchase_name, purchase_price, purchase_quantity, purchase_datetill, purchase_date, purchase_note)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['structureorganization_id'],
        $values['purchasestatus_id'],
        $values['purchase_name'],
        $values['purchase_price'],
        $values['purchase_quantity'],
        $values['purchase_datetill'],
        $values['purchase_date'],
        $values['purchase_note']
      );
      //SiteLibs::GetVarDump($data);
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTablePurchaseTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTablePurchase, 'purchase_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->VarTablePurchase ." SET
                  structureorganization_id = ?,
                  purchasestatus_id = ?,
                  purchase_name = ?,
                  purchase_price = ?,
                  purchase_quantity = ?,
                  purchase_datetill = ?,
                  purchase_date = ?,
                  purchase_note = ?
                  WHERE purchase_id = '".  $values['purchase_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['structureorganization_id'],
        $values['purchasestatus_id'],
        $values['purchase_name'],
        $values['purchase_price'],
        $values['purchase_quantity'],
        $values['purchase_datetill'],
        $values['purchase_date'],
        $values['purchase_note']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['purchase_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'purchase_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTablePurchase ." 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_id'] = NULL;
      if(true === isset($values['FrmAgreePurchaseStatusCheckboxes']) && '' != $values['FrmAgreePurchaseStatusCheckboxes']){
        $values['purchasestatus_id'] = current($values['FrmAgreePurchaseStatusCheckboxes']);
      }
      $values['structureorganization_id'] = '';
      if(true === isset($values['FrmAgreeStructureOrganizationCheckboxes']) && '' != current($values['FrmAgreeStructureOrganizationCheckboxes'])){
        $values['structureorganization_id'] = current($values['FrmAgreeStructureOrganizationCheckboxes']);
      }
      if(true === isset($values['purchase_name']) && '' != $values['purchase_name']){
        $values['purchase_name'] = SiteLibs::ConvertTextToSql($values['purchase_name']);
      }
      else{
        $values['purchase_name'] = NULL;
      }
      if(true === isset($values['purchase_price']) && true === is_numeric($values['purchase_price'])){
        $values['purchase_price'] = SiteLibs::ConvertSqlIntegerInsert($values['purchase_price']);
      }
      else{
        $values['purchase_price'] = NULL;
      }
      if(true === isset($values['purchase_quantity']) && true === is_numeric($values['purchase_quantity'])){
        $values['purchase_quantity'] = SiteLibs::ConvertSqlIntegerInsert($values['purchase_quantity']);
      }
      else{
        $values['purchase_quantity'] = NULL;
      }
      if(true === isset($values['purchase_datetill']) && '' != $values['purchase_datetill']){
        $values['purchase_datetill'] = SiteLibs::ConvertSqlDateInsert($values['purchase_datetill']);
      }
      else{
        $values['purchase_datetill'] = NULL;
      }
      $values['purchase_date'] = date('YmdHis');
    }
    return $values;
  }
}
?>