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


/**
 * ダウンロード商品
 *
 * @package     Onedepo.Db.Dlitem
 * @access      public
 */
class Onedepo_Db_Dlitem
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess;

    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 delflg=FALSE ";

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

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

        return $data;
    }

    function getDownloadOrder($orderno, $orderrowno) 
    {
        $log =& LogFactory::getLog();

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

        $sql  = "SELECT * FROM t_dl_order ";
        $sql .= " WHERE orderno=? AND orderrowno=? AND delflg=FALSE ";

        $params = array($orderno, $orderrowno);

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

        return $data;
    }

    /**
     * ダウンロード販売の在庫データを取得
     */
    function getDownloadStock($itemcd, $structcd)
    {
        $log =& LogFactory::getLog();

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

        $sql  = "SELECT * FROM v_stock_stocktitle_list WHERE itemcd = ? AND structcd = ? AND sellercd = 9999";
        $params = array($itemcd, $structcd);

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

        return $data;
    }

    function getDownloadOK($orderno, $orderrowno, $membercd)
    {
        $log =& LogFactory::getLog();

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

        $params = array($orderno, $orderrowno, $membercd);

        $sql  = "SELECT o.orderno, o.orderrowno, o.itemcd, o.structcd, ";
        $sql .= "t.orderdate, i.itemnm, st.variationnm, ";
        $sql .= "d_o.maxdlcount, d_o.dlperiodend ";

        $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_dl_order d_o";
        $sql .= " ON  o.orderno = d_o.orderno ";
        $sql .= " AND o.orderrowno = d_o.orderrowno ";
        $sql .= " AND d_o.dlperiodend >= CURRENT_DATE "; // ダウンロード期間内
        $sql .= " AND d_o.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 .= " INNER JOIN t_dl_item d_i ";
        $sql .= " ON  o.itemcd = d_i.itemcd ";
        $sql .= " AND o.structcd = d_i.structcd ";
        $sql .= " AND d_i.fileexistence = TRUE "; // ダウンロードファイル登録済み
        $sql .= " AND d_i.delflg = FALSE ";

        /*
        $sql .= " INNER JOIN t_forward_detail f";
        $sql .= " ON  o.orderno = f.orderno ";
        $sql .= " AND o.orderrowno = f.orderrowno ";
        $sql .= " AND f.forwardstate = 4 ";
        $sql .= " AND f.delflg = FALSE ";

        $sql .= " INNER JOIN t_deposit d";
        $sql .= " ON  o.orderno = d.orderno ";
        $sql .= " AND o.orderrowno = d.orderrowno ";
        $sql .= " AND d.depositdiv = 1 AND d.depositstate = 2 ";
        $sql .= " AND d.delflg = FALSE ";
        */

        $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.sellercd = 9999"; // 出品者コード ダウンロード用
        $sql .= " AND ex.delflg  = FALSE ";

        $sql .= " INNER JOIN t_member_basic_attr mba ";
        $sql .= " ON  t.ordermember = mba.membercd AND d_o.membercd=mba.membercd ";
        $sql .= " AND mba.delflg = FALSE ";

        $sql .= " WHERE o.orderno=? AND o.orderrowno=? AND t.ordermember=?";
        $sql .= " AND o.typediv=1 ";
        $sql .= " AND o.delflg=FALSE ";
        $sql .= " ORDER BY o.orderno";
        $sql = sprintf($sql, $params);
        $data = $db->getRow($sql, $params);

        if(DB::isError($data) || !$data)
        {
            $log->error($data->userinfo, "Onedepo_Db_Dlitem#getDownloadOK");
            $this->dbaccess->doClose($db);
            return false;
        }
        $this->dbaccess->doClose($db);
        return $data;
    }

    function getDownloadFile($itemcd, $structcd)
    {
        $download_dir = IMAGE_DIR ."/7/". $itemcd ."_". $structcd;
        foreach (glob($download_dir ."*", GLOB_NOSORT) as $download_file);
        return $download_file;
    }

    function doDownload($orderno, $orderrowno, $membercd)
    {
        $log =& LogFactory::getLog();

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

        // ダウンロード回数を減らす １以上の場合のみ
        $sql  = "UPDATE t_dl_order SET maxdlcount=maxdlcount-1 ";
        $sql .= "WHERE orderno=? AND orderrowno=? AND membercd=? AND maxdlcount>0";
        $params = array($orderno, $orderrowno, $membercd);
        $res = $db->query($sql, $params);
        if (DB::isError($res)) {
            $log->error("dl_order rollback ".$res->userinfo, "doDownload");
            $db->query('ROLLBACK;');
            $this->dbaccess->doClose($db);
            return false;
        }
        //--

        // ダウンロード履歴
        $sql  = "INSERT INTO t_dl_history ";
        $sql .= "(orderno, orderrowno, dldate, dluserip, dluseragent) ";
        $sql .= "VALUES (?, ?, ?, ?, ?)";
        $params = array($orderno, $orderrowno, date("Y-m-d"), $_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"]);
        $res = $db->query($sql, $params);
        if (DB::isError($res)) {
            $log->error("dl_history rollback ".$res->userinfo, "doDownload");
            $db->query('ROLLBACK;');
            $this->dbaccess->doClose($db);
            return false;
        }
        //--

        $db->query('COMMIT;');
        $log->debug("commit", "doDownload");

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

    function getDownloadURL($fid, $orderno, $orderrowno)
    {
        $dirname = dirname($_SERVER['SCRIPT_NAME']);
        $dirname = ($dirname=="/") ? "" : $dirname;
        $location  = SSL_URL_ROOT .$dirname ."/?action=download&fid=". $fid;
        $location .= "&o=". $orderno ."&r=". $orderrowno;
        return $location;
    }

    function getSellerName($itemcd)
    {
        $log =& LogFactory::getLog();

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

        $sql  = "select b.sellercd,a.lastnm,a.firstnm from t_seller_item b, t_member_basic_attr a";
        $sql .= " where b.itemcd=? and b.sellercd=a.membercd";

        $params = array($itemcd);

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

        return $data;
    }
}
?>
