<?php
class Table_InventoryBatch
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_InventoryBatch();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_InventoryBatch();
    }

    /**
     * @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 = "ib_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 = "ib_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 = 'ib_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 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, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["lc_code"]) && $condition["lc_code"] != ""){
            $select->where("lc_code = ?",$condition["lc_code"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["reference_no"]) && $condition["reference_no"] != ""){
            $select->where("reference_no = ?",$condition["reference_no"]);
        }
        if(isset($condition["application_code"]) && $condition["application_code"] != ""){
            $select->where("application_code = ?",$condition["application_code"]);
        }
        if(isset($condition["supplier_id"]) && $condition["supplier_id"] != ""){
            $select->where("supplier_id = ?",$condition["supplier_id"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"] != ""){
            $select->where("warehouse_id = ?",$condition["warehouse_id"]);
        }
        if(isset($condition["receiving_code"]) && $condition["receiving_code"] != ""){
            $select->where("receiving_code = ?",$condition["receiving_code"]);
        }
        if(isset($condition["receiving_id"]) && $condition["receiving_id"] != ""){
            $select->where("receiving_id = ?",$condition["receiving_id"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }

		if(isset($condition["receiving_id_array"]) && $condition["receiving_id_array"] != ""){
        	$select->where("receiving_id In(?)",$condition["receiving_id_array"]);
        }	
		
        if(isset($condition["lot_number"]) && $condition["lot_number"] != ""){
            $select->where("lot_number = ?",$condition["lot_number"]);
        }
        if(isset($condition["ib_status"]) && $condition["ib_status"] != ""){
            $select->where("ib_status = ?",$condition["ib_status"]);
        }
        if(isset($condition["ib_hold_status"]) && $condition["ib_hold_status"] !== ""){
            $select->where("ib_hold_status = ?",$condition["ib_hold_status"]);
        }
        if(isset($condition["ib_quantity"]) && $condition["ib_quantity"] != ""){
            $select->where("ib_quantity = ?",$condition["ib_quantity"]);
        }
        if(isset($condition["ib_note"]) && $condition["ib_note"] != ""){
            $select->where("ib_note = ?",$condition["ib_note"]);
        }
		
        if(isset($condition["product_id_array"]) && is_array($condition["product_id_array"]) && $condition["product_id_array"]   ){
            $select->where("product_id IN (?)", $condition["product_id_array"]);	        	
		}
        if(isset($condition["goods_id"]) && $condition["goods_id"] != ""){
            $select->where("goods_id = ?",$condition["goods_id"]);
        }
        if(isset($condition["ib_hold_status_neq"]) && $condition["ib_hold_status_neq"] !== ""){
            $select->where("ib_hold_status != ?", $condition["ib_hold_status_neq"]);
        }
        if(isset($condition["from_product_id"]) && $condition["from_product_id"] !== ""){
            $select->where("from_product_id = ?", $condition["from_product_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);
        }
    }
}