<?php
class Table_PackedRecord {
    protected $_table = null;
    
    public function __construct(){
        $this->_table = new DbTable_PackedRecord();
    }
    
    public function getAdapter(){
        return $this->_table->getAdapter();
    }
    
    public static function getInstance(){
        return new Table_PackedRecord();
    }
    
    public function add($row) {
        return $this->_table->insert($row);
    }
    
    public function updata($row, $value, $field = 'pr_id'){
        $where = $this->_table->getAdapter()->qutoInto("{$field} = ?",$value);
        return $this->_table->update($row,$where);
    }
    
    public function delete($value, $field="pr_id"){
        $where = $this->_table->getAdapter()->qutoInto("{$field} =?",$value);
        return $this->_table->delete($where);
    }
    
    public function getByField($value, $field="pr_id", $colums="*"){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->getAdapter()->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?",$value);
        return $this->_table->getAdapter()->fetchRow($select);
    }
    
    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["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["date_for"]) && $condition["date_for"] != ""){
            $select->where("pr_packing_time >= ?",$condition["date_for"]);
        }
        if(isset($condition["date_to"]) && $condition["date_to"] != ""){
            $select->where("pr_packing_time <= ?",$condition["date_to"]);
        }
        /*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);
        }
    }
}