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


/**
 * 銀行関連
 *
 * @package     Onedepo.Db.Bank
 * @author      netnee
 * @access      public
 */
class Onedepo_Db_Bank
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess;

    function selectOne()
    {
        $func_name = "Onedepo_Db_Bank#selectOne";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $sql  = "SELECT * FROM t_bank WHERE lastusedate IS NULL AND delflg=FALSE LIMIT 1";
        $data = $db->getRow($sql);
        if (DB::isError($data) || $data < 0) {
            $log->debug($data->userinfo, $func_name);
            $this->dbaccess->doClose($db);
            return false;
        }
        if (count($data) <= 0) {
            $sql  = "SELECT * FROM t_bank WHERE delflg=FALSE ORDER BY lastusedate LIMIT 1";
            $data = $db->getRow($sql);
            if (DB::isError($data) || $data < 0) {
                $log->debug($data->userinfo, $func_name);
                $this->dbaccess->doClose($db);
                return false;
            }
        }

        $this->dbaccess->doClose($db);
        return $data;
    }

    function update($shiten, $kouza, $user, $cnt)
    {
        $func_name = "Onedepo_Db_Bank_#update";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($shiten, $kouza, $user, $_SERVER['REMOTE_ADDR'], $cnt);
        $sql  = "SELECT * FROM f_bank_update(?, ?, ?, ?, ?)";
        $data = $db->getRow($sql, $params);
        if (DB::isError($data) || $data < 0) {
            $log->debug("rollback ". $data->userinfo, $func_name);
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        return $data;
    }
}
?>
