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

/**
 * 在庫関連
 *
 * @package     Onedepo.Logic.Stock
 * @author      netnee <mail@example.com>
 * @access      public
 */
class Onedepo_Logic_Stock
{
    var $dbaccess;
    var $db_stock;
    
    /**
    * 在庫取得
    * 
    * @return   object
    */
    function searchStock($to, $from, $makercd, $labelcd, $stock, $title, $shopdiv=2)
    {
        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Stock#searchStock");
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $pager = "SELECT DISTINCT on (c.stocktitlecd, e.itemcd, e.structcd) e.itemcd,e.structcd,e.stockcd,c.stocktitlecd,i.itemnm,ma.makernm,la.labelnm,me.name,COALESCE(c.stocktitlecdcont,0) as stocktitlecdcont ";
        $cnt = "SELECT count(s.itemcd) ";
        
        $sql  = " FROM t_item_struct s
                  INNER JOIN  t_item_basic_attr i 
                    ON s.itemcd=i.itemcd AND i.delflg=FALSE
                  INNER JOIN  t_ex_stock_detail e
                    ON e.itemcd=s.itemcd AND e.structcd=s.structcd
                  LEFT JOIN (SELECT t_s.itemcd, t_s.structcd,
                       t_s.stocktitlecd, 
                       COUNT(t_s.itemcd) AS stocktitlecdcont
                       FROM t_ex_stock_detail t_s
                      LEFT JOIN
                       (SELECT * FROM t_order_detail t_o
                         LEFT JOIN t_forward_detail t_f
                         ON t_f.orderno=t_o.orderno
                         AND t_f.orderrowno=t_o.orderrowno
                        WHERE t_f.forwardstate IS NULL 
                        OR NOT (t_f.forwardstate IS NOT NULL 
                        AND (t_f.forwardstate=9 
                        OR (t_f.forwardstate=8 AND t_f.returndate IS NOT NULL)))
                       ) t_o 
                      ON t_o.itemcd = t_s.itemcd 
                      AND t_o.structcd = t_s.structcd 
                      AND t_s.stockcd = t_o.stockcd
                     WHERE t_o.itemcd IS NULL
                     AND   t_s.stockstate = 3
                     AND   t_s.sellercd IS NULL
                     AND   t_s.delflg = false
                    GROUP BY t_s.itemcd, t_s.structcd,t_s.stocktitlecd) c
                    ON c.itemcd=s.itemcd AND c.structcd=s.structcd AND c.stocktitlecd=e.stocktitlecd
                  LEFT JOIN t_name me
                    ON me.namecd = s.mediacd AND me.namediv = 4
                  LEFT JOIN t_maker ma
                    ON i.makercd = ma.makercd
                  LEFT JOIN t_label la
                    ON i.makercd = la.makercd AND i.labelcd = la.labelcd
";

        $where = '';
        
        $params[] = $shopdiv;
        $where .= ' WHERE i.shopdiv = ? ';
        
        if($stock == 0) {
            $where .= " AND COALESCE(c.stocktitlecdcont, 0) = 0";
        }
        
        if($makercd != 0) {
            $params[] = $makercd;
            $where .= ' AND i.makercd = ? ';
        }
        
        if($makercd != 0 && $labelcd != 0) {
            $params[] = $makercd;
            $params[] = $labelcd;
            $where .= ' AND i.makercd = ? AND i.labelcd = ?';
        }
        
        //タイトル(スペース区切りでAND検索)
        if( !empty($title) ) {
            $title = mb_convert_kana($title, "s", "UTF-8");
            $title = explode(' ',$title);
            
            foreach ($title as $value) {
                $params[] = '%'. $value .'%';
                $where .= " AND i.itemnm LIKE ?";
            }
        }
        $sql .= $where;

        $pager .= $sql . " LIMIT ". $to  ." OFFSET ". $from;
        $cnt   .= $sql;

        $data  = $db->getAll($pager, $params);
        $count = $db->getOne($cnt,   $params);
        
        $data += array("recordcount" => $count);
        
        $this->dbaccess->doClose($db);
        return $data;
    }

    /**
    * 在庫数変更
    * 
    * @return   object
    */
    function stockUpdate($itemcd, $structcd, $stockcd, $stockcnt, $user)
    {
        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Stock#stockUpdate");
        $data = $this->db_stock->getStockTilte($itemcd, $structcd, $stockcd);
        
        foreach($data as $key => $value) {
            if(empty($value) )    $data[$key] = NULL;
        }
        
        //在庫状態
        if($stockcnt == 0 ) {
            $stockstate = 5;
        } else {
            $stockstate = 3;
        }
        
        $ret = $this->db_stock->update($itemcd,
                                    $structcd,
                                    $stockcd,
                                    $data['supplidiv'],
                                    $stockcnt,
                                    $data['condition'],
                                    $data['sellingprice'], 
                                    $data['souryouprc'], 
                                    $stockstate, 
                                    $data['scdstartdate'], 
                                    $data['scdcompdate'], 
                                    $data['itemexp'], 
                                    $data['costprice'], 
                                    $user, 
                                    NULL);
        return $ret;
    }
}
?>