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


/**
 * 仕入先マスタテーブル関連
 *
 * @package     Onedepo.Db.Supplier
 * @author      netnee T.Otogawa<tomokazu.otogawa@netnee.jp>
 * @access      public
 */
class Onedepo_Db_Supplier
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess = null;

    /**
     * 全件取得
     * 
     * @param string $sort ソートキー
     * @return object 取得レコード
     */
    function selectAll($sort="disppriority")
    {
        $log =& LogFactory::getLog();

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

        $params = array($sort);

        $sql  = "SELECT suppliercd, suppliernm, disppriority ";
        $sql .= "  FROM t_supplier ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= " ORDER BY %s";
        $sql = sprintf($sql, $sort);
        $log->debug($sql, "Onedepo_Db_Supplier#selectAll");

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

        return $data;
    }

    /**
     * 条件取得
     * 
     * @param string $code コード
     * @return object 取得レコード
     */
    function selectKey($code)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Supplier#selectKey";

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

        $params = array($code);
        
        $sql  = "SELECT suppliercd, suppliernm, disppriority, updcnt ";
        $sql .= "  FROM t_supplier ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= "   AND suppliercd = ?;";
        $log->debug($sql, $func_name);
        $log->debug(print_r($params, true), $func_name);

        $data = $db->getRow($sql, $params);
        $log->debug(print_r($data, true), $func_name);
        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * 仕入れ区分の名称取得
     *
     * @param integer $supplidivcd 仕入れ区分コード
     * @return string 仕入れ区分名
     */
    function getSuppliDivName($supplidivcd)
    {
    	$log =& LogFactory::getLog();

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

    	$sql  = "SELECT name ";
    	$sql .= "  FROM t_name ";
    	$sql .= " WHERE namediv=8 AND namecd = $supplidivcd ";
    	$log->debug($sql, "Onedepo_Db_Supplier#selectAll");

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

    	return $data["name"];
    }

    /**
     * サーチ検索
     *
     * @param string  $word 検索ワード
     * @param string  $sort ソートキー
     * @return object 取得レコード
     */
    function selectSearch($word, $sort="disppriority")
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Supplier#selectSearch";

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

        $params = NULL;

        $sql  = "SELECT suppliercd, suppliernm, disppriority, updcnt ";
        $sql .= "  FROM t_supplier ";
        $sql .= " WHERE delflg = FALSE ";

        // 検索ワード
        if (!empty($word)) {
            $sql .= "   AND suppliernm LIKE ? ";
            $params[] = "%". trim($word) . "%";
        }
        
        $sql .= " ORDER BY %s";
        $sql = sprintf($sql, $sort);
        
        $log->debug($sql, $func_name);
        $log->debug(print_r($params, true), $func_name);

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

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

        return $data;
    }

    /**
     * 仕入れ見出しをチェック
     * 
     * @param string $code コード
     * @return object 取得レコード
     */
    function getStockTitleCount($code)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Supplier#getStockTitleCount";

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

        $params = array($code);
        
        $sql  = "SELECT count(stocktitlecd) as count ";
        $sql .= "  FROM t_stock_title ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= "   AND suppliercd = ?;";
        $log->debug($sql, $func_name);
        $log->debug(print_r($params, true), $func_name);

        $data = $db->getOne($sql, $params);
        $log->debug(print_r($data, true), $func_name);
        $this->dbaccess->doClose($db);

        return $data;
    }

    /**
     * 追加
     * 
     * @param string  $name 名称
     * @param integer $dispsort 表示順
     * @param string  $upduser 更新ユーザ
     * @return boolean 結果ステータス
     */
    function insert($name, $dispsort, $upduser) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Supplier#insert";

        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($name, $dispsort, $upduser, $_SERVER['REMOTE_ADDR']);

        $sql  = "select f_supplier_update(1, NULL, ?, ?, ?, ?, NULL);";

        $res = $db->getOne($sql, $params);
        if (DB::isError($res) || $res < 0) {
            $log->debug("rollback  ". $res->userinfo, $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

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

    /**
     * 更新
     *
     * @param string  $code コード
     * @param string  $name 名称
     * @param integer $dispsort 表示順
     * @param integer $updcnt 更新カウンタ
     * @param string  $upduser 更新ユーザ
     * @return boolean 結果ステータス
     */
    function update($code, $name, $dispsort, $updcnt, $upduser) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Supplier#update";

        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($code, $name, $dispsort, $upduser, $_SERVER['REMOTE_ADDR'], $updcnt);

        $sql  = "select f_supplier_update(2, ?, ?, ?, ?, ?, ?);";

        $res = $db->getOne($sql, $params);
        if (DB::isError($res) || $res < 0) {
            $log->debug("rollback  ". $res->userinfo, $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

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

    }

    /**
     * 削除
     *
     * @param string  $code コード
     * @param integer $updcnt 更新カウンタ
     * @param string  $upduser 更新ユーザ
     * @return boolean 結果ステータス
     */
    function delete($code, $updcnt, $upduser) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Supplier#delete";

        if ($this->getStockTitleCount($code) > 0) {
            return false;
        }

        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--

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

        $sql  = "SELECT f_supplier_update(3, ?, NULL, NULL, ?, ?, ?);";

        $res = $db->getOne($sql, $params);
        if (DB::isError($res) || $res < 0) {
            $log->debug("rollback  ". $res->userinfo, $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

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