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


/**
 * 新規会員テーブル関連
 *
 * @package     Onedepo.Db.Member.Member
 * @author      FUCO Tominaga
 * @access      public
 */
class Onedepo_Db_Member_Seller
{
    var $dbaccess;
    
    function checkDuplicate($membercd)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#checkDuplicate";
        
        $params = array($membercd);
        
        $sql = "SELECT COUNT(*) AS cnt FROM t_seller_attr WHERE membercd = ?";
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        $res = $db->getRow($sql,$params);
        
        if (DB::isError($res) || !$res) {
            $ret = false;
        } else {
            if($res['cnt'] <= 0){
                $ret = false;
            } else {
                $ret = true;
            }
        }
        $this->dbaccess->doClose($db);
        return $ret;
        
    }
    
    function insert($membercd, $data, $payinfo, $updusernm)
    {
        $ret = true;
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#insert";

        if($data->paymode == "card") {
            $credit = 1;
        } else {
            $credit = 2;
        }
        
        //$data->news = nl2br($data->news);
        $params = array( $membercd,
                         $data->receive_bank,
                         $data->receive_branch,
                         $data->receive_account, //4
                         
                         $data->receive_account_num,
                         $data->receive_account_name,
                         null, //depinfo  //7
                         
                         $data->post_code_a."-".$data->post_code_b,
                         $data->pref,
                         $data->address1,
                         $data->address2,
                         $data->phone_a."-".$data->phone_b."-".$data->phone_c,
                         NULL, //$data->destination, //13
                         
                         $payinfo,
                         $data->pay_bank,
                         $data->pay_branch,  //16
                         
                         $data->pay_account,
                         $data->pay_account_num,
                         $data->pay_account_name,
                         null, //identtelno  //20
                         
                         $credit,
                         $data->card_type,
                         $data->card_name,
                         $data->card_num,  //24
                         
                         $data->card_limit_y,
                         $data->card_limit_m,
                         $data->card_sec_code,  //27
                         
                         $data->sellerdiv,      //sellerdiv
                         null,  //basicfee
                         null,  //exsystemfee
                         null,  //stsystemfee
                         1,  //sellerstate  初期値は 1:未承認
                         true,  //itemexhibitstop  初期値は true: 出品停止  //33
                         
                         $data->news,

                         (empty($data->sellersouryou)) ? 0 : $data->sellersouryou,
                         (empty($data->sellersouryoumax)) ? 0 : $data->sellersouryoumax,
                         
                         null,  //profile
                         
                         $data->takkyubinprc,  //takkyubinprc
                         
                         date("Y-m-d H:i:s"),
                         $updusernm,  //updusernm
                         $_SERVER['REMOTE_ADDR'],  //updpcnm
                         null,  //updcnt  //40
                         
                         );
        
        /*
        $sql = "INSERT INTO t_seller_attr(" .
        	   "MemberCd, DepBankNm, DepBranchNm, DepAccount, " .
        	   "DepAccountNo, DepAccountNm, DepInfo, DepPostalCd, " .
        	   "DepPrefCd, DepAddr1, DepAddr2, DepTelno1, " .
        	   "DepAddressee, PayInfo, PayBankNm, PayBranchNm, " .
        	   "PayAccount, PayAccountNo, PayAccountNm, IdentTelno, " .
        	   "CreditRegist, CreditType, CreditAccountNm, CreditAccountNo, " .
        	   "CreditExpireYear, CreditExpireMonth, SecurityCd, News," .
        	   "DelFlg, AddDate, UpdDate, UpdUsrNm, " .
        	   "UpdPcNm, UpdCnt) " .
        	   "values (" .
        	   "?, ?, ?, ?, " .
        	   "?, ?, 1, ?, " .
        	   "?, ?, ?, ?, " .
        	   "?, ?, ?, ?, " .
        	   "?, ?, ?, null, " .
        	   "?, ?, ?, ?, " .
        	   "?, ?, ?, ?, " .
        	   "'false', ?, ?, '', " .
        	   "?, '0');";
		*/

        $sql  = "select * from f_seller_attr_update(";
		$sql .= "1, ?, ?, ?, ?, ";
 		$sql .= "?, ?, ?, ?, ?, "; //10
 		$sql .= "?, ?, ?, ?, ?, ";
 		$sql .= "?, ?, ?, ?, ?, "; //20
 		$sql .= "?, ?, ?, ?, ?, ";
 		$sql .= "?, ?, ?, ?, ?, "; //30
 		$sql .= "?, ?, ?, ?, ?, ";
 		$sql .= "?, ?, ?, ?, ?, "; //40
 		$sql .= "?, ?, ?)";
 		
        $db = $this->dbaccess->doConnect();
        $res = $db->getRow($sql,$params);

        if (DB::isError($res) || !$res) {
            $log->trace("rollback", $func_name);
            $db->rollback();
            $ret = false;
        } else {
            $log->trace("commit", $func_name);

            $db->commit();
            $ret = true;
        }
        $this->dbaccess->doClose($db);
        return $ret;
    }
    
    
    function update($membercd, $data, $payinfo, $updusernm)
    {
        $ret = true;
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#update";

        if($data->paymode == "card"){
            $credit = 1;
        } else {
            $credit = 2;
        }
        
        //$data->news = nl2br($data->news);
        $params = array($membercd,
                        $data->receive_bank,
                        $data->receive_branch,
                        $data->receive_account, //4
                         
                        $data->receive_account_num,
                        $data->receive_account_name,
                        null, //depinfo  //7
                         
                        $data->post_code_a."-".$data->post_code_b,
                        $data->pref,
                        $data->address1,
                        $data->address2,
                        $data->phone_a."-".$data->phone_b."-".$data->phone_c,
                        NULL, //$data->destination, //13
                         
                        $payinfo,
                        $data->pay_bank,
                        $data->pay_branch,  //16
                         
                        $data->pay_account,
                        $data->pay_account_num,
                        $data->pay_account_name,
                        null, //identtelno  //20
                         
                        $credit,
                        $data->card_type,
                        $data->card_name,
                        $data->card_num,  //24
                         
                        $data->card_limit_y,
                        $data->card_limit_m,
                        $data->card_sec_code,  //27
                         
                        $data->sellerdiv,  //sellerdiv
                        null,  //basicfee
                        null,  //exsystemfee
                        null,  //stsystemfee
                        null,  //sellerstate
                        null,  //itemexhibitstop  //33
                         
                        $data->news,
                         
                        (empty($data->sellersouryou)) ? 0 : $data->sellersouryou,
                        (empty($data->sellersouryoumax)) ? 0 : $data->sellersouryoumax,
                         
                        null,  //profile
                         
                        $data->takkyubinprc,  //takkyubinprc
                         
                        date("Y-m-d H:i:s"),
                        $updusernm,  //updusernm
                        $_SERVER['REMOTE_ADDR'],  //updpcnm
                        $data->updCnt  //updcnt  //40
                         
                        );

        $sql  = "select * from f_seller_attr_update(";
        $sql .= "2, ?, ?, ?, ?, ";
        $sql .= "?, ?, ?, ?, ?, "; //10
        $sql .= "?, ?, ?, ?, ?, ";
        $sql .= "?, ?, ?, ?, ?, "; //20
        $sql .= "?, ?, ?, ?, ?, ";
        $sql .= "?, ?, ?, ?, ?, "; //30
        $sql .= "?, ?, ?, ?, ?, ";
        $sql .= "?, ?, ?, ?, ?, "; //40
        $sql .= "?, ?, ?)";
        
        $db = $this->dbaccess->doConnect();
        $res = $db->getRow($sql,$params);

        if (DB::isError($res) || !$res) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $ret = false;
            trigger_error($res->userinfo, E_USER_ERROR);
        } else {
            $log->debug("commit", $func_name);

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

    function getSellerInfo($memberCd)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#getSellerInfo";

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

        $params = array($memberCd);
        $sql = "select * from t_seller_attr where membercd = ?;";
        $data = $db->getRow($sql, $params);
        if (DB::isError($data) || !$data) {
            $log->debug("err", $func_name);
            //die($data->getMessage());
            return false;
        }
        $this->dbaccess->doClose($db);
        return $data;
    }
    
    // 出品一時停止
    function stopItemExhibit($membercd)
    {
        $params = array($membercd);
        $ret = true;
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#changeItemExhibit";

        $sql  = "UPDATE t_seller_attr SET itemexhibitstop = TRUE ";
        $sql .= " WHERE membercd = ?;";
        
        $db = $this->dbaccess->doConnect();
        $res = $db->query($sql,$params);

        if (DB::isError($res) || !$res) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $ret = false;
        } else {
            $log->debug("commit", $func_name);

            $db->commit();
            $ret = true;
        }
        
        $this->dbaccess->doClose($db);
        return $ret;
    }
    
    // 出品一時停止解除
    function releaseItemExhibit($membercd)
    {
        $params = array($membercd);
        $ret = true;
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#changeItemExhibit";

        $sql  = "UPDATE t_seller_attr SET itemexhibitstop = FALSE ";
        $sql .= " WHERE membercd = ?;";
        
        $db = $this->dbaccess->doConnect();
        $res = $db->query($sql,$params);

        if (DB::isError($res) || !$res) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $ret = false;
        } else {
            $log->debug("commit", $func_name);

            $db->commit();
            $ret = true;
        }
        
        $this->dbaccess->doClose($db);
        return $ret;
    }
    
    // 出品一時停止のチェック
    function checkItemExhibit($membercd)
    {
        $params = array($membercd);
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#changeItemExhibit";
        
        $sql  = "SELECT itemexhibitstop FROM t_seller_attr WHERE membercd = ?;";
        
        $db = $this->dbaccess->doConnect();
        $data = $db->getRow($sql,$params);

        if (DB::isError($data) || !$data) {
            $log->debug("err", $func_name);
            //die($data->getMessage());
            return false;
        }
        
        $this->dbaccess->doClose($db);
        return $data;
    }
    
    function getSellerVal($memberCd)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#getSellerVal";

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

        $params = array($memberCd);
        $sql = "select AVG(valuation) as val from t_seller_val where membercd = ? group by membercd;";
        $data = $db->getRow($sql, $params);
        if (DB::isError($data) || !$data) {
            $log->debug("error ". $data->userinfo, $func_name);
            return false;
        }

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

    function getSellerValCount($memberCd)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#getSellerValCount";

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

        $params = array($memberCd);
        $sql = "select coalesce(count(valuation), 0) as val from t_seller_val where membercd = ? group by membercd;";
        $data = $db->getOne($sql, $params);
        if (DB::isError($data) || !$data) {
            $log->debug("error ". $data->userinfo, $func_name);
            return false;
        }

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

    
    // 出品者承認
    function changeState($membercd, $state)
    {
        $params = array($state, $membercd);
        $ret = true;
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#changeState";

        $sql  = "UPDATE t_seller_attr SET ";
        switch ($state) {
            case 2:  // 承認
            $sql .= "sellerstate = ?, ";
            $sql .= "itemexhibitstop = FALSE ";
            break;

            case 3:  // 承認却下
            $sql .= "sellerstate = ?, ";
            $sql .= "itemexhibitstop = TRUE ";
            break;
        }
        $sql .= " WHERE membercd = ?;";
        
        $db = $this->dbaccess->doConnect();
        $res = $db->query($sql,$params);

        if (DB::isError($res) || !$res) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $ret = false;
        } else {
            $log->debug("commit", $func_name);

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

    /**
     * 出品者ページ
     */
    function updateInfomation($membercd, $infomation, $updcnt)
    {
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();
        $params = array($membercd, $infomation, '', $_SERVER['REMOTE_ADDR'], $updcnt);

        $sql  = "SELECT * FROM f_seller_infomation_update (2, ?, ?, ?, ?, ?);";
        $res = $db->getOne($sql, $params);

        if (DB::isError($res) || $res < 0) {
            $log->error("rollback ".$res->userinfo, $func_name);
            return false;
        }

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

    function selectSellers($limit=0, $offset=0){
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Seller#selectSellers";

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

        $sql  = "SELECT tba.*, tsa.membercd as sellercd, tsa.sellerstate as sellerstate, tca.membercd as consumercd, tsa.infomation as infomation";
        $sql  .= " FROM t_member_basic_attr as tba";
        $sql  .= " LEFT JOIN t_seller_attr as tsa ON tba.membercd = tsa.membercd AND tsa.delflg = false";
        $sql  .= " LEFT JOIN t_consumer_attr as tca ON tba.membercd = tca.membercd AND tca.delflg = false";
        $sql  .= " WHERE tba.delflg=false AND sellerstate=2";
/****
        $sql  = "SELECT makercd, makernm, makernmkana, recommenddiv, comment ";
        $sql .= "  FROM v_maker_list ";
        $sql .= "  WHERE item_count > 0";
        $params = array();
        foreach ($kana as $key => $val) {
            if ($key==0) $sql .= " AND (";
            else         $sql .= " OR";
            $sql .= " makernmkana LIKE ?";
            $params[] = "$val%";
        }
        $sql .= ") ORDER BY %s";
        $sql = sprintf($sql, $sort);
***/
        if(!empty($limit)) {
            $sql .= " LIMIT ". $limit  ." OFFSET ". $offset;
        }

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

        return $data;
    }

}
?>