<?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_Cart
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess;

    /**
     * 全件取得
     *
     * @param string $sort ソートキー
     * @return object 取得レコード
     */
    function selectAll($sort="ItemCd")
    {
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($sort);

        $sql  = "SELECT ItemCd, ItemNm";
        $sql .= "  FROM t_item_basic_attr ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= " ORDER BY %s";
        $sql = sprintf($sql, $sort);

        $data = $db->getAll($sql);
        $this->dbaccess->doClose($db);

        return $data;
    }


    function selectOne($ItemCd, $StructCd, $StockCd)
    {
        $func_name = "Onedepo_Db_Cart#selectOne";
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $params = array($ItemCd, $StructCd, $StockCd);

    //v_stock_listから販売価格配列を取得
        $sql  = "SELECT sellercd, sellingprice, itemnm, variationnm, COALESCE(souryouprc, 0) as souryouprc, ";
        $sql .= "(sellingprice + COALESCE(souryouprc, 0)) as totalprice, takkyubinprc, reserveflg, ";
        $sql .= " sellernm, sellersouryou, sellersouryoumax, itemexp ";
        $sql .= " FROM v_stock_list v";
        $sql .= "   INNER JOIN t_item_struct USING(itemcd, structcd) ";
        $sql .= " WHERE itemcd = ? AND structcd = ? AND stockcd = ?";
        $sql .= " AND ((sellercd IS NOT NULL AND sellernm IS NOT NULL AND memberstate=1 AND exhibitcount>0)";
        $sql .= " OR (sellercd=9999) OR (sellercd IS NULL))";
        $sql .= " AND ((v.salestartdate>CURRENT_DATE AND (v.orderclosedate IS NULL OR v.orderclosedate>=CURRENT_DATE) AND reserveflg=1)";
        $sql .= " OR (((v.salestartdate IS NULL OR v.salestartdate<=CURRENT_DATE) AND
                       (v.orderclosedate IS NULL OR v.orderclosedate<=CURRENT_DATE))";
        $sql .= " AND reserveflg=0))";
        $sql .= " GROUP BY itemcd, sellercd, sellingprice, itemnm, variationnm,";
        $sql .= " souryouprc, takkyubinprc, reserveflg, sellernm, sellersouryou, sellersouryoumax, itemexp";
        $sql .= " ORDER BY itemcd ";

        $data = $db->getRow($sql, $params);
        if (DB::isError($data) || $data < 0) {
            $log->debug($data->userinfo, $func_name);
            $this->dbaccess->doClose($db);
            return false;
        }

        // ショップ販売分の宅急便料金設定
        if (empty($data["sellercd"])) {
            $data["takkyubinprc"] = SITE_TAKKYUBIN;
        }

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

    function getDownloadItem($itemcd, $structcd) 
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $sql  = "SELECT * FROM t_dl_item ";
        $sql .= " WHERE itemcd=? AND structcd=? AND d.fileflg=TRUE AND delflg=FALSE ";

        $params = array($itemcd, $structcd);

        $data = $db->getRow($sql, $params);
        if (DB::isError($data) || $data < 0) {
            $log->debug($data->userinfo, $func_name);
            $this->dbaccess->doClose($db);
            return false;
        }

        return $data;
    }

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

        $sql  = "SELECT count(*) as c FROM t_order_title WHERE ordermember = ? AND preorderflg = TRUE AND orderprocessflg = TRUE AND delflg=FALSE";
        $params = array($membercd);

        $c = $db->getOne($sql, $params);
        if (DB::isError($c)) {
            $log->debug($c->userinfo, $func_name);
            $this->dbaccess->doClose($db);
            return false;
        }

        return $c;
    }

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

        $sql  = "SELECT * FROM f_upd_preorder(?); ";
        $params = array($membercd);

        $data = $db->getOne($sql, $params);
        if (DB::isError($data) || $data < 0) {
            $log->debug($data->userinfo, $func_name);
            $this->dbaccess->doClose($db);
            return false;
        }

        return $data;
    }
}
?>
