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


/**
 * 受注テーブル関連
 *
 * @package     Onedepo.Db.Member.Customer
 * @author      netnee H.tominaga
 * @access      public
 */
class Onedepo_Db_Preorder
{
    var $dbaccess;

//仮受注見出しテーブルに関する操作
    function selectAll($memberCd)
    {
        $func_name = "Onedepo_Db_Order#select_itemcd";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($memberCd, date('Y-m-d', time() - 86400));
        $sql  = "SELECT *";
        $sql .= " FROM t_pre_order_title t_t, t_pre_order_detail t_d";
        $sql .= " WHERE t_t.preordermember = ?";
        $sql .=	" AND t_t.preorderdate >= ?";
        $sql .= " AND t_t.preorderno = t_d.preorderno";
        $sql .=	" AND t_t.delflg = FALSE ";
        $sql .= " ORDER BY t_t.preorderno";
        $sql = sprintf($sql, $params);
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        return $data;

    }

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

        $params = array($memberCd, date('Y-m-d', time() - 86400));
        $sql  = "SELECT *";
        $sql .= " FROM t_pre_order_title";
        $sql .= " WHERE preordermember = ?";
        $sql .=	" AND preorderdate >= ?";
        $sql .=	" AND delflg = FALSE ";
        $data = $db->getOne($sql, $params);
        if (DB::isError($data) || count($data) <= 0) {
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        return true;
        
    }
    
    function updateTakkyubin($orderno, $membercd, $takkyubin, $user)
    {
        $func_name = "Onedepo_Db_Order#update";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($takkyubin,
                        $user,
                        $_SERVER['REMOTE_ADDR'],
                        $membercd,
                        $orderno);
        $sql  = "update t_pre_order_title 
                     set takkyubin=?,
                         updusrnm=?,
                         updpcnm=? 
                   where preordermember=? 
                         AND preorderno=?";
        $data = $db->query($sql, $params);
        if (DB::isError($data)) {
            $log->debug("update takkyubin ". $data->userinfo, $func_name);
            $this->dbaccess->doClose($db);
            trigger_error($data->userinfo, E_USER_ERROR);
            return false;
        }
        $this->dbaccess->doClose($db);
        return true;
    }

    function insert($membercd, $takkyubin, $itemcd, $structcd, $stockcd, $count, $user)
    {
        $func_name = "Onedepo_Db_Order#insert";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($membercd, $takkyubin, $itemcd, $structcd, $stockcd, $count, $user, $_SERVER['REMOTE_ADDR']);
        $sql  = "SELECT * FROM f_pre_order_detail_update(1, ?, ?, ?, ?, ?, ?, ?, ?)";
        $data = $db->getRow($sql, $params);
        if (DB::isError($data) || $data < 0) {
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        return $data;
    }
    
    function update($membercd, $takkyubin, $itemcd, $structcd, $stockcd, $count, $user)
    {
        $func_name = "Onedepo_Db_Order#update";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($membercd, $takkyubin, $itemcd, $structcd, $stockcd, $count, $user, $_SERVER['REMOTE_ADDR']);
        $sql  = "SELECT * FROM f_pre_order_detail_update(2, ?, ?, ?, ?, ?, ?, ?, ?)";
        $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;
    }
    
    function delete($membercd, $itemcd, $structcd, $stockcd)
    {
        $func_name = "Onedepo_Db_Order#delete";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($membercd, $itemcd, $structcd, $stockcd);
        $sql  = "SELECT * FROM f_pre_order_detail_update(3, ?, NULL, ?, ?, ?, NULL, NULL, NULL)";
        $data = $db->getRow($sql, $params);
        if (DB::isError($data) || $data < 0) {
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        return $data;
    }
    
/*
    function insertMember($memberCd)
    {
        $func_name = "Onedepo_Db_Order#insertMember";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($memberCd, date('Y-m-d', time()));
        $sql  = " INSERT INTO t_pre_order_title (preordermember, preorderdate) ";
        $sql .= " VALUES";
        $sql .= " (?, ?) ";
        $log->debug($sql, $func_name);
        $data = $db->query($sql, $params);
        if (DB::isError($data) || $data < 0) {
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        return $data;
    }

    function update($memberCd, $item_array)
    {
        $func_name = "Onedepo_Db_Order#update";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        //memberCdから更新すべき仮受注番号を取得
        $params = array($memberCd);
        $sql  = "SELECT preorderno";
        $sql .= " FROM t_pre_order_title";
        $sql .= " WHERE preordermember = ?";
        $sql .=	" AND delflg = FALSE ";
        $sql = sprintf($sql, $params);
        $log->debug($sql, $func_name);
        $updateId = $db->getOne($sql, $params);
        if (DB::isError($updateId) || $updateId < 0) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }

        //取得した仮受注番号をもとに、仮受注テーブルの受注日を更新
        $params = array(date('Y-m-d', time()), $updateId);
        $sql  = "UPDATE t_pre_order_title";
        $sql .= " SET preorderdate = ?";
        $sql .= " WHERE preorderno = ?";
        $sql .= " AND delflg = FALSE ";
        $sql = sprintf($sql, $params);
        $log->debug($sql, $func_name);
        $res = $db->query($sql, $params);
        if (DB::isError($res) || $res < 0) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }

        //仮受注詳細テーブルに新しい情報を挿入
        $count = 1;
        if(!is_null($item_array)) {
            foreach($item_array as $item)
            {
                $params = array($updateId, 
                                 $count, 
                                 $item[0], 
                                 $item[1], 
                                 $item[2],
                                 $item[3]
                                 );

                $sql  = "INSERT INTO t_pre_order_detail(preorderno, preorderrowno, itemcd, structcd, stockcd, count) VALUES";
                $sql .= " (?, ?, ?, ?, ?, ?)";
                $sql = sprintf($sql, $params);
                $log->debug($sql, $func_name);
                $res = $db->query($sql, $params);
                $count +=1;
            }
            if (DB::isError($res) || $res < 0) {
                $log->debug("rollback", $func_name);
                $db->rollback();
                $this->dbaccess->doClose($db);
                return false;
            }
        }

        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        return true;
    }

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

        //memberCdから更新すべき仮受注番号を取得
        $params = array($memberCd);
        $sql  = "SELECT preorderno";
        $sql .= " FROM t_pre_order_title";
        $sql .= " WHERE preordermember = ?";
        $sql .=	" AND delflg = FALSE ";
        $sql = sprintf($sql, $params);
        $log->debug($sql, $func_name);
        $deleteId = $db->getOne($sql, $params);
        if (DB::isError($deleteId) || $deleteId < 0) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }

        //取得した仮受注番号を元に、仮受注詳細テーブルから該当データを全て削除
        $params = array($deleteId);
        $sql  = "DELETE FROM t_pre_order_detail";
        $sql .= " WHERE preorderno = ?";
        $sql .=	" AND delflg = FALSE ";
        $sql = sprintf($sql, $params);
        $log->debug($sql, "Onedepo_Db_Order#delete");
        $data = $db->getAll($sql, $params);
        if (DB::isError($data) || $data < 0) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        return $data;
    }
*/
}
?>
