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

    /**
     * 全件取得
     * 
     * @param string $sort ソートキー
     * @return object 取得レコード
     */
    function selectAll($key)
    {
        switch($key) {
            case "date":
                $sort = "adddate";
                break;
            case "valup":
                $sort = "valuation";
                break;
            case "valdown":
                $sort = "valuation DESC";
                break;
            case "consumer":
                $sort = "consumermembercd";
                break;
            default:
                $sort = "orderno";
        }
        
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        $sql  = "SELECT sv.membercd, (mb1.lastnm || mb1.firstnm) AS membernm, sv.adddate, ";
        $sql .= "sv.valuation, sv.comment, sv.consumermembercd, (mb2.lastnm || mb2.firstnm) AS consumernm, sv.orderno ";
        $sql .= "FROM t_seller_val sv ";
        $sql .= "INNER JOIN t_member_basic_attr mb2 ";
        $sql .= "        ON mb2.membercd = sv.consumermembercd ";
        $sql .= "LEFT  JOIN t_member_basic_attr mb1 ";
        $sql .= "        ON mb1.membercd = sv.membercd ";
        $sql .= "WHERE mb2.delflg = false ";
        $sql .= "AND   sv.delflg = false ";
        $sql .= "ORDER BY %s ";
        
        /*
        $sql  = "SELECT membercd, adddate, valuation, comment, consumermembercd, orderno ";
        $sql .= "FROM t_seller_val ";
        $sql .= "WHERE delflg = FALSE ";
        $sql .= "ORDER BY %s;";
        */
        $sql = sprintf($sql, $sort);
        $log->debug($sql, "Onedepo_Db_Usermanage_Valuation#selectAllTag");

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

        return $data;
    }
    
    function getConsumerName($consumercd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $key  = $consumercd;
        $sql  = "SELECT lastnm, firstnm ";
        $sql .= "FROM t_member_basic_attr ";
        $sql .= "WHERE delflg = FALSE ";
        $sql .=    "AND membercd = %s;";
        $sql = sprintf($sql, $key);
        $log->debug($sql, "Onedepo_Db_Usermanage_Valuation#getUserName");

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

        return $data;
    }
    
    function getSellerName($sellercd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $key  = $sellercd;
        $sql  = "SELECT lastnm, firstnm ";
        $sql .= "FROM t_member_basic_attr ";
        $sql .= "WHERE delflg = FALSE ";
        $sql .=    "AND membercd = %s;";
        $sql = sprintf($sql, $key);
        $log->debug($sql, "Onedepo_Db_Usermanage_Valuation#getUserName");

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

        return $data;
    }
    
    function updateValuation($code)
    {
        $ret = true;
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        $params = array($code);
        
        $res = $db->query("update t_seller_val " .
                          "set delflg=TRUE " .
                          "where orderno=?;" ,$params);
        
        if (DB::isError($res) || !$res) {
            $log->trace("update rollback", "Onedepo_Db_Usermanage_Valuation#update");

            $db->rollback();
            $ret = false;
        }
        $log->trace("commit", "Onedepo_Db_Usermanage_Valuation#update");
        $db->commit();
        $this->dbaccess->doClose($db);
        return $ret;
    }

}
?>
