<?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_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["supplier_id"]) && $condition["supplier_id"] != ""){
            $select->where("supplier_id = ?",$condition["supplier_id"]);
        }
        if(isset($condition["receiving_update_user"]) && $condition["receiving_update_user"] != ""){
            $select->where("receiving_update_user = ?",$condition["receiving_update_user"]);
        }
        if(isset($condition["receiving_add_user"]) && $condition["receiving_add_user"] != ""){
            $select->where("receiving_add_user = ?",$condition["receiving_add_user"]);
        }
        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["relate_receiving_code"]) && $condition["relate_receiving_code"] != ""){
            $select->where("receiving_code = ?",$condition["relate_receiving_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["contacter"]) && $condition["contacter"] != ""){
            $select->where("contacter = ?",$condition["contacter"]);
        }
        if(isset($condition['receiving_status_arr'])&&!empty($condition['receiving_status_arr'])){
            $select->where("receiving_status in (?)",$condition['receiving_status_arr']);
        }
        if(isset($condition["contact_phone"]) && $condition["contact_phone"] != ""){
            $select->where("contact_phone = ?",$condition["contact_phone"]);
        }
        if(isset($condition["receiving_description"]) && $condition["receiving_description"] != ""){
            $select->where("receiving_description = ?",$condition["receiving_description"]);
        }
        if(isset($condition["receive_model_type"]) && $condition["receive_model_type"] != ""){
        	$select->where("receive_model_type = ?",$condition["receive_model_type"]);
        }

        if(isset($condition["created_start"]) && $condition["created_start"] != ""){
        	$select->where("receiving_add_time >=?",$condition["created_start"]);
        }

        if(isset($condition["created_end"]) && $condition["created_end"] != ""){
        	$select->where("receiving_add_time <= ?",$condition["created_end"]);
        }	
       	
					
		
		
        /*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);
        }
    }
    
    /**
     * 各仓库状态统计
     * @author solar
     * @param int $customer_id
     * @param int $model_type
     */
    public function stat($customer_id, $model_type) {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, 'warehouse_id,receiving_status,count(*) as num');
    	$select->where('customer_id=?', $customer_id);
    	$select->where("receive_model_type=?", $model_type);
    	$select->group(array('warehouse_id','receiving_status'));
    	return $this->_table->getAdapter()->fetchAll($select);
    }
}