<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * zsb
 *
 * 租售宝系统基本类库
 *
 * @package         zsb
 * @author          EllisLab Dev Team
 * @copyright       Copyright (c) 2006 - 2014, HOUSE365, Inc.
 * @link            http://nj.sell.house.com
 * @since           Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Rent_demand_model CLASS
 *
 * 求租管理类，提供管理求租购信息的方法。
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          sun     
 */
class payment_model  extends MY_Model {
    
    //充值别名
    public $way_alias = 'recharge';
    
    //控制器名
    public $module_controll = 'payment';
    
    //充值银行配置
    public $gate_port = array(
        'zggsyh' => array('image_url' => 'zggsyh.png', 'port' => '0005','newPort' => '700000000000003', 'name' => '中国工商银行'),
        'zgjsyh' => array('image_url' => 'zgjsyh.png', 'port' => '1010','newPort' => '700000000000004', 'name' => '中国建设银行'),
        'zsyh' => array('image_url' => 'zsyh.png',  'port' => '4007','newPort' => '700000000000010', 'value' => 'zsyh', 'name' => '招商银行'),
                /**'zgyzcxyh' => array('image_url' => 'zgyzcxyh.png', 'port' => '0028', 'value' => 'zsyh', 'name' => '中国邮政储蓄银行'),***/
        'zgyh' => array('image_url' => 'zgyh.png', 'port' => '1023','newPort' => '700000000000001', 'value' => 'zgyh', 'name' => '中国银行'),
        'zgnyyh' => array('image_url' => 'zgnyyh.png', 'port' => '4008','newPort' => '700000000000002', 'value' => 'zgnyyh', 'name' => '中国农业银行'),
        'jtyh' => array('image_url' => 'jtyh.png', 'port' => '0025','newPort' => '700000000000006', 'value' => 'jtyh', 'name' => '交通银行'),
        'zgmsyh' => array('image_url' => 'zgmsyh.png', 'port' => '0015','newPort' => '700000000000008', 'value' => 'zgmsyh', 'name' => '中国民生银行'),
        'zxyh' => array('image_url' => 'zxyh.png', 'port' => '0004','newPort' => '700000000000013', 'value' => 'zxyh', 'name' => '中信银行'),
        'xyyh' => array('image_url' => 'xyyh.png', 'port' => '0009','newPort' => '700000000000009', 'value' => 'xyyh', 'name' => '兴业银行'),
        'zggdyh' => array('image_url' => 'zggdyh.png', 'port' => '0027','newPort' => '700000000000005', 'value' => 'zggdyh', 'name' => '中国光大银行'),
        'pfyh' => array('image_url' => 'pfyh.png', 'port' => '1022','newPort' => '700000000000007', 'value' => 'pfyh', 'name' => '浦发银行'),
        'alipay' => array('name' => '支付宝', 'display' => 0),
        'wxpay' => array('name' => '微信', 'display' => 0),
    );
    
    //交易记录TAB
    public $transaction_tab = array(
        'detail' => array('name' => '房豆明细', 'module' => 'transaction_detail'), 
        'expend' => array('name' => '房豆消耗记录', 'module' => 'transaction_expend'), 
        'get' => array('name' => '房豆获取记录', 'module' => 'transaction_get'),
        'recharge' => array('name' => '充值记录', 'module' => 'recharge_record')
    );
    
    //以银行标志组成的一维数组
    private function format_gate_key()
    {
        return array_keys($this->gate_port);
    }
    
    /**
     * 类初始化
     */
    public function __construct() 
    {   
        parent::__construct();
    }
    
    //获取活动
    public function get_recharge_active()
    {
        $paymentRechargeActive = new PaymentRechargeActive();
        return $paymentRechargeActive->getAll();
    }
    
    /**
     * 获取使用银联还是支付宝
     * @param string $bank_name 银行名称
     * @return string 支付接口
     */
    public function get_pay_api($bank_name)
    {
        $gate_key = $this->format_gate_key();
        $pay_api = '';
        if ( in_array( $bank_name,array('wxpay','alipay') ) )
        {
            $pay_api = $bank_name;
        }
        else
        {
            $pay_api = 'chinapay';
        }
        return $pay_api;
    }
    
    /**
     * 检查银行
     * @param string $bank_name 银行名称
     * @return boolean
     */
    public function check_pay_way($bank_name)
    {
        $gate_key = $this->format_gate_key();
        return in_array($bank_name, $gate_key) ?  true : false;
    }
    
    /**
     * 检查选择钱的类型
     * @param array $price 几种钱的套餐
     * @param int $type 类型
     * @return boolean
     */
    public function check_price($price, $type)
    {
        $price_key = array_keys($price);
        return in_array($type, $price_key) ?  true : false;
    }
    
    /**
     * 获取检查后返回的结果字符串
     * @param string $str
     * @return string
     */
    public function get_check_result_str($str) 
    {
        return '<script type="text/javascript">alert("' . $str 
                . '");window.close();</script>';
    }
    
    /**
     * 判断是否参加活动
     * @param date $active_time 活动参加的时间
     * @return boolean true 参加， false 不参加
     */
    public function is_active($active_time) 
    {
        return strtotime($active_time) >= time() ? true : false;
    }
    
    /**
     * 获取经纪人的等级和分值
     * @param array $broker 经纪人数组
     * @return array
     */
    public function get_broker_base($broker) {
        $broker_base = array('id' => $broker['uid'], 
            'groupid' => $broker['groupid']
        );
        $gbe = new GrowthBrokerExp($broker_base);
        $gbc = new GrowthBrokerCredit($broker_base);
        return array('credit' => $gbc->getValidCredit(), 
            'level' => $gbe->getLevel(2));
    }
    
    /**
     * 获取支付中心各模块的url地址
     * @param string 模块功能名称
     */
    public function get_module_url($module = null)
    {
        $url = '/' . $this->module_controll . '/';
        switch ($module)
        {
            case 'recharge': //充值页
                $url .= $module . '/';
                break;
            case 'recharge_record': //充值记录
                $url .= 'transaction/recharge/';
                break;
            case 'transaction_detail': //房豆明细
                $url .= 'transaction/';
                break;
            case 'transaction_expend': //房豆消费
                $url .= 'transaction/expend/';
                break;
            case 'transaction_get': //房豆获取
                $url .= 'transaction/get/';
                break;
            case 'agreement': //支付协议
                $url .= 'agreement/';
                break;
            default: 
        }
        return $url;
    }
   
    //获取交易TAB数据
    public function get_transaction_tab()
    {
		$functionSwitch = $this->config->item('functionSwitch');
        $transaction_tab = $this->transaction_tab;

        foreach($transaction_tab as $k => $v)
        {
			if($k == 'recharge' && empty($functionSwitch['payment']))
			{
				unset($transaction_tab[$k]);
				continue;
			}

            $transaction_tab[$k]['url'] = $this->get_module_url($v['module']);
        }
        return $transaction_tab;
    }
}
