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


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

    function insert($orderno, $membercd, $consumermembercd, $rank, $comment) {
        $res = $this -> select($orderno);
        if(is_null($res))
        {
            $ret = true;
            $log =& LogFactory::getLog();

            $params = array($orderno,
                             ($membercd) ? $membercd : NULL,
                             ($consumermembercd!="") ? $consumermembercd : NULL,
                             $rank,
                             $comment,
                             $_SERVER['REMOTE_ADDR']);
            $sql = "insert into t_seller_val(" .
                   "orderno, membercd, consumermembercd, valuation, comment, " .
                   "updusrnm, updpcnm)" .
                    "values (" .
                   "?, ?, ?, ?, ?, '', ?);";

            $db  = $this->dbaccess->doConnect();
            $res = $db->query($sql, $params);

            if (DB::isError($res)) {
                var_dump($res);
                $log->debug("rollback", "Onedepo_Db_Valuation#insert");
                //echo $res->toString();
                $db->rollback();
                return false;
            }
            $log->debug("commit", "Onedepo_Db_Valuation#insert");
            $this->dbaccess->doClose($db);
            return $ret;
        }
        else
        {
            $ret = true;
            $log =& LogFactory::getLog();

            $time = time();
            $params = array($rank,
                             $comment,
                             $_SERVER['REMOTE_ADDR'],
                             $orderno);
            $sql = "update t_seller_val set " .
                   "Valuation=?, " .
                   "Comment=?, " .
                   "UpdUsrNm='', " .
                   "UpdPcNm=? " .
                   "where OrderNo=?";
            $db  = $this->dbaccess->doConnect();
            $res = $db->query($sql, $params);

            if (DB::isError($res)) {
                $log->trace("rollback", "Onedepo_Db_Valuation#insert");
                //echo $res->toString();
                $db->rollback();
                $ret = false;
            }
            $log->trace("commit", "Onedepo_Db_Valuation#insert");
            $this->dbaccess->doClose($db);
            return $ret;
            
        }
    }

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

        $params = array($OrderNo);
        $sql  = "SELECT *";
        $sql .= " FROM t_seller_val ";
        $sql .= " WHERE OrderNo = ?";
        $sql .= " AND delflg = FALSE ";
        $res = $db->getRow($sql, $params);

        if (DB::isError($res)) {
            $log->trace("rollback", "Onedepo_Db_Valuation#select");
            $db->rollback();
            $res = false;
        }
        $log->trace("commit", "Onedepo_Db_Valuation#select");
        $this->dbaccess->doClose($db);
        return $res;
    }
    
    function selectByMembercd($membercd){
        $res = true;
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($membercd);
        $sql  = "SELECT val.valuation, val.comment, val.orderno, val.adddate, val.consumermembercd, bas.lastnm, bas.firstnm ";
        $sql .= " FROM t_seller_val val, t_member_basic_attr bas ";
        $sql .= " WHERE val.membercd = ? ";
        $sql .= " AND val.consumermembercd = bas.membercd ";
        $sql .= " AND bas.delflg = FALSE ";
        $sql .= " AND val.delflg = FALSE ";
        $res = $db->getAll($sql, $params);

        if (DB::isError($res)) {
            $log->trace("rollback", "Onedepo_Db_Valuation#selectByMembercd");
            $db->rollback();
            $res = false;
        }
        $log->trace("commit", "Onedepo_Db_Valuation#selectByMembercd");
        $this->dbaccess->doClose($db);
        return $res;
    }

    function getConsumerData($membercd, $orderno)
    {
        $res = true;
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($orderno);
        $sql  = "SELECT val.valuation, val.comment, tit.orderno, tit.orderdate, val.consumermembercd, ";
        $sql .=        "tit.ordermember, tit.sendpostalcd, tit.sendaddr1, tit.sendaddr2, tit.sendtelno1, tit.addressee, ";
        $sql .=        "tit.sendbuyer, ";
        $sql .=        "bas.lastnm, bas.firstnm, od.price, ";
        $sql .=        "pre.prefnm ";
        
        $sql .= " FROM t_order_title tit ";
        $sql .= " LEFT  JOIN ( ";
        $sql .= "     SELECT orderno, SUM(price) as price ";
        $sql .= "       FROM t_order_detail ";
        $sql .= "      WHERE typediv IN (1,2,3,10,11) ";
        $sql .= "   GROUP BY orderno ";
        $sql .= " )as od  ON od.orderno = tit.orderno ";
        $sql .= " LEFT  JOIN t_member_basic_attr bas ";
        $sql .= "         ON bas.membercd = tit.ordermember ";
        $sql .= "        AND bas.delflg = false ";
        $sql .= " INNER JOIN t_pref pre ";
        $sql .= "         ON pre.prefcd = tit.sendprefcd ";
        $sql .= "        AND pre.delflg = false ";
        $sql .= " LEFT  JOIN t_seller_val val";
        $sql .= "         ON val.orderno = tit.orderno ";
        $sql .= "        AND val.delflg = false ";

        $sql .= " WHERE tit.orderno    = ? ";

        $res = $db->getRow($sql, $params);

        if (DB::isError($res)) {

            $log->trace("rollback", "Onedepo_Db_Valuation#selectByMembercd");
            $db->rollback();
            $res = false;
        }
        $log->trace("commit", "Onedepo_Db_Valuation#selectByMembercd");
        $this->dbaccess->doClose($db);
        return $res;
    }
/*    
    function getNoEstimateList($membercd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        $params = array($membercd);
        
        $sql  = "SELECT distinct ot.*, od.*, item.*, fd.forwardstate ";
        $sql .= "FROM t_order_detail od ";
        $sql .= "INNER JOIN t_order_title ot ";
        $sql .= "        ON ot.orderno = od.orderno ";
        $sql .= "       AND ot.delflg = false ";
        $sql .= "INNER JOIN t_item_basic_attr item ";
        $sql .= "        ON item.itemcd = od.itemcd ";
        $sql .= "       AND item.delflg = false ";
        $sql .= "LEFT  JOIN t_seller_val sv ";
        $sql .= "        ON ot.orderno = sv.orderno ";
        $sql .= "       AND sv.delflg = false ";
        $sql .= "LEFT  JOIN t_forward_detail fd ";
        $sql .= "        ON ot.orderno = fd.orderno ";
        $sql .= "       AND fd.delflg = false ";
        $sql .= "WHERE sv.orderno IS NULL ";
        $sql .= "AND   od.delflg = false ";
        $sql .= "AND   ot.ordermeber = ? ";
        
        $res = $db->getAll($sql, $params);

        if (DB::isError($res)) {

            $log->trace("error", "Onedepo_Db_Valuation#getNoEstimateList");
            $res = false;
        }

        $log->trace("success", "Onedepo_Db_Valuation#getNoEstimateList");
        $this->dbaccess->doClose($db);
        return $res;
    }
*/
    function checkEstimate($orderno)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        $params = array($orderno);
        
        $sql  = "SELECT count(*) ";
        $sql .= "FROM  t_order_title ot ";
        $sql .= "LEFT  JOIN t_seller_val sv ";
        $sql .= "        ON ot.orderno = sv.orderno ";
        $sql .= "       AND sv.delflg = false ";
        $sql .= "WHERE sv.orderno IS NULL ";
        $sql .= "AND   ot.delflg = false ";
        $sql .= "AND   ot.orderno = ? ";
        
        $res = $db->getOne($sql, $params);

        if (DB::isError($res)) {

            $log->trace("error", "Onedepo_Db_Valuation#checkEstimate");
            $res = false;
        }

        $log->trace("success", "Onedepo_Db_Valuation#checkEstimate");
        $this->dbaccess->doClose($db);
        if($res != 0){
            return true;
        }else{
            return false;
        }
    }

}
?>
