<?php

class Table_AccountPaymentRecords
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_AccountPaymentRecords();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_AccountPaymentRecords();
    }

    /**
     * @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 = "apr_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 = "apr_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 = 'apr_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 getTotal($soCode = 'cm', $payerCode = '', $type = 1)
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $colums = array(
            'sum(apr_amount) as amount',
            'sum(apr_writeoff_amount) as writeoff_amount',
        );
        $select->from($table, $colums);
        $select->where("apr_settle_sign = 'y'");
        $select->where("apr_writeoff_sign = 'n'");
        $select->where("apr_type = ?", $type);
        $select->where("so_code = ?", $soCode);
        $select->where("payer_code = ?", $payerCode);
        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["pm_code"]) && $condition["pm_code"] != "") {
            $select->where("pm_code = ?", $condition["pm_code"]);
        }
        if (isset($condition["apr_type"]) && $condition["apr_type"] != "") {
            $select->where("apr_type = ?", $condition["apr_type"]);
        }
        if (isset($condition["ba_id"]) && $condition["ba_id"] !== "") {
            $select->where("ba_id = ?", $condition["ba_id"]);
        }
        if (isset($condition["ftr_id"]) && $condition["ftr_id"] !== "") {
            $select->where("ftr_id = ?", $condition["ftr_id"]);
        }
        if (isset($condition["apr_id"]) && $condition["apr_id"] !== "") {
            $select->where("apr_id = ?", $condition["apr_id"]);
        }
        if (isset($condition["apr_amount"]) && $condition["apr_amount"] != "") {
            $select->where("apr_amount = ?", $condition["apr_amount"]);
        }
        if (isset($condition["apr_writeoff_amount"]) && $condition["apr_writeoff_amount"] != "") {
            $select->where("apr_writeoff_amount = ?", $condition["apr_writeoff_amount"]);
        }
        if (isset($condition["apr_creator_id"]) && $condition["apr_creator_id"] != "") {
            $select->where("apr_creator_id = ?", $condition["apr_creator_id"]);
        }
        if (isset($condition["bill_id"]) && $condition["bill_id"] != "") {
            $select->where("bill_id = ?", $condition["bill_id"]);
        }
        if (isset($condition["apr_create_type"]) && $condition["apr_create_type"] != "") {
            $select->where("apr_create_type = ?", $condition["apr_create_type"]);
        }
        if (isset($condition["apr_settle_sign"]) && $condition["apr_settle_sign"] != "") {
            $select->where("apr_settle_sign = ?", $condition["apr_settle_sign"]);
        }
        if (isset($condition["apr_writeoff_sign"]) && $condition["apr_writeoff_sign"] != "") {
            $select->where("apr_writeoff_sign = ?", $condition["apr_writeoff_sign"]);
        }
        if (isset($condition["apr_inneroffset_sign"]) && $condition["apr_inneroffset_sign"] != "") {
            $select->where("apr_inneroffset_sign = ?", $condition["apr_inneroffset_sign"]);
        }
        /*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("bank_account", "bank_account.ba_id=account_payment_records.ba_id");
        $select->joinLeft("payment_method", "payment_method.pm_code=account_payment_records.pm_code", array("title"));
        $select->joinLeft("account_payment_records_attach", "account_payment_records_attach.apr_id=account_payment_records.apr_id");
        $select->joinLeft("fee_transfer_records", "fee_transfer_records.ftr_id=account_payment_records.ftr_id", null);

        $select->joinLeft("user as u1", "u1.user_id=account_payment_records.apr_creator_id", array("u1.user_name as creatorName"));


        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if (isset($condition["payer_code"]) && $condition["payer_code"] != "") {
            $select->where("account_payment_records.payer_code = ?", $condition["payer_code"]);
        }
        if (isset($condition["so_code"]) && $condition["so_code"] != "") {
            $select->where("account_payment_records.so_code = ?", $condition["so_code"]);
        }
        if (isset($condition["pm_code"]) && $condition["pm_code"] != "") {
            $select->where("account_payment_records.pm_code = ?", $condition["pm_code"]);
        }
        if (isset($condition["apr_type"]) && $condition["apr_type"] !== "") {
            $select->where("account_payment_records.apr_type = ?", $condition["apr_type"]);
        }
        if (isset($condition["ba_id"]) && $condition["ba_id"] != "") {
            $select->where("account_payment_records.ba_id = ?", $condition["ba_id"]);
        }
        if (isset($condition["ftr_id"]) && $condition["ftr_id"] != "") {
            $select->where("account_payment_records.ftr_id = ?", $condition["ftr_id"]);
        }
        if (isset($condition["apr_amount"]) && $condition["apr_amount"] != "") {
            $select->where("apr_amount = ?", $condition["apr_amount"]);
        }
        if (isset($condition["apr_writeoff_amount"]) && $condition["apr_writeoff_amount"] != "") {
            $select->where("apr_writeoff_amount = ?", $condition["apr_writeoff_amount"]);
        }
        if (isset($condition["apr_currency_code"]) && $condition["apr_currency_code"] != "") {
            $select->where("apr_currency_code = ?", $condition["apr_currency_code"]);
        }
        if (isset($condition["apr_currency_rate"]) && $condition["apr_currency_rate"] != "") {
            $select->where("apr_currency_rate = ?", $condition["apr_currency_rate"]);
        }
        if (isset($condition["apr_note"]) && $condition["apr_note"] != "") {
            $select->where("apr_note = ?", $condition["apr_note"]);
        }
        if (isset($condition["apr_creator_id"]) && $condition["apr_creator_id"] != "") {
            $select->where("apr_creator_id = ?", $condition["apr_creator_id"]);
        }
        if (isset($condition["bill_id"]) && $condition["bill_id"] != "") {
            $select->where("bill_id = ?", $condition["bill_id"]);
        }
        if (isset($condition["apr_create_type"]) && $condition["apr_create_type"] != "") {
            $select->where("apr_create_type = ?", $condition["apr_create_type"]);
        }
        if (isset($condition["apr_settle_sign"]) && $condition["apr_settle_sign"] != "") {
            $select->where("apr_settle_sign = ?", $condition["apr_settle_sign"]);
        }
        if (isset($condition["apr_writeoff_sign"]) && $condition["apr_writeoff_sign"] != "") {
            $select->where("apr_writeoff_sign = ?", $condition["apr_writeoff_sign"]);
        }
        if (isset($condition["apr_inneroffset_sign"]) && $condition["apr_inneroffset_sign"] != "") {
            $select->where("apr_inneroffset_sign = ?", $condition["apr_inneroffset_sign"]);
        }
        if (isset($condition["ftr_transaction_no"]) && $condition["ftr_transaction_no"] != "") {
            $select->where("fee_transfer_records.ftr_transaction_no = ?", $condition["ftr_transaction_no"]);
        }

        if (isset($condition["dateFor"]) && $condition["dateFor"] != "") {
            $select->where("apr_transaction_time >= ?", $condition["dateFor"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("apr_transaction_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 判断是否存在用于内部核销的记录
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getInternalBalanceByCondition($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["apr_type"]) && $condition["apr_type"] != "") {
            $select->where("apr_type = ?", $condition["apr_type"]);
        }
        if (isset($condition["apr_settle_sign"]) && $condition["apr_settle_sign"] != "") {
            $select->where("apr_settle_sign = ?", $condition["apr_settle_sign"]);
        }
        if (isset($condition["apr_writeoff_sign"]) && $condition["apr_writeoff_sign"] != "") {
            $select->where("apr_writeoff_sign = ?", $condition["apr_writeoff_sign"]);
        }
        if (isset($condition["apr_inneroffset_sign"]) && $condition["apr_inneroffset_sign"] != "") {
            $select->where("apr_inneroffset_sign = ?", $condition["apr_inneroffset_sign"]);
        }
        /*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);
        }
    }

}