<?php
class Table_FeeCreditNote
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_FeeCreditNote();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_FeeCreditNote();
    }

    /**
     * @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 = "fcn_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 = "fcn_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 = 'fcn_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["payer_code"]) && $condition["payer_code"] != ""){
            $select->where("payer_code = ?",$condition["payer_code"]);
        }
        if(isset($condition["so_code"]) && $condition["so_code"] != ""){
            $select->where("so_code = ?",$condition["so_code"]);
        }
        if(isset($condition["ft_code"]) && $condition["ft_code"] != ""){
            $select->where("ft_code = ?",$condition["ft_code"]);
        }
        if(isset($condition["fcn_code"]) && $condition["fcn_code"] != ""){
            $select->where("fcn_code = ?",$condition["fcn_code"]);
        }
        if(isset($condition["fcn_reference_no"]) && $condition["fcn_reference_no"] != ""){
            $select->where("fcn_reference_no = ?",$condition["fcn_reference_no"]);
        }
        if(isset($condition["fcn_status"]) && $condition["fcn_status"] != ""){
            $select->where("fcn_status = ?",$condition["fcn_status"]);
        }
        if(isset($condition["fcn_amount"]) && $condition["fcn_amount"] != ""){
            $select->where("fcn_amount = ?",$condition["fcn_amount"]);
        }
        if(isset($condition["fcn_currency_code"]) && $condition["fcn_currency_code"] != ""){
            $select->where("fcn_currency_code = ?",$condition["fcn_currency_code"]);
        }
        if(isset($condition["fcn_desc"]) && $condition["fcn_desc"] != ""){
            $select->where("fcn_desc = ?",$condition["fcn_desc"]);
        }
        if(isset($condition["fcn_note"]) && $condition["fcn_note"] != ""){
            $select->where("fcn_note = ?",$condition["fcn_note"]);
        }
        if(isset($condition["fcn_creator_id"]) && $condition["fcn_creator_id"] != ""){
            $select->where("fcn_creator_id = ?",$condition["fcn_creator_id"]);
        }
        if(isset($condition["fcn_confirm_id"]) && $condition["fcn_confirm_id"] != ""){
            $select->where("fcn_confirm_id = ?",$condition["fcn_confirm_id"]);
        }
        if(isset($condition["fcn_delete_id"]) && $condition["fcn_delete_id"] != ""){
            $select->where("fcn_delete_id = ?",$condition["fcn_delete_id"]);
        }
        if(isset($condition["bcb_id"]) && $condition["bcb_id"] != ""){
            $select->where("bcb_id = ?",$condition["bcb_id"]);
        }
        if(isset($condition["ftr_id"]) && $condition["ftr_id"] != ""){
            $select->where("ftr_id = ?",$condition["ftr_id"]);
        }
        /*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 getAllByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("fee_type", "fee_type.ft_code=fee_credit_note.ft_code",array("ft_name_cn"));
        $select->joinLeft("user as u1", "u1.user_id=fee_credit_note.fcn_creator_id",array("u1.user_name as creatorName"));
        $select->joinLeft("user as u2", "u2.user_id=fee_credit_note.fcn_confirm_id",array("u2.user_name as confirmName"));
        $select->joinLeft("user as u3", "u3.user_id=fee_credit_note.fcn_delete_id",array("u3.user_name as deleteName"));
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if(isset($condition["payer_code"]) && $condition["payer_code"] != ""){
            $select->where("payer_code = ?",$condition["payer_code"]);
        }
        if(isset($condition["so_code"]) && $condition["so_code"] != ""){
            $select->where("so_code = ?",$condition["so_code"]);
        }
        if(isset($condition["ft_code"]) && $condition["ft_code"] != ""){
            $select->where("ft_code = ?",$condition["ft_code"]);
        }
        if(isset($condition["fcn_code"]) && $condition["fcn_code"] != ""){
            $select->where("fcn_code = ?",$condition["fcn_code"]);
        }
        if(isset($condition["fcn_reference_no"]) && $condition["fcn_reference_no"] != ""){
            $select->where("fcn_reference_no = ?",$condition["fcn_reference_no"]);
        }
        if(isset($condition["fcn_status"]) && $condition["fcn_status"] != ""){
            $select->where("fcn_status = ?",$condition["fcn_status"]);
        }
        if(isset($condition["fcn_amount"]) && $condition["fcn_amount"] != ""){
            $select->where("fcn_amount = ?",$condition["fcn_amount"]);
        }
        if(isset($condition["fcn_currency_code"]) && $condition["fcn_currency_code"] != ""){
            $select->where("fcn_currency_code = ?",$condition["fcn_currency_code"]);
        }
        if(isset($condition["fcn_desc"]) && $condition["fcn_desc"] != ""){
            $select->where("fcn_desc = ?",$condition["fcn_desc"]);
        }
        if(isset($condition["fcn_note"]) && $condition["fcn_note"] != ""){
            $select->where("fcn_note = ?",$condition["fcn_note"]);
        }
        if(isset($condition["fcn_creator_id"]) && $condition["fcn_creator_id"] != ""){
            $select->where("fcn_creator_id = ?",$condition["fcn_creator_id"]);
        }
        if(isset($condition["fcn_confirm_id"]) && $condition["fcn_confirm_id"] != ""){
            $select->where("fcn_confirm_id = ?",$condition["fcn_confirm_id"]);
        }
        if(isset($condition["fcn_delete_id"]) && $condition["fcn_delete_id"] != ""){
            $select->where("fcn_delete_id = ?",$condition["fcn_delete_id"]);
        }
        if(isset($condition["bcb_id"]) && $condition["bcb_id"] != ""){
            $select->where("bcb_id = ?",$condition["bcb_id"]);
        }
        if(isset($condition["ftr_id"]) && $condition["ftr_id"] != ""){
            $select->where("ftr_id = ?",$condition["ftr_id"]);
        }

        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("fcn_add_time >= ?",$condition["dateFor"]);
        }

        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("fcn_add_time <= ?",$condition["dateTo"]);
        }


        /*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);
        }
    }
}