<?php
class Table_Receiving
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_Receiving();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_Receiving();
    }

    /**
     * @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 = "receiving_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 = "receiving_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 = 'receiving_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['receiving_id'])&&is_array($condition['receiving_id']) && !empty($condition['receiving_id'])){
            $select->where("receiving_id in (?)",$condition['receiving_id']);
        }		
        
        if(isset($condition["receiving_code"]) && $condition["receiving_code"] != ""){
            $select->where("receiving_code = ?",$condition["receiving_code"]);
        }
        if(isset($condition["reference_no"]) && $condition["reference_no"] != ""){
            $select->where("reference_no = ?",$condition["reference_no"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"] != ""){
            $select->where("warehouse_id = ?",$condition["warehouse_id"]);
        }
        if(isset($condition["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["receiving_type"]) && $condition["receiving_type"] != ""){
            $select->where("receiving_type = ?",$condition["receiving_type"]);
        }
        if(isset($condition["receiving_status"]) && $condition["receiving_status"] != ""){
            $select->where("receiving_status = ?",$condition["receiving_status"]);
        }
        if(isset($condition["receive_model_type"]) && $condition["receive_model_type"] != ""){
            $select->where("receive_model_type = ?",$condition["receive_model_type"]);
        }
        if(isset($condition["into_area"]) && $condition["into_area"] !== ""){
            $select->where("into_area = ?",$condition["into_area"]);
        }
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("receiving_add_time >=?",$condition["dateFor"].' 00:00:00');
        }

        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("receiving_add_time <=?",$condition["dateTo"].' 23:59:59');
        }
		
        if(isset($condition['receiving_status_arr'])&&!empty($condition['receiving_status_arr'])){
            $select->where("receiving_status in (?)",$condition['receiving_status_arr']);
        }		
        if(isset($condition["form_type"]) && $condition["form_type"] !== ""){
            $select->where("form_type = ?",$condition["form_type"]);
        }
        if(isset($condition["trade_mode"]) && $condition["trade_mode"] !== ""){
            $select->where("trade_mode = ?",$condition["trade_mode"]);
        }
        if(isset($condition["is_delivery"]) && $condition["is_delivery"] !== ""){
            $select->where("is_delivery = ?",$condition["is_delivery"]);
        }
        /*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);
        }
    }
}