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


/**
 * キャストマスタテーブル関連
 *
 * @package     Onedepo.Db.Untansdetail
 * @author      fuco T.Sakai<sakai@fuco.jp>
 * @access      public
 */
class Onedepo_Db_Untransdetail
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess;
    
    
    //受注明細T,出品在庫明細T,
    //引数＝受注番号
    //受注行順に出していく
    //種別=1(売り上げ)
    //出品番号＝stockcd?（在庫コード
    //タイトル、数量は言うまでもなし
    
    //購入情報のとこ
    //受注番号は流用
    //評価、コメント、購入者＝出品者評価T
    //購入者名を取るときに会員基本属性Tからメアドも一緒にとると良い
    //購入日＝受注見出しTの受注日？
    //送付先情報のとこ
    //受注見出しTからもろにもらえる
    
    //出品番号はt_ex_stock_detailの品目Cと構成Cと在庫Cを連結
    
    /**
     * 条件取得
     * 
     * @param $orderno
     * @return object 取得レコード
     */   
    function selectItemlist($orderno)
    {
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $log =& LogFactory::getLog();
        
        $params = array($orderno);
        
        $sql  = "SELECT odr.orderno, odr.orderrowno, odr.itemcd, odr.structcd, odr.stockcd, itm.itemnm, odr.count ";
        $sql .= "FROM t_order_detail odr ";
		$sql .= "    INNER JOIN t_item_basic_attr itm ";
		$sql .= "        ON  odr.itemcd = itm.itemcd ";
		$sql .= "        AND itm.delflg = false ";
		$sql .= "    LEFT  JOIN t_forward_detail fd ";
		$sql .= "        ON  odr.orderno    = fd.orderno ";
		$sql .= "        AND odr.orderrowno = fd.orderrowno ";
		$sql .= "        AND fd.delflg    = false ";
        $sql .= "WHERE odr.orderno = ? ";
        $sql .= "AND   odr.typediv = 1 ";
        $sql .= "AND  (fd.forwardstate < 4 OR fd.orderno IS NULL) ";
        $sql .= "AND   odr.delflg  = false ";
        $sql .= "ORDER BY odr.orderrowno ";
        /*
        $sql  = "SELECT itm.itemnm, odr.count, exs.itemcd, exs.structcd, exs.stockcd "; 
        $sql .= "FROM t_order_detail odr, t_item_basic_attr itm, t_ex_stock_detail exs ";
        $sql .= "WHERE odr.orderno = ? ";
        $sql .= "AND odr.typediv = 1 ";
        $sql .= "AND itm.itemcd = odr.itemcd ";
        $sql .= "AND itm.itemcd = exs.itemcd ";
        $sql .= "ORDER BY orderrowno;";
        */
        $log->debug($sql, "Onedepo_Db_Untansdetail#selectItemlist");
        $data = $db->getAll($sql, $params);
        
        $this->dbaccess->doClose($db);
        
        return $data;
    }
    
    //購入情報、送付先情報取得
    function getInformation($orderno)
    {
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        $log =& LogFactory::getLog();
        
        $params = array($orderno);
        
        //なんかsumがうまくいかないので、価格の合計は自力でやる。
        $sql  = "SELECT odt.orderdate, odt.orderno, mbr.lastnm, mbr.firstnm, odd.price, slv.valuation, slv.comment, ";
        $sql .= "odt.sendpostalcd, prf.prefnm, odt.sendaddr1, odt.sendaddr2, odt.sendtelno1, odt.addressee, mbr.mailaddr ";
        $sql .= "FROM t_order_detail odd "; 
        $sql .= "    INNER JOIN t_order_title odt ";
        $sql .= "        ON  odd.orderno = odt.orderno ";
        $sql .= "        AND odt.delflg  = false ";
        $sql .= "    LEFT JOIN t_seller_val slv ";
        $sql .= "        ON  odd.orderno = slv.orderno ";
        $sql .= "        AND slv.delflg  = false ";
        $sql .= "    LEFT JOIN t_member_basic_attr mbr ";
        $sql .= "        ON  odt.ordermember = mbr.membercd ";
        $sql .= "        AND mbr.delflg      = false ";
        $sql .= "    INNER JOIN t_pref prf ";
        $sql .= "        ON  odt.sendprefcd = prf.prefcd ";
        $sql .= "        AND prf.delflg     = false ";
        $sql .= "    LEFT  JOIN t_forward_detail fd ";
        $sql .= "        ON  odd.orderno    = fd.orderno ";
        $sql .= "        AND odd.orderrowno = fd.orderrowno ";
        $sql .= "        AND fd.delflg    = false ";
        $sql .= "WHERE odd.orderno = ? ";
        $sql .= "AND   odd.typediv = 1 ";
        $sql .= "AND  (fd.forwardstate < 4 OR fd.orderno IS NULL) ";
        $sql .= "AND   odd.delflg = false ";

        $sql .= "ORDER BY odd.orderrowno;";
        
        $log->debug($sql, "Onedepo_Db_Untansdetail#getInformation");
        $cnt = $db->getAll($sql, $params);
        
        $this->dbaccess->doClose($db);
        
        return $cnt;
    }
}
?>
