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


/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * 商品構成
 *
 * @package     Onedepo.Db.Itemstruct
 * @author      H.Watanabe <hiroshi.watanabe@netnee.jp>
 * @access      public
 */
class Onedepo_Db_Itemstruct
{
    var $dbaccess;
    
    function getOneItem($itemcd, $structcd)
    {
        $log =& LogFactory::getLog();

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


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

        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);
        
        return $data;
    }
    
    function getOneItemOneStruct($itemcd, $structcd)
    {
        $log =& LogFactory::getLog();

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


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

        $data = $db->getRow($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);
        
        return $data;
    }
    
    
    /*
    * 更新
    *
    */
    function update($itemcd, $structcd, $variationnm, $makeritemcd, $jancd, $salestartdate, $orderclosedate, $makerprice, $comment, $option01, $option02, $option03, $option04, $option05, $option06, $option07, $option08, $option09, $option10, $download, $dldata, $stock, $upduser, $updcnt) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Itemstruct#update";
        
        
        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $db->autoCommit(false);
        $db->query("BEGIN;");
        
        if (empty($itemcd)){
            $itemcd = NULL;
        }
        if (empty($structcd)){
            $structcd = NULL;
        }
        if (strlen($makerprice)<=0){
            $makerprice = NULL;
        }
        if (empty($salestartdate)){
            $salestartdate = NULL;
        }
        if (empty($orderclosedate)){
            $orderclosedate = NULL;
        }
        if (empty($option01)){
            $option01 = NULL;
        }
        if (empty($option02)){
            $option02 = NULL;
        }
        if (empty($option03)){
            $option03 = NULL;
        }
        if (empty($option04)){
            $option04 = NULL;
        }
        if (empty($option05)){
            $option05 = NULL;
        }
        if (empty($option06)){
            $option06 = NULL;
        }
        if (empty($option07)){
            $option07 = NULL;
        }
        if (empty($option08)){
            $option08 = NULL;
        }
        if (empty($option09)){
            $option09 = NULL;
        }
        if (empty($option10)){
            $option10 = NULL;
        }
        
        $params = array($itemcd, $structcd, $variationnm, $makeritemcd, $jancd, 
                        $salestartdate, $orderclosedate, 
                        $makerprice, $comment,
                        $option01, $option02, $option03, $option04, $option05,
                        $option06, $option07, $option08, $option09, $option10, 
                        $upduser, $_SERVER['REMOTE_ADDR'], $updcnt);
        $sql  = "SELECT * FROM f_item_struct_update(2, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        $res = $db->getRow($sql, $params);
        
        if (DB::isError($res) || $res["errcd"] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("rollback : ".$res->userinfo, $func_name);
            
            return $res;
        }

        // ダウンロード販売
        if($download==1) {
            
            // ダウンロード商品テーブル
            $sql  = "SELECT * FROM f_dl_item_update(2, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
            $params = $dldata;
            
            $res = $db->getRow($sql, $params);
            if (DB::isError($res) || $res["errcd"] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_dl_item_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
            
            // 出品在庫明細
            $sql  = "SELECT * FROM f_stock_update_price(?, ?, ?, ?, ?, ?, ?);";
            $params = $stock;
            
            $res = $db->getRow($sql, $params);
            if (DB::isError($res) || $res["errcd"] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_stock_update_price#rollback : ". $res->userinfo, $func_name);
                return false;
            }
        }
        
        $db->commit();
        $db->query("COMMIT;");
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        
        return true;
    }
    
    
    /**
     * 追加
     *
     */
    function insert($itemcd, $variationnm, $makeritemcd, $jancd, $salestartdate, $orderclosedate, $makerprice, $comment, $option01, $option02, $option03, $option04, $option05, $option06, $option07, $option08, $option09, $option10, $download, $upduser) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Itemstruct#insert";
        
        
        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $db->autoCommit(false);
        $db->query("BEGIN;");
        
        if (empty($itemcd)){
            $itemcd = NULL;
        }
        if (empty($makerprice)){
            $makerprice = NULL;
        }
        if (empty($salestartdate)){
            $salestartdate = NULL;
        }
        if (empty($orderclosedate)){
            $orderclosedate = NULL;
        }
        if (empty($option01)){
            $option01 = NULL;
        }
        if (empty($option02)){
            $option02 = NULL;
        }
        if (empty($option03)){
            $option03 = NULL;
        }
        if (empty($option04)){
            $option04 = NULL;
        }
        if (empty($option05)){
            $option05 = NULL;
        }
        if (empty($option06)){
            $option06 = NULL;
        }
        if (empty($option07)){
            $option07 = NULL;
        }
        if (empty($option08)){
            $option08 = NULL;
        }
        if (empty($option09)){
            $option09 = NULL;
        }
        if (empty($option10)){
            $option10 = NULL;
        }
        
        $params = array($itemcd, $variationnm, $makeritemcd, $jancd, $salestartdate, 
                        $orderclosedate, $makerprice, $comment,
                        $option01, $option02, $option03, $option04, $option05,
                        $option06, $option07, $option08, $option09, $option10, 
                        $upduser, $_SERVER['REMOTE_ADDR']);
        $sql  = "SELECT * FROM f_item_struct_update(1, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL);";
        $res = $db->getRow($sql, $params);
        
        if (DB::isError($res) || $res["errcd"] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("rollback : ".$res->userinfo, $func_name);
            
            return $res;
        }
        
        // ダウンロード販売
        if($download==1) {
            // 追加した構成コード取得
            $fixcode2 = $res["code"];
            
            // ダウンロード商品テーブル
            $sql  = "SELECT * FROM f_dl_item_update(1, ?, ?, ?, ?, ?, ?, ?, ?, NULL);";
            $params = array($itemcd, $fixcode2, false, DL_COUNT, DL_REPIOD_DAYS, '', $upduser, $_SERVER['REMOTE_ADDR']);
            
            $res = $db->getRow($sql, $params);
            if (DB::isError($res) || $res["errcd"] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_dl_item_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
            
            // 仕入見出しテーブル
            $sql  = "SELECT * FROM f_stock_title_update(1, NULL, NULL, NULL, 1, ?, ?, NULL);";
            $params = array($upduser, $_SERVER['REMOTE_ADDR']);
            
            $res = $db->getRow($sql, $params);
            if (DB::isError($res) || $res["errcd"] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_stock_title_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
            
            // 追加した仕入れ見出しコード(stocktitlecd)取得
            $fixcode3 = $res["code"];
            
            // 出品在庫明細
            $sql  = "SELECT * FROM f_ex_stock_detail_update(1, ?, ?, NULL, 9999,?, 10000, 1, 2000, 0, 3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,?,?, NULL);";
            $params = array($itemcd, $fixcode2, $fixcode3, $upduser, $_SERVER['REMOTE_ADDR']);
            
            $res = $db->getRow($sql, $params);
            if (DB::isError($res) || $res["errcd"] < 0) {
                $db->rollback();
                $db->query("ROLLBACK;");
                $log->error("f_ex_stock_detail_update#rollback : ". $res->userinfo, $func_name);
                return false;
            }
        }

        $db->commit();
        $db->query("COMMIT;");
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        
        return true;
    }
    
    /**
     * 削除
     *
     */
    function delete($itemcd, $structcd, $upduser, $updcnt) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Itemstruct#delete";
        
        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $db->autoCommit(false);
        $db->query("BEGIN;");
        
        if (empty($itemcd)){
            $itemcd = NULL;
        }
        if (empty($structcd)){
            $structcd = NULL;
        }
        
        $params = array($itemcd, $structcd, $upduser, $_SERVER['REMOTE_ADDR'], $updcnt);
        $sql  = "SELECT * FROM f_item_struct_update(3, ?, ?, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, ?, ?, ?);";
        $res = $db->getRow($sql, $params);
        
        if (DB::isError($res) || $res["errcd"] < 0) {
            $db->rollback();
            $db->query("ROLLBACK;");
            $log->error("rollback : ".$res->userinfo, $func_name);
            
            return $res;
        }
        
        $db->commit();
        $db->query("COMMIT;");
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        
        return true;
    }
    
    
}
?>
