<?php
class Table_TakeStockItem
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_TakeStockItem();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_TakeStockItem();
    }

    /**
     * @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 = "tsi_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 = "tsi_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 = 'tsi_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);
    }
    
    public function getList($ts_code) {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, "*");
    	$select->where('ts_code=?', $ts_code);
    	return $this->_table->getAdapter()->fetchAll($select);
    }
    
    public function listWithAssignment($tsi_ids) {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table.' as tsi', "*");
    	$select->joinLeft('take_stock_assignment as tsa', 'tsi.tsi_id=tsa.tsi_id', '*');
    	$select->where('tsi.tsi_id IN(?)', $tsi_ids);
    	return $this->_table->getAdapter()->fetchAll($select);
    }
    
    public function listTakeStockItem($condition, $pageSize, $page) {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table.' as tsi', '*');
    	$fields1 = array('tsa1.user_id as user_id1','tsa1.tsa_quantity as tsa_quantity1');
    	$select->joinLeft('take_stock_assignment as tsa1', 'tsi.tsi_id=tsa1.tsi_id and tsa1.tsa_type=1', $fields1);
    	$fields2 = array('tsa2.user_id as user_id2','tsa2.tsa_quantity as tsa_quantity2');
    	$select->joinLeft('take_stock_assignment as tsa2','tsi.tsi_id=tsa2.tsi_id and tsa2.tsa_type=2', $fields2);
    	$select->where("tsi.ts_code=?", $condition['ts_code']);
    	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 countConfirming($ts_code) {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, 'count(*)');
    	$select->where('ts_code=?', $ts_code);
    	$select->where('tsi_status=?', 1);
    	return $this->_table->getAdapter()->fetchOne($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["ts_code"]) && $condition["ts_code"] != ""){
            $select->where("ts_code = ?",$condition["ts_code"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["lc_code"]) && $condition["lc_code"] != ""){
            $select->where("lc_code = ?",$condition["lc_code"]);
        }
        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["tsi_abnormal_type"]) && $condition["tsi_abnormal_type"] != ""){
            $select->where("tsi_abnormal_type = ?",$condition["tsi_abnormal_type"]);
        }
        if(isset($condition["tsi_current_quantity"]) && $condition["tsi_current_quantity"] != ""){
            $select->where("tsi_current_quantity = ?",$condition["tsi_current_quantity"]);
        }
        if(isset($condition["tsi_quantity"]) && $condition["tsi_quantity"] != ""){
            $select->where("tsi_quantity = ?",$condition["tsi_quantity"]);
        }
        if(isset($condition["tsi_note"]) && $condition["tsi_note"] != ""){
            $select->where("tsi_note = ?",$condition["tsi_note"]);
        }
        /*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);
        }
    }
}