<?php
class Table_ProductInventory
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ProductInventory();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ProductInventory();
    }

    /**
     * @param $row
     * @return mixed
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "pi_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function delete($value, $field = "pi_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'pi_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getByWhProduct($warehouseId=0, $productId =0, $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("product_id = ?", $productId);
        $select->where("warehouse_id = ?", $warehouseId);        
        return $this->_table->getAdapter()->fetchRow($select);
    }
    
    public function getForUpdate($pi_id) {
    	$sql = 'select * from product_inventory where pi_id='.$pi_id.' for update;';
    	return $this->_table->getAdapter()->fetchRow($sql);
    }

    public function getAll()
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, "*");
        return $this->_table->getAdapter()->fetchAll($select);
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table.' as pi', $type);
        $select->where("1 =?", 1);
        $select->joinLeft('product as p','p.product_id=pi.product_id',"");
        /*CONDITION_START*/
        
        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("pi.product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("pi.product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"] != ""){
            $select->where("pi.warehouse_id = ?",$condition["warehouse_id"]);
        }
        if(isset($condition["pi_onway"]) && $condition["pi_onway"] != ""){
            $select->where("pi.pi_onway = ?",$condition["pi_onway"]);
        }
        if(isset($condition["pi_pending"]) && $condition["pi_pending"] != ""){
            $select->where("pi.pi_pending = ?",$condition["pi_pending"]);
        }
        if(isset($condition["pi_sellable"]) && $condition["pi_sellable"] != ""){
            $select->where("pi.pi_sellable = ?",$condition["pi_sellable"]);
        }
        if(isset($condition["pi_unsellable"]) && $condition["pi_unsellable"] != ""){
            $select->where("pi.pi_unsellable = ?",$condition["pi_unsellable"]);
        }
        if(isset($condition["pi_reserved"]) && $condition["pi_reserved"] != ""){
            $select->where("pi.pi_reserved = ?",$condition["pi_reserved"]);
        }
        if(isset($condition["pi_shipped"]) && $condition["pi_shipped"] != ""){
            $select->where("pi.pi_shipped = ?",$condition["pi_shipped"]);
        }
        if(isset($condition["pi_hold"]) && $condition["pi_hold"] != ""){
            $select->where("pi.pi_hold = ?",$condition["pi_hold"]);
        }
		
        if(isset($condition["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("pi.customer_id = ?",$condition["customer_id"]);
        }

        if(isset($condition["product_id_array"]) && $condition["product_id_array"] != "" && is_array($condition["product_id_array"])){
            $select->where("pi.product_id IN(?)",$condition["product_id_array"]);
        }
        
        if(isset($condition["goods_id"]) && $condition["goods_id"] != ""){
            $select->where("p.goods_id = ?",$condition["goods_id"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

    public function getJoinProductByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        $select->joinLeft('product','product.product_id=product_inventory.product_id',"");
        /*CONDITION_START*/

        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("product_inventory.product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_inventory.product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"] != ""){
            $select->where("product_inventory.warehouse_id = ?",$condition["warehouse_id"]);
        }
        if(isset($condition["pi_onway"]) && $condition["pi_onway"] != ""){
            $select->where("product_inventory.pi_onway = ?",$condition["pi_onway"]);
        }
        if(isset($condition["pi_pending"]) && $condition["pi_pending"] != ""){
            $select->where("product_inventory.pi_pending = ?",$condition["pi_pending"]);
        }
        if(isset($condition["pi_sellable"]) && $condition["pi_sellable"] != ""){
            $select->where("product_inventory.pi_sellable = ?",$condition["pi_sellable"]);
        }
        if(isset($condition["pi_unsellable"]) && $condition["pi_unsellable"] != ""){
            $select->where("product_inventory.pi_unsellable = ?",$condition["pi_unsellable"]);
        }
        if(isset($condition["pi_reserved"]) && $condition["pi_reserved"] != ""){
            $select->where("product_inventory.pi_reserved = ?",$condition["pi_reserved"]);
        }
        if(isset($condition["pi_shipped"]) && $condition["pi_shipped"] != ""){
            $select->where("product_inventory.pi_shipped = ?",$condition["pi_shipped"]);
        }
        if(isset($condition["pi_hold"]) && $condition["pi_hold"] != ""){
            $select->where("product_inventory.pi_hold = ?",$condition["pi_hold"]);
        }

        if(isset($condition["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("product_inventory.customer_id = ?",$condition["customer_id"]);
        }

        if(isset($condition["product_id_array"]) && $condition["product_id_array"] != "" && is_array($condition["product_id_array"])){
            $select->where("product_id IN(?)",$condition["product_id_array"]);
        }
        if(isset($condition['goods_id'])&&$condition['goods_id']!=""){
            $select->where("product.goods_id = ?",$condition["goods_id"]);
        }
        
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getLeftJoinByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('product', 'product.product_id='.$table.'.product_id',array('product_sku','product_title','goods_id'));
        $select->joinLeft('customer', 'customer.customer_id='.$table.'.customer_id',array('customer_code'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("product_inventory.product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_inventory.product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"] != ""){
            $select->where("warehouse_id = ?",$condition["warehouse_id"]);
        }
        if(isset($condition["pi_onway"]) && $condition["pi_onway"] != ""){
            $select->where("pi_onway = ?",$condition["pi_onway"]);
        }
        if(isset($condition["pi_pending"]) && $condition["pi_pending"] != ""){
            $select->where("pi_pending = ?",$condition["pi_pending"]);
        }
        if(isset($condition["pi_sellable"]) && $condition["pi_sellable"] != ""){
            $select->where("pi_sellable = ?",$condition["pi_sellable"]);
        }
        if(isset($condition["pi_unsellable"]) && $condition["pi_unsellable"] != ""){
            $select->where("pi_unsellable = ?",$condition["pi_unsellable"]);
        }
        if(isset($condition["pi_reserved"]) && $condition["pi_reserved"] != ""){
            $select->where("pi_reserved = ?",$condition["pi_reserved"]);
        }
        if(isset($condition["pi_shipped"]) && $condition["pi_shipped"] != ""){
            $select->where("pi_shipped = ?",$condition["pi_shipped"]);
        }
        if(isset($condition["pi_hold"]) && $condition["pi_hold"] != ""){
            $select->where("pi_hold = ?",$condition["pi_hold"]);
        }

        if(isset($condition["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("product_inventory.customer_id = ?",$condition["customer_id"]);
        }
        if(isset($condition["customer_codes"]) && $condition["customer_codes"] != ""){
            $select->where("customer.customer_code IN(?)",$condition["customer_codes"]);
        }
        if(isset($condition["product_id_array"]) && $condition["product_id_array"] != "" && is_array($condition["product_id_array"])){
            $select->where("product_inventory.product_id IN(?)",$condition["product_id_array"]);
        }
       
        /*CONDITION_END*/
        if ('count(*)' == $type) { 
            return $this->_table->getAdapter()->fetchOne($select);
        } else { 
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
}