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


/**
 * 一覧表示関連
 *
 * @package     Onedepo.Db.Cart
 * @author      FUCO H.Tominaga
 * @access      public
 */
class Onedepo_Db_List
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess;

    /**
     * 全件取得
     *
     * @param string $sort ソートキー
     * @return object 取得レコード
     */

//--------------------------t_order_title--------------------------
    function select_OrderTitle($OrderMember, $limit)
    {

        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $params = array($OrderMember);

        $sql  = "SELECT *";
        $sql .= " FROM t_order_title ";
        $sql .= " WHERE OrderMember = ?";
        $sql .= $limit;
        $sql .= " AND delflg = FALSE ";
        $sql .= "ORDER BY orderdate DESC ";

        $sql = sprintf($sql, $params);
        $log->debug($sql, "Onedepo_Db_List#select_OrderTitle");
        $data = $db->getAll($sql, $params);

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


    function select_One_OrderTitle($OrderNo)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $params = array($OrderNo);

        $sql  = "SELECT *";
        $sql .= " FROM t_order_title ";
        $sql .= " WHERE OrderNo = ?";
        $sql .= " AND delflg = FALSE ";
        $sql = sprintf($sql, $params);
        $log->debug($sql, "Onedepo_Db_List#select_One_OrderTitle");
        $data = $db->getRow($sql, $params);

        $this->dbaccess->doClose($db);
        return $data;
    }
//--------------------------t_order_detail--------------------------
    function selectOrderDetail($OrderNo)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $params = array($OrderNo);

        $sql  = "SELECT o.orderno, o.orderrowno, o.itemcd, o.structcd, t.orderdate, i.itemnm, st.variationnm, ";
        $sql .= "f.forwardstate, mba.membercd, mba.lastnm, mba.firstnm, mba.nicknm, f.forwarddate ";

        $sql .= " FROM t_order_detail o ";
        $sql .= " INNER JOIN t_order_title t";
        $sql .= " ON  o.orderno = t.orderno ";
        $sql .= " AND t.delflg = FALSE ";

        $sql .= " INNER JOIN t_item_basic_attr i ";
        $sql .= " ON  o.itemcd = i.itemcd ";
        $sql .= " AND i.delflg = FALSE ";

        $sql .= " INNER JOIN t_item_struct st ";
        $sql .= " ON  o.itemcd = st.itemcd ";
        $sql .= " AND o.structcd = st.structcd ";
        $sql .= " AND st.delflg = FALSE ";

        $sql .= " LEFT JOIN t_forward_detail f";
        $sql .= " ON  o.orderno = f.orderno ";
        $sql .= " AND o.orderrowno = f.orderrowno ";
        $sql .= " AND f.delflg = FALSE ";

//追加 070706
        $sql .= " INNER JOIN t_ex_stock_detail ex ";
        $sql .= " ON  o.itemcd   = ex.itemcd ";
        $sql .= " AND o.structcd = ex.structcd ";
        $sql .= " AND o.stockcd  = ex.stockcd ";
        $sql .= " AND ex.delflg  = FALSE ";

        $sql .= " LEFT JOIN t_member_basic_attr mba ";
        $sql .= " ON  ex.sellercd = mba.membercd ";
        $sql .= " AND mba.delflg = FALSE ";
//冨永仁志

        $sql .= " WHERE o.OrderNo = ? ";
        $sql .= " AND o.typediv = 1 ";
        $sql .= " AND o.delflg = FALSE ";
        $sql .= " ORDER BY o.orderno";
        $sql = sprintf($sql, $params);
        $log->debug($sql, "Onedepo_Db_List#select_OrderDetail");
        $data = $db->getAll($sql, $params);

        if(DB::isError($data) || !$data)
        {
            //echo $data->toString();
            $this->dbaccess->doClose($db);
            return false;
        }
        else
        {
            $this->dbaccess->doClose($db);
            return $data;
        }
    }


    function select_One_OrderDetail($OrderNo, $OrderRowNo)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($OrderNo, $OrderRowNo);

        $sql  = "SELECT *";
        $sql .= " FROM t_order_detail ";
        $sql .= " WHERE orderNo = ?";
        $sql .=    " AND orderRowNo = ?";
        $sql .=    " AND delflg = FALSE ";
        $sql = sprintf($sql, $params);
        $log->debug($sql, "Onedepo_Db_List#select_One_OrderDetail");
        $data = $db->getRow($sql, $params);
        $this->dbaccess->doClose($db);
        return $data;
    }

    function getSellerInfo($OrderNo)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($OrderNo);

        $sql  = "SELECT ex.sellercd, mba.lastnm, mba.firstnm, mba.nicknm ";
        $sql .= " FROM t_order_detail ord ";

        $sql .= " INNER JOIN t_ex_stock_detail ex ";
        $sql .= " ON  ord.itemcd   = ex.itemcd ";
        $sql .= " AND ord.structcd = ex.structcd ";
        $sql .= " AND ord.stockcd  = ex.stockcd ";
        $sql .= " AND ex.delflg  = FALSE ";

        $sql .= " LEFT JOIN t_member_basic_attr mba ";
        $sql .= " ON  ex.sellercd = mba.membercd ";
        $sql .= " AND mba.delflg = FALSE ";

        $sql .= " WHERE ord.orderNo = ?";
        $sql .= " AND ord.delflg = FALSE ";
        $sql = sprintf($sql, $params);
        $log->debug($sql, "Onedepo_Db_List#select_One_OrderDetail");
        $data = $db->getRow($sql, $params);
        $this->dbaccess->doClose($db);
        return $data;
    }

//--------------------------t_item_basic_attr--------------------------
    function select_ItemBasicAttr($ItemCd)
    {
        $sort="ItemCd";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $params = array($ItemCd);

        $sql  = "SELECT *";
        $sql .= " FROM t_item_basic_attr ";
        $sql .= " WHERE ItemCd = ?";
        $sql .= " AND delflg = FALSE ";
        $sql .= " ORDER BY %s";
        $sql = sprintf($sql, $sort);
        $log->debug($sql, "Onedepo_Db_List#select_Itemname");
        $data = $db->getRow($sql, $params);

        if(DB::isError($data) || !$data)
        {
            //echo $data->toString();
            $this->dbaccess->doClose($db);
            return false;
        }
        else
        {
            $this->dbaccess->doClose($db);
            return $data;
        }
    }

//--------------------------t_ex_stock_detail--------------------------
    function select_ExStockDetail($ItemCd, $StructCd, $StockCd)
    {
        $sort="ItemCd";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $params = array($ItemCd,
                        $StructCd,
                        $StockCd);

        $sql  = "SELECT *";
        $sql .= " FROM t_ex_stock_detail";
        $sql .= " WHERE ";
        $sql .= " ItemCd = ? AND ";
        $sql .= " StructCd = ? AND ";
        $sql .= " StockCd = ? AND ";
        $sql .=    " delflg = FALSE ";
        $sql .= " ORDER BY %s";
        $sql = sprintf($sql, $sort);
        $log->debug($sql, "Onedepo_Db_Cart#select_Itemprice");
        $data = $db->getRow($sql, $params);
        $this->dbaccess->doClose($db);
        return $data;
    }

//--------------------------t_member_basic_attr--------------------------
    function select_MemberBasicAttr($MemberCd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($MemberCd);
        $sql = "select * from t_member_basic_attr where MemberCd=?;";

        $data = $db->getRow($sql,$params);
        if(DB::isError($data) || !$data)
        {
            $this->dbaccess->doClose($db);
            return false;
        }
        else
        {
            $this->dbaccess->doClose($db);
            return $data;
        }
    }
}
?>
