<?php
class Table_CustomerOtherFee
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_CustomerOtherFee();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_CustomerOtherFee();
    }

    /**
     * @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 = "cof_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 = "cof_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 = 'cof_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["fee_id"]) && $condition["fee_id"] != ""){
            $select->where("fee_id = ?",$condition["fee_id"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["discount"]) && $condition["discount"] !== ""){
        	$select->where("discount = ?",$condition["discount"]);
        }
        if(isset($condition["status"]) && $condition["status"] !== ""){
        	$select->where("status = ?",$condition["status"]);
        }
        /*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);
        }
    }

    public function leftJoinCustomerByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = ""){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('customer as customer', 'customer.customer_code='.$table.'.customer_code', array('customer.customer_id as customer_id', 'customer.cash_type as cash_type', 'customer.customer_currency as customer_currency'));
        $select->joinLeft('customer_balance as customer_balance', 'customer_balance.customer_code='.$table.'.customer_code');
        $select->joinLeft('other_fee as other_fee', 'other_fee.fee_id='.$table.'.fee_id', array('other_fee.warehouse_id as warehouse_id'));
        $select->where("1 = ?", 1);
        /*CONDITION_START*/
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer.customer_code = ?", $condition["customer_code"]);
        }
        if(isset($condition['customer_other_fee.status']) && !empty($condition['customer_other_fee.status'])){
            $select->where('customer_other_fee.status = ?', $condition['customer_other_fee.status']);
        }
        if(isset($condition['customer_other_fee.fee_value']) && !empty($condition['customer_other_fee.fee_value'])){
            if(isset($condition['customer_other_fee.fee_value']['gt'])){
                $select->where('customer_other_fee.fee_value > ?', $condition['customer_other_fee.fee_value']['gt']);
            }
            if(isset($condition['customer_other_fee.fee_value']['lt']) && !empty($condition['customer_other_fee.fee_value']['lt'])){
                $select->where('customer_other_fee.fee_value < ?', $condition['customer_other_fee.fee_value']['lt']);
            }
        }
        if(isset($condition['other_fee.fee_code']) && !empty($condition['other_fee.fee_code'])){
            $select->where('other_fee.fee_code = ?', $condition['other_fee.fee_code']);
        }
        if(isset($condition['customer.customer_status']) && !empty($condition['customer.customer_status'])){
            $select->where('customer.customer_status = ?', $condition['customer.customer_status']);
        }
        /*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);
        }
    }

}