<?php

class Table_BilPayment
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BilPayment();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BilPayment();
    }

    /**
     * @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 = "bp_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 = "bp_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 = 'bp_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);
    }

    /**
     * @desc 获取服务商未出帐总费用
     * @param string $sp_code
     * @param int $status
     * @return mixed
     */
    public function getTotal($sp_code = '', $status = 2)
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $colums = "sum(bp_amount) amount";
        $select->from($table, $colums);
        $select->where("bp_status =?", $status);
        $select->where("sp_code = ?", $sp_code);
        return $this->_table->getAdapter()->fetchOne($select);
    }


    public function getByFeeIdTotal($bsbdf_id = '')
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $colums = "sum(bp_amount) amount";
        $select->from($table, $colums);
        $select->where("bsbdf_id =?", $bsbdf_id);
        return $this->_table->getAdapter()->fetchOne($select);
    }

    public function getByJoinInnerFeeIdTotal($value, $field = 'bsbd_id')
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $colums = "sum(bp_amount) amount";
        $select->from($table, $colums);
        $select->joinInner("bil_serve_bill_detail_fee", "bil_serve_bill_detail_fee.bsbdf_id=bil_payment.bsbdf_id",null);
        $select->where("bil_serve_bill_detail_fee.{$field} =?", $value);
        return $this->_table->getAdapter()->fetchOne($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["sp_code"]) && $condition["sp_code"] != "") {
            $select->where("sp_code = ?", $condition["sp_code"]);
        }
        if (isset($condition["bb_id"]) && $condition["bb_id"] != "") {
            $select->where("bb_id = ?", $condition["bb_id"]);
        }
        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["ut_code"]) && $condition["ut_code"] != "") {
            $select->where("ut_code = ?", $condition["ut_code"]);
        }
        if (isset($condition["bp_creator_id"]) && $condition["bp_creator_id"] != "") {
            $select->where("bp_creator_id = ?", $condition["bp_creator_id"]);
        }
        if (isset($condition["bsbdf_id"]) && $condition["bsbdf_id"] != "") {
            $select->where("bsbdf_id = ?", $condition["bsbdf_id"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["bp_amount"]) && $condition["bp_amount"] != "") {
            $select->where("bp_amount = ?", $condition["bp_amount"]);
        }
        if (isset($condition["currency_code"]) && $condition["currency_code"] != "") {
            $select->where("currency_code = ?", $condition["currency_code"]);
        }
        if (isset($condition["currency_rate"]) && $condition["currency_rate"] != "") {
            $select->where("currency_rate = ?", $condition["currency_rate"]);
        }
        if (isset($condition["bp_status"]) && $condition["bp_status"] != "") {
            $select->where("bp_status = ?", $condition["bp_status"]);
        }

        //数组
        if (isset($condition["bp_status_arr"]) && !empty($condition["bp_status_arr"]) && is_array($condition["bp_status_arr"])) {
            $select->where("bp_status in(?)", $condition["bp_status_arr"]);
        }
        if (isset($condition["bp_create_type_arr"]) && !empty($condition["bp_create_type_arr"]) && is_array($condition["bp_create_type_arr"])) {
            $select->where("bp_create_type in(?)", $condition["bp_create_type_arr"]);
        }
        /*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);
        }
    }


    /**
     * @desc 查看应付费用
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getNotAuditFeesByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("service_provider", "service_provider.sp_code=bil_payment.sp_code", array("currency_code as sp_currency_code", 'sp_name', 'sp_settlement_type'));
        $select->joinLeft("bil_business", "bil_business.bb_id=bil_payment.bb_id", array("ds_code", "warehouse_id", "bb_sales_id", "bb_status"));
        $select->joinLeft("bil_business_attach", "bil_business.bb_id=bil_business_attach.bb_id", array("bb_refer_code", "bb_reference_no", "bb_service_number"));
        $select->joinLeft("fee_type", "fee_type.ft_code=bil_payment.ft_code", array("ft_name_cn"));
        $select->joinLeft("user as u1", "u1.user_id=bil_payment.bp_creator_id", array("u1.user_name as creator"));
        $select->joinLeft("user as u2", "u2.user_id=bil_payment.bp_verifier_id", array("u2.user_name as verifier"));
        $select->where("1 =?", 1);

        /*CONDITION_START*/
        if (isset($condition["bb_refer_code"]) && $condition["bb_refer_code"] != "") {
            $select->where("bil_business_attach.bb_refer_code = ?", $condition["bb_refer_code"]);
        }
        if (isset($condition["sp_code"]) && $condition["sp_code"] != "") {
            $select->where("bil_payment.sp_code = ?", $condition["sp_code"]);
        }
        if (isset($condition["bb_id"]) && $condition["bb_id"] != "") {
            $select->where("bb_id = ?", $condition["bb_id"]);
        }
        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["ut_code"]) && $condition["ut_code"] != "") {
            $select->where("ut_code = ?", $condition["ut_code"]);
        }
        if (isset($condition["bp_creator_id"]) && $condition["bp_creator_id"] != "") {
            $select->where("bp_creator_id = ?", $condition["bp_creator_id"]);
        }
        if (isset($condition["bsbdf_id"]) && $condition["bsbdf_id"] != "") {
            $select->where("bsbdf_id = ?", $condition["bsbdf_id"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("bil_business.sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["bp_status"]) && $condition["bp_status"] != "") {
            $select->where("bp_status = ?", $condition["bp_status"]);
        }
        if (isset($condition["bil_payment"]) && $condition["bil_payment"] !== "") {
            $select->where("bil_payment = ?", $condition["bil_payment"]);
        }
        if (isset($condition["bt_code"]) && $condition["bt_code"] != "") {
            $select->where("bil_business.bt_code = ?", $condition["bt_code"]);
        }
        if (isset($condition["ds_code"]) && $condition["ds_code"] != "") {
            $select->where("bil_business.ds_code = ?", $condition["ds_code"]);
        }
        //计费时间
        if (isset($condition["bp_chargeable_time"]) && $condition["bp_chargeable_time"] != "") {
            $select->where("bp_chargeable_time <= ?", $condition["bp_chargeable_time"]);
        }

        if (isset($condition["dateFrom"]) && $condition["dateFrom"] != "") {
            $select->where("bp_add_time >= ?", $condition["dateFrom"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("bp_add_time <= ?", $condition["dateTo"]);
        }

        if (isset($condition["verifyFrom"]) && $condition["verifyFrom"] != "") {
            $select->where("bp_verify_time >= ?", $condition["verifyFrom"]);
        }
        if (isset($condition["verifyTo"]) && $condition["verifyTo"] != "") {
            $select->where("bp_verify_time <= ?", $condition["verifyTo"]);
        }


        /*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);
        }
    }

    /**
     * @desc 查看全部应付未出帐费用
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getNotAuditByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("service_provider", "service_provider.sp_code=bil_payment.sp_code", array("currency_code as sp_currency_code", 'sp_name', 'sp_settlement_type'));
        $select->joinLeft("bil_business", "bil_business.bb_id=bil_payment.bb_id", array("ds_code", "warehouse_id", "bb_sales_id", "bb_status"));
        $select->joinLeft("bil_business_attach", "bil_business.bb_id=bil_business_attach.bb_id", array("bb_refer_code", "bb_reference_no", "bb_service_number"));
        $select->joinLeft("fee_type", "fee_type.ft_code=bil_payment.ft_code", array("ft_name_cn"));
        $select->joinLeft("user as u1", "u1.user_id=bil_payment.bp_creator_id", array("u1.user_name as creator"));
        $select->joinLeft("user as u2", "u2.user_id=bil_payment.bp_verifier_id", array("u2.user_name as verifier"));
        $select->where("1 =?", 1);

        /*CONDITION_START*/

        if (isset($condition["sp_code"]) && $condition["sp_code"] != "") {
            $select->where("bil_payment.sp_code = ?", $condition["sp_code"]);
        }
        if (isset($condition["bb_id"]) && $condition["bb_id"] != "") {
            $select->where("bb_id = ?", $condition["bb_id"]);
        }
        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["ut_code"]) && $condition["ut_code"] != "") {
            $select->where("ut_code = ?", $condition["ut_code"]);
        }
        if (isset($condition["bp_creator_id"]) && $condition["bp_creator_id"] != "") {
            $select->where("bp_creator_id = ?", $condition["bp_creator_id"]);
        }
        if (isset($condition["bsbdf_id"]) && $condition["bsbdf_id"] != "") {
            $select->where("bsbdf_id = ?", $condition["bsbdf_id"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("bil_business.sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["bp_status"]) && $condition["bp_status"] != "") {
            $select->where("bp_status = ?", $condition["bp_status"]);
        }
        if (isset($condition["bil_payment"]) && $condition["bil_payment"] !== "") {
            $select->where("bil_payment = ?", $condition["bil_payment"]);
        }
        if (isset($condition["bt_code"]) && $condition["bt_code"] != "") {
            $select->where("bil_business.bt_code = ?", $condition["bt_code"]);
        }
        if (isset($condition["ds_code"]) && $condition["ds_code"] != "") {
            $select->where("bil_business.ds_code = ?", $condition["ds_code"]);
        }
        //计费时间
        if (isset($condition["bp_chargeable_time"]) && $condition["bp_chargeable_time"] != "") {
            $select->where("bp_chargeable_time <= ?", $condition["bp_chargeable_time"]);
        }

        if (isset($condition["dateFrom"]) && $condition["dateFrom"] != "") {
            $select->where("bp_chargeable_time >= ?", $condition["dateFrom"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("bp_chargeable_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);
        }
    }


    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getGroupByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $types = ($type) == 'count(*)' ? "COUNT(DISTINCT(bil_payment.bb_id))" : $type;
        $select->from($table, $types);
        $select->joinLeft("bil_business", "bil_business.bb_id=bil_payment.bb_id", array("ds_code", "warehouse_id", "bb_sales_id", "bb_status"));
        $select->joinLeft("bil_business_attach", "bil_payment.bb_id=bil_business_attach.bb_id", array("bb_refer_code"));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        //必须为生效
        $select->where("bil_business.bb_status = 'y'");
        //未生成帐单
        $select->where("bil_payment.bsbdf_id = '0'");

        if (isset($condition["bp_status"]) && $condition["bp_status"] != "") {
            $select->where("bil_payment.bp_status = ?", $condition["bp_status"]);
        }

        if (isset($condition["sp_code"]) && $condition["sp_code"] !== "") {
            $select->where("bil_payment.sp_code = ?", $condition["sp_code"]);
        }

        if (isset($condition["sp_code_arr"]) && !empty($condition["sp_code_arr"])) {
            $select->where("bil_payment.sp_code in(?)", $condition["sp_code_arr"]);
        }

        if (isset($condition["bp_create_type"]) && $condition["bp_create_type"] !== "") {
            $select->where("bil_payment.bp_create_type = ?", $condition["bp_create_type"]);
        }

        if (isset($condition["bb_id"]) && $condition["bb_id"] != "") {
            $select->where("bil_payment.bb_id = ?", $condition["bb_id"]);
        }
        if (isset($condition["ds_code"]) && $condition["ds_code"] !== "") {
            $select->where("bil_business.ds_code = ?", $condition["ds_code"]);
        }
        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["sp_code"]) && $condition["sp_code"] != "") {
            $select->where("bil_payment.sp_code = ?", $condition["sp_code"]);
        }
        if (isset($condition["ds_code_arr"]) && !empty($condition["ds_code_arr"]) && is_array($condition["ds_code_arr"])) {
            $select->where("bil_business.ds_code in(?)", $condition["ds_code_arr"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            //费用汇总
            $select->group(array("bil_payment.bb_id"));
            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);
        }
    }


    /**
     * @desc 获取费用单据明细
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getFeeDetailByBusinessCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("service_provider", "service_provider.sp_code=bil_payment.sp_code", array("currency_code as sp_currency_code"));
        $select->joinLeft("fee_type", "fee_type.ft_code=bil_payment.ft_code", array("ft_name_cn"));
        $select->joinLeft("user as u1", "u1.user_id=bil_payment.bp_creator_id", array("u1.user_name as creator"));
        $select->joinLeft("user as u2", "u2.user_id=bil_payment.bp_verifier_id", array("u2.user_name as verifier"));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["bb_id"]) && $condition["bb_id"] !== "") {
            $select->where("bil_payment.bb_id = ?", $condition["bb_id"]);
        }

        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("bil_payment.sm_code = ?", $condition["sm_code"]);
        }

        /*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);
        }
    }


    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getBusinessDetailFeeByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("bil_business", "bil_business.bb_id=bil_payment.bb_id", array("ds_code", "warehouse_id", "bb_sales_id", "bb_status"));
        $select->joinLeft("bil_business_attach", "bil_payment.bb_id=bil_business_attach.bb_id", array("bb_refer_code"));
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if (isset($condition["bp_status"]) && $condition["bp_status"] != "") {
            $select->where("bil_payment.bp_status = ?", $condition["bp_status"]);
        }
        if (isset($condition["bp_create_type"]) && $condition["bp_create_type"] !== "") {
            $select->where("bil_payment.bp_create_type = ?", $condition["bp_create_type"]);
        }
        if (isset($condition["bb_id"]) && $condition["bb_id"] != "") {
            $select->where("bil_payment.bb_id = ?", $condition["bb_id"]);
        }
        if (isset($condition["ds_code"]) && $condition["ds_code"] !== "") {
            $select->where("bil_business.ds_code = ?", $condition["ds_code"]);
        }
        if (isset($condition["bb_status"]) && $condition["bb_status"] !== "") {
            $select->where("bil_business.bb_status = ?", $condition["bb_status"]);
        }
        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["sp_code"]) && $condition["sp_code"] != "") {
            $select->where("bil_payment.sp_code = ?", $condition["sp_code"]);
        }
        if (isset($condition["bb_refer_code"]) && $condition["bb_refer_code"] != "") {
            $select->where("bil_business_attach.bb_refer_code = ?", $condition["bb_refer_code"]);
        }
        if (isset($condition["ds_code_arr"]) && !empty($condition["ds_code_arr"]) && is_array($condition["ds_code_arr"])) {
            $select->where("bil_business.ds_code in(?)", $condition["ds_code_arr"]);
        }

        /*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);
        }
    }


    /**
     * @desc 获取对帐单费用单据明细
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getBillFeeDetailByBusinessCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("service_provider", "service_provider.sp_code=bil_payment.sp_code", array("currency_code as sp_currency_code"));
        $select->joinLeft("fee_type", "fee_type.ft_code=bil_payment.ft_code", array("ft_name_cn"));
        $select->joinLeft("user as u1", "u1.user_id=bil_payment.bp_creator_id", array("u1.user_name as creator"));
        $select->joinLeft("user as u2", "u2.user_id=bil_payment.bp_verifier_id", array("u2.user_name as verifier"));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["bb_id"]) && $condition["bb_id"] !== "") {
            $select->where("bil_payment.bb_id = ?", $condition["bb_id"]);
        }

        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["bsbdf_id"]) && $condition["bsbdf_id"] !== "") {
            $select->where("bil_payment.bsbdf_id = ?", $condition["bsbdf_id"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("bil_payment.sm_code = ?", $condition["sm_code"]);
        }

        /*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);
        }
    }


    /**
     * @desc 应付费用
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getPaymentCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("service_provider", "service_provider.sp_code=bil_payment.sp_code", array("currency_code as sp_currency_code"));
        $select->joinLeft('bil_business','bil_business.bb_id=bil_payment.bb_id',array('ds_code'));
        $select->joinLeft("fee_type", "fee_type.ft_code=bil_payment.ft_code", array("ft_name_cn"));
        $select->joinLeft("import_data_detail", "import_data_detail.bp_id=bil_payment.bp_id", array("idd_id", 'id_id'));
        $select->joinLeft("user as u1", "u1.user_id=bil_payment.bp_creator_id", array("u1.user_name as creator"));
        $select->joinLeft("user as u2", "u2.user_id=bil_payment.bp_verifier_id", array("u2.user_name as verifier"));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["bb_id"]) && $condition["bb_id"] !== "") {
            $select->where("bil_payment.bb_id = ?", $condition["bb_id"]);
        }

        if (isset($condition["id_id_is_null"]) && $condition["id_id_is_null"] !== "") {
            $select->where("import_data_detail.id_id is null");
        }

        if (isset($condition["id_id"]) && $condition["id_id"] !== "") {
            $select->where("import_data_detail.id_id = ?", $condition["id_id"]);
        }
        if (isset($condition["ds_code"]) && $condition["ds_code"] !== "") {
            $select->where("bil_business.ds_code = ?", $condition["ds_code"]);
        }
        if (isset($condition["sp_code"]) && $condition["sp_code"] != "") {
            $select->where("bil_payment.sp_code = ?", $condition["sp_code"]);
        }
        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("bil_payment.sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["bp_status"]) && $condition["bp_status"] != "") {
            $select->where("bil_payment.bp_status = ?", $condition["bp_status"]);
        }
        if (isset($condition["dateFrom"]) && $condition["dateFrom"] != "") {
            $select->where("bil_payment.bp_chargeable_time >= ?", $condition["dateFrom"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("bil_payment.bp_chargeable_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);
        }
    }


    public function getPaymentListCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("bil_business", "bil_business.bb_id=bil_payment.bb_id", array("ds_code", "warehouse_id", "bb_sales_id", "bb_status"));
        $select->joinLeft("bil_business_attach", "bil_payment.bb_id=bil_business_attach.bb_id", array("bb_refer_code"));
        $select->joinLeft("import_data_detail", "import_data_detail.bp_id=bil_payment.bp_id", array("idd_id", 'id_id'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["bb_id"]) && $condition["bb_id"] !== "") {
            $select->where("bil_payment.bb_id = ?", $condition["bb_id"]);
        }
        if (isset($condition["bb_refer_code"]) && $condition["bb_refer_code"] !== "") {
            $select->where("bil_business_attach.bb_refer_code = ?", $condition["bb_refer_code"]);
        }
        if (isset($condition["bb_service_number_arr"]) && $condition["bb_service_number_arr"] !== "") {
            $select->where("bil_business_attach.bb_service_number in(?)", $condition["bb_service_number_arr"]);
        }
        if (isset($condition["bb_service_number"]) && $condition["bb_service_number"] !== "") {
            $select->where("bil_business_attach.bb_service_number = ?", $condition["bb_service_number"]);
        }
        if (isset($condition["id_id_is_null"]) && $condition["id_id_is_null"] !== "") {
            $select->where("import_data_detail.id_id is null");
        }
        if (isset($condition["id_id"]) && $condition["id_id"] !== "") {
            $select->where("import_data_detail.id_id = ?", $condition["id_id"]);
        }
        if (isset($condition["ft_code"]) && $condition["ft_code"] !== "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["bp_status"]) && $condition["bp_status"] != "") {
            $select->where("bil_payment.bp_status = ?", $condition["bp_status"]);
        }
        //小于
        if (isset($condition["bp_chargeable_time_lt"]) && $condition["bp_chargeable_time_lt"] != "") {
            $select->where("bil_payment.bp_chargeable_time <= ?", $condition["bp_chargeable_time_lt"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("bil_payment.bp_chargeable_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);
        }
    }


    /**
     * @desc 获取对帐单费用单据明细 根据帐单明细ID
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getBillFeeDetailByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("service_provider", "service_provider.sp_code=bil_payment.sp_code", array("currency_code as sp_currency_code"));
        $select->joinLeft("fee_type", "fee_type.ft_code=bil_payment.ft_code", array("ft_name_cn"));
        $select->joinLeft("bil_serve_bill_detail_fee", "bil_serve_bill_detail_fee.bsbdf_id=bil_payment.bsbdf_id", array("bsbdf_amount"));
        $select->joinLeft("bil_business", "bil_business.bb_id=bil_payment.bb_id", array("ds_code", "warehouse_id", "bb_sales_id", "bb_status"));
        $select->joinLeft("bil_business_attach", "bil_payment.bb_id=bil_business_attach.bb_id", array("bb_refer_code"));
        $select->joinLeft("user as u1", "u1.user_id=bil_payment.bp_creator_id", array("u1.user_name as creator"));
        $select->joinLeft("user as u2", "u2.user_id=bil_payment.bp_verifier_id", array("u2.user_name as verifier"));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["bb_id"]) && $condition["bb_id"] !== "") {
            $select->where("bil_payment.bb_id = ?", $condition["bb_id"]);
        }

        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["bsbdf_id"]) && $condition["bsbdf_id"] !== "") {
            $select->where("bil_payment.bsbdf_id = ?", $condition["bsbdf_id"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("bil_payment.sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["bsbd_id"]) && $condition["bsbd_id"] != "") {
            $select->where("bil_serve_bill_detail_fee.bsbd_id = ?", $condition["bsbd_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);
        }
    }


    /**
     * @desc 查看应付费用(按服务商汇总)
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getNotAuditFeesGroupByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $types = $type == 'count(*)' ? "COUNT(DISTINCT(bil_payment.sp_code))" : $type;
        $select->from($table, $types);
        $select->joinLeft("service_provider", "service_provider.sp_code=bil_payment.sp_code", array("currency_code as sp_currency_code", 'sp_name', 'sp_settlement_type'));
        $select->joinLeft("bil_business", "bil_business.bb_id=bil_payment.bb_id", array("ds_code", "warehouse_id", "bb_sales_id", "bb_status"));
        $select->where("1 =?", 1);
        //必须为生效
        $select->where("bil_business.bb_status = 'y'");
        //未生成帐单
        $select->where("bil_payment.bsbdf_id = '0'");
        //已审核状态
        $select->where("bil_payment.bp_status = '2'");
        /*CONDITION_START*/
        if (isset($condition["bb_refer_code"]) && $condition["bb_refer_code"] != "") {
            $select->where("bil_business_attach.bb_refer_code = ?", $condition["bb_refer_code"]);
        }
        if (isset($condition["sp_code"]) && $condition["sp_code"] != "") {
            $select->where("bil_payment.sp_code = ?", $condition["sp_code"]);
        }
        if (isset($condition["bb_id"]) && $condition["bb_id"] != "") {
            $select->where("bb_id = ?", $condition["bb_id"]);
        }
        if (isset($condition["ft_code"]) && $condition["ft_code"] != "") {
            $select->where("bil_payment.ft_code = ?", $condition["ft_code"]);
        }
        if (isset($condition["ut_code"]) && $condition["ut_code"] != "") {
            $select->where("ut_code = ?", $condition["ut_code"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("bil_business.sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["bil_payment"]) && $condition["bil_payment"] !== "") {
            $select->where("bil_payment = ?", $condition["bil_payment"]);
        }
        if (isset($condition["bt_code"]) && $condition["bt_code"] != "") {
            $select->where("bil_business.bt_code = ?", $condition["bt_code"]);
        }
        if (isset($condition["ds_code"]) && $condition["ds_code"] != "") {
            $select->where("bil_business.ds_code = ?", $condition["ds_code"]);
        }
        //计费时间
        if (isset($condition["bp_chargeable_time"]) && $condition["bp_chargeable_time"] != "") {
            $select->where("bp_chargeable_time <= ?", $condition["bp_chargeable_time"]);
        }

        if (isset($condition["dateFrom"]) && $condition["dateFrom"] != "") {
            $select->where("bp_chargeable_time >= ?", $condition["dateFrom"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("bp_chargeable_time <= ?", $condition["dateTo"]);
        }
        //费用汇总
        $select->group(array("bil_payment.sp_code"));

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return count($this->_table->getAdapter()->fetchAll($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);
        }
    }

}