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


/**
 * 購入者属性テーブル関連
 *
 * @package     Onedepo.Db.Member.Customer
 * @author      netnee T.Otogawa<tomokazu.otogawa@netnee.jp>
 * @access      public
 */
class Onedepo_Db_Member_Consumer
{
    var $dbaccess;


/*
      function select_all($sort="disppriority")
    {
        $db = $this->dbaccess->doConnect();
        $db->autoCommit(false);
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($sort);

        $db->query("BEGIN;");
        $db->query("select f_cast_select_all(?);", $params);
        $data = $db->getAll("FETCH ALL IN results;");
        $db->query("COMMIT;");
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);

        return $data;
    }
*/

    function select_one($code)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
//      $db->autoCommit(false);
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($code);

//      $db->query("BEGIN;");
        $data = $db->getRow("select * from t_consumer_attr where MemberCd = ? AND delflg=FALSE;", $params);
//      $db->query("COMMIT;");
        if (DB::isError($data)) {
            $log->debug("err", "Onedepo_Db_Member_Consumer#select_one");
            //die($data->getMessage());
            return false;
        }

        $this->dbaccess->doClose($db);

        return $data;
    }

    function insert($membercd,$zip1,$zip2,$pref,$addr1,$addr2,$phone1,$phone2,$sendzip1,$sendzip2,$sendpref,$sendaddr1,$sendaddr2,$sendphone,$addressee) {
        $ret = true;
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();
        
        $zip                = $zip1 . "-" . $zip2;
        $sendzip            = $sendzip1 . "-" . $sendzip2;
        $phone1_notarray    = $phone1[0] . "-" . $phone1[1] . "-" . $phone1[2];
        $phone2_notarray    = $phone2[0] . "-" . $phone2[1] . "-" . $phone2[2];
        $sendphone_notarray = $sendphone[0] . "-" . $sendphone[1] . "-" . $sendphone[2];
        $adddate            = date("Y-m-d H:i:s");
        //MemberCd（本来は会員コードを貰ってくる）の暫定的な代入
        //$membercd = mt_rand();
        
        //必須項目以外のnullチェックここから
        if ($addr2              == "") $addr2              = null;
        //if ($phone2             == "") $phone2             = null;
        if ($sendzip            == "") $sendzip            = null;
        if ($sendpref           == "") $sendpref           = null;
        if ($sendaddr1          == "") $sendaddr1          = null;
        if ($sendaddr2          == "") $sendaddr2          = null;
        if ($sendphone_notarray == "") $sendphone_notarray = null;
        if ($addressee          == "") $addressee          = null;
        if ($phone2[0] == "" || $phone2[1] == "" || $phone2[2] == "") $phone2_notarray = null;
        //nullチェックここまで
        
        $params = array($membercd,
        				$zip, 
                        $pref, 
                        $addr1, 
                        $addr2, 
                        $phone1_notarray, 
                        $phone2_notarray, 
                        $sendzip, 
                        $sendpref, 
                        $sendaddr1, 
                        $sendaddr2,
                        $sendphone_notarray, 
                        $addressee,
                        date("Y-m-d H:i:s"),
                        $_SERVER['REMOTE_ADDR']);

        /*
        $res = $db->query("insert into t_consumer_attr(" .
        		          "MemberCd, PostalCd, PrefCd, Addr1, Addr2, Telno1, Telno2, " .
        		          "SendPostalCd, SendPrefCd, SendAddr1, SendAddr2, SendTelno1, Addressee, " .
        		          "DelFlg, AddDate, UpdUsrNm, UpdPcNm, UpdCnt) " .
        		          "values(" .
        		          "?, ?, ?, ?, ?, ?, ?, " .
        		          "?, ?, ?, ?, ?, ?, " .
        		          "false, ?, '', ?, 1);" ,$params);
        		          
        		          //エラーチェック用
        		          //var_dump($db);
        		          //exit;
        */
        
        $res = $db -> getRow("select * from f_consumer_attr_update(1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL);", $params);
        
        if (DB::isError($res) || !$res) {
            $log->trace("rollback", "Onedepo_Db_Member_Consumer#insert");
			
			//エラーチェック用
			//echo $res->toString();
			
            $db->rollback();
            $ret = false;
        }
        $log->trace("commit", "Onedepo_Db_Member_Consumer#insert");

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

    function InsertForBuyerInformation($membercd, $zip1, $zip2, $pref, $addr1, $addr2, $phone1, $phone2,
                                        $sendzip1, $sendzip2, $sendpref, $sendaddr1, $sendaddr2, $sendphone,
                                        $addressee, $lastnm, $firstnm, $lastnmkana, $firstnmkana) {
        //$ret = true;
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Consumer#InsertForBuyerInformation";
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $db->autoCommit(false);
        $db->query('BEGIN;');
        
        //購入者情報追加
        
        $zip                = $zip1 . "-" . $zip2;
        $sendzip            = $sendzip1 . "-" . $sendzip2;
        $phone1_notarray    = $phone1[0] . "-" . $phone1[1] . "-" . $phone1[2];
        $phone2_notarray    = $phone2[0] . "-" . $phone2[1] . "-" . $phone2[2];
        $sendphone_notarray = $sendphone[0] . "-" . $sendphone[1] . "-" . $sendphone[2];
        $adddate            = date("Y-m-d H:i:s");
        //MemberCd（本来は会員コードを貰ってくる）の暫定的な代入
        //$membercd = mt_rand();
        
        //必須項目以外のnullチェックここから
        if ($addr2              == "") $addr2              = null;
        //if ($phone2             == "") $phone2             = null;
        if ($sendzip            == "") $sendzip            = null;
        if ($sendpref           == "") $sendpref           = null;
        if ($sendaddr1          == "") $sendaddr1          = null;
        if ($sendaddr2          == "") $sendaddr2          = null;
        if ($sendphone_notarray == "") $sendphone_notarray = null;
        if ($addressee          == "") $addressee          = null;
        if ($phone2[0] == "" || $phone2[1] == "" || $phone2[2] == "") $phone2_notarray = null;
        //nullチェックここまで
        
        $params = array($membercd, $zip, $pref, $addr1, $addr2, $phone1_notarray, $phone2_notarray, $sendzip,
                        $sendpref, $sendaddr1, $sendaddr2, $sendphone_notarray, $addressee, date("Y-m-d H:i:s"),
                        $_SERVER['REMOTE_ADDR']);
        
        $res = $db -> getRow("SELECT * FROM f_consumer_attr_update(1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL);", $params);
        
        if (DB::isError($res) || $res < 0) {
            $db->query('ROLLBACK;');
            $db->rollback();
            $this->dbaccess->doClose($db);
            trigger_error($res->userinfo, E_USER_ERROR);
            return false;
        }
        
        //会員情報系更新
        
        $sql  = "SELECT * FROM t_member_basic_attr WHERE membercd=? AND delflg=FALSE";
        $data = $db->getRow($sql, array($membercd));
        
        if (DB::isError($data) || $data <= 0) {
            $db->query('ROLLBACK;');
            $db->rollback();
            $this->dbaccess->doClose($db);
            trigger_error($data->userinfo, E_USER_ERROR);
            return false;
        }
        
        $params = array($membercd,          //会員CD
                        $data['memberdiv'],         //会員区分
                        $lastnm,            //姓
                        $firstnm,           //名
                        $lastnmkana,        //姓（かな）
                        $firstnmkana,       //名（かな）
                        $data['mailaddr'],          //メールアドレス
                        $data['mobmailaddr'],       //携帯電話メールアドレス
                        $data['password'],          //パスワード
                        $data['nicknm'],            //ニックネーム
                        $data['secquestioncd'],     //秘密の質問C
                        $data['secquestionans'],    //秘密の質問の答え
                        $data['birthdate'],         //生年月日
                        $data['sex'],               //性別
                        $data['selfintro'],         //自己紹介
                        $data['memberstate'],       //会員状態
                        $data['maildiv'],           //メール区分
                        $data['registdate'],        //登録日
                        $upduser,                   //更新ユーザ名
                        $_SERVER['REMOTE_ADDR'],    //更新マシン名
                        $data['updcnt']);           //更新カウンタ
        
        $sql  = "SELECT * FROM f_member_basic_attr_update(2, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        $result = $db->getRow($sql, $params);
        
        if (DB::isError($result) || $result < 0) {
            $db->query('ROLLBACK;');
            $db->rollback();
            $this->dbaccess->doClose($db);
            trigger_error($result->userinfo, E_USER_ERROR);
            return false;
        }
        
        $log->debug("commit", $func_name);
        $db->query('COMMIT;');
        $db->commit();
        $this->dbaccess->doClose($db);
        return true;
    }
    
    
    function update($membercd,$zip1,$zip2,$pref,$addr1,$addr2,$phone1,$phone2,$sendzip1,$sendzip2,$sendpref,$sendaddr1,$sendaddr2,$sendphone,$addressee,$updcnt) {
        $ret = true;
        $log =& LogFactory::getLog();

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

        $zip                = $zip1 . "-" . $zip2;
        $sendzip            = $sendzip1 . "-" . $sendzip2;
        $phone1_notarray    = $phone1[0] . "-" . $phone1[1] . "-" . $phone1[2];
        $phone2_notarray    = $phone2[0] . "-" . $phone2[1] . "-" . $phone2[2];
        $sendphone_notarray = $sendphone[0] . "-" . $sendphone[1] . "-" . $sendphone[2];
        $upddate            = date("Y-m-d H:i:s");
        $cntarray           = $db->getRow("select updcnt from t_consumer_attr where MemberCd = ? AND delflg=FALSE;", array($membercd));
        $cnt                = $cntarray['updcnt'];

        //必須項目以外のnullチェックここから
        if ($addr2              == "") $addr2              = null;
        //if ($phone2             == "") $phone2             = null;
        if ($sendzip            == "") $sendzip            = null;
        if ($sendpref           == "") $sendpref           = null;
        if ($sendaddr1          == "") $sendaddr1          = null;
        if ($sendaddr2          == "") $sendaddr2          = null;
        if ($sendphone_notarray == "") $sendphone_notarray = null;
        if ($addressee          == "") $addressee          = null;
        if ($phone2[0] == "" || $phone2[1] == "" || $phone2[2] == "") $phone2_notarray = null;
        //nullチェックここまで
        
        //更新回数の加算
        //$updcnt = $cnt + 1;
          
        $params = array($membercd,
                        $zip,
                        $pref,
                        $addr1,
                        $addr2,
                        $phone1_notarray,
                        $phone2_notarray,
                        $sendzip,
                        $sendpref,
                        $sendaddr1,
                        $sendaddr2,
                        $sendphone_notarray,
                        $addressee,
                        date("Y-m-d H:i:s"),
                        $_SERVER['REMOTE_ADDR'],
                        $updcnt);

        /*
        $res = $db->query("update t_consumer_attr " .
        		          "set PostalCd=?, PrefCd=?, Addr1=?, Addr2=?, Telno1=?, Telno2=?, " .
        		          "SendPostalCd=?, SendPrefCd=?, SendAddr1=?, SendAddr2=?, SendTelno1=?, " .
        		          "Addressee=?, UpdDate=?, UpdPcNm=?, UpdCnt=? " .
        		          "where MemberCd=?;" ,$params);
		*/
        
        $res = $db -> getRow("select * from f_consumer_attr_update(2, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", $params);
        
        if (DB::isError($res) || !$res) {
            $log->trace("update rollback", "Onedepo_Db_Member_Consumer#update");
            
            $db->rollback();
            $ret = false;
        }
        $log->trace("commit", "Onedepo_Db_Member_Consumer#update");
		$db->commit();
        $this->dbaccess->doClose($db);
        return $ret;
    }
    
    /****
     * 購入者情報 更新
     */
    function UpdateForBuyerInformation($membercd, $zip1, $zip2, $pref, $addr1, $addr2, $phone1, $phone2, $sendzip1, $sendzip2,
                                        $sendpref, $sendaddr1, $sendaddr2, $sendphone, $addressee, $updcnt, $lastnm, $firstnm,
                                        $lastnmkana, $firstnmkana) {
        //$ret = true;
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Member_Consumer#UpdateForBuyerInformation";
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $db->autoCommit(false);
        $db->query('BEGIN;');
        
        //購入者情報更新
        
        $zip                = $zip1 . "-" . $zip2;
        $sendzip            = $sendzip1 . "-" . $sendzip2;
        $phone1_notarray    = $phone1[0] . "-" . $phone1[1] . "-" . $phone1[2];
        $phone2_notarray    = $phone2[0] . "-" . $phone2[1] . "-" . $phone2[2];
        $sendphone_notarray = $sendphone[0] . "-" . $sendphone[1] . "-" . $sendphone[2];
        $upddate            = date("Y-m-d H:i:s");
        $cntarray           = $db->getRow("SELECT updcnt FROM t_consumer_attr where MemberCd = ? AND delflg=FALSE;", array($membercd));
        $cnt                = $cntarray['updcnt'];
        
        //必須項目以外のnullチェックここから
        if ($addr2              == "") $addr2              = null;
        //if ($phone2             == "") $phone2             = null;
        if ($sendzip            == "") $sendzip            = null;
        if ($sendpref           == "") $sendpref           = null;
        if ($sendaddr1          == "") $sendaddr1          = null;
        if ($sendaddr2          == "") $sendaddr2          = null;
        if ($sendphone_notarray == "") $sendphone_notarray = null;
        if ($addressee          == "") $addressee          = null;
        if ($phone2[0] == "" || $phone2[1] == "" || $phone2[2] == "") $phone2_notarray = null;
        //nullチェックここまで
        
        //更新回数の加算
        //$updcnt = $cnt + 1;
        
        $params = array($membercd, $zip, $pref, $addr1, $addr2, $phone1_notarray, $phone2_notarray,
                        $sendzip, $sendpref, $sendaddr1, $sendaddr2, $sendphone_notarray, $addressee,
                        date("Y-m-d H:i:s"), $_SERVER['REMOTE_ADDR'], $updcnt);
        
        $res = $db -> getRow("SELECT * FROM f_consumer_attr_update(2, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", $params);
        
        if (DB::isError($res) || $res < 0) {
            $db->query('ROLLBACK;');
            $db->rollback();
            $this->dbaccess->doClose($db);
            trigger_error($res->userinfo, E_USER_ERROR);
            return false;
        }
        
        
        //会員情報系更新
        
        $sql  = "SELECT * FROM t_member_basic_attr WHERE membercd=? AND delflg=FALSE";
        $data = $db->getRow($sql, array($membercd));
        
        if (DB::isError($data) || $data <= 0) {
            $db->query('ROLLBACK;');
            $db->rollback();
            $this->dbaccess->doClose($db);
            trigger_error($data->userinfo, E_USER_ERROR);
            return false;
        }
        
        $params = array($membercd,     //会員CD
                        $data['memberdiv'],           //会員区分
                        $lastnm,       //姓
                        $firstnm,      //名
                        $lastnmkana,   //姓（かな）
                        $firstnmkana,  //名（かな）
                        $data['mailaddr'],           //メールアドレス
                        $data['mobmailaddr'],        //携帯電話メールアドレス
                        $data['password'],           //パスワード
                        $data['nicknm'],             //ニックネーム
                        $data['secquestioncd'],      //秘密の質問C
                        $data['secquestionans'],     //秘密の質問の答え
                        $data['birthdate'],          //生年月日
                        $data['sex'],                //性別
                        $data['selfintro'],          //自己紹介
                        $data['memberstate'],        //会員状態
                        $data['maildiv'],            //メール区分
                        $data['registdate'],         //登録日
                        $upduser,                    //更新ユーザ名
                        $_SERVER['REMOTE_ADDR'],     //更新マシン名
                        $data['updcnt']);            //更新カウンタ
        
        $sql  = "SELECT * FROM f_member_basic_attr_update(2, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        $result = $db->getRow($sql, $params);
        
        if (DB::isError($result) || $result < 0) {
            $db->query('ROLLBACK;');
            $db->rollback();
            $this->dbaccess->doClose($db);
            trigger_error($result->userinfo, E_USER_ERROR);
            return false;
        }
        
        $log->debug("commit", $func_name);
        $db->query('COMMIT;');
        $db->commit();
        $this->dbaccess->doClose($db);
        return true;
    }
    
    
    function checkDuplicate($membercd)
    {
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($membercd);

        $data = $db->getRow("SELECT COUNT(*) AS cnt FROM t_consumer_attr WHERE membercd = ?;", $params);
        if (DB::isError($data)) {
            //die($data->getMessage());
            $ret = false;
        } else {
            if($data['cnt'] <= 0){
                $ret = false;
            } else {
                $ret = true;
            }
        }

        $this->dbaccess->doClose($db);

        return $ret;
    }
    /*
     function delete($code, $updcnt) {
        $ret = true;
        $log =& LogFactory::getLog();

        $db = $this->dbaccess->doConnect();
        $params = array($code, $updcnt);

        $res = $db->getOne("select f_cast_update(3, ?, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, ?);", $params);
        if (DB::isError($res) || !$res) {
            $log->trace("rollback", "Onedepo_Db_Cast#delete");

            $db->rollback();
            $ret = false;
        }
        $log->trace("commit", "Onedepo_Db_Cast#delete");

        $this->dbaccess->doClose($db);
        return $ret;
    }
*/
}
?>
