<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */

# マーチャントの設定ファイルを読み込む
include_once BASE_DIR . "/webapp/config/merchant.inc.php";
define('CONFIG_PATH', $CONFIG_PATH);
define('MERCHANT_ID', $MERCHANT_ID);
define('PASS_PHRASE', $PASS_PHRASE);
define('ENCODE',      $ENCODE);
define('TXN_TYPE',    $TXN_TYPE);
define('PAYTO_URL',   $PAYTO_URL);


/**
 *売上
 *
 * @package     Reservsales
 * @author      netnee Shiozawa
 * @access      public
 */
class Onedepo_Logic_Reservsales {

    var $t;
    var $data;
    var $dbaccess;
  
    var $merchant_id;
    var $pass_phrase;
    var $encode;
    var $paytourl;
    var $txn_type;
    
    var $deposit;
    
    var $orderno;
    var $sumprice;
    var $upduser;
    var $deposit_updcnt;
    
     function Onedepo_Logic_Reservsales() {
        # トランザクションオブジェクトを作成
        $this->t = new Transaction;

        # 設定ファイル jpgwclient.conf によりオブジェクトを設定
        $this->t->setServer(CONFIG_PATH);

        $this->merchant_id = MERCHANT_ID;
        $this->pass_phrase = PASS_PHRASE;
        $this->encode      = ENCODE;
        $this->txn_type    = TXN_TYPE;
        $this->paytourl    = PAYTO_URL;
    }    

    function ReservSales($upduser){

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Reservsales#ReservSales");
        
    //t_depositの変更
        $today = date("Y-m-d");
        $state = 2;

        $func_name = "Onedepo_Logic_Reserv#ReservSales";
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $db->autoCommit(false);
        $db->query('BEGIN;');
        

        $params = array($this->orderno, $today, $state, $upduser, $_SERVER['REMOTE_ADDR'], $this->deposit_updcnt);
        $sql  = "select f_deposit_state_update(?, ?, ?, ?, ?, ?);";
        $res = $db->getOne($sql, $params);

        if (DB::isError($res) || $res < 0) {
            $log->error("rollback". $res->userinfo, $func_name);
            $db->query('ROLLBACK;');
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }

        
   //ベリトランス側の更新
      
      // 予約はタイトルごとに決済を行う
         $txn_type = $request = "";
      // 取引タイプ
         $txn_type = "postauth";
      // リクエスト
         $request = array(
                          "order-id"      =>  $this->orderno,
                          "amount"        =>  $this->sumprice
                          );
                     
       $result = $this->t->doTransaction($txn_type, $request);
       
            if (substr_count($result[MSTATUS], MSTATUS_SC) == 0) {
                $log->error(print_r($result, true), $func_name);

                $db->query('ROLLBACK;');
                $db->rollback();
                $this->dbaccess->doClose($db);
                return false;
            }
      
    //db処理
        $log->debug("commit", $func_name);
        $db->query('COMMIT;');
        $db->commit();
        $this->dbaccess->doClose($db);
        return true;
            
    }
    
}
?>